-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
340 lines (289 loc) · 7.39 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// This is the Purppura bridge, which reads test-results from redis, and submits
// them to purppura, such that a human can be notified of test failures.
//
// The program should be built like so:
//
// go build .
//
// Once built launch it like so:
//
// $ ./purppura-bridge -url="http://purppura.example.com/events"
//
// Every two minutes it will send a heartbeat to the purppura-server so
// that you know it is working.
//
// Steve
// --
//
package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"sync"
"time"
"github.com/go-redis/redis"
"github.com/robfig/cron"
_ "github.com/skx/golang-metrics"
)
// Avoid threading issues with our last update-time
var mutex sync.RWMutex
// The last time we received an update
var update int64
// Should we be verbose?
var verbose *bool
// The redis handle
var r *redis.Client
// The URL of the purppura server
var pURL *string
// Given a JSON string decode it and post to the Purppura URL.
func process(msg []byte) {
// Update our last received time
mutex.Lock()
update = time.Now().Unix()
mutex.Unlock()
data := map[string]string{}
if err := json.Unmarshal(msg, &data); err != nil {
panic(err)
}
testType := data["type"]
testTarget := data["target"]
input := data["input"]
//
// We need a stable ID for each test - get one by hashing the
// complete input-line and the target we executed against.
//
hasher := sha1.New()
hasher.Write([]byte(testTarget))
hasher.Write([]byte(input))
hash := hex.EncodeToString(hasher.Sum(nil))
//
// Populate the default fields.
//
values := map[string]string{
"detail": fmt.Sprintf("<p>The <code>%s</code> test against <code>%s</code> passed.</p>", testType, testTarget),
"id": hash,
"raise": "clear",
"subject": input,
}
//
// If the test failed we'll update the detail and trigger a raise
//
if data["error"] != "" {
values["detail"] =
fmt.Sprintf("<p>The <code>%s</code> test against <code>%s</code> failed:</p><p><pre>%s</pre></p>",
testType, testTarget, data["error"])
values["raise"] = "now"
}
//
// Export the fields to json to post.
//
jsonValue, err := json.Marshal(values)
if err != nil {
fmt.Printf("process: Failed to encode JSON:%s\n", err.Error())
os.Exit(1)
}
//
// If we're being verbose show what we're going to POST
//
if *verbose {
fmt.Printf("%s\n", jsonValue)
}
//
// Post to purppura
//
res, err := http.Post(*pURL,
"application/json",
bytes.NewBuffer(jsonValue))
if err != nil {
fmt.Printf("process: Failed to post to purppura:%s\n", err.Error())
os.Exit(1)
}
//
// OK now we've submitted the post.
//
// We should retrieve the status-code + body, if the status-code
// is "odd" then we'll show them.
//
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("process: Error reading response to post: %s\n", err.Error())
return
}
status := res.StatusCode
if status != 200 {
fmt.Printf("process: Error - Status code was not 200: %d\n", status)
fmt.Printf("process: Response - %s\n", body)
}
}
// CheckUpdates triggers an alert if we've not received anything recently
func CheckUpdates() {
// Get our last-received time
mutex.Lock()
then := update
mutex.Unlock()
// Get the current time
now := time.Now().Unix()
//
// The alert we'll send to the purppura server
//
values := map[string]string{
"detail": fmt.Sprintf("The purppura-bridge last received an update %d seconds ago.", now-then),
"subject": "No traffic seen recently",
"id": "purppura-bridge-traffic",
"source": "127.127.127.127",
}
// Raise or clear?
if now-then > (60 * 5) {
values["raise"] = "now"
} else {
values["raise"] = "clear"
}
//
// Export the fields to json to post.
//
jsonValue, err := json.Marshal(values)
if err != nil {
fmt.Printf("Failed to export to JSON - %s\n", err.Error())
os.Exit(1)
}
//
// Post to purppura
//
res, err := http.Post(*pURL,
"application/json",
bytes.NewBuffer(jsonValue))
if err != nil {
fmt.Printf("CheckUpdates: Failed to post purppura-bridge to purppura:%s\n", err.Error())
os.Exit(1)
}
//
// OK now we've submitted the post.
//
// We should retrieve the status-code + body, if the status-code
// is "odd" then we'll show them.
//
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("CheckUpdates: Error reading response to post: %s\n", err.Error())
return
}
status := res.StatusCode
if status != 200 {
fmt.Printf("CheckUpdates: Error - Status code was not 200: %d\n", status)
fmt.Printf("CheckUpdates: Response - %s\n", body)
}
}
// SendHeartbeat updates the purppura server with a hearbeat alert.
// This will ensure that you're alerted if this bridge fails, dies, or
// isn't running
func SendHeartbeat() {
//
// The alert we'll send to the purppura server
//
values := map[string]string{
"detail": "The purppura-bridge hasn't sent a heartbeat recently, which means that overseer test-results won't raise alerts.",
"subject": "The purppura bridge isn't running!",
"id": "purppura-bridge-heartbeat",
"source": "127.127.127.127",
"raise": "+5m",
}
//
// Export the fields to json to post.
//
jsonValue, _ := json.Marshal(values)
//
// Post to purppura
//
res, err := http.Post(*pURL,
"application/json",
bytes.NewBuffer(jsonValue))
if err != nil {
fmt.Printf("SendHeartbeat: Failed to post heartbeat to purppura:%s\n", err.Error())
os.Exit(1)
}
//
// OK now we've submitted the post.
//
// We should retrieve the status-code + body, if the status-code
// is "odd" then we'll show them.
//
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("SendHeartbeat: Error reading response to post: %s\n", err.Error())
return
}
status := res.StatusCode
if status != 200 {
fmt.Printf("SendHeartbeat: Error - Status code was not 200: %d\n", status)
fmt.Printf("SendHeartbeat: Response - %s\n", body)
}
}
//
// Entry Point
//
func main() {
//
// Parse our flags
//
redisHost := flag.String("redis-host", "127.0.0.1:6379", "Specify the address of the redis queue.")
redisPass := flag.String("redis-pass", "", "Specify the password of the redis queue.")
pURL = flag.String("purppura", "", "The purppura-server URL")
verbose = flag.Bool("verbose", false, "Be verbose?")
flag.Parse()
//
// Sanity-check
//
if *pURL == "" {
fmt.Printf("Usage: purppura-bridge -purppura=https://alert.steve.fi/events [-redis-host=127.0.0.1:6379] [-redis-pass=secret]\n")
os.Exit(1)
}
//
// Create the redis client
//
r = redis.NewClient(&redis.Options{
Addr: *redisHost,
Password: *redisPass,
DB: 0, // use default DB
})
//
// And run a ping, just to make sure it worked.
//
_, err := r.Ping().Result()
if err != nil {
fmt.Printf("Redis connection failed: %s\n", err.Error())
os.Exit(1)
}
c := cron.New()
// Make sure we send a heartbeat so we're alerted if the bridge fails
c.AddFunc("@every 30s", func() { SendHeartbeat() })
// Make sure we raise an alert if we don't have recent results.
c.AddFunc("@every 5m", func() { CheckUpdates() })
c.Start()
for {
//
// Get test-results
//
msg, _ := r.BLPop(0, "overseer.results").Result()
//
// If they were non-empty, process them.
//
// msg[0] will be "overseer.results"
//
// msg[1] will be the value removed from the list.
//
if len(msg) >= 1 {
process([]byte(msg[1]))
}
}
}