diff options
| author | Paul Buetow <paul@buetow.org> | 2025-06-01 15:12:32 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-06-01 15:12:32 +0300 |
| commit | 49fe110a6263c64d34c95024fd1ffa7352c63006 (patch) | |
| tree | 1d827ee5d0d71e07a7fe3e8f9325936fa1d0a5cd | |
| parent | 9fe227b79ea15ffcde8bf175ff879f991d183eab (diff) | |
more on federated
| -rw-r--r-- | internal/config.go | 3 | ||||
| -rw-r--r-- | internal/federated.go | 59 | ||||
| -rw-r--r-- | internal/notify.go | 4 | ||||
| -rw-r--r-- | internal/state.go | 7 |
4 files changed, 70 insertions, 3 deletions
diff --git a/internal/config.go b/internal/config.go index 222750b..740b19b 100644 --- a/internal/config.go +++ b/internal/config.go @@ -12,11 +12,12 @@ type config struct { EmailTo string EmailFrom string SMTPServer string `json:"SMTPServer,omitempty"` + SMTPDisable bool `json:"SMTPDisable,omitempty"` // TODO: Document this option StateDir string `json:"StateDir,omitempty"` CheckTimeoutS int CheckConcurrency int StaleThreshold int `json:"StaleThreshold,omitempty"` - Federated []string `json:"Federated,omitempty"` + Federated []string `json:"Federated,omitempty"` // TODO: Document this option Checks map[string]check } diff --git a/internal/federated.go b/internal/federated.go new file mode 100644 index 0000000..9bc900b --- /dev/null +++ b/internal/federated.go @@ -0,0 +1,59 @@ +package internal + +import ( + "context" + "fmt" + "io" + "log" + "net/http" + "time" +) + +// Query all federated endpoints and merge states +func mergeFederated(ctx context.Context, state state, conf config) state { + critical := func(cs checkResult, err error) { + cs.output = err.Error() + cs.status = nagiosCritical + state.update(cs) + } + + for _, endpoint := range conf.Federated { + log.Println("Querying federated endpoint", endpoint) + cs := checkResult{ + name: fmt.Sprintf("Federated endpoint %s", endpoint), + epoch: time.Now().Unix(), + federated: true, + } + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) + if err != nil { + critical(cs, err) + continue + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + critical(cs, err) + continue + } + + bytes, err := io.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + critical(cs, err) + continue + } + + if err := state.mergeFromBytes(bytes); err != nil { + critical(cs, err) + continue + } + + cs.output = fmt.Sprintf("OK: Federated endpoint returned %d bytes", len(bytes)) + cs.status = nagiosOk + state.update(cs) + } + + log.Println(state) + return state +} diff --git a/internal/notify.go b/internal/notify.go index a89035b..3a294b2 100644 --- a/internal/notify.go +++ b/internal/notify.go @@ -7,6 +7,10 @@ import ( ) func notify(conf config, subject, body string) error { + if conf.SMTPDisable { + log.Println("Notification disabled") + return nil + } log.Println("notify", subject, body) headers := map[string]string{ diff --git a/internal/state.go b/internal/state.go index 9580db6..8de7f15 100644 --- a/internal/state.go +++ b/internal/state.go @@ -16,6 +16,7 @@ type checkState struct { PrevStatus nagiosCode Epoch int64 `json:"Epoch,omitempty"` output string + federated bool } func (cs checkState) changed() bool { @@ -70,7 +71,6 @@ func newState(conf config) (state, error) { return s, nil } - func (s state) update(result checkResult) { prevStatus := nagiosUnknown prevState, ok := s.checks[result.name] @@ -78,7 +78,7 @@ func (s state) update(result checkResult) { prevStatus = prevState.Status } - cs := checkState{result.status, prevStatus, result.epoch, result.output} + cs := checkState{result.status, prevStatus, result.epoch, result.output, result.federated} s.checks[result.name] = cs log.Println(result.name, cs) } @@ -229,6 +229,9 @@ func (s state) reportBy(sb *strings.Builder, showStatusChange, isStaleReport boo sb.WriteString(name) sb.WriteString(": ") sb.WriteString(cs.output) + if cs.federated { + sb.WriteString(" [federated]") + } if isStaleReport { lastCheckedAgo := time.Since(time.Unix(cs.Epoch, 0)) |
