Use int64 throughout the ZFS collector.

This avoids issues with integer overflows on 32-bit architectures. The
Prometheus data format is float64, so regardless of the architecture we
should handle large numbers.

Fixes #629.
This commit is contained in:
Matthias Rampke 2017-08-21 16:40:16 +00:00
parent 8661bbbb42
commit e1f129c729
3 changed files with 24 additions and 24 deletions

View File

@ -75,7 +75,7 @@ func (s zfsSysctl) metricName() string {
return strings.Replace(parts[len(parts)-1], "-", "_", -1) return strings.Replace(parts[len(parts)-1], "-", "_", -1)
} }
func (c *zfsCollector) constSysctlMetric(subsystem string, sysctl zfsSysctl, value int) prometheus.Metric { func (c *zfsCollector) constSysctlMetric(subsystem string, sysctl zfsSysctl, value int64) prometheus.Metric {
metricName := sysctl.metricName() metricName := sysctl.metricName()
return prometheus.MustNewConstMetric( return prometheus.MustNewConstMetric(
@ -90,7 +90,7 @@ func (c *zfsCollector) constSysctlMetric(subsystem string, sysctl zfsSysctl, val
) )
} }
func (c *zfsCollector) constPoolMetric(poolName string, sysctl zfsSysctl, value int) prometheus.Metric { func (c *zfsCollector) constPoolMetric(poolName string, sysctl zfsSysctl, value int64) prometheus.Metric {
metricName := sysctl.metricName() metricName := sysctl.metricName()
return prometheus.MustNewConstMetric( return prometheus.MustNewConstMetric(

View File

@ -42,7 +42,7 @@ func (c *zfsCollector) updateZfsStats(subsystem string, ch chan<- prometheus.Met
} }
defer file.Close() defer file.Close()
return c.parseProcfsFile(file, c.linuxPathMap[subsystem], func(s zfsSysctl, v int) { return c.parseProcfsFile(file, c.linuxPathMap[subsystem], func(s zfsSysctl, v int64) {
ch <- c.constSysctlMetric(subsystem, s, v) ch <- c.constSysctlMetric(subsystem, s, v)
}) })
} }
@ -64,7 +64,7 @@ func (c *zfsCollector) updatePoolStats(ch chan<- prometheus.Metric) error {
return errZFSNotAvailable return errZFSNotAvailable
} }
err = c.parsePoolProcfsFile(file, zpoolPath, func(poolName string, s zfsSysctl, v int) { err = c.parsePoolProcfsFile(file, zpoolPath, func(poolName string, s zfsSysctl, v int64) {
ch <- c.constPoolMetric(poolName, s, v) ch <- c.constPoolMetric(poolName, s, v)
}) })
file.Close() file.Close()
@ -76,7 +76,7 @@ func (c *zfsCollector) updatePoolStats(ch chan<- prometheus.Metric) error {
return nil return nil
} }
func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler func(zfsSysctl, int)) error { func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler func(zfsSysctl, int64)) error {
scanner := bufio.NewScanner(reader) scanner := bufio.NewScanner(reader)
parseLine := false parseLine := false
@ -95,7 +95,7 @@ func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler
key := fmt.Sprintf("kstat.zfs.misc.%s.%s", fmtExt, parts[0]) key := fmt.Sprintf("kstat.zfs.misc.%s.%s", fmtExt, parts[0])
value, err := strconv.Atoi(parts[2]) value, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("could not parse expected integer value for %q", key) return fmt.Errorf("could not parse expected integer value for %q", key)
} }
@ -109,7 +109,7 @@ func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler
return scanner.Err() return scanner.Err()
} }
func (c *zfsCollector) parsePoolProcfsFile(reader io.Reader, zpoolPath string, handler func(string, zfsSysctl, int)) error { func (c *zfsCollector) parsePoolProcfsFile(reader io.Reader, zpoolPath string, handler func(string, zfsSysctl, int64)) error {
scanner := bufio.NewScanner(reader) scanner := bufio.NewScanner(reader)
parseLine := false parseLine := false
@ -139,7 +139,7 @@ func (c *zfsCollector) parsePoolProcfsFile(reader io.Reader, zpoolPath string, h
for i, field := range fields { for i, field := range fields {
key := fmt.Sprintf("kstat.zfs.misc.%s.%s", zpoolFile, field) key := fmt.Sprintf("kstat.zfs.misc.%s.%s", zpoolFile, field)
value, err := strconv.Atoi(line[i]) value, err := strconv.ParseInt(line[i], 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("could not parse expected integer value for %q: %v", key, err) return fmt.Errorf("could not parse expected integer value for %q: %v", key, err)
} }

View File

@ -32,7 +32,7 @@ func TestArcstatsParsing(t *testing.T) {
} }
handlerCalled := false handlerCalled := false
err = c.parseProcfsFile(arcstatsFile, "arcstats", func(s zfsSysctl, v int) { err = c.parseProcfsFile(arcstatsFile, "arcstats", func(s zfsSysctl, v int64) {
if s != zfsSysctl("kstat.zfs.misc.arcstats.hits") { if s != zfsSysctl("kstat.zfs.misc.arcstats.hits") {
return return
@ -40,7 +40,7 @@ func TestArcstatsParsing(t *testing.T) {
handlerCalled = true handlerCalled = true
if v != int(8772612) { if v != int64(8772612) {
t.Fatalf("Incorrect value parsed from procfs data") t.Fatalf("Incorrect value parsed from procfs data")
} }
@ -68,7 +68,7 @@ func TestZfetchstatsParsing(t *testing.T) {
} }
handlerCalled := false handlerCalled := false
err = c.parseProcfsFile(zfetchstatsFile, "zfetchstats", func(s zfsSysctl, v int) { err = c.parseProcfsFile(zfetchstatsFile, "zfetchstats", func(s zfsSysctl, v int64) {
if s != zfsSysctl("kstat.zfs.misc.zfetchstats.hits") { if s != zfsSysctl("kstat.zfs.misc.zfetchstats.hits") {
return return
@ -76,7 +76,7 @@ func TestZfetchstatsParsing(t *testing.T) {
handlerCalled = true handlerCalled = true
if v != int(7067992) { if v != int64(7067992) {
t.Fatalf("Incorrect value parsed from procfs data") t.Fatalf("Incorrect value parsed from procfs data")
} }
@ -104,7 +104,7 @@ func TestZilParsing(t *testing.T) {
} }
handlerCalled := false handlerCalled := false
err = c.parseProcfsFile(zilFile, "zil", func(s zfsSysctl, v int) { err = c.parseProcfsFile(zilFile, "zil", func(s zfsSysctl, v int64) {
if s != zfsSysctl("kstat.zfs.misc.zil.zil_commit_count") { if s != zfsSysctl("kstat.zfs.misc.zil.zil_commit_count") {
return return
@ -112,7 +112,7 @@ func TestZilParsing(t *testing.T) {
handlerCalled = true handlerCalled = true
if v != int(10) { if v != int64(10) {
t.Fatalf("Incorrect value parsed from procfs data") t.Fatalf("Incorrect value parsed from procfs data")
} }
@ -140,7 +140,7 @@ func TestVdevCacheStatsParsing(t *testing.T) {
} }
handlerCalled := false handlerCalled := false
err = c.parseProcfsFile(vdevCacheStatsFile, "vdev_cache_stats", func(s zfsSysctl, v int) { err = c.parseProcfsFile(vdevCacheStatsFile, "vdev_cache_stats", func(s zfsSysctl, v int64) {
if s != zfsSysctl("kstat.zfs.misc.vdev_cache_stats.delegations") { if s != zfsSysctl("kstat.zfs.misc.vdev_cache_stats.delegations") {
return return
@ -148,7 +148,7 @@ func TestVdevCacheStatsParsing(t *testing.T) {
handlerCalled = true handlerCalled = true
if v != int(40) { if v != int64(40) {
t.Fatalf("Incorrect value parsed from procfs data") t.Fatalf("Incorrect value parsed from procfs data")
} }
@ -176,7 +176,7 @@ func TestXuioStatsParsing(t *testing.T) {
} }
handlerCalled := false handlerCalled := false
err = c.parseProcfsFile(xuioStatsFile, "xuio_stats", func(s zfsSysctl, v int) { err = c.parseProcfsFile(xuioStatsFile, "xuio_stats", func(s zfsSysctl, v int64) {
if s != zfsSysctl("kstat.zfs.misc.xuio_stats.onloan_read_buf") { if s != zfsSysctl("kstat.zfs.misc.xuio_stats.onloan_read_buf") {
return return
@ -184,7 +184,7 @@ func TestXuioStatsParsing(t *testing.T) {
handlerCalled = true handlerCalled = true
if v != int(32) { if v != int64(32) {
t.Fatalf("Incorrect value parsed from procfs data") t.Fatalf("Incorrect value parsed from procfs data")
} }
@ -212,7 +212,7 @@ func TestFmParsing(t *testing.T) {
} }
handlerCalled := false handlerCalled := false
err = c.parseProcfsFile(fmFile, "fm", func(s zfsSysctl, v int) { err = c.parseProcfsFile(fmFile, "fm", func(s zfsSysctl, v int64) {
if s != zfsSysctl("kstat.zfs.misc.fm.erpt-dropped") { if s != zfsSysctl("kstat.zfs.misc.fm.erpt-dropped") {
return return
@ -220,7 +220,7 @@ func TestFmParsing(t *testing.T) {
handlerCalled = true handlerCalled = true
if v != int(18) { if v != int64(18) {
t.Fatalf("Incorrect value parsed from procfs data") t.Fatalf("Incorrect value parsed from procfs data")
} }
@ -248,7 +248,7 @@ func TestDmuTxParsing(t *testing.T) {
} }
handlerCalled := false handlerCalled := false
err = c.parseProcfsFile(dmuTxFile, "dmu_tx", func(s zfsSysctl, v int) { err = c.parseProcfsFile(dmuTxFile, "dmu_tx", func(s zfsSysctl, v int64) {
if s != zfsSysctl("kstat.zfs.misc.dmu_tx.dmu_tx_assigned") { if s != zfsSysctl("kstat.zfs.misc.dmu_tx.dmu_tx_assigned") {
return return
@ -256,7 +256,7 @@ func TestDmuTxParsing(t *testing.T) {
handlerCalled = true handlerCalled = true
if v != int(3532844) { if v != int64(3532844) {
t.Fatalf("Incorrect value parsed from procfs data") t.Fatalf("Incorrect value parsed from procfs data")
} }
@ -289,14 +289,14 @@ func TestZpoolParsing(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = c.parsePoolProcfsFile(file, zpoolPath, func(poolName string, s zfsSysctl, v int) { err = c.parsePoolProcfsFile(file, zpoolPath, func(poolName string, s zfsSysctl, v int64) {
if s != zfsSysctl("kstat.zfs.misc.io.nread") { if s != zfsSysctl("kstat.zfs.misc.io.nread") {
return return
} }
handlerCalled = true handlerCalled = true
if v != int(1884160) && v != int(2826240) { if v != int64(1884160) && v != int64(2826240) {
t.Fatalf("Incorrect value parsed from procfs data %v", v) t.Fatalf("Incorrect value parsed from procfs data %v", v)
} }