Merge branch 'xenrox-add-alertmanager'

master
Thomas Maier 2021-02-28 09:57:08 +01:00
commit 075ff1c136
3 zmienionych plików z 86 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,30 @@
{
"receiver": "xmpp-email",
"status": "firing",
"alerts": [
{
"status": "firing",
"labels": {
"alertname": "testalert",
"instance": "test.net",
"severity": "critical"
},
"annotations": { "summary": "Simple test" },
"startsAt": "2021-02-27T18:38:56Z",
"endsAt": "0001-01-01T00:00:00Z",
"generatorURL": "http://local-example-alert/testalert",
"fingerprint": "d4baf2738cfc5a30"
}
],
"groupLabels": { "instance": "test.net", "severity": "critical" },
"commonLabels": {
"alertname": "testalert",
"instance": "test.net",
"severity": "critical"
},
"commonAnnotations": { "summary": "Simple test" },
"externalURL": "http://127.0.0.1:9093",
"version": "4",
"groupKey": "{}/{severity=\"critical\"}:{instance=\"test.net\", severity=\"critical\"}",
"truncatedAlerts": 0
}

Wyświetl plik

@ -169,6 +169,7 @@ func main() {
// initialize handlers with accociated parser functions
http.Handle("/grafana", newMessageHandler(messages, parser.GrafanaParserFunc))
http.Handle("/slack", newMessageHandler(messages, parser.SlackParserFunc))
http.Handle("/alertmanager", newMessageHandler(messages, parser.AlertmanagerParserFunc))
// listen for requests
_ = http.ListenAndServe(listenAddress, nil)

Wyświetl plik

@ -0,0 +1,55 @@
package parser
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
func AlertmanagerParserFunc(r *http.Request) (string, error) {
// get alert data from request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", errors.New(readErr)
}
payload := &struct {
Alerts []struct {
Status string `json:"status"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
} `json:"alerts"`
}{}
// parse body into the alert struct
err = json.Unmarshal(body, &payload)
if err != nil {
return "", errors.New(parseErr)
}
// contruct 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"
}
return message, nil
}