github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/fingerprint/memory.go (about)

     1  package fingerprint
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/nomad/client/config"
     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(cfg *config.Config, node *structs.Node) (bool, error) {
    27  	memInfo, err := mem.VirtualMemory()
    28  	if err != nil {
    29  		f.logger.Printf("[WARN] Error reading memory information: %s", err)
    30  		return false, err
    31  	}
    32  
    33  	if memInfo.Total > 0 {
    34  		node.Attributes["memory.totalbytes"] = fmt.Sprintf("%d", memInfo.Total)
    35  
    36  		if node.Resources == nil {
    37  			node.Resources = &structs.Resources{}
    38  		}
    39  		node.Resources.MemoryMB = int(memInfo.Total / 1024 / 1024)
    40  	}
    41  
    42  	return true, nil
    43  }