forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hipchat-notifier.go
59 lines (47 loc) · 1.37 KB
/
hipchat-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"
"net/url"
"github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/tbruyelle/hipchat-go/hipchat"
log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)
type HipChatNotifier struct {
ClusterName string
RoomId string
AuthToken string
BaseURL string
}
func (notifier *HipChatNotifier) Notify(messages Messages) bool {
overallStatus, pass, warn, fail := messages.Summary()
text := fmt.Sprintf(header, notifier.ClusterName, overallStatus, fail, warn, pass)
for _, message := range messages {
text += fmt.Sprintf("\n%s:%s:%s is %s.", message.Node, message.Service, message.Check, message.Status)
text += fmt.Sprintf("\n%s", message.Output)
}
level := "green"
if fail > 0 {
level = "red"
} else if warn > 0 {
level = "yellow"
}
client := hipchat.NewClient(notifier.AuthToken)
if notifier.BaseURL != "" {
url, err := url.Parse(notifier.BaseURL)
if err != nil {
log.Printf("Error parsing hipchat base url: %s\n", err)
}
client.BaseURL = url
}
notifRq := &hipchat.NotificationRequest{
Message: text,
Color: level,
Notify: true,
}
resp, err := client.Room.Notification(notifier.RoomId, notifRq)
if err != nil {
log.Printf("Error sending notification to hipchat: %s\n", err)
log.Printf("Server returns %+v\n", resp)
return false
}
return true
}