kopia lustrzana https://github.com/tmsmr/xmpp-webhook
Add alertmanager parser
rodzic
34d3730325
commit
dd30c9c50f
|
@ -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
|
||||||
|
}
|
1
main.go
1
main.go
|
@ -154,6 +154,7 @@ func main() {
|
||||||
// initialize handlers with accociated parser functions
|
// initialize handlers with accociated parser functions
|
||||||
http.Handle("/grafana", newMessageHandler(messages, parser.GrafanaParserFunc))
|
http.Handle("/grafana", newMessageHandler(messages, parser.GrafanaParserFunc))
|
||||||
http.Handle("/slack", newMessageHandler(messages, parser.SlackParserFunc))
|
http.Handle("/slack", newMessageHandler(messages, parser.SlackParserFunc))
|
||||||
|
http.Handle("/alertmanager", newMessageHandler(messages, parser.AlertmanagerParserFunc))
|
||||||
|
|
||||||
// listen for requests
|
// listen for requests
|
||||||
_ = http.ListenAndServe(":4321", nil)
|
_ = http.ListenAndServe(":4321", nil)
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
Ładowanie…
Reference in New Issue