feat: add the check_output_matches plugin #5

Merged
Emmanuel BENOîT merged 16 commits from tseeker/gomonop:20240720-line-matches into master 2024-07-20 22:57:10 +02:00
2 changed files with 50 additions and 0 deletions
Showing only changes of commit ee05d6f004 - Show all commits

View file

@ -3,7 +3,10 @@
package perfdata // import nocternity.net/gomonop/pkg/perfdata package perfdata // import nocternity.net/gomonop/pkg/perfdata
import ( import (
"strconv"
"strings" "strings"
"nocternity.net/gomonop/pkg/status"
) )
// Performance data, including a label, units, a value, warning/critical // Performance data, including a label, units, a value, warning/critical
@ -71,6 +74,23 @@ func (d *PerfData) SetMax(max string) {
d.bits |= PDatMax d.bits |= PDatMax
} }
// Check the performance data's value against its configured ranges.
func (d *PerfData) GetStatus() status.Status {
value, err := strconv.ParseFloat(d.value, 64)
if err != nil {
return status.StatusUnknown
}
if d.bits&PDatCrit != 0 && d.crit.Contains(value) {
return status.StatusCritical
}
if d.bits&PDatWarn != 0 && d.warn.Contains(value) {
return status.StatusWarning
}
return status.StatusOK
}
// Converts performance data to a string which may be read by the monitoring // Converts performance data to a string which may be read by the monitoring
// system. // system.
func (d *PerfData) String() string { func (d *PerfData) String() string {

View file

@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"nocternity.net/gomonop/pkg/status"
) )
func TestNewNoValue(t *testing.T) { func TestNewNoValue(t *testing.T) {
@ -167,6 +168,35 @@ func TestSetMaxTwice(t *testing.T) {
assert.True(t, data.bits&PDatMax != 0) assert.True(t, data.bits&PDatMax != 0)
} }
func TestCheck(t *testing.T) {
warnRange := RangeMinMax("0", "10").Inside()
critRange := RangeMinMax("1", "9").Inside()
type Test struct {
in string
out status.Status
}
tests := []Test{
{in: "moo", out: status.StatusUnknown},
{in: "-1", out: status.StatusOK},
{in: "0", out: status.StatusWarning},
{in: "1", out: status.StatusCritical},
{in: "9", out: status.StatusCritical},
{in: "10", out: status.StatusWarning},
{in: "11", out: status.StatusOK},
}
for _, test := range tests {
pdat := PerfData{
value: test.in,
bits: PDatCrit | PDatWarn,
warn: *warnRange,
crit: *critRange,
}
assert.Equal(t, test.out, pdat.GetStatus())
}
}
func TestString(t *testing.T) { func TestString(t *testing.T) {
type Test struct { type Test struct {
PerfData PerfData