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()) dn := strings.ToLower(certificate.Subject.String())
if !strings.HasPrefix(dn, fmt.Sprintf("cn=%s,", flags.hostname)) { 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 return
} }
} else if !findHostname(certificate, flags.hostname) { } 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 return
} }
timeLeft := certificate.NotAfter.Sub(time.Now()) timeLeft := certificate.NotAfter.Sub(time.Now())
tlDays := int((timeLeft + 86399*time.Second) / (24 * time.Hour)) tlDays := int((timeLeft + 86399*time.Second) / (24 * time.Hour))
if flags.crit > 0 && tlDays <= flags.crit { 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 { } else if flags.warn > 0 && tlDays <= flags.warn {
p.SetState(plugin.WARNING, fmt.Sprintf("certificate will expire in %d days (<= %d)", tlDays, flags.warn)) p.SetState(plugin.WARNING, fmt.Sprintf("certificate will expire in %d days (<= %d)", tlDays, flags.warn))
} else { } else {

View file

@ -1,3 +1,4 @@
// Package that represents a monitoring plugin.
package plugin package plugin
import ( import (
@ -8,12 +9,15 @@ import (
"strings" "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 type Status int
const ( const (
OK Status = iota OK Status = iota
WARNING WARNING
ERROR CRITICAL
UNKNOWN UNKNOWN
) )
@ -21,6 +25,9 @@ func (s Status) String() string {
return [...]string{"OK", "WARNING", "ERROR", "UNKNOWN"}[s] 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 { type Plugin struct {
name string name string
status Status status Status
@ -29,6 +36,7 @@ type Plugin struct {
perfData map[string]perfdata.PerfData perfData map[string]perfdata.PerfData
} }
// `New` creates the plugin with `name` as its name and an unknown status.
func New(name string) *Plugin { func New(name string) *Plugin {
p := new(Plugin) p := new(Plugin)
p.name = name p.name = name
@ -38,12 +46,15 @@ func New(name string) *Plugin {
return p 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) { func (p *Plugin) SetState(status Status, message string) {
p.status = status p.status = status
p.message = message p.message = message
p.extraText = nil p.extraText = nil
} }
// `AddLine` adds `line` to the extra output text.
func (p *Plugin) AddLine(line string) { func (p *Plugin) AddLine(line string) {
if p.extraText == nil { if p.extraText == nil {
p.extraText = list.New() p.extraText = list.New()
@ -51,12 +62,16 @@ func (p *Plugin) AddLine(line string) {
p.extraText.PushBack(line) p.extraText.PushBack(line)
} }
// `AddLines` add the specified `lines` to the output text.
func (p *Plugin) AddLines(lines []string) { func (p *Plugin) AddLines(lines []string) {
for _, line := range lines { for _, line := range lines {
p.AddLine(line) 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) { func (p *Plugin) AddPerfData(pd perfdata.PerfData) {
_, exists := p.perfData[pd.Label] _, exists := p.perfData[pd.Label]
if exists { if exists {
@ -65,6 +80,9 @@ func (p *Plugin) AddPerfData(pd perfdata.PerfData) {
p.perfData[pd.Label] = pd 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() { func (p *Plugin) Done() {
var sb strings.Builder var sb strings.Builder
sb.WriteString(p.name) sb.WriteString(p.name)