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 22:01:34 +02:00
|
|
|
package plugin // import nocternity.net/gomonop/pkg/perfdata
|
2021-01-03 10:33:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-02-19 12:47:00 +01:00
|
|
|
|
2024-07-19 22:01:34 +02:00
|
|
|
"nocternity.net/gomonop/pkg/perfdata"
|
2021-01-03 10:33:21 +01:00
|
|
|
)
|
|
|
|
|
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
|
2024-07-19 23:24:52 +02:00
|
|
|
p.status = StatusUnknown
|
2021-01-03 10:33:21 +01:00
|
|
|
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.
|
2024-07-19 22:01:34 +02:00
|
|
|
func (p *Plugin) AddLine(line string) {
|
2021-01-03 10:33:21 +01:00
|
|
|
if p.extraText == nil {
|
|
|
|
p.extraText = list.New()
|
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
p.extraText.PushBack(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLinef formats the input and adds it to the text buffer.
|
|
|
|
func (p *Plugin) AddLinef(format string, data ...interface{}) {
|
|
|
|
p.AddLine(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 {
|
2024-07-19 22:01:34 +02:00
|
|
|
p.AddLine(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 {
|
2024-07-19 22:01:34 +02:00
|
|
|
panic("duplicate performance data " + pd.Label)
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
p.perfData[pd.Label] = pd
|
|
|
|
}
|
|
|
|
|
2024-07-19 23:46:33 +02:00
|
|
|
// String generates the plugin's text output from its name, status, text data
|
|
|
|
// and performance data.
|
|
|
|
func (p *Plugin) String() string {
|
2024-07-19 22:01:34 +02:00
|
|
|
var strBuilder strings.Builder
|
|
|
|
strBuilder.WriteString(p.name)
|
|
|
|
strBuilder.WriteString(" ")
|
|
|
|
strBuilder.WriteString(p.status.String())
|
|
|
|
strBuilder.WriteString(": ")
|
|
|
|
strBuilder.WriteString(p.message)
|
2021-01-03 10:33:21 +01:00
|
|
|
if len(p.perfData) > 0 {
|
2024-07-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString(" | ")
|
2021-01-03 10:33:21 +01:00
|
|
|
needSep := false
|
2024-07-19 22:01:34 +02:00
|
|
|
for _, data := range p.perfData {
|
2021-01-03 10:33:21 +01:00
|
|
|
if needSep {
|
2024-07-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString(", ")
|
2021-01-03 10:33:21 +01:00
|
|
|
} else {
|
|
|
|
needSep = true
|
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString(data.String())
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.extraText != nil {
|
|
|
|
for em := p.extraText.Front(); em != nil; em = em.Next() {
|
2024-07-19 22:01:34 +02:00
|
|
|
strBuilder.WriteString("\n")
|
|
|
|
//nolint:forcetypeassert // we want to panic if this isn't a string
|
|
|
|
strBuilder.WriteString(em.Value.(string))
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
}
|
2024-07-19 23:46:33 +02:00
|
|
|
return strBuilder.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExitCode returns the plugin's exit code.
|
|
|
|
func (p *Plugin) ExitCode() int {
|
|
|
|
return int(p.status)
|
2021-01-03 10:33:21 +01:00
|
|
|
}
|