2015-08-26 21:27:44 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
2015-08-27 14:19:53 +00:00
|
|
|
"fmt"
|
2015-08-26 21:27:44 +00:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2016-06-17 19:13:53 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/stats"
|
2015-08-26 21:27:44 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CPUFingerprint is used to fingerprint the CPU
|
|
|
|
type CPUFingerprint struct {
|
2015-11-05 21:46:02 +00:00
|
|
|
StaticFingerprinter
|
2015-08-26 21:27:44 +00:00
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCPUFingerprint is used to create a CPU fingerprint
|
|
|
|
func NewCPUFingerprint(logger *log.Logger) Fingerprint {
|
2015-08-27 00:16:34 +00:00
|
|
|
f := &CPUFingerprint{logger: logger}
|
2015-08-26 21:27:44 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *CPUFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
|
2016-06-17 19:13:53 +00:00
|
|
|
if err := stats.Init(); err != nil {
|
2016-08-07 03:49:40 +00:00
|
|
|
return false, fmt.Errorf("Unable to obtain CPU information: %v", err)
|
2015-08-26 21:27:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
modelName := stats.CPUModelName()
|
|
|
|
if modelName != "" {
|
|
|
|
node.Attributes["cpu.modelname"] = modelName
|
2016-05-09 19:22:40 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
mhz := stats.CPUMHzPerCore()
|
2016-06-22 22:09:39 +00:00
|
|
|
node.Attributes["cpu.frequency"] = fmt.Sprintf("%.0f", mhz)
|
|
|
|
f.logger.Printf("[DEBUG] fingerprint.cpu: frequency: %.0f MHz", mhz)
|
2015-08-27 14:19:53 +00:00
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
numCores := stats.CPUNumCores()
|
|
|
|
node.Attributes["cpu.numcores"] = fmt.Sprintf("%d", numCores)
|
|
|
|
f.logger.Printf("[DEBUG] fingerprint.cpu: core count: %d", numCores)
|
2015-08-27 19:15:56 +00:00
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
tt := stats.TotalTicksAvailable()
|
2016-06-22 22:09:39 +00:00
|
|
|
node.Attributes["cpu.totalcompute"] = fmt.Sprintf("%.0f", tt)
|
2015-08-27 19:15:56 +00:00
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
if node.Resources == nil {
|
|
|
|
node.Resources = &structs.Resources{}
|
2015-08-26 21:27:44 +00:00
|
|
|
}
|
2015-08-27 14:19:53 +00:00
|
|
|
|
2016-06-17 19:13:53 +00:00
|
|
|
node.Resources.CPU = int(tt)
|
2015-08-26 21:27:44 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|