Files
ITWorx-Pulse-Public/internal/hostcollect/loadavg.go
T
ITWorx Pulse release export bd774932d5
Public source validation / validate (push) Failing after 3m8s
Publish ITWorx Pulse source
2026-09-03 02:09:19 +02:00

37 lines
1.1 KiB
Go

package hostcollect
import (
"fmt"
"math"
"strings"
"github.com/itworx/pulse/internal/host"
)
// parseLoadAverage reads the three load figures from /proc/loadavg. The remaining
// fields (runnable/total tasks and last PID) are deliberately ignored: the domain has
// no place for them and a process count from a PID namespace would be misleading.
func parseLoadAverage(data []byte) (host.RawLoad, error) {
fields := strings.Fields(string(data))
if len(fields) < 3 {
return host.RawLoad{}, errBadLoadAverage
}
values := [3]float64{}
for index := 0; index < 3; index++ {
value, ok := parseFloat(fields[index])
if !ok || value < 0 || math.IsNaN(value) || math.IsInf(value, 0) {
return host.RawLoad{}, errBadLoadAverage
}
values[index] = value
}
return host.RawLoad{One: values[0], Five: values[1], Fifteen: values[2]}, nil
}
func (c *Collector) loadAverage() (host.RawLoad, error) {
data, err := readLimited(c.procPath("loadavg"), maxProcessFileBytes)
if err != nil {
return host.RawLoad{}, fmt.Errorf("hostcollect: read loadavg: %w", err)
}
return parseLoadAverage(data)
}