Documentation
¶
Overview ¶
Package plugin provides a Go library which helps writing monitoring plugins.
Features:
- Setting and appending check messages
- Aggregation of results
- Thresholds and ranges for metrics with breaches reported
- Exit shortcut helper methods
- Provides extensive command line options parser
Example usage:
package main
// import plugin library
import (
"github.com/ajgb/go-plugin"
)
// define command line options
var opts struct {
Hostname string `short:"H" long:"hostname" description:"Host" required:"true"`
Port int `short:"p" long:"port" description:"Port" required:"true"`
}
func main() {
// initialize check
check := plugin.New("check_service", "v1.0.0")
// parse command line arguments
if err := check.ParseArgs(&opts); err != nil {
check.ExitCritical("Error parsing arguments: %s", err)
}
// return result on exit
defer check.Final()
// add service data to output
check.AddMessage("Service %s:%d", opts.Hostname, opts.Port)
// gather metrics - provided by some function
serviceMetrics, err := .... // retrieve metrics
if err != nil {
check.ExitCritical("Connection failed: %s", err)
}
// add metrics to output
for metric, value := range serviceMetrics {
check.AddMetric(metric, value)
}
}
Index ¶
- type Plugin
- func (p *Plugin) AddMessage(format string, args ...interface{})
- func (p *Plugin) AddMetric(name string, value interface{}, args ...string) error
- func (p *Plugin) AddResult(code Status, format string, args ...interface{})
- func (p *Plugin) ExitCritical(format string, args ...interface{})
- func (p *Plugin) ExitOK(format string, args ...interface{})
- func (p *Plugin) ExitUnknown(format string, args ...interface{})
- func (p *Plugin) ExitWarning(format string, args ...interface{})
- func (p *Plugin) Final()
- func (p *Plugin) ParseArgs(opts interface{}) error
- func (p *Plugin) SetMessage(format string, args ...interface{})
- func (p *Plugin) Status() Status
- func (p *Plugin) UpdateStatus(status Status)
- type Status
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Plugin ¶
type Plugin struct {
// Plugin name
Name string
// Plugin version
Version string
// Preamble displayed in help output before flags usage
Preamble string
// Plugin description displayed in help after flags usage
Description string
// If true all metrics will be added to check message
AllMetricsInOutput bool
// Messages separator, default: ", "
MessageSeparator string
// contains filtered or unexported fields
}
Plugin represents the check - its name, version and help messages. It also stores the check status, messages and metrics data.
func (*Plugin) AddMessage ¶
AddMessage appends message to check output.
check.AddMessage("Server %s", opts.Hostname)
func (*Plugin) AddMetric ¶
AddMetric adds new metric to check's performance data, with name and value parameters required. The optional string arguments include (in order): uom (unit of measurement), warning threshold, critical threshold - for details see Monitoring Plugins Development Guidelines. Note: Metrics names have to be unique.
// basic usage - add metric with value
check.AddMetric("load5", 0.98)
// metric with UOM
check.AddMetric("tmp", 15789, "MB")
// metric with warning threshold (without uom)
check.AddMetric("rtmax", 28.723, "", 75)
// metric with warning & critical thresholds (with uom)
check.AddMetric("rta", 24.558, "ms", 50, 100)
func (*Plugin) AddResult ¶
AddResult aggregates results and appends message to check output - the worst result is final.
// would not change the final result
check.AddResult(plugin.OK, "Server %s", opts.Hostname)
// increases to WARNING level
if opts.SkipSSLChecks {
check.AddResult(plugin.WARNING, "Skiping SSL Certificate checks")
}
func (*Plugin) ExitCritical ¶
ExitCritical exits with specified message and CRITICAL exit status. Note: existing messages and metrics are discarded.
func (*Plugin) ExitOK ¶
ExitOK exits with specified message and OK exit status. Note: existing messages and metrics are discarded.
check.ExitOK("Test mode | metric1=1.1; metric2=2.2")
func (*Plugin) ExitUnknown ¶
ExitUnknown exits with specified message and UNKNOWN exit status. Note: existing messages and metrics are discarded.
func (*Plugin) ExitWarning ¶
ExitWarning exits with specified message and WARNING exit status. Note: existing messages and metrics are discarded.
func (*Plugin) Final ¶
func (p *Plugin) Final()
Final calculates the final check output and exit status.
check := plugin.New("check_service, "v1.0.0")
// make sure Final() is called
defer check.Final()
func (*Plugin) ParseArgs ¶
ParseArgs parses the command line options using flags parsing library providing handling of short/long names, flags and lists, and default and required options. For details please see https://godoc.org/github.com/jessevdk/go-flags. Note: -h/--help is automatically added
if err := check.ParseArgs(&opts); err != nil {
check.ExitCritical("Error parsing arguments: %s", err)
}
func (*Plugin) SetMessage ¶
SetMessage replaces accumulated messages with new one provided.
check.SetMessage("%s", opts.Hostname)
func (*Plugin) Status ¶
Status returns current status.
fmt.Printf("Status after first five metrics: %s\n", check.Status)
func (*Plugin) UpdateStatus ¶
UpdateStatus updates final exit status if the provided value is higher (worse) then the current Status.
// keep people awake
if rand.Intn(100) % 3 == 0 {
check.UpdateStatus(plugin.CRITICAL)
}
