~gjabell/xmpp-webhook

0dcb55fa017402e9ca40f2ae3fc31d34875107c6 — Galen Abell 1 year, 1 month ago 5c9fded
Better alertmanage message formatting
1 files changed, 20 insertions(+), 22 deletions(-)

M parser/alertmanager.go
M parser/alertmanager.go => parser/alertmanager.go +20 -22
@@ 6,8 6,26 @@ import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

type Alert struct {
	Status string `json:"status"`
	Labels struct {
		Alertname string `json:"alertname"`
		Instance  string `json:"instance"`
	} `json:"labels"`
	Annotations struct {
		Summary string `json:"summary"`
	} `json:"annotations"`
}

func (a *Alert) toMessage() string {
	status := strings.ToUpper(a.Status)
	return fmt.Sprintf("[%s] *%s* on %s: %s", status, a.Labels.Alertname,
		a.Labels.Instance, a.Annotations.Summary)
}

func AlertmanagerParserFunc(r *http.Request) (string, error) {
	// get alert data from request
	body, err := ioutil.ReadAll(r.Body)


@@ 16,11 34,7 @@ func AlertmanagerParserFunc(r *http.Request) (string, error) {
	}

	payload := &struct {
		Alerts []struct {
			Status      string            `json:"status"`
			Labels      map[string]string `json:"labels"`
			Annotations map[string]string `json:"annotations"`
		} `json:"alerts"`
		Alerts []Alert `json:"alerts"`
	}{}

	// parse body into the alert struct


@@ 32,23 46,7 @@ func AlertmanagerParserFunc(r *http.Request) (string, error) {
	// construct alert message
	var message string
	for _, alert := range payload.Alerts {
		if alert.Status == "resolved" {
			message = "Resolved" + "\n"
		} else {
			message = "Firing" + "\n"
		}

		message += "Labels" + "\n"
		for key, label := range alert.Labels {
			message += fmt.Sprintf("%s = %s\n", key, label)
		}

		message += "Annotations" + "\n"
		for key, annotation := range alert.Annotations {
			message += fmt.Sprintf("%s = %s\n", key, annotation)
		}

		message += "\n"
		message += alert.toMessage() + "\n"
	}

	return message, nil