Added SMS notification via Twilio
shadycuz committed
Nov 3, 2016 at 04:13 UTC
9170d3b405cff7e02ff4f0db726e3e72b28d0178
2 files changed
+99
-3
conf.d/health_alarm_notify.conf
+29
@@ -8,6 +8,7 @@
8
# - push notifications to your mobile phone (pushover.net),
9
# - messages to your slack team (slack.com),
10
# - messages to your telegram chat / group chat (telegram.org)
11
+# - sms messages to your cell phone or any sms enabled device (twilio.com)
12
#
13
# The 'to' line given at netdata alarms defines a *role*, so that many
14
# people can be notified for each role.
@@ -60,6 +61,7 @@ curl=""
61
# - pushover user tokens
62
# - telegram chat ids
63
# - slack channels
64
+# - sms phone numbers
65
#
66
# You can append |critical to limit the notifications to be sent.
67
#
@@ -70,6 +72,7 @@ curl=""
72
# pushover: "2987343...9437837 8756278...2362736|critical"
73
# telegram: "111827421 112746832|critical"
74
# slack : "alarms disasters|critical"
75
+# twilio : "+15555555555 +17777777777|critical"
76
#
77
# If a recipient is set to empty string, the default recipient of the given
78
# notification method (email, pushover, telegram, slack) will be used.
@@ -133,6 +136,26 @@ SEND_PUSHBULLET="YES"
136
PUSHBULLET_ACCESS_TOKEN=""
137
DEFAULT_RECIPIENT_PUSHBULLET=""
138
139
+###############################################################################
140
+# Twilio (twilio.com) SMS options
141
+
142
+# multiple recipients can be given like this:
143
+# "+15555555555 +17777777777"
144
+
145
+# enable/disable sending twilio SMS
146
+SEND_TWILIO="YES"
147
+
148
+# Signup for free trial and select a SMS capable Twilio Number
149
+# To get your Account SID and Token, go to https://www.twilio.com/console
150
+# Place your sid, token and number below.
151
+# Then just set the recipients' phone numbers.
152
+# The trial account is only allowed to use the number specified when set up.
153
+
154
+# Without an account sid and token, netdata cannot send Twilio text messages.
155
+TWILIO_ACCOUNT_SID=""
156
+TWILIO_ACCOUNT_TOKEN=""
157
+TWILIO_NUMBER=""
158
+DEFAULT_RECIPIENT_TWILIO=""
159
160
###############################################################################
161
# telegram (telegram.org) global notification options
@@ -193,6 +216,7 @@ role_recipients_telegram[sysadmin]="${DEFAULT_RECIPIENT_TELEGRAM}"
216
217
role_recipients_slack[sysadmin]="${DEFAULT_RECIPIENT_SLACK}"
218
219
+role_recipients_twilio[sysadmin]="${DEFAULT_RECIPIENT_TWILIO}"
220
221
# -----------------------------------------------------------------------------
222
# DNS related alarms
@@ -207,6 +231,7 @@ role_recipients_telegram[domainadmin]="${DEFAULT_RECIPIENT_TELEGRAM}"
231
232
role_recipients_slack[domainadmin]="${DEFAULT_RECIPIENT_SLACK}"
233
234
+role_recipients_twilio[domainadmin]="${DEFAULT_RECIPIENT_TWILIO}"
235
236
# -----------------------------------------------------------------------------
237
# database servers alarms
@@ -222,6 +247,7 @@ role_recipients_telegram[dba]="${DEFAULT_RECIPIENT_TELEGRAM}"
247
248
role_recipients_slack[dba]="${DEFAULT_RECIPIENT_SLACK}"
249
250
+role_recipients_twilio[dba]="${DEFAULT_RECIPIENT_TWILIO}"
251
252
# -----------------------------------------------------------------------------
253
# web servers alarms
@@ -237,6 +263,7 @@ role_recipients_telegram[webmaster]="${DEFAULT_RECIPIENT_TELEGRAM}"
263
264
role_recipients_slack[webmaster]="${DEFAULT_RECIPIENT_SLACK}"
265
266
+role_recipients_twilio[webmaster]="${DEFAULT_RECIPIENT_TWILIO}"
267
268
# -----------------------------------------------------------------------------
269
# proxy servers alarms
@@ -251,3 +278,5 @@ role_recipients_pushbullet[proxyadmin]="${DEFAULT_RECIPIENT_PUSHBULLET}"
278
role_recipients_telegram[proxyadmin]="${DEFAULT_RECIPIENT_TELEGRAM}"
279
280
role_recipients_slack[proxyadmin]="${DEFAULT_RECIPIENT_SLACK}"
281
+
282
+role_recipients_twilio[proxyadmin]="${DEFAULT_RECIPIENT_TWILIO}"
plugins.d/alarm-notify.sh
+70
-3
@@ -169,6 +169,7 @@ sendmail=
169
# enable / disable features
170
SEND_SLACK="YES"
171
SEND_PUSHOVER="YES"
172
+SEND_TWILIO="YES"
173
SEND_TELEGRAM="YES"
174
SEND_EMAIL="YES"
175
SEND_PUSHBULLET="YES"
@@ -188,6 +189,13 @@ PUSHBULLET_ACCESS_TOKEN=
189
DEFAULT_RECIPIENT_PUSHBULLET=
190
declare -A role_recipients_pushbullet=()
191
192
+# twilio configs
193
+TWILIO_ACCOUNT_SID=
194
+TWILIO_ACCOUNT_TOKEN=
195
+TWILIO_NUMBER=
196
+DEFAULT_RECIPIENT_TWILIO=
197
+declare -A role_recipients_twilio=()
198
+
199
# telegram configs
200
TELEGRAM_BOT_TOKEN=
201
DEFAULT_RECIPIENT_TELEGRAM=
@@ -250,6 +258,7 @@ filter_recipient_by_criticality() {
258
declare -A arr_slack=()
259
declare -A arr_pushover=()
260
declare -A arr_pushbullet=()
261
+declare -A arr_twilio=()
262
declare -A arr_telegram=()
263
declare -A arr_email=()
264
@@ -285,6 +294,14 @@ do
294
[ "${r}" != "disabled" ] && filter_recipient_by_criticality pushbullet "${r}" && arr_pushbullet[${r/|*/}]="1"
295
done
296
297
+ # twilio
298
+ a="${role_recipients_twilio[${x}]}"
299
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TWILIO}"
300
+ for r in ${a//,/ }
301
+ do
302
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality twilio "${r}" && arr_twilio[${r/|*/}]="1"
303
+ done
304
+
305
# telegram
306
a="${role_recipients_telegram[${x}]}"
307
[ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}"
@@ -314,6 +331,10 @@ to_pushover="${!arr_pushover[*]}"
331
to_pushbullet="${!arr_pushbullet[*]}"
332
[ -z "${to_pushbullet}" ] && SEND_PUSHBULLET="NO"
333
334
+# build the list of twilio recipients (phone numbers)
335
+to_twilio="${!arr_twilio[*]}"
336
+[ -z "${to_twilio}" ] && SEND_TWILIO="NO"
337
+
338
# check array of telegram recipients (chat ids)
339
to_telegram="${!arr_telegram[*]}"
340
[ -z "${to_telegram}" ] && SEND_TELEGRAM="NO"
@@ -340,10 +361,13 @@ done
361
# check pushbullet
362
[ -z "${DEFAULT_RECIPIENT_PUSHBULLET}" ] && SEND_PUSHBULLET="NO"
363
364
+# check twilio
365
+[ -z "${DEFAULT_RECIPIENT_TWILIO}" ] && SEND_TWILIO="NO"
366
+
367
# check telegram
368
[ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO"
369
346
-if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" -o "${SEND_TELEGRAM}" = "YES" -o "${SEND_PUSHBULLET}" = "YES" \) -a -z "${curl}" ]
370
+if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" -o "${SEND_TWILIO}" = "YES" -o "${SEND_TELEGRAM}" = "YES" -o "${SEND_PUSHBULLET}" = "YES" \) -a -z "${curl}" ]
371
then
372
curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
373
if [ -z "${curl}" ]
@@ -352,6 +376,7 @@ if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" -o "${SEND_TELEGRA
376
SEND_PUSHBULLET="NO"
377
SEND_TELEGRAM="NO"
378
SEND_SLACK="NO"
379
+ SEND_TWILIO="NO"
380
fi
381
fi
382
@@ -362,7 +387,7 @@ if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
387
fi
388
389
# check that we have at least a method enabled
365
-if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_TELEGRAM}" != "YES" -a "${SEND_SLACK}" != "YES" -a "${SEND_PUSHBULLET}" != "YES" ]
390
+if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_TELEGRAM}" != "YES" -a "${SEND_SLACK}" != "YES" -a "${SEND_TWILIO}" != "YES" -a "${SEND_PUSHBULLET}" != "YES" ]
391
then
392
fatal "All notification methods are disabled. Not sending notification to '${role}' for '${name}' = '${value}' of chart '${chart}' for status '${status}'."
393
fi
@@ -567,6 +592,38 @@ EOF
592
return 1
593
}
594
595
+# -----------------------------------------------------------------------------
596
+# twilio sender
597
+
598
+send_twilio() {
599
+ local accountsid="${1}" accounttoken="${2}" twilionumber="${3}" recipients="${4}" title="${5}" message="${6}" httpcode sent=0 user
600
+ if [ "${SEND_TWILIO}" = "YES" -a ! -z "${accountsid}" -a ! -z "${accounttoken}" -a ! -z "${twilionumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
601
+ then
602
+ #https://www.twilio.com/packages/labs/code/bash/twilio-sms
603
+ for user in ${recipients}
604
+ do
605
+ httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
606
+ --data-urlencode "From=${twilionumber}" \
607
+ --data-urlencode "To=${user}" \
608
+ --data-urlencode "Body=${title}${message}" \
609
+ -u "${accountsid}:${accounttoken}" \
610
+ "https://api.twilio.com/2010-04-01/Accounts/${accountsid}/Messages.json")
611
+
612
+ if [ "${httpcode}" == "201" ]
613
+ then
614
+ info "sent Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
615
+ sent=$((sent + 1))
616
+ else
617
+ error "failed to send Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
618
+ fi
619
+ done
620
+
621
+ [ ${sent} -gt 0 ] && return 0
622
+ fi
623
+
624
+ return 1
625
+}
626
+
627
# -----------------------------------------------------------------------------
628
# telegram sender
629
@@ -795,6 +852,16 @@ The source of this alarm is line ${src}"
852
853
SENT_PUSHBULLET=$?
854
855
+# send the twilio SMS
856
+
857
+send_twilio "${TWILIO_ACCOUNT_SID}" "${TWILIO_ACCOUNT_TOKEN}" "${TWILIO_NUMBER}" "${to_twilio}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
858
+Severity: ${severity}
859
+Chart: ${chart}
860
+Family: ${family}
861
+The source of this alarm is line ${src}"
862
+
863
+SENT_TWILIO=$?
864
+
865
# -----------------------------------------------------------------------------
866
# send the telegram.org message
867
@@ -905,7 +972,7 @@ SENT_EMAIL=$?
972
# let netdata know
973
974
# we did send something
908
-[ ${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
975
+[ ${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
976
977
# we did not send anything
978
exit 1