From 9170d3b405cff7e02ff4f0db726e3e72b28d0178 Mon Sep 17 00:00:00 2001 From: shadycuz Date: Thu, 3 Nov 2016 04:13:06 -0400 Subject: [PATCH] Added SMS notification via Twilio --- conf.d/health_alarm_notify.conf | 29 +++++++++++++ plugins.d/alarm-notify.sh | 73 +++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 3 deletions(-) diff --git a/conf.d/health_alarm_notify.conf b/conf.d/health_alarm_notify.conf index 3f0876e6..33a752f9 100644 --- a/conf.d/health_alarm_notify.conf +++ b/conf.d/health_alarm_notify.conf @@ -8,6 +8,7 @@ # - push notifications to your mobile phone (pushover.net), # - messages to your slack team (slack.com), # - messages to your telegram chat / group chat (telegram.org) +# - sms messages to your cell phone or any sms enabled device (twilio.com) # # The 'to' line given at netdata alarms defines a *role*, so that many # people can be notified for each role. @@ -60,6 +61,7 @@ curl="" # - pushover user tokens # - telegram chat ids # - slack channels +# - sms phone numbers # # You can append |critical to limit the notifications to be sent. # @@ -70,6 +72,7 @@ curl="" # pushover: "2987343...9437837 8756278...2362736|critical" # telegram: "111827421 112746832|critical" # slack : "alarms disasters|critical" +# twilio : "+15555555555 +17777777777|critical" # # If a recipient is set to empty string, the default recipient of the given # notification method (email, pushover, telegram, slack) will be used. @@ -133,6 +136,26 @@ SEND_PUSHBULLET="YES" PUSHBULLET_ACCESS_TOKEN="" DEFAULT_RECIPIENT_PUSHBULLET="" +############################################################################### +# Twilio (twilio.com) SMS options + +# multiple recipients can be given like this: +# "+15555555555 +17777777777" + +# enable/disable sending twilio SMS +SEND_TWILIO="YES" + +# Signup for free trial and select a SMS capable Twilio Number +# To get your Account SID and Token, go to https://www.twilio.com/console +# Place your sid, token and number below. +# Then just set the recipients' phone numbers. +# The trial account is only allowed to use the number specified when set up. + +# Without an account sid and token, netdata cannot send Twilio text messages. +TWILIO_ACCOUNT_SID="" +TWILIO_ACCOUNT_TOKEN="" +TWILIO_NUMBER="" +DEFAULT_RECIPIENT_TWILIO="" ############################################################################### # telegram (telegram.org) global notification options @@ -193,6 +216,7 @@ role_recipients_telegram[sysadmin]="${DEFAULT_RECIPIENT_TELEGRAM}" role_recipients_slack[sysadmin]="${DEFAULT_RECIPIENT_SLACK}" +role_recipients_twilio[sysadmin]="${DEFAULT_RECIPIENT_TWILIO}" # ----------------------------------------------------------------------------- # DNS related alarms @@ -207,6 +231,7 @@ role_recipients_telegram[domainadmin]="${DEFAULT_RECIPIENT_TELEGRAM}" role_recipients_slack[domainadmin]="${DEFAULT_RECIPIENT_SLACK}" +role_recipients_twilio[domainadmin]="${DEFAULT_RECIPIENT_TWILIO}" # ----------------------------------------------------------------------------- # database servers alarms @@ -222,6 +247,7 @@ role_recipients_telegram[dba]="${DEFAULT_RECIPIENT_TELEGRAM}" role_recipients_slack[dba]="${DEFAULT_RECIPIENT_SLACK}" +role_recipients_twilio[dba]="${DEFAULT_RECIPIENT_TWILIO}" # ----------------------------------------------------------------------------- # web servers alarms @@ -237,6 +263,7 @@ role_recipients_telegram[webmaster]="${DEFAULT_RECIPIENT_TELEGRAM}" role_recipients_slack[webmaster]="${DEFAULT_RECIPIENT_SLACK}" +role_recipients_twilio[webmaster]="${DEFAULT_RECIPIENT_TWILIO}" # ----------------------------------------------------------------------------- # proxy servers alarms @@ -251,3 +278,5 @@ role_recipients_pushbullet[proxyadmin]="${DEFAULT_RECIPIENT_PUSHBULLET}" role_recipients_telegram[proxyadmin]="${DEFAULT_RECIPIENT_TELEGRAM}" role_recipients_slack[proxyadmin]="${DEFAULT_RECIPIENT_SLACK}" + +role_recipients_twilio[proxyadmin]="${DEFAULT_RECIPIENT_TWILIO}" diff --git a/plugins.d/alarm-notify.sh b/plugins.d/alarm-notify.sh index a2e3f8e7..93f497a9 100755 --- a/plugins.d/alarm-notify.sh +++ b/plugins.d/alarm-notify.sh @@ -169,6 +169,7 @@ sendmail= # enable / disable features SEND_SLACK="YES" SEND_PUSHOVER="YES" +SEND_TWILIO="YES" SEND_TELEGRAM="YES" SEND_EMAIL="YES" SEND_PUSHBULLET="YES" @@ -188,6 +189,13 @@ PUSHBULLET_ACCESS_TOKEN= DEFAULT_RECIPIENT_PUSHBULLET= declare -A role_recipients_pushbullet=() +# twilio configs +TWILIO_ACCOUNT_SID= +TWILIO_ACCOUNT_TOKEN= +TWILIO_NUMBER= +DEFAULT_RECIPIENT_TWILIO= +declare -A role_recipients_twilio=() + # telegram configs TELEGRAM_BOT_TOKEN= DEFAULT_RECIPIENT_TELEGRAM= @@ -250,6 +258,7 @@ filter_recipient_by_criticality() { declare -A arr_slack=() declare -A arr_pushover=() declare -A arr_pushbullet=() +declare -A arr_twilio=() declare -A arr_telegram=() declare -A arr_email=() @@ -285,6 +294,14 @@ do [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushbullet "${r}" && arr_pushbullet[${r/|*/}]="1" done + # twilio + a="${role_recipients_twilio[${x}]}" + [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TWILIO}" + for r in ${a//,/ } + do + [ "${r}" != "disabled" ] && filter_recipient_by_criticality twilio "${r}" && arr_twilio[${r/|*/}]="1" + done + # telegram a="${role_recipients_telegram[${x}]}" [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}" @@ -314,6 +331,10 @@ to_pushover="${!arr_pushover[*]}" to_pushbullet="${!arr_pushbullet[*]}" [ -z "${to_pushbullet}" ] && SEND_PUSHBULLET="NO" +# build the list of twilio recipients (phone numbers) +to_twilio="${!arr_twilio[*]}" +[ -z "${to_twilio}" ] && SEND_TWILIO="NO" + # check array of telegram recipients (chat ids) to_telegram="${!arr_telegram[*]}" [ -z "${to_telegram}" ] && SEND_TELEGRAM="NO" @@ -340,10 +361,13 @@ done # check pushbullet [ -z "${DEFAULT_RECIPIENT_PUSHBULLET}" ] && SEND_PUSHBULLET="NO" +# check twilio +[ -z "${DEFAULT_RECIPIENT_TWILIO}" ] && SEND_TWILIO="NO" + # check telegram [ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO" -if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" -o "${SEND_TELEGRAM}" = "YES" -o "${SEND_PUSHBULLET}" = "YES" \) -a -z "${curl}" ] +if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" -o "${SEND_TWILIO}" = "YES" -o "${SEND_TELEGRAM}" = "YES" -o "${SEND_PUSHBULLET}" = "YES" \) -a -z "${curl}" ] then curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)" if [ -z "${curl}" ] @@ -352,6 +376,7 @@ if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" -o "${SEND_TELEGRA SEND_PUSHBULLET="NO" SEND_TELEGRAM="NO" SEND_SLACK="NO" + SEND_TWILIO="NO" fi fi @@ -362,7 +387,7 @@ if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ] fi # check that we have at least a method enabled -if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_TELEGRAM}" != "YES" -a "${SEND_SLACK}" != "YES" -a "${SEND_PUSHBULLET}" != "YES" ] +if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_TELEGRAM}" != "YES" -a "${SEND_SLACK}" != "YES" -a "${SEND_TWILIO}" != "YES" -a "${SEND_PUSHBULLET}" != "YES" ] then fatal "All notification methods are disabled. Not sending notification to '${role}' for '${name}' = '${value}' of chart '${chart}' for status '${status}'." fi @@ -567,6 +592,38 @@ EOF return 1 } +# ----------------------------------------------------------------------------- +# twilio sender + +send_twilio() { + local accountsid="${1}" accounttoken="${2}" twilionumber="${3}" recipients="${4}" title="${5}" message="${6}" httpcode sent=0 user + if [ "${SEND_TWILIO}" = "YES" -a ! -z "${accountsid}" -a ! -z "${accounttoken}" -a ! -z "${twilionumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ] + then + #https://www.twilio.com/packages/labs/code/bash/twilio-sms + for user in ${recipients} + do + httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \ + --data-urlencode "From=${twilionumber}" \ + --data-urlencode "To=${user}" \ + --data-urlencode "Body=${title}${message}" \ + -u "${accountsid}:${accounttoken}" \ + "https://api.twilio.com/2010-04-01/Accounts/${accountsid}/Messages.json") + + if [ "${httpcode}" == "201" ] + then + info "sent Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}'" + sent=$((sent + 1)) + else + error "failed to send Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}." + fi + done + + [ ${sent} -gt 0 ] && return 0 + fi + + return 1 +} + # ----------------------------------------------------------------------------- # telegram sender @@ -795,6 +852,16 @@ The source of this alarm is line ${src}" SENT_PUSHBULLET=$? +# send the twilio SMS + +send_twilio "${TWILIO_ACCOUNT_SID}" "${TWILIO_ACCOUNT_TOKEN}" "${TWILIO_NUMBER}" "${to_twilio}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm} +Severity: ${severity} +Chart: ${chart} +Family: ${family} +The source of this alarm is line ${src}" + +SENT_TWILIO=$? + # ----------------------------------------------------------------------------- # send the telegram.org message @@ -905,7 +972,7 @@ SENT_EMAIL=$? # let netdata know # we did send something -[ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 -o ${SENT_TELEGRAM} -eq 0 -o ${SENT_SLACK} -eq 0 -o ${SENT_PUSHBULLET} -eq 0 ] && exit 0 +[ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 -o ${SENT_TELEGRAM} -eq 0 -o ${SENT_SLACK} -eq 0 -o ${SENT_TWILIO} -eq 0 -o ${SENT_PUSHBULLET} -eq 0 ] && exit 0 # we did not send anything exit 1 -- 2.39.2