open-nomad/client/fingerprint/memory.go

40 lines
909 B
Go
Raw Normal View History

package fingerprint
import (
"fmt"
"log"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/shirou/gopsutil/mem"
)
// MemoryFingerprint is used to fingerprint the available memory on the node
type MemoryFingerprint struct {
2015-11-05 21:46:02 +00:00
StaticFingerprinter
logger *log.Logger
}
// NewMemoryFingerprint is used to create a Memory fingerprint
func NewMemoryFingerprint(logger *log.Logger) Fingerprint {
f := &MemoryFingerprint{
logger: logger,
}
return f
}
func (f *MemoryFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error {
memInfo, err := mem.VirtualMemory()
if err != nil {
2015-10-07 10:18:19 +00:00
f.logger.Printf("[WARN] Error reading memory information: %s", err)
return err
}
if memInfo.Total > 0 {
resp.Attributes["memory.totalbytes"] = fmt.Sprintf("%d", memInfo.Total)
resp.Resources.MemoryMB = int(memInfo.Total / 1024 / 1024)
}
return nil
}