ntfy-alertmanager/config.go
Thorben Günther 9043ccfb5e
Set ntfy priority depending on Prometheus labels
It is possible to configure a priority for an arbitrary label.
Furthermore a priority/order of those arbitrary labels should be
defined. The Alertmanager payload is checked for these and the
priority will be selected from the highest valued label that has one
set in the configuration file.
2022-10-12 16:35:04 +02:00

139 lines
2.5 KiB
Go

package main
import (
"fmt"
"strings"
"git.sr.ht/~emersion/go-scfg"
)
type config struct {
HTTPAddress string
LogLevel string
User string
Password string
ntfy ntfyConfig
labels labels
}
type ntfyConfig struct {
Topic string
User string
Password string
}
type labels struct {
Order []string
Label map[string]labelConfig
}
type labelConfig struct {
Priority string
}
func readConfig(path string) (*config, error) {
cfg, err := scfg.Load(path)
if err != nil {
return nil, err
}
config := new(config)
// Set default values
config.HTTPAddress = "127.0.0.1:8080"
config.LogLevel = "info"
d := cfg.Get("log-level")
if d != nil {
if err := d.ParseParams(&config.LogLevel); err != nil {
return nil, err
}
}
d = cfg.Get("http-address")
if d != nil {
if err := d.ParseParams(&config.HTTPAddress); err != nil {
return nil, err
}
}
d = cfg.Get("user")
if d != nil {
if err := d.ParseParams(&config.User); err != nil {
return nil, err
}
}
d = cfg.Get("password")
if d != nil {
if err := d.ParseParams(&config.Password); err != nil {
return nil, err
}
}
labelsDir := cfg.Get("labels")
if labelsDir != nil {
d = labelsDir.Children.Get("order")
if d != nil {
var order string
if err := d.ParseParams(&order); err != nil {
return nil, err
}
config.labels.Order = strings.Split(order, ",")
}
labels := make(map[string]labelConfig)
for _, labelName := range config.labels.Order {
labelDir := labelsDir.Children.Get(labelName)
if labelDir != nil {
labelConfig := new(labelConfig)
var name string
if err := labelDir.ParseParams(&name); err != nil {
return nil, err
}
d = labelDir.Children.Get("priority")
if d != nil {
if err := d.ParseParams(&labelConfig.Priority); err != nil {
return nil, err
}
}
labels[fmt.Sprintf("%s:%s", labelName, name)] = *labelConfig
}
}
config.labels.Label = labels
}
ntfyDir := cfg.Get("ntfy")
if ntfyDir == nil {
return nil, fmt.Errorf("%q directive missing", "ntfy")
}
d = ntfyDir.Children.Get("topic")
if d == nil {
return nil, fmt.Errorf("%q missing from %q directive", "topic", "ntfy")
}
if err := d.ParseParams(&config.ntfy.Topic); err != nil {
return nil, err
}
d = ntfyDir.Children.Get("user")
if d != nil {
if err := d.ParseParams(&config.ntfy.User); err != nil {
return nil, err
}
}
d = ntfyDir.Children.Get("password")
if d != nil {
if err := d.ParseParams(&config.ntfy.Password); err != nil {
return nil, err
}
}
return config, nil
}