2015-10-19 17:38:16 +00:00
|
|
|
package bcache
|
|
|
|
|
|
|
|
import (
|
2015-11-04 11:03:43 +00:00
|
|
|
"errors"
|
2015-10-19 17:38:16 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-10-19 17:38:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Bcache struct {
|
|
|
|
BcachePath string
|
|
|
|
BcacheDevs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Bcache sets path
|
|
|
|
## If not specified, then default is:
|
2016-02-09 05:57:26 +00:00
|
|
|
bcachePath = "/sys/fs/bcache"
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## By default, telegraf gather stats for all bcache devices
|
|
|
|
## Setting devices will restrict the stats to the specified
|
|
|
|
## bcache devices.
|
2016-02-09 05:57:26 +00:00
|
|
|
bcacheDevs = ["bcache0"]
|
2015-10-19 17:38:16 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (b *Bcache) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bcache) Description() string {
|
|
|
|
return "Read metrics of bcache from stats_total and dirty_data"
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTags(bdev string) map[string]string {
|
|
|
|
backingDevFile, _ := os.Readlink(bdev)
|
|
|
|
backingDevPath := strings.Split(backingDevFile, "/")
|
|
|
|
backingDev := backingDevPath[len(backingDevPath)-2]
|
|
|
|
|
|
|
|
bcacheDevFile, _ := os.Readlink(bdev + "/dev")
|
|
|
|
bcacheDevPath := strings.Split(bcacheDevFile, "/")
|
|
|
|
bcacheDev := bcacheDevPath[len(bcacheDevPath)-1]
|
|
|
|
|
|
|
|
return map[string]string{"backing_dev": backingDev, "bcache_dev": bcacheDev}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prettyToBytes(v string) uint64 {
|
|
|
|
var factors = map[string]uint64{
|
|
|
|
"k": 1 << 10,
|
|
|
|
"M": 1 << 20,
|
|
|
|
"G": 1 << 30,
|
|
|
|
"T": 1 << 40,
|
|
|
|
"P": 1 << 50,
|
|
|
|
"E": 1 << 60,
|
|
|
|
}
|
|
|
|
var factor uint64
|
|
|
|
factor = 1
|
2018-10-19 20:32:54 +00:00
|
|
|
prefix := v[len(v)-1:]
|
2015-10-19 17:38:16 +00:00
|
|
|
if factors[prefix] != 0 {
|
|
|
|
v = v[:len(v)-1]
|
|
|
|
factor = factors[prefix]
|
|
|
|
}
|
|
|
|
result, _ := strconv.ParseFloat(v, 32)
|
|
|
|
result = result * float64(factor)
|
|
|
|
|
|
|
|
return uint64(result)
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (b *Bcache) gatherBcache(bdev string, acc telegraf.Accumulator) error {
|
2015-10-19 17:38:16 +00:00
|
|
|
tags := getTags(bdev)
|
|
|
|
metrics, err := filepath.Glob(bdev + "/stats_total/*")
|
|
|
|
if len(metrics) < 0 {
|
2015-11-04 11:03:43 +00:00
|
|
|
return errors.New("Can't read any stats file")
|
2015-10-19 17:38:16 +00:00
|
|
|
}
|
|
|
|
file, err := ioutil.ReadFile(bdev + "/dirty_data")
|
|
|
|
if err != nil {
|
2015-11-04 11:03:43 +00:00
|
|
|
return err
|
2015-10-19 17:38:16 +00:00
|
|
|
}
|
|
|
|
rawValue := strings.TrimSpace(string(file))
|
|
|
|
value := prettyToBytes(rawValue)
|
2015-12-14 22:15:51 +00:00
|
|
|
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
fields["dirty_data"] = value
|
2015-10-19 17:38:16 +00:00
|
|
|
|
|
|
|
for _, path := range metrics {
|
|
|
|
key := filepath.Base(path)
|
|
|
|
file, err := ioutil.ReadFile(path)
|
|
|
|
rawValue := strings.TrimSpace(string(file))
|
|
|
|
if err != nil {
|
2015-11-04 11:03:43 +00:00
|
|
|
return err
|
2015-10-19 17:38:16 +00:00
|
|
|
}
|
|
|
|
if key == "bypassed" {
|
|
|
|
value := prettyToBytes(rawValue)
|
2015-12-14 22:15:51 +00:00
|
|
|
fields[key] = value
|
2015-10-19 17:38:16 +00:00
|
|
|
} else {
|
|
|
|
value, _ := strconv.ParseUint(rawValue, 10, 64)
|
2015-12-14 22:15:51 +00:00
|
|
|
fields[key] = value
|
2015-10-19 17:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-14 22:15:51 +00:00
|
|
|
acc.AddFields("bcache", fields, tags)
|
2015-10-19 17:38:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (b *Bcache) Gather(acc telegraf.Accumulator) error {
|
2015-10-19 17:38:16 +00:00
|
|
|
bcacheDevsChecked := make(map[string]bool)
|
|
|
|
var restrictDevs bool
|
|
|
|
if len(b.BcacheDevs) != 0 {
|
|
|
|
restrictDevs = true
|
|
|
|
for _, bcacheDev := range b.BcacheDevs {
|
|
|
|
bcacheDevsChecked[bcacheDev] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bcachePath := b.BcachePath
|
|
|
|
if len(bcachePath) == 0 {
|
|
|
|
bcachePath = "/sys/fs/bcache"
|
|
|
|
}
|
2015-11-04 11:03:43 +00:00
|
|
|
bdevs, _ := filepath.Glob(bcachePath + "/*/bdev*")
|
|
|
|
if len(bdevs) < 1 {
|
2015-12-14 22:15:51 +00:00
|
|
|
return errors.New("Can't find any bcache device")
|
2015-11-04 11:03:43 +00:00
|
|
|
}
|
|
|
|
for _, bdev := range bdevs {
|
2015-10-19 17:38:16 +00:00
|
|
|
if restrictDevs {
|
|
|
|
bcacheDev := getTags(bdev)["bcache_dev"]
|
|
|
|
if !bcacheDevsChecked[bcacheDev] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.gatherBcache(bdev, acc)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("bcache", func() telegraf.Input {
|
2015-10-19 17:38:16 +00:00
|
|
|
return &Bcache{}
|
|
|
|
})
|
|
|
|
}
|