@cryptotaxi247 / netdata-1 / commits / b7a194749

add support for alerta.io #3147

Christian Pradelli committed Dec 20, 2017 at 15:36 UTC b7a194749ef9189fb2ae1f851960e011f76068c6
2 files changed +107 -2
conf.d/health_alarm_notify.conf
+25 -2
@@ -7,6 +7,7 @@
7 # - e-mails (using the sendmail command),
8 # - push notifications to your mobile phone (pushover.net),
9 # - messages to your slack team (slack.com),
10 +# - messages to your alerta server (alerta.io),
11 # - messages to your flock team (flock.com),
12 # - messages to your discord guild (discordapp.com),
13 # - messages to your telegram chat / group chat (telegram.org)
@@ -23,7 +24,7 @@
24 #------------------------------------------------------------------------------
25 # proxy configuration
26 #
26 -# If you need to send curl based notifications (pushover, pushbullet, slack,
27 +# If you need to send curl based notifications (pushover, pushbullet, slack, alerta,
28 # flock, discord, telegram) via a proxy, set these to your proxy address:
29 #export http_proxy="http://10.0.0.1:3128/"
30 #export https_proxy="http://10.0.0.1:3128/"
@@ -64,6 +65,7 @@ curl=""
65 # - pushover user tokens
66 # - telegram chat ids
67 # - slack channels
68 +# - alerta environment
69 # - flock rooms
70 # - discord channels
71 # - hipchat rooms
@@ -79,6 +81,7 @@ curl=""
81 # pushover : "2987343...9437837 8756278...2362736|critical"
82 # telegram : "111827421 112746832|critical"
83 # slack : "alarms disasters|critical"
84 +# alerta : "alarms disasters|critical"
85 # flock : "alarms disasters|critical"
86 # discord : "alarms disasters|critical"
87 # twilio : "+15555555555 +17777777777|critical"
@@ -87,7 +90,7 @@ curl=""
90 # pd : "<pd_service_key_1> <pd_service_key_2>|critical"
91 #
92 # If a recipient is set to empty string, the default recipient of the given
90 -# notification method (email, pushover, telegram, slack, etc) will be used.
93 +# notification method (email, pushover, telegram, slack, alerta, etc) will be used.
94 # To disable a notification, use the recipient called: disabled
95 # This works for all notification methods (including the default recipients).
96
@@ -275,6 +278,26 @@ SLACK_WEBHOOK_URL=""
278 DEFAULT_RECIPIENT_SLACK=""
279
280
281 +#------------------------------------------------------------------------------
282 +# alerta (alerta.io) global notification options
283 +
284 +# multiple recipients can be given like this:
285 +# "Environment1 Environment1 ..."
286 +
287 +# enable/disable sending alerta notifications
288 +SEND_ALERTA="YES"
289 +
290 +# set your alerta server API url
291 +ALERTA_WEBHOOK_URL=""
292 +
293 +# Login to you arerta server and create an API KEY with write permisions
294 +ALERTA_API_KEY=""
295 +
296 +# if a role's recipients are not configured, a notification will be send to
297 +# this Environment (empty = do not send a notification for unconfigured roles):
298 +DEFAULT_RECIPIENT_ALERTA=""
299 +
300 +
301 #------------------------------------------------------------------------------
302 # flock (flock.com) global notification options
303
plugins.d/alarm-notify.sh
+82
@@ -16,6 +16,7 @@
16 # Supported notification methods:
17 # - emails by @ktsaou
18 # - slack.com notifications by @ktsaou
19 +# - alerta.io notifications by @kattunga
20 # - discordapp.com notifications by @lowfive
21 # - pushover.net notifications by @ktsaou
22 # - pushbullet.com push notifications by Tiago Peralta @tperalta82 #1070
@@ -219,6 +220,7 @@ sendmail=
220
221 # enable / disable features
222 SEND_SLACK="YES"
223 +SEND_ALERTA="YES"
224 SEND_FLOCK="YES"
225 SEND_DISCORD="YES"
226 SEND_PUSHOVER="YES"
@@ -238,6 +240,12 @@ SLACK_WEBHOOK_URL=
240 DEFAULT_RECIPIENT_SLACK=
241 declare -A role_recipients_slack=()
242
243 +# alerta configs
244 +ALERTA_WEBHOOK_URL=
245 +ALERTA_API_KEY=
246 +DEFAULT_RECIPIENT_ALERTA=
247 +declare -A role_recipients_alerta=()
248 +
249 # flock configs
250 FLOCK_WEBHOOK_URL=
251 DEFAULT_RECIPIENT_FLOCK=
@@ -386,6 +394,7 @@ filter_recipient_by_criticality() {
394 # find the recipients' addresses per method
395
396 declare -A arr_slack=()
397 +declare -A arr_alerta=()
398 declare -A arr_flock=()
399 declare -A arr_discord=()
400 declare -A arr_pushover=()
@@ -479,6 +488,14 @@ do
488 [ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
489 done
490
491 + # alerta
492 + a="${role_recipients_alerta[${x}]}"
493 + [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_ALERTA}"
494 + for r in ${a//,/ }
495 + do
496 + [ "${r}" != "disabled" ] && filter_recipient_by_criticality alerta "${r}" && arr_alerta[${r/|*/}]="1"
497 + done
498 +
499 # flock
500 a="${role_recipients_flock[${x}]}"
501 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_FLOCK}"
@@ -517,6 +534,10 @@ done
534 to_slack="${!arr_slack[*]}"
535 [ -z "${to_slack}" ] && SEND_SLACK="NO"
536
537 +# build the list of alerta recipients (channels)
538 +to_alerta="${!arr_alerta[*]}"
539 +[ -z "${to_alerta}" ] && SEND_ALERTA="NO"
540 +
541 # build the list of flock recipients (channels)
542 to_flock="${!arr_flock[*]}"
543 [ -z "${to_flock}" ] && SEND_FLOCK="NO"
@@ -577,6 +598,9 @@ done
598 # check slack
599 [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
600
601 +# check alerta
602 +[ -z "${ALERTA_WEBHOOK_URL}" ] && SEND_ALERTA="NO"
603 +
604 # check flock
605 [ -z "${FLOCK_WEBHOOK_URL}" ] && SEND_FLOCK="NO"
606
@@ -624,6 +648,7 @@ fi
648 if [ \( \
649 "${SEND_PUSHOVER}" = "YES" \
650 -o "${SEND_SLACK}" = "YES" \
651 + -o "${SEND_ALERTA}" = "YES" \
652 -o "${SEND_FLOCK}" = "YES" \
653 -o "${SEND_DISCORD}" = "YES" \
654 -o "${SEND_HIPCHAT}" = "YES" \
@@ -644,6 +669,7 @@ if [ \( \
669 SEND_PUSHBULLET="NO"
670 SEND_TELEGRAM="NO"
671 SEND_SLACK="NO"
672 + SEND_ALERTA="NO"
673 SEND_FLOCK="NO"
674 SEND_DISCORD="NO"
675 SEND_TWILIO="NO"
@@ -671,6 +697,7 @@ if [ "${SEND_EMAIL}" != "YES" \
697 -a "${SEND_PUSHOVER}" != "YES" \
698 -a "${SEND_TELEGRAM}" != "YES" \
699 -a "${SEND_SLACK}" != "YES" \
700 + -a "${SEND_ALERTA}" != "YES" \
701 -a "${SEND_FLOCK}" != "YES" \
702 -a "${SEND_DISCORD}" != "YES" \
703 -a "${SEND_TWILIO}" != "YES" \
@@ -1247,6 +1274,51 @@ EOF
1274 return 1
1275 }
1276
1277 +# -----------------------------------------------------------------------------
1278 +# alerta sender
1279 +
1280 +send_alerta() {
1281 + local webhook="${1}" channels="${2}" httpcode sent=0 channel severity content
1282 +
1283 + [ "${SEND_ALERTA}" != "YES" ] && return 1
1284 +
1285 + case "${status}" in
1286 + WARNING) severity="warning" ;;
1287 + CRITICAL) severity="critical" ;;
1288 + CLEAR) severity="cleared" ;;
1289 + *) severity="unknown" ;;
1290 + esac
1291 +
1292 + info=$( echo -n ${info})
1293 +
1294 + for channel in ${channels}
1295 + do
1296 + content="{"
1297 + content="$content \"resource\": \"${chart}.${name}\","
1298 + content="$content \"event\": \"${alarm}\","
1299 + content="$content \"environment\": \"${channel}\","
1300 + content="$content \"service\": [\"${host}\"],"
1301 + content="$content \"severity\": \"${severity}\","
1302 + content="$content \"value\": \"${status_message}\","
1303 + content="$content \"text\": \"${info}\""
1304 + content="$content }"
1305 +
1306 + httpcode=$(docurl -X POST "${webhook}" -H "Content-Type: application/json" -H "Authorization: Key $ALERTA_API_KEY" -d "$content" )
1307 +
1308 + if [[ "${httpcode}" = "200" || "${httpcode}" = "201" ]]
1309 + then
1310 + info "sent alerta notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1311 + sent=$((sent + 1))
1312 + else
1313 + error "failed to send alerta notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1314 + fi
1315 + done
1316 +
1317 + [ ${sent} -gt 0 ] && return 0
1318 +
1319 + return 1
1320 +}
1321 +
1322 # -----------------------------------------------------------------------------
1323 # flock sender
1324
@@ -1465,6 +1537,15 @@ raised_for_html=
1537 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
1538 SENT_SLACK=$?
1539
1540 +# -----------------------------------------------------------------------------
1541 +# send the alerta notification
1542 +
1543 +# alerta aggregates posts from the same username
1544 +# so we use "${host} ${status}" as the bot username, to make them diff
1545 +
1546 +send_alerta "${ALERTA_WEBHOOK_URL}" "${to_alerta}"
1547 +SENT_ALERTA=$?
1548 +
1549 # -----------------------------------------------------------------------------
1550 # send the flock notification
1551
@@ -1733,6 +1814,7 @@ if [ ${SENT_EMAIL} -eq 0 \
1814 -o ${SENT_PUSHOVER} -eq 0 \
1815 -o ${SENT_TELEGRAM} -eq 0 \
1816 -o ${SENT_SLACK} -eq 0 \
1817 + -o ${SENT_ALERTA} -eq 0 \
1818 -o ${SENT_FLOCK} -eq 0 \
1819 -o ${SENT_DISCORD} -eq 0 \
1820 -o ${SENT_TWILIO} -eq 0 \