diff options
| author | Paul Buetow <paul@buetow.org> | 2025-11-22 09:32:52 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-11-22 09:32:52 +0200 |
| commit | 5fa8012c6b085c8d4244d3b4f9c99c1937fb65a2 (patch) | |
| tree | 6d166352229d903aeb52ca06c66cdc26f54147b9 | |
| parent | 1cde39e35c0329b4783f188b050b78b520e43470 (diff) | |
more on federation
| -rw-r--r-- | internal/check.go | 22 | ||||
| -rw-r--r-- | internal/federated.go | 13 | ||||
| -rw-r--r-- | internal/runchecks.go | 12 | ||||
| -rw-r--r-- | internal/state.go | 24 |
4 files changed, 44 insertions, 27 deletions
diff --git a/internal/check.go b/internal/check.go index 3f2e4cc..a171c29 100644 --- a/internal/check.go +++ b/internal/check.go @@ -24,13 +24,17 @@ type namedCheck struct { } type checkResult struct { - name string - output string - epoch int64 - status nagiosCode - federated bool + name string + output string + epoch int64 + status nagiosCode + federatedFrom string } +// func (c checkResult) federated() bool { +// return c.federatedFrom != "" +// } + func (c check) run(ctx context.Context, name string) checkResult { cmd := exec.CommandContext(ctx, c.Plugin, c.Args...) @@ -40,7 +44,7 @@ func (c check) run(ctx context.Context, name string) checkResult { if err := cmd.Run(); err != nil { if ctx.Err() == context.DeadlineExceeded { - return checkResult{name, "Check command timed out", time.Now().Unix(), nagiosCritical, false} + return checkResult{name, "Check command timed out", time.Now().Unix(), nagiosCritical, ""} } } @@ -54,11 +58,11 @@ func (c check) run(ctx context.Context, name string) checkResult { ec = int(nagiosUnknown) } - return checkResult{name, output, time.Now().Unix(), nagiosCode(ec), false} + return checkResult{name, output, time.Now().Unix(), nagiosCode(ec), ""} } func (c check) skip(name, output string) checkResult { - return checkResult{name, output, time.Now().Unix(), nagiosUnknown, false} + return checkResult{name, output, time.Now().Unix(), nagiosUnknown, ""} } func (c namedCheck) run(ctx context.Context) checkResult { @@ -67,4 +71,4 @@ func (c namedCheck) run(ctx context.Context) checkResult { func (c namedCheck) skip(output string) checkResult { return c.check.skip(c.name, output) -}
\ No newline at end of file +} diff --git a/internal/federated.go b/internal/federated.go index 9bc900b..5e98ebb 100644 --- a/internal/federated.go +++ b/internal/federated.go @@ -20,9 +20,16 @@ func mergeFederated(ctx context.Context, state state, conf config) state { 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, + name: fmt.Sprintf("Federated endpoint %s", endpoint), + epoch: time.Now().Unix(), + federatedFrom: func() string { + // Extract the hostname from the endpoint + u, err := http.NewRequest("GET", endpoint, nil) + if err != nil { + return endpoint + } + return u.URL.Hostname() + }(), } req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) diff --git a/internal/runchecks.go b/internal/runchecks.go index fb7a9c4..e3e802f 100644 --- a/internal/runchecks.go +++ b/internal/runchecks.go @@ -43,11 +43,11 @@ func runChecks(ctx context.Context, state state, conf config) state { log.Printf("Skipping %s: interval not yet reached (%v (%v) <= %v)", check.name, int(age.Seconds()), age, check.RunInterval) outputCh <- checkResult{ - name: check.name, - output: lastCheckState.output, - epoch: lastCheckState.Epoch, - status: lastCheckState.Status, - federated: lastCheckState.federated, + name: check.name, + output: lastCheckState.output, + epoch: lastCheckState.Epoch, + status: lastCheckState.Status, + federatedFrom: lastCheckState.federatedFrom, } inputWg.Done() continue @@ -110,4 +110,4 @@ func runCheck(ctx context.Context, limitCh chan struct{}, deps dependency, <-limitCh return checkResult -}
\ No newline at end of file +} diff --git a/internal/state.go b/internal/state.go index dceb108..d1225f0 100644 --- a/internal/state.go +++ b/internal/state.go @@ -12,11 +12,15 @@ import ( ) type checkState struct { - Status nagiosCode - PrevStatus nagiosCode - Epoch int64 `json:"Epoch,omitempty"` - output string - federated bool + Status nagiosCode + PrevStatus nagiosCode + Epoch int64 `json:"Epoch,omitempty"` + output string + federatedFrom string +} + +func (cs checkState) federated() bool { + return cs.federatedFrom != "" } func (cs checkState) changed() bool { @@ -78,7 +82,7 @@ func (s state) update(result checkResult) { prevStatus = prevState.Status } - cs := checkState{result.status, prevStatus, result.epoch, result.output, result.federated} + cs := checkState{result.status, prevStatus, result.epoch, result.output, result.federatedFrom} s.checks[result.name] = cs log.Println(result.name, cs) } @@ -237,8 +241,10 @@ 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 cs.federated() { + sb.WriteString(" [federated from ") + sb.WriteString(cs.federatedFrom) + sb.WriteString("]") } if isStaleReport { @@ -262,4 +268,4 @@ func (s state) countBy(filter func(cs checkState) bool) (count int) { } } return -}
\ No newline at end of file +} |
