2021-02-19 12:47:00 +01:00
|
|
|
// Package plugin implements a helper that can be used to implement a Nagios,
|
|
|
|
// Centreon, Icinga... service monitoring plugin.
|
2024-07-19 14:29:51 +02:00
|
|
|
package plugin // import nocternity.net/go-monitoring/pkg/perfdata
|
2021-01-03 10:33:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2021-02-19 12:47:00 +01:00
|
|
|
|
2024-07-19 14:29:51 +02:00
|
|
|
"nocternity.net/go-monitoring/pkg/perfdata"
|
2021-01-03 10:33:21 +01:00
|
|
|
)
|
|
|
|
|
2021-02-19 12:47:00 +01:00
|
|
|
// Status represents the return status of the monitoring plugin. The
|
|
|
|
// corresponding integer value will be used as the program's exit code,
|
|
|
|
// to be interpreted by the monitoring system.
|
2021-01-03 10:33:21 +01:00
|
|
|
type Status int
|
|
|
|
|
2021-02-19 12:47:00 +01:00
|
|
|
// Plugin exit statuses.
|
2021-01-03 10:33:21 +01:00
|
|
|
const (
|
|
|
|
OK Status = iota
|
|
|
|
WARNING
|
2021-01-03 11:59:05 +01:00
|
|
|
CRITICAL
|
2021-01-03 10:33:21 +01:00
|
|
|
UNKNOWN
|
|
|
|
)
|
|
|
|
|
2021-02-19 12:47:00 +01:00
|
|
|
// String representations of the plugin statuses.
|
2021-01-03 10:33:21 +01:00
|
|
|
func (s Status) String() string {
|
|
|
|
return [...]string{"OK", "WARNING", "ERROR", "UNKNOWN"}[s]
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:47:00 +01:00
|
|
|
// Plugin represents the monitoring plugin's state, including its name,
|
|
|
|
// return status and message, additional lines of text, and performance
|
|
|
|
// data to be encoded in the output.
|
2021-01-03 10:33:21 +01:00
|
|
|
type Plugin struct {
|
|
|
|
name string
|
|
|
|
status Status
|
|
|
|
message string
|
|
|
|
extraText *list.List
|
2021-01-07 20:55:10 +01:00
|
|
|
perfData map[string]*perfdata.PerfData
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 12:47:00 +01:00
|
|
|
// New creates the plugin with `name` as its name and an unknown status.
|
2021-01-03 10:33:21 +01:00
|
|
|
func New(name string) *Plugin {
|
|
|
|
p := new(Plugin)
|
|
|
|
p.name = name
|
|
|
|
p.status = UNKNOWN
|
|
|
|
p.message = "no status set"
|
2021-01-07 20:55:10 +01:00
|
|
|
p.perfData = make(map[string]*perfdata.PerfData)
|
2021-01-03 10:33:21 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:47:00 +01:00
|
|
|
// SetState sets the plugin's output code to `status` and its message to
|
2021-02-19 13:18:21 +01:00
|
|
|
// the specified `message`.
|
2021-01-03 10:33:21 +01:00
|
|
|
func (p *Plugin) SetState(status Status, message string) {
|
|
|
|
p.status = status
|
|
|
|
p.message = message
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:47:00 +01:00
|
|
|
// AddLine adds the specified string to the extra output text buffer.
|
2021-02-27 10:54:40 +01:00
|
|
|
func (p *Plugin) AddLine(format string, data ...interface{}) {
|
2021-01-03 10:33:21 +01:00
|
|
|
if p.extraText == nil {
|
|
|
|
p.extraText = list.New()
|
|
|
|
}
|
2021-02-27 10:54:40 +01:00
|
|
|
p.extraText.PushBack(fmt.Sprintf(format, data...))
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 12:47:00 +01:00
|
|
|
// AddLines add the specified `lines` to the output text.
|
2021-01-03 10:33:21 +01:00
|
|
|
func (p *Plugin) AddLines(lines []string) {
|
|
|
|
for _, line := range lines {
|
2021-02-27 10:54:40 +01:00
|
|
|
p.AddLine("%s", line)
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:47:00 +01:00
|
|
|
// AddPerfData adds performance data described by the "pd" argument to the
|
|
|
|
// output's performance data. If two performance data records are added for
|
|
|
|
// the same label, the program panics.
|
2021-01-07 20:55:10 +01:00
|
|
|
func (p *Plugin) AddPerfData(pd *perfdata.PerfData) {
|
2021-01-03 10:33:21 +01:00
|
|
|
_, exists := p.perfData[pd.Label]
|
|
|
|
if exists {
|
|
|
|
panic(fmt.Sprintf("duplicate performance data %s", pd.Label))
|
|
|
|
}
|
|
|
|
p.perfData[pd.Label] = pd
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:47:00 +01:00
|
|
|
// Done generates the plugin's text output from its name, status, text data
|
2021-01-03 11:59:05 +01:00
|
|
|
// and performance data, before exiting with the code corresponding to the
|
|
|
|
// status.
|
2021-01-03 10:33:21 +01:00
|
|
|
func (p *Plugin) Done() {
|
|
|
|
var sb strings.Builder
|
|
|
|
sb.WriteString(p.name)
|
|
|
|
sb.WriteString(" ")
|
|
|
|
sb.WriteString(p.status.String())
|
|
|
|
sb.WriteString(": ")
|
|
|
|
sb.WriteString(p.message)
|
|
|
|
if len(p.perfData) > 0 {
|
|
|
|
sb.WriteString(" | ")
|
|
|
|
needSep := false
|
|
|
|
for k := range p.perfData {
|
|
|
|
if needSep {
|
|
|
|
sb.WriteString(", ")
|
|
|
|
} else {
|
|
|
|
needSep = true
|
|
|
|
}
|
|
|
|
sb.WriteString(p.perfData[k].String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.extraText != nil {
|
|
|
|
for em := p.extraText.Front(); em != nil; em = em.Next() {
|
|
|
|
sb.WriteString("\n")
|
|
|
|
sb.WriteString(em.Value.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println(sb.String())
|
|
|
|
os.Exit(int(p.status))
|
|
|
|
}
|