-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
238 lines (215 loc) · 5.99 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"fmt"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu-community/sensu-plugin-sdk/templates"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/slack-go/slack"
"os"
"strings"
)
// HandlerConfig contains the Slack handler configuration
type HandlerConfig struct {
sensu.PluginConfig
slackwebHookURL string
slackChannel string
slackUsername string
slackIconURL string
slackDescriptionTemplate string
slackAlertCritical bool
}
const (
webHookURL = "webhook-url"
channel = "channel"
username = "username"
iconURL = "icon-url"
descriptionTemplate = "description-template"
alertCritical = "alert-on-critical"
defaultChannel = "#general"
defaultIconURL = "https://www.sensu.io/img/sensu-logo.png"
defaultUsername = "sensu"
defaultTemplate = "{{ .Check.Output }}"
defaultAlert bool = false
)
var (
config = HandlerConfig{
PluginConfig: sensu.PluginConfig{
Name: "sensu-slack-handler",
Short: "The Sensu Go Slack handler for notifying a channel",
Keyspace: "sensu.io/plugins/slack/config",
},
}
slackConfigOptions = []*sensu.PluginConfigOption{
{
Path: webHookURL,
Env: "SLACK_WEBHOOK_URL",
Argument: webHookURL,
Shorthand: "w",
Secret: true,
Usage: "The webhook url to send messages to",
Value: &config.slackwebHookURL,
},
{
Path: channel,
Env: "SLACK_CHANNEL",
Argument: channel,
Shorthand: "c",
Default: defaultChannel,
Usage: "The channel to post messages to",
Value: &config.slackChannel,
},
{
Path: username,
Env: "SLACK_USERNAME",
Argument: username,
Shorthand: "u",
Default: defaultUsername,
Usage: "The username that messages will be sent as",
Value: &config.slackUsername,
},
{
Path: iconURL,
Env: "SLACK_ICON_URL",
Argument: iconURL,
Shorthand: "i",
Default: defaultIconURL,
Usage: "A URL to an image to use as the user avatar",
Value: &config.slackIconURL,
},
{
Path: descriptionTemplate,
Env: "SLACK_DESCRIPTION_TEMPLATE",
Argument: descriptionTemplate,
Shorthand: "t",
Default: defaultTemplate,
Usage: "The Slack notification output template, in Golang text/template format",
Value: &config.slackDescriptionTemplate,
},
{
Path: alertCritical,
Env: "SLACK_ALERT_ON_CRITICAL",
Argument: alertCritical,
Shorthand: "a",
Default: defaultAlert,
Usage: "The Slack notification will alert the channel with @channel",
Value: &config.slackAlertCritical,
},
}
)
func main() {
goHandler := sensu.NewGoHandler(&config.PluginConfig, slackConfigOptions, checkArgs, sendMessage)
goHandler.Execute()
}
func checkArgs(_ *corev2.Event) error {
// Support deprecated environment variables
if webhook := os.Getenv("SENSU_SLACK_WEBHOOK_URL"); webhook != "" {
config.slackwebHookURL = webhook
}
if channel := os.Getenv("SENSU_SLACK_CHANNEL"); channel != "" && config.slackChannel == defaultChannel {
config.slackChannel = channel
}
if username := os.Getenv("SENSU_SLACK_USERNAME"); username != "" && config.slackUsername == defaultUsername {
config.slackUsername = username
}
if icon := os.Getenv("SENSU_SLACK_ICON_URL"); icon != "" && config.slackIconURL == defaultIconURL {
config.slackIconURL = icon
}
if len(config.slackwebHookURL) == 0 {
return fmt.Errorf("--%s or SLACK_WEBHOOK_URL environment variable is required", webHookURL)
}
return nil
}
func formattedEventAction(event *corev2.Event) string {
switch event.Check.Status {
case 0:
return "RESOLVED"
default:
return "ALERT"
}
}
func chomp(s string) string {
return strings.Trim(strings.Trim(strings.Trim(s, "\n"), "\r"), "\r\n")
}
func eventKey(event *corev2.Event) string {
return fmt.Sprintf("%s/%s", event.Entity.Name, event.Check.Name)
}
func eventSummary(event *corev2.Event, maxLength int) string {
output := chomp(event.Check.Output)
if len(event.Check.Output) > maxLength {
output = output[0:maxLength] + "..."
}
return fmt.Sprintf("%s:%s", eventKey(event), output)
}
func formattedMessage(event *corev2.Event) string {
return fmt.Sprintf("%s - %s", formattedEventAction(event), eventSummary(event, 100))
}
func messageColor(event *corev2.Event) string {
switch event.Check.Status {
case 0:
return "good"
case 2:
return "danger"
default:
return "warning"
}
}
func messageStatus(event *corev2.Event) string {
switch event.Check.Status {
case 0:
return "Resolved"
case 2:
if config.slackAlertCritical {
return "<!channel> Critical"
} else {
return "Critical"
}
default:
return "Warning"
}
}
func messageAttachment(event *corev2.Event) slack.Attachment {
description, err := templates.EvalTemplate("description", config.slackDescriptionTemplate, event)
if err != nil {
fmt.Printf("%s: Error processing template: %s", config.PluginConfig.Name, err)
}
description = strings.Replace(description, `\n`, "\n", -1)
attachment := slack.Attachment{
Title: "Description",
Text: description,
Fallback: formattedMessage(event),
Color: messageColor(event),
Fields: []slack.AttachmentField{
{
Title: "Status",
Value: messageStatus(event),
Short: false,
},
{
Title: "Entity",
Value: event.Entity.Name,
Short: true,
},
{
Title: "Check",
Value: event.Check.Name,
Short: true,
},
},
}
return attachment
}
func sendMessage(event *corev2.Event) error {
hookmsg := &slack.WebhookMessage{
Attachments: []slack.Attachment{messageAttachment(event)},
Channel: config.slackChannel,
IconURL: config.slackIconURL,
Username: config.slackUsername,
}
err := slack.PostWebhook(config.slackwebHookURL, hookmsg)
if err != nil {
return fmt.Errorf("Failed to send Slack message: %v", err)
}
// FUTURE: send to AH
fmt.Printf("Notification sent to Slack channel %s\n", config.slackChannel)
return nil
}