mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-12-27 14:39:53 -08:00
Fix documentation and a possible slice bounds out of range
This commit is contained in:
parent
832ba9cc1d
commit
5918be9a8c
|
@ -33,7 +33,7 @@ func init() {
|
||||||
Factories["loadavg"] = NewLoadavgCollector
|
Factories["loadavg"] = NewLoadavgCollector
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes a prometheus registry and returns a new Collector exposing load average
|
// Take a prometheus registry and return a new Collector exposing load average.
|
||||||
func NewLoadavgCollector() (Collector, error) {
|
func NewLoadavgCollector() (Collector, error) {
|
||||||
return &loadavgCollector{
|
return &loadavgCollector{
|
||||||
metric: []prometheus.Gauge{
|
metric: []prometheus.Gauge{
|
||||||
|
@ -59,7 +59,7 @@ func NewLoadavgCollector() (Collector, error) {
|
||||||
func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
||||||
loads, err := getLoad()
|
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)
|
||||||
}
|
}
|
||||||
for i, load := range loads {
|
for i, load := range loads {
|
||||||
log.Debugf("Set load %d: %f", i, load)
|
log.Debugf("Set load %d: %f", i, load)
|
||||||
|
@ -69,28 +69,32 @@ func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
||||||
return err
|
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"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return loads, err
|
return nil, err
|
||||||
}
|
}
|
||||||
loads, err = parseLoad(string(data))
|
loads, err = parseLoad(string(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return loads, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return loads, nil
|
return loads, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse /proc loadavg and return 1m, 5m and 15m
|
// Parse /proc loadavg and return 1m, 5m and 15m.
|
||||||
func parseLoad(data string) (loads []float64, err error) {
|
func parseLoad(data string) (loads []float64, err error) {
|
||||||
loads = make([]float64, 3)
|
loads = make([]float64, 3)
|
||||||
parts := strings.Fields(data)
|
parts := strings.Fields(data)
|
||||||
for i, load := range parts[0:3] {
|
if len(parts) >= 3 {
|
||||||
loads[i], err = strconv.ParseFloat(load, 64)
|
for i, load := range parts[0:3] {
|
||||||
if err != nil {
|
loads[i], err = strconv.ParseFloat(load, 64)
|
||||||
return loads, fmt.Errorf("Could not parse load '%s': %s", load, err)
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not parse load '%s': %s", load, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("unexpected content in %s", procFilePath("loadavg"))
|
||||||
}
|
}
|
||||||
return loads, nil
|
return loads, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue