Skip to content

Commit

Permalink
Convert AIX cpu usage to seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
discordianfish committed Sep 7, 2024
1 parent 163e932 commit 05a8c77
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions collector/cpu_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand All @@ -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
}

0 comments on commit 05a8c77

Please sign in to comment.