adding Microsoft Team support for alert notification
Guillaume Hospital committed
Jun 28, 2018 at 17:10 UTC
79f5e60cfaa6f29092551daf83b6669eea227ca4
2 files changed
+127
-1
conf.d/health_alarm_notify.conf
+31
@@ -16,6 +16,7 @@
16
# - notifications to users on pagerduty.com
17
# - messages to your irc channel on your selected network
18
# - messages to a local or remote syslog daemon
19
+# - message to Microsoft Team (through webhook)
20
#
21
# The 'to' line given at netdata alarms defines a *role*, so that many
22
# people can be notified for each role.
@@ -332,6 +333,24 @@ SLACK_WEBHOOK_URL=""
333
# roles):
334
DEFAULT_RECIPIENT_SLACK=""
335
336
+#------------------------------------------------------------------------------
337
+# MS Team (teams.com) global notification options
338
+# More details are available here regarding the payload syntax options : https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference
339
+# Online designer : https://acdesignerbeta.azurewebsites.net/
340
+# multiple recipients can be given like this:
341
+# "CHANNEL1 CHANNEL2 ..."
342
+
343
+# enable/disable sending team notifications
344
+SEND_TEAM="YES"
345
+
346
+TEAM_WEBHOOK_URL=""
347
+
348
+# if a role's recipients are not configured, a notification will be send to
349
+# this slack channel (empty = do not send a notification for unconfigured
350
+# roles):
351
+# For team the channel name is encoded in the URI after ....IncomingWebhook/___/.....
352
+DEFAULT_RECIPIENT_TEAM=""
353
+
354
#------------------------------------------------------------------------------
355
# rocketchat (rocket.chat) global notification options
356
@@ -685,6 +704,8 @@ role_recipients_syslog[sysadmin]="${DEFAULT_RECIPIENT_SYSLOG}"
704
705
role_recipients_custom[sysadmin]="${DEFAULT_RECIPIENT_CUSTOM}"
706
707
+role_recipients_team[sysadmin]="${DEFAULT_RECIPIENT_TEAM}"
708
+
709
# -----------------------------------------------------------------------------
710
# DNS related alarms
711
@@ -722,6 +743,8 @@ role_recipients_syslog[domainadmin]="${DEFAULT_RECIPIENT_SYSLOG}"
743
744
role_recipients_custom[domainadmin]="${DEFAULT_RECIPIENT_CUSTOM}"
745
746
+role_recipients_team[domainadmin]="${DEFAULT_RECIPIENT_TEAM}"
747
+
748
# -----------------------------------------------------------------------------
749
# database servers alarms
750
# mysql, redis, memcached, postgres, etc
@@ -760,6 +783,8 @@ role_recipients_syslog[dba]="${DEFAULT_RECIPIENT_SYSLOG}"
783
784
role_recipients_custom[dba]="${DEFAULT_RECIPIENT_CUSTOM}"
785
786
+role_recipients_team[dba]="${DEFAULT_RECIPIENT_TEAM}"
787
+
788
# -----------------------------------------------------------------------------
789
# web servers alarms
790
# apache, nginx, lighttpd, etc
@@ -798,6 +823,8 @@ role_recipients_syslog[webmaster]="${DEFAULT_RECIPIENT_SYSLOG}"
823
824
role_recipients_custom[webmaster]="${DEFAULT_RECIPIENT_CUSTOM}"
825
826
+role_recipients_team[webmaster]="${DEFAULT_RECIPIENT_TEAM}"
827
+
828
# -----------------------------------------------------------------------------
829
# proxy servers alarms
830
# squid, etc
@@ -836,6 +863,8 @@ role_recipients_syslog[proxyadmin]="${DEFAULT_RECIPIENT_SYSLOG}"
863
864
role_recipients_custom[proxyadmin]="${DEFAULT_RECIPIENT_CUSTOM}"
865
866
+role_recipients_team[proxyadmin]="${DEFAULT_RECIPIENT_TEAM}"
867
+
868
# -----------------------------------------------------------------------------
869
# peripheral devices
870
# UPS, photovoltaics, etc
@@ -871,3 +900,5 @@ role_recipients_fleep[sitemgr]="${DEFAULT_RECIPIENT_FLEEP}"
900
role_recipients_syslog[sitemgr]="${DEFAULT_RECIPIENT_SYSLOG}"
901
902
role_recipients_custom[sitemgr]="${DEFAULT_RECIPIENT_CUSTOM}"
903
+
904
+role_recipients_team[sitemgr]="${DEFAULT_RECIPIENT_TEAM}"
\ No newline at end of file
plugins.d/alarm-notify.sh
+96
-1
@@ -29,6 +29,7 @@
29
# - fleep notifications by @Ferroin
30
# - custom notifications by @ktsaou
31
# - syslog messages by @Ferroin
32
+# - MS Team notification @tioumen @Michelin
33
34
# -----------------------------------------------------------------------------
35
# testing notifications
@@ -218,7 +219,7 @@ fi
219
images_base_url="https://registry.my-netdata.io"
220
221
# curl options to use
221
-curl_options=
222
+curl_options="--insecure"
223
224
# needed commands
225
# if empty they will be searched in the system path
@@ -227,6 +228,7 @@ sendmail=
228
229
# enable / disable features
230
SEND_SLACK="YES"
231
+SEND_TEAM="YES"
232
SEND_ALERTA="YES"
233
SEND_FLOCK="YES"
234
SEND_DISCORD="YES"
@@ -250,6 +252,11 @@ SLACK_WEBHOOK_URL=
252
DEFAULT_RECIPIENT_SLACK=
253
declare -A role_recipients_slack=()
254
255
+# Microsoft Team configs
256
+TEAM_WEBHOOK_URL=
257
+DEFAULT_RECIPIENT_TEAM=
258
+declare -A role_recipients_team=()
259
+
260
# rocketchat configs
261
ROCKETCHAT_WEBHOOK_URL=
262
DEFAULT_RECIPIENT_ROCKETCHAT=
@@ -426,6 +433,7 @@ filter_recipient_by_criticality() {
433
# find the recipients' addresses per method
434
435
declare -A arr_slack=()
436
+declare -A arr_team=()
437
declare -A arr_rocketchat=()
438
declare -A arr_alerta=()
439
declare -A arr_flock=()
@@ -524,6 +532,14 @@ do
532
[ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
533
done
534
535
+ # Team
536
+ a="${role_recipients_team[${x}]}"
537
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TEAM}"
538
+ for r in ${a//,/ }
539
+ do
540
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality team "${r}" && arr_team[${r/|*/}]="1"
541
+ done
542
+
543
# rocketchat
544
a="${role_recipients_rocketchat[${x}]}"
545
[ -z "${a}" ] && a="${DEFAULT_RECIPIENT_ROCKETCHAT}"
@@ -602,6 +618,10 @@ done
618
to_slack="${!arr_slack[*]}"
619
[ -z "${to_slack}" ] && SEND_SLACK="NO"
620
621
+# build the list of team recipients (channels)
622
+to_team="${!arr_team[*]}"
623
+[ -z "${to_team}" ] && SEND_TEAM="NO"
624
+
625
# build the list of rocketchat recipients (channels)
626
to_rocketchat="${!arr_rocketchat[*]}"
627
[ -z "${to_rocketchat}" ] && SEND_ROCKETCHAT="NO"
@@ -753,6 +773,7 @@ if [ \( \
773
-o "${SEND_KAFKA}" = "YES" \
774
-o "${SEND_FLEEP}" = "YES" \
775
-o "${SEND_CUSTOM}" = "YES" \
776
+ -o "${SEND_TEAM}" = "YES" \
777
\) -a -z "${curl}" ]
778
then
779
curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
@@ -763,6 +784,7 @@ if [ \( \
784
SEND_PUSHBULLET="NO"
785
SEND_TELEGRAM="NO"
786
SEND_SLACK="NO"
787
+ SEND_TEAM="NO"
788
SEND_ROCKETCHAT="NO"
789
SEND_ALERTA="NO"
790
SEND_FLOCK="NO"
@@ -1339,6 +1361,69 @@ send_telegram() {
1361
}
1362
1363
# -----------------------------------------------------------------------------
1364
+# Team sender
1365
+
1366
+send_team() {
1367
+echo "entering in send_team function"
1368
+ local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
1369
+
1370
+ [ "${SEND_TEAM}" != "YES" ] && return 1
1371
+
1372
+ case "${status}" in
1373
+ WARNING) icon="⚠️" && color="FFA500";;
1374
+ CRITICAL) icon="🔥" && color="D93F3C";;
1375
+ CLEAR) icon="💚" && color="65A677";;
1376
+ *) icon="♡" && color="0076D7";;
1377
+ esac
1378
+
1379
+ for channels in ${channels}
1380
+ do
1381
+ ## More details are available here regarding the payload syntax options : https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference
1382
+ ## Online designer : https://acdesignerbeta.azurewebsites.net/
1383
+ payload="$(cat <<EOF
1384
+ {
1385
+ "@context": "http://schema.org/extensions",
1386
+ "@type": "MessageCard",
1387
+ "themeColor": "${color}",
1388
+ "title": "$icon Alert ${status} from netdata for ${host}",
1389
+ "text": "${host} ${status_message}, ${chart} (_${family}_), *${alarm}*",
1390
+ "potentialAction": [
1391
+ {
1392
+ "@type": "OpenUri",
1393
+ "name": "Netdata",
1394
+ "targets": [
1395
+ { "os": "default", "uri": "http://$(hostname -f):19999" }
1396
+ ]
1397
+ },
1398
+ {
1399
+ "@type": "OpenUri",
1400
+ "name": "Splunk",
1401
+ "targets": [
1402
+ { "os": "default", "uri": "https://your_url.com" }
1403
+ ]
1404
+ }
1405
+ ]
1406
+ }
1407
+EOF
1408
+ )"
1409
+
1410
+ httpcode=$(docurl -H "Content-Type: application/json" -d "${payload}" "${webhook}")
1411
+
1412
+ if [ "${httpcode}" = "200" ]
1413
+ then
1414
+ info "sent team notification for: ${host} ${chart}.${name} is ${status} to '${webhook}'"
1415
+ sent=$((sent + 1))
1416
+ else
1417
+ error "failed to send team notification for: ${host} ${chart}.${name} is ${status} to '${webhook}', with HTTP error code ${httpcode}."
1418
+ fi
1419
+ done
1420
+
1421
+ [ ${sent} -gt 0 ] && return 0
1422
+
1423
+ return 1
1424
+}
1425
+
1426
+
1427
# slack sender
1428
1429
send_slack() {
@@ -1874,6 +1959,15 @@ raised_for_html=
1959
send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
1960
SENT_SLACK=$?
1961
1962
+# -----------------------------------------------------------------------------
1963
+# send the team notification
1964
+
1965
+# team aggregates posts from the same username
1966
+# so we use "${host} ${status}" as the bot username, to make them diff
1967
+
1968
+send_team "${TEAM_WEBHOOK_URL}" "${to_team}"
1969
+SENT_TEAM=$?
1970
+
1971
# -----------------------------------------------------------------------------
1972
# send the rocketchat notification
1973
@@ -2185,6 +2279,7 @@ if [ ${SENT_EMAIL} -eq 0 \
2279
-o ${SENT_PUSHOVER} -eq 0 \
2280
-o ${SENT_TELEGRAM} -eq 0 \
2281
-o ${SENT_SLACK} -eq 0 \
2282
+ -o ${SENT_TEAM} -eq 0 \
2283
-o ${SENT_ROCKETCHAT} -eq 0 \
2284
-o ${SENT_ALERTA} -eq 0 \
2285
-o ${SENT_FLOCK} -eq 0 \