config: Add alert-mode

This commit is contained in:
Thorben Günther 2023-01-20 15:03:46 +01:00
parent 78801c98e4
commit 46f4590d11
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
3 changed files with 35 additions and 1 deletions

View file

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

View file

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

View file

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