refactor(pkg): move Status to a separate module

This commit is contained in:
Emmanuel BENOîT 2024-07-20 18:56:51 +02:00
parent 7209591e08
commit 3263d8c583
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg
7 changed files with 51 additions and 44 deletions

View file

@ -17,6 +17,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/results" "nocternity.net/gomonop/pkg/results"
"nocternity.net/gomonop/pkg/status"
) )
//-------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------
@ -224,20 +225,20 @@ func (program *checkProgram) Results() *results.Results {
// 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(results.StatusUnknown, "no hostname specified") program.plugin.SetState(status.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(results.StatusUnknown, "invalid or missing port number") program.plugin.SetState(status.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(results.StatusUnknown, "nonsensical thresholds") program.plugin.SetState(status.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(results.StatusUnknown, errstr) program.plugin.SetState(status.StatusUnknown, errstr)
return false return false
} }
program.hostname = strings.ToLower(program.hostname) program.hostname = strings.ToLower(program.hostname)
@ -262,13 +263,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(results.StatusWarning, program.plugin.SetState(status.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(results.StatusCritical, "incorrect certificate CN") program.plugin.SetState(status.StatusCritical, "incorrect certificate CN")
return false return false
} }
return true return true
@ -298,7 +299,7 @@ func (program *checkProgram) checkNames() bool {
certificateIsOk = program.checkHostName(name) && certificateIsOk certificateIsOk = program.checkHostName(name) && certificateIsOk
} }
if !certificateIsOk { if !certificateIsOk {
program.plugin.SetState(results.StatusCritical, "names missing from SAN domain names") program.plugin.SetState(status.StatusCritical, "names missing from SAN domain names")
} }
return certificateIsOk return certificateIsOk
} }
@ -306,26 +307,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) (results.Status, string) { func (program *checkProgram) checkCertificateExpiry(tlDays int) (status.Status, string) {
if tlDays <= 0 { if tlDays <= 0 {
return results.StatusCritical, "certificate expired" return status.StatusCritical, "certificate expired"
} }
var limitStr string var limitStr string
var state results.Status var state status.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 = results.StatusCritical state = status.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 = results.StatusWarning state = status.StatusWarning
default: default:
limitStr = "" limitStr = ""
state = results.StatusOK state = status.StatusOK
} }
statusString := fmt.Sprintf("certificate will expire in %d days%s", statusString := fmt.Sprintf("certificate will expire in %d days%s",
@ -351,7 +352,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(results.StatusUnknown, err.Error()) program.plugin.SetState(status.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))

View file

@ -15,6 +15,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/results" "nocternity.net/gomonop/pkg/results"
"nocternity.net/gomonop/pkg/status"
) )
//------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------
@ -93,23 +94,23 @@ func (program *checkProgram) Results() *results.Results {
// 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(results.StatusUnknown, "no DNS hostname specified") program.plugin.SetState(status.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(results.StatusUnknown, "invalid DNS port number") program.plugin.SetState(status.StatusUnknown, "invalid DNS port number")
return false return false
} }
if program.zone == "" { if program.zone == "" {
program.plugin.SetState(results.StatusUnknown, "no DNS zone specified") program.plugin.SetState(status.StatusUnknown, "no DNS zone specified")
return false return false
} }
if program.rsHostname == "" { if program.rsHostname == "" {
program.plugin.SetState(results.StatusUnknown, "no reference DNS hostname specified") program.plugin.SetState(status.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(results.StatusUnknown, "invalid reference DNS port number") program.plugin.SetState(status.StatusUnknown, "invalid reference DNS port number")
return false return false
} }
program.hostname = strings.ToLower(program.hostname) program.hostname = strings.ToLower(program.hostname)
@ -176,12 +177,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(results.StatusUnknown, "could not read serials") program.plugin.SetState(status.StatusUnknown, "could not read serials")
return return
} }
if cSerial == rSerial { if cSerial == rSerial {
program.plugin.SetState(results.StatusOK, "serials match") program.plugin.SetState(status.StatusOK, "serials match")
} else { } else {
program.plugin.SetState(results.StatusCritical, "serials mismatch") program.plugin.SetState(status.StatusCritical, "serials mismatch")
} }
} }

View file

@ -8,7 +8,7 @@ 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/results" "nocternity.net/gomonop/pkg/status"
"nocternity.net/gomonop/pkg/version" "nocternity.net/gomonop/pkg/version"
) )
@ -61,7 +61,7 @@ func main() {
output := runPlugin.Results() output := runPlugin.Results()
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
output.SetState(results.StatusUnknown, "Internal error") output.SetState(status.StatusUnknown, "Internal error")
output.AddLinef("Error info: %v", r) output.AddLinef("Error info: %v", r)
} }
fmt.Println(output.String()) fmt.Println(output.String())

View file

@ -9,6 +9,7 @@ import (
"strings" "strings"
"nocternity.net/gomonop/pkg/perfdata" "nocternity.net/gomonop/pkg/perfdata"
"nocternity.net/gomonop/pkg/status"
) )
// Results represents the monitoring plugin's results, including its name, // Results represents the monitoring plugin's results, including its name,
@ -16,7 +17,7 @@ import (
// data to be encoded in the output. // data to be encoded in the output.
type Results struct { type Results struct {
name string name string
status Status status status.Status
message string message string
extraText *list.List extraText *list.List
perfData map[string]*perfdata.PerfData perfData map[string]*perfdata.PerfData
@ -26,7 +27,7 @@ type Results struct {
func New(name string) *Results { func New(name string) *Results {
p := new(Results) p := new(Results)
p.name = name p.name = name
p.status = StatusUnknown p.status = status.StatusUnknown
p.message = "no status set" p.message = "no status set"
p.perfData = make(map[string]*perfdata.PerfData) p.perfData = make(map[string]*perfdata.PerfData)
return p return p
@ -34,7 +35,7 @@ func New(name string) *Results {
// SetState sets the plugin's output code to `status` and its message to // SetState sets the plugin's output code to `status` and its message to
// the specified `message`. // the specified `message`.
func (p *Results) SetState(status Status, message string) { func (p *Results) SetState(status status.Status, message string) {
p.status = status p.status = status
p.message = message p.message = message
} }

View file

@ -5,14 +5,16 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"nocternity.net/gomonop/pkg/perfdata" "nocternity.net/gomonop/pkg/perfdata"
"nocternity.net/gomonop/pkg/status"
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
p := New("test") p := New("test")
assert.Equal(t, p.name, "test") assert.Equal(t, p.name, "test")
assert.Equal(t, p.status, StatusUnknown) assert.Equal(t, p.status, status.StatusUnknown)
assert.Equal(t, p.message, "no status set") assert.Equal(t, p.message, "no status set")
assert.Nil(t, p.extraText) assert.Nil(t, p.extraText)
assert.NotNil(t, p.perfData) assert.NotNil(t, p.perfData)
@ -21,9 +23,9 @@ func TestNew(t *testing.T) {
func TestSetState(t *testing.T) { func TestSetState(t *testing.T) {
p := Results{} p := Results{}
p.SetState(StatusWarning, "test") p.SetState(status.StatusWarning, "test")
assert.Equal(t, p.status, StatusWarning) assert.Equal(t, p.status, status.StatusWarning)
assert.Equal(t, p.message, "test") assert.Equal(t, p.message, "test")
} }
@ -96,7 +98,7 @@ func TestString(t *testing.T) {
{ {
Results{ Results{
name: "test", name: "test",
status: StatusWarning, status: status.StatusWarning,
message: "test", message: "test",
perfData: make(map[string]*perfdata.PerfData), perfData: make(map[string]*perfdata.PerfData),
}, },
@ -106,7 +108,7 @@ func TestString(t *testing.T) {
func() Results { func() Results {
p := Results{ p := Results{
name: "test", name: "test",
status: StatusWarning, status: status.StatusWarning,
message: "test", message: "test",
perfData: make(map[string]*perfdata.PerfData), perfData: make(map[string]*perfdata.PerfData),
extraText: list.New(), extraText: list.New(),
@ -121,7 +123,7 @@ func TestString(t *testing.T) {
func() Results { func() Results {
p := Results{ p := Results{
name: "test", name: "test",
status: StatusWarning, status: status.StatusWarning,
message: "test", message: "test",
perfData: make(map[string]*perfdata.PerfData), perfData: make(map[string]*perfdata.PerfData),
} }
@ -136,7 +138,7 @@ func TestString(t *testing.T) {
func() Results { func() Results {
p := Results{ p := Results{
name: "test", name: "test",
status: StatusWarning, status: status.StatusWarning,
message: "test", message: "test",
perfData: make(map[string]*perfdata.PerfData), perfData: make(map[string]*perfdata.PerfData),
extraText: list.New(), extraText: list.New(),
@ -161,15 +163,15 @@ func TestString(t *testing.T) {
func TestExitCode(t *testing.T) { func TestExitCode(t *testing.T) {
p := Results{} p := Results{}
p.status = StatusOK p.status = status.StatusOK
assert.Equal(t, int(StatusOK), p.ExitCode()) assert.Equal(t, int(status.StatusOK), p.ExitCode())
p.status = StatusWarning p.status = status.StatusWarning
assert.Equal(t, int(StatusWarning), p.ExitCode()) assert.Equal(t, int(status.StatusWarning), p.ExitCode())
p.status = StatusCritical p.status = status.StatusCritical
assert.Equal(t, int(StatusCritical), p.ExitCode()) assert.Equal(t, int(status.StatusCritical), p.ExitCode())
p.status = StatusUnknown p.status = status.StatusUnknown
assert.Equal(t, int(StatusUnknown), p.ExitCode()) assert.Equal(t, int(status.StatusUnknown), p.ExitCode())
} }

View file

@ -1,4 +1,6 @@
package results // import nocternity.net/gomonop/pkg/results // The status package contains the datatype that corresponds to monitoring
// plugin status values.
package status // import nocternity.net/gomonop/pkg/status
// 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,

View file

@ -1,4 +1,4 @@
package results // import nocternity.net/gomonop/pkg/results package status // import nocternity.net/gomonop/pkg/status
import ( import (
"testing" "testing"