mirror of
https://github.com/prometheus/node_exporter.git
synced 2025-01-11 22:07:32 -08:00
collector: support 1,5,15m load averages for all unices
This commit is contained in:
parent
6ebbda7f9e
commit
2cb489253c
|
@ -12,59 +12,56 @@
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// +build !noloadavg
|
// +build !noloadavg
|
||||||
// +build !linux
|
|
||||||
|
|
||||||
package collector
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/common/log"
|
"github.com/prometheus/common/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// #include <stdlib.h>
|
|
||||||
import "C"
|
|
||||||
|
|
||||||
type loadavgCollector struct {
|
type loadavgCollector struct {
|
||||||
metric prometheus.Gauge
|
metric []prometheus.Gauge
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Factories["loadavg"] = NewLoadavgCollector
|
Factories["loadavg"] = NewLoadavgCollector
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes a prometheus registry and returns a new Collector exposing
|
// Take a prometheus registry and return a new Collector exposing load average.
|
||||||
// load1 stat.
|
|
||||||
func NewLoadavgCollector() (Collector, error) {
|
func NewLoadavgCollector() (Collector, error) {
|
||||||
return &loadavgCollector{
|
return &loadavgCollector{
|
||||||
metric: prometheus.NewGauge(prometheus.GaugeOpts{
|
metric: []prometheus.Gauge{
|
||||||
Namespace: Namespace,
|
prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
Name: "load1",
|
Namespace: Namespace,
|
||||||
Help: "1m load average.",
|
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
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
||||||
load, err := getLoad1()
|
loads, err := getLoad()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("couldn't get load: %s", err)
|
return fmt.Errorf("couldn't get load: %s", err)
|
||||||
}
|
}
|
||||||
log.Debugf("Set node_load: %f", load)
|
for i, load := range loads {
|
||||||
c.metric.Set(load)
|
log.Debugf("Set load %d: %f", i, load)
|
||||||
c.metric.Collect(ch)
|
c.metric[i].Set(load)
|
||||||
|
c.metric[i].Collect(ch)
|
||||||
|
}
|
||||||
return err
|
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -20,55 +20,8 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"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.
|
// Read loadavg from /proc.
|
||||||
func getLoad() (loads []float64, err error) {
|
func getLoad() (loads []float64, err error) {
|
||||||
data, err := ioutil.ReadFile(procFilePath("loadavg"))
|
data, err := ioutil.ReadFile(procFilePath("loadavg"))
|
||||||
|
|
34
collector/loadavg_unix.go
Normal file
34
collector/loadavg_unix.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
// Copyright 2015 The Prometheus Authors
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// +build darwin dragonfly freebsd netbsd openbsd solaris
|
||||||
|
// +build !noloadavg
|
||||||
|
|
||||||
|
package collector
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// #include <stdlib.h>
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
func getLoad() ([]float64, error) {
|
||||||
|
var loadavg [3]C.double
|
||||||
|
samples := C.getloadavg(&loadavg[0], 3)
|
||||||
|
if samples > 0 {
|
||||||
|
return []float64{float64(loadavg[0]), float64(loadavg[1]), float64(loadavg[2])}, nil
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("failed to get load average")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue