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
pkg
|
@ -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
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"strings"
|
||||
import "nocternity.net/gomonop/pkg/results"
|
||||
|
||||
"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,
|
||||
// return status and message, additional lines of text, and performance
|
||||
// data to be encoded in the output.
|
||||
type Plugin struct {
|
||||
name string
|
||||
status Status
|
||||
message string
|
||||
extraText *list.List
|
||||
perfData map[string]*perfdata.PerfData
|
||||
// CheckArguments ensures that the arguments that were passed to the plugin
|
||||
// actually make sense. Errors should be stored in the plugin's results.
|
||||
CheckArguments() bool
|
||||
|
||||
// RunCheck actually runs whatever checks are implemented by the plugin and
|
||||
// updates the results accordingly.
|
||||
RunCheck()
|
||||
}
|
||||
|
||||
// New creates the plugin with `name` as its name and an unknown status.
|
||||
func New(name string) *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)
|
||||
}
|
||||
// Builder is a function that can be called in order to instantiate a plugin.
|
||||
type Builder func() Plugin
|
||||
|
|
|
@ -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 (
|
||||
"container/list"
|
||||
|
@ -19,7 +19,7 @@ func TestNew(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSetState(t *testing.T) {
|
||||
p := Plugin{}
|
||||
p := Results{}
|
||||
|
||||
p.SetState(StatusWarning, "test")
|
||||
|
||||
|
@ -28,7 +28,7 @@ func TestSetState(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAddLineFirst(t *testing.T) {
|
||||
p := Plugin{}
|
||||
p := Results{}
|
||||
|
||||
p.AddLine("test")
|
||||
|
||||
|
@ -38,7 +38,7 @@ func TestAddLineFirst(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAddLineNext(t *testing.T) {
|
||||
p := Plugin{}
|
||||
p := Results{}
|
||||
p.extraText = list.New()
|
||||
|
||||
p.AddLine("test")
|
||||
|
@ -48,7 +48,7 @@ func TestAddLineNext(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAddLinef(t *testing.T) {
|
||||
p := Plugin{}
|
||||
p := Results{}
|
||||
|
||||
p.AddLinef("test %d", 123)
|
||||
|
||||
|
@ -57,7 +57,7 @@ func TestAddLinef(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAddLines(t *testing.T) {
|
||||
p := Plugin{}
|
||||
p := Results{}
|
||||
|
||||
p.AddLines([]string{"test", "test2"})
|
||||
|
||||
|
@ -67,7 +67,7 @@ func TestAddLines(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAddPerfData(t *testing.T) {
|
||||
p := Plugin{}
|
||||
p := Results{}
|
||||
p.perfData = make(map[string]*perfdata.PerfData)
|
||||
|
||||
p.AddPerfData(&perfdata.PerfData{Label: "test"})
|
||||
|
@ -78,7 +78,7 @@ func TestAddPerfData(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAddPerfDataDuplicate(t *testing.T) {
|
||||
p := Plugin{}
|
||||
p := Results{}
|
||||
p.perfData = make(map[string]*perfdata.PerfData)
|
||||
p.perfData["test"] = &perfdata.PerfData{Label: "test"}
|
||||
|
||||
|
@ -87,14 +87,14 @@ func TestAddPerfDataDuplicate(t *testing.T) {
|
|||
|
||||
func TestString(t *testing.T) {
|
||||
type Test struct {
|
||||
Plugin
|
||||
Results
|
||||
expected string
|
||||
}
|
||||
|
||||
pdat := perfdata.PerfData{Label: "test"}
|
||||
tests := []Test{
|
||||
{
|
||||
Plugin{
|
||||
Results{
|
||||
name: "test",
|
||||
status: StatusWarning,
|
||||
message: "test",
|
||||
|
@ -103,8 +103,8 @@ func TestString(t *testing.T) {
|
|||
"test WARNING: test",
|
||||
},
|
||||
{
|
||||
func() Plugin {
|
||||
p := Plugin{
|
||||
func() Results {
|
||||
p := Results{
|
||||
name: "test",
|
||||
status: StatusWarning,
|
||||
message: "test",
|
||||
|
@ -118,8 +118,8 @@ func TestString(t *testing.T) {
|
|||
"test WARNING: test\ntest 1\ntest 2",
|
||||
},
|
||||
{
|
||||
func() Plugin {
|
||||
p := Plugin{
|
||||
func() Results {
|
||||
p := Results{
|
||||
name: "test",
|
||||
status: StatusWarning,
|
||||
message: "test",
|
||||
|
@ -133,8 +133,8 @@ func TestString(t *testing.T) {
|
|||
pdat.String(),
|
||||
},
|
||||
{
|
||||
func() Plugin {
|
||||
p := Plugin{
|
||||
func() Results {
|
||||
p := Results{
|
||||
name: "test",
|
||||
status: StatusWarning,
|
||||
message: "test",
|
||||
|
@ -153,13 +153,13 @@ func TestString(t *testing.T) {
|
|||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExitCode(t *testing.T) {
|
||||
p := Plugin{}
|
||||
p := Results{}
|
||||
|
||||
p.status = StatusOK
|
||||
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
|
||||
// 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 (
|
||||
"testing"
|
Loading…
Add table
Add a link
Reference in a new issue