collector: support 1,5,15m load averages for all unices

This commit is contained in:
Nick Owens 2015-10-30 20:11:57 -07:00
commit 2cb489253c
3 changed files with 59 additions and 75 deletions

View file

@ -12,59 +12,56 @@
// limitations under the License.
// +build !noloadavg
// +build !linux
package collector
import (
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
// #include <stdlib.h>
import "C"
type loadavgCollector struct {
metric prometheus.Gauge
metric []prometheus.Gauge
}
func init() {
Factories["loadavg"] = NewLoadavgCollector
}
// Takes a prometheus registry and returns a new Collector exposing
// load1 stat.
// Take a prometheus registry and return a new Collector exposing load average.
func NewLoadavgCollector() (Collector, error) {
return &loadavgCollector{
metric: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "load1",
Help: "1m load average.",
}),
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) {
load, err := getLoad1()
loads, err := getLoad()
if err != nil {
return fmt.Errorf("couldn't get load: %s", err)
}
log.Debugf("Set node_load: %f", load)
c.metric.Set(load)
c.metric.Collect(ch)
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
}
func getLoad1() (float64, error) {
var loadavg [1]C.double
samples := C.getloadavg(&loadavg[0], 1)
if samples > 0 {
return float64(loadavg[0]), nil
} else {
return 0, errors.New("failed to get load average")
}
}