diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index 79b8992d..91a90d15 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -239,5 +239,5 @@ func parseDiskStats(r io.Reader) (map[string]map[int]string, error) { diskStats[dev][12] = bytesWritten } - return diskStats, nil + return diskStats, scanner.Err() } diff --git a/collector/filefd_linux.go b/collector/filefd_linux.go index 2c55a527..709e5850 100644 --- a/collector/filefd_linux.go +++ b/collector/filefd_linux.go @@ -16,12 +16,11 @@ package collector import ( - "bufio" + "bytes" "fmt" - "io" + "io/ioutil" "os" "strconv" - "strings" "github.com/prometheus/client_golang/prometheus" ) @@ -41,8 +40,8 @@ func NewFileFDStatCollector() (Collector, error) { return &fileFDStatCollector{}, nil } -func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) { - fileFDStat, err := getFileFDStats(procFilePath("sys/fs/file-nr")) +func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) error { + fileFDStat, err := parseFileFDStats(procFilePath("sys/fs/file-nr")) if err != nil { 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 } -func getFileFDStats(fileName string) (map[string]string, error) { - file, err := os.Open(fileName) +func parseFileFDStats(filename string) (map[string]string, error) { + file, err := os.Open(filename) if err != nil { return nil, err } defer file.Close() - return parseFileFDStats(file, fileName) -} -func parseFileFDStats(r io.Reader, fileName string) (map[string]string, error) { - var scanner = bufio.NewScanner(r) - scanner.Scan() - // The file-nr proc file is separated by tabs, not spaces. - line := strings.Split(scanner.Text(), "\u0009") + content, err := ioutil.ReadAll(file) + if err != nil { + return nil, err + } + 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{} // 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. - fileFDStat["maximum"] = line[2] + fileFDStat["maximum"] = string(parts[2]) return fileFDStat, nil } diff --git a/collector/filefd_linux_test.go b/collector/filefd_linux_test.go index 11781eb7..37e16a4e 100644 --- a/collector/filefd_linux_test.go +++ b/collector/filefd_linux_test.go @@ -13,28 +13,19 @@ package collector -import ( - "os" - "testing" -) +import "testing" func TestFileFDStats(t *testing.T) { - file, err := os.Open("fixtures/proc/sys/fs/file-nr") - if err != nil { - t.Fatal(err) - } - defer file.Close() - - fileFDStats, err := parseFileFDStats(file, fileName) + fileFDStats, err := parseFileFDStats("fixtures/proc/sys/fs/file-nr") if err != nil { t.Fatal(err) } 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 { - t.Errorf("want filefd maximum %s, got %s", want, got) + t.Errorf("want filefd maximum %q, got %q", want, got) } } diff --git a/collector/interrupts_linux.go b/collector/interrupts_linux.go index 3fc3aa79..1ef899c8 100644 --- a/collector/interrupts_linux.go +++ b/collector/interrupts_linux.go @@ -94,5 +94,5 @@ func parseInterrupts(r io.Reader) (map[string]interrupt, error) { interrupts[intName] = intr } - return interrupts, nil + return interrupts, scanner.Err() } diff --git a/collector/megacli.go b/collector/megacli.go index f5c7dc83..453366a5 100644 --- a/collector/megacli.go +++ b/collector/megacli.go @@ -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) { @@ -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 { diff --git a/collector/meminfo_linux.go b/collector/meminfo_linux.go index 3a625a5f..e17626dc 100644 --- a/collector/meminfo_linux.go +++ b/collector/meminfo_linux.go @@ -62,5 +62,5 @@ func parseMemInfo(r io.Reader) (map[string]float64, error) { memInfo[key] = fv } - return memInfo, nil + return memInfo, scanner.Err() } diff --git a/collector/netstat_linux.go b/collector/netstat_linux.go index 65b7897c..50231f0f 100644 --- a/collector/netstat_linux.go +++ b/collector/netstat_linux.go @@ -108,5 +108,5 @@ func parseNetStats(r io.Reader, fileName string) (map[string]map[string]string, } } - return netStats, nil + return netStats, scanner.Err() } diff --git a/collector/sockstat_linux.go b/collector/sockstat_linux.go index 77873f2b..0c45a428 100644 --- a/collector/sockstat_linux.go +++ b/collector/sockstat_linux.go @@ -95,6 +95,9 @@ func parseSockStats(r io.Reader, fileName string) (map[string]map[string]string, 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 page size from the kernel to get the number of bytes used. diff --git a/collector/stat_linux.go b/collector/stat_linux.go index 77929354..1aff0a3e 100644 --- a/collector/stat_linux.go +++ b/collector/stat_linux.go @@ -158,5 +158,5 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) { ch <- prometheus.MustNewConstMetric(c.procsBlocked, prometheus.GaugeValue, value) } } - return err + return scanner.Err() } diff --git a/collector/tcpstat_linux.go b/collector/tcpstat_linux.go index 78cb5e09..579b5a25 100644 --- a/collector/tcpstat_linux.go +++ b/collector/tcpstat_linux.go @@ -130,7 +130,7 @@ func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) { tcpStats[tcpConnectionState(st)]++ } - return tcpStats, nil + return tcpStats, scanner.Err() } func (st tcpConnectionState) String() string { diff --git a/collector/vmstat_linux.go b/collector/vmstat_linux.go index beb41e77..3f68c146 100644 --- a/collector/vmstat_linux.go +++ b/collector/vmstat_linux.go @@ -64,5 +64,5 @@ func (c *vmStatCollector) Update(ch chan<- prometheus.Metric) (err error) { value, ) } - return err + return scanner.Err() }