gomonop/pkg/perfdata/perfdata.go

99 lines
2.3 KiB
Go
Raw Normal View History

// Package `perfdata` provides representations for a monitoring plugin's
// performance data.
package perfdata // import nocternity.net/gomonop/pkg/perfdata
import (
"fmt"
"strings"
)
// Performance data, including a label, units, a value, warning/critical
// ranges and min/max boundaries.
type PerfData struct {
Label string
units UnitOfMeasurement
bits perfDataBits
value string
warn, crit Range
min, max string
}
// Create performance data using the specified label and units.
func New(label string, units UnitOfMeasurement, value string) *PerfData {
if value != "" && !valueCheck.MatchString(value) {
panic("invalid value")
}
pdRange := &PerfData{}
pdRange.Label = label
pdRange.units = units
if value == "" {
pdRange.value = "U"
} else {
pdRange.value = value
}
return pdRange
}
// Set the warning range for the performance data record.
func (d *PerfData) SetWarn(r *Range) {
d.warn = *r
d.bits |= PDatWarn
}
// Set the critical range for the performance data record.
func (d *PerfData) SetCrit(r *Range) {
d.crit = *r
d.bits |= PDatCrit
}
// Set the performance data's minimal value.
func (d *PerfData) SetMin(min string) {
if !valueCheck.MatchString(min) {
panic("invalid value")
}
d.min = min
d.bits |= PDatMin
}
// Set the performance data's maximal value.
func (d *PerfData) SetMax(max string) {
if !valueCheck.MatchString(max) {
panic("invalid value")
}
d.max = max
d.bits |= PDatMax
}
// Converts performance data to a string which may be read by the monitoring
// system.
func (d *PerfData) String() string {
var strBuilder strings.Builder
needsQuotes := strings.ContainsAny(d.Label, " '=\"")
if needsQuotes {
strBuilder.WriteString("'")
}
strBuilder.WriteString(strings.ReplaceAll(d.Label, "'", "''"))
if needsQuotes {
strBuilder.WriteString("'")
}
strBuilder.WriteString("=")
strBuilder.WriteString(fmt.Sprintf("%s%s;", d.value, d.units.String()))
if d.bits&PDatWarn != 0 {
strBuilder.WriteString(d.warn.String())
}
strBuilder.WriteString(";")
if d.bits&PDatCrit != 0 {
strBuilder.WriteString(d.crit.String())
}
strBuilder.WriteString(";")
if d.bits&PDatMin != 0 {
strBuilder.WriteString(d.min)
}
strBuilder.WriteString(";")
if d.bits&PDatMax != 0 {
strBuilder.WriteString(d.max)
}
return strBuilder.String()
}