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 {
|
2015-08-27 19:58:37 +00:00
|
|
|
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)
|
2018-01-24 14:09:53 +00:00
|
|
|
return err
|
2015-08-27 19:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if memInfo.Total > 0 {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("memory.totalbytes", fmt.Sprintf("%d", memInfo.Total))
|
2015-08-27 19:58:37 +00:00
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
resp.Resources = &structs.Resources{
|
|
|
|
MemoryMB: int(memInfo.Total / 1024 / 1024),
|
|
|
|
}
|
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
|
|
|
}
|