Convert AIX cpu usage to seconds

This commit is contained in:
Johannes Ziemke 2024-09-03 10:30:09 +02:00
parent 163e9325c3
commit 05a8c77433

View file

@ -16,7 +16,13 @@
package collector
/*
#include <unistd.h> // Include the standard Unix header
#include <errno.h> // For errno
*/
import "C"
import (
"fmt"
"strconv"
"github.com/go-kit/log"
@ -27,16 +33,30 @@ import (
type cpuCollector struct {
cpu typedDesc
logger log.Logger
tickPerSecond int64
}
func init() {
registerCollector("cpu", defaultEnabled, NewCpuCollector)
}
func tickPerSecond() (int64, error) {
ticks, err := C.sysconf(C._SC_CLK_TCK)
if ticks == -1 || err != nil {
return 0, fmt.Errorf("failed to get clock ticks per second: %v", err)
}
return int64(ticks), nil
}
func NewCpuCollector(logger log.Logger) (Collector, error) {
ticks, err := tickPerSecond()
if err != nil {
return nil, err
}
return &cpuCollector{
cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue},
logger: logger,
tickPerSecond: ticks,
}, nil
}
@ -47,10 +67,10 @@ func (c *cpuCollector) Update(ch chan<- prometheus.Metric) error {
}
for n, stat := range stats {
ch <- c.cpu.mustNewConstMetric(float64(stat.User), strconv.Itoa(n), "user")
ch <- c.cpu.mustNewConstMetric(float64(stat.Sys), strconv.Itoa(n), "system")
ch <- c.cpu.mustNewConstMetric(float64(stat.Idle), strconv.Itoa(n), "idle")
ch <- c.cpu.mustNewConstMetric(float64(stat.Wait), strconv.Itoa(n), "wait")
ch <- c.cpu.mustNewConstMetric(float64(stat.User/c.tickPerSecond), strconv.Itoa(n), "user")
ch <- c.cpu.mustNewConstMetric(float64(stat.Sys/c.tickPerSecond), strconv.Itoa(n), "system")
ch <- c.cpu.mustNewConstMetric(float64(stat.Idle/c.tickPerSecond), strconv.Itoa(n), "idle")
ch <- c.cpu.mustNewConstMetric(float64(stat.Wait/c.tickPerSecond), strconv.Itoa(n), "wait")
}
return nil
}