github.com/anuvu/nomad@v0.8.7-atom1/client/fingerprint/memory.go (about) 1 package fingerprint 2 3 import ( 4 "fmt" 5 "log" 6 7 cstructs "github.com/hashicorp/nomad/client/structs" 8 "github.com/hashicorp/nomad/nomad/structs" 9 "github.com/shirou/gopsutil/mem" 10 ) 11 12 const bytesInMB = 1024 * 1024 13 14 // MemoryFingerprint is used to fingerprint the available memory on the node 15 type MemoryFingerprint struct { 16 StaticFingerprinter 17 logger *log.Logger 18 } 19 20 // NewMemoryFingerprint is used to create a Memory fingerprint 21 func NewMemoryFingerprint(logger *log.Logger) Fingerprint { 22 f := &MemoryFingerprint{ 23 logger: logger, 24 } 25 return f 26 } 27 28 func (f *MemoryFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error { 29 var totalMemory int 30 cfg := req.Config 31 if cfg.MemoryMB != 0 { 32 totalMemory = cfg.MemoryMB * bytesInMB 33 } else { 34 memInfo, err := mem.VirtualMemory() 35 if err != nil { 36 f.logger.Printf("[WARN] Error reading memory information: %s", err) 37 return err 38 } 39 if memInfo.Total > 0 { 40 totalMemory = int(memInfo.Total) 41 } 42 } 43 44 if totalMemory > 0 { 45 resp.AddAttribute("memory.totalbytes", fmt.Sprintf("%d", totalMemory)) 46 resp.Resources = &structs.Resources{ 47 MemoryMB: totalMemory / bytesInMB, 48 } 49 } 50 51 return nil 52 }