From 5918be9a8c416ce6936cde3eacae0213e70fdfa1 Mon Sep 17 00:00:00 2001 From: Matteo Bigoi Date: Mon, 19 Oct 2015 19:31:31 +0000 Subject: [PATCH] Fix documentation and a possible slice bounds out of range --- collector/loadavg_linux.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/collector/loadavg_linux.go b/collector/loadavg_linux.go index 38e329d2..162057ad 100644 --- a/collector/loadavg_linux.go +++ b/collector/loadavg_linux.go @@ -33,7 +33,7 @@ func init() { 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) { return &loadavgCollector{ metric: []prometheus.Gauge{ @@ -59,7 +59,7 @@ func NewLoadavgCollector() (Collector, error) { 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) + return fmt.Errorf("couldn't get load: %s", err) } for i, load := range loads { log.Debugf("Set load %d: %f", i, load) @@ -69,28 +69,32 @@ func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) (err error) { return err } -// Read loadavg from /proc +// Read loadavg from /proc. func getLoad() (loads []float64, err error) { data, err := ioutil.ReadFile(procFilePath("loadavg")) if err != nil { - return loads, err + return nil, err } loads, err = parseLoad(string(data)) if err != nil { - return loads, err + return nil, err } 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) { loads = make([]float64, 3) parts := strings.Fields(data) - for i, load := range parts[0:3] { - loads[i], err = strconv.ParseFloat(load, 64) - if err != nil { - return loads, fmt.Errorf("Could not parse load '%s': %s", load, err) + if len(parts) >= 3 { + for i, load := range parts[0:3] { + loads[i], err = strconv.ParseFloat(load, 64) + 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 }