42 lines
712 B
Go
42 lines
712 B
Go
package loadavg
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"bugs.watch/agent/internal/aggregate"
|
|
)
|
|
|
|
type Collector struct{}
|
|
|
|
func New() *Collector {
|
|
return &Collector{}
|
|
}
|
|
|
|
func (c *Collector) Name() string {
|
|
return "loadavg"
|
|
}
|
|
|
|
// Collect reads /proc/loadavg and stores the 1-minute load average.
|
|
// Failure is silently ignored by design.
|
|
func (c *Collector) Collect(window *aggregate.Window) {
|
|
data, err := os.ReadFile("/proc/loadavg")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// Format: "<load1> <load5> <load15> <running/total> <last_pid>"
|
|
fields := strings.Fields(string(data))
|
|
if len(fields) < 1 {
|
|
return
|
|
}
|
|
|
|
load1, err := strconv.ParseFloat(fields[0], 64)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
window.SetLoad(load1)
|
|
}
|