Skip to content

Commit 85ff16f

Browse files
committed
Merge branch 'rewbycraft-master'
2 parents e6edc22 + f581cbc commit 85ff16f

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

main.go

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ import (
55
"encoding/json"
66
"flag"
77
"fmt"
8-
"os"
98
"io/ioutil"
109
"net/http"
10+
"os"
1111
"strings"
1212
)
1313

14+
type alertManAlert 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+
}
25+
1426
type alertManOut struct {
15-
Alerts []struct {
16-
Annotations struct {
17-
Description string `json:"description"`
18-
Summary string `json:"summary"`
19-
} `json:"annotations"`
20-
EndsAt string `json:"endsAt"`
21-
GeneratorURL string `json:"generatorURL"`
22-
Labels map[string]string `json:"labels"`
23-
StartsAt string `json:"startsAt"`
24-
Status string `json:"status"`
25-
} `json:"alerts"`
27+
Alerts []alertManAlert `json:"alerts"`
2628
CommonAnnotations struct {
2729
Summary string `json:"summary"`
2830
} `json:"commonAnnotations"`
@@ -48,7 +50,7 @@ func main() {
4850
webhookUrl := os.Getenv("DISCORD_WEBHOOK")
4951
if webhookUrl == "" {
5052
fmt.Fprintf(os.Stderr, "error: environment variable DISCORD_WEBHOOK not found\n")
51-
os.Exit(1)
53+
os.Exit(1)
5254
}
5355
whURL := flag.String("webhook.url", webhookUrl, "")
5456
flag.Parse()
@@ -65,26 +67,34 @@ func main() {
6567
panic(err)
6668
}
6769

68-
DO := discordOut{
69-
Name: amo.Status,
70-
}
70+
groupedAlerts := make(map[string][]alertManAlert)
7171

72-
Content := "```"
73-
if amo.CommonAnnotations.Summary != "" {
74-
Content = fmt.Sprintf(" === %s === \n```", amo.CommonAnnotations.Summary)
72+
for _, alert := range amo.Alerts {
73+
groupedAlerts[alert.Status] = append(groupedAlerts[alert.Status], alert)
7574
}
7675

77-
for _, alert := range amo.Alerts {
78-
realname := alert.Labels["instance"]
79-
if strings.Contains(realname, "localhost") && alert.Labels["exported_instance"] != "" {
80-
realname = alert.Labels["exported_instance"]
76+
for status, alerts := range groupedAlerts {
77+
DO := discordOut{
78+
Name: status,
8179
}
82-
Content += fmt.Sprintf("[%s]: %s on %s\n%s\n\n", strings.ToUpper(amo.Status), alert.Labels["alertname"], realname, alert.Annotations.Description)
83-
}
8480

85-
DO.Content = Content + "```"
81+
Content := "```"
82+
if amo.CommonAnnotations.Summary != "" {
83+
Content = fmt.Sprintf(" === %s === \n```", amo.CommonAnnotations.Summary)
84+
}
85+
86+
for _, alert := range alerts {
87+
realname := alert.Labels["instance"]
88+
if strings.Contains(realname, "localhost") && alert.Labels["exported_instance"] != "" {
89+
realname = alert.Labels["exported_instance"]
90+
}
91+
Content += fmt.Sprintf("[%s]: %s on %s\n%s\n\n", strings.ToUpper(status), alert.Labels["alertname"], realname, alert.Annotations.Description)
92+
}
8693

87-
DOD, _ := json.Marshal(DO)
88-
http.Post(*whURL, "application/json", bytes.NewReader(DOD))
94+
DO.Content = Content + "```"
95+
96+
DOD, _ := json.Marshal(DO)
97+
http.Post(*whURL, "application/json", bytes.NewReader(DOD))
98+
}
8999
}))
90100
}

0 commit comments

Comments
 (0)