2024-07-20 10:01:05 +02:00
|
|
|
// Package results implements a helper that can be used to store the results of
|
|
|
|
// a Nagios, Centreon, Icinga... service monitoring plugin, and convert them to
|
|
|
|
// text which can be sent to the monitoring server.
|
|
|
|
package results // import nocternity.net/gomonop/pkg/results
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"nocternity.net/gomonop/pkg/perfdata"
|
2024-07-20 18:56:51 +02:00
|
|
|
"nocternity.net/gomonop/pkg/status"
|
2024-07-20 10:01:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Results represents the monitoring plugin's results, including its name,
|
|
|
|
// return status and message, additional lines of text, and performance
|
|
|
|
// data to be encoded in the output.
|
|
|
|
type Results struct {
|
|
|
|
name string
|
2024-07-20 18:56:51 +02:00
|
|
|
status status.Status
|
2024-07-20 10:01:05 +02:00
|
|
|
message string
|
|
|
|
extraText *list.List
|
|
|
|
perfData map[string]*perfdata.PerfData
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates the plugin with `name` as its name and an unknown status.
|
|
|
|
func New(name string) *Results {
|
|
|
|
p := new(Results)
|
|
|
|
p.name = name
|
2024-07-20 18:56:51 +02:00
|
|
|
p.status = status.StatusUnknown
|
2024-07-20 10:01:05 +02:00
|
|
|
p.message = "no status set"
|
|
|
|
p.perfData = make(map[string]*perfdata.PerfData)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetState sets the plugin's output code to `status` and its message to
|
|
|
|
// the specified `message`.
|
2024-07-20 18:56:51 +02:00
|
|
|
func (p *Results) SetState(status status.Status, message string) {
|
2024-07-20 10:01:05 +02:00
|
|
|
p.status = status
|
|
|
|
p.message = message
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLine adds the specified string to the extra output text buffer.
|
|
|
|
func (p *Results) AddLine(line string) {
|
|
|
|
if p.extraText == nil {
|
|
|
|
p.extraText = list.New()
|
|
|
|
}
|
|
|
|
p.extraText.PushBack(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLinef formats the input and adds it to the text buffer.
|
|
|
|
func (p *Results) AddLinef(format string, data ...interface{}) {
|
|
|
|
p.AddLine(fmt.Sprintf(format, data...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLines add the specified `lines` to the output text.
|
|
|
|
func (p *Results) AddLines(lines []string) {
|
|
|
|
for _, line := range lines {
|
|
|
|
p.AddLine(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (p *Results) AddPerfData(pd *perfdata.PerfData) {
|
|
|
|
_, exists := p.perfData[pd.Label]
|
|
|
|
if exists {
|
|
|
|
panic("duplicate performance data " + pd.Label)
|
|
|
|
}
|
|
|
|
p.perfData[pd.Label] = pd
|
|
|
|
}
|
|
|
|
|
|
|
|
// String generates the plugin's text output from its name, status, text data
|
|
|
|
// and performance data.
|
|
|
|
func (p *Results) String() string {
|
|
|
|
var strBuilder strings.Builder
|
|
|
|
strBuilder.WriteString(p.name)
|
|
|
|
strBuilder.WriteString(" ")
|
|
|
|
strBuilder.WriteString(p.status.String())
|
|
|
|
strBuilder.WriteString(": ")
|
|
|
|
strBuilder.WriteString(p.message)
|
|
|
|
if len(p.perfData) > 0 {
|
|
|
|
strBuilder.WriteString(" | ")
|
|
|
|
needSep := false
|
|
|
|
for _, data := range p.perfData {
|
|
|
|
if needSep {
|
|
|
|
strBuilder.WriteString(", ")
|
|
|
|
} else {
|
|
|
|
needSep = true
|
|
|
|
}
|
|
|
|
strBuilder.WriteString(data.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.extraText != nil {
|
|
|
|
for em := p.extraText.Front(); em != nil; em = em.Next() {
|
|
|
|
strBuilder.WriteString("\n")
|
|
|
|
//nolint:forcetypeassert // we want to panic if this isn't a string
|
|
|
|
strBuilder.WriteString(em.Value.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strBuilder.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExitCode returns the plugin's exit code.
|
|
|
|
func (p *Results) ExitCode() int {
|
|
|
|
return int(p.status)
|
|
|
|
}
|