mirror of
https://github.com/prometheus/node_exporter.git
synced 2025-01-28 14:22:51 -08:00
20b551ab2b
Remove all hardcoded references to `/proc`. For all collectors that do not use `github.com/prometheus/procfs` yet, provide a wrapper to generate the full paths. Reformulate help strings, errors and comments to remove absolute references to `/proc`. This is a breaking change: the `-collector.ipvs.procfs` flag is removed in favor of the general flag. Since it only affected that collector it was only useful for development, so this should not cause many issues.
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
// +build !noloadavg
|
|
|
|
package collector
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/log"
|
|
)
|
|
|
|
type loadavgCollector struct {
|
|
metric prometheus.Gauge
|
|
}
|
|
|
|
func init() {
|
|
Factories["loadavg"] = NewLoadavgCollector
|
|
}
|
|
|
|
// Takes a prometheus registry and returns a new Collector exposing
|
|
// load, seconds since last login and a list of tags as specified by config.
|
|
func NewLoadavgCollector() (Collector, error) {
|
|
return &loadavgCollector{
|
|
metric: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
Namespace: Namespace,
|
|
Name: "load1",
|
|
Help: "1m load average.",
|
|
}),
|
|
}, nil
|
|
}
|
|
|
|
func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
|
load, err := getLoad1()
|
|
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)
|
|
return err
|
|
}
|
|
|
|
func getLoad1() (float64, error) {
|
|
data, err := ioutil.ReadFile(procFilePath("loadavg"))
|
|
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
|
|
}
|