Documentation ¶
Overview ¶
Package monitoringplugin provides types for writing monitoring check plugins for nagios, icinga2, zabbix, etc
Index ¶
- Constants
- func StatusCode2Text(statusCode int) string
- func String2StatusCode(s string) int
- type InvalidCharacterBehavior
- type OutputMessage
- type PerformanceDataPoint
- func (p *PerformanceDataPoint) SetLabel(label string) *PerformanceDataPoint
- func (p *PerformanceDataPoint) SetMax(max interface{}) *PerformanceDataPoint
- func (p *PerformanceDataPoint) SetMin(min interface{}) *PerformanceDataPoint
- func (p *PerformanceDataPoint) SetThresholds(thresholds Thresholds) *PerformanceDataPoint
- func (p *PerformanceDataPoint) SetUnit(unit string) *PerformanceDataPoint
- func (p *PerformanceDataPoint) Validate() error
- type Response
- func (r *Response) AddPerformanceDataPoint(point *PerformanceDataPoint) error
- func (r *Response) CheckThresholds(point *PerformanceDataPoint) error
- func (r *Response) GetInfo() ResponseInfo
- func (r *Response) GetStatusCode() int
- func (r *Response) OutputAndExit()
- func (r *Response) OutputDelimiterMultiline()
- func (r *Response) PrintPerformanceData(b bool)
- func (r *Response) SetInvalidCharacterBehavior(behavior InvalidCharacterBehavior, replaceCharacter string) error
- func (r *Response) SetOutputDelimiter(delimiter string)
- func (r *Response) SetPerformanceDataJSONLabel(jsonLabel bool)
- func (r *Response) SortOutputMessagesByStatus(b bool)
- func (r *Response) UpdateStatus(statusCode int, statusMessage string)
- func (r *Response) UpdateStatusIf(condition bool, statusCode int, statusMessage string) bool
- func (r *Response) UpdateStatusIfNot(condition bool, statusCode int, statusMessage string) bool
- func (r *Response) UpdateStatusOnError(err error, statusCode int, statusMessage string, includeErrorMessage bool) bool
- type ResponseInfo
- type Thresholds
Constants ¶
const ( // OK check plugin status = OK OK = 0 // WARNING check plugin status = WARNING WARNING = 1 // CRITICAL check plugin status = CRITICAL CRITICAL = 2 // UNKNOWN check plugin status = UNKNOWN UNKNOWN = 3 )
Variables ¶
This section is empty.
Functions ¶
func StatusCode2Text ¶
StatusCode2Text is used to map the status code to a string.
func String2StatusCode ¶
String2StatusCode returns the status code for a string. OK -> 1, WARNING -> 2, CRITICAL -> 3, UNKNOWN and everything else -> 4 (case insensitive)
Types ¶
type InvalidCharacterBehavior ¶
type InvalidCharacterBehavior int
InvalidCharacterBehavior specifies how the monitoringplugin should behave if an invalid character is found in the output message. Does not affect invalid characters in the performance data.
const ( // InvalidCharacterRemove removes invalid character in the output message. InvalidCharacterRemove InvalidCharacterBehavior = iota + 1 // InvalidCharacterReplace replaces invalid character in the output message with another character. // Only valid if replace character is set InvalidCharacterReplace // InvalidCharacterRemoveMessage removes the message with the invalid character. // StatusCode of the message will still be set. InvalidCharacterRemoveMessage // InvalidCharacterReplaceWithError replaces the whole message with an error message if an invalid character is found. InvalidCharacterReplaceWithError // InvalidCharacterReplaceWithErrorAndSetUNKNOWN replaces the whole message with an error message if an invalid character is found. // Also sets the status code to UNKNOWN. InvalidCharacterReplaceWithErrorAndSetUNKNOWN )
type OutputMessage ¶
type OutputMessage struct { Status int `yaml:"status" json:"status" xml:"status"` Message string `yaml:"message" json:"message" xml:"message"` }
OutputMessage represents a message of the response. It contains a message and a status code.
type PerformanceDataPoint ¶
type PerformanceDataPoint struct { Metric string `json:"metric" xml:"metric"` Label string `json:"label" xml:"label"` Value interface{} `json:"value" xml:"value"` Unit string `json:"unit" xml:"unit"` Thresholds Thresholds `json:"thresholds" xml:"thresholds"` Min interface{} `json:"min" xml:"min"` Max interface{} `json:"max" xml:"max"` }
PerformanceDataPoint contains all information of one PerformanceDataPoint.
func NewPerformanceDataPoint ¶
func NewPerformanceDataPoint(metric string, value interface{}) *PerformanceDataPoint
NewPerformanceDataPoint creates a new PerformanceDataPoint. Metric and value are mandatory but are not checked at this point, the performanceDatePoint's validation is checked later when it is added to the performanceData list in the function performanceData.add(*PerformanceDataPoint). It is possible to directly add thresholds, min and max values with the functions SetThresholds(Thresholds), SetMin(int) and SetMax(int). Usage:
PerformanceDataPoint := NewPerformanceDataPoint("memory_usage", 55).SetUnit("%")
func (*PerformanceDataPoint) SetLabel ¶
func (p *PerformanceDataPoint) SetLabel(label string) *PerformanceDataPoint
SetLabel adds a tag to the performance data point If one tag is added more than once, the value before will be overwritten
func (*PerformanceDataPoint) SetMax ¶
func (p *PerformanceDataPoint) SetMax(max interface{}) *PerformanceDataPoint
SetMax sets maximum value.
func (*PerformanceDataPoint) SetMin ¶
func (p *PerformanceDataPoint) SetMin(min interface{}) *PerformanceDataPoint
SetMin sets minimum value.
func (*PerformanceDataPoint) SetThresholds ¶
func (p *PerformanceDataPoint) SetThresholds(thresholds Thresholds) *PerformanceDataPoint
SetThresholds sets the thresholds for the performance data point
func (*PerformanceDataPoint) SetUnit ¶
func (p *PerformanceDataPoint) SetUnit(unit string) *PerformanceDataPoint
SetUnit sets the unit of the performance data point
func (*PerformanceDataPoint) Validate ¶
func (p *PerformanceDataPoint) Validate() error
Validate validates a PerformanceDataPoint. This function is used to check if a PerformanceDataPoint is compatible with the documentation from 'http://nagios-plugins.org/doc/guidelines.html'(valid name and unit, value is inside the range of min and max etc.)
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response is the main type that is responsible for the check plugin Response. It stores the current status code, output messages, performance data and the output message delimiter.
func NewResponse ¶
NewResponse creates a new Response and sets the default OK message to the given string. The default OK message will be displayed together with the other output messages, but only if the status is still OK when the check exits.
func (*Response) AddPerformanceDataPoint ¶
func (r *Response) AddPerformanceDataPoint(point *PerformanceDataPoint) error
AddPerformanceDataPoint adds a PerformanceDataPoint to the performanceData map, using performanceData.add(*PerformanceDataPoint). Usage:
err := Response.AddPerformanceDataPoint(NewPerformanceDataPoint("temperature", 32, "°C").SetWarn(35).SetCrit(40)) if err != nil { ... }
func (*Response) CheckThresholds ¶
func (r *Response) CheckThresholds(point *PerformanceDataPoint) error
CheckThresholds checks if the value contained in the performance data point exceeds the contained thresholds and updates the response.
func (*Response) GetInfo ¶
func (r *Response) GetInfo() ResponseInfo
GetInfo returns all information for a response.
func (*Response) GetStatusCode ¶
GetStatusCode returns the current status code.
func (*Response) OutputAndExit ¶
func (r *Response) OutputAndExit()
OutputAndExit generates the output string and prints it to stdout. After that the check plugin exits with the current exit code. Example:
Response := NewResponse("everything checked!") defer Response.OutputAndExit() //check plugin logic...
func (*Response) OutputDelimiterMultiline ¶
func (r *Response) OutputDelimiterMultiline()
OutputDelimiterMultiline sets the outputDelimiter to "\n". (See Response.SetOutputDelimiter(string))
func (*Response) PrintPerformanceData ¶
PrintPerformanceData activates or deactivates printing performance data
func (*Response) SetInvalidCharacterBehavior ¶
func (r *Response) SetInvalidCharacterBehavior(behavior InvalidCharacterBehavior, replaceCharacter string) error
SetInvalidCharacterBehavior sets the desired behavior if an invalid character is found in a message. Default is InvalidCharacterRemove. replaceCharacter is only necessary if InvalidCharacterReplace is set.
func (*Response) SetOutputDelimiter ¶
SetOutputDelimiter is used to set the delimiter that is used to separate the outputMessages that will be displayed when the check plugin exits. The default value is a linebreak (\n) It can be set to any string. Example:
Response.SetOutputDelimiter(" / ") //this results in the output having the following format: //OK: defaultOkMessage / outputMessage1 / outputMessage2 / outputMessage3 | performanceData
func (*Response) SetPerformanceDataJSONLabel ¶
SetPerformanceDataJSONLabel updates the JSON metric.
func (*Response) SortOutputMessagesByStatus ¶
SortOutputMessagesByStatus sorts the output messages according to their status.
func (*Response) UpdateStatus ¶
UpdateStatus updates the exit status of the Response and adds a statusMessage to the outputMessages that will be displayed when the check exits. See updateStatusCode(int) for a detailed description of the algorithm that is used to update the status code.
func (*Response) UpdateStatusIf ¶
UpdateStatusIf calls UpdateStatus(statusCode, statusMessage) if the given condition is true.
func (*Response) UpdateStatusIfNot ¶
UpdateStatusIfNot calls UpdateStatus(statusCode, statusMessage) if the given condition is false.
type ResponseInfo ¶
type ResponseInfo struct { StatusCode int `yaml:"status_code" json:"status_code" xml:"status_code"` PerformanceData []PerformanceDataPoint `yaml:"performance_data" json:"performance_data" xml:"performance_data"` RawOutput string `yaml:"raw_output" json:"raw_output" xml:"raw_output"` Messages []OutputMessage `yaml:"messages" json:"messages" xml:"messages"` }
ResponseInfo has all available information for a response. It also contains the RawOutput.
type Thresholds ¶
type Thresholds struct { WarningMin interface{} `json:"warningMin" xml:"warningMin"` WarningMax interface{} `json:"warningMax" xml:"warningMax"` CriticalMin interface{} `json:"criticalMin" xml:"criticalMin"` CriticalMax interface{} `json:"criticalMax" xml:"criticalMax"` }
Thresholds contains all threshold values
func NewThresholds ¶
func NewThresholds(warningMin, warningMax, criticalMin, criticalMax interface{}) Thresholds
NewThresholds creates a new threshold
func (*Thresholds) CheckValue ¶
func (c *Thresholds) CheckValue(v interface{}) (int, error)
CheckValue checks if the input is violating the thresholds
func (*Thresholds) HasCritical ¶
func (c *Thresholds) HasCritical() bool
HasCritical checks if a critical threshold is set
func (*Thresholds) HasWarning ¶
func (c *Thresholds) HasWarning() bool
HasWarning checks if a warning threshold is set
func (*Thresholds) IsEmpty ¶
func (c *Thresholds) IsEmpty() bool
IsEmpty checks if the thresholds are empty
func (*Thresholds) Validate ¶
func (c *Thresholds) Validate() error
Validate checks if the Thresholds contains some invalid combination of warning and critical values