2021-01-07 20:55:10 +01:00
|
|
|
// Package `perfdata` provides representations for a monitoring plugin's
|
|
|
|
// performance data.
|
2024-07-19 22:01:34 +02:00
|
|
|
package perfdata // import nocternity.net/gomonop/pkg/perfdata
|
2021-01-03 10:33:21 +01:00
|
|
|
|
|
|
|
import (
|
2024-07-20 19:12:59 +02:00
|
|
|
"strconv"
|
2021-01-03 10:33:21 +01:00
|
|
|
"strings"
|
2024-07-20 19:12:59 +02:00
|
|
|
|
|
|
|
"nocternity.net/gomonop/pkg/status"
|
2021-01-03 10:33:21 +01:00
|
|
|
)
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Performance data, including a label, units, a value, warning/critical
|
|
|
|
// ranges and min/max boundaries.
|
2021-01-03 10:33:21 +01:00
|
|
|
type PerfData struct {
|
|
|
|
Label string
|
|
|
|
units UnitOfMeasurement
|
2021-01-07 20:55:10 +01:00
|
|
|
bits perfDataBits
|
2021-01-03 10:33:21 +01:00
|
|
|
value string
|
2024-07-20 10:01:05 +02:00
|
|
|
warn, crit Range
|
2021-01-03 10:33:21 +01:00
|
|
|
min, max string
|
|
|
|
}
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Create performance data using the specified label and units.
|
|
|
|
func New(label string, units UnitOfMeasurement, value string) *PerfData {
|
2021-01-03 10:33:21 +01:00
|
|
|
if value != "" && !valueCheck.MatchString(value) {
|
|
|
|
panic("invalid value")
|
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
pdRange := &PerfData{}
|
|
|
|
pdRange.Label = label
|
|
|
|
pdRange.units = units
|
2021-01-03 10:33:21 +01:00
|
|
|
if value == "" {
|
2024-07-19 22:01:34 +02:00
|
|
|
pdRange.value = "U"
|
2021-01-03 10:33:21 +01:00
|
|
|
} else {
|
2024-07-19 22:01:34 +02:00
|
|
|
pdRange.value = value
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
return pdRange
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Set the warning range for the performance data record.
|
2024-07-20 10:01:05 +02:00
|
|
|
func (d *PerfData) SetWarn(r *Range) {
|
2024-07-20 18:11:06 +02:00
|
|
|
if r == nil {
|
|
|
|
d.bits &^= PDatWarn
|
|
|
|
} else {
|
|
|
|
d.warn = *r
|
|
|
|
d.bits |= PDatWarn
|
|
|
|
}
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Set the critical range for the performance data record.
|
2024-07-20 10:01:05 +02:00
|
|
|
func (d *PerfData) SetCrit(r *Range) {
|
2024-07-20 18:11:06 +02:00
|
|
|
if r == nil {
|
|
|
|
d.bits &^= PDatCrit
|
|
|
|
} else {
|
|
|
|
d.crit = *r
|
|
|
|
d.bits |= PDatCrit
|
|
|
|
}
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2024-07-19 22:01:34 +02:00
|
|
|
// Set the performance data's minimal value.
|
2021-01-03 10:33:21 +01:00
|
|
|
func (d *PerfData) SetMin(min string) {
|
|
|
|
if !valueCheck.MatchString(min) {
|
|
|
|
panic("invalid value")
|
|
|
|
}
|
|
|
|
d.min = min
|
2024-07-19 22:01:34 +02:00
|
|
|
d.bits |= PDatMin
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Set the performance data's maximal value.
|
2021-01-03 10:33:21 +01:00
|
|
|
func (d *PerfData) SetMax(max string) {
|
|
|
|
if !valueCheck.MatchString(max) {
|
|
|
|
panic("invalid value")
|
|
|
|
}
|
|
|
|
d.max = max
|
2024-07-19 22:01:34 +02:00
|
|
|
d.bits |= PDatMax
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2024-07-20 19:12:59 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Converts performance data to a string which may be read by the monitoring
|
|
|
|
// system.
|
|
|
|
func (d *PerfData) String() string {
|
2024-07-19 22:01:34 +02:00
|
|
|
var strBuilder strings.Builder
|
2021-01-03 10:33:21 +01:00
|
|
|
needsQuotes := strings.ContainsAny(d.Label, " '=\"")
|
|
|
|
if needsQuotes {
|
2024-07-20 10:01:05 +02:00
|
|
|
strBuilder.WriteRune('\'')
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString(strings.ReplaceAll(d.Label, "'", "''"))
|
2021-01-03 10:33:21 +01:00
|
|
|
if needsQuotes {
|
2024-07-20 10:01:05 +02:00
|
|
|
strBuilder.WriteRune('\'')
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
2024-07-20 10:01:05 +02:00
|
|
|
strBuilder.WriteRune('=')
|
|
|
|
strBuilder.WriteString(d.value)
|
|
|
|
strBuilder.WriteString(d.units.String())
|
|
|
|
strBuilder.WriteRune(';')
|
2024-07-19 22:01:34 +02:00
|
|
|
if d.bits&PDatWarn != 0 {
|
|
|
|
strBuilder.WriteString(d.warn.String())
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
2024-07-20 10:01:05 +02:00
|
|
|
strBuilder.WriteRune(';')
|
2024-07-19 22:01:34 +02:00
|
|
|
if d.bits&PDatCrit != 0 {
|
|
|
|
strBuilder.WriteString(d.crit.String())
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
2024-07-20 10:01:05 +02:00
|
|
|
strBuilder.WriteRune(';')
|
2024-07-19 22:01:34 +02:00
|
|
|
if d.bits&PDatMin != 0 {
|
|
|
|
strBuilder.WriteString(d.min)
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
2024-07-20 10:01:05 +02:00
|
|
|
strBuilder.WriteRune(';')
|
2024-07-19 22:01:34 +02:00
|
|
|
if d.bits&PDatMax != 0 {
|
|
|
|
strBuilder.WriteString(d.max)
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2024-07-19 22:01:34 +02:00
|
|
|
return strBuilder.String()
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|