github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/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  // MemoryFingerprint is used to fingerprint the available memory on the node
    13  type MemoryFingerprint struct {
    14  	StaticFingerprinter
    15  	logger *log.Logger
    16  }
    17  
    18  // NewMemoryFingerprint is used to create a Memory fingerprint
    19  func NewMemoryFingerprint(logger *log.Logger) Fingerprint {
    20  	f := &MemoryFingerprint{
    21  		logger: logger,
    22  	}
    23  	return f
    24  }
    25  
    26  func (f *MemoryFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error {
    27  	memInfo, err := mem.VirtualMemory()
    28  	if err != nil {
    29  		f.logger.Printf("[WARN] Error reading memory information: %s", err)
    30  		return err
    31  	}
    32  
    33  	if memInfo.Total > 0 {
    34  		resp.AddAttribute("memory.totalbytes", fmt.Sprintf("%d", memInfo.Total))
    35  
    36  		resp.Resources = &structs.Resources{
    37  			MemoryMB: int(memInfo.Total / 1024 / 1024),
    38  		}
    39  	}
    40  
    41  	return nil
    42  }