@cryptotaxi247 / netdata-1 / commits / caf7b045a

Improve support for slack recipients (#4765)

Fixes #3722 Fixes #4755 ##### Summary - Removed the default addition of '#' to the "channel", so it can support both channels and users - Add the '#' if it's not already there in the recipient, to specify a channel (for backwards compatibility). - If the recipient is just a '#', netdata will not be sending the channel at all. This means that users only need to configure the channel or the user on the Slack webhook. ##### Component Name health/notifications ##### Additional Information

Chris Akritidis committed Nov 28, 2018 at 08:44 UTC caf7b045a5d5ab87f61411f45e3eeeb3ffd161bc
3 files changed +35 -14
health/notifications/alarm-notify.sh.in
+15 -3
@@ -1476,9 +1476,21 @@ send_slack() {
1476
1477 for channel in ${channels}
1478 do
1479 + # Default entry in the recipient is without a hash in front (backwards-compatible). Accept specification of channel or user.
1480 + if [ "${channel::1}" != "#" ] && [ "${channel::1}" != "@" ] ; then channel="#$channel"; fi
1481 +
1482 + # If channel is equal to "#" then do not send the channel attribute at all. Slack also defines channels and users in webhooks.
1483 + if [ "${channel}" = "#" ] ; then
1484 + ch=""
1485 + chstr="without specifying a channel"
1486 + else
1487 + ch="\"channel\": \"${channel}\","
1488 + chstr="to '${channel}'"
1489 + fi
1490 +
1491 payload="$(cat <<EOF
1492 {
1481 - "channel": "#${channel}",
1493 + $ch
1494 "username": "netdata on ${host}",
1495 "icon_url": "${images_base_url}/images/banner-icon-144x144.png",
1496 "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
@@ -1511,10 +1523,10 @@ EOF
1523 httpcode=$(docurl -X POST --data-urlencode "payload=${payload}" "${webhook}")
1524 if [ "${httpcode}" = "200" ]
1525 then
1514 - info "sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1526 + info "sent slack notification for: ${host} ${chart}.${name} is ${status} ${chstr}"
1527 sent=$((sent + 1))
1528 else
1517 - error "failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1529 + error "failed to send slack notification for: ${host} ${chart}.${name} is ${status} ${chstr}, with HTTP error code ${httpcode}."
1530 fi
1531 done
1532
health/notifications/health_alarm_notify.conf
+6 -4
@@ -324,7 +324,7 @@ DEFAULT_RECIPIENT_TELEGRAM=""
324 # slack (slack.com) global notification options
325
326 # multiple recipients can be given like this:
327 -# "CHANNEL1 CHANNEL2 ..."
327 +# "RECIPIENT1 RECIPIENT2 ..."
328
329 # enable/disable sending slack notifications
330 SEND_SLACK="YES"
@@ -335,9 +335,11 @@ SEND_SLACK="YES"
335 # Get yours from: https://api.slack.com/incoming-webhooks
336 SLACK_WEBHOOK_URL=""
337
338 -# if a role's recipients are not configured, a notification will be send to
339 -# this slack channel (empty = do not send a notification for unconfigured
340 -# roles):
338 +# if a role's recipients are not configured, a notification will be send to:
339 +# - A slack channel (syntax: '#channel' or 'channel')
340 +# - A slack user (syntax: '@user')
341 +# - The channel or user defined in slack for the webhook (syntax: '#')
342 +# empty = do not send a notification for unconfigured roles
343 DEFAULT_RECIPIENT_SLACK=""
344
345 #------------------------------------------------------------------------------
health/notifications/slack/README.md
+14 -7
@@ -17,7 +17,7 @@ Set them in `/etc/netdata/health_alarm_notify.conf` (to edit it on your system r
17 # sending slack notifications
18
19 # note: multiple recipients can be given like this:
20 -# "CHANNEL1 CHANNEL2 ..."
20 +# "RECIPIENT1 RECIPIENT2 ..."
21
22 # enable/disable sending pushover notifications
23 SEND_SLACK="YES"
@@ -27,19 +27,26 @@ SEND_SLACK="YES"
27 # Without it, netdata cannot send slack notifications.
28 SLACK_WEBHOOK_URL="https://hooks.slack.com/services/XXXXXXXX/XXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
29
30 -# if a role recipient is not configured, a notification will be send to
31 -# this slack channel:
30 +# if a role's recipients are not configured, a notification will be send to:
31 +# - A slack channel (syntax: '#channel' or 'channel')
32 +# - A slack user (syntax: '@user')
33 +# - The channel or user defined in slack for the webhook (syntax: '#')
34 +# empty = do not send a notification for unconfigured roles
35 DEFAULT_RECIPIENT_SLACK="alarms"
36
37 ```
38
36 -You can define multiple channels like this: `alarms systems`.
37 -You can give different channels per **role** using these (at the same file):
39 +You can define multiple recipients like this: `# #alarms systems @myuser`.
40 +This example will send the alarm to:
41 +- The recipient defined in slack for the webhook (not known to netdata)
42 +- The channel 'alarms'
43 +- The channel 'systems'
44 +- The user @myuser
45 +
46 +You can give different recipients per **role** using these (at the same file):
47
48 ```
49 role_recipients_slack[sysadmin]="systems"
50 role_recipients_slack[dba]="databases systems"
51 role_recipients_slack[webmaster]="marketing development"
52 ```
44 -
45 -The keywords `systems`, `databases`, `marketing`, `development` are slack.com channels (they should already exist in slack).