add debug log support for the checker

This makes it possible to use this library and get a debug checker
output.
This commit is contained in:
Gibheer 2023-09-05 15:23:43 +02:00
parent 26a4ca6ab5
commit 71cd89a4f1
1 changed files with 13 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"log/slog"
"time"
)
@ -19,6 +20,7 @@ type (
executor func(Check, context.Context) CheckResult
timeout time.Duration
ident string // the host identifier
logger *slog.Logger
}
CheckerConfig struct {
@ -42,6 +44,9 @@ type (
// HostIdentifier is used in notifications to point to the source of the
// notification.
HostIdentifier string
// Checker will send debug details to the logger for each command executed.
Logger *slog.Logger
}
// Check is contains the metadata to run a check and its current state.
@ -75,6 +80,7 @@ func NewChecker(cfg CheckerConfig) (*Checker, error) {
executor: cfg.Executor,
timeout: cfg.Timeout,
ident: cfg.HostIdentifier,
logger: cfg.Logger,
}
if c.executor == nil {
return nil, fmt.Errorf("executor must not be nil")
@ -116,6 +122,13 @@ func (c *Checker) Next() error {
result.Message = fmt.Sprintf("check took longer than %s", c.timeout)
result.ExitCode = 2
}
c.logger.Debug(
"check command run",
"id", check.id,
"command", check.Command,
"exit code", result.ExitCode,
"message", result.Message,
)
backToOkay := false
if len(check.ExitCodes) == 0 && result.ExitCode == 0 {