From 34c057416336c4105d3e2633e1bbd4f1cbebc9ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Fri, 10 Feb 2023 17:16:13 +0100 Subject: [PATCH] config: Add required settings for silencing alerts --- README.md | 11 +++++++++++ config.go | 32 ++++++++++++++++++++++++++++++++ config_test.go | 21 +++++++++++++++++++-- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 78524ce..2caaee9 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ decreasing order of labels in the config file and map those labels to tags or pr Example: ``` +# Public facing base URL of the service (e.g. https://ntfy-alertmanager.xenrox.net) +# This setting is required for the "Silence" feature. +base-url https://ntfy-alertmanager.xenrox.net # http listen address http-address :8080 # Log level (either debug, info, warning, error) @@ -61,6 +64,14 @@ ntfy { password pass } +alertmanager { + # If set, the ntfy message will contain a "Silence" button, which can be used + # to create a silence via the Alertmanager API. Because of limitations in ntfy, + # the request will be proxied through ntfy-alertmanager. Therefore ntfy-alertmanager + # needs to be exposed to external network requests and base-url has to be set. + silence-duration 24h +} + # When the alert-mode is set to single, ntfy-alertmanager will cache each single alert # to avoid sending recurrences. cache { diff --git a/config.go b/config.go index a6c27a2..e88e885 100644 --- a/config.go +++ b/config.go @@ -16,6 +16,7 @@ const ( ) type config struct { + BaseURL string HTTPAddress string LogLevel string alertMode alertMode @@ -24,6 +25,7 @@ type config struct { ntfy ntfyConfig labels labels cache cacheConfig + am alertmanagerConfig } type ntfyConfig struct { @@ -47,6 +49,10 @@ type cacheConfig struct { Duration time.Duration } +type alertmanagerConfig struct { + SilenceDuration time.Duration +} + func readConfig(path string) (*config, error) { cfg, err := scfg.Load(path) if err != nil { @@ -76,6 +82,13 @@ func readConfig(path string) (*config, error) { } } + d = cfg.Get("base-url") + if d != nil { + if err := d.ParseParams(&config.BaseURL); err != nil { + return nil, err + } + } + d = cfg.Get("alert-mode") if d != nil { var mode string @@ -216,5 +229,24 @@ func readConfig(path string) (*config, error) { } } + amDir := cfg.Get("alertmanager") + + if amDir != nil { + var durationString string + d = amDir.Children.Get("silence-duration") + if d != nil { + if err := d.ParseParams(&durationString); err != nil { + return nil, err + } + + duration, err := time.ParseDuration(durationString) + if err != nil { + return nil, err + } + + config.am.SilenceDuration = duration + } + } + return config, nil } diff --git a/config_test.go b/config_test.go index a64fc60..93c74b4 100644 --- a/config_test.go +++ b/config_test.go @@ -10,6 +10,9 @@ import ( func TestReadConfig(t *testing.T) { configContent := ` +# Public facing base URL of the service (e.g. https://ntfy-alertmanager.xenrox.net) +# This setting is required for the "Silence" feature. +base-url https://ntfy-alertmanager.xenrox.net # http listen address http-address :8080 # Log level (either debug, info, warning, error) @@ -46,6 +49,14 @@ ntfy { password pass } +alertmanager { + # If set, the ntfy message will contain a "Silence" button, which can be used + # to create a silence via the Alertmanager API. Because of limitations in ntfy, + # the request will be proxied through ntfy-alertmanager. Therefore ntfy-alertmanager + # needs to be exposed to external network requests and base-url has to be set. + silence-duration 24h +} + # When the alert-mode is set to single, ntfy-alertmanager will cache each single alert # to avoid sending recurrences. cache { @@ -57,8 +68,13 @@ cache { ` expectedCfg := &config{ - HTTPAddress: ":8080", LogLevel: "info", alertMode: multi, User: "webhookUser", Password: "webhookPass", - ntfy: ntfyConfig{Topic: "https://ntfy.sh/alertmanager-alerts", User: "user", Password: "pass"}, + BaseURL: "https://ntfy-alertmanager.xenrox.net", + HTTPAddress: ":8080", + LogLevel: "info", + alertMode: multi, + User: "webhookUser", + Password: "webhookPass", + ntfy: ntfyConfig{Topic: "https://ntfy.sh/alertmanager-alerts", User: "user", Password: "pass"}, labels: labels{Order: []string{"severity", "instance"}, Label: map[string]labelConfig{ "severity:critical": {Priority: "5", Tags: []string{"rotating_light"}}, @@ -67,6 +83,7 @@ cache { }, }, cache: cacheConfig{CleanupInterval: time.Hour, Duration: 48 * time.Hour}, + am: alertmanagerConfig{SilenceDuration: time.Hour * 24}, } configPath := filepath.Join(t.TempDir(), "config") -- 2.44.0