diff --git a/cmd/sslcert/main.go b/cmd/sslcert/main.go index efb4971..be9c921 100644 --- a/cmd/sslcert/main.go +++ b/cmd/sslcert/main.go @@ -215,9 +215,9 @@ func NewProgram() program.Program { return program } -// Terminate the monitoring check program. -func (program *checkProgram) Done() { - program.plugin.Done() +// Return the program's output value. +func (program *checkProgram) Output() *plugin.Plugin { + return program.plugin } // Check the values that were specified from the command line. Returns true diff --git a/cmd/zoneserial/main.go b/cmd/zoneserial/main.go index e8bc7d6..69bd27d 100644 --- a/cmd/zoneserial/main.go +++ b/cmd/zoneserial/main.go @@ -85,13 +85,9 @@ func NewProgram() program.Program { return program } -// Terminate the monitoring check program. -func (program *checkProgram) Done() { - if r := recover(); r != nil { - program.plugin.SetState(plugin.StatusUnknown, "Internal error") - program.plugin.AddLinef("Error info: %v", r) - } - program.plugin.Done() +// Return the program's output value. +func (program *checkProgram) Output() *plugin.Plugin { + return program.plugin } // Check the values that were specified from the command line. Returns true if the arguments made sense. diff --git a/main.go b/main.go index e8ce79a..af989b1 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "nocternity.net/gomonop/cmd/sslcert" "nocternity.net/gomonop/cmd/zoneserial" + "nocternity.net/gomonop/pkg/plugin" "nocternity.net/gomonop/pkg/program" "nocternity.net/gomonop/pkg/version" ) @@ -55,7 +56,17 @@ func getProgram() program.Program { func main() { program := getProgram() - defer program.Done() + + output := program.Output() + defer func() { + if r := recover(); r != nil { + output.SetState(plugin.StatusUnknown, "Internal error") + output.AddLinef("Error info: %v", r) + } + fmt.Println(output.String()) + os.Exit(output.ExitCode()) + }() + if program.CheckArguments() { program.RunCheck() } diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index 061b06f..b967e9f 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -5,7 +5,6 @@ package plugin // import nocternity.net/gomonop/pkg/perfdata import ( "container/list" "fmt" - "os" "strings" "nocternity.net/gomonop/pkg/perfdata" @@ -70,10 +69,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() { +// 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(" ") @@ -99,6 +97,10 @@ func (p *Plugin) Done() { strBuilder.WriteString(em.Value.(string)) } } - fmt.Println(strBuilder.String()) - os.Exit(int(p.status)) + return strBuilder.String() +} + +// ExitCode returns the plugin's exit code. +func (p *Plugin) ExitCode() int { + return int(p.status) } diff --git a/pkg/program/program.go b/pkg/program/program.go index d8b3510..2d5a1dd 100644 --- a/pkg/program/program.go +++ b/pkg/program/program.go @@ -1,9 +1,11 @@ package program // import nocternity.net/gomonop/pkg/program +import "nocternity.net/gomonop/pkg/plugin" + type Program interface { + Output() *plugin.Plugin CheckArguments() bool RunCheck() - Done() } type Builder func() Program