forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ilert-notifier.go
99 lines (80 loc) · 2.22 KB
/
ilert-notifier.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
package notifier
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)
const apiEndpoint string = "https://ilertnow.com/api/v1/events"
type ILertNotifier struct {
Enabled bool
ApiKey string `json:"api-key"`
}
type iLertEvent struct {
ApiKey string `json:"apiKey"`
EventType string `json:"eventType"`
Summary string `json:"summary"`
Details string `json:"details"`
IncidentKey string `json:"incidentKey"`
}
// NotifierName provides name for notifier selection
func (il *ILertNotifier) NotifierName() string {
return "ilert"
}
func (il *ILertNotifier) Copy() Notifier {
notifier := *il
return ¬ifier
}
//Notify sends messages to the endpoint notifier
func (il *ILertNotifier) Notify(messages Messages) bool {
result := true
for _, message := range messages {
var eventType string
var summary string
incidentKey := fmt.Sprintf("%s:%s:%s", message.Node, message.Service, message.Check)
switch {
case message.IsPassing():
summary = incidentKey + " is now HEALTHY"
eventType = "RESOLVE"
case message.IsWarning():
// iLert does not support warning state
continue
case message.IsCritical():
summary = incidentKey + " is CRITICAL"
eventType = "ALERT"
}
if err := il.sendEvent(eventType, summary, message.Output, incidentKey); err != nil {
log.Error("Problem while sending iLert event:", err)
result = false
}
}
log.Println("iLert notification complete")
return result
}
func (il *ILertNotifier) sendEvent(eventType, summary, details, incidentKey string) error {
event := iLertEvent{
ApiKey: il.ApiKey,
EventType: eventType,
Summary: summary,
Details: details,
IncidentKey: incidentKey,
}
body, err := json.Marshal(event)
if err != nil {
return err
}
log.Debugf("struct = %+v, json = %s", event, string(body))
res, err := http.Post(apiEndpoint, "application/json", bytes.NewBuffer(body))
if err != nil {
return err
}
if res.StatusCode != 200 {
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
return errors.New(fmt.Sprintf("unexpected HTTP status code: %d (%s)", res.StatusCode, string(body)))
}
return nil
}