removed "panic" from bcache plugin

This commit is contained in:
cornerot 2015-11-04 14:03:43 +03:00
parent e2854232d0
commit cc498c2442
1 changed files with 9 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package bcache package bcache
import ( import (
"errors"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -34,17 +35,6 @@ func (b *Bcache) Description() string {
return "Read metrics of bcache from stats_total and dirty_data" return "Read metrics of bcache from stats_total and dirty_data"
} }
func getBackingDevs(bcachePath string) []string {
bdevs, err := filepath.Glob(bcachePath + "/*/bdev*")
if len(bdevs) < 1 {
panic("Can't found any bcache device")
}
if err != nil {
panic(err)
}
return bdevs
}
func getTags(bdev string) map[string]string { func getTags(bdev string) map[string]string {
backingDevFile, _ := os.Readlink(bdev) backingDevFile, _ := os.Readlink(bdev)
backingDevPath := strings.Split(backingDevFile, "/") backingDevPath := strings.Split(backingDevFile, "/")
@ -83,11 +73,11 @@ func (b *Bcache) gatherBcache(bdev string, acc plugins.Accumulator) error {
tags := getTags(bdev) tags := getTags(bdev)
metrics, err := filepath.Glob(bdev + "/stats_total/*") metrics, err := filepath.Glob(bdev + "/stats_total/*")
if len(metrics) < 0 { if len(metrics) < 0 {
panic("Can't read any stats file") return errors.New("Can't read any stats file")
} }
file, err := ioutil.ReadFile(bdev + "/dirty_data") file, err := ioutil.ReadFile(bdev + "/dirty_data")
if err != nil { if err != nil {
panic(err) return err
} }
rawValue := strings.TrimSpace(string(file)) rawValue := strings.TrimSpace(string(file))
value := prettyToBytes(rawValue) value := prettyToBytes(rawValue)
@ -98,7 +88,7 @@ func (b *Bcache) gatherBcache(bdev string, acc plugins.Accumulator) error {
file, err := ioutil.ReadFile(path) file, err := ioutil.ReadFile(path)
rawValue := strings.TrimSpace(string(file)) rawValue := strings.TrimSpace(string(file))
if err != nil { if err != nil {
panic(err) return err
} }
if key == "bypassed" { if key == "bypassed" {
value := prettyToBytes(rawValue) value := prettyToBytes(rawValue)
@ -125,7 +115,11 @@ func (b *Bcache) Gather(acc plugins.Accumulator) error {
if len(bcachePath) == 0 { if len(bcachePath) == 0 {
bcachePath = "/sys/fs/bcache" bcachePath = "/sys/fs/bcache"
} }
for _, bdev := range getBackingDevs(bcachePath) { bdevs, _ := filepath.Glob(bcachePath + "/*/bdev*")
if len(bdevs) < 1 {
return errors.New("Can't found any bcache device")
}
for _, bdev := range bdevs {
if restrictDevs { if restrictDevs {
bcacheDev := getTags(bdev)["bcache_dev"] bcacheDev := getTags(bdev)["bcache_dev"]
if !bcacheDevsChecked[bcacheDev] { if !bcacheDevsChecked[bcacheDev] {