Use explicit zpool properties to fix parse error on FreeBSD 11.2 (#4510)

This commit is contained in:
Daniel Nelson 2018-08-07 11:07:07 -07:00 committed by GitHub
parent 0759c8b22b
commit a5409d7cf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 12 deletions

View File

@ -30,7 +30,11 @@ func (z *Zfs) gatherPoolStats(acc telegraf.Accumulator) (string, error) {
if z.PoolMetrics { if z.PoolMetrics {
for _, line := range lines { for _, line := range lines {
col := strings.Split(line, "\t") col := strings.Split(line, "\t")
tags := map[string]string{"pool": col[0], "health": col[8]} if len(col) != 8 {
continue
}
tags := map[string]string{"pool": col[0], "health": col[1]}
fields := map[string]interface{}{} fields := map[string]interface{}{}
if tags["health"] == "UNAVAIL" { if tags["health"] == "UNAVAIL" {
@ -39,19 +43,19 @@ func (z *Zfs) gatherPoolStats(acc telegraf.Accumulator) (string, error) {
} else { } else {
size, err := strconv.ParseInt(col[1], 10, 64) size, err := strconv.ParseInt(col[2], 10, 64)
if err != nil { if err != nil {
return "", fmt.Errorf("Error parsing size: %s", err) return "", fmt.Errorf("Error parsing size: %s", err)
} }
fields["size"] = size fields["size"] = size
alloc, err := strconv.ParseInt(col[2], 10, 64) alloc, err := strconv.ParseInt(col[3], 10, 64)
if err != nil { if err != nil {
return "", fmt.Errorf("Error parsing allocation: %s", err) return "", fmt.Errorf("Error parsing allocation: %s", err)
} }
fields["allocated"] = alloc fields["allocated"] = alloc
free, err := strconv.ParseInt(col[3], 10, 64) free, err := strconv.ParseInt(col[4], 10, 64)
if err != nil { if err != nil {
return "", fmt.Errorf("Error parsing free: %s", err) return "", fmt.Errorf("Error parsing free: %s", err)
} }
@ -130,7 +134,7 @@ func run(command string, args ...string) ([]string, error) {
} }
func zpool() ([]string, error) { func zpool() ([]string, error) {
return run("zpool", []string{"list", "-Hp"}...) return run("zpool", []string{"list", "-Hp", "-o", "name,health,size,alloc,free,fragmentation,capacity,dedupratio"}...)
} }
func sysctl(metric string) ([]string, error) { func sysctl(metric string) ([]string, error) {

View File

@ -10,21 +10,21 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// $ zpool list -Hp // $ zpool list -Hp -o name,health,size,alloc,free,fragmentation,capacity,dedupratio
var zpool_output = []string{ var zpool_output = []string{
"freenas-boot 30601641984 2022177280 28579464704 - - 6 1.00x ONLINE -", "freenas-boot ONLINE 30601641984 2022177280 28579464704 - 6 1.00x",
"red1 8933531975680 1126164848640 7807367127040 - 8% 12 1.83x ONLINE /mnt", "red1 ONLINE 8933531975680 1126164848640 7807367127040 8% 12 1.83x",
"temp1 2989297238016 1626309320704 1362987917312 - 38% 54 1.28x ONLINE /mnt", "temp1 ONLINE 2989297238016 1626309320704 1362987917312 38% 54 1.28x",
"temp2 2989297238016 626958278656 2362338959360 - 12% 20 1.00x ONLINE /mnt", "temp2 ONLINE 2989297238016 626958278656 2362338959360 12% 20 1.00x",
} }
func mock_zpool() ([]string, error) { func mock_zpool() ([]string, error) {
return zpool_output, nil return zpool_output, nil
} }
// $ zpool list -Hp // $ zpool list -Hp -o name,health,size,alloc,free,fragmentation,capacity,dedupratio
var zpool_output_unavail = []string{ var zpool_output_unavail = []string{
"temp2 - - - - - - - UNAVAIL -", "temp2 UNAVAIL - - - - - -",
} }
func mock_zpool_unavail() ([]string, error) { func mock_zpool_unavail() ([]string, error) {