RUD/internal/builder/presets.go

366 lines
11 KiB
Go

package builder
import (
"time"
"git.gsdistance.org/tyuXX/RUD/internal/config"
)
// ThemePreset defines a named visual theme for the status page.
type ThemePreset struct {
Name string
Description string
Theme config.ThemeConfig
}
// ThemePresets lists the built-in color/typography presets for page.theme.
func ThemePresets() []ThemePreset {
def := config.DefaultConfig().Page.Theme
return []ThemePreset{
{
Name: "Dark Minimal (Default)",
Description: "Charcoal dark monospace theme",
Theme: def,
},
{
Name: "Clean Light",
Description: "Crisp light background with high-readability ink",
Theme: config.ThemeConfig{
BgColor: "#f8fafc",
CardBgColor: "#ffffff",
TextColor: "#0f172a",
MutedTextColor: "#64748b",
BorderColor: "#e2e8f0",
AccentColor: "#2563eb",
UpColor: "#16a34a",
DegradedColor: "#d97706",
DownColor: "#dc2626",
PendingColor: "#94a3b8",
FontFamily: "ui-sans-serif, system-ui, -apple-system, sans-serif",
MaxWidth: "860px",
BorderRadius: "6px",
},
},
{
Name: "Terminal Green",
Description: "Classic phosphor green-on-black terminal style",
Theme: config.ThemeConfig{
BgColor: "#050a05",
CardBgColor: "#0a140a",
TextColor: "#7dff7d",
MutedTextColor: "#3b8a3b",
BorderColor: "#1b3b1b",
AccentColor: "#9aff9a",
UpColor: "#00ff66",
DegradedColor: "#ffd000",
DownColor: "#ff3b3b",
PendingColor: "#4a6b4a",
FontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
MaxWidth: "820px",
BorderRadius: "0px",
},
},
{
Name: "Nord",
Description: "Arctic bluish-slate palette",
Theme: config.ThemeConfig{
BgColor: "#2e3440",
CardBgColor: "#3b4252",
TextColor: "#eceff4",
MutedTextColor: "#d8dee9",
BorderColor: "#4c566a",
AccentColor: "#88c0d0",
UpColor: "#a3be8c",
DegradedColor: "#ebcb8b",
DownColor: "#bf616a",
PendingColor: "#81a1c1",
FontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
MaxWidth: "840px",
BorderRadius: "4px",
},
},
{
Name: "High Contrast",
Description: "Pure black/white high-contrast accessibility palette",
Theme: config.ThemeConfig{
BgColor: "#000000",
CardBgColor: "#0a0a0a",
TextColor: "#ffffff",
MutedTextColor: "#d4d4d4",
BorderColor: "#ffffff",
AccentColor: "#00ffff",
UpColor: "#00ff00",
DegradedColor: "#ffff00",
DownColor: "#ff3333",
PendingColor: "#a3a3a3",
FontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
MaxWidth: "860px",
BorderRadius: "2px",
},
},
}
}
// BlankMinimalConfig returns a clean minimal config ready for custom monitors.
func BlankMinimalConfig() config.Config {
def := config.DefaultConfig()
return config.Config{
Server: def.Server,
Storage: def.Storage,
Defaults: def.Defaults,
Monitors: []config.MonitorConfig{},
Events: []config.EventConfig{},
Page: config.PageConfig{
Title: "Service Status",
Theme: def.Page.Theme,
Elements: []config.PageElement{
{
Type: "header",
Title: "Service Status",
Subtitle: "System health & uptime",
Align: "split",
ShowLastUpdated: true,
ShowLiveIndicator: true,
},
{
Type: "status_banner",
Messages: map[string]string{
"up": "All systems operational",
"degraded": "Some systems experiencing degraded performance",
"down": "One or more systems are down",
"pending": "Checking system status...",
},
ShowOverallUptime: true,
ShowCounts: true,
},
{
Type: "monitor_group",
Title: "Services",
Monitors: []string{"*"},
Layout: "list",
Display: &config.MonitorDisplayConfig{
ShowBar: config.BoolPtr(true),
BarTicks: 40,
ShowUptime: config.BoolPtr(true),
ShowLatency: config.BoolPtr(true),
ShowStatusBadge: config.BoolPtr(true),
ShowMethod: config.BoolPtr(true),
ShowDescription: config.BoolPtr(true),
},
},
},
},
}
}
// WebAPIStarterConfig returns a starter configuration tailored for public web/API endpoints.
func WebAPIStarterConfig() config.Config {
cfg := BlankMinimalConfig()
cfg.Page.Title = "API & Web Status"
cfg.Page.Elements[0].Title = "API & Web Status"
cfg.Page.Elements[0].Subtitle = "Public website and API availability"
cfg.Monitors = []config.MonitorConfig{
{
ID: "main-web",
Name: "Main Website",
Description: "Primary HTTPS landing page",
Method: "https",
URL: "https://example.com/",
HTTPMethod: "GET",
ExpectedStatus: []int{200},
},
{
ID: "api-health",
Name: "Core API Health",
Description: "JSON health endpoint",
Method: "https",
URL: "https://api.example.com/health",
HTTPMethod: "GET",
ExpectedStatus: []int{200},
BodyRegex: `"ok":\s*true`,
},
{
ID: "domain-dns",
Name: "Domain DNS (A)",
Method: "dns",
Host: "example.com",
RecordType: "A",
},
}
cfg.Events = []config.EventConfig{
{
ID: "web-outage",
Name: "Website or API Outage",
Cooldown: config.NewDuration(15 * time.Minute),
TriggerOnRecovery: true,
Condition: config.EventCondition{
Type: "state",
Monitors: []string{"main-web", "api-health"},
Match: "any",
State: "down",
For: config.NewDuration(1 * time.Minute),
},
Actions: []config.EventAction{
{
Type: "command",
Command: `echo "[RUD ALERT] {{.EventName}}: {{.Status}} ({{.TriggeredMonitors}})"`,
Shell: true,
Timeout: config.NewDuration(5 * time.Second),
},
},
},
}
cfg.Page.Elements = []config.PageElement{
cfg.Page.Elements[0],
cfg.Page.Elements[1],
{
Type: "summary_cards",
Metrics: []string{"total", "up", "degraded", "down", "overall_uptime", "avg_latency"},
},
{
Type: "monitor_group",
Title: "Web & APIs",
Monitors: []string{"main-web", "api-health"},
Layout: "list",
Display: cfg.Page.Elements[2].Display,
},
{
Type: "monitor_group",
Title: "DNS",
Monitors: []string{"domain-dns"},
Layout: "list",
Display: cfg.Page.Elements[2].Display,
},
}
return cfg
}
// HomelabStarterConfig returns a starter configuration tailored for homelab / self-hosted services.
func HomelabStarterConfig() config.Config {
cfg := BlankMinimalConfig()
cfg.Page.Title = "Homelab Status"
cfg.Page.Elements[0].Title = "Homelab Status"
cfg.Page.Elements[0].Subtitle = "Internal nodes, databases & services"
cfg.Monitors = []config.MonitorConfig{
{
ID: "gateway-ping",
Name: "Router / Gateway",
Description: "Local network gateway",
Method: "ping",
Host: "127.0.0.1",
Count: 1,
},
{
ID: "db-tcp",
Name: "Database TCP",
Description: "Database port handshake",
Method: "tcp",
Target: "127.0.0.1:5432",
},
{
ID: "reverse-proxy",
Name: "Reverse Proxy",
Description: "Local HTTP ingress",
Method: "http",
URL: "http://127.0.0.1:80/",
},
{
ID: "root-disk",
Name: "Root Disk Free",
Description: "Fails if root filesystem exceeds 90% usage",
Method: "cmd",
Command: "df -P / | awk 'NR==2 {print $5}'",
Shell: true,
ExpectedExitCode: config.IntPtr(0),
OutputRegex: `^([0-8]?[0-9])%$`,
},
}
cfg.Events = []config.EventConfig{
{
ID: "node-down",
Name: "Homelab Service Down",
Cooldown: config.NewDuration(30 * time.Minute),
TriggerOnRecovery: true,
Condition: config.EventCondition{
Type: "state",
Monitors: []string{"*"},
Match: "any",
State: "down",
For: config.NewDuration(2 * time.Minute),
},
Actions: []config.EventAction{
{
Type: "command",
Command: `echo "[HOMELAB] {{.EventName}}: {{.Status}} monitors={{.TriggeredMonitors}}"`,
Shell: true,
Timeout: config.NewDuration(5 * time.Second),
},
},
},
}
return cfg
}
// WebhookPreset describes a pre-configured webhook payload template.
type WebhookPreset struct {
Name string
Description string
URLHint string
HTTPMethod string
Headers map[string]string
Template string
}
// WebhookPresets lists ready-to-use templates for common alerting targets.
func WebhookPresets() []WebhookPreset {
return []WebhookPreset{
{
Name: "Discord Webhook",
Description: "Posts a formatted message to a Discord channel webhook",
URLHint: "https://discord.com/api/webhooks/<id>/<token>",
HTTPMethod: "POST",
Template: `{"content":"**[{{jsonStr .Status}}] {{jsonStr .EventName}}** — State: ` + "`{{jsonStr .OverallState}}`" + ` | Monitors: {{jsonStr .TriggeredMonitors}} | Uptime: {{.OverallUptimePct}}%"}`,
},
{
Name: "Slack Incoming Webhook",
Description: "Posts a clean text alert to a Slack webhook",
URLHint: "https://hooks.slack.com/services/T.../B.../...",
HTTPMethod: "POST",
Template: `{"text":"*[{{jsonStr .Status}}] {{jsonStr .EventName}}*\nState: {{jsonStr .OverallState}} | Monitors: {{jsonStr .TriggeredMonitors}} | Time: {{jsonStr .Timestamp}}"}`,
},
{
Name: "ntfy (ntfy.sh or self-hosted)",
Description: "Push notification via ntfy HTTP POST",
URLHint: "https://ntfy.sh/<your-topic>",
HTTPMethod: "POST",
Headers: map[string]string{
"Content-Type": "text/plain; charset=utf-8",
"Title": "RUD Alert",
"Priority": "high",
},
Template: `[{{.Status}}] {{.EventName}} — State: {{.OverallState}} | Monitors: {{.TriggeredMonitors}} | Uptime: {{.OverallUptimePct}}%`,
},
{
Name: "Gotify",
Description: "Push notification to a Gotify server (/message?token=...)",
URLHint: "https://gotify.example.com/message?token=<app-token>",
HTTPMethod: "POST",
Template: `{"title":"[{{jsonStr .Status}}] {{jsonStr .EventName}}","message":"Overall: {{jsonStr .OverallState}}\nMonitors: {{jsonStr .TriggeredMonitors}}\nTime: {{jsonStr .Timestamp}}","priority":5}`,
},
{
Name: "Generic JSON Payload",
Description: "Sends RUD's full native EventPayload JSON (empty body_template)",
URLHint: "https://hooks.example.com/rud",
HTTPMethod: "POST",
Template: "",
},
}
}
const templateCheatsheet = `Template variables:
{{.EventID}}, {{.EventName}}, {{.Status}} ("TRIGGERED"/"RESOLVED"),
{{.OverallState}}, {{.OverallUptimePct}}, {{.TriggeredMonitors}},
{{.MonitorsJSON}}, {{.Timestamp}}, {{json .Field}}, {{jsonStr .Field}}
Command env vars:
RUD_EVENT_ID, RUD_EVENT_NAME, RUD_EVENT_STATUS,
RUD_OVERALL_STATE, RUD_TRIGGERED_MONITORS, RUD_TIMESTAMP`