Update CpustatCollector to use percentages and add 'num_cpus' metric

This commit is contained in:
Thomas Roehl 2022-02-17 15:25:01 +01:00
parent 6adcc077ba
commit f41ef0ccbf

@ -21,16 +21,17 @@ type CpustatCollectorConfig struct {
type CpustatCollector struct { type CpustatCollector struct {
metricCollector metricCollector
config CpustatCollectorConfig config CpustatCollectorConfig
matches map[string]int matches map[string]int
cputags map[string]map[string]string cputags map[string]map[string]string
nodetags map[string]string nodetags map[string]string
num_cpus_metric lp.CCMetric
} }
func (m *CpustatCollector) Init(config json.RawMessage) error { func (m *CpustatCollector) Init(config json.RawMessage) error {
m.name = "CpustatCollector" m.name = "CpustatCollector"
m.setup() m.setup()
m.meta = map[string]string{"source": m.name, "group": "CPU"} m.meta = map[string]string{"source": m.name, "group": "CPU", "unit": "Percent"}
m.nodetags = map[string]string{"type": "node"} m.nodetags = map[string]string{"type": "node"}
if len(config) > 0 { if len(config) > 0 {
err := json.Unmarshal(config, &m.config) err := json.Unmarshal(config, &m.config)
@ -38,34 +39,31 @@ func (m *CpustatCollector) Init(config json.RawMessage) error {
return err return err
} }
} }
matches := []string{ matches := map[string]int{
"", "cpu_user": 1,
"cpu_user", "cpu_nice": 2,
"cpu_nice", "cpu_system": 3,
"cpu_system", "cpu_idle": 4,
"cpu_idle", "cpu_iowait": 5,
"cpu_iowait", "cpu_irq": 6,
"cpu_irq", "cpu_softirq": 7,
"cpu_softirq", "cpu_steal": 8,
"cpu_steal", "cpu_guest": 9,
"cpu_guest", "cpu_guest_nice": 10,
"cpu_guest_nice",
} }
m.matches = make(map[string]int) m.matches = make(map[string]int)
for index, match := range matches { for match, index := range matches {
if len(match) > 0 { doExclude := false
doExclude := false for _, exclude := range m.config.ExcludeMetrics {
for _, exclude := range m.config.ExcludeMetrics { if match == exclude {
if match == exclude { doExclude = true
doExclude = true break
break
}
}
if !doExclude {
m.matches[match] = index
} }
} }
if !doExclude {
m.matches[match] = index
}
} }
// Check input file // Check input file
@ -76,6 +74,7 @@ func (m *CpustatCollector) Init(config json.RawMessage) error {
defer file.Close() defer file.Close()
// Pre-generate tags for all CPUs // Pre-generate tags for all CPUs
num_cpus := 0
m.cputags = make(map[string]map[string]string) m.cputags = make(map[string]map[string]string)
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
@ -85,6 +84,7 @@ func (m *CpustatCollector) Init(config json.RawMessage) error {
cpustr := strings.TrimLeft(linefields[0], "cpu") cpustr := strings.TrimLeft(linefields[0], "cpu")
cpu, _ := strconv.Atoi(cpustr) cpu, _ := strconv.Atoi(cpustr)
m.cputags[linefields[0]] = map[string]string{"type": "cpu", "type-id": fmt.Sprintf("%d", cpu)} m.cputags[linefields[0]] = map[string]string{"type": "cpu", "type-id": fmt.Sprintf("%d", cpu)}
num_cpus++
} }
} }
m.init = true m.init = true
@ -103,8 +103,9 @@ func (m *CpustatCollector) parseStatLine(linefields []string, tags map[string]st
} }
} }
} }
t := time.Now()
for name, value := range values { for name, value := range values {
y, err := lp.New(name, tags, m.meta, map[string]interface{}{"value": (value * 100.0) / total}, time.Now()) y, err := lp.New(name, tags, m.meta, map[string]interface{}{"value": (value * 100.0) / total}, t)
if err == nil { if err == nil {
output <- y output <- y
} }
@ -134,14 +135,14 @@ func (m *CpustatCollector) Read(interval time.Duration, output chan lp.CCMetric)
} }
} }
y, err := lp.New("num_cpus", num_cpus_metric, err := lp.New("num_cpus",
m.nodetags, m.nodetags,
m.meta, m.meta,
map[string]interface{}{"value": int(num_cpus)}, map[string]interface{}{"value": int(num_cpus)},
time.Now(), time.Now(),
) )
if err == nil { if err == nil {
output <- y output <- num_cpus_metric
} }
} }