/
main.go
200 lines (181 loc) · 4.92 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu-community/sensu-plugin-sdk/templates"
"github.com/sensu/sensu-go/types"
)
// Config represents the handler plugin config.
type Config struct {
sensu.PluginConfig
discordWebHookURL string
discordCustomUsername string
discordCustomAvatarURL string
discordDescriptionTemplate string
discordAlertCritical bool
discordAlertMention string
}
const (
webHookURL = "webhook-url"
customUsername = "custom-username"
customAvatarURL = "custom-avatar-url"
descriptionTemplate = "description-template"
alertCritical = "alert-on-critical"
alertMention = "alert-mention"
defaultTemplate = "{{ .Check.Output }}"
defaultAlert = false
defaultAlertMention = "@everyone"
)
var (
plugin = Config{
PluginConfig: sensu.PluginConfig{
Name: "sensu-discord-handler",
Short: "The Sensu Go Discord handler for notifying a channel.",
Keyspace: "sensu.io/plugins/sensu-discord-handler/config",
},
}
options = []*sensu.PluginConfigOption{
{
Path: webHookURL,
Env: "DISCORD_WEBHOOK_URL",
Argument: webHookURL,
Shorthand: "w",
Secret: true,
Usage: "The WebHook URL to send messages to",
Value: &plugin.discordWebHookURL,
},
{
Path: customUsername,
Env: "DISCORD_CUSTOM_USERNAME",
Argument: customUsername,
Shorthand: "u",
Default: "",
Usage: "The username that messages will be sent as",
Value: &plugin.discordCustomUsername,
},
{
Path: customAvatarURL,
Env: "DISCORD_CUSTOM_AVATAR_URL",
Argument: customAvatarURL,
Shorthand: "i",
Default: "",
Usage: "A URL to an image to use as the user avatar",
Value: &plugin.discordCustomAvatarURL,
},
{
Path: descriptionTemplate,
Env: "DISCORD_DESCRIPTION_TEMPLATE",
Argument: descriptionTemplate,
Shorthand: "t",
Default: defaultTemplate,
Usage: "The Discord notification output template, in Golang text/template format",
Value: &plugin.discordDescriptionTemplate,
},
{
Path: alertCritical,
Env: "DISCORD_ALERT_ON_CRITICAL",
Argument: alertCritical,
Shorthand: "a",
Default: defaultAlert,
Usage: "The Discord notification will alert the channel with a specified mentions (--alert-mention)",
Value: &plugin.discordAlertCritical,
},
{
Path: alertMention,
Env: "DISCORD_ALERT_MENTION",
Argument: alertMention,
Shorthand: "m",
Default: defaultAlertMention,
Usage: "Specifies the mentions to use if --alert-on-critical is enabled",
Value: &plugin.discordAlertMention,
},
}
)
func main() {
handler := sensu.NewGoHandler(&plugin.PluginConfig, options, checkArgs, executeHandler)
handler.Execute()
}
func checkArgs(_ *types.Event) error {
if len(plugin.discordWebHookURL) == 0 {
return fmt.Errorf("--%s or DISCORD_WEBHOOK_URL environment variable is required", webHookURL)
}
return nil
}
func messageColor(event *types.Event) int {
switch event.Check.Status {
case 0:
return 3061373
case 2:
return 14687834
default:
return 15512110
}
}
func messageStatus(event *types.Event) string {
switch event.Check.Status {
case 0:
return "Resolved"
case 2:
if plugin.discordAlertCritical {
return fmt.Sprintf("%s Critical", plugin.discordAlertMention)
} else {
return "Critical"
}
default:
return "Warning"
}
}
func limitTextLength(text string, limit int) string {
textRune := []rune(text)
if len(textRune) > limit {
return string(textRune[0:limit-3]) + "..."
}
return text
}
func messageEmbed(event *types.Event) *discordgo.MessageEmbed {
description, err := templates.EvalTemplate("description", plugin.discordDescriptionTemplate, event)
if err != nil {
fmt.Printf("%s: Error processing template: %s", plugin.PluginConfig.Name, err)
}
description = strings.Replace(description, `\n`, "\n", -1)
embed := &discordgo.MessageEmbed{
Title: "Description",
Description: limitTextLength(description, 4096),
Color: messageColor(event),
Fields: []*discordgo.MessageEmbedField{
{
Name: "Status",
Value: messageStatus(event),
Inline: false,
},
{
Name: "Entity",
Value: event.Entity.Name,
Inline: true,
},
{
Name: "Check",
Value: event.Check.Name,
Inline: true,
},
},
}
return embed
}
func executeHandler(event *types.Event) error {
embedJSON, err := json.Marshal(messageEmbed(event))
if err != nil {
return fmt.Errorf("%v", err)
}
resp, err := http.Post(plugin.discordWebHookURL, "application/json", strings.NewReader(fmt.Sprintf("{\"embeds\": [%s]}", string(embedJSON))))
if err != nil {
return fmt.Errorf("%v", err)
}
defer resp.Body.Close()
fmt.Print("Notification sent to Discord WebHook destination\n")
return nil
}