@@ 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