2015-11-03 15:53:09 +00:00
|
|
|
package zfs
|
|
|
|
|
|
|
|
import (
|
2015-12-08 09:50:42 +00:00
|
|
|
"fmt"
|
2015-11-03 15:53:09 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2015-11-10 21:40:39 +00:00
|
|
|
"github.com/influxdb/telegraf/internal"
|
2015-11-03 15:53:09 +00:00
|
|
|
"github.com/influxdb/telegraf/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Zfs struct {
|
|
|
|
KstatPath string
|
|
|
|
KstatMetrics []string
|
2015-12-08 09:50:42 +00:00
|
|
|
PoolMetrics bool
|
2015-11-03 15:53:09 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 18:38:47 +00:00
|
|
|
type poolInfo struct {
|
|
|
|
name string
|
|
|
|
ioFilename string
|
|
|
|
}
|
|
|
|
|
2015-11-03 15:53:09 +00:00
|
|
|
var sampleConfig = `
|
|
|
|
# ZFS kstat path
|
|
|
|
# If not specified, then default is:
|
|
|
|
# kstatPath = "/proc/spl/kstat/zfs"
|
|
|
|
#
|
|
|
|
# By default, telegraf gather all zfs stats
|
|
|
|
# If not specified, then default is:
|
|
|
|
# kstatMetrics = ["arcstats", "zfetchstats", "vdev_cache_stats"]
|
2015-12-08 09:50:42 +00:00
|
|
|
#
|
|
|
|
# By default, don't gather zpool stats
|
|
|
|
# poolMetrics = false
|
2015-11-03 15:53:09 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (z *Zfs) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *Zfs) Description() string {
|
|
|
|
return "Read metrics of ZFS from arcstats, zfetchstats and vdev_cache_stats"
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:38:47 +00:00
|
|
|
func getPools(kstatPath string) []poolInfo {
|
|
|
|
pools := make([]poolInfo, 0)
|
2015-11-03 15:53:09 +00:00
|
|
|
poolsDirs, _ := filepath.Glob(kstatPath + "/*/io")
|
2015-12-10 18:38:47 +00:00
|
|
|
|
2015-11-03 15:53:09 +00:00
|
|
|
for _, poolDir := range poolsDirs {
|
|
|
|
poolDirSplit := strings.Split(poolDir, "/")
|
|
|
|
pool := poolDirSplit[len(poolDirSplit)-2]
|
2015-12-10 18:38:47 +00:00
|
|
|
pools = append(pools, poolInfo{name: pool, ioFilename: poolDir})
|
|
|
|
}
|
2015-12-08 09:50:42 +00:00
|
|
|
|
2015-12-10 18:38:47 +00:00
|
|
|
return pools
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTags(pools []poolInfo) map[string]string {
|
|
|
|
var poolNames string
|
|
|
|
|
|
|
|
for _, pool := range pools {
|
|
|
|
if len(poolNames) != 0 {
|
|
|
|
poolNames += "::"
|
2015-12-08 09:50:42 +00:00
|
|
|
}
|
2015-12-10 18:38:47 +00:00
|
|
|
poolNames += pool.name
|
2015-12-08 09:50:42 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 18:38:47 +00:00
|
|
|
return map[string]string{"pools": poolNames}
|
2015-12-08 09:50:42 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 18:38:47 +00:00
|
|
|
func gatherPoolStats(pool poolInfo, acc plugins.Accumulator) error {
|
|
|
|
lines, err := internal.ReadLines(pool.ioFilename)
|
2015-12-08 09:50:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lines) != 3 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := strings.Fields(lines[1])
|
|
|
|
values := strings.Fields(lines[2])
|
|
|
|
|
|
|
|
keyCount := len(keys)
|
|
|
|
|
|
|
|
if keyCount != len(values) {
|
|
|
|
return fmt.Errorf("Key and value count don't match Keys:%v Values:%v", keys, values)
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:38:47 +00:00
|
|
|
tag := map[string]string{"pool": pool.name}
|
2015-12-15 21:11:23 +00:00
|
|
|
fields := make(map[string]interface{})
|
2015-12-08 09:50:42 +00:00
|
|
|
for i := 0; i < keyCount; i++ {
|
|
|
|
value, err := strconv.ParseInt(values[i], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-15 21:11:23 +00:00
|
|
|
fields[keys[i]] = value
|
2015-11-03 15:53:09 +00:00
|
|
|
}
|
2015-12-15 21:11:23 +00:00
|
|
|
acc.AddFields("zfs_pool", fields, tag)
|
2015-12-08 09:50:42 +00:00
|
|
|
|
|
|
|
return nil
|
2015-11-03 15:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (z *Zfs) Gather(acc plugins.Accumulator) error {
|
|
|
|
kstatMetrics := z.KstatMetrics
|
|
|
|
if len(kstatMetrics) == 0 {
|
|
|
|
kstatMetrics = []string{"arcstats", "zfetchstats", "vdev_cache_stats"}
|
|
|
|
}
|
|
|
|
|
|
|
|
kstatPath := z.KstatPath
|
|
|
|
if len(kstatPath) == 0 {
|
|
|
|
kstatPath = "/proc/spl/kstat/zfs"
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:38:47 +00:00
|
|
|
pools := getPools(kstatPath)
|
|
|
|
tags := getTags(pools)
|
|
|
|
|
|
|
|
if z.PoolMetrics {
|
|
|
|
for _, pool := range pools {
|
|
|
|
err := gatherPoolStats(pool, acc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-12-08 09:50:42 +00:00
|
|
|
}
|
2015-11-03 15:53:09 +00:00
|
|
|
|
2015-12-15 21:11:23 +00:00
|
|
|
fields := make(map[string]interface{})
|
2015-11-03 15:53:09 +00:00
|
|
|
for _, metric := range kstatMetrics {
|
2015-11-10 21:40:39 +00:00
|
|
|
lines, err := internal.ReadLines(kstatPath + "/" + metric)
|
2015-11-03 15:53:09 +00:00
|
|
|
if err != nil {
|
2015-11-09 12:37:00 +00:00
|
|
|
return err
|
2015-11-03 15:53:09 +00:00
|
|
|
}
|
|
|
|
for i, line := range lines {
|
|
|
|
if i == 0 || i == 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(line) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rawData := strings.Split(line, " ")
|
|
|
|
key := metric + "_" + rawData[0]
|
|
|
|
rawValue := rawData[len(rawData)-1]
|
|
|
|
value, _ := strconv.ParseInt(rawValue, 10, 64)
|
2015-12-15 21:11:23 +00:00
|
|
|
fields[key] = value
|
2015-11-03 15:53:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 21:11:23 +00:00
|
|
|
acc.AddFields("zfs", fields, tags)
|
2015-11-03 15:53:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
plugins.Add("zfs", func() plugins.Plugin {
|
|
|
|
return &Zfs{}
|
|
|
|
})
|
|
|
|
}
|