From 46f4590d11abf1beb7028b06650c8e2f53eb0a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Fri, 20 Jan 2023 15:03:46 +0100 Subject: [PATCH] config: Add alert-mode --- README.md | 3 +++ config.go | 28 ++++++++++++++++++++++++++++ config_test.go | 5 ++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3fb7f77..50fb575 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,9 @@ Example: http-address :8080 # Log level (either debug, info, warning, error) log-level info +# When multiple alerts are grouped together by Alertmanager, they can either be sent +# each on their own (single mode) or be kept together (multi mode) (either single or multi; default is single) +alert-mode single # Optionally protect with HTTP basic authentication user webhookUser password webhookPass diff --git a/config.go b/config.go index 3a8883d..7fb0653 100644 --- a/config.go +++ b/config.go @@ -7,9 +7,17 @@ import ( "git.sr.ht/~emersion/go-scfg" ) +type alertMode int + +const ( + single alertMode = iota + multi +) + type config struct { HTTPAddress string LogLevel string + alertMode alertMode User string Password string ntfy ntfyConfig @@ -42,6 +50,7 @@ func readConfig(path string) (*config, error) { // Set default values config.HTTPAddress = "127.0.0.1:8080" config.LogLevel = "info" + config.alertMode = single d := cfg.Get("log-level") if d != nil { @@ -57,6 +66,25 @@ func readConfig(path string) (*config, error) { } } + d = cfg.Get("alert-mode") + if d != nil { + var mode string + if err := d.ParseParams(&mode); err != nil { + return nil, err + } + + switch strings.ToLower(mode) { + case "single": + config.alertMode = single + + case "multi": + config.alertMode = multi + + default: + return nil, fmt.Errorf("%q directive: illegal mode %q", d.Name, mode) + } + } + d = cfg.Get("user") if d != nil { if err := d.ParseParams(&config.User); err != nil { diff --git a/config_test.go b/config_test.go index 7d744af..8e3b4e8 100644 --- a/config_test.go +++ b/config_test.go @@ -13,6 +13,9 @@ func TestReadConfig(t *testing.T) { http-address :8080 # Log level (either debug, info, warning, error) log-level info +# When multiple alerts are grouped together by Alertmanager, they can either be sent +# each on their own (single mode) or be kept together (multi mode) (either single or multi; default is single) +alert-mode multi # Optionally protect with HTTP basic authentication user webhookUser password webhookPass @@ -44,7 +47,7 @@ ntfy { ` expectedCfg := &config{ - HTTPAddress: ":8080", LogLevel: "info", User: "webhookUser", Password: "webhookPass", + 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{ -- 2.44.0