-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from atc0005/i251-add-support-for-encoded-pay…
…loads Add support for embedded/encoded payloads
- Loading branch information
Showing
38 changed files
with
2,904 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2020 Adam Chalkley | ||
// | ||
// https://github.com/atc0005/go-nagios | ||
// | ||
// Licensed under the MIT License. See LICENSE file in the project root for | ||
// full license information. | ||
|
||
package nagios_test | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/atc0005/go-nagios" | ||
) | ||
|
||
// Ignore this. This is just to satisfy the "whole file" example requirements | ||
// per https://go.dev/blog/examples. | ||
var _ = "https://github.com/atc0005/go-nagios" | ||
|
||
// Example_addEncodedPayload demonstrates adding an encoded payload to plugin | ||
// output (e.g., for potential later retrieval via the monitor systems' API). | ||
func Example_addEncodedPayload() { | ||
// First, create an instance of the Plugin type. By default this value is | ||
// configured to indicate a successful execution. This should be | ||
// overridden by client code to indicate the final plugin state to Nagios | ||
// when the plugin exits. | ||
var plugin = nagios.NewPlugin() | ||
|
||
// Second, immediately defer ReturnCheckResults() so that it runs as the | ||
// last step in your client code. If you do not defer ReturnCheckResults() | ||
// immediately any other deferred functions in your client code will not | ||
// run. | ||
// | ||
// Avoid calling os.Exit() directly from your code. If you do, this | ||
// library is unable to function properly; this library expects that it | ||
// will handle calling os.Exit() with the required exit code (and | ||
// specifically formatted output). | ||
// | ||
// For handling error cases, the approach is roughly the same, only you | ||
// call return explicitly to end execution of the client code and allow | ||
// deferred functions to run. | ||
defer plugin.ReturnCheckResults() | ||
|
||
// more stuff here involving performing the actual service check | ||
|
||
// This simple JSON structure represents a more detailed blob of data that | ||
// might need post-processing once retrieved from the monitoring system's | ||
// API, etc. | ||
// | ||
// Imagine this is something like a full certificate chain or a system's | ||
// state and not easily represented by performance data metrics (e.g., | ||
// best represented in a structured format once later extracted and | ||
// decoded). | ||
sampleComplexData := `{"Age":17,"Interests":["books","games", "Crystal Stix"]}` | ||
|
||
if _, err := plugin.AddPayloadString(sampleComplexData); err != nil { | ||
log.Printf("failed to add encoded payload: %v", err) | ||
plugin.Errors = append(plugin.Errors, err) | ||
|
||
return | ||
} | ||
|
||
//nolint:goconst | ||
plugin.ServiceOutput = "one-line summary of plugin results " | ||
|
||
//nolint:goconst | ||
plugin.LongServiceOutput = "more detailed output from plugin here" | ||
|
||
// more stuff here involving wrapping up the service check | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2020 Adam Chalkley | ||
// | ||
// https://github.com/atc0005/go-nagios | ||
// | ||
// Licensed under the MIT License. See LICENSE file in the project root for | ||
// full license information. | ||
|
||
package nagios_test | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/atc0005/go-nagios" | ||
) | ||
|
||
// Ignore this. This is just to satisfy the "whole file" example requirements | ||
// per https://go.dev/blog/examples. | ||
var _ = "https://github.com/atc0005/go-nagios" | ||
|
||
// Example_extractEncodedPayload represents a sample client application that | ||
// extracts a previously encoded payload from plugin output (e.g., retrieved | ||
// via the monitoring system's API or a log file). | ||
func Example_extractEncodedPayload() { | ||
// This represents the data before it was encoded and added to the plugin | ||
// output. | ||
origData := `{"Age":17,"Interests":["books","games", "Crystal Stix"]}` | ||
|
||
// This represents the encoded data that was previously added to the | ||
// plugin output. | ||
encodedData := `<~HQkagAKj/i2_6.EDKKH1ATMs7,!&pP@W-1#F!<.ZB45XgF!<.X,"$BrF*(i,+B*ArGTpFA~>` | ||
|
||
sampleServiceOutput := "one-line summary of plugin results " | ||
sampleLongServiceOutput := "detailed text line1\ndetailed text line2\ndetailed text line3" | ||
sampleMetricsOutput := `| 'time'=874ms;;;;` | ||
|
||
// This represents the original plugin output captured by the monitoring | ||
// system which we retrieved via the monitoring system's API, a log file, | ||
// etc. | ||
originalPluginOutput := fmt.Sprintf( | ||
"%s%s%s%s%s%s%s", | ||
sampleServiceOutput, | ||
sampleLongServiceOutput, | ||
nagios.CheckOutputEOL, | ||
encodedData, | ||
nagios.CheckOutputEOL, | ||
sampleMetricsOutput, | ||
nagios.CheckOutputEOL, | ||
) | ||
|
||
decodedPayload, err := nagios.ExtractAndDecodeASCII85Payload( | ||
originalPluginOutput, | ||
"", | ||
nagios.DefaultASCII85EncodingDelimiterLeft, | ||
nagios.DefaultASCII85EncodingDelimiterRight, | ||
) | ||
|
||
if err != nil { | ||
log.Println("Failed to extract and decode payload from original plugin output", err) | ||
|
||
os.Exit(1) | ||
} | ||
|
||
// We compare here for illustration purposes, but in many cases you may | ||
// not have access to the data in its original form as collected by the | ||
// monitoring plugin (e.g., it was generated dynamically or retrieved from | ||
// a remote system's state and not stored long-term). | ||
if decodedPayload != origData { | ||
log.Println("Extracted & decoded payload data does not match original data") | ||
|
||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("Original data:", decodedPayload) | ||
|
||
// Output: | ||
// | ||
// Original data: {"Age":17,"Interests":["books","games", "Crystal Stix"]} | ||
} |
Oops, something went wrong.