Public source validation / validate (push) Failing after 3m8s
37 lines
1.1 KiB
Go
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)
|
|
}
|