forked from damomurf/keybase-alertmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
233 lines (185 loc) · 6.13 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"text/template"
"time"
"github.com/keybase/go-keybase-chat-bot/kbchat"
"github.com/prometheus/alertmanager/notify/webhook"
atmpl "github.com/prometheus/alertmanager/template"
)
var watchdogCache map[string]watchdog = map[string]watchdog{}
type watchdog struct {
id string
lastPing time.Time
lastAlert atmpl.Alert
firing bool
}
// DefaultFuncs is the default list additional Go Template functions supported.
var DefaultFuncs = template.FuncMap{
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"title": strings.Title,
// join is equal to strings.Join but inverts the argument order
// for easier pipelining in templates.
"join": func(sep string, s []string) string {
return strings.Join(s, sep)
},
"match": regexp.MatchString,
"reReplaceAll": func(pattern, repl, text string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(text, repl)
},
"stringSlice": func(s ...string) []string {
return s
},
}
func escapePercents(str string) string {
return strings.ReplaceAll(str, "%", "%%")
}
func sendToKeybase(kbc *kbchat.API, recipient string, message string) {
if strings.ContainsRune(recipient, '#') {
// send message to team
teamChannel := strings.Split(recipient, "#")
team, channel := teamChannel[0], teamChannel[1]
if _, err := kbc.SendMessageByTeamName(team, &channel, escapePercents(message)); err != nil {
log.Printf("Error sending message: %+v", err)
}
} else {
// send message to user
tlfName := fmt.Sprintf("%s,%s", kbc.GetUsername(), recipient)
log.Printf("tlfName: %s", tlfName)
if _, err := kbc.SendMessageByTlfName(tlfName, escapePercents(message)); err != nil {
log.Printf("Error sending message: %+v", err)
}
}
}
func handleWebhook(kbc *kbchat.API, recipient string, tmpl *template.Template) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading webhook post: %+v", err)
}
wh := &webhook.Message{}
err = json.Unmarshal(buf, wh)
if err != nil {
log.Printf("Error parsing webhook post: %+v", err)
}
log.Printf("Received and parsed incoming webhook: %+v", wh)
writer := bytes.NewBufferString("")
tmpl.ExecuteTemplate(writer, "keybaseAlert", *wh)
log.Printf("%s", writer.String())
sendToKeybase(kbc, recipient, writer.String())
}
}
func handleWatchdog(kbc *kbchat.API, recipient string, tmpl *template.Template) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading watchdog post: %+v", err)
}
wh := &webhook.Message{}
err = json.Unmarshal(buf, wh)
if err != nil {
log.Printf("Error parsing watchdog post: %+v", err)
}
log.Printf("Received and parsed incoming watchdog: %+v", wh)
alerts := wh.Alerts.Firing()
for _, alert := range alerts {
hash := sha256.New()
for _, k := range alert.Labels.SortedPairs().Names() {
v := alert.Labels[k]
hash.Write([]byte(fmt.Sprintf("%s:%s", k, v)))
}
watchdogID := hex.EncodeToString(hash.Sum([]byte{}))
log.Printf("Incoming watchdog request for: %+v ID: %s", alert.Labels, watchdogID)
entry, ok := watchdogCache[watchdogID]
if ok {
if entry.firing {
// Recover the watchdog alert as we've seen pings return
writer := bytes.NewBufferString("")
tmpl.ExecuteTemplate(writer, "watchdogAlertRecover", entry.lastAlert)
sendToKeybase(kbc, recipient, writer.String())
}
entry.firing = false
entry.lastPing = time.Now()
watchdogCache[watchdogID] = entry
} else {
watchdogCache[watchdogID] = watchdog{
id: watchdogID,
lastPing: time.Now(),
lastAlert: alert,
firing: false,
}
}
}
}
}
func main() {
var kbLoc string
var kbc *kbchat.API
var listenPort int
var interval time.Duration
var expiry time.Duration
var recipient string
var templatePath string
var err error
flag.StringVar(&kbLoc, "keybase", "keybase", "the location of the Keybase app")
flag.IntVar(&listenPort, "port", 3000, "Port to listen for webhooks")
flag.StringVar(&recipient, "recipient", "", "Keybase user or team#channel to send message to")
flag.DurationVar(&interval, "interval", 10*time.Second, "The interval at which to check for watchdog expiry")
flag.DurationVar(&expiry, "expiry", 2*time.Minute, "The amount of time after which a non-pinging watchdog check will be considered to have expired")
flag.StringVar(&templatePath, "template", "default.tmpl", "Go text template definition file")
flag.Parse()
tmpl, err := template.New("default.tmpl").Funcs(DefaultFuncs).ParseFiles(templatePath)
if err != nil {
log.Panicf("Unable to parse template: %+v", err)
}
log.Printf("Templates parsed successfully.")
if kbc, err = kbchat.Start(kbchat.RunOptions{
KeybaseLocation: kbLoc,
StartService: true,
DisableBotLiteMode: true,
Oneshot: &kbchat.OneshotOptions{
PaperKey: os.Getenv("KEYBASE_PAPERKEY"),
Username: os.Getenv("KEYBASE_USERNAME"),
},
}); err != nil {
log.Fatalf("Error creating API: %+v", err)
}
log.Printf("Keybase API setup complete.")
// Start the watchdog timer
ticker := time.NewTicker(interval)
go func() {
for {
select {
case _ = <-ticker.C:
for id, watchdog := range watchdogCache {
if watchdog.firing == false && time.Now().Sub(watchdog.lastPing) > expiry {
// Watchdog has expired, we need to alert
watchdog.firing = true
watchdogCache[id] = watchdog
writer := bytes.NewBufferString("")
tmpl.ExecuteTemplate(writer, "watchdogAlertFire", watchdog.lastAlert)
sendToKeybase(kbc, recipient, writer.String())
}
}
}
}
}()
log.Printf("Started watchdog timer routine.")
http.HandleFunc("/webhook", handleWebhook(kbc, recipient, tmpl))
http.HandleFunc("/watchdog", handleWatchdog(kbc, recipient, tmpl))
log.Printf("Listening on port %d", listenPort)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", listenPort), nil))
}