Fix scanner usage without error handling

This commit is contained in:
Tobias Schmidt 2017-02-28 14:31:35 -04:00
parent d1dfda86ee
commit 084e585c2a
11 changed files with 33 additions and 38 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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