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"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Units of measurement, which may be used to qualify the performance data.
|
2021-01-03 10:33:21 +01:00
|
|
|
type UnitOfMeasurement int
|
|
|
|
|
|
|
|
const (
|
2024-07-19 22:01:34 +02:00
|
|
|
UomNone UnitOfMeasurement = iota
|
|
|
|
UomSeconds
|
|
|
|
UomPercent
|
|
|
|
UomBytes
|
|
|
|
UomKilobytes
|
|
|
|
UomMegabytes
|
|
|
|
UomGigabytes
|
|
|
|
UomTerabytes
|
|
|
|
UomCounter
|
2021-01-03 10:33:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (u UnitOfMeasurement) String() string {
|
|
|
|
return [...]string{"", "s", "%", "B", "KB", "MB", "GB", "TB", "c"}[u]
|
|
|
|
}
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Flags indicating which elements of performance data have been set.
|
|
|
|
type perfDataBits int
|
2021-01-03 10:33:21 +01:00
|
|
|
|
|
|
|
const (
|
2024-07-19 22:01:34 +02:00
|
|
|
PDatWarn perfDataBits = 1 << iota
|
|
|
|
PDatCrit
|
|
|
|
PDatMin
|
|
|
|
PDatMax
|
2021-01-03 10:33:21 +01:00
|
|
|
)
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Regexps used to check values and ranges in performance data records.
|
2021-01-03 10:33:21 +01:00
|
|
|
var (
|
2024-07-19 22:01:34 +02:00
|
|
|
// Common value check regexp.
|
2021-01-07 20:55:10 +01:00
|
|
|
vcRegexp = `^-?(0(\.\d*)?|[1-9]\d*(\.\d*)?|\.\d+)$`
|
2024-07-19 22:01:34 +02:00
|
|
|
// Compiled value check regexp.
|
2021-01-07 20:55:10 +01:00
|
|
|
valueCheck = regexp.MustCompile(vcRegexp)
|
2024-07-19 22:01:34 +02:00
|
|
|
// Compiled range min value check.
|
2021-01-07 20:55:10 +01:00
|
|
|
rangeMinCheck = regexp.MustCompile(vcRegexp + `|^~$`)
|
2021-01-03 10:33:21 +01:00
|
|
|
)
|
|
|
|
|
2024-07-19 22:01:34 +02:00
|
|
|
// Performance data range.
|
|
|
|
type PDRange struct {
|
2021-01-03 10:33:21 +01:00
|
|
|
start string
|
|
|
|
end string
|
|
|
|
inside bool
|
|
|
|
}
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Creates a performance data range from -inf to 0 and from the specified
|
|
|
|
// value to +inf.
|
2024-07-19 22:01:34 +02:00
|
|
|
func PDRMax(max string) *PDRange {
|
2021-01-03 10:33:21 +01:00
|
|
|
if !valueCheck.MatchString(max) {
|
|
|
|
panic("invalid performance data range maximum value")
|
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
pdRange := &PDRange{}
|
|
|
|
pdRange.start = "0"
|
|
|
|
pdRange.end = max
|
|
|
|
return pdRange
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Creates a performance data range from -inf to the specified minimal value
|
|
|
|
// and from the specified maximal value to +inf.
|
2024-07-19 22:01:34 +02:00
|
|
|
func PDRMinMax(min, max string) *PDRange {
|
2021-01-03 10:33:21 +01:00
|
|
|
if !valueCheck.MatchString(max) {
|
|
|
|
panic("invalid performance data range maximum value")
|
|
|
|
}
|
|
|
|
if !rangeMinCheck.MatchString(min) {
|
|
|
|
panic("invalid performance data range minimum value")
|
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
pdRange := &PDRange{}
|
|
|
|
pdRange.start = min
|
|
|
|
pdRange.end = max
|
|
|
|
return pdRange
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Inverts the range.
|
2024-07-19 22:01:34 +02:00
|
|
|
func (r *PDRange) Inside() *PDRange {
|
2021-01-03 10:33:21 +01:00
|
|
|
r.inside = true
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-01-07 20:55:10 +01:00
|
|
|
// Generates the range's string representation so it can be sent to the
|
|
|
|
// monitoring system.
|
2024-07-19 22:01:34 +02:00
|
|
|
func (r *PDRange) String() string {
|
2021-01-03 10:33:21 +01:00
|
|
|
var start, inside string
|
2024-07-19 22:01:34 +02:00
|
|
|
|
|
|
|
switch r.start {
|
|
|
|
case "":
|
2021-01-03 10:33:21 +01:00
|
|
|
start = "~"
|
2024-07-19 22:01:34 +02:00
|
|
|
case "0":
|
2021-01-03 10:33:21 +01:00
|
|
|
start = ""
|
2024-07-19 22:01:34 +02:00
|
|
|
default:
|
2021-01-03 10:33:21 +01:00
|
|
|
start = r.start
|
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
|
2021-01-03 10:33:21 +01:00
|
|
|
if r.inside {
|
|
|
|
inside = "@"
|
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
|
|
|
|
return inside + start + ":" + r.end
|
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-19 22:01:34 +02:00
|
|
|
warn, crit PDRange
|
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 22:01:34 +02:00
|
|
|
func (d *PerfData) SetWarn(r *PDRange) {
|
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 22:01:34 +02:00
|
|
|
func (d *PerfData) SetCrit(r *PDRange) {
|
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
|
|
|
}
|