Organizations create receiver URLs for Alertmanager's webhook (token in the path or as bearer). A monitor of type alertmanager is down while an alert matching all its label matchers fires, and goes through the usual evaluation, so confirmations, maintenance, notifications and SLA apply. Results are recorded when the state changes and once per interval; alerts Alertmanager stops repeating end after the monitor's stale period, so a lost resolve cannot hold it down. An overview lists firing alerts with the monitors following them and creates a prefilled monitor for the others. Closes #91 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
133 lines
4.0 KiB
Go
133 lines
4.0 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"slices"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/datapolicy"
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
|
||
)
|
||
|
||
func (s *Server) retention() datapolicy.Retention {
|
||
r := s.cfg.Retention
|
||
return datapolicy.Retention{Raw: r.Raw, Hourly: r.Hourly, Daily: r.Daily}
|
||
}
|
||
|
||
// RunHousekeeping aligns TimescaleDB's retention with the configuration and
|
||
// then purges rows TimescaleDB does not manage, hourly, until ctx ends.
|
||
// Every replica runs it; the deletes are idempotent.
|
||
func (s *Server) RunHousekeeping(ctx context.Context) {
|
||
if err := datapolicy.Apply(ctx, s.db.Pool(), s.retention(), s.log); err != nil {
|
||
// The API role may not own the tables; "api migrate up" applies the
|
||
// policies as owner.
|
||
s.log.WarnContext(ctx, "applying retention policies failed", "error", err)
|
||
}
|
||
tick := time.NewTicker(time.Hour)
|
||
defer tick.Stop()
|
||
for {
|
||
if err := s.Housekeep(ctx, time.Now()); err != nil && ctx.Err() == nil {
|
||
s.log.ErrorContext(ctx, "housekeeping failed", "error", err)
|
||
}
|
||
select {
|
||
case <-ctx.Done():
|
||
return
|
||
case <-tick.C:
|
||
}
|
||
}
|
||
}
|
||
|
||
// Housekeep deletes delivered notifications, stale invitations, expired API
|
||
// tokens and incidents older than the daily aggregate.
|
||
func (s *Server) Housekeep(ctx context.Context, now time.Time) error {
|
||
keep := s.cfg.Retention
|
||
var counts [6]int64
|
||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||
var err error
|
||
if keep.Housekeeping > 0 {
|
||
before := now.Add(-keep.Housekeeping)
|
||
if counts[0], err = q.PurgeNotifications(ctx, before); err != nil {
|
||
return err
|
||
}
|
||
if counts[1], err = q.PurgeInvitations(ctx, before); err != nil {
|
||
return err
|
||
}
|
||
if counts[2], err = q.PurgeExpiredTokens(ctx, &before); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if keep.Evidence > 0 {
|
||
if counts[5], err = q.PurgeEvidence(ctx, now.Add(-keep.Evidence)); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if keep.Audit > 0 {
|
||
if counts[4], err = q.PurgeAuditEvents(ctx, now.Add(-keep.Audit)); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if _, err = q.PurgeAlertmanagerAlerts(ctx, now.Add(-keepAlerts)); err != nil {
|
||
return err
|
||
}
|
||
if keep.Daily > 0 {
|
||
before := now.Add(-keep.Daily)
|
||
counts[3], err = q.PurgeIncidents(ctx, &before)
|
||
}
|
||
return err
|
||
})
|
||
if err == nil && counts != [6]int64{} {
|
||
s.log.InfoContext(ctx, "housekeeping purged rows",
|
||
"notifications", counts[0], "invitations", counts[1], "api_tokens", counts[2], "incidents", counts[3], "audit_events", counts[4], "evidence", counts[5])
|
||
}
|
||
return err
|
||
}
|
||
|
||
func storageView(c *gin.Context, st datapolicy.Stats) web.StorageView {
|
||
ctx := c.Request.Context()
|
||
var v web.StorageView
|
||
for _, r := range st.Relations {
|
||
l := web.StorageLevel{
|
||
Key: strings.TrimPrefix(strings.TrimPrefix(r.Name, "check_results"), "_"),
|
||
Size: web.Bytes(r.TotalBytes),
|
||
Chunks: fmt.Sprintf("%d / %d", r.CompressedChunks, r.Chunks),
|
||
Retention: web.Days(ctx, r.Retention),
|
||
CompressAfter: "–",
|
||
}
|
||
if l.Key == "" {
|
||
l.Key = "raw"
|
||
}
|
||
if r.CompressAfter > 0 {
|
||
l.CompressAfter = web.Days(ctx, r.CompressAfter)
|
||
}
|
||
if r.AfterBytes > 0 {
|
||
l.Compressed = fmt.Sprintf("%s → %s", web.Bytes(r.BeforeBytes), web.Bytes(r.AfterBytes))
|
||
}
|
||
v.Levels = append(v.Levels, l)
|
||
}
|
||
const layout = "2006-01-02 15:04"
|
||
for _, j := range st.Jobs {
|
||
sj := web.StorageJob{
|
||
Name: strings.TrimPrefix(j.Proc, "policy_"), Relation: j.Relation,
|
||
OK: j.LastStatus != "Failed", Failures: j.Failures, LastRun: "–", NextRun: "–",
|
||
}
|
||
if j.LastRun != nil {
|
||
sj.LastRun = j.LastRun.Local().Format(layout)
|
||
}
|
||
if j.NextRun != nil {
|
||
sj.NextRun = j.NextRun.Local().Format(layout)
|
||
}
|
||
v.Jobs = append(v.Jobs, sj)
|
||
}
|
||
for name, size := range st.TableBytes {
|
||
v.Tables = append(v.Tables, web.StorageTable{Name: name, Size: web.Bytes(size)})
|
||
}
|
||
slices.SortFunc(v.Tables, func(a, b web.StorageTable) int { return strings.Compare(a.Name, b.Name) })
|
||
return v
|
||
}
|