gomonop/pkg/perfdata/range.go

62 lines
1.3 KiB
Go

package perfdata // import nocternity.net/gomonop/pkg/perfdata
// Performance data range.
type PDRange struct {
start string
end string
inside bool
}
// Creates a performance data range from -inf to 0 and from the specified
// value to +inf.
func PDRMax(max string) *PDRange {
if !valueCheck.MatchString(max) {
panic("invalid performance data range maximum value")
}
pdRange := &PDRange{}
pdRange.start = "0"
pdRange.end = max
return pdRange
}
// Creates a performance data range from -inf to the specified minimal value
// and from the specified maximal value to +inf.
func PDRMinMax(min, max string) *PDRange {
if !valueCheck.MatchString(max) {
panic("invalid performance data range maximum value")
}
if !rangeMinCheck.MatchString(min) {
panic("invalid performance data range minimum value")
}
pdRange := &PDRange{}
pdRange.start = min
pdRange.end = max
return pdRange
}
// Inverts the range.
func (r *PDRange) Inside() *PDRange {
r.inside = true
return r
}
// Generates the range's string representation so it can be sent to the
// monitoring system.
func (r *PDRange) String() string {
var start, inside string
switch r.start {
case "":
start = "~"
case "0":
start = ""
default:
start = r.start
}
if r.inside {
inside = "@"
}
return inside + start + ":" + r.end
}