diff --git a/go.mod b/go.mod index 0f2b743..e5d6fab 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/atc0005/check-mail go 1.19 require ( - github.com/atc0005/go-nagios v0.18.1 + github.com/atc0005/go-nagios v0.19.0 github.com/emersion/go-imap v1.2.1 github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 github.com/google/go-cmp v0.6.0 diff --git a/go.sum b/go.sum index 7c63f36..d287b8c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/atc0005/go-nagios v0.18.1 h1:YGYNTyjNJiGXcCXYMhatHKzddjij06Peeb0va/wX45g= -github.com/atc0005/go-nagios v0.18.1/go.mod h1:n2RHhsrgI8xiapqkJ240dKLwMXWbWvkOPLE92x0IGaM= +github.com/atc0005/go-nagios v0.19.0 h1:QMqu4iAKb4SD/kdAJdnMYZjHL4KzfYOSq5Lq30AZc0U= +github.com/atc0005/go-nagios v0.19.0/go.mod h1:7XhhQHYOD+jZQVrTWXuWzSoPoDb6/FQh60dbGauQ5lQ= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/emersion/go-imap v1.2.1 h1:+s9ZjMEjOB8NzZMVTM3cCenz2JrQIGGo5j1df19WjTA= @@ -25,7 +25,7 @@ github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/sqs/go-xoauth2 v0.0.0-20120917012134-0911dad68e56 h1:KCgKdj+ha4CgnVHIiJYGKzgZk3HfCc6XssESfOa6atM= github.com/sqs/go-xoauth2 v0.0.0-20120917012134-0911dad68e56/go.mod h1:ghDEBrT4oFcM4rv18bzcZaAWXbHPGpDa4e2hh9oXL8A= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE= golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/vendor/github.com/atc0005/go-nagios/CHANGELOG.md b/vendor/github.com/atc0005/go-nagios/CHANGELOG.md index 07eb135..fdd0538 100644 --- a/vendor/github.com/atc0005/go-nagios/CHANGELOG.md +++ b/vendor/github.com/atc0005/go-nagios/CHANGELOG.md @@ -26,6 +26,16 @@ The following types of changes will be recorded in this file: - placeholder +## [v0.19.0] - 2024-11-27 + +### Added + +- (GH-330) Add plugin output size metric + +#### Dependency Updates + +- (GH-323) Go Dependency: Bump github.com/stretchr/testify from 1.9.0 to 1.10.0 + ## [v0.18.1] - 2024-11-23 ### Changed @@ -586,7 +596,8 @@ Initial package state - Nagios state map -[Unreleased]: https://github.com/atc0005/go-nagios/compare/v0.18.1...HEAD +[Unreleased]: https://github.com/atc0005/go-nagios/compare/v0.19.0...HEAD +[v0.19.0]: https://github.com/atc0005/go-nagios/releases/tag/v0.19.0 [v0.18.1]: https://github.com/atc0005/go-nagios/releases/tag/v0.18.1 [v0.18.0]: https://github.com/atc0005/go-nagios/releases/tag/v0.18.0 [v0.17.1]: https://github.com/atc0005/go-nagios/releases/tag/v0.17.1 diff --git a/vendor/github.com/atc0005/go-nagios/README.md b/vendor/github.com/atc0005/go-nagios/README.md index 91fe837..a3603f3 100644 --- a/vendor/github.com/atc0005/go-nagios/README.md +++ b/vendor/github.com/atc0005/go-nagios/README.md @@ -60,6 +60,9 @@ such time that the API is considered stable. - Panics from client code are captured and reported - panics are surfaced as `CRITICAL` state - service output and error details are overridden to make panics prominent +- Optional support for emitting performance data metric for plugin output size + - disabled by default + - can be toggled on by client code as desired - Optional support for emitting performance data generated by plugins - if not overridden by client code *and* if using the provided `nagios.NewPlugin()` constructor, a default `time` performance data metric diff --git a/vendor/github.com/atc0005/go-nagios/nagios.go b/vendor/github.com/atc0005/go-nagios/nagios.go index 4048116..08028c4 100644 --- a/vendor/github.com/atc0005/go-nagios/nagios.go +++ b/vendor/github.com/atc0005/go-nagios/nagios.go @@ -15,6 +15,7 @@ import ( "log" "os" "runtime/debug" + "strconv" "strings" "time" ) @@ -305,6 +306,11 @@ type Plugin struct { // instead. shouldSkipOSExit bool + // shouldEmitTotalPluginSizeMetric indicates whether client code has opted + // to emit (append) a performance data metric calculating the total plugin + // output size. + shouldEmitTotalPluginSizeMetric bool + // debugLogging is the collection of debug logging options for the plugin. debugLogging debugLoggingOptions @@ -589,6 +595,13 @@ func (p *Plugin) SkipOSExit() { p.shouldSkipOSExit = true } +// EnablePluginOutputSizePerfDataMetric appends a performance data metric +// noting the total plugin output size. +func (p *Plugin) EnablePluginOutputSizePerfDataMetric() { + p.logAction("Enabling total plugin output size metric as requested") + p.shouldEmitTotalPluginSizeMetric = true +} + // SetPayloadBytes uses the given input in bytes to overwrite any existing // content in the payload buffer. It returns the length of input and a // potential error. If given empty input the payload buffer is reset without @@ -704,6 +717,10 @@ func (p Plugin) emitOutput(pluginOutput string) { p.logAction("Writing plugin output") + if p.shouldEmitTotalPluginSizeMetric { + pluginOutput = addPluginOutputSizeMetric(pluginOutput) + } + // Attempt to write to output sink. If this fails, send error to the // default abort message output target. If that fails (however unlikely), // we have bigger problems and should abort. @@ -754,6 +771,49 @@ func (p *Plugin) tryAddDefaultTimeMetric() { p.logAction("Added default time metric to collection") } +// addPluginOutputSizeMetric appends a performance data metric to the given +// input noting the total plugin output size. If the metric is already present +// the original input is returned unmodified. +func addPluginOutputSizeMetric(pluginOutput string) string { + metricLabel := "plugin_output_size" + + // Attempt to prevent adding the same metric twice. + if strings.Contains(pluginOutput, metricLabel) { + return pluginOutput + } + + pluginOutput = strings.TrimSuffix(pluginOutput, CheckOutputEOL) + + outputSizeMetric := PerformanceData{ + Label: metricLabel, + Value: "PLACEHOLDER", + UnitOfMeasurement: "B", // bytes + } + + var modifiedPluginOutput string + + // Code snippet inspired by or generated with the help of ChatGPT, OpenAI. + for { + // Calculate the current length. + length := len(modifiedPluginOutput) + + // Construct metric using updated length value. + outputSizeMetric.Value = strconv.Itoa(length) + outputSizeMetricString := outputSizeMetric.String() + CheckOutputEOL + + // Construct modified plugin output using the updated metric. + modifiedPluginOutput = pluginOutput + outputSizeMetricString + + // Break when the actual length of modified plugin output containing + // the output size metric matches the calculated length. + if len(modifiedPluginOutput) == length { + break + } + } + + return modifiedPluginOutput +} + // defaultTimeMetric is a helper function that wraps the logic used to provide // a default performance data metric that tracks plugin execution time. func defaultTimeMetric(start time.Time) PerformanceData { diff --git a/vendor/github.com/atc0005/go-nagios/testdata/plugin-output-multiline-with-optional-perf-data-included.txt b/vendor/github.com/atc0005/go-nagios/testdata/plugin-output-multiline-with-optional-perf-data-included.txt new file mode 100644 index 0000000..c78e71c --- /dev/null +++ b/vendor/github.com/atc0005/go-nagios/testdata/plugin-output-multiline-with-optional-perf-data-included.txt @@ -0,0 +1,22 @@ +OK: Datastore HUSVM-DC1-vol6 space usage (0 VMs) is 0.01% of 18.0TB with 18.0TB remaining [WARNING: 90% , CRITICAL: 95%] +**THRESHOLDS** + +* CRITICAL: 95% datastore usage +* WARNING: 90% datastore usage + +**DETAILED INFO** + +Datastore Space Summary: + +* Name: HUSVM-DC1-vol6 +* Space Used: 2.3GB (0.01%) +* Space Remaining: 18.0TB (99.99%) +* VMs: 0 + + +--- + +* vSphere environment: https://vc1.example.com:443/sdk +* Plugin User Agent: check-vmware/v0.30.6-0-g25fdcdc + + | 'time'=874ms;;;; 'plugin_output_size'=530B;;;; diff --git a/vendor/github.com/atc0005/go-nagios/testdata/plugin-output-one-line-with-optional-perf-data-included.txt b/vendor/github.com/atc0005/go-nagios/testdata/plugin-output-one-line-with-optional-perf-data-included.txt new file mode 100644 index 0000000..2807376 --- /dev/null +++ b/vendor/github.com/atc0005/go-nagios/testdata/plugin-output-one-line-with-optional-perf-data-included.txt @@ -0,0 +1 @@ +OK: Datastore HUSVM-DC1-vol6 space usage (0 VMs) is 0.01% of 18.0TB with 18.0TB remaining [WARNING: 90% , CRITICAL: 95%] | 'time'=874ms;;;; 'plugin_output_size'=171B;;;; diff --git a/vendor/modules.txt b/vendor/modules.txt index d5ed8b2..bd2d8cf 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# github.com/atc0005/go-nagios v0.18.1 +# github.com/atc0005/go-nagios v0.19.0 ## explicit; go 1.19 github.com/atc0005/go-nagios # github.com/emersion/go-imap v1.2.1