From 2b3ca42119f42a0590df002f10d6b84e334ccc1c Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Wed, 5 Jun 2024 11:40:31 +0200 Subject: [PATCH] Add /dump-schedules debug endpoint This allows to quickly dump all schedules and how they expand to contacts for the next two days for debugging/testing purposes. --- internal/listener/listener.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/internal/listener/listener.go b/internal/listener/listener.go index 4a3bc67b..566d9049 100644 --- a/internal/listener/listener.go +++ b/internal/listener/listener.go @@ -36,6 +36,7 @@ func NewListener(db *database.DB, runtimeConfig *config.RuntimeConfig, logs *log l.mux.HandleFunc("/process-event", l.ProcessEvent) l.mux.HandleFunc("/dump-config", l.DumpConfig) l.mux.HandleFunc("/dump-incidents", l.DumpIncidents) + l.mux.HandleFunc("/dump-schedules", l.DumpSchedules) return l } @@ -220,3 +221,32 @@ func (l *Listener) DumpIncidents(w http.ResponseWriter, r *http.Request) { enc.SetIndent("", " ") _ = enc.Encode(encodedIncidents) } + +func (l *Listener) DumpSchedules(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + w.WriteHeader(http.StatusMethodNotAllowed) + _, _ = fmt.Fprintln(w, "GET required") + return + } + + if !l.checkDebugPassword(w, r) { + return + } + + l.runtimeConfig.RLock() + defer l.runtimeConfig.RUnlock() + + for _, schedule := range l.runtimeConfig.Schedules { + fmt.Fprintf(w, "[id=%d] %q:\n", schedule.ID, schedule.Name) + + // Iterate in 30 minute steps as this is the granularity Icinga Notifications Web allows in the configuration. + // Truncation to seconds happens only for a more readable output. + step := 30 * time.Minute + start := time.Now().Truncate(time.Second) + for t := start; t.Before(start.Add(48 * time.Hour)); t = t.Add(step) { + fmt.Fprintf(w, "\t%v: %v\n", t, schedule.GetContactsAt(t)) + } + + fmt.Fprintln(w) + } +}