Add telegram notifications to health monitoring
hashworks committed
Sep 21, 2016 at 16:33 UTC
ab2cf0c135d67dd630d9e2544b69a4df82b515c4
2 files changed
+113
-5
conf.d/health_alarm_notify.conf
+35
-2
@@ -7,6 +7,7 @@
7
# - e-mails,
8
# - push notifications to your mobile phone,
9
# - messages to your slack team
10
+# - messages to your telegram chat / group chat
11
#
12
# the recipient given in netdata alarms defines a role, so that different
13
# people can be notified for each role.
@@ -33,7 +34,7 @@ sendmail=""
34
35
# The full path of the curl command.
36
# If empty, the system $PATH will be searched for it.
36
-# If not found, pushover and slack notifications will be disabled.
37
+# If not found, pushover, telegram and slack notifications will be disabled.
38
curl=""
39
40
@@ -44,6 +45,7 @@ curl=""
45
#
46
# - emails addresses
47
# - pushover user tokens
48
+# - telegram chat ids
49
# - slack channels
50
#
51
# You can append |critical to limit the notifications to be sent.
@@ -52,10 +54,11 @@ curl=""
54
# while the second one receives only the critical ones:
55
# email : "user1@example.com user2@example.com|critical"
56
# pushover: "2987343...9437837 8756278...2362736|critical"
57
+# telegram: "111827421 112746832|critical"
58
# slack : "alarms disasters|critical"
59
#
60
# If a recipient is set to empty string, the default recipient of the given
58
-# notification method (email, pushover, slack) will be used.
61
+# notification method (email, pushover, telegram, slack) will be used.
62
# To disable a notification, use the recipient called: disabled
63
# This works for all notification methods (including the default recipients).
64
@@ -92,6 +95,26 @@ PUSHOVER_APP_TOKEN=""
95
DEFAULT_RECIPIENT_PUSHOVER=""
96
97
98
+###############################################################################
99
+# sending telegram messages (telegram.org)
100
+
101
+# To get your chat ID send the command /my_id to telegram bot @get_id.
102
+# Users also need to open a query with the bot.
103
+
104
+# note: multiple recipients can be given like this:
105
+# "CHAT_ID_1 CHAT_ID_1 ..."
106
+
107
+# enable/disable sending telegram messages
108
+SEND_TELEGRAM="YES"
109
+
110
+# Contact the bot @BotFather to create a new bot and to receive a bot token.
111
+# Without it, netdata cannot send telegram messages.
112
+TELEGRAM_BOT_TOKEN=""
113
+
114
+# If a role's recipients are not configured, a message will be send to this chat id:
115
+DEFAULT_RECIPIENT_TELEGRAM=""
116
+
117
+
118
###############################################################################
119
# sending slack notifications
120
@@ -123,6 +146,8 @@ role_recipients_email[sysadmin]="${DEFAULT_RECIPIENT_EMAIL}"
146
147
role_recipients_pushover[sysadmin]="${DEFAULT_RECIPIENT_PUSHOVER}"
148
149
+role_recipients_telegram[sysadmin]="${DEFAULT_RECIPIENT_TELEGRAM}"
150
+
151
role_recipients_slack[sysadmin]="${DEFAULT_RECIPIENT_SLACK}"
152
153
@@ -133,6 +158,8 @@ role_recipients_email[domainadmin]="${DEFAULT_RECIPIENT_EMAIL}"
158
159
role_recipients_pushover[domainadmin]="${DEFAULT_RECIPIENT_PUSHOVER}"
160
161
+role_recipients_telegram[domainadmin]="${DEFAULT_RECIPIENT_TELEGRAM}"
162
+
163
role_recipients_slack[domainadmin]="${DEFAULT_RECIPIENT_SLACK}"
164
165
@@ -144,6 +171,8 @@ role_recipients_email[dba]="${DEFAULT_RECIPIENT_EMAIL}"
171
172
role_recipients_pushover[dba]="${DEFAULT_RECIPIENT_PUSHOVER}"
173
174
+role_recipients_telegram[dba]="${DEFAULT_RECIPIENT_TELEGRAM}"
175
+
176
role_recipients_slack[dba]="${DEFAULT_RECIPIENT_SLACK}"
177
178
@@ -155,6 +184,8 @@ role_recipients_email[webmaster]="${DEFAULT_RECIPIENT_EMAIL}"
184
185
role_recipients_pushover[webmaster]="${DEFAULT_RECIPIENT_PUSHOVER}"
186
187
+role_recipients_telegram[webmaster]="${DEFAULT_RECIPIENT_TELEGRAM}"
188
+
189
role_recipients_slack[webmaster]="${DEFAULT_RECIPIENT_SLACK}"
190
191
@@ -166,4 +197,6 @@ role_recipients_email[proxyadmin]="${DEFAULT_RECIPIENT_EMAIL}"
197
198
role_recipients_pushover[proxyadmin]="${DEFAULT_RECIPIENT_PUSHOVER}"
199
200
+role_recipients_telegram[proxyadmin]="${DEFAULT_RECIPIENT_TELEGRAM}"
201
+
202
role_recipients_slack[proxyadmin]="${DEFAULT_RECIPIENT_SLACK}"
plugins.d/alarm-notify.sh
+78
-3
@@ -78,6 +78,7 @@ sendmail=
78
# enable / disable features
79
SEND_SLACK="YES"
80
SEND_PUSHOVER="YES"
81
+SEND_TELEGRAM="YES"
82
SEND_EMAIL="YES"
83
84
# slack configs
@@ -90,6 +91,11 @@ PUSHOVER_APP_TOKEN=
91
DEFAULT_RECIPIENT_PUSHOVER=
92
declare -A role_recipients_pushover=()
93
94
+# telegram configs
95
+TELEGRAM_BOT_TOKEN=
96
+DEFAULT_RECIPIENT_TELEGRAM=
97
+declare -A role_recipients_telegram=()
98
+
99
# email configs
100
DEFAULT_RECIPIENT_EMAIL="root"
101
declare -A role_recipients_email=()
@@ -101,6 +107,10 @@ if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
107
source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
108
fi
109
110
+SEND_TELEGRAM="YES"
111
+DEFAULT_RECIPIENT_TELEGRAM="111477881 111477882"
112
+TELEGRAM_BOT_TOKEN="279223724:AAHJ3MGQKBTuKdSumDvmVGopluOWDPszTeI"
113
+
114
# -----------------------------------------------------------------------------
115
# filter recipients based on the criticality of each
116
@@ -146,6 +156,7 @@ filter_recipient_by_criticality() {
156
157
declare -A arr_slack=()
158
declare -A arr_pushover=()
159
+declare -A arr_telegram=()
160
declare -A arr_email=()
161
162
# netdata may call us with multiple recipients
@@ -168,6 +179,14 @@ do
179
[ "${r}" != "disabled" ] && filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
180
done
181
182
+ # telegram
183
+ a="${role_recipients_telegram[${recipient}]}"
184
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}"
185
+ for r in ${a//,/ }
186
+ do
187
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality telegram "${r}" && arr_telegram[${r/|*/}]="1"
188
+ done
189
+
190
# slack
191
a="${role_recipients_slack[${recipient}]}"
192
[ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
@@ -185,6 +204,10 @@ to_slack="${!arr_slack[*]}"
204
to_pushover="${!arr_pushover[*]}"
205
[ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
206
207
+# check array of telegram recipients (chat ids)
208
+to_telegram="${!arr_telegram[*]}"
209
+[ -z "${to_telegram}" ] && SEND_TELEGRAM="NO"
210
+
211
# build the list of email recipients (email addresses)
212
to_email=
213
for x in "${!arr_email[@]}"
@@ -204,12 +227,16 @@ done
227
# check pushover
228
[ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
229
207
-if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" \) -a -z "${curl}" ]
230
+# check telegram
231
+[ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO"
232
+
233
+if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" -o "${SEND_TELEGRAM}" = "YES" \) -a -z "${curl}" ]
234
then
235
curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
236
if [ -z "${curl}" ]
237
then
238
SEND_PUSHOVER="NO"
239
+ SEND_TELEGRAM="NO"
240
SEND_SLACK="NO"
241
fi
242
fi
@@ -221,7 +248,7 @@ if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
248
fi
249
250
# check that we have at least a method enabled
224
-if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_SLACK}" != "YES" ]
251
+if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_TELEGRAM}" != "YES" -a "${SEND_SLACK}" != "YES" ]
252
then
253
echo >&2 "All notification methods are disabled. Not sending a notification."
254
exit 1
@@ -391,6 +418,44 @@ send_pushover() {
418
return 1
419
}
420
421
+
422
+# -----------------------------------------------------------------------------
423
+# telegram sender
424
+
425
+send_telegram() {
426
+ local bottoken="${1}" chatids="${2}" message="${3}" httpcode sent=0 chatid disableNotification=""
427
+
428
+ if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi
429
+
430
+ if [ "${SEND_TELEGRAM}" = "YES" -a ! -z "${bottoken}" -a ! -z "${chatids}" -a ! -z "${message}" ];
431
+ then
432
+ for chatid in ${chatids}
433
+ do
434
+ # https://core.telegram.org/bots/api#sendmessage
435
+ httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \
436
+ --data-urlencode "parse_mode=HTML" \
437
+ --data-urlencode "disable_web_page_preview=true" \
438
+ --data-urlencode "text=$message" \
439
+ "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=$chatid")
440
+
441
+ if [ "${httpcode}" == "200" ]
442
+ then
443
+ echo >&2 "${me}: Sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'"
444
+ sent=$((sent + 1))
445
+ elif [ "${httpcode}" == "401" ]
446
+ then
447
+ echo >&2 "${me}: Failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token."
448
+ else
449
+ echo >&2 "${me}: Failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP error code ${httpcode}."
450
+ fi
451
+ done
452
+
453
+ [ ${sent} -gt 0 ] && return 0
454
+ fi
455
+
456
+ return 1
457
+}
458
+
459
# -----------------------------------------------------------------------------
460
# slack sender
461
@@ -570,6 +635,16 @@ send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "
635
636
SENT_PUSHOVER=$?
637
638
+# https://core.telegram.org/bots/api#formatting-options
639
+raised_for_paranthesis=" (${raised_for})"
640
+[ -z "$raised_for" ] && raised_for_paranthesis=""
641
+send_telegram "${TELEGRAM_BOT_TOKEN}" "${to_telegram}" "<b>${severity}, ${status_message}
642
+${chart} (${family})</b>
643
+<a href=\"${goto_url}\">${alarm}${raised_for_paranthesis}</a>
644
+<i>${info}</i>"
645
+
646
+SENT_TELEGRAM=$?
647
+
648
# -----------------------------------------------------------------------------
649
# send the email
650
@@ -669,7 +744,7 @@ SENT_EMAIL=$?
744
# let netdata know
745
746
# we did send somehting
672
-[ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 -o ${SENT_SLACK} -eq 0 ] && exit 0
747
+[ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 -o ${SENT_TELEGRAM} -eq 0 -o ${SENT_SLACK} -eq 0 ] && exit 0
748
749
# we did not send anything
750
exit 1