From 05a8c77433c79a89734b6d77c63dc07b2192e022 Mon Sep 17 00:00:00 2001 From: Johannes Ziemke Date: Tue, 3 Sep 2024 10:30:09 +0200 Subject: [PATCH] Convert AIX cpu usage to seconds --- collector/cpu_aix.go | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/collector/cpu_aix.go b/collector/cpu_aix.go index 3cfd79c343..a5f99f34de 100644 --- a/collector/cpu_aix.go +++ b/collector/cpu_aix.go @@ -16,7 +16,13 @@ package collector +/* +#include // Include the standard Unix header +#include // For errno +*/ +import "C" import ( + "fmt" "strconv" "github.com/go-kit/log" @@ -25,18 +31,32 @@ import ( ) type cpuCollector struct { - cpu typedDesc - logger log.Logger + 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, + 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 }