gomonop/pkg/perfdata/range.go
Emmanuel BENOîT c46c9d76d9
All checks were successful
Run tests and linters / test (push) Successful in 50s
Run tests and linters / build (push) Successful in 48s
Run tests and linters / lint (push) Successful in 1m27s
feat: add the check_output_matches plugin (#5)
This PR adds the `check_output_matches` plugin, which can be used to count regexp or substring matches from either text files or command outputs and determine the final status based on the amount of matches that were found.

Reviewed-on: #5
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-20 22:57:10 +02:00

93 lines
1.9 KiB
Go

package perfdata // import nocternity.net/gomonop/pkg/perfdata
import (
"fmt"
"strconv"
)
// Performance data range.
type Range struct {
start string
end string
inside bool
}
// Creates a performance data range from -inf to 0 and from the specified
// value to +inf.
func RangeMax(max string) *Range {
if !valueCheck.MatchString(max) {
panic("invalid performance data range maximum value")
}
pdRange := &Range{}
pdRange.start = "0"
pdRange.end = max
return pdRange
}
// Creates a performance data range from -inf to the specified minimal value
// and from the specified maximal value to +inf.
func RangeMinMax(min, max string) *Range {
if !valueCheck.MatchString(max) {
panic("invalid performance data range maximum value")
}
if !rangeMinCheck.MatchString(min) {
panic("invalid performance data range minimum value")
}
pdRange := &Range{}
if min == "~" {
min = ""
}
pdRange.start = min
pdRange.end = max
return pdRange
}
// Inverts the range.
func (r *Range) Inside() *Range {
r.inside = true
return r
}
// Generates the range's string representation so it can be sent to the
// monitoring system.
func (r *Range) String() string {
var start, inside string
switch r.start {
case "":
start = "~"
case "0":
start = ""
default:
start = r.start
}
if r.inside {
inside = "@"
}
return inside + start + ":" + r.end
}
// Contains checks whether a numeric value is within the range.
func (r *Range) Contains(value float64) bool {
var inStart, inEnd bool
if r.start != "" {
startValue, err := strconv.ParseFloat(r.start, 64)
if err != nil {
panic(fmt.Sprintf("invalid performance data range start value: %v", err))
}
inStart = value < startValue
}
if r.end != "" {
endValue, err := strconv.ParseFloat(r.end, 64)
if err != nil {
panic(fmt.Sprintf("invalid performance data range end value: %v", err))
}
inEnd = value > endValue
}
return (inStart || inEnd) != r.inside
}