feat: add the check_output_matches plugin ()

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: 
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
This commit is contained in:
Emmanuel BENOîT 2024-07-20 22:57:10 +02:00 committed by Emmanuel BENOîT
parent 9fac656cdf
commit c46c9d76d9
22 changed files with 1063 additions and 55 deletions

View file

@ -3,7 +3,10 @@
package perfdata // import nocternity.net/gomonop/pkg/perfdata
import (
"strconv"
"strings"
"nocternity.net/gomonop/pkg/status"
)
// Performance data, including a label, units, a value, warning/critical
@ -35,14 +38,22 @@ func New(label string, units UnitOfMeasurement, value string) *PerfData {
// Set the warning range for the performance data record.
func (d *PerfData) SetWarn(r *Range) {
d.warn = *r
d.bits |= PDatWarn
if r == nil {
d.bits &^= PDatWarn
} else {
d.warn = *r
d.bits |= PDatWarn
}
}
// Set the critical range for the performance data record.
func (d *PerfData) SetCrit(r *Range) {
d.crit = *r
d.bits |= PDatCrit
if r == nil {
d.bits &^= PDatCrit
} else {
d.crit = *r
d.bits |= PDatCrit
}
}
// Set the performance data's minimal value.
@ -63,6 +74,23 @@ func (d *PerfData) SetMax(max string) {
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
// system.
func (d *PerfData) String() string {

View file

@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"nocternity.net/gomonop/pkg/status"
)
func TestNewNoValue(t *testing.T) {
@ -50,6 +51,12 @@ func TestSetWarn(t *testing.T) {
assert.Equal(t, rangeStr, data.warn.String())
}
func TestSetWarnNil(t *testing.T) {
data := PerfData{}
data.SetWarn(nil)
assert.Equal(t, perfDataBits(0), data.bits&PDatWarn)
}
func TestSetWarnTwice(t *testing.T) {
range1Value := Range{start: "A", end: "B"}
range2Value := Range{start: "C", end: "D"}
@ -64,6 +71,16 @@ func TestSetWarnTwice(t *testing.T) {
assert.Equal(t, range2Str, data.warn.String())
}
func TestSetWarnClear(t *testing.T) {
range1Value := Range{start: "A", end: "B"}
data := PerfData{}
data.SetWarn(&range1Value)
data.SetWarn(nil)
assert.Equal(t, perfDataBits(0), data.bits&PDatWarn)
}
func TestSetCrit(t *testing.T) {
rangeValue := Range{start: "A", end: "B"}
rangeStr := rangeValue.String()
@ -75,6 +92,12 @@ func TestSetCrit(t *testing.T) {
assert.Equal(t, rangeStr, data.crit.String())
}
func TestSetCritNil(t *testing.T) {
data := PerfData{}
data.SetCrit(nil)
assert.Equal(t, perfDataBits(0), data.bits&PDatCrit)
}
func TestSetCritTwice(t *testing.T) {
range1Value := Range{start: "A", end: "B"}
range2Value := Range{start: "C", end: "D"}
@ -89,6 +112,16 @@ func TestSetCritTwice(t *testing.T) {
assert.Equal(t, range2Str, data.crit.String())
}
func TestSetCritClear(t *testing.T) {
range1Value := Range{start: "A", end: "B"}
data := PerfData{}
data.SetCrit(&range1Value)
data.SetCrit(nil)
assert.Equal(t, perfDataBits(0), data.bits&PDatCrit)
}
func TestSetMin(t *testing.T) {
const min = "100"
@ -135,6 +168,35 @@ func TestSetMaxTwice(t *testing.T) {
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) {
type Test struct {
PerfData

View file

@ -1,5 +1,10 @@
package perfdata // import nocternity.net/gomonop/pkg/perfdata
import (
"fmt"
"strconv"
)
// Performance data range.
type Range struct {
start string
@ -29,6 +34,9 @@ func RangeMinMax(min, max string) *Range {
panic("invalid performance data range minimum value")
}
pdRange := &Range{}
if min == "~" {
min = ""
}
pdRange.start = min
pdRange.end = max
return pdRange
@ -60,3 +68,26 @@ func (r *Range) String() string {
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
}

View file

@ -1,6 +1,7 @@
package perfdata // import nocternity.net/gomonop/pkg/perfdata
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
@ -40,6 +41,14 @@ func TestRangeMinMax(t *testing.T) {
assert.False(t, pdr.inside, "Inside flag should not be set")
}
func TestRangeMinMaxOpen(t *testing.T) {
min, max := "~", "456"
pdr := RangeMinMax(min, max)
assert.Equal(t, "", pdr.start, "Min value not clear in PerfDataRange")
assert.Equal(t, max, pdr.end, "Max value not copied to PerfDataRange")
assert.False(t, pdr.inside, "Inside flag should not be set")
}
func TestRangeInside(t *testing.T) {
pdr := &Range{}
pdr = pdr.Inside()
@ -65,3 +74,53 @@ func TestRangeString(t *testing.T) {
assert.Equal(t, test.out, result, "Expected '%s', got '%s'", test.out, result)
}
}
func TestRangeContains(t *testing.T) {
type Test struct {
pdr Range
value float64
result bool
}
tests := []Test{
{pdr: Range{start: "0", end: "10"}, value: 0, result: false},
{pdr: Range{start: "0", end: "10"}, value: 10, result: false},
{pdr: Range{start: "0", end: "10"}, value: -1, result: true},
{pdr: Range{start: "0", end: "10"}, value: 11, result: true},
{pdr: Range{start: "", end: "10"}, value: -1000, result: false},
{pdr: Range{start: "", end: "10"}, value: 10, result: false},
{pdr: Range{start: "", end: "10"}, value: 11, result: true},
{pdr: Range{start: "10", end: ""}, value: -1000, result: true},
{pdr: Range{start: "10", end: ""}, value: 9, result: true},
{pdr: Range{start: "10", end: ""}, value: 10, result: false},
{pdr: Range{start: "10", end: "20"}, value: 9, result: true},
{pdr: Range{start: "10", end: "20"}, value: 10, result: false},
{pdr: Range{start: "10", end: "20"}, value: 20, result: false},
{pdr: Range{start: "10", end: "20"}, value: 21, result: true},
}
// Test cases with the inside flag set and the opposite result
n := len(tests)
for i := range n {
tests = append(tests, Test{
pdr: Range{
start: tests[i].pdr.start,
end: tests[i].pdr.end,
inside: !tests[i].pdr.inside,
},
value: tests[i].value,
result: !tests[i].result,
})
}
for _, test := range tests {
t.Run(fmt.Sprintf("%v", test), func(t *testing.T) {
result := test.pdr.Contains(test.value)
assert.Equal(
t, test.result, result,
"Expected '%v', got '%v' for value '%f' and range '%s'",
test.result, result, test.value, test.pdr.String(),
)
})
}
}

196
pkg/perfdata/rangeparser.go Normal file
View file

@ -0,0 +1,196 @@
package perfdata // import nocternity.net/gomonop/pkg/perfdata
import (
"fmt"
"strings"
)
// A state of the range parser.
type rangeParserState int
const (
rpsInit rangeParserState = iota // Initial state
rpsExpectStart // Expect the start of the range
rpsInStart // Reading the start of the range
rpsExpectColon // Expect the colon that separates the start and end
rpsExpectEnd // Expect the end of the range
rpsInEnd // Reading the end of the range
)
// An error emitted by the range parser.
type rangeParserError struct {
input string
position int
message string
}
// parseError creates a new range parser error.
func parseError(input string, position int, message string) *rangeParserError {
return &rangeParserError{
input: input,
position: position,
message: message,
}
}
// Error implements the error interface for the range parser error type.
func (rpe *rangeParserError) Error() string {
return fmt.Sprintf("in `%s' at position %d: %s", rpe.input, rpe.position, rpe.message)
}
// The full state of the range parser.
type rangeParser struct {
runes []rune // The runes being parsed
index int // The read position
state rangeParserState // The FSM state
startOfValue int // The position of the start of the value being read
strBuilder strings.Builder // An accumulator for values
output Range // The output being generated
}
// initRangeParser initializes the range parser from the specified input.
func initRangeParser(input string) *rangeParser {
return &rangeParser{
runes: []rune(input),
state: rpsInit,
}
}
// handleInit handles the parser's rpsInit state. It can accept a '@' or skip
// to rpsExpectStart.
func (rp *rangeParser) handleInit(current rune) {
if current == '@' {
rp.output.inside = true
rp.index++
}
rp.state = rpsExpectStart
}
// handleExpectStart handles the parser's rpsExpectStart state. It may accept
// a colon, which will move the parser to the rpsExpectEnd state, a '~' which
// will move it to rpsExpectColon, or any other rune which will cause the
// accumulation of the "start" value to begin, and the state to be moved to
// handleInStart.
func (rp *rangeParser) handleExpectStart(current rune) {
switch current {
case ':':
rp.output.start = "0"
rp.state = rpsExpectEnd
rp.index++
case '~':
rp.state = rpsExpectColon
rp.index++
default:
rp.startOfValue = rp.index
rp.state = rpsInStart
}
}
// handleInStart handles the parser's rpsInStart state, which corresponds to the
// reading of the "start" value. If a colon is found, the value will be written
// to the range structure and validated, before switching to the rpsExpectEnd
// state. Otherwise it will simply accumulate runes.
func (rp *rangeParser) handleInStart(current rune) error {
switch current {
case ':':
rp.output.start = rp.strBuilder.String()
if !valueCheck.MatchString(rp.output.start) {
return parseError(string(rp.runes), rp.startOfValue, "invalid start value")
}
rp.strBuilder.Reset()
rp.state = rpsExpectEnd
default:
rp.strBuilder.WriteRune(current)
}
rp.index++
return nil
}
// handleExpectColon handles the parser's rpsExpectColon state. A colon MUST be
// read, otherwise an error will be returned. The parser will then switch to the
// rpsExpectEnd state.
func (rp *rangeParser) handleExpectColon(current rune) error {
switch current {
case ':':
rp.state = rpsExpectEnd
rp.index++
default:
return parseError(string(rp.runes), rp.index, "expected ':'")
}
return nil
}
// handleExpectEnd handles the parser's rpsExpectEnd state, which sets the
// position of the beginning of the "end" value, and jumps to the rpsInEnd
// state.
func (rp *rangeParser) handleExpectEnd() {
rp.startOfValue = rp.index
rp.state = rpsInEnd
}
// handleInEnd handles the parser's rpsInEnd state, which accumulates runes for
// the "end" value.
func (rp *rangeParser) handleInEnd(current rune) {
rp.strBuilder.WriteRune(current)
rp.index++
}
// consumeInput is the parser's state machine.
func (rp *rangeParser) consumeInput() error {
for rp.index < len(rp.runes) {
var err error
curRune := rp.runes[rp.index]
switch rp.state {
case rpsInit:
rp.handleInit(curRune)
case rpsExpectStart:
rp.handleExpectStart(curRune)
case rpsInStart:
err = rp.handleInStart(curRune)
case rpsExpectColon:
err = rp.handleExpectColon(curRune)
case rpsExpectEnd:
rp.handleExpectEnd()
case rpsInEnd:
rp.handleInEnd(curRune)
}
if err != nil {
return err
}
}
return nil
}
// Try to parse a string into a performance data range.
func ParseRange(input string) (*Range, error) {
parser := initRangeParser(input)
if err := parser.consumeInput(); err != nil {
return nil, err
}
if parser.state == rpsInStart {
// The range was a single value, so that's the upper bound.
parser.output.start = "0"
parser.state = rpsInEnd
}
if parser.state != rpsInEnd {
return nil, parseError(input, parser.index, "unexpected end of input")
}
parser.output.end = parser.strBuilder.String()
if !valueCheck.MatchString(parser.output.end) {
return nil, parseError(input, parser.startOfValue, "invalid end value")
}
return &parser.output, nil
}

View file

@ -0,0 +1,66 @@
package perfdata // import nocternity.net/gomonop/pkg/perfdata
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRangeParserOk(t *testing.T) {
type Test struct {
in string
out Range
}
tests := []Test{
{in: ":0", out: Range{start: "0", end: "0"}},
{in: "0:0", out: Range{start: "0", end: "0"}},
{in: "~:0", out: Range{start: "", end: "0"}},
{in: ":123", out: Range{start: "0", end: "123"}},
{in: "0:123", out: Range{start: "0", end: "123"}},
{in: "~:123", out: Range{start: "", end: "123"}},
{in: "1", out: Range{start: "0", end: "1"}},
{in: "@:0", out: Range{start: "0", end: "0", inside: true}},
{in: "@0:0", out: Range{start: "0", end: "0", inside: true}},
{in: "@~:0", out: Range{start: "", end: "0", inside: true}},
{in: "@:123", out: Range{start: "0", end: "123", inside: true}},
{in: "@0:123", out: Range{start: "0", end: "123", inside: true}},
{in: "@~:123", out: Range{start: "", end: "123", inside: true}},
{in: "@1", out: Range{start: "0", end: "1", inside: true}},
}
for _, test := range tests {
result, err := ParseRange(test.in)
require.NoError(t, err, "Expected no error, got '%v'", err)
assert.Equal(t, test.out, *result, "Expected '%v', got '%v'", test.out, *result)
}
}
func TestRangeParserError(t *testing.T) {
type Test struct {
in string
errPos int
}
tests := []Test{
{in: "", errPos: 0},
{in: ":", errPos: 1},
{in: "x:1", errPos: 0},
{in: ":~", errPos: 1},
{in: "@", errPos: 1},
{in: "@:", errPos: 2},
{in: "@x:1", errPos: 1},
{in: "@:~", errPos: 2},
}
for _, test := range tests {
result, err := ParseRange(test.in)
require.Error(t, err, "Expected error, got '%v'", err)
assert.Nil(t, result, "Expected nil result, got '%v'", result)
assert.True(
t, strings.Contains(err.Error(), fmt.Sprintf("at position %d", test.errPos)),
"Expected error to contain '%s', got '%s'", fmt.Sprintf("at position %d", test.errPos), err,
)
}
}

View file

@ -9,6 +9,7 @@ import (
"strings"
"nocternity.net/gomonop/pkg/perfdata"
"nocternity.net/gomonop/pkg/status"
)
// Results represents the monitoring plugin's results, including its name,
@ -16,7 +17,7 @@ import (
// data to be encoded in the output.
type Results struct {
name string
status Status
status status.Status
message string
extraText *list.List
perfData map[string]*perfdata.PerfData
@ -26,7 +27,7 @@ type Results struct {
func New(name string) *Results {
p := new(Results)
p.name = name
p.status = StatusUnknown
p.status = status.StatusUnknown
p.message = "no status set"
p.perfData = make(map[string]*perfdata.PerfData)
return p
@ -34,7 +35,7 @@ func New(name string) *Results {
// SetState sets the plugin's output code to `status` and its message to
// the specified `message`.
func (p *Results) SetState(status Status, message string) {
func (p *Results) SetState(status status.Status, message string) {
p.status = status
p.message = message
}

View file

@ -5,14 +5,16 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"nocternity.net/gomonop/pkg/perfdata"
"nocternity.net/gomonop/pkg/status"
)
func TestNew(t *testing.T) {
p := New("test")
assert.Equal(t, p.name, "test")
assert.Equal(t, p.status, StatusUnknown)
assert.Equal(t, p.status, status.StatusUnknown)
assert.Equal(t, p.message, "no status set")
assert.Nil(t, p.extraText)
assert.NotNil(t, p.perfData)
@ -21,9 +23,9 @@ func TestNew(t *testing.T) {
func TestSetState(t *testing.T) {
p := Results{}
p.SetState(StatusWarning, "test")
p.SetState(status.StatusWarning, "test")
assert.Equal(t, p.status, StatusWarning)
assert.Equal(t, p.status, status.StatusWarning)
assert.Equal(t, p.message, "test")
}
@ -96,7 +98,7 @@ func TestString(t *testing.T) {
{
Results{
name: "test",
status: StatusWarning,
status: status.StatusWarning,
message: "test",
perfData: make(map[string]*perfdata.PerfData),
},
@ -106,7 +108,7 @@ func TestString(t *testing.T) {
func() Results {
p := Results{
name: "test",
status: StatusWarning,
status: status.StatusWarning,
message: "test",
perfData: make(map[string]*perfdata.PerfData),
extraText: list.New(),
@ -121,7 +123,7 @@ func TestString(t *testing.T) {
func() Results {
p := Results{
name: "test",
status: StatusWarning,
status: status.StatusWarning,
message: "test",
perfData: make(map[string]*perfdata.PerfData),
}
@ -136,7 +138,7 @@ func TestString(t *testing.T) {
func() Results {
p := Results{
name: "test",
status: StatusWarning,
status: status.StatusWarning,
message: "test",
perfData: make(map[string]*perfdata.PerfData),
extraText: list.New(),
@ -161,15 +163,15 @@ func TestString(t *testing.T) {
func TestExitCode(t *testing.T) {
p := Results{}
p.status = StatusOK
assert.Equal(t, int(StatusOK), p.ExitCode())
p.status = status.StatusOK
assert.Equal(t, int(status.StatusOK), p.ExitCode())
p.status = StatusWarning
assert.Equal(t, int(StatusWarning), p.ExitCode())
p.status = status.StatusWarning
assert.Equal(t, int(status.StatusWarning), p.ExitCode())
p.status = StatusCritical
assert.Equal(t, int(StatusCritical), p.ExitCode())
p.status = status.StatusCritical
assert.Equal(t, int(status.StatusCritical), p.ExitCode())
p.status = StatusUnknown
assert.Equal(t, int(StatusUnknown), p.ExitCode())
p.status = status.StatusUnknown
assert.Equal(t, int(status.StatusUnknown), p.ExitCode())
}

View file

@ -1,4 +1,6 @@
package results // import nocternity.net/gomonop/pkg/results
// The status package contains the datatype that corresponds to monitoring
// plugin status values.
package status // import nocternity.net/gomonop/pkg/status
// Status represents the return status of the monitoring plugin. The
// corresponding integer value will be used as the program's exit code,

View file

@ -1,4 +1,4 @@
package results // import nocternity.net/gomonop/pkg/results
package status // import nocternity.net/gomonop/pkg/status
import (
"testing"