forked from ansrivas/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-notifier.go
147 lines (117 loc) · 3.49 KB
/
slack-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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package notifier
import (
"bytes"
"fmt"
"strings"
"io/ioutil"
"encoding/json"
"net/http"
log "github.com/Sirupsen/logrus"
)
type SlackNotifier struct {
ClusterName string `json:"cluster_name"`
Url string `json:"url"`
Channel string `json:"channel"`
Username string `json:"username"`
IconUrl string `json:"icon_url"`
IconEmoji string `json:"icon_emoji"`
Text string `json:"text,omitempty"`
Attachments []attachment `json:"attachments,omitempty"`
Detailed bool `json:"detailed"`
Enabled bool `json:"enabled"`
}
type attachment struct {
Color string `json:"color"`
Title string `json:"title"`
Pretext string `json:"pretext"`
Text string `json:"text"`
MrkdwnIn []string `json:"mrkdwn_in"`
}
// NotifierName provides name for notifier selection
func (slack *SlackNotifier) NotifierName() string {
return "slack"
}
func (slack *SlackNotifier) Copy() Notifier {
notifier := *slack
return ¬ifier
}
//Notify sends messages to the endpoint notifier
func (slack *SlackNotifier) Notify(messages Messages) bool {
if slack.Detailed {
return slack.notifyDetailed(messages)
} else {
return slack.notifySimple(messages)
}
}
func (slack *SlackNotifier) notifySimple(messages Messages) bool {
overallStatus, pass, warn, fail := messages.Summary()
text := fmt.Sprintf(header, slack.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)
}
slack.Text = text
return slack.postToSlack()
}
func (slack *SlackNotifier) notifyDetailed(messages Messages) bool {
overallStatus, pass, warn, fail := messages.Summary()
var emoji, color string
switch overallStatus {
case SYSTEM_HEALTHY:
emoji = ":white_check_mark:"
color = "good"
case SYSTEM_UNSTABLE:
emoji = ":question:"
color = "warning"
case SYSTEM_CRITICAL:
emoji = ":x:"
color = "danger"
default:
emoji = ":question:"
}
title := "Consul monitoring report"
pretext := fmt.Sprintf("%s %s is *%s*", emoji, slack.ClusterName, overallStatus)
detailedBody := ""
detailedBody += fmt.Sprintf("*Changes:* Fail = %d, Warn = %d, Pass = %d", fail, warn, pass)
detailedBody += fmt.Sprintf("\n")
for _, message := range messages {
detailedBody += fmt.Sprintf("\n*[%s:%s]* %s is *%s.*", message.Node, message.Service, message.Check, message.Status)
var msg = strings.TrimSpace(message.Output)
if len(msg) != 0 {
detailedBody += fmt.Sprintf("\n```%s```", msg)
}
}
a := attachment{
Color: color,
Title: title,
Pretext: pretext,
Text: detailedBody,
MrkdwnIn: []string{"text", "pretext"},
}
slack.Attachments = []attachment{a}
return slack.postToSlack()
}
func (slack *SlackNotifier) postToSlack() bool {
data, err := json.Marshal(slack)
if err != nil {
log.Println("Unable to marshal slack payload:", err)
return false
}
log.Debugf("struct = %+v, json = %s", slack, string(data))
b := bytes.NewBuffer(data)
if res, err := http.Post(slack.Url, "application/json", b); err != nil {
log.Println("Unable to send data to slack:", err)
return false
} else {
defer res.Body.Close()
statusCode := res.StatusCode
if statusCode != 200 {
body, _ := ioutil.ReadAll(res.Body)
log.Println("Unable to notify slack:", string(body))
return false
} else {
log.Println("Slack notification sent.")
return true
}
}
}