Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #28 from obourdon/fix_4_issue_27
Browse files Browse the repository at this point in the history
Fix for issue #27: smart plugin returns error if nothing is collected
  • Loading branch information
candysmurf committed Sep 8, 2016
2 parents 5f06ea6 + 9ff59e4 commit bd97d46
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
23 changes: 13 additions & 10 deletions smart/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ func (sc *SmartCollector) CollectMetrics(mts []plugin.MetricType) ([]plugin.Metr
for _, mt := range mts {
ns := mt.Namespace()
if !validateName(ns.Strings()) {
errs = append(errs, fmt.Sprintf("%s is not valid metric", ns.String()))
eStr := fmt.Sprintf("%s is not valid metric", ns.String())
sc.logger.Error(eStr)
errs = append(errs, eStr)
continue
}
disk, attribute_path := parseName(ns.Strings())
Expand All @@ -203,7 +205,9 @@ func (sc *SmartCollector) CollectMetrics(mts []plugin.MetricType) ([]plugin.Metr
for _, dev := range devices {
collected, result, err := sc.DiskMetrics(ns, t, dev, attribute_path, buffered_results, errs)
if err != nil {
sc.logger.Error(fmt.Sprintf("Error collecting SMART %s data on %s disk: %#+v", attribute_path, dev, err))
eStr := fmt.Sprintf("Error collecting SMART %s data on %s disk: %#+v", attribute_path, dev, err)
sc.logger.Error(eStr)
errs = append(errs, eStr)
} else {
if collected {
results = append(results, result)
Expand All @@ -213,10 +217,11 @@ func (sc *SmartCollector) CollectMetrics(mts []plugin.MetricType) ([]plugin.Metr
}
} else {
// Single disk requested

collected, result, err := sc.DiskMetrics(ns, t, disk, attribute_path, buffered_results, errs)
if err != nil {
sc.logger.Error(fmt.Sprintf("Error collecting SMART %s data on %s disk: %#+v", attribute_path, disk, err))
eStr := fmt.Sprintf("Error collecting SMART %s data on %s disk: %#+v", attribute_path, disk, err)
sc.logger.Error(eStr)
errs = append(errs, eStr)
} else {
if collected {
results = append(results, result)
Expand All @@ -226,14 +231,12 @@ func (sc *SmartCollector) CollectMetrics(mts []plugin.MetricType) ([]plugin.Metr
}
}
errsStr := strings.Join(errs, "; ")
if something_collected {
if len(errs) > 0 {
sc.logger.Error(fmt.Sprintf("Data collected but error(s) occured: %v", errsStr))
if len(errs) > 0 {
if !something_collected {
return nil, errors.New(errsStr)
}
return results, nil
} else {
return nil, errors.New(errsStr)
}
return results, nil
}

// GetMetricTypes returns the metric types exposed by smart
Expand Down
7 changes: 6 additions & 1 deletion smart/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func TestCollectMetrics(t *testing.T) {

ReadSmartData = func(device string,
sysutilProvider SysutilProvider) (*SmartValues, error) {
return &SmartValues{}, nil
return nil, errors.New("x not valid disk")
}

_, err := sc.CollectMetrics([]plugin.MetricType{
Expand All @@ -293,6 +293,11 @@ func TestCollectMetrics(t *testing.T) {

So(err, ShouldNotBeNil)

Convey("Error is about invalid metric", func() {

So(err.Error(), ShouldContainSubstring, "not valid disk")

})
})

})
Expand Down

0 comments on commit bd97d46

Please sign in to comment.