@cryptotaxi247 / netdata-1 / commits / 883e0e7e8

alarm-notify.sh now supports disabling notification delivery for certain roles; slack appearance re-worked too

Costa Tsaousis (ktsaou) committed Sep 18, 2016 at 03:19 UTC 883e0e7e8f08c9c7907b2a403ca9dc3c1805b9a4
2 files changed +70 -30
conf.d/health_alarm_notify.conf
+5 -1
@@ -53,7 +53,11 @@ curl=""
53 # email : "user1@example.com user2@example.com|critical"
54 # pushover: "2987343...9437837 8756278...2362736|critical"
55 # slack : "alarms disasters|critical"
56 -
56 +#
57 +# If a recipient is set to empty string, the default recipient of the given
58 +# notification method (email, pushover, slack) will be used.
59 +# To disable a notification, use the recipient called: disabled
60 +# This works for all notification methods (including the default recipients).
61
62 ###############################################################################
63 # sending emails
plugins.d/alarm-notify.sh
+65 -29
@@ -16,6 +16,13 @@ then
16 exit 1
17 fi
18
19 +# defaults to allow running this script by hand
20 +NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
21 +NETDATA_CACHE_DIR="${NETDATA_CACHE_DIR-/var/cache/netdata}"
22 +[ -z "${NETDATA_REGISTRY_URL}" ] && NETDATA_REGISTRY_URL="https://registry.my-netdata.io"
23 +[ -z "${NETDATA_HOSTNAME}" ] && NETDATA_HOSTNAME="$(hostname)"
24 +[ -z "${NETDATA_REGISTRY_HOSTNAME}" ] && NETDATA_REGISTRY_HOSTNAME="${NETDATA_HOSTNAME}"
25 +
26 # -----------------------------------------------------------------------------
27 # parse command line parameters
28
@@ -150,7 +157,7 @@ do
157 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
158 for r in ${a//,/ }
159 do
153 - filter_recipient_by_criticality email "${r}" && arr_email[${r/|*/}]="1"
160 + [ "${r}" != "disabled" ] && filter_recipient_by_criticality email "${r}" && arr_email[${r/|*/}]="1"
161 done
162
163 # pushover
@@ -158,7 +165,7 @@ do
165 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
166 for r in ${a//,/ }
167 do
161 - filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
168 + [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
169 done
170
171 # slack
@@ -166,7 +173,7 @@ do
173 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
174 for r in ${a//,/ }
175 do
169 - filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
176 + [ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
177 done
178 done
179
@@ -216,7 +223,7 @@ fi
223 # check that we have at least a method enabled
224 if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_SLACK}" != "YES" ]
225 then
219 - echo >&2 "I don't have a means to send a notification. Sorry!"
226 + echo >&2 "All notification methods are disabled. Not sending a notification."
227 exit 1
228 fi
229
@@ -388,35 +395,64 @@ send_pushover() {
395 # slack sender
396
397 send_slack() {
391 - local webhook="${1}" channels="${2}" when="${3}" username="${4}" image="${5}" httpcode sent=0 channel color
398 + local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
399
393 - if [ "${SEND_SLACK}" = "YES" -a ! -z "${webhook}" -a ! -z "${channels}" -a ! -z "${username}" -a ! -z "${image}" ]
394 - then
400 + [ "${SEND_SLACK}" != "YES" ] && return 1
401
396 - case "${status}" in
397 - WARNING) color="warning" ;;
398 - CRITICAL) color="danger" ;;
399 - CLEAR) color="good" ;;
400 - *) color="#777777" ;;
401 - esac
402 + case "${status}" in
403 + WARNING) color="warning" ;;
404 + CRITICAL) color="danger" ;;
405 + CLEAR) color="good" ;;
406 + *) color="#777777" ;;
407 + esac
408
403 - for channel in ${channels}
404 - do
405 - httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode \
406 - "payload={\"channel\": \"#${channel}\", \"username\": \"${username}\", \"text\": \"${host} ${status_message} - ${chart} (${family}) ${alarm} - click <${goto_url}|here> to view the netdata dashboard.\", \"icon_url\": \"${image}\", \"attachments\": [{\"fallback\": \"${alarm} - ${info}\", \"color\": \"${color}\", \"title\": \"${alarm}\", \"title_link\": \"${goto_url}\", \"text\": \"${chart} (${family}): ${info}\", \"footer\": \"netdata\", \"footer_icon\": \"${images_base_url}/images/seo-performance-128.png\", \"ts\": ${when}}]}" \
407 - "${webhook}")
409 + for channel in ${channels}
410 + do
411 + payload="$(cat <<EOF
412 + {
413 + "channel": "#${channel}",
414 + "username": "netdata on ${host}",
415 + "icon_url": "${images_base_url}/images/seo-performance-128.png",
416 + "attachments": [
417 + {
418 + "fallback": "${alarm} - ${chart} (${family}) - ${info}",
419 + "pretext": "${host} ${status_message}",
420 + "color": "${color}",
421 + "title": "${alarm}",
422 + "title_link": "${goto_url}",
423 + "text": "${info}",
424 + "fields": [
425 + {
426 + "title": "${chart}",
427 + "short": true
428 + },
429 + {
430 + "title": "${family}",
431 + "short": true
432 + }
433 + ],
434 + "thumb_url": "${image}",
435 + "footer": "<${goto_url}|${host}>",
436 + "ts": ${when}
437 + }
438 + ]
439 + }
440 +EOF
441 + )"
442
409 - if [ "${httpcode}" == "200" ]
410 - then
411 - echo >&2 "${me}: Sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
412 - sent=$((sent + 1))
413 - else
414 - echo >&2 "${me}: Failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
415 - fi
416 - done
443 + echo "${payload}" >/tmp/slack.payload
444
418 - [ ${sent} -gt 0 ] && return 0
419 - fi
445 + httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
446 + if [ "${httpcode}" == "200" ]
447 + then
448 + echo >&2 "${me}: Sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
449 + sent=$((sent + 1))
450 + else
451 + echo >&2 "${me}: Failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
452 + fi
453 + done
454 +
455 + [ ${sent} -gt 0 ] && return 0
456
457 return 1
458 }
@@ -518,7 +554,7 @@ raised_for_html=
554 # slack aggregates posts from the same username
555 # so we use "${host} ${status}" as the bot username, to make them diff
556
521 -send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}" "${when}" "${host} ${status}" "${image}"
557 +send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
558 SENT_SLACK=$?
559
560 # -----------------------------------------------------------------------------