Skip to content

Commit

Permalink
Separate pool tag and stat collection.
Browse files Browse the repository at this point in the history
closes #427
  • Loading branch information
allenpetersen authored and sparrc committed Dec 11, 2015
1 parent e3c8a11 commit c89ef84
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.2.5 [unreleased]

### Features
- [#427](https://github.com/influxdb/telegraf/pull/427): zfs plugin: pool stats added. Thanks @allenpetersen!

## v0.2.4 [2015-12-08]

### Features
Expand Down
52 changes: 34 additions & 18 deletions plugins/zfs/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type Zfs struct {
PoolMetrics bool
}

type poolInfo struct {
name string
ioFilename string
}

var sampleConfig = `
# ZFS kstat path
# If not specified, then default is:
Expand All @@ -37,30 +42,34 @@ func (z *Zfs) Description() string {
return "Read metrics of ZFS from arcstats, zfetchstats and vdev_cache_stats"
}

func getPoolStats(kstatPath string, poolMetrics bool, acc plugins.Accumulator) (map[string]string, error) {
var pools string
func getPools(kstatPath string) []poolInfo {
pools := make([]poolInfo, 0)
poolsDirs, _ := filepath.Glob(kstatPath + "/*/io")

for _, poolDir := range poolsDirs {
poolDirSplit := strings.Split(poolDir, "/")
pool := poolDirSplit[len(poolDirSplit)-2]
if len(pools) != 0 {
pools += "::"
}
pools += pool
pools = append(pools, poolInfo{name: pool, ioFilename: poolDir})
}

if poolMetrics {
err := readPoolStats(poolDir, pool, acc)
if err != nil {
return nil, err
}
return pools
}

func getTags(pools []poolInfo) map[string]string {
var poolNames string

for _, pool := range pools {
if len(poolNames) != 0 {
poolNames += "::"
}
poolNames += pool.name
}

return map[string]string{"pools": pools}, nil
return map[string]string{"pools": poolNames}
}

func readPoolStats(poolIoPath string, poolName string, acc plugins.Accumulator) error {
lines, err := internal.ReadLines(poolIoPath)
func gatherPoolStats(pool poolInfo, acc plugins.Accumulator) error {
lines, err := internal.ReadLines(pool.ioFilename)
if err != nil {
return err
}
Expand All @@ -78,7 +87,7 @@ func readPoolStats(poolIoPath string, poolName string, acc plugins.Accumulator)
return fmt.Errorf("Key and value count don't match Keys:%v Values:%v", keys, values)
}

tag := map[string]string{"pool": poolName}
tag := map[string]string{"pool": pool.name}

for i := 0; i < keyCount; i++ {
value, err := strconv.ParseInt(values[i], 10, 64)
Expand All @@ -103,9 +112,16 @@ func (z *Zfs) Gather(acc plugins.Accumulator) error {
kstatPath = "/proc/spl/kstat/zfs"
}

tags, err := getPoolStats(kstatPath, z.PoolMetrics, acc)
if err != nil {
return err
pools := getPools(kstatPath)
tags := getTags(pools)

if z.PoolMetrics {
for _, pool := range pools {
err := gatherPoolStats(pool, acc)
if err != nil {
return err
}
}
}

for _, metric := range kstatMetrics {
Expand Down

0 comments on commit c89ef84

Please sign in to comment.