Add rocketchat integration
Dennis Hermsmeier committed
Jun 1, 2018 at 10:13 UTC
c984745d42ee2e3544b09b3929039ca65890b963
2 files changed
+118
conf.d/health_alarm_notify.conf
+19
@@ -332,6 +332,25 @@ SLACK_WEBHOOK_URL=""
332
# roles):
333
DEFAULT_RECIPIENT_SLACK=""
334
335
+#------------------------------------------------------------------------------
336
+# rocketchat (rocket.chat) global notification options
337
+
338
+# multiple recipients can be given like this:
339
+# "CHANNEL1 CHANNEL2 ..."
340
+
341
+# enable/disable sending slack notifications
342
+SEND_ROCKETCHAT="YES"
343
+
344
+# Login to rocket.chat and create an incoming webhook. You need only one for all
345
+# your netdata servers (or you can have one for each of your netdata).
346
+# Without it, netdata cannot send rocketchat notifications.
347
+ROCKETCHAT_WEBHOOK_URL=""
348
+
349
+# if a role's recipients are not configured, a notification will be send to
350
+# this slack channel (empty = do not send a notification for unconfigured
351
+# roles):
352
+DEFAULT_RECIPIENT_ROCKETCHAT=""
353
+
354
355
#------------------------------------------------------------------------------
356
# alerta (alerta.io) global notification options
plugins.d/alarm-notify.sh
+99
@@ -248,6 +248,11 @@ SLACK_WEBHOOK_URL=
248
DEFAULT_RECIPIENT_SLACK=
249
declare -A role_recipients_slack=()
250
251
+# rocketchat configs
252
+ROCKETCHAT_WEBHOOK_URL=
253
+DEFAULT_RECIPIENT_ROCKETCHAT=
254
+declare -A role_recipients_rocketchat=()
255
+
256
# alerta configs
257
ALERTA_WEBHOOK_URL=
258
ALERTA_API_KEY=
@@ -414,6 +419,7 @@ filter_recipient_by_criticality() {
419
# find the recipients' addresses per method
420
421
declare -A arr_slack=()
422
+declare -A arr_rocketchat=()
423
declare -A arr_alerta=()
424
declare -A arr_flock=()
425
declare -A arr_discord=()
@@ -510,6 +516,14 @@ do
516
[ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
517
done
518
519
+ # rocketchat
520
+ a="${role_recipients_rocketchat[${x}]}"
521
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_ROCKETCHAT}"
522
+ for r in ${a//,/ }
523
+ do
524
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality rocketchat "${r}" && arr_rocketchat[${r/|*/}]="1"
525
+ done
526
+
527
# alerta
528
a="${role_recipients_alerta[${x}]}"
529
[ -z "${a}" ] && a="${DEFAULT_RECIPIENT_ALERTA}"
@@ -572,6 +586,10 @@ done
586
to_slack="${!arr_slack[*]}"
587
[ -z "${to_slack}" ] && SEND_SLACK="NO"
588
589
+# build the list of rocketchat recipients (channels)
590
+to_rocketchat="${!arr_rocketchat[*]}"
591
+[ -z "${to_rocketchat}" ] && SEND_ROCKETCHAT="NO"
592
+
593
# build the list of alerta recipients (channels)
594
to_alerta="${!arr_alerta[*]}"
595
[ -z "${to_alerta}" ] && SEND_ALERTA="NO"
@@ -643,6 +661,9 @@ to_syslog="${!arr_syslog[*]}"
661
# check slack
662
[ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
663
664
+# check rocketchat
665
+[ -z "${ROCKETCHAT_WEBHOOK_URL}" ] && SEND_ROCKETCHAT="NO"
666
+
667
# check alerta
668
[ -z "${ALERTA_WEBHOOK_URL}" ] && SEND_ALERTA="NO"
669
@@ -696,6 +717,7 @@ fi
717
if [ \( \
718
"${SEND_PUSHOVER}" = "YES" \
719
-o "${SEND_SLACK}" = "YES" \
720
+ -o "${SEND_ROCKETCHAT}" = "YES" \
721
-o "${SEND_ALERTA}" = "YES" \
722
-o "${SEND_FLOCK}" = "YES" \
723
-o "${SEND_DISCORD}" = "YES" \
@@ -717,6 +739,7 @@ if [ \( \
739
SEND_PUSHBULLET="NO"
740
SEND_TELEGRAM="NO"
741
SEND_SLACK="NO"
742
+ SEND_ROCKETCHAT="NO"
743
SEND_ALERTA="NO"
744
SEND_FLOCK="NO"
745
SEND_DISCORD="NO"
@@ -756,6 +779,7 @@ if [ "${SEND_EMAIL}" != "YES" \
779
-a "${SEND_PUSHOVER}" != "YES" \
780
-a "${SEND_TELEGRAM}" != "YES" \
781
-a "${SEND_SLACK}" != "YES" \
782
+ -a "${SEND_ROCKETCHAT}" != "YES" \
783
-a "${SEND_ALERTA}" != "YES" \
784
-a "${SEND_FLOCK}" != "YES" \
785
-a "${SEND_DISCORD}" != "YES" \
@@ -1349,6 +1373,71 @@ EOF
1373
return 1
1374
}
1375
1376
+
1377
+# -----------------------------------------------------------------------------
1378
+# rocketchat sender
1379
+
1380
+send_rocketchat() {
1381
+ local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
1382
+
1383
+ [ "${SEND_ROCKETCHAT}" != "YES" ] && return 1
1384
+
1385
+ case "${status}" in
1386
+ WARNING) color="warning" ;;
1387
+ CRITICAL) color="danger" ;;
1388
+ CLEAR) color="good" ;;
1389
+ *) color="#777777" ;;
1390
+ esac
1391
+
1392
+ for channel in ${channels}
1393
+ do
1394
+ payload="$(cat <<EOF
1395
+ {
1396
+ "channel": "#${channel}",
1397
+ "alias": "netdata on ${host}",
1398
+ "avatar": "${images_base_url}/images/seo-performance-128.png",
1399
+ "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
1400
+ "attachments": [
1401
+ {
1402
+ "color": "${color}",
1403
+ "title": "${alarm}",
1404
+ "title_link": "${goto_url}",
1405
+ "text": "${info}",
1406
+ "fields": [
1407
+ {
1408
+ "title": "${chart}",
1409
+ "short": true,
1410
+ "value": "chart"
1411
+ },
1412
+ {
1413
+ "title": "${family}",
1414
+ "short": true,
1415
+ "value": "family"
1416
+ }
1417
+ ],
1418
+ "thumb_url": "${image}",
1419
+ "ts": "${when}"
1420
+ }
1421
+ ]
1422
+ }
1423
+EOF
1424
+ )"
1425
+
1426
+ httpcode=$(docurl -X POST --data-urlencode "payload=${payload}" "${webhook}")
1427
+ if [ "${httpcode}" = "200" ]
1428
+ then
1429
+ info "sent rocketchat notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1430
+ sent=$((sent + 1))
1431
+ else
1432
+ error "failed to send rocketchat notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1433
+ fi
1434
+ done
1435
+
1436
+ [ ${sent} -gt 0 ] && return 0
1437
+
1438
+ return 1
1439
+}
1440
+
1441
# -----------------------------------------------------------------------------
1442
# alerta sender
1443
@@ -1726,6 +1815,15 @@ raised_for_html=
1815
send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
1816
SENT_SLACK=$?
1817
1818
+# -----------------------------------------------------------------------------
1819
+# send the rocketchat notification
1820
+
1821
+# rocketchat aggregates posts from the same username
1822
+# so we use "${host} ${status}" as the bot username, to make them diff
1823
+
1824
+send_rocketchat "${ROCKETCHAT_WEBHOOK_URL}" "${to_rocketchat}"
1825
+SENT_ROCKETCHAT=$?
1826
+
1827
# -----------------------------------------------------------------------------
1828
# send the alerta notification
1829
@@ -2022,6 +2120,7 @@ if [ ${SENT_EMAIL} -eq 0 \
2120
-o ${SENT_PUSHOVER} -eq 0 \
2121
-o ${SENT_TELEGRAM} -eq 0 \
2122
-o ${SENT_SLACK} -eq 0 \
2123
+ -o ${SENT_ROCKETCHAT} -eq 0 \
2124
-o ${SENT_ALERTA} -eq 0 \
2125
-o ${SENT_FLOCK} -eq 0 \
2126
-o ${SENT_DISCORD} -eq 0 \