feat(pkg): add status check method to PerfData
This commit is contained in:
parent
3263d8c583
commit
ee05d6f004
2 changed files with 50 additions and 0 deletions
|
@ -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
|
||||
|
@ -71,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 {
|
||||
|
|
|
@ -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) {
|
||||
|
@ -167,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
|
||||
|
|
Loading…
Reference in a new issue