refactor: change the code's whole structure

This commit is contained in:
Emmanuel BENOîT 2024-07-19 14:29:51 +02:00
parent a659154937
commit 96637019c1
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg
13 changed files with 295 additions and 52 deletions
cmd
sslcert
zoneserial

View file

@ -1,4 +1,4 @@
package main
package sslcert // import nocternity.net/go-monitoring/cmd/sslcert
import (
"bufio"
@ -12,8 +12,9 @@ import (
"strings"
"time"
"nocternity.net/go/monitoring/perfdata"
"nocternity.net/go/monitoring/plugin"
"nocternity.net/go-monitoring/pkg/perfdata"
"nocternity.net/go-monitoring/pkg/plugin"
"nocternity.net/go-monitoring/pkg/program"
"github.com/karrick/golf"
)
@ -198,7 +199,7 @@ func (flags *programFlags) parseArguments() {
}
// Initialise the monitoring check program.
func newProgram() *checkProgram {
func NewProgram() program.Program {
program := &checkProgram{
plugin: plugin.New("Certificate check"),
}
@ -207,13 +208,13 @@ func newProgram() *checkProgram {
}
// Terminate the monitoring check program.
func (program *checkProgram) close() {
func (program *checkProgram) Done() {
program.plugin.Done()
}
// Check the values that were specified from the command line. Returns true
// if the arguments made sense.
func (program *checkProgram) checkFlags() bool {
func (program *checkProgram) CheckArguments() bool {
if program.hostname == "" {
program.plugin.SetState(plugin.UNKNOWN, "no hostname specified")
return false
@ -332,7 +333,7 @@ func (program *checkProgram) setPerfData(tlDays int) {
// Run the check: fetch the certificate, check its names then check its time
// to expiry and update the plugin's performance data.
func (program *checkProgram) runCheck() {
func (program *checkProgram) RunCheck() {
err := program.getCertificate()
if err != nil {
program.plugin.SetState(plugin.UNKNOWN, err.Error())
@ -343,11 +344,3 @@ func (program *checkProgram) runCheck() {
program.setPerfData(tlDays)
}
}
func main() {
program := newProgram()
defer program.close()
if program.checkFlags() {
program.runCheck()
}
}

View file

@ -1,4 +1,4 @@
package main
package zoneserial // import nocternity.net/go-monitoring/cmd/zoneserial
import (
"fmt"
@ -8,8 +8,9 @@ import (
"strings"
"time"
"nocternity.net/go/monitoring/perfdata"
"nocternity.net/go/monitoring/plugin"
"nocternity.net/go-monitoring/pkg/perfdata"
"nocternity.net/go-monitoring/pkg/plugin"
"nocternity.net/go-monitoring/pkg/program"
"github.com/karrick/golf"
"github.com/miekg/dns"
@ -75,7 +76,7 @@ func (flags *programFlags) parseArguments() {
}
// Initialise the monitoring check program.
func newProgram() *checkProgram {
func NewProgram() program.Program {
program := &checkProgram{
plugin: plugin.New("DNS zone serial match check"),
}
@ -84,7 +85,7 @@ func newProgram() *checkProgram {
}
// Terminate the monitoring check program.
func (program *checkProgram) close() {
func (program *checkProgram) Done() {
if r := recover(); r != nil {
program.plugin.SetState(plugin.UNKNOWN, "Internal error")
program.plugin.AddLine("Error info: %v", r)
@ -93,7 +94,7 @@ func (program *checkProgram) close() {
}
// Check the values that were specified from the command line. Returns true if the arguments made sense.
func (program *checkProgram) checkFlags() bool {
func (program *checkProgram) CheckArguments() bool {
if program.hostname == "" {
program.plugin.SetState(plugin.UNKNOWN, "no DNS hostname specified")
return false
@ -176,7 +177,7 @@ func (program *checkProgram) getSerial(server string, response queryResponse) (o
// Run the monitoring check. This implies querying both servers, extracting the serial from
// their responses, then comparing the serials.
func (program *checkProgram) runCheck() {
func (program *checkProgram) RunCheck() {
checkResponse, refResponse := program.queryServers()
cOk, cSerial := program.getSerial("checked", checkResponse)
rOk, rSerial := program.getSerial("reference", refResponse)
@ -190,11 +191,3 @@ func (program *checkProgram) runCheck() {
program.plugin.SetState(plugin.CRITICAL, "serials mismatch")
}
}
func main() {
program := newProgram()
defer program.close()
if program.checkFlags() {
program.runCheck()
}
}