forked from dsmith73/alertmanager-discord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (101 loc) · 3.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/namsral/flag"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
//json structure from Alertmanager
type alertManOut struct {
Alerts []alertManAlert `json:"alerts"`
CommonAnnotations struct {
Summary string `json:"summary"`
Description string `json:"description"`
} `json:"commonAnnotations"`
CommonLabels struct {
Alertname string `json:"alertname"`
Severity string `json:"severity"`
} `json:"commonLabels"`
ExternalURL string `json:"externalURL"`
GroupKey string `json:"groupKey"`
GroupLabels struct {
Alertname string `json:"alertname"`
} `json:"groupLabels"`
Receiver string `json:"receiver"`
Status string `json:"status"`
Version string `json:"version"`
}
type alertManAlert struct {
Annotations struct {
Description string `json:"description"`
Summary string `json:"summary"`
} `json:"annotations"`
EndsAt string `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
Labels map[string]string `json:"labels"`
StartsAt string `json:"startsAt"`
Status string `json:"status"`
}
//data sent to Discord. Where Name - is bot Name
type discordOut struct {
Content string `json:"content"`
Name string `json:"username"`
}
var (
config string
address string
webhookUrl string
discordName string
logger *log.Logger
v bool
)
func handler(w http.ResponseWriter, r *http.Request) {
//parse json from Alertmanager
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
if v == true {
logger.Println("body:", string(b), "\n")
}
amo := alertManOut{}
ama := alertManAlert{}
err = json.Unmarshal(b, &amo)
if err != nil {
panic(err)
}
for _, alert := range amo.Alerts {
DO := discordOut{
//Name: status,
Name: discordName,
}
Content := ""
if amo.CommonAnnotations.Summary != "" {
Content = fmt.Sprintf(" ** [%s - %s]: %s **\n", strings.ToUpper(alert.Status), amo.CommonLabels.Severity, amo.CommonAnnotations.Summary)
}
DO.Content = Content + fmt.Sprintf("@here - %s - %s\nView: **[Prometheus]( %s )** , **[Runbook](https://101101.github.io/kb/search/?q=%s )**", amo.CommonLabels.Alertname, amo.CommonAnnotations.Description, ama.GeneratorURL, amo.CommonLabels.Alertname)
DOD, _ := json.Marshal(DO)
http.Post(webhookUrl, "application/json", bytes.NewReader(DOD))
}
}
func main() {
flag.StringVar(&config, "config", "", "Config file with variables - optional. Can parse both variables from config and CLI")
flag.StringVar(&address, "address", ":9095", "Service listen address and port")
flag.StringVar(&webhookUrl, "discord_webhook", "", "DISCORD_WEBHOOK to push messages")
flag.StringVar(&discordName, "discord_name", "AlertManager", "DISCORD_NAME of bot pushing messages")
flag.BoolVar(&v, "verbose", false, "Verbose mode")
flag.Parse()
if webhookUrl == "" {
fmt.Fprintf(os.Stderr, "error: environment variable DISCORD_WEBHOOK not found\n")
os.Exit(1)
}
logger = log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile)
logger.Println("Starting listening on ", address, "\n")
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(address, nil))
}