2015-08-27 19:58:37 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-01-30 17:57:37 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-08-27 19:58:37 +00:00
|
|
|
"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
|
2015-08-27 19:58:37 +00:00
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMemoryFingerprint is used to create a Memory fingerprint
|
|
|
|
func NewMemoryFingerprint(logger *log.Logger) Fingerprint {
|
|
|
|
f := &MemoryFingerprint{
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
func (f *MemoryFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error {
|
2018-01-22 20:28:29 +00:00
|
|
|
var totalMemory int
|
|
|
|
cfg := req.Config
|
|
|
|
if cfg.MemoryMB != 0 {
|
|
|
|
totalMemory = cfg.MemoryMB * 1024 * 1024
|
|
|
|
} else {
|
|
|
|
memInfo, err := mem.VirtualMemory()
|
|
|
|
if err != nil {
|
|
|
|
f.logger.Printf("[WARN] Error reading memory information: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if memInfo.Total > 0 {
|
|
|
|
totalMemory = int(memInfo.Total)
|
|
|
|
}
|
2015-08-27 19:58:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 20:28:29 +00:00
|
|
|
if totalMemory > 0 {
|
|
|
|
resp.AddAttribute("memory.totalbytes", fmt.Sprintf("%d", totalMemory))
|
2018-01-30 17:57:37 +00:00
|
|
|
resp.Resources = &structs.Resources{
|
2018-01-22 20:28:29 +00:00
|
|
|
MemoryMB: int(totalMemory / 1024 / 1024),
|
2018-01-30 17:57:37 +00:00
|
|
|
}
|
2015-08-27 19:58:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2015-08-27 19:58:37 +00:00
|
|
|
}
|