From 70670e0657008e4dd427e52c144b30c805d649c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20BENO=C3=8ET?= Date: Fri, 19 Jul 2024 23:14:59 +0200 Subject: [PATCH] refactor(pkg): split `perfdata` across multiple files --- pkg/perfdata/internals.go | 25 ++++++++++ pkg/perfdata/perfdata.go | 101 -------------------------------------- pkg/perfdata/range.go | 62 +++++++++++++++++++++++ pkg/perfdata/units.go | 20 ++++++++ 4 files changed, 107 insertions(+), 101 deletions(-) create mode 100644 pkg/perfdata/internals.go create mode 100644 pkg/perfdata/range.go create mode 100644 pkg/perfdata/units.go diff --git a/pkg/perfdata/internals.go b/pkg/perfdata/internals.go new file mode 100644 index 0000000..0f5f0da --- /dev/null +++ b/pkg/perfdata/internals.go @@ -0,0 +1,25 @@ +package perfdata // import nocternity.net/gomonop/pkg/perfdata + +import ( + "regexp" +) + +// Flags indicating which elements of performance data have been set. +type perfDataBits int + +const ( + PDatWarn perfDataBits = 1 << iota + PDatCrit + PDatMin + PDatMax +) + +// Regexps used to check values and ranges in performance data records. +var ( + // Common value check regexp. + vcRegexp = `^-?(0(\.\d*)?|[1-9]\d*(\.\d*)?|\.\d+)$` + // Compiled value check regexp. + valueCheck = regexp.MustCompile(vcRegexp) + // Compiled range min value check. + rangeMinCheck = regexp.MustCompile(vcRegexp + `|^~$`) +) diff --git a/pkg/perfdata/perfdata.go b/pkg/perfdata/perfdata.go index bc7195b..b1943f3 100644 --- a/pkg/perfdata/perfdata.go +++ b/pkg/perfdata/perfdata.go @@ -4,110 +4,9 @@ package perfdata // import nocternity.net/gomonop/pkg/perfdata import ( "fmt" - "regexp" "strings" ) -// Units of measurement, which may be used to qualify the performance data. -type UnitOfMeasurement int - -const ( - UomNone UnitOfMeasurement = iota - UomSeconds - UomPercent - UomBytes - UomKilobytes - UomMegabytes - UomGigabytes - UomTerabytes - UomCounter -) - -func (u UnitOfMeasurement) String() string { - return [...]string{"", "s", "%", "B", "KB", "MB", "GB", "TB", "c"}[u] -} - -// Flags indicating which elements of performance data have been set. -type perfDataBits int - -const ( - PDatWarn perfDataBits = 1 << iota - PDatCrit - PDatMin - PDatMax -) - -// Regexps used to check values and ranges in performance data records. -var ( - // Common value check regexp. - vcRegexp = `^-?(0(\.\d*)?|[1-9]\d*(\.\d*)?|\.\d+)$` - // Compiled value check regexp. - valueCheck = regexp.MustCompile(vcRegexp) - // Compiled range min value check. - rangeMinCheck = regexp.MustCompile(vcRegexp + `|^~$`) -) - -// 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 -} - // Performance data, including a label, units, a value, warning/critical // ranges and min/max boundaries. type PerfData struct { diff --git a/pkg/perfdata/range.go b/pkg/perfdata/range.go new file mode 100644 index 0000000..0c7a3a4 --- /dev/null +++ b/pkg/perfdata/range.go @@ -0,0 +1,62 @@ +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 +} diff --git a/pkg/perfdata/units.go b/pkg/perfdata/units.go new file mode 100644 index 0000000..604625c --- /dev/null +++ b/pkg/perfdata/units.go @@ -0,0 +1,20 @@ +package perfdata // import nocternity.net/gomonop/pkg/perfdata + +// Units of measurement, which may be used to qualify the performance data. +type UnitOfMeasurement int + +const ( + UomNone UnitOfMeasurement = iota + UomSeconds + UomPercent + UomBytes + UomKilobytes + UomMegabytes + UomGigabytes + UomTerabytes + UomCounter +) + +func (u UnitOfMeasurement) String() string { + return [...]string{"", "s", "%", "B", "KB", "MB", "GB", "TB", "c"}[u] +}