forked from AcalephStorage/consul-alerts
/
hipchat-notifier.go
87 lines (70 loc) · 2.18 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
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
package notifier
import (
"fmt"
"html"
"net/url"
"strings"
"unicode/utf8"
"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 {
Enabled bool
ClusterName string `json:"cluster-name"`
RoomId string `json:"room-id"`
AuthToken string `json:"auth-token"`
BaseURL string `json:"base-url"`
From string `json:"from"`
}
// NotifierName provides name for notifier selection
func (notifier *HipChatNotifier) NotifierName() string {
return "hipchat"
}
func (notifier *HipChatNotifier) Copy() Notifier {
n := *notifier
return &n
}
//Notify sends messages to the endpoint notifier
func (notifier *HipChatNotifier) Notify(messages Messages) bool {
overallStatus, pass, warn, fail := messages.Summary()
text := fmt.Sprintf("%s is <STRONG>%s</STRONG>. Fail: %d, Warn: %d, Pass: %d", notifier.ClusterName, overallStatus, fail, warn, pass)
for _, message := range messages {
text += fmt.Sprintf("<BR><STRONG><CODE>%s</CODE></STRONG>:%s:%s is <STRONG>%s</STRONG>.",
message.Node, html.EscapeString(message.Service), html.EscapeString(message.Check), message.Status)
if utf8.RuneCountInString(message.Output) > 0 {
text += fmt.Sprintf("<BR>%s", strings.Replace(html.EscapeString(strings.TrimSpace(message.Output)), "\n", "<BR>", -1))
}
}
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
}
from := ""
if notifier.From != "" {
from = notifier.From
}
notifRq := &hipchat.NotificationRequest{
Color: level,
Message: text,
Notify: true,
MessageFormat: "html",
From: from,
}
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
}