2014-06-04 13:12:34 +02:00
|
|
|
// +build !noloadavg
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-05-28 21:21:44 +02:00
|
|
|
"github.com/prometheus/log"
|
2014-06-04 13:12:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
procLoad = "/proc/loadavg"
|
|
|
|
)
|
|
|
|
|
|
|
|
type loadavgCollector struct {
|
2014-11-25 03:00:17 +01:00
|
|
|
metric prometheus.Gauge
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["loadavg"] = NewLoadavgCollector
|
|
|
|
}
|
|
|
|
|
2015-05-20 20:04:49 +02:00
|
|
|
// Takes a prometheus registry and returns a new Collector exposing
|
2014-06-04 13:12:34 +02:00
|
|
|
// load, seconds since last login and a list of tags as specified by config.
|
2015-05-20 20:04:49 +02:00
|
|
|
func NewLoadavgCollector() (Collector, error) {
|
2014-11-25 03:00:17 +01:00
|
|
|
return &loadavgCollector{
|
|
|
|
metric: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "load1",
|
|
|
|
Help: "1m load average.",
|
|
|
|
}),
|
|
|
|
}, nil
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 15:16:43 +01:00
|
|
|
func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
2014-06-04 13:12:34 +02:00
|
|
|
load, err := getLoad1()
|
|
|
|
if err != nil {
|
2014-10-29 15:16:43 +01:00
|
|
|
return fmt.Errorf("Couldn't get load: %s", err)
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
2015-05-28 21:21:44 +02:00
|
|
|
log.Debugf("Set node_load: %f", load)
|
2014-11-25 03:00:17 +01:00
|
|
|
c.metric.Set(load)
|
|
|
|
c.metric.Collect(ch)
|
2014-10-29 15:16:43 +01:00
|
|
|
return err
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLoad1() (float64, error) {
|
|
|
|
data, err := ioutil.ReadFile(procLoad)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return parseLoad(string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLoad(data string) (float64, error) {
|
|
|
|
parts := strings.Fields(data)
|
|
|
|
load, err := strconv.ParseFloat(parts[0], 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("Could not parse load '%s': %s", parts[0], err)
|
|
|
|
}
|
|
|
|
return load, nil
|
|
|
|
}
|