Initial implementation of Discord notifications
lowfive committed
Jan 22, 2017 at 23:07 UTC
7026a6d1fd9990dbe92c35bad2530a88a4b05101
2 files changed
+130
-1
conf.d/health_alarm_notify.conf
+33
-1
@@ -7,6 +7,7 @@
7
# - e-mails (using the sendmail command),
8
# - push notifications to your mobile phone (pushover.net),
9
# - messages to your slack team (slack.com),
10
+# - messages to your discord guild (discordapp.com),
11
# - messages to your telegram chat / group chat (telegram.org)
12
# - sms messages to your cell phone or any sms enabled device (twilio.com)
13
# - sms messages to your cell phone or any sms enabled device (messagebird.com)
@@ -22,7 +23,7 @@
23
# proxy configuration
24
#
25
# If you need to send curl based notifications (pushover, pushbullet, slack,
25
-# telegram) via a proxy, set these to your proxy address:
26
+# discord, telegram) via a proxy, set these to your proxy address:
27
#export http_proxy="http://10.0.0.1:3128/"
28
#export https_proxy="http://10.0.0.1:3128/"
29
@@ -62,6 +63,7 @@ curl=""
63
# - pushover user tokens
64
# - telegram chat ids
65
# - slack channels
66
+# - discord channels
67
# - hipchat rooms
68
# - sms phone numbers
69
# - pagerduty.com (pd) services
@@ -75,6 +77,7 @@ curl=""
77
# pushover : "2987343...9437837 8756278...2362736|critical"
78
# telegram : "111827421 112746832|critical"
79
# slack : "alarms disasters|critical"
80
+# discord : "alarms disasters|critical"
81
# twilio : "+15555555555 +17777777777|critical"
82
# messagebird: "+15555555555 +17777777777|critical"
83
# pd : "<pd_service_key_1> <pd_service_key_2>|critical"
@@ -227,6 +230,25 @@ SLACK_WEBHOOK_URL=""
230
DEFAULT_RECIPIENT_SLACK=""
231
232
233
+#------------------------------------------------------------------------------
234
+# discord (discordapp.com) global notification options
235
+
236
+# multiple recipients can be given like this:
237
+# "CHANNEL1 CHANNEL2 ..."
238
+
239
+# enable/disable sending discord notifications
240
+SEND_DISCORD="YES"
241
+
242
+# Create a webhook by following the official documentation -
243
+# https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks
244
+DISCORD_WEBHOOK_URL=""
245
+
246
+# if a role's recipients are not configured, a notification will be send to
247
+# this discord channel (empty = do not send a notification for unconfigured
248
+# roles):
249
+DEFAULT_RECIPIENT_DISCORD=""
250
+
251
+
252
#------------------------------------------------------------------------------
253
# hipchat global notification options
254
@@ -295,6 +317,8 @@ role_recipients_telegram[sysadmin]="${DEFAULT_RECIPIENT_TELEGRAM}"
317
318
role_recipients_slack[sysadmin]="${DEFAULT_RECIPIENT_SLACK}"
319
320
+role_recipients_discord[sysadmin]="${DEFAULT_RECIPIENT_DISCORD}"
321
+
322
role_recipients_hipchat[sysadmin]="${DEFAULT_RECIPIENT_HIPCHAT}"
323
324
role_recipients_twilio[sysadmin]="${DEFAULT_RECIPIENT_TWILIO}"
@@ -316,6 +340,8 @@ role_recipients_telegram[domainadmin]="${DEFAULT_RECIPIENT_TELEGRAM}"
340
341
role_recipients_slack[domainadmin]="${DEFAULT_RECIPIENT_SLACK}"
342
343
+role_recipients_discord[domainadmin]="${DEFAULT_RECIPIENT_DISCORD}"
344
+
345
role_recipients_hipchat[domainadmin]="${DEFAULT_RECIPIENT_HIPCHAT}"
346
347
role_recipients_twilio[domainadmin]="${DEFAULT_RECIPIENT_TWILIO}"
@@ -338,6 +364,8 @@ role_recipients_telegram[dba]="${DEFAULT_RECIPIENT_TELEGRAM}"
364
365
role_recipients_slack[dba]="${DEFAULT_RECIPIENT_SLACK}"
366
367
+role_recipients_discord[dba]="${DEFAULT_RECIPIENT_DISCORD}"
368
+
369
role_recipients_hipchat[dba]="${DEFAULT_RECIPIENT_HIPCHAT}"
370
371
role_recipients_twilio[dba]="${DEFAULT_RECIPIENT_TWILIO}"
@@ -360,6 +388,8 @@ role_recipients_telegram[webmaster]="${DEFAULT_RECIPIENT_TELEGRAM}"
388
389
role_recipients_slack[webmaster]="${DEFAULT_RECIPIENT_SLACK}"
390
391
+role_recipients_discord[webmaster]="${DEFAULT_RECIPIENT_DISCORD}"
392
+
393
role_recipients_hipchat[webmaster]="${DEFAULT_RECIPIENT_HIPCHAT}"
394
395
role_recipients_twilio[webmaster]="${DEFAULT_RECIPIENT_TWILIO}"
@@ -382,6 +412,8 @@ role_recipients_telegram[proxyadmin]="${DEFAULT_RECIPIENT_TELEGRAM}"
412
413
role_recipients_slack[proxyadmin]="${DEFAULT_RECIPIENT_SLACK}"
414
415
+role_recipients_discord[proxyadmin]="${DEFAULT_RECIPIENT_DISCORD}"
416
+
417
role_recipients_hipchat[proxyadmin]="${DEFAULT_RECIPIENT_HIPCHAT}"
418
419
role_recipients_twilio[proxyadmin]="${DEFAULT_RECIPIENT_TWILIO}"
plugins.d/alarm-notify.sh
+97
@@ -16,6 +16,7 @@
16
# Supported notification methods:
17
# - emails by @ktsaou
18
# - slack.com notifications by @ktsaou
19
+# - discordapp.com notifications by @lowfive
20
# - pushover.net notifications by @ktsaou
21
# - pushbullet.com push notifications by Tiago Peralta @tperalta82 PR #1070
22
# - telegram.org notifications by @hashworks PR #1002
@@ -172,6 +173,7 @@ sendmail=
173
174
# enable / disable features
175
SEND_SLACK="YES"
176
+SEND_DISCORD="YES"
177
SEND_PUSHOVER="YES"
178
SEND_TWILIO="YES"
179
SEND_HIPCHAT="YES"
@@ -187,6 +189,11 @@ SLACK_WEBHOOK_URL=
189
DEFAULT_RECIPIENT_SLACK=
190
declare -A role_recipients_slack=()
191
192
+# discord configs
193
+DISCORD_WEBHOOK_URL=
194
+DEFAULT_RECIPIENT_DISCORD=
195
+declare -A role_recipients_discord=()
196
+
197
# pushover configs
198
PUSHOVER_APP_TOKEN=
199
DEFAULT_RECIPIENT_PUSHOVER=
@@ -283,6 +290,7 @@ filter_recipient_by_criticality() {
290
# find the recipients' addresses per method
291
292
declare -A arr_slack=()
293
+declare -A arr_discord=()
294
declare -A arr_pushover=()
295
declare -A arr_pushbullet=()
296
declare -A arr_twilio=()
@@ -363,6 +371,14 @@ do
371
[ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
372
done
373
374
+ # discord
375
+ a="${role_recipients_discord[${x}]}"
376
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_DISCORD}"
377
+ for r in ${a//,/ }
378
+ do
379
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality discord "${r}" && arr_discord[${r/|*/}]="1"
380
+ done
381
+
382
# pagerduty.com
383
a="${role_recipients_pd[${x}]}"
384
[ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PD}"
@@ -376,6 +392,10 @@ done
392
to_slack="${!arr_slack[*]}"
393
[ -z "${to_slack}" ] && SEND_SLACK="NO"
394
395
+# build the list of discord recipients (channels)
396
+to_discord="${!arr_discord[*]}"
397
+[ -z "${to_discord}" ] && SEND_DISCORD="NO"
398
+
399
# build the list of pushover recipients (user tokens)
400
to_pushover="${!arr_pushover[*]}"
401
[ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
@@ -420,6 +440,9 @@ done
440
# check slack
441
[ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
442
443
+# check discord
444
+[ -z "${DISCORD_WEBHOOK_URL}" ] && SEND_DISCORD="NO"
445
+
446
# check pushover
447
[ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
448
@@ -459,6 +482,7 @@ fi
482
if [ \( \
483
"${SEND_PUSHOVER}" = "YES" \
484
-o "${SEND_SLACK}" = "YES" \
485
+ -o "${SEND_DISCORD}" = "YES" \
486
-o "${SEND_HIPCHAT}" = "YES" \
487
-o "${SEND_TWILIO}" = "YES" \
488
-o "${SEND_MESSAGEBIRD}" = "YES" \
@@ -476,6 +500,7 @@ if [ \( \
500
SEND_PUSHBULLET="NO"
501
SEND_TELEGRAM="NO"
502
SEND_SLACK="NO"
503
+ SEND_DISCORD="NO"
504
SEND_TWILIO="NO"
505
SEND_HIPCHAT="NO"
506
SEND_MESSAGEBIRD="NO"
@@ -495,6 +520,7 @@ if [ "${SEND_EMAIL}" != "YES" \
520
-a "${SEND_PUSHOVER}" != "YES" \
521
-a "${SEND_TELEGRAM}" != "YES" \
522
-a "${SEND_SLACK}" != "YES" \
523
+ -a "${SEND_DISCORD}" != "YES" \
524
-a "${SEND_TWILIO}" != "YES" \
525
-a "${SEND_HIPCHAT}" != "YES" \
526
-a "${SEND_MESSAGEBIRD}" != "YES" \
@@ -1008,6 +1034,66 @@ EOF
1034
return 1
1035
}
1036
1037
+# -----------------------------------------------------------------------------
1038
+# discord sender
1039
+
1040
+send_discord() {
1041
+ local webhook="${1}/slack" channels="${2}" httpcode sent=0 channel color payload
1042
+
1043
+ [ "${SEND_DISCORD}" != "YES" ] && return 1
1044
+
1045
+ case "${status}" in
1046
+ WARNING) color="warning" ;;
1047
+ CRITICAL) color="danger" ;;
1048
+ CLEAR) color="good" ;;
1049
+ *) color="#777777" ;;
1050
+ esac
1051
+
1052
+ for channel in ${channels}
1053
+ do
1054
+ payload="$(cat <<EOF
1055
+ {
1056
+ "channel": "#${channel}",
1057
+ "username": "netdata on ${host}",
1058
+ "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
1059
+ "icon_url": "https://discordapp.com/assets/f78426a064bc9dd24847519259bc42af.png",
1060
+ "attachments": [
1061
+ {
1062
+ "color": "${color}",
1063
+ "title": "${alarm}",
1064
+ "title_link": "${goto_url}",
1065
+ "text": "${info}",
1066
+ "fields": [
1067
+ {
1068
+ "title": "${chart}",
1069
+ "value": "${family}"
1070
+ }
1071
+ ],
1072
+ "thumb_url": "${image}",
1073
+ "footer_icon": "${images_base_url}/images/seo-performance-128.png",
1074
+ "footer": "${host}",
1075
+ "ts": ${when}
1076
+ }
1077
+ ]
1078
+ }
1079
+EOF
1080
+ )"
1081
+
1082
+ httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1083
+ if [ "${httpcode}" == "200" ]
1084
+ then
1085
+ info "sent discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1086
+ sent=$((sent + 1))
1087
+ else
1088
+ error "failed to send discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1089
+ fi
1090
+ done
1091
+
1092
+ [ ${sent} -gt 0 ] && return 0
1093
+
1094
+ return 1
1095
+}
1096
+
1097
1098
# -----------------------------------------------------------------------------
1099
# prepare the content of the notification
@@ -1108,6 +1194,15 @@ raised_for_html=
1194
send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
1195
SENT_SLACK=$?
1196
1197
+# -----------------------------------------------------------------------------
1198
+# send the discord notification
1199
+
1200
+# discord aggregates posts from the same username
1201
+# so we use "${host} ${status}" as the bot username, to make them diff
1202
+
1203
+send_discord "${DISCORD_WEBHOOK_URL}" "${to_discord}"
1204
+SENT_DISCORD=$?
1205
+
1206
# -----------------------------------------------------------------------------
1207
# send the pushover notification
1208
@@ -1301,6 +1396,7 @@ if [ ${SENT_EMAIL} -eq 0 \
1396
-o ${SENT_PUSHOVER} -eq 0 \
1397
-o ${SENT_TELEGRAM} -eq 0 \
1398
-o ${SENT_SLACK} -eq 0 \
1399
+ -o ${SENT_DISCORD} -eq 0 \
1400
-o ${SENT_TWILIO} -eq 0 \
1401
-o ${SENT_HIPCHAT} -eq 0 \
1402
-o ${SENT_MESSAGEBIRD} -eq 0 \
@@ -1315,3 +1411,4 @@ fi
1411
1412
# we did not send anything
1413
exit 1
1414
+