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 (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
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-19 23:16:57 +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-19 23:16:57 +02:00
|
|
|
func (d *PerfData) SetWarn(r *Range) {
|
2021-01-03 10:33:21 +01:00
|
|
|
d.warn = *r
|
2024-07-19 22:01:34 +02:00
|
|
|
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-19 23:16:57 +02:00
|
|
|
func (d *PerfData) SetCrit(r *Range) {
|
2021-01-03 10:33:21 +01:00
|
|
|
d.crit = *r
|
2024-07-19 22:01:34 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString("'")
|
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-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString("'")
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString("=")
|
|
|
|
strBuilder.WriteString(fmt.Sprintf("%s%s;", d.value, d.units.String()))
|
|
|
|
if d.bits&PDatWarn != 0 {
|
|
|
|
strBuilder.WriteString(d.warn.String())
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString(";")
|
|
|
|
if d.bits&PDatCrit != 0 {
|
|
|
|
strBuilder.WriteString(d.crit.String())
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString(";")
|
|
|
|
if d.bits&PDatMin != 0 {
|
|
|
|
strBuilder.WriteString(d.min)
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString(";")
|
|
|
|
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
|
|
|
}
|