Skip to content

Commit

Permalink
Add /dump-schedules debug endpoint
Browse files Browse the repository at this point in the history
This allows to quickly dump all schedules and how they expand to contacts for
the next two days for debugging/testing purposes.
  • Loading branch information
julianbrost committed Jun 12, 2024
1 parent 8c713e1 commit 2b3ca42
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions internal/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}

0 comments on commit 2b3ca42

Please sign in to comment.