forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsgenie-notifier.go
59 lines (44 loc) · 1.7 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
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
}
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
}
for _, message := range messages {
title := fmt.Sprintf("\n%s:%s:%s is %s.", message.Node, message.Service, message.Check, message.Status)
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
response, alertErr := opsgenie.Send(alertCli, title, content)
if alertErr != nil {
log.Println("Opsgenie notification trouble.", response.Status)
return false
}
}
log.Println("Opsgenie notification send.")
return true
}
func (opsgenie *OpsGenieNotifier) Send(alertCli *ogcli.OpsGenieAlertClient, message string, content string) (*alerts.CreateAlertResponse, error) {
req := alerts.CreateAlertRequest{
Message: message,
Description: content,
Source: "consul",
Entity: opsgenie.ClusterName,
}
return alertCli.Create(req)
}