Fix scanner usage without error handling

pull/482/head
Tobias Schmidt 8 years ago
parent d1dfda86ee
commit 084e585c2a

@ -239,5 +239,5 @@ func parseDiskStats(r io.Reader) (map[string]map[int]string, error) {
diskStats[dev][12] = bytesWritten diskStats[dev][12] = bytesWritten
} }
return diskStats, nil return diskStats, scanner.Err()
} }

@ -16,12 +16,11 @@
package collector package collector
import ( import (
"bufio" "bytes"
"fmt" "fmt"
"io" "io/ioutil"
"os" "os"
"strconv" "strconv"
"strings"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
@ -41,8 +40,8 @@ func NewFileFDStatCollector() (Collector, error) {
return &fileFDStatCollector{}, nil return &fileFDStatCollector{}, nil
} }
func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) { func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) error {
fileFDStat, err := getFileFDStats(procFilePath("sys/fs/file-nr")) fileFDStat, err := parseFileFDStats(procFilePath("sys/fs/file-nr"))
if err != nil { if err != nil {
return fmt.Errorf("couldn't get file-nr: %s", err) return fmt.Errorf("couldn't get file-nr: %s", err)
} }
@ -63,25 +62,27 @@ func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
return nil return nil
} }
func getFileFDStats(fileName string) (map[string]string, error) { func parseFileFDStats(filename string) (map[string]string, error) {
file, err := os.Open(fileName) file, err := os.Open(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close() defer file.Close()
return parseFileFDStats(file, fileName)
}
func parseFileFDStats(r io.Reader, fileName string) (map[string]string, error) { content, err := ioutil.ReadAll(file)
var scanner = bufio.NewScanner(r) if err != nil {
scanner.Scan() return nil, err
// The file-nr proc file is separated by tabs, not spaces. }
line := strings.Split(scanner.Text(), "\u0009") parts := bytes.Split(bytes.TrimSpace(content), []byte("\u0009"))
if len(parts) < 3 {
return nil, fmt.Errorf("unexpected number of file stats in %q", filename)
}
var fileFDStat = map[string]string{} var fileFDStat = map[string]string{}
// The file-nr proc is only 1 line with 3 values. // The file-nr proc is only 1 line with 3 values.
fileFDStat["allocated"] = line[0] fileFDStat["allocated"] = string(parts[0])
// The second value is skipped as it will always be zero in linux 2.6. // The second value is skipped as it will always be zero in linux 2.6.
fileFDStat["maximum"] = line[2] fileFDStat["maximum"] = string(parts[2])
return fileFDStat, nil return fileFDStat, nil
} }

@ -13,28 +13,19 @@
package collector package collector
import ( import "testing"
"os"
"testing"
)
func TestFileFDStats(t *testing.T) { func TestFileFDStats(t *testing.T) {
file, err := os.Open("fixtures/proc/sys/fs/file-nr") fileFDStats, err := parseFileFDStats("fixtures/proc/sys/fs/file-nr")
if err != nil {
t.Fatal(err)
}
defer file.Close()
fileFDStats, err := parseFileFDStats(file, fileName)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if want, got := "1024", fileFDStats["allocated"]; want != got { if want, got := "1024", fileFDStats["allocated"]; want != got {
t.Errorf("want filefd allocated %s, got %s", want, got) t.Errorf("want filefd allocated %q, got %q", want, got)
} }
if want, got := "1631329", fileFDStats["maximum"]; want != got { if want, got := "1631329", fileFDStats["maximum"]; want != got {
t.Errorf("want filefd maximum %s, got %s", want, got) t.Errorf("want filefd maximum %q, got %q", want, got)
} }
} }

@ -94,5 +94,5 @@ func parseInterrupts(r io.Reader) (map[string]interrupt, error) {
interrupts[intName] = intr interrupts[intName] = intr
} }
return interrupts, nil return interrupts, scanner.Err()
} }

@ -122,7 +122,7 @@ func parseMegaCliDisks(r io.Reader) (map[int]map[int]map[string]string, error) {
} }
} }
return stats, nil return stats, scanner.Err()
} }
func parseMegaCliAdapter(r io.Reader) (map[string]map[string]string, error) { func parseMegaCliAdapter(r io.Reader) (map[string]map[string]string, error) {
@ -155,7 +155,7 @@ func parseMegaCliAdapter(r io.Reader) (map[string]map[string]string, error) {
} }
return raidStats, nil return raidStats, scanner.Err()
} }
func (c *megaCliCollector) updateAdapter() error { func (c *megaCliCollector) updateAdapter() error {

@ -62,5 +62,5 @@ func parseMemInfo(r io.Reader) (map[string]float64, error) {
memInfo[key] = fv memInfo[key] = fv
} }
return memInfo, nil return memInfo, scanner.Err()
} }

@ -108,5 +108,5 @@ func parseNetStats(r io.Reader, fileName string) (map[string]map[string]string,
} }
} }
return netStats, nil return netStats, scanner.Err()
} }

@ -95,6 +95,9 @@ func parseSockStats(r io.Reader, fileName string) (map[string]map[string]string,
i++ i++
} }
} }
if err := scanner.Err(); err != nil {
return nil, err
}
// The mem metrics is the count of pages used. Multiply the mem metrics by // The mem metrics is the count of pages used. Multiply the mem metrics by
// the page size from the kernel to get the number of bytes used. // the page size from the kernel to get the number of bytes used.

@ -158,5 +158,5 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
ch <- prometheus.MustNewConstMetric(c.procsBlocked, prometheus.GaugeValue, value) ch <- prometheus.MustNewConstMetric(c.procsBlocked, prometheus.GaugeValue, value)
} }
} }
return err return scanner.Err()
} }

@ -130,7 +130,7 @@ func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) {
tcpStats[tcpConnectionState(st)]++ tcpStats[tcpConnectionState(st)]++
} }
return tcpStats, nil return tcpStats, scanner.Err()
} }
func (st tcpConnectionState) String() string { func (st tcpConnectionState) String() string {

@ -64,5 +64,5 @@ func (c *vmStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
value, value,
) )
} }
return err return scanner.Err()
} }

Loading…
Cancel
Save