Skip to content

Commit

Permalink
validate range return value in EvaluateThreshold
Browse files Browse the repository at this point in the history
fixes #233
  • Loading branch information
Tommi2Day committed Jan 18, 2024
1 parent 7a6e921 commit eefbcd5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nagios.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ var (
// metric is not in a supported format.
ErrInvalidPerformanceDataFormat = errors.New("invalid performance data format")

// ErrInvalidRangeThreshold indicates that a given range threshold is not in a supported format.
ErrInvalidRangeThreshold = errors.New("invalid range threshold")

// TODO: Should we use field-specific errors or is the more general
// ErrInvalidPerformanceDataFormat "good enough" ? Wrapped versions of
// that error will likely already indicate which field is a problem, but
Expand Down
11 changes: 11 additions & 0 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package nagios

import (
"fmt"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -149,6 +150,11 @@ func (p *Plugin) EvaluateThreshold(perfData ...PerformanceData) error {
if perfData[i].Crit != "" {

CriticalThresholdObject := ParseRangeString(perfData[i].Crit)
if CriticalThresholdObject == nil {
err := fmt.Errorf("failed to parse critical range string %s: %w ", perfData[i].Crit, ErrInvalidRangeThreshold)
p.ExitStatusCode = StateUNKNOWNExitCode
return err
}

if CriticalThresholdObject.CheckRange(perfData[i].Value) {
p.ExitStatusCode = StateCRITICALExitCode
Expand All @@ -158,6 +164,11 @@ func (p *Plugin) EvaluateThreshold(perfData ...PerformanceData) error {

if perfData[i].Warn != "" {
warningThresholdObject := ParseRangeString(perfData[i].Warn)
if warningThresholdObject == nil {
err := fmt.Errorf("failed to parse warning range string %s: %w ", perfData[i].Warn, ErrInvalidRangeThreshold)
p.ExitStatusCode = StateUNKNOWNExitCode
return err
}

if warningThresholdObject.CheckRange(perfData[i].Value) {
p.ExitStatusCode = StateWARNINGExitCode
Expand Down

0 comments on commit eefbcd5

Please sign in to comment.