From ab2cf0c135d67dd630d9e2544b69a4df82b515c4 Mon Sep 17 00:00:00 2001 From: hashworks Date: Wed, 21 Sep 2016 16:33:28 +0200 Subject: [PATCH] Add telegram notifications to health monitoring --- conf.d/health_alarm_notify.conf | 37 ++++++++++++++- plugins.d/alarm-notify.sh | 81 +++++++++++++++++++++++++++++++-- 2 files changed, 113 insertions(+), 5 deletions(-) diff --git a/conf.d/health_alarm_notify.conf b/conf.d/health_alarm_notify.conf index bb3761e1..76359759 100644 --- a/conf.d/health_alarm_notify.conf +++ b/conf.d/health_alarm_notify.conf @@ -7,6 +7,7 @@ # - e-mails, # - push notifications to your mobile phone, # - messages to your slack team +# - messages to your telegram chat / group chat # # the recipient given in netdata alarms defines a role, so that different # people can be notified for each role. @@ -33,7 +34,7 @@ sendmail="" # The full path of the curl command. # If empty, the system $PATH will be searched for it. -# If not found, pushover and slack notifications will be disabled. +# If not found, pushover, telegram and slack notifications will be disabled. curl="" @@ -44,6 +45,7 @@ curl="" # # - emails addresses # - pushover user tokens +# - telegram chat ids # - slack channels # # You can append |critical to limit the notifications to be sent. @@ -52,10 +54,11 @@ curl="" # while the second one receives only the critical ones: # email : "user1@example.com user2@example.com|critical" # pushover: "2987343...9437837 8756278...2362736|critical" +# telegram: "111827421 112746832|critical" # slack : "alarms disasters|critical" # # If a recipient is set to empty string, the default recipient of the given -# notification method (email, pushover, slack) will be used. +# notification method (email, pushover, telegram, slack) will be used. # To disable a notification, use the recipient called: disabled # This works for all notification methods (including the default recipients). @@ -92,6 +95,26 @@ PUSHOVER_APP_TOKEN="" DEFAULT_RECIPIENT_PUSHOVER="" +############################################################################### +# sending telegram messages (telegram.org) + +# To get your chat ID send the command /my_id to telegram bot @get_id. +# Users also need to open a query with the bot. + +# note: multiple recipients can be given like this: +# "CHAT_ID_1 CHAT_ID_1 ..." + +# enable/disable sending telegram messages +SEND_TELEGRAM="YES" + +# Contact the bot @BotFather to create a new bot and to receive a bot token. +# Without it, netdata cannot send telegram messages. +TELEGRAM_BOT_TOKEN="" + +# If a role's recipients are not configured, a message will be send to this chat id: +DEFAULT_RECIPIENT_TELEGRAM="" + + ############################################################################### # sending slack notifications @@ -123,6 +146,8 @@ role_recipients_email[sysadmin]="${DEFAULT_RECIPIENT_EMAIL}" role_recipients_pushover[sysadmin]="${DEFAULT_RECIPIENT_PUSHOVER}" +role_recipients_telegram[sysadmin]="${DEFAULT_RECIPIENT_TELEGRAM}" + role_recipients_slack[sysadmin]="${DEFAULT_RECIPIENT_SLACK}" @@ -133,6 +158,8 @@ role_recipients_email[domainadmin]="${DEFAULT_RECIPIENT_EMAIL}" role_recipients_pushover[domainadmin]="${DEFAULT_RECIPIENT_PUSHOVER}" +role_recipients_telegram[domainadmin]="${DEFAULT_RECIPIENT_TELEGRAM}" + role_recipients_slack[domainadmin]="${DEFAULT_RECIPIENT_SLACK}" @@ -144,6 +171,8 @@ role_recipients_email[dba]="${DEFAULT_RECIPIENT_EMAIL}" role_recipients_pushover[dba]="${DEFAULT_RECIPIENT_PUSHOVER}" +role_recipients_telegram[dba]="${DEFAULT_RECIPIENT_TELEGRAM}" + role_recipients_slack[dba]="${DEFAULT_RECIPIENT_SLACK}" @@ -155,6 +184,8 @@ role_recipients_email[webmaster]="${DEFAULT_RECIPIENT_EMAIL}" role_recipients_pushover[webmaster]="${DEFAULT_RECIPIENT_PUSHOVER}" +role_recipients_telegram[webmaster]="${DEFAULT_RECIPIENT_TELEGRAM}" + role_recipients_slack[webmaster]="${DEFAULT_RECIPIENT_SLACK}" @@ -166,4 +197,6 @@ role_recipients_email[proxyadmin]="${DEFAULT_RECIPIENT_EMAIL}" role_recipients_pushover[proxyadmin]="${DEFAULT_RECIPIENT_PUSHOVER}" +role_recipients_telegram[proxyadmin]="${DEFAULT_RECIPIENT_TELEGRAM}" + role_recipients_slack[proxyadmin]="${DEFAULT_RECIPIENT_SLACK}" diff --git a/plugins.d/alarm-notify.sh b/plugins.d/alarm-notify.sh index f1cfce22..0823489c 100755 --- a/plugins.d/alarm-notify.sh +++ b/plugins.d/alarm-notify.sh @@ -78,6 +78,7 @@ sendmail= # enable / disable features SEND_SLACK="YES" SEND_PUSHOVER="YES" +SEND_TELEGRAM="YES" SEND_EMAIL="YES" # slack configs @@ -90,6 +91,11 @@ PUSHOVER_APP_TOKEN= DEFAULT_RECIPIENT_PUSHOVER= declare -A role_recipients_pushover=() +# telegram configs +TELEGRAM_BOT_TOKEN= +DEFAULT_RECIPIENT_TELEGRAM= +declare -A role_recipients_telegram=() + # email configs DEFAULT_RECIPIENT_EMAIL="root" declare -A role_recipients_email=() @@ -101,6 +107,10 @@ if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ] source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" fi +SEND_TELEGRAM="YES" +DEFAULT_RECIPIENT_TELEGRAM="111477881 111477882" +TELEGRAM_BOT_TOKEN="279223724:AAHJ3MGQKBTuKdSumDvmVGopluOWDPszTeI" + # ----------------------------------------------------------------------------- # filter recipients based on the criticality of each @@ -146,6 +156,7 @@ filter_recipient_by_criticality() { declare -A arr_slack=() declare -A arr_pushover=() +declare -A arr_telegram=() declare -A arr_email=() # netdata may call us with multiple recipients @@ -168,6 +179,14 @@ do [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1" done + # telegram + a="${role_recipients_telegram[${recipient}]}" + [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}" + for r in ${a//,/ } + do + [ "${r}" != "disabled" ] && filter_recipient_by_criticality telegram "${r}" && arr_telegram[${r/|*/}]="1" + done + # slack a="${role_recipients_slack[${recipient}]}" [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}" @@ -185,6 +204,10 @@ to_slack="${!arr_slack[*]}" to_pushover="${!arr_pushover[*]}" [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO" +# check array of telegram recipients (chat ids) +to_telegram="${!arr_telegram[*]}" +[ -z "${to_telegram}" ] && SEND_TELEGRAM="NO" + # build the list of email recipients (email addresses) to_email= for x in "${!arr_email[@]}" @@ -204,12 +227,16 @@ done # check pushover [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO" -if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" \) -a -z "${curl}" ] +# check telegram +[ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO" + +if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" -o "${SEND_TELEGRAM}" = "YES" \) -a -z "${curl}" ] then curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)" if [ -z "${curl}" ] then SEND_PUSHOVER="NO" + SEND_TELEGRAM="NO" SEND_SLACK="NO" fi fi @@ -221,7 +248,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_SLACK}" != "YES" ] +if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_TELEGRAM}" != "YES" -a "${SEND_SLACK}" != "YES" ] then echo >&2 "All notification methods are disabled. Not sending a notification." exit 1 @@ -391,6 +418,44 @@ send_pushover() { return 1 } + +# ----------------------------------------------------------------------------- +# telegram sender + +send_telegram() { + local bottoken="${1}" chatids="${2}" message="${3}" httpcode sent=0 chatid disableNotification="" + + if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi + + if [ "${SEND_TELEGRAM}" = "YES" -a ! -z "${bottoken}" -a ! -z "${chatids}" -a ! -z "${message}" ]; + then + for chatid in ${chatids} + do + # https://core.telegram.org/bots/api#sendmessage + httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \ + --data-urlencode "parse_mode=HTML" \ + --data-urlencode "disable_web_page_preview=true" \ + --data-urlencode "text=$message" \ + "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=$chatid") + + if [ "${httpcode}" == "200" ] + then + echo >&2 "${me}: Sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'" + sent=$((sent + 1)) + elif [ "${httpcode}" == "401" ] + then + echo >&2 "${me}: Failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token." + else + echo >&2 "${me}: Failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP error code ${httpcode}." + fi + done + + [ ${sent} -gt 0 ] && return 0 + fi + + return 1 +} + # ----------------------------------------------------------------------------- # slack sender @@ -570,6 +635,16 @@ send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" " SENT_PUSHOVER=$? +# https://core.telegram.org/bots/api#formatting-options +raised_for_paranthesis=" (${raised_for})" +[ -z "$raised_for" ] && raised_for_paranthesis="" +send_telegram "${TELEGRAM_BOT_TOKEN}" "${to_telegram}" "${severity}, ${status_message} +${chart} (${family}) +${alarm}${raised_for_paranthesis} +${info}" + +SENT_TELEGRAM=$? + # ----------------------------------------------------------------------------- # send the email @@ -669,7 +744,7 @@ SENT_EMAIL=$? # let netdata know # we did send somehting -[ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 -o ${SENT_SLACK} -eq 0 ] && exit 0 +[ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 -o ${SENT_TELEGRAM} -eq 0 -o ${SENT_SLACK} -eq 0 ] && exit 0 # we did not send anything exit 1 -- 2.39.2