@@ -5,24 +5,26 @@ import (
5
5
"encoding/json"
6
6
"flag"
7
7
"fmt"
8
- "os"
9
8
"io/ioutil"
10
9
"net/http"
10
+ "os"
11
11
"strings"
12
12
)
13
13
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
+
14
26
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"`
26
28
CommonAnnotations struct {
27
29
Summary string `json:"summary"`
28
30
} `json:"commonAnnotations"`
@@ -48,7 +50,7 @@ func main() {
48
50
webhookUrl := os .Getenv ("DISCORD_WEBHOOK" )
49
51
if webhookUrl == "" {
50
52
fmt .Fprintf (os .Stderr , "error: environment variable DISCORD_WEBHOOK not found\n " )
51
- os .Exit (1 )
53
+ os .Exit (1 )
52
54
}
53
55
whURL := flag .String ("webhook.url" , webhookUrl , "" )
54
56
flag .Parse ()
@@ -65,26 +67,34 @@ func main() {
65
67
panic (err )
66
68
}
67
69
68
- DO := discordOut {
69
- Name : amo .Status ,
70
- }
70
+ groupedAlerts := make (map [string ][]alertManAlert )
71
71
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 )
75
74
}
76
75
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 ,
81
79
}
82
- Content += fmt .Sprintf ("[%s]: %s on %s\n %s\n \n " , strings .ToUpper (amo .Status ), alert .Labels ["alertname" ], realname , alert .Annotations .Description )
83
- }
84
80
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
+ }
86
93
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
+ }
89
99
}))
90
100
}
0 commit comments