aboutsummaryrefslogtreecommitdiff
path: root/cmd/moncheck
diff options
context:
space:
mode:
authorGibheer <gibheer+git@zero-knowledge.org>2023-09-05 15:07:38 +0200
committerGibheer <gibheer+git@zero-knowledge.org>2023-09-05 15:07:38 +0200
commit26a4ca6ab56d23054f606bc378ba355cebce485a (patch)
treebc5568b6e520eb262a61374fd1da83c12baff208 /cmd/moncheck
parent8e6e01f47c320f862b9ba44815587006ba80cf77 (diff)
migrate moncheck to slog
With this moncheck itself is also changed to use slog. The early config parsing is still using log as we do not have any idea what else to use. But from then slog is used at all points. With the additional config options a man page was also added to explain the new config options.
Diffstat (limited to 'cmd/moncheck')
-rw-r--r--cmd/moncheck/main.go77
1 files changed, 68 insertions, 9 deletions
diff --git a/cmd/moncheck/main.go b/cmd/moncheck/main.go
index 73eee1a..12695d4 100644
--- a/cmd/moncheck/main.go
+++ b/cmd/moncheck/main.go
@@ -7,8 +7,10 @@ import (
"encoding/json"
"flag"
"fmt"
+ "io"
"io/ioutil"
"log"
+ "log/slog"
"os"
"strconv"
"strings"
@@ -30,6 +32,12 @@ type (
Path []string `json:"path"`
Workers int `json:"workers"`
CheckerID int `json:"checker_id"`
+
+ Log struct {
+ Format string `json:"format"`
+ Level string `json:"level"`
+ Output string `json:"output"`
+ } `json:"log"`
}
States []int
@@ -47,27 +55,34 @@ func main() {
log.Fatalf("could not parse config: %s", err)
}
+ logger := parseLogger(config)
+
if err := os.Setenv("PATH", strings.Join(config.Path, ":")); err != nil {
- log.Fatalf("could not set PATH: %s", err)
+ logger.Error("could not set PATH", "error", err, "configured path", config.Path)
+ os.Exit(1)
}
waitDuration, err := time.ParseDuration(config.Wait)
if err != nil {
- log.Fatalf("could not parse wait duration: %s", err)
+ logger.Error("could not parse wait duration", "error", err, "wait duration", config.Wait)
+ os.Exit(1)
}
timeout, err := time.ParseDuration(config.Timeout)
if err != nil {
- log.Fatalf("could not parse timeout: %s", err)
+ logger.Error("could not parse timeout", "error", err, "timeout", config.Timeout)
+ os.Exit(1)
}
db, err := sql.Open("postgres", config.DB)
if err != nil {
- log.Fatalf("could not open database connection: %s", err)
+ logger.Error("could not open database connection", "error", err)
+ os.Exit(1)
}
hostname, err := os.Hostname()
if err != nil {
- log.Fatalf("could not resolve hostname: %s", err)
+ logger.Error("could not resolve hostname", "error", err)
+ os.Exit(1)
}
checker, err := monzero.NewChecker(monzero.CheckerConfig{
@@ -78,22 +93,23 @@ func main() {
Executor: monzero.CheckExec,
})
if err != nil {
- log.Fatalf("could not create checker instance: %s", err)
+ logger.Error("could not create checker instance", "error", err)
+ os.Exit(1)
}
for i := 0; i < config.Workers; i++ {
- go check(checker, waitDuration)
+ go check(checker, waitDuration, logger)
}
wg := sync.WaitGroup{}
wg.Add(1)
wg.Wait()
}
-func check(checker *monzero.Checker, waitDuration time.Duration) {
+func check(checker *monzero.Checker, waitDuration time.Duration, logger *slog.Logger) {
for {
if err := checker.Next(); err != nil {
if err != monzero.ErrNoCheck {
- log.Printf("could not run check: %s", err)
+ logger.Info("check returned error", "error", err)
}
time.Sleep(waitDuration)
}
@@ -171,3 +187,46 @@ func (s *States) ToOK() bool {
}
return false
}
+
+// parse the log settings and generate the slog output
+func parseLogger(config Config) *slog.Logger {
+ var output io.Writer
+ switch config.Log.Output {
+ case "", "stderr":
+ output = os.Stderr
+ case "stdout":
+ output = os.Stdout
+ default:
+ var err error
+ output, err = os.OpenFile(config.Log.Output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0640)
+ if err != nil {
+ log.Fatalf("could not open log file handler: %s", err)
+ }
+ }
+
+ var level slog.Level
+ switch config.Log.Level {
+ case "debug":
+ level = slog.LevelDebug
+ case "", "info":
+ level = slog.LevelInfo
+ case "warn":
+ level = slog.LevelWarn
+ case "error":
+ level = slog.LevelError
+ default:
+ log.Fatalf("unknown log level '%s', only 'debug', 'info', 'warn' and 'error' are supported", config.Log.Level)
+ }
+
+ var handler slog.Handler
+ switch config.Log.Format {
+ case "", "text":
+ handler = slog.NewTextHandler(output, &slog.HandlerOptions{Level: level})
+ case "json":
+ handler = slog.NewJSONHandler(output, &slog.HandlerOptions{Level: level})
+ default:
+ log.Fatalf("unknown log format '%s', only 'text' and 'json' are supported", config.Log.Format)
+ }
+
+ return slog.New(handler)
+}