2014-06-04 04:12:34 -07:00
|
|
|
// +build !noloadavg
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2015-05-11 22:46:08 -07:00
|
|
|
"errors"
|
2014-06-04 04:12:34 -07:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-05-28 12:21:44 -07:00
|
|
|
"github.com/prometheus/log"
|
2014-06-04 04:12:34 -07:00
|
|
|
)
|
|
|
|
|
2015-05-11 22:46:08 -07:00
|
|
|
// #include <stdlib.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
var loadavg [1]C.double
|
2014-06-04 04:12:34 -07:00
|
|
|
|
|
|
|
type loadavgCollector struct {
|
2014-11-24 18:00:17 -08:00
|
|
|
metric prometheus.Gauge
|
2014-06-04 04:12:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["loadavg"] = NewLoadavgCollector
|
2015-05-11 22:46:08 -07:00
|
|
|
loadavg[0] = 0
|
2014-06-04 04:12:34 -07:00
|
|
|
}
|
|
|
|
|
2015-05-20 11:04:49 -07:00
|
|
|
// Takes a prometheus registry and returns a new Collector exposing
|
2014-06-04 04:12:34 -07:00
|
|
|
// load, seconds since last login and a list of tags as specified by config.
|
2015-05-20 11:04:49 -07:00
|
|
|
func NewLoadavgCollector() (Collector, error) {
|
2014-11-24 18:00:17 -08:00
|
|
|
return &loadavgCollector{
|
|
|
|
metric: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "load1",
|
|
|
|
Help: "1m load average.",
|
|
|
|
}),
|
|
|
|
}, nil
|
2014-06-04 04:12:34 -07:00
|
|
|
}
|
|
|
|
|
2014-10-29 07:16:43 -07:00
|
|
|
func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
2014-06-04 04:12:34 -07:00
|
|
|
load, err := getLoad1()
|
|
|
|
if err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return fmt.Errorf("Couldn't get load: %s", err)
|
2014-06-04 04:12:34 -07:00
|
|
|
}
|
2015-05-28 12:21:44 -07:00
|
|
|
log.Debugf("Set node_load: %f", load)
|
2014-11-24 18:00:17 -08:00
|
|
|
c.metric.Set(load)
|
|
|
|
c.metric.Collect(ch)
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-06-04 04:12:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLoad1() (float64, error) {
|
2015-05-11 22:46:08 -07:00
|
|
|
samples := C.getloadavg(&loadavg[0], 1)
|
|
|
|
if samples > 0 {
|
|
|
|
return float64(loadavg[0]), nil
|
|
|
|
} else {
|
|
|
|
return 0, errors.New("Failed to get load average!")
|
2014-06-04 04:12:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|