Merge pull request #39 from DSpeichert/master

Fixed panic when parsing /proc/stat on 2.6.32-25-pve
This commit is contained in:
Tobias Schmidt 2015-02-07 04:39:13 -05:00
commit 5bf5eef004

View file

@ -90,6 +90,9 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
parts := strings.Fields(scanner.Text()) parts := strings.Fields(scanner.Text())
if len(parts) == 0 {
continue
}
switch { switch {
case strings.HasPrefix(parts[0], "cpu"): case strings.HasPrefix(parts[0], "cpu"):
// Export only per-cpu stats, it can be aggregated up in prometheus. // Export only per-cpu stats, it can be aggregated up in prometheus.
@ -98,7 +101,12 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
} }
// Only some of these may be present, depending on kernel version. // Only some of these may be present, depending on kernel version.
cpuFields := []string{"user", "nice", "system", "idle", "iowait", "irq", "softirq", "steal", "guest"} cpuFields := []string{"user", "nice", "system", "idle", "iowait", "irq", "softirq", "steal", "guest"}
for i, v := range parts[1 : len(cpuFields)+1] { // OpenVZ guests lack the "guest" CPU field, which needs to be ignored.
expectedFieldNum := len(cpuFields)+1
if expectedFieldNum > len(parts) {
expectedFieldNum = len(parts)
}
for i, v := range parts[1 : expectedFieldNum] {
value, err := strconv.ParseFloat(v, 64) value, err := strconv.ParseFloat(v, 64)
if err != nil { if err != nil {
return err return err