collector: support 1,5,15m load averages for all unices
This commit is contained in:
parent
6ebbda7f9e
commit
2cb489253c
3 changed files with 59 additions and 75 deletions
|
|
@ -20,55 +20,8 @@ import (
|
|||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/common/log"
|
||||
)
|
||||
|
||||
type loadavgCollector struct {
|
||||
metric []prometheus.Gauge
|
||||
}
|
||||
|
||||
func init() {
|
||||
Factories["loadavg"] = NewLoadavgCollector
|
||||
}
|
||||
|
||||
// Take a prometheus registry and return a new Collector exposing load average.
|
||||
func NewLoadavgCollector() (Collector, error) {
|
||||
return &loadavgCollector{
|
||||
metric: []prometheus.Gauge{
|
||||
prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: Namespace,
|
||||
Name: "load1",
|
||||
Help: "1m load average.",
|
||||
}),
|
||||
prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: Namespace,
|
||||
Name: "load5",
|
||||
Help: "5m load average.",
|
||||
}),
|
||||
prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: Namespace,
|
||||
Name: "load15",
|
||||
Help: "15m load average.",
|
||||
}),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
||||
loads, err := getLoad()
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't get load: %s", err)
|
||||
}
|
||||
for i, load := range loads {
|
||||
log.Debugf("Set load %d: %f", i, load)
|
||||
c.metric[i].Set(load)
|
||||
c.metric[i].Collect(ch)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Read loadavg from /proc.
|
||||
func getLoad() (loads []float64, err error) {
|
||||
data, err := ioutil.ReadFile(procFilePath("loadavg"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue