~xenrox/ntfy-alertmanager

8a387ff8344de19cb21e2b5d0059d458a54e2978 — Thorben Günther 1 year, 2 months ago df02ee5 v0.2.0
Add tags for resolved alerts

Implements: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/8
4 files changed, 38 insertions(+), 0 deletions(-)

M README.md
M config.go
M config_test.go
M main.go
M README.md => README.md +5 -0
@@ 56,6 56,11 @@ labels {
    }
}

# Set tags for resolved alerts
resolved {
    tags "resolved,partying_face"
}

ntfy {
    # URL of the ntfy topic - required
    topic https://ntfy.sh/alertmanager-alerts

M config.go => config.go +18 -0
@@ 26,6 26,7 @@ type config struct {
	labels      labels
	cache       cacheConfig
	am          alertmanagerConfig
	resolved    resolvedConfig
}

type ntfyConfig struct {


@@ 56,6 57,10 @@ type alertmanagerConfig struct {
	URL             string
}

type resolvedConfig struct {
	Tags []string
}

func readConfig(path string) (*config, error) {
	cfg, err := scfg.Load(path)
	if err != nil {


@@ 272,5 277,18 @@ func readConfig(path string) (*config, error) {
		}
	}

	resolvedDir := cfg.Get("resolved")
	if resolvedDir != nil {
		d = resolvedDir.Children.Get("tags")
		if d != nil {
			var tags string
			if err := d.ParseParams(&tags); err != nil {
				return nil, err
			}

			config.resolved.Tags = strings.Split(tags, ",")
		}
	}

	return config, nil
}

M config_test.go => config_test.go +7 -0
@@ 41,6 41,10 @@ labels {
    }
}

resolved {
    tags "resolved,partying_face"
}

ntfy {
    # URL of the ntfy topic - required
    topic https://ntfy.sh/alertmanager-alerts


@@ 93,6 97,9 @@ cache {
			Password:        "pass",
			URL:             "https://alertmanager.xenrox.net",
		},
		resolved: resolvedConfig{
			Tags: []string{"resolved", "partying_face"},
		},
	}

	configPath := filepath.Join(t.TempDir(), "config")

M main.go => main.go +8 -0
@@ 89,6 89,10 @@ func (rcv *receiver) singleAlertNotifications(p *payload) []*notification {
		}

		var tags []string
		if alert.Status == "resolved" {
			tags = append(tags, rcv.cfg.resolved.Tags...)
		}

		for _, labelName := range rcv.cfg.labels.Order {
			val, ok := alert.Labels[labelName]
			if !ok {


@@ 176,6 180,10 @@ func (rcv *receiver) multiAlertNotification(p *payload) *notification {

	var priority string
	var tags []string
	if p.Status == "resolved" {
		tags = append(tags, rcv.cfg.resolved.Tags...)
	}

	for _, labelName := range rcv.cfg.labels.Order {
		val, ok := p.CommonLabels[labelName]
		if !ok {