From b86fdf53b97988a969296c31642333daf1ee9019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 3 Jan 2021 11:59:05 +0100 Subject: [PATCH] Short documentation for the plugin state code --- cmd/check_ssl_certificate/main.go | 6 +++--- plugin/plugin.go | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cmd/check_ssl_certificate/main.go b/cmd/check_ssl_certificate/main.go index 0c7f92a..29a23e8 100644 --- a/cmd/check_ssl_certificate/main.go +++ b/cmd/check_ssl_certificate/main.go @@ -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 { diff --git a/plugin/plugin.go b/plugin/plugin.go index 68b1df7..b9ccfcd 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -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)