-
Notifications
You must be signed in to change notification settings - Fork 2
/
loadavg.go
44 lines (32 loc) · 1.07 KB
/
loadavg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"golang.org/x/sys/unix" //see https://godoc.org/golang.org/x/sys/unix
)
const LoadModuleName = "loadavg"
// LinuxSysinfoLoadsScale magic number
const LinuxSysinfoLoadsScale = 65536.0
type LoadInputModule struct{}
func (m *LoadInputModule) Name() string {
return LoadModuleName
}
func (m *LoadInputModule) Init(config *Config, moduleConfig *ModuleConfig) error {
return nil
}
func (m *LoadInputModule) TearDown() error {
return nil
}
func (m *LoadInputModule) GetMetrics() (*ModuleMetrics, error) {
metrics := make([]Metric, 0, 48)
info := unix.Sysinfo_t{}
err := unix.Sysinfo(&info)
if err != nil {
return nil, err
}
shortterm := float64(info.Loads[0]) / LinuxSysinfoLoadsScale
midterm := float64(info.Loads[1]) / LinuxSysinfoLoadsScale
longterm := float64(info.Loads[2]) / LinuxSysinfoLoadsScale
metrics = append(metrics, NewMetric("shortterm", shortterm))
metrics = append(metrics, NewMetric("midterm", midterm))
metrics = append(metrics, NewMetric("longterm", longterm))
return &ModuleMetrics{Module: m.Name(), Metrics: metrics}, nil
}