feat: add unauthenticated web dashboard showing monitoring state and recent alerts
All checks were successful
check / check (push) Successful in 4s
All checks were successful
check / check (push) Successful in 4s
Add a read-only web dashboard at GET / that displays: - Summary counts for all monitored resources - Domain nameserver state with per-NS records and status - Hostname DNS records per authoritative nameserver - TCP port open/closed state with associated hostnames - TLS certificate details (CN, issuer, expiry, status) - Last 100 alerts in reverse chronological order Every data point shows relative age (e.g. '5m ago') for freshness. Page auto-refreshes every 30 seconds via meta refresh. Uses Tailwind CSS via CDN for a dark, technical aesthetic with saturated teals and blues on dark slate. Single page, no navigation. Implementation: - internal/notify/history.go: thread-safe ring buffer (last 100 alerts) - internal/notify/notify.go: record alerts in history before dispatch, refactor SendNotification into smaller dispatch helpers (funlen) - internal/handlers/dashboard.go: template rendering with embedded HTML, helper functions for relative time, record formatting, expiry days - internal/handlers/templates/dashboard.html: Tailwind-styled dashboard - internal/handlers/handlers.go: add State and Notify dependencies - internal/server/routes.go: register GET / dashboard route - README.md: document dashboard and new / endpoint No secrets (webhook URLs, API tokens, notification endpoints) are exposed in the dashboard. closes #82
This commit is contained in:
@@ -34,6 +34,7 @@ func NewTestService(transport http.RoundTripper) *Service {
|
||||
return &Service{
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
transport: transport,
|
||||
history: NewAlertHistory(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
62
internal/notify/history.go
Normal file
62
internal/notify/history.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// maxAlertHistory is the maximum number of alerts to retain.
|
||||
const maxAlertHistory = 100
|
||||
|
||||
// AlertEntry represents a single notification that was sent.
|
||||
type AlertEntry struct {
|
||||
Timestamp time.Time
|
||||
Title string
|
||||
Message string
|
||||
Priority string
|
||||
}
|
||||
|
||||
// AlertHistory is a thread-safe ring buffer that stores
|
||||
// the most recent alerts.
|
||||
type AlertHistory struct {
|
||||
mu sync.RWMutex
|
||||
entries [maxAlertHistory]AlertEntry
|
||||
count int
|
||||
index int
|
||||
}
|
||||
|
||||
// NewAlertHistory creates a new empty AlertHistory.
|
||||
func NewAlertHistory() *AlertHistory {
|
||||
return &AlertHistory{}
|
||||
}
|
||||
|
||||
// Add records a new alert entry in the ring buffer.
|
||||
func (h *AlertHistory) Add(entry AlertEntry) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
|
||||
h.entries[h.index] = entry
|
||||
h.index = (h.index + 1) % maxAlertHistory
|
||||
|
||||
if h.count < maxAlertHistory {
|
||||
h.count++
|
||||
}
|
||||
}
|
||||
|
||||
// Recent returns the stored alerts in reverse chronological
|
||||
// order (newest first). Returns at most maxAlertHistory entries.
|
||||
func (h *AlertHistory) Recent() []AlertEntry {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
|
||||
result := make([]AlertEntry, h.count)
|
||||
|
||||
for i := range h.count {
|
||||
// Walk backwards from the most recent entry.
|
||||
idx := (h.index - 1 - i + maxAlertHistory) %
|
||||
maxAlertHistory
|
||||
result[i] = h.entries[idx]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
88
internal/notify/history_test.go
Normal file
88
internal/notify/history_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package notify_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/dnswatcher/internal/notify"
|
||||
)
|
||||
|
||||
func TestAlertHistoryEmpty(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
h := notify.NewAlertHistory()
|
||||
|
||||
entries := h.Recent()
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("expected 0 entries, got %d", len(entries))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertHistoryAddAndRecent(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
h := notify.NewAlertHistory()
|
||||
|
||||
now := time.Now().UTC()
|
||||
|
||||
h.Add(notify.AlertEntry{
|
||||
Timestamp: now.Add(-2 * time.Minute),
|
||||
Title: "first",
|
||||
Message: "msg1",
|
||||
Priority: "info",
|
||||
})
|
||||
|
||||
h.Add(notify.AlertEntry{
|
||||
Timestamp: now.Add(-1 * time.Minute),
|
||||
Title: "second",
|
||||
Message: "msg2",
|
||||
Priority: "warning",
|
||||
})
|
||||
|
||||
entries := h.Recent()
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("expected 2 entries, got %d", len(entries))
|
||||
}
|
||||
|
||||
// Newest first.
|
||||
if entries[0].Title != "second" {
|
||||
t.Errorf(
|
||||
"expected newest first, got %q", entries[0].Title,
|
||||
)
|
||||
}
|
||||
|
||||
if entries[1].Title != "first" {
|
||||
t.Errorf(
|
||||
"expected oldest second, got %q", entries[1].Title,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertHistoryOverflow(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
h := notify.NewAlertHistory()
|
||||
|
||||
const totalEntries = 110
|
||||
|
||||
// Fill beyond capacity.
|
||||
for i := range totalEntries {
|
||||
h.Add(notify.AlertEntry{
|
||||
Timestamp: time.Now().UTC(),
|
||||
Title: "alert",
|
||||
Message: "msg",
|
||||
Priority: string(rune('0' + i%10)),
|
||||
})
|
||||
}
|
||||
|
||||
entries := h.Recent()
|
||||
|
||||
const maxHistory = 100
|
||||
|
||||
if len(entries) != maxHistory {
|
||||
t.Fatalf(
|
||||
"expected %d entries, got %d",
|
||||
maxHistory, len(entries),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -112,6 +112,7 @@ type Service struct {
|
||||
ntfyURL *url.URL
|
||||
slackWebhookURL *url.URL
|
||||
mattermostWebhookURL *url.URL
|
||||
history *AlertHistory
|
||||
}
|
||||
|
||||
// New creates a new notify Service.
|
||||
@@ -123,6 +124,7 @@ func New(
|
||||
log: params.Logger.Get(),
|
||||
transport: http.DefaultTransport,
|
||||
config: params.Config,
|
||||
history: NewAlertHistory(),
|
||||
}
|
||||
|
||||
if params.Config.NtfyTopic != "" {
|
||||
@@ -167,65 +169,99 @@ func New(
|
||||
return svc, nil
|
||||
}
|
||||
|
||||
// History returns the alert history for reading recent alerts.
|
||||
func (svc *Service) History() *AlertHistory {
|
||||
return svc.history
|
||||
}
|
||||
|
||||
// SendNotification sends a notification to all configured
|
||||
// endpoints.
|
||||
// endpoints and records it in the alert history.
|
||||
func (svc *Service) SendNotification(
|
||||
ctx context.Context,
|
||||
title, message, priority string,
|
||||
) {
|
||||
if svc.ntfyURL != nil {
|
||||
go func() {
|
||||
notifyCtx := context.WithoutCancel(ctx)
|
||||
svc.history.Add(AlertEntry{
|
||||
Timestamp: time.Now().UTC(),
|
||||
Title: title,
|
||||
Message: message,
|
||||
Priority: priority,
|
||||
})
|
||||
|
||||
err := svc.sendNtfy(
|
||||
notifyCtx,
|
||||
svc.ntfyURL,
|
||||
title, message, priority,
|
||||
)
|
||||
if err != nil {
|
||||
svc.log.Error(
|
||||
"failed to send ntfy notification",
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
}()
|
||||
svc.dispatchNtfy(ctx, title, message, priority)
|
||||
svc.dispatchSlack(ctx, title, message, priority)
|
||||
svc.dispatchMattermost(ctx, title, message, priority)
|
||||
}
|
||||
|
||||
func (svc *Service) dispatchNtfy(
|
||||
ctx context.Context,
|
||||
title, message, priority string,
|
||||
) {
|
||||
if svc.ntfyURL == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if svc.slackWebhookURL != nil {
|
||||
go func() {
|
||||
notifyCtx := context.WithoutCancel(ctx)
|
||||
go func() {
|
||||
notifyCtx := context.WithoutCancel(ctx)
|
||||
|
||||
err := svc.sendSlack(
|
||||
notifyCtx,
|
||||
svc.slackWebhookURL,
|
||||
title, message, priority,
|
||||
err := svc.sendNtfy(
|
||||
notifyCtx, svc.ntfyURL,
|
||||
title, message, priority,
|
||||
)
|
||||
if err != nil {
|
||||
svc.log.Error(
|
||||
"failed to send ntfy notification",
|
||||
"error", err,
|
||||
)
|
||||
if err != nil {
|
||||
svc.log.Error(
|
||||
"failed to send slack notification",
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (svc *Service) dispatchSlack(
|
||||
ctx context.Context,
|
||||
title, message, priority string,
|
||||
) {
|
||||
if svc.slackWebhookURL == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if svc.mattermostWebhookURL != nil {
|
||||
go func() {
|
||||
notifyCtx := context.WithoutCancel(ctx)
|
||||
go func() {
|
||||
notifyCtx := context.WithoutCancel(ctx)
|
||||
|
||||
err := svc.sendSlack(
|
||||
notifyCtx,
|
||||
svc.mattermostWebhookURL,
|
||||
title, message, priority,
|
||||
err := svc.sendSlack(
|
||||
notifyCtx, svc.slackWebhookURL,
|
||||
title, message, priority,
|
||||
)
|
||||
if err != nil {
|
||||
svc.log.Error(
|
||||
"failed to send slack notification",
|
||||
"error", err,
|
||||
)
|
||||
if err != nil {
|
||||
svc.log.Error(
|
||||
"failed to send mattermost notification",
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (svc *Service) dispatchMattermost(
|
||||
ctx context.Context,
|
||||
title, message, priority string,
|
||||
) {
|
||||
if svc.mattermostWebhookURL == nil {
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
notifyCtx := context.WithoutCancel(ctx)
|
||||
|
||||
err := svc.sendSlack(
|
||||
notifyCtx, svc.mattermostWebhookURL,
|
||||
title, message, priority,
|
||||
)
|
||||
if err != nil {
|
||||
svc.log.Error(
|
||||
"failed to send mattermost notification",
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (svc *Service) sendNtfy(
|
||||
|
||||
Reference in New Issue
Block a user