Short documentation for the plugin state code

This commit is contained in:
Emmanuel BENOîT 2021-01-03 11:59:05 +01:00
parent 66a1b017be
commit b86fdf53b9
2 changed files with 22 additions and 4 deletions

View file

@ -93,17 +93,17 @@ func main() {
}
dn := strings.ToLower(certificate.Subject.String())
if !strings.HasPrefix(dn, fmt.Sprintf("cn=%s,", flags.hostname)) {
p.SetState(plugin.ERROR, "incorrect certificate CN")
p.SetState(plugin.CRITICAL, "incorrect certificate CN")
return
}
} else if !findHostname(certificate, flags.hostname) {
p.SetState(plugin.ERROR, "host name not found in SAN domain names")
p.SetState(plugin.CRITICAL, "host name not found in SAN domain names")
return
}
timeLeft := certificate.NotAfter.Sub(time.Now())
tlDays := int((timeLeft + 86399*time.Second) / (24 * time.Hour))
if flags.crit > 0 && tlDays <= flags.crit {
p.SetState(plugin.ERROR, fmt.Sprintf("certificate will expire in %d days (<= %d)", tlDays, flags.crit))
p.SetState(plugin.CRITICAL, fmt.Sprintf("certificate will expire in %d days (<= %d)", tlDays, flags.crit))
} else if flags.warn > 0 && tlDays <= flags.warn {
p.SetState(plugin.WARNING, fmt.Sprintf("certificate will expire in %d days (<= %d)", tlDays, flags.warn))
} else {

View file

@ -1,3 +1,4 @@
// Package that represents a monitoring plugin.
package plugin
import (
@ -8,12 +9,15 @@ import (
"strings"
)
// 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.
type Status int
const (
OK Status = iota
WARNING
ERROR
CRITICAL
UNKNOWN
)
@ -21,6 +25,9 @@ func (s Status) String() string {
return [...]string{"OK", "WARNING", "ERROR", "UNKNOWN"}[s]
}
// Monitoring plugin state, including a name, return status and message,
// additional lines of text, and performance data to be encoded in the
// output.
type Plugin struct {
name string
status Status
@ -29,6 +36,7 @@ type Plugin struct {
perfData map[string]perfdata.PerfData
}
// `New` creates the plugin with `name` as its name and an unknown status.
func New(name string) *Plugin {
p := new(Plugin)
p.name = name
@ -38,12 +46,15 @@ func New(name string) *Plugin {
return p
}
// `SetState` sets the plugin's output code to `status` and its message to
// the specified `message`. Any extra text is cleared.
func (p *Plugin) SetState(status Status, message string) {
p.status = status
p.message = message
p.extraText = nil
}
// `AddLine` adds `line` to the extra output text.
func (p *Plugin) AddLine(line string) {
if p.extraText == nil {
p.extraText = list.New()
@ -51,12 +62,16 @@ func (p *Plugin) AddLine(line string) {
p.extraText.PushBack(line)
}
// `AddLines` add the specified `lines` to the output text.
func (p *Plugin) AddLines(lines []string) {
for _, line := range lines {
p.AddLine(line)
}
}
// `AddPerfData` adds performance data described by `pd` to the output's
// performance data. If two performance data records are added for the same
// label, the program panics.
func (p *Plugin) AddPerfData(pd perfdata.PerfData) {
_, exists := p.perfData[pd.Label]
if exists {
@ -65,6 +80,9 @@ func (p *Plugin) AddPerfData(pd perfdata.PerfData) {
p.perfData[pd.Label] = pd
}
// `Done` generates the plugin's text output from its name, status, text data
// and performance data, before exiting with the code corresponding to the
// status.
func (p *Plugin) Done() {
var sb strings.Builder
sb.WriteString(p.name)