From cc498c24428bf90ec2be5d80bdc4cbb00a65dde1 Mon Sep 17 00:00:00 2001 From: cornerot Date: Wed, 4 Nov 2015 14:03:43 +0300 Subject: [PATCH] removed "panic" from bcache plugin --- plugins/bcache/bcache.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/plugins/bcache/bcache.go b/plugins/bcache/bcache.go index ee63f3c48..76e638ea4 100644 --- a/plugins/bcache/bcache.go +++ b/plugins/bcache/bcache.go @@ -1,6 +1,7 @@ package bcache import ( + "errors" "io/ioutil" "os" "path/filepath" @@ -34,17 +35,6 @@ func (b *Bcache) Description() string { 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 { backingDevFile, _ := os.Readlink(bdev) backingDevPath := strings.Split(backingDevFile, "/") @@ -83,11 +73,11 @@ func (b *Bcache) gatherBcache(bdev string, acc plugins.Accumulator) error { tags := getTags(bdev) metrics, err := filepath.Glob(bdev + "/stats_total/*") 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") if err != nil { - panic(err) + return err } rawValue := strings.TrimSpace(string(file)) value := prettyToBytes(rawValue) @@ -98,7 +88,7 @@ func (b *Bcache) gatherBcache(bdev string, acc plugins.Accumulator) error { file, err := ioutil.ReadFile(path) rawValue := strings.TrimSpace(string(file)) if err != nil { - panic(err) + return err } if key == "bypassed" { value := prettyToBytes(rawValue) @@ -125,7 +115,11 @@ func (b *Bcache) Gather(acc plugins.Accumulator) error { if len(bcachePath) == 0 { 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 { bcacheDev := getTags(bdev)["bcache_dev"] if !bcacheDevsChecked[bcacheDev] {