Documentation
¶
Index ¶
- Constants
- Variables
- func BaseExit(rc Status)
- func BoundaryToString(value float64) (s string)
- func CatchPanic()
- func Compare(a Status, b Status) int
- func Exit(rc Status, output ...string)
- func ExitError(err error)
- func FormatFloat(value float64) string
- func HandleTimeout(timeout int)
- func LoadFromEnv(config any)
- type Config
- type Status
- type Threshold
Examples ¶
Constants ¶
const ( // OKString means everything is fine OKString = "OK" // WarningString means there is a problem the admin should review WarningString = "WARNING" // CriticalString means there is a problem that requires immediate action CriticalString = "CRITICAL" // UnknownString means the status can not be determined, probably due to an error or something missing UnknownString = "UNKNOWN" )
Variables ¶
var ( PosInf = math.Inf(1) NegInf = math.Inf(-1) )
var AllowExit = true
AllowExit lets you disable the call to os.Exit() in the BaseExit function of this package. This should be used carefully and most likely only for testing.
var PrintStack = true
PrintStack prints the error stack when recovering from a panic with CatchPanic()
Functions ¶
func BaseExit ¶
func BaseExit(rc Status)
BaseExit exits the process with a given return code.
Can be controlled with the global AllowExit. This should be used carefully and most likely only for testing.
func BoundaryToString ¶
BoundaryToString returns the string representation of a Threshold boundary.
func CatchPanic ¶
func CatchPanic()
CatchPanic is a general function for defer, to capture any panic that occurred during runtime of a check
The function will recover from the condition and exit with a proper UNKNOWN status, while showing error and the call stack.
Example ¶
defer CatchPanic()
panic("something bad happened")
Output: [UNKNOWN] - Golang encountered a panic: something bad happened would exit with code 3
func Compare ¶ added in v1.0.0
Compare compares two Status types. If the left one (a) is worse than the right one (b), the result is < 0
If they are equal, the result is 0 ¶
If the right one (b) is worse than the left one (a), the result is > 0
func Exit ¶
Exit exits the process with a given return code determined from the given Status and a text representation to stdout.
Example: OK - everything is fine exit 0 Example: [UNKNOWN] - not sure what happened exit 3
Example ¶
Exit(OK, fmt.Sprintf("Everything is fine - value=%d", 42))
Output: [OK] - Everything is fine - value=42 would exit with code 0
func ExitError ¶
func ExitError(err error)
ExitError exists with an Unknown state while reporting the error The Unknown state is used, since the plugin likely could not determine the actual status of whatever was meant to be checked.
Example ¶
err := fmt.Errorf("connection to %s has been timed out", "localhost:12345")
ExitError(err)
Output: [UNKNOWN] - connection to localhost:12345 has been timed out (*errors.errorString) would exit with code 3
func FormatFloat ¶ added in v0.3.0
FormatFloat returns a string representation of floats, avoiding scientific notation and removes trailing zeros.
func HandleTimeout ¶
func HandleTimeout(timeout int)
HandleTimeout is a helper for a goroutine, to wait for signals and timeout, and exit with a proper code
func LoadFromEnv ¶ added in v0.6.0
func LoadFromEnv(config any)
LoadFromEnv can be used to load struct values from 'env' tags. Mainly used to avoid passing secrets via the CLI
type Config struct {
Token string `env:"BEARER_TOKEN"`
}
Types ¶
type Config ¶
type Config struct {
// Name of the monitoring plugin
Name string
// README represents the help text for the CLI usage
Readme string
// Output for the --version flag
Version string
// Default for the --timeout flag
Timeout int
// Default for the --verbose flag
Verbose bool
// Default for the --debug flag
Debug bool
// Enable predefined --version output
PrintVersion bool
// Enable predefined default flags for the monitoring plugin
DefaultFlags bool
// Enable predefined default functions (e.g. Timeout handler) for the monitoring plugin
DefaultHelper bool
// Additional CLI flags for the monitoring plugin
FlagSet *flag.FlagSet
}
Config represents a configuration for a monitoring plugin's CLI
Example ¶
config := NewConfig()
config.Name = "check_test"
config.Readme = `Test Plugin`
config.Version = "1.0.0"
_ = config.FlagSet.StringP("hostname", "H", "localhost", "Hostname to check")
config.ParseArguments()
// Some checking should be done here
Exit(OK, fmt.Sprintf("Everything is fine - answer=%d", 42))
Output: [OK] - Everything is fine - answer=42 would exit with code 0
func (*Config) EnableTimeoutHandler ¶
func (c *Config) EnableTimeoutHandler()
EnableTimeoutHandler starts the timeout and signal handler in a goroutine
func (*Config) ParseArguments ¶
func (c *Config) ParseArguments()
ParseArguments parses the command line arguments given by os.Args
func (*Config) ParseArray ¶
ParseArray parses a list of command line arguments
type Status ¶ added in v1.0.0
type Status int
func NewStatus ¶ added in v1.0.0
NewStatus returns a state corresponding to its common string representation. When an invalid parameter is passed an error and an Unknown status will be returned
func NewStatusFromString ¶ added in v1.0.0
NewStatusFromString returns a state corresponding to its common string representation. When an invalid parameter is passed an error and an Unknown status will be returned
func WorstState ¶ added in v1.0.0
WorstState determines the worst state from a list of states
Helps combining an overall states, only based on a few numbers for various checks.
Order of preference: Critical, Unknown, Warning, Ok
type Threshold ¶
Threshold defines threshold for any numeric value
Format: [@]start:end
Threshold Generate an alert if x... 10 < 0 or > 10, (outside the range of {0 .. 10}) 10: < 10, (outside {10 .. ∞}) ~:10 > 10, (outside the range of {-∞ .. 10}) 10:20 < 10 or > 20, (outside the range of {10 .. 20}) @10:20 ≥ 10 and ≤ 20, (inside the range of {10 .. 20})
Reference: https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT
func ParseThreshold ¶
ParseThreshold parses a Threshold from a string.
See the Threshold type for details.
func (Threshold) DoesViolate ¶
DoesViolate compares a value against the threshold, and returns true if the value violates the threshold.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package convert provides function to convert and humanize units
|
Package convert provides function to convert and humanize units |
|
examples
|
|
|
check_example
command
|
|
|
check_example2
command
|
|
|
Package perfdata provides types and functions to handle check plugin performance data
|
Package perfdata provides types and functions to handle check plugin performance data |
|
Package result provides types and functions to organize results in a check plugin
|
Package result provides types and functions to organize results in a check plugin |
|
Package testhelper is used in testing.
|
Package testhelper is used in testing. |