Fix documentation and a possible slice bounds out of range

This commit is contained in:
Matteo Bigoi 2015-10-19 19:31:31 +00:00
parent 832ba9cc1d
commit 5918be9a8c
1 changed files with 14 additions and 10 deletions

View File

@ -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
}