Added IRC Notifications support.
manosf committed
Feb 22, 2018 at 22:40 UTC
f571148d6210b85bd9ab807e003891bb293ebd23
1 file changed
+71
-1
plugins.d/alarm-notify.sh
+71
-1
@@ -233,6 +233,7 @@ SEND_EMAIL="YES"
233
SEND_PUSHBULLET="YES"
234
SEND_KAFKA="YES"
235
SEND_PD="YES"
236
+SEND_IRC="YES"
237
SEND_CUSTOM="YES"
238
239
# slack configs
@@ -316,6 +317,13 @@ DEFAULT_RECIPIENT_EMAIL="root"
317
EMAIL_CHARSET=$(locale charmap 2>/dev/null)
318
declare -A role_recipients_email=()
319
320
+# irc configs
321
+IRC_NICKNAME=
322
+IRC_REALNAME=
323
+DEFAULT_RECIPIENT_IRC=
324
+DEFAULT_IRC_NETWORK="irc.freenode.net"
325
+declare -A role_recipients_irc=()
326
+
327
# load the user configuration
328
# this will overwrite the variables above
329
if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
@@ -407,6 +415,7 @@ declare -A arr_email=()
415
declare -A arr_custom=()
416
declare -A arr_messagebird=()
417
declare -A arr_kavenegar=()
418
+declare -A arr_irc=()
419
420
# netdata may call us with multiple roles, and roles may have multiple but
421
# overlapping recipients - so, here we find the unique recipients.
@@ -519,6 +528,14 @@ do
528
do
529
[ "${r}" != "disabled" ] && filter_recipient_by_criticality pd "${r}" && arr_pd[${r/|*/}]="1"
530
done
531
+
532
+ # irc
533
+ a="${role_recipients_irc[${x}]}"
534
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_IRC}"
535
+ for r in ${a//,/ }
536
+ do
537
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality irc "${r}" && arr_irc[${r/|*/}]="1"
538
+ done
539
540
# custom
541
a="${role_recipients_custom[${x}]}"
@@ -591,6 +608,9 @@ do
608
done
609
[ -z "${to_email}" ] && SEND_EMAIL="NO"
610
611
+# build the list of irc recipients (channels)
612
+to_irc="${!arr_irc[*]}"
613
+[ -z "${to_irc}" ] && SEND_IRC="NO"
614
615
# -----------------------------------------------------------------------------
616
# verify the delivery methods supported
@@ -631,6 +651,9 @@ done
651
# check kafka
652
[ -z "${KAFKA_URL}" -o -z "${KAFKA_SENDER_IP}" ] && SEND_KAFKA="NO"
653
654
+# check irc
655
+[ -z "${DEFAULT_IRC_NETWORK}" -o -z "${IRC_CHANNEL}" ] && SEND_IRC="NO"
656
+
657
# check pagerduty.com
658
# if we need pd-send, check for the pd-send command
659
# https://www.pagerduty.com/docs/guides/agent-install-guide/
@@ -659,7 +682,8 @@ if [ \( \
682
-o "${SEND_PUSHBULLET}" = "YES" \
683
-o "${SEND_KAFKA}" = "YES" \
684
-o "${SEND_CUSTOM}" = "YES" \
662
- \) -a -z "${curl}" ]
685
+ -o "${SEND_IRC}" = "YES" \
686
+ \) -a -z "${curl}" ]
687
then
688
curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
689
if [ -z "${curl}" ]
@@ -678,6 +702,7 @@ if [ \( \
702
SEND_KAVENEGAR="NO"
703
SEND_KAFKA="NO"
704
SEND_CUSTOM="NO"
705
+ SEND_IRC="NO"
706
fi
707
fi
708
@@ -708,6 +733,7 @@ if [ "${SEND_EMAIL}" != "YES" \
733
-a "${SEND_KAFKA}" != "YES" \
734
-a "${SEND_PD}" != "YES" \
735
-a "${SEND_CUSTOM}" != "YES" \
736
+ -a "${SEND_IRC}" != "YES" \
737
]
738
then
739
fatal "All notification methods are disabled. Not sending notification for host '${host}', chart '${chart}' to '${roles}' for '${name}' = '${value}' for status '${status}'."
@@ -1443,6 +1469,39 @@ EOF
1469
return 1
1470
}
1471
1472
+# -----------------------------------------------------------------------------
1473
+# irc sender
1474
+
1475
+send_irc() {
1476
+ local NICKNAME="${1}" REALNAME="${2}" CHANNELS="${3}" NETWORK="${4}" MESSAGE="${5}" httpcode sent=0 channel color
1477
+
1478
+ case "${status}" in
1479
+ WARNING) color="warning" ;;
1480
+ CRITICAL) color="danger" ;;
1481
+ CLEAR) color="good" ;;
1482
+ *) color="#777777" ;;
1483
+ esac
1484
+
1485
+ if ["${SEND_IRC}" = "YES" -a ! -z "${nickname}" -a ! -z "${realname}" -a ! -z "${channels}" -a ! -z "${network}"]
1486
+ then
1487
+ for channel in ${CHANNELS}
1488
+ do
1489
+ httpcode=$(echo -e "USER ${NICKNAME} guest ${REALNAME}\nNICK ${NICKNAME}\nJOIN ${CHANNEL}\nPRIVMSG ${CHANNEL}> :${MESSAGE}\nQUIT\n" \| nc ${NETWORK} 6667)
1490
+ if [ "${httpcode}" = "200" ]
1491
+ then
1492
+ info "sent flock notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1493
+ sent=$((sent + 1))
1494
+ else
1495
+ error "failed to send flock notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1496
+ fi
1497
+ done
1498
+ fi
1499
+
1500
+ [ ${sent} -gt 0 ] && return 0
1501
+
1502
+ return 1
1503
+}
1504
+
1505
1506
# -----------------------------------------------------------------------------
1507
# prepare the content of the notification
@@ -1657,6 +1716,16 @@ SENT_KAFKA=$?
1716
send_pd "${to_pd}"
1717
SENT_PD=$?
1718
1719
+# -----------------------------------------------------------------------------
1720
+# send the irc message
1721
+
1722
+send_irc "${IRC_NICKNAME}" "${IRC_REALNAME}" "${to_irc}" "${DEFAULT_IRC_NETWORK}" "${host} ${status_message} - ${name//_/ } - ${chart} ----- ${alarm}
1723
+Severity: ${severity}
1724
+Chart: ${chart}
1725
+Family: ${family}
1726
+${info}"
1727
+
1728
+SENT_IRC=$?
1729
1730
# -----------------------------------------------------------------------------
1731
# send the custom message
@@ -1830,6 +1899,7 @@ if [ ${SENT_EMAIL} -eq 0 \
1899
-o ${SENT_PUSHBULLET} -eq 0 \
1900
-o ${SENT_KAFKA} -eq 0 \
1901
-o ${SENT_PD} -eq 0 \
1902
+ -o ${SENT_IRC} -eq 0 \
1903
-o ${SENT_CUSTOM} -eq 0 \
1904
]
1905
then