|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +type alertManOut struct { |
| 14 | + Alerts []struct { |
| 15 | + Annotations struct { |
| 16 | + Description string `json:"description"` |
| 17 | + Summary string `json:"summary"` |
| 18 | + } `json:"annotations"` |
| 19 | + EndsAt string `json:"endsAt"` |
| 20 | + GeneratorURL string `json:"generatorURL"` |
| 21 | + Labels map[string]string `json:"labels"` |
| 22 | + StartsAt string `json:"startsAt"` |
| 23 | + Status string `json:"status"` |
| 24 | + } `json:"alerts"` |
| 25 | + CommonAnnotations struct { |
| 26 | + Summary string `json:"summary"` |
| 27 | + } `json:"commonAnnotations"` |
| 28 | + CommonLabels struct { |
| 29 | + Alertname string `json:"alertname"` |
| 30 | + } `json:"commonLabels"` |
| 31 | + ExternalURL string `json:"externalURL"` |
| 32 | + GroupKey string `json:"groupKey"` |
| 33 | + GroupLabels struct { |
| 34 | + Alertname string `json:"alertname"` |
| 35 | + } `json:"groupLabels"` |
| 36 | + Receiver string `json:"receiver"` |
| 37 | + Status string `json:"status"` |
| 38 | + Version string `json:"version"` |
| 39 | +} |
| 40 | + |
| 41 | +type discordOut struct { |
| 42 | + Content string `json:"content"` |
| 43 | + Name string `json:"username"` |
| 44 | +} |
| 45 | + |
| 46 | +func main() { |
| 47 | + whURL := flag.String("webhook.url", "https://blah", "") |
| 48 | + flag.Parse() |
| 49 | + http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 50 | + b, err := ioutil.ReadAll(r.Body) |
| 51 | + if err != nil { |
| 52 | + panic(err) |
| 53 | + } |
| 54 | + |
| 55 | + amo := alertManOut{} |
| 56 | + err = json.Unmarshal(b, &amo) |
| 57 | + if err != nil { |
| 58 | + panic(err) |
| 59 | + } |
| 60 | + |
| 61 | + DO := discordOut{ |
| 62 | + Name: amo.Status, |
| 63 | + } |
| 64 | + |
| 65 | + Content := "!!" |
| 66 | + if amo.CommonAnnotations.Summary != "" { |
| 67 | + Content = fmt.Sprintf(" === %s === \n", amo.CommonAnnotations.Summary) |
| 68 | + } |
| 69 | + |
| 70 | + for _, alert := range amo.Alerts { |
| 71 | + realname := alert.Labels["instance"] |
| 72 | + if strings.Contains(realname, "localhost") && alert.Labels["exported_instance"] != "" { |
| 73 | + realname = alert.Labels["exported_instance"] |
| 74 | + } |
| 75 | + Content += fmt.Sprintf("FIRING: %s on %s\n", alert.Labels["alertname"], realname) |
| 76 | + } |
| 77 | + |
| 78 | + DO.Content = Content |
| 79 | + |
| 80 | + DOD, _ := json.Marshal(DO) |
| 81 | + http.Post(*whURL, "application/json", bytes.NewReader(DOD)) |
| 82 | + })) |
| 83 | +} |
0 commit comments