forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsgenie-notifier.go
115 lines (94 loc) · 3.25 KB
/
opsgenie-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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package notifier
import (
"fmt"
alerts "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/opsgenie/opsgenie-go-sdk/alerts"
ogcli "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/opsgenie/opsgenie-go-sdk/client"
log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)
type OpsGenieNotifier struct {
ClusterName string
ApiKey string
NotifName string
}
// NotifierName provides name for notifier selection
func (opsgenie *OpsGenieNotifier) NotifierName() string {
return opsgenie.NotifName
}
//Notify sends messages to the endpoint notifier
func (opsgenie *OpsGenieNotifier) Notify(messages Messages) bool {
overallStatus, pass, warn, fail := messages.Summary()
client := new(ogcli.OpsGenieClient)
client.SetApiKey(opsgenie.ApiKey)
alertCli, cliErr := client.Alert()
if cliErr != nil {
log.Println("Opsgenie notification trouble with client")
return false
}
ok := true
for _, message := range messages {
title := fmt.Sprintf("\n%s:%s:%s is %s.", message.Node, message.Service, message.Check, message.Status)
alias := opsgenie.createAlias(message)
content := fmt.Sprintf(header, opsgenie.ClusterName, overallStatus, fail, warn, pass)
content += fmt.Sprintf("\n%s:%s:%s is %s.", message.Node, message.Service, message.Check, message.Status)
content += fmt.Sprintf("\n%s", message.Output)
// create the alert
switch {
case message.IsCritical():
ok = opsgenie.createAlert(alertCli, title, content, alias) && ok
case message.IsWarning():
ok = opsgenie.createAlert(alertCli, title, content, alias) && ok
case message.IsPassing():
ok = opsgenie.closeAlert(alertCli, alias) && ok
default:
ok = false
log.Warn("Message was not either IsCritical, IsWarning or IsPasssing. No notification was sent for ", alias)
}
}
return ok
}
func (opsgenie *OpsGenieNotifier) createAlias(message Message) string {
incidentKey := message.Node
if message.ServiceId != "" {
incidentKey += ":" + message.ServiceId
}
return incidentKey
}
func (opsgenie *OpsGenieNotifier) createAlert(alertCli *ogcli.OpsGenieAlertClient, message string, content string, alias string) bool {
log.Debug(fmt.Sprintf("OpsGenieAlertClient.CreateAlert alias: %s", alias))
req := alerts.CreateAlertRequest{
Message: message,
Description: content,
Alias: alias,
Source: "consul",
Entity: opsgenie.ClusterName,
}
response, alertErr := alertCli.Create(req)
if alertErr != nil {
if response == nil {
log.Warn("Opsgenie notification trouble. ", alertErr)
} else {
log.Warn("Opsgenie notification trouble. ", response.Status)
}
return false
}
log.Println("Opsgenie notification sent.")
return true
}
func (opsgenie *OpsGenieNotifier) closeAlert(alertCli *ogcli.OpsGenieAlertClient, alias string) bool {
log.Debug(fmt.Sprintf("OpsGenieAlertClient.CloseAlert alias: %s", alias))
req := alerts.CloseAlertRequest{
Alias: alias,
Source: "consul",
}
response, alertErr := alertCli.Close(req)
if alertErr != nil {
if response == nil {
log.Warn("Opsgenie notification trouble. ", alertErr)
} else {
log.Warn("Opsgenie notification trouble. ", response.Status)
}
return false
}
log.Println("Opsgenie close alert sent.")
return true
}