config: Add required settings for silencing alerts

This commit is contained in:
Thorben Günther 2023-02-10 17:16:13 +01:00
parent 7a6b7c9cdb
commit 34c0574163
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
3 changed files with 62 additions and 2 deletions

View file

@ -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 {

View file

@ -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
}

View file

@ -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")