~xenrox/ntfy-alertmanager

34c057416336c4105d3e2633e1bbd4f1cbebc9ce — Thorben Günther 1 year, 2 months ago 7a6b7c9
config: Add required settings for silencing alerts
3 files changed, 62 insertions(+), 2 deletions(-)

M README.md
M config.go
M config_test.go
M README.md => README.md +11 -0
@@ 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 {

M config.go => config.go +32 -0
@@ 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
}

M config_test.go => config_test.go +19 -2
@@ 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")