refactor(pkg): rename internals so their names actually make sense
A "program" was in fact a plugin, while a "plugin" represented the plugin's results.
This commit is contained in:
parent
ffbec78937
commit
1a29325c34
9 changed files with 189 additions and 179 deletions
|
@ -16,7 +16,7 @@ import (
|
||||||
|
|
||||||
"nocternity.net/gomonop/pkg/perfdata"
|
"nocternity.net/gomonop/pkg/perfdata"
|
||||||
"nocternity.net/gomonop/pkg/plugin"
|
"nocternity.net/gomonop/pkg/plugin"
|
||||||
"nocternity.net/gomonop/pkg/program"
|
"nocternity.net/gomonop/pkg/results"
|
||||||
)
|
)
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------------
|
||||||
|
@ -167,7 +167,7 @@ type programFlags struct {
|
||||||
// Program data including configuration and runtime data.
|
// Program data including configuration and runtime data.
|
||||||
type checkProgram struct {
|
type checkProgram struct {
|
||||||
programFlags // Flags from the command line
|
programFlags // Flags from the command line
|
||||||
plugin *plugin.Plugin // Plugin output state
|
plugin *results.Results // Plugin output state
|
||||||
certificate *x509.Certificate // X.509 certificate from the server
|
certificate *x509.Certificate // X.509 certificate from the server
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,16 +207,16 @@ func (flags *programFlags) parseArguments() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialise the monitoring check program.
|
// Initialise the monitoring check program.
|
||||||
func NewProgram() program.Program {
|
func NewProgram() plugin.Plugin {
|
||||||
program := &checkProgram{
|
program := &checkProgram{
|
||||||
plugin: plugin.New("Certificate check"),
|
plugin: results.New("Certificate check"),
|
||||||
}
|
}
|
||||||
program.parseArguments()
|
program.parseArguments()
|
||||||
return program
|
return program
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the program's output value.
|
// Return the program's output value.
|
||||||
func (program *checkProgram) Output() *plugin.Plugin {
|
func (program *checkProgram) Results() *results.Results {
|
||||||
return program.plugin
|
return program.plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,20 +224,20 @@ func (program *checkProgram) Output() *plugin.Plugin {
|
||||||
// if the arguments made sense.
|
// if the arguments made sense.
|
||||||
func (program *checkProgram) CheckArguments() bool {
|
func (program *checkProgram) CheckArguments() bool {
|
||||||
if program.hostname == "" {
|
if program.hostname == "" {
|
||||||
program.plugin.SetState(plugin.StatusUnknown, "no hostname specified")
|
program.plugin.SetState(results.StatusUnknown, "no hostname specified")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if program.port < 1 || program.port > 65535 {
|
if program.port < 1 || program.port > 65535 {
|
||||||
program.plugin.SetState(plugin.StatusUnknown, "invalid or missing port number")
|
program.plugin.SetState(results.StatusUnknown, "invalid or missing port number")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if program.warn != -1 && program.crit != -1 && program.warn <= program.crit {
|
if program.warn != -1 && program.crit != -1 && program.warn <= program.crit {
|
||||||
program.plugin.SetState(plugin.StatusUnknown, "nonsensical thresholds")
|
program.plugin.SetState(results.StatusUnknown, "nonsensical thresholds")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if _, ok := certGetters[program.startTLS]; !ok {
|
if _, ok := certGetters[program.startTLS]; !ok {
|
||||||
errstr := "unsupported StartTLS protocol " + program.startTLS
|
errstr := "unsupported StartTLS protocol " + program.startTLS
|
||||||
program.plugin.SetState(plugin.StatusUnknown, errstr)
|
program.plugin.SetState(results.StatusUnknown, errstr)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
program.hostname = strings.ToLower(program.hostname)
|
program.hostname = strings.ToLower(program.hostname)
|
||||||
|
@ -262,13 +262,13 @@ func (program *checkProgram) getCertificate() error {
|
||||||
// matches the requested host name.
|
// matches the requested host name.
|
||||||
func (program *checkProgram) checkSANlessCertificate() bool {
|
func (program *checkProgram) checkSANlessCertificate() bool {
|
||||||
if !program.ignoreCnOnly || len(program.extraNames) != 0 {
|
if !program.ignoreCnOnly || len(program.extraNames) != 0 {
|
||||||
program.plugin.SetState(plugin.StatusWarning,
|
program.plugin.SetState(results.StatusWarning,
|
||||||
"certificate doesn't have SAN domain names")
|
"certificate doesn't have SAN domain names")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
dn := strings.ToLower(program.certificate.Subject.String())
|
dn := strings.ToLower(program.certificate.Subject.String())
|
||||||
if !strings.HasPrefix(dn, fmt.Sprintf("cn=%s,", program.hostname)) {
|
if !strings.HasPrefix(dn, fmt.Sprintf("cn=%s,", program.hostname)) {
|
||||||
program.plugin.SetState(plugin.StatusCritical, "incorrect certificate CN")
|
program.plugin.SetState(results.StatusCritical, "incorrect certificate CN")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -298,7 +298,7 @@ func (program *checkProgram) checkNames() bool {
|
||||||
certificateIsOk = program.checkHostName(name) && certificateIsOk
|
certificateIsOk = program.checkHostName(name) && certificateIsOk
|
||||||
}
|
}
|
||||||
if !certificateIsOk {
|
if !certificateIsOk {
|
||||||
program.plugin.SetState(plugin.StatusCritical, "names missing from SAN domain names")
|
program.plugin.SetState(results.StatusCritical, "names missing from SAN domain names")
|
||||||
}
|
}
|
||||||
return certificateIsOk
|
return certificateIsOk
|
||||||
}
|
}
|
||||||
|
@ -306,26 +306,26 @@ func (program *checkProgram) checkNames() bool {
|
||||||
// Check a certificate's time to expiry against the warning and critical
|
// Check a certificate's time to expiry against the warning and critical
|
||||||
// thresholds, returning a status code and description based on these
|
// thresholds, returning a status code and description based on these
|
||||||
// values.
|
// values.
|
||||||
func (program *checkProgram) checkCertificateExpiry(tlDays int) (plugin.Status, string) {
|
func (program *checkProgram) checkCertificateExpiry(tlDays int) (results.Status, string) {
|
||||||
if tlDays <= 0 {
|
if tlDays <= 0 {
|
||||||
return plugin.StatusCritical, "certificate expired"
|
return results.StatusCritical, "certificate expired"
|
||||||
}
|
}
|
||||||
|
|
||||||
var limitStr string
|
var limitStr string
|
||||||
var state plugin.Status
|
var state results.Status
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case program.crit > 0 && tlDays <= program.crit:
|
case program.crit > 0 && tlDays <= program.crit:
|
||||||
limitStr = fmt.Sprintf(" (<= %d)", program.crit)
|
limitStr = fmt.Sprintf(" (<= %d)", program.crit)
|
||||||
state = plugin.StatusCritical
|
state = results.StatusCritical
|
||||||
|
|
||||||
case program.warn > 0 && tlDays <= program.warn:
|
case program.warn > 0 && tlDays <= program.warn:
|
||||||
limitStr = fmt.Sprintf(" (<= %d)", program.warn)
|
limitStr = fmt.Sprintf(" (<= %d)", program.warn)
|
||||||
state = plugin.StatusWarning
|
state = results.StatusWarning
|
||||||
|
|
||||||
default:
|
default:
|
||||||
limitStr = ""
|
limitStr = ""
|
||||||
state = plugin.StatusOK
|
state = results.StatusOK
|
||||||
}
|
}
|
||||||
|
|
||||||
statusString := fmt.Sprintf("certificate will expire in %d days%s",
|
statusString := fmt.Sprintf("certificate will expire in %d days%s",
|
||||||
|
@ -351,7 +351,7 @@ func (program *checkProgram) setPerfData(tlDays int) {
|
||||||
func (program *checkProgram) RunCheck() {
|
func (program *checkProgram) RunCheck() {
|
||||||
err := program.getCertificate()
|
err := program.getCertificate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
program.plugin.SetState(plugin.StatusUnknown, err.Error())
|
program.plugin.SetState(results.StatusUnknown, err.Error())
|
||||||
} else if program.checkNames() {
|
} else if program.checkNames() {
|
||||||
timeLeft := time.Until(program.certificate.NotAfter)
|
timeLeft := time.Until(program.certificate.NotAfter)
|
||||||
tlDays := int((timeLeft + 86399*time.Second) / (24 * time.Hour))
|
tlDays := int((timeLeft + 86399*time.Second) / (24 * time.Hour))
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
|
|
||||||
"nocternity.net/gomonop/pkg/perfdata"
|
"nocternity.net/gomonop/pkg/perfdata"
|
||||||
"nocternity.net/gomonop/pkg/plugin"
|
"nocternity.net/gomonop/pkg/plugin"
|
||||||
"nocternity.net/gomonop/pkg/program"
|
"nocternity.net/gomonop/pkg/results"
|
||||||
)
|
)
|
||||||
|
|
||||||
//-------------------------------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------------------------------
|
||||||
|
@ -55,8 +55,8 @@ type programFlags struct {
|
||||||
|
|
||||||
// Program data including configuration and runtime data.
|
// Program data including configuration and runtime data.
|
||||||
type checkProgram struct {
|
type checkProgram struct {
|
||||||
programFlags // Flags from the command line
|
programFlags // Flags from the command line
|
||||||
plugin *plugin.Plugin // Plugin output state
|
plugin *results.Results // Plugin output state
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse command line arguments and store their values. If the -h flag is present,
|
// Parse command line arguments and store their values. If the -h flag is present,
|
||||||
|
@ -77,39 +77,39 @@ func (flags *programFlags) parseArguments() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialise the monitoring check program.
|
// Initialise the monitoring check program.
|
||||||
func NewProgram() program.Program {
|
func NewProgram() plugin.Plugin {
|
||||||
program := &checkProgram{
|
program := &checkProgram{
|
||||||
plugin: plugin.New("DNS zone serial match check"),
|
plugin: results.New("DNS zone serial match check"),
|
||||||
}
|
}
|
||||||
program.parseArguments()
|
program.parseArguments()
|
||||||
return program
|
return program
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the program's output value.
|
// Return the program's output value.
|
||||||
func (program *checkProgram) Output() *plugin.Plugin {
|
func (program *checkProgram) Results() *results.Results {
|
||||||
return program.plugin
|
return program.plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the values that were specified from the command line. Returns true if the arguments made sense.
|
// Check the values that were specified from the command line. Returns true if the arguments made sense.
|
||||||
func (program *checkProgram) CheckArguments() bool {
|
func (program *checkProgram) CheckArguments() bool {
|
||||||
if program.hostname == "" {
|
if program.hostname == "" {
|
||||||
program.plugin.SetState(plugin.StatusUnknown, "no DNS hostname specified")
|
program.plugin.SetState(results.StatusUnknown, "no DNS hostname specified")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if program.port < 1 || program.port > 65535 {
|
if program.port < 1 || program.port > 65535 {
|
||||||
program.plugin.SetState(plugin.StatusUnknown, "invalid DNS port number")
|
program.plugin.SetState(results.StatusUnknown, "invalid DNS port number")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if program.zone == "" {
|
if program.zone == "" {
|
||||||
program.plugin.SetState(plugin.StatusUnknown, "no DNS zone specified")
|
program.plugin.SetState(results.StatusUnknown, "no DNS zone specified")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if program.rsHostname == "" {
|
if program.rsHostname == "" {
|
||||||
program.plugin.SetState(plugin.StatusUnknown, "no reference DNS hostname specified")
|
program.plugin.SetState(results.StatusUnknown, "no reference DNS hostname specified")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if program.rsPort < 1 || program.rsPort > 65535 {
|
if program.rsPort < 1 || program.rsPort > 65535 {
|
||||||
program.plugin.SetState(plugin.StatusUnknown, "invalid reference DNS port number")
|
program.plugin.SetState(results.StatusUnknown, "invalid reference DNS port number")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
program.hostname = strings.ToLower(program.hostname)
|
program.hostname = strings.ToLower(program.hostname)
|
||||||
|
@ -176,12 +176,12 @@ func (program *checkProgram) RunCheck() {
|
||||||
cOk, cSerial := program.getSerial("checked", checkResponse)
|
cOk, cSerial := program.getSerial("checked", checkResponse)
|
||||||
rOk, rSerial := program.getSerial("reference", refResponse)
|
rOk, rSerial := program.getSerial("reference", refResponse)
|
||||||
if !(cOk && rOk) {
|
if !(cOk && rOk) {
|
||||||
program.plugin.SetState(plugin.StatusUnknown, "could not read serials")
|
program.plugin.SetState(results.StatusUnknown, "could not read serials")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if cSerial == rSerial {
|
if cSerial == rSerial {
|
||||||
program.plugin.SetState(plugin.StatusOK, "serials match")
|
program.plugin.SetState(results.StatusOK, "serials match")
|
||||||
} else {
|
} else {
|
||||||
program.plugin.SetState(plugin.StatusCritical, "serials mismatch")
|
program.plugin.SetState(results.StatusCritical, "serials mismatch")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
30
main.go
30
main.go
|
@ -8,33 +8,33 @@ import (
|
||||||
"nocternity.net/gomonop/cmd/sslcert"
|
"nocternity.net/gomonop/cmd/sslcert"
|
||||||
"nocternity.net/gomonop/cmd/zoneserial"
|
"nocternity.net/gomonop/cmd/zoneserial"
|
||||||
"nocternity.net/gomonop/pkg/plugin"
|
"nocternity.net/gomonop/pkg/plugin"
|
||||||
"nocternity.net/gomonop/pkg/program"
|
"nocternity.net/gomonop/pkg/results"
|
||||||
"nocternity.net/gomonop/pkg/version"
|
"nocternity.net/gomonop/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
programs map[string]program.Builder = map[string]program.Builder{
|
plugins map[string]plugin.Builder = map[string]plugin.Builder{
|
||||||
"check_ssl_certificate": sslcert.NewProgram,
|
"check_ssl_certificate": sslcert.NewProgram,
|
||||||
"check_zone_serial": zoneserial.NewProgram,
|
"check_zone_serial": zoneserial.NewProgram,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func getProgram() program.Program {
|
func getPlugin() plugin.Plugin {
|
||||||
ownName := filepath.Base(os.Args[0])
|
ownName := filepath.Base(os.Args[0])
|
||||||
|
|
||||||
if builder, ok := programs[ownName]; ok {
|
if builder, ok := plugins[ownName]; ok {
|
||||||
return builder()
|
return builder()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
fmt.Printf("Syntax: %s <program> [arguments]\n", ownName)
|
fmt.Printf("Syntax: %s <plugin> [arguments]\n", ownName)
|
||||||
fmt.Printf(" %s --programs|-p\n", ownName)
|
fmt.Printf(" %s --plugin|-p\n", ownName)
|
||||||
fmt.Printf(" %s --version|-v", ownName)
|
fmt.Printf(" %s --version|-v", ownName)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
case "--programs", "-p":
|
case "--plugins", "-p":
|
||||||
for name := range programs {
|
for name := range plugins {
|
||||||
fmt.Println(name)
|
fmt.Println(name)
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
@ -44,30 +44,30 @@ func getProgram() program.Program {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if builder, ok := programs[os.Args[1]]; ok {
|
if builder, ok := plugins[os.Args[1]]; ok {
|
||||||
os.Args = os.Args[1:]
|
os.Args = os.Args[1:]
|
||||||
return builder()
|
return builder()
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Unknown program: %s\n", os.Args[1])
|
fmt.Printf("Unknown plugin: %s\n", os.Args[1])
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
program := getProgram()
|
runPlugin := getPlugin()
|
||||||
|
|
||||||
output := program.Output()
|
output := runPlugin.Results()
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
output.SetState(plugin.StatusUnknown, "Internal error")
|
output.SetState(results.StatusUnknown, "Internal error")
|
||||||
output.AddLinef("Error info: %v", r)
|
output.AddLinef("Error info: %v", r)
|
||||||
}
|
}
|
||||||
fmt.Println(output.String())
|
fmt.Println(output.String())
|
||||||
os.Exit(output.ExitCode())
|
os.Exit(output.ExitCode())
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if program.CheckArguments() {
|
if runPlugin.CheckArguments() {
|
||||||
program.RunCheck()
|
runPlugin.RunCheck()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,106 +1,20 @@
|
||||||
// Package plugin implements a helper that can be used to implement a Nagios,
|
|
||||||
// Centreon, Icinga... service monitoring plugin.
|
|
||||||
package plugin // import nocternity.net/gomonop/pkg/plugin
|
package plugin // import nocternity.net/gomonop/pkg/plugin
|
||||||
|
|
||||||
import (
|
import "nocternity.net/gomonop/pkg/results"
|
||||||
"container/list"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"nocternity.net/gomonop/pkg/perfdata"
|
// Plugin represents the interface to a monitoring plugin.
|
||||||
)
|
type Plugin interface {
|
||||||
|
// Results accesses the results of the monitoring plugin.
|
||||||
|
Results() *results.Results
|
||||||
|
|
||||||
// Plugin represents the monitoring plugin's state, including its name,
|
// CheckArguments ensures that the arguments that were passed to the plugin
|
||||||
// return status and message, additional lines of text, and performance
|
// actually make sense. Errors should be stored in the plugin's results.
|
||||||
// data to be encoded in the output.
|
CheckArguments() bool
|
||||||
type Plugin struct {
|
|
||||||
name string
|
// RunCheck actually runs whatever checks are implemented by the plugin and
|
||||||
status Status
|
// updates the results accordingly.
|
||||||
message string
|
RunCheck()
|
||||||
extraText *list.List
|
|
||||||
perfData map[string]*perfdata.PerfData
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates the plugin with `name` as its name and an unknown status.
|
// Builder is a function that can be called in order to instantiate a plugin.
|
||||||
func New(name string) *Plugin {
|
type Builder func() Plugin
|
||||||
p := new(Plugin)
|
|
||||||
p.name = name
|
|
||||||
p.status = StatusUnknown
|
|
||||||
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`.
|
|
||||||
func (p *Plugin) SetState(status Status, message string) {
|
|
||||||
p.status = status
|
|
||||||
p.message = message
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddLine adds the specified string to the extra output text buffer.
|
|
||||||
func (p *Plugin) 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 *Plugin) AddLinef(format string, data ...interface{}) {
|
|
||||||
p.AddLine(fmt.Sprintf(format, data...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 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 *Plugin) 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 *Plugin) 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 *Plugin) ExitCode() int {
|
|
||||||
return int(p.status)
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
package program // import nocternity.net/gomonop/pkg/program
|
|
||||||
|
|
||||||
import "nocternity.net/gomonop/pkg/plugin"
|
|
||||||
|
|
||||||
type Program interface {
|
|
||||||
Output() *plugin.Plugin
|
|
||||||
CheckArguments() bool
|
|
||||||
RunCheck()
|
|
||||||
}
|
|
||||||
|
|
||||||
type Builder func() Program
|
|
107
pkg/results/results.go
Normal file
107
pkg/results/results.go
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
// 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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
status Status
|
||||||
|
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
|
||||||
|
p.status = StatusUnknown
|
||||||
|
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`.
|
||||||
|
func (p *Results) SetState(status Status, message string) {
|
||||||
|
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)
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package plugin // import nocternity.net/gomonop/pkg/plugin
|
package results // import nocternity.net/gomonop/pkg/results
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/list"
|
"container/list"
|
||||||
|
@ -19,7 +19,7 @@ func TestNew(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetState(t *testing.T) {
|
func TestSetState(t *testing.T) {
|
||||||
p := Plugin{}
|
p := Results{}
|
||||||
|
|
||||||
p.SetState(StatusWarning, "test")
|
p.SetState(StatusWarning, "test")
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ func TestSetState(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddLineFirst(t *testing.T) {
|
func TestAddLineFirst(t *testing.T) {
|
||||||
p := Plugin{}
|
p := Results{}
|
||||||
|
|
||||||
p.AddLine("test")
|
p.AddLine("test")
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ func TestAddLineFirst(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddLineNext(t *testing.T) {
|
func TestAddLineNext(t *testing.T) {
|
||||||
p := Plugin{}
|
p := Results{}
|
||||||
p.extraText = list.New()
|
p.extraText = list.New()
|
||||||
|
|
||||||
p.AddLine("test")
|
p.AddLine("test")
|
||||||
|
@ -48,7 +48,7 @@ func TestAddLineNext(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddLinef(t *testing.T) {
|
func TestAddLinef(t *testing.T) {
|
||||||
p := Plugin{}
|
p := Results{}
|
||||||
|
|
||||||
p.AddLinef("test %d", 123)
|
p.AddLinef("test %d", 123)
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ func TestAddLinef(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddLines(t *testing.T) {
|
func TestAddLines(t *testing.T) {
|
||||||
p := Plugin{}
|
p := Results{}
|
||||||
|
|
||||||
p.AddLines([]string{"test", "test2"})
|
p.AddLines([]string{"test", "test2"})
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ func TestAddLines(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddPerfData(t *testing.T) {
|
func TestAddPerfData(t *testing.T) {
|
||||||
p := Plugin{}
|
p := Results{}
|
||||||
p.perfData = make(map[string]*perfdata.PerfData)
|
p.perfData = make(map[string]*perfdata.PerfData)
|
||||||
|
|
||||||
p.AddPerfData(&perfdata.PerfData{Label: "test"})
|
p.AddPerfData(&perfdata.PerfData{Label: "test"})
|
||||||
|
@ -78,7 +78,7 @@ func TestAddPerfData(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddPerfDataDuplicate(t *testing.T) {
|
func TestAddPerfDataDuplicate(t *testing.T) {
|
||||||
p := Plugin{}
|
p := Results{}
|
||||||
p.perfData = make(map[string]*perfdata.PerfData)
|
p.perfData = make(map[string]*perfdata.PerfData)
|
||||||
p.perfData["test"] = &perfdata.PerfData{Label: "test"}
|
p.perfData["test"] = &perfdata.PerfData{Label: "test"}
|
||||||
|
|
||||||
|
@ -87,14 +87,14 @@ func TestAddPerfDataDuplicate(t *testing.T) {
|
||||||
|
|
||||||
func TestString(t *testing.T) {
|
func TestString(t *testing.T) {
|
||||||
type Test struct {
|
type Test struct {
|
||||||
Plugin
|
Results
|
||||||
expected string
|
expected string
|
||||||
}
|
}
|
||||||
|
|
||||||
pdat := perfdata.PerfData{Label: "test"}
|
pdat := perfdata.PerfData{Label: "test"}
|
||||||
tests := []Test{
|
tests := []Test{
|
||||||
{
|
{
|
||||||
Plugin{
|
Results{
|
||||||
name: "test",
|
name: "test",
|
||||||
status: StatusWarning,
|
status: StatusWarning,
|
||||||
message: "test",
|
message: "test",
|
||||||
|
@ -103,8 +103,8 @@ func TestString(t *testing.T) {
|
||||||
"test WARNING: test",
|
"test WARNING: test",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() Plugin {
|
func() Results {
|
||||||
p := Plugin{
|
p := Results{
|
||||||
name: "test",
|
name: "test",
|
||||||
status: StatusWarning,
|
status: StatusWarning,
|
||||||
message: "test",
|
message: "test",
|
||||||
|
@ -118,8 +118,8 @@ func TestString(t *testing.T) {
|
||||||
"test WARNING: test\ntest 1\ntest 2",
|
"test WARNING: test\ntest 1\ntest 2",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() Plugin {
|
func() Results {
|
||||||
p := Plugin{
|
p := Results{
|
||||||
name: "test",
|
name: "test",
|
||||||
status: StatusWarning,
|
status: StatusWarning,
|
||||||
message: "test",
|
message: "test",
|
||||||
|
@ -133,8 +133,8 @@ func TestString(t *testing.T) {
|
||||||
pdat.String(),
|
pdat.String(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() Plugin {
|
func() Results {
|
||||||
p := Plugin{
|
p := Results{
|
||||||
name: "test",
|
name: "test",
|
||||||
status: StatusWarning,
|
status: StatusWarning,
|
||||||
message: "test",
|
message: "test",
|
||||||
|
@ -153,13 +153,13 @@ func TestString(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
result := test.Plugin.String()
|
result := test.Results.String()
|
||||||
assert.Equal(t, test.expected, result, "Expected '%s', got '%s'", test.expected, result)
|
assert.Equal(t, test.expected, result, "Expected '%s', got '%s'", test.expected, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExitCode(t *testing.T) {
|
func TestExitCode(t *testing.T) {
|
||||||
p := Plugin{}
|
p := Results{}
|
||||||
|
|
||||||
p.status = StatusOK
|
p.status = StatusOK
|
||||||
assert.Equal(t, int(StatusOK), p.ExitCode())
|
assert.Equal(t, int(StatusOK), p.ExitCode())
|
|
@ -1,4 +1,4 @@
|
||||||
package plugin // import nocternity.net/gomonop/pkg/plugin
|
package results // import nocternity.net/gomonop/pkg/results
|
||||||
|
|
||||||
// Status represents the return status of the monitoring plugin. The
|
// Status represents the return status of the monitoring plugin. The
|
||||||
// corresponding integer value will be used as the program's exit code,
|
// corresponding integer value will be used as the program's exit code,
|
|
@ -1,4 +1,4 @@
|
||||||
package plugin // import nocternity.net/gomonop/pkg/plugin
|
package results // import nocternity.net/gomonop/pkg/results
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
Loading…
Reference in a new issue