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>
This commit is contained in:
parent
9fac656cdf
commit
c46c9d76d9
22 changed files with 1063 additions and 55 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
package results // import nocternity.net/gomonop/pkg/results
|
||||
|
||||
// Status represents the return status of the monitoring plugin. The
|
||||
// corresponding integer value will be used as the program's exit code,
|
||||
// to be interpreted by the monitoring system.
|
||||
type Status int
|
||||
|
||||
// Plugin exit statuses.
|
||||
const (
|
||||
StatusOK Status = iota
|
||||
StatusWarning
|
||||
StatusCritical
|
||||
StatusUnknown
|
||||
)
|
||||
|
||||
// String representations of the plugin statuses.
|
||||
func (s Status) String() string {
|
||||
return [...]string{"OK", "WARNING", "ERROR", "UNKNOWN"}[s]
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package results // import nocternity.net/gomonop/pkg/results
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStatusDefaultOK(t *testing.T) {
|
||||
var s Status
|
||||
assert.Equal(t, StatusOK, s)
|
||||
}
|
||||
|
||||
func TestStatusToString(t *testing.T) {
|
||||
assert.Equal(t, "OK", StatusOK.String())
|
||||
assert.Equal(t, "WARNING", StatusWarning.String())
|
||||
assert.Equal(t, "ERROR", StatusCritical.String())
|
||||
assert.Equal(t, "UNKNOWN", StatusUnknown.String())
|
||||
}
|
||||
|
||||
func TestStatusToInt(t *testing.T) {
|
||||
assert.Equal(t, 0, int(StatusOK))
|
||||
assert.Equal(t, 1, int(StatusWarning))
|
||||
assert.Equal(t, 2, int(StatusCritical))
|
||||
assert.Equal(t, 3, int(StatusUnknown))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue