Documentation ¶
Overview ¶
Package nagios provides common types and package-level variables for use with Nagios plugins.
OVERVIEW ¶
This package contains common types and package-level variables. The intent is to reduce code duplication between various plugins that we maintain.
PROJECT HOME ¶
See our GitHub repo (https://github.com/atc0005/go-nagios) for the latest code, to file an issue or submit improvements for review and potential inclusion into the project.
FEATURES ¶
• Nagios state labels (e.g., `StateOKLabel`), state exit codes (e.g., `StateOKExitCode`)
• `ExitState` type with `ReturnCheckResults` method used to process and return all applicable check results to Nagios for further processing/display
HOW TO USE ¶
Assuming that you're using Go Modules (https://blog.golang.org/using-go-modules), add this line to your imports like so:
package main import ( "fmt" "log" "os" "github.com/atc0005/go-nagios" )
Then in your code reference the data types as you would from any other package:
fmt.Println("OK: All checks have passed") os.Exit(nagios.StateOK)
When you next build your package this one should be pulled in.
See the README for this project for further examples.
Package nagios is a small collection of common types and package-level variables intended for use with various plugins to reduce code duplication.
Index ¶
Constants ¶
const ( StateOKExitCode int = 0 StateWARNINGExitCode int = 1 StateCRITICALExitCode int = 2 StateUNKNOWNExitCode int = 3 StateDEPENDENTExitCode int = 4 )
Nagios plugin/service check states. These constants replicate the values from utils.sh which is normally found at one of these two locations, depending on which Linux distribution you're using:
/usr/lib/nagios/plugins/utils.sh /usr/local/nagios/libexec/utils.sh
See also http://nagios-plugins.org/doc/guidelines.html
const ( StateOKLabel string = "OK" StateWARNINGLabel string = "WARNING" StateCRITICALLabel string = "CRITICAL" StateUNKNOWNLabel string = "UNKNOWN" StateDEPENDENTLabel string = "DEPENDENT" )
Nagios plugin/service check state "labels". These constants are provided as an alternative to using literal state strings throughout client application code.
const CheckOutputEOL string = "\r\n"
CheckOutputEOL is the newline used with formatted service and host check output. Based on previous testing, Nagios treats LF newlines within the `$LONGSERVICEOUTPUT$` macro as literal values instead of parsing them for display purposes. Using DOS EOL values with `fmt.Printf()` gives the results that we're looking for with that output and (presumably) host check output as well.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExitCallBackFunc ¶ added in v0.4.0
type ExitCallBackFunc func() string
ExitCallBackFunc represents a function that is called as a final step before application termination so that branding information can be emitted for inclusion in the notification. This helps identify which specific application (and its version) that is responsible for the notification.
type ExitState ¶ added in v0.4.0
type ExitState struct { // LastError is the last error encountered which should be reported as // part of ending the service check (e.g., "Failed to connect to XYZ to // check contents of Inbox"). LastError error // ExitStatusCode is the exit or exit status code provided to the Nagios // instance that calls this service check. These status codes indicate to // Nagios "state" the service is considered to be in. The most common // states are OK (0), WARNING (1) and CRITICAL (2). ExitStatusCode int // ServiceOutput is the first line of text output from the last service // check (i.e. "Ping OK"). ServiceOutput string // LongServiceOutput is the full text output (aside from the first line) // from the last service check. LongServiceOutput string // WarningThreshold is the value used to determine when the service check // has crossed between an existing state into a WARNING state. This value // is used for display purposes. WarningThreshold string // CriticalThreshold is the value used to determine when the service check // has crossed between an existing state into a CRITICAL state. This value // is used for display purposes. CriticalThreshold string // BrandingCallback is a function that is called before application // termination to emit branding details at the end of the notification. // See also ExitCallBackFunc. BrandingCallback ExitCallBackFunc }
ExitState represents the last known execution state of the application, including the most recent error and the final intended plugin state.
func (*ExitState) ReturnCheckResults ¶ added in v0.4.0
func (es *ExitState) ReturnCheckResults()
ReturnCheckResults is intended to provide a reliable way to return a desired exit code from applications used as Nagios plugins. In most cases, this method should be registered as the first deferred function in client code. See remarks regarding "masking" or "swallowing" application panics.
Since Nagios relies on plugin exit codes to determine success/failure of checks, the approach that is most often used with other languages is to use something like Using os.Exit() directly and force an early exit of the application with an explicit exit code. Using os.Exit() directly in Go does not run deferred functions. Go-based plugins that do not rely on deferring function calls may be able to use os.Exit(), but introducing new dependencies later could introduce problems if those dependencies rely on deferring functions.
Before calling this method, client code should first set appropriate field values on the receiver. When called, this method will process them and exit with the desired exit code and status output.
To repeat, if scheduled via defer, this method should be registered first; because this method calls os.Exit to set the intended plugin exit state, no other deferred functions will have an opportunity to run, so register this method first so that when deferred, it will be run last (FILO).
Because this method is (or should be) deferred first within client code, it will run after all other deferred functions. It will also run before a panic in client code forces the application to exit. As already noted, this method calls os.Exit to set the plugin exit state. Because os.Exit forces the application to terminate immediately without running other deferred functions or processing panics, this "masks", "swallows" or "blocks" panics from client code from surfacing. This method checks for unhandled panics and if found, overrides exit state details from client code and surfaces details from the panic instead as a CRITICAL state.
type ServiceState ¶ added in v0.7.0
type ServiceState struct { // Label maps directly to one of the supported Nagios state labels. Label string // ExitCode is the exit or exit status code associated with a Nagios // service check. ExitCode int }
ServiceState represents the status label and exit code for a service check.