├── .gitignore ├── LICENSE ├── README.md ├── img ├── old-zabbix-media-type.png ├── slack-integration.png ├── terminal-example-message.png ├── zabbix-media-type-test-message.png ├── zabbix-media-type-test.png ├── zabbix-media-type.png └── zabbix-user.png └── slack.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.save 3 | .* 4 | !.gitignore 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Eric O'Callaghan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Zabbix Slack AlertScript 2 | ======================== 3 | 4 | 5 | About 6 | ----- 7 | This is simply a Bash script that uses the custom alert script functionality within [Zabbix](http://www.zabbix.com/) along with the incoming web-hook feature of [Slack](https://slack.com/). I originally created this since I could not find any already existing/similar scripts at the time. This is [one of many](https://www.zabbix.com/integrations/slack) now. 8 | 9 | #### Versions 10 | 11 | This works with Zabbix 1.8 or greater - including 2.x and 3.x, as well as the newest 4.0 and 4.2 versions! 12 | 13 | ##### 4.4 14 | 15 | The latest version of Zabbix (4.4) has included its own built-in webhook media type which is documented here: 16 | 17 | [Official Zabbix Documentation 4.4 > Media Types > Webhook](https://www.zabbix.com/documentation/4.4/manual/config/notifications/media/webhook) 18 | 19 | The Zabbix 4.4 webhook documentation includes a specific example of how to set up Slack webhook notifications with their new included feature. 20 | 21 | #### Thanks 22 | Huge thanks and appreciation to every one who has submitted issues or pull requests including: 23 | 24 | * [Paul Reeves](https://github.com/pdareeves/) for the hint that Slack changed their API/URLs! 25 | * [Igor Shishkin](https://github.com/teran) for the ability to message users as well as channels! 26 | * Leslie at AspirationHosting for confirming that this script worked on Zabbix 1.8.2! 27 | * [Hiromu Yakura](https://github.com/hiromu) for escaping quotation marks in the fields received from Zabbix to have valid JSON! 28 | * [Devlin Gonçalves](https://github.com/devlinrcg), [tkdywc](https://github.com/tkdywc), [damaarten](https://github.com/damaarten), and [lunchables](https://github.com/lunchables) for Zabbix 3.0 AlertScript documentation, suggestions and testing! 29 | 30 | Installation 31 | ------------ 32 | 33 | ### The script itself 34 | 35 | This [`slack.sh` script](slack.sh) needs to be placed in the `AlertScriptsPath` directory that is specified within the Zabbix servers' configuration file (`zabbix_server.conf`) and must be executable by the user running the zabbix_server binary (usually "zabbix") on the Zabbix server: 36 | 37 | [root@zabbix ~]# grep AlertScriptsPath /etc/zabbix/zabbix_server.conf 38 | ### Option: AlertScriptsPath 39 | AlertScriptsPath=/usr/local/share/zabbix/alertscripts 40 | 41 | [root@zabbix ~]# ls -lh /usr/local/share/zabbix/alertscripts/slack.sh 42 | -rwxr-xr-x 1 root root 1.4K Dec 27 13:48 /usr/local/share/zabbix/alertscripts/slack.sh 43 | 44 | If you do change `AlertScriptsPath` (or any other values) within `zabbix_server.conf`, a restart of the Zabbix server software is required. 45 | 46 | Configuration 47 | ------------- 48 | 49 | ### Slack.com web-hook 50 | 51 | An incoming web-hook integration must be created within your Slack.com account which can be done at [https://my.slack.com/services/new/incoming-webhook](https://my.slack.com/services/new/incoming-webhook) as shown below: 52 | 53 | ![Slack.com Incoming Web-hook Integration](img/slack-integration.png "Slack.com Incoming Web-hook Integration") 54 | 55 | Given the above screenshot, the incoming Slack.com web-hook URL would be: 56 | 57 | https://hooks.slack.com/services/QW3R7Y/D34DC0D3/BCADFGabcDEF123 58 | 59 | Make sure that you specify your correct Slack.com incoming web-hook URL and feel free to edit the sender user name at the top of the script: 60 | 61 | # Slack incoming web-hook URL and user name 62 | url='https://hooks.slack.com/services/QW3R7Y/D34DC0D3/BCADFGabcDEF123' 63 | username='Zabbix' 64 | 65 | 66 | ### Within the Zabbix web interface 67 | 68 | When logged in to the Zabbix servers web interface with super-administrator privileges, navigate to the "Administration" tab, access the "Media Types" sub-tab, and click the "Create media type" button. 69 | 70 | You need to create a media type as follows: 71 | 72 | * **Name**: Slack 73 | * **Type**: Script 74 | * **Script name**: slack.sh 75 | 76 | ...and ensure that it is enabled before clicking "Save", like so: 77 | 78 | ![Old Zabbix Media Type](img/old-zabbix-media-type.png "Old Zabbix Media Type") 79 | 80 | However, on Zabbix 3.x and greater (including 4.x), media types are configured slightly differently and you must explicity define the parameters sent to the `slack.sh` script. On Zabbix 3.x and 4.x, three script parameters should be added as follows: 81 | 82 | * `{ALERT.SENDTO}` 83 | * `{ALERT.SUBJECT}` 84 | * `{ALERT.MESSAGE}` 85 | 86 | ...as shown here: 87 | 88 | ![Zabbix Media Type](img/zabbix-media-type.png "Zabbix Media Type") 89 | 90 | An optional fourth script parameter can be added to over-ride the `url` value that is hard-coded in the `slack.sh` script. This makes it possible to re-use the script for multiple Slack teams. 91 | 92 | An optional fifth script parameter can be added for proxy support. You can supply a `host:port` value (such as `example.com:8080`) that will be used when connecting to the Slack.com web-hook URL. 93 | 94 | Then, create a "Slack" user on the "Users" sub-tab of the "Administration" tab within the Zabbix servers web interface and specify this users "Media" as the "Slack" media type that was just created with the Slack.com channel ("#alerts" in the example) or user name (such as "@ericoc") that you want messages to go to in the "Send to" field as seen below: 95 | 96 | ![Zabbix User](img/zabbix-user.png "Zabbix User") 97 | 98 | Finally, an action can then be created on the "Actions" sub-tab of the "Configuration" tab within the Zabbix servers web interface to notify the Zabbix "Slack" user. 99 | You can stick with the default subject and message text for your problem (and optional recovery) operations. 100 | 101 | Additionally, you can have multiple different Zabbix users each with "Slack" media types that notify unique Slack users or channels upon different triggered Zabbix actions. 102 | 103 | 104 | Testing 105 | ------- 106 | 107 | The new [Testing media types](https://www.zabbix.com/documentation/4.2/manual/web_interface/frontend_sections/administration/mediatypes#testing_media_types) feature within Zabbix 4.2 should allow you to test sending messages to Slack: 108 | 109 | ![Zabbix Media Type Test](img/zabbix-media-type-test.png "Zabbix Media Type Test") 110 | 111 | ![Zabbix Media Type Test Message](img/zabbix-media-type-test-message.png "Zabbix Media Type Test Message") 112 | 113 | Alternatively, you can execute the script manually (as opposed to via Zabbix) from Bash on a terminal: 114 | 115 | $ bash slack.sh '@ericoc' 'Problem: This is a test' 'This is a test of something being wrong!' 116 | 117 | Alerting a specific user name results in the message actually coming from the "Slackbot" user using a sort-of "spoofed" user name within the message. A channel alert is sent as you would normally expect from whatever user name you specify in `slack.sh`: 118 | 119 | ![Slack Terminal Example Message](img/terminal-example-message.png "Slack Terminal Example Message") 120 | 121 | 122 | More Information 123 | ---------------- 124 | * [Slack incoming web-hook functionality](https://my.slack.com/services/new/incoming-webhook) 125 | * [Zabbix custom alertscripts documentation](https://www.zabbix.com/documentation/current/manual/config/notifications/media/script) 126 | -------------------------------------------------------------------------------- /img/old-zabbix-media-type.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericoc/zabbix-slack-alertscript/2a78db5b9298bbec84be54382e2c0b6a86b95517/img/old-zabbix-media-type.png -------------------------------------------------------------------------------- /img/slack-integration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericoc/zabbix-slack-alertscript/2a78db5b9298bbec84be54382e2c0b6a86b95517/img/slack-integration.png -------------------------------------------------------------------------------- /img/terminal-example-message.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericoc/zabbix-slack-alertscript/2a78db5b9298bbec84be54382e2c0b6a86b95517/img/terminal-example-message.png -------------------------------------------------------------------------------- /img/zabbix-media-type-test-message.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericoc/zabbix-slack-alertscript/2a78db5b9298bbec84be54382e2c0b6a86b95517/img/zabbix-media-type-test-message.png -------------------------------------------------------------------------------- /img/zabbix-media-type-test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericoc/zabbix-slack-alertscript/2a78db5b9298bbec84be54382e2c0b6a86b95517/img/zabbix-media-type-test.png -------------------------------------------------------------------------------- /img/zabbix-media-type.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericoc/zabbix-slack-alertscript/2a78db5b9298bbec84be54382e2c0b6a86b95517/img/zabbix-media-type.png -------------------------------------------------------------------------------- /img/zabbix-user.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericoc/zabbix-slack-alertscript/2a78db5b9298bbec84be54382e2c0b6a86b95517/img/zabbix-user.png -------------------------------------------------------------------------------- /slack.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Slack incoming web-hook URL and user name 4 | url='CHANGEME' # example: url='https://hooks.slack.com/services/QW3R7Y/D34DC0D3/BCADFGabcDEF123' 5 | username='Zabbix' 6 | 7 | ## Values received by this script: 8 | # To = $1 / Slack channel or user to send the message to, specified in the Zabbix web interface; "@username" or "#channel" 9 | # Subject = $2 / subject of the message sent by Zabbix; by default, it is usually something like "(Problem|Resolved): Lack of free swap space on Zabbix server" 10 | # Message = $3 / message body sent by Zabbix; by default, it is usually approximately 4 lines detailing the specific trigger involved 11 | # Alternate URL = $4 (optional) / alternative Slack.com web-hook URL to replace the above hard-coded one; useful when multiple groups have seperate Slack teams 12 | # Proxy = $5 (optional) / proxy host including port (such as "example.com:8080") 13 | 14 | # Get the user/channel ($1), subject ($2), and message ($3) 15 | to="$1" 16 | subject="$2" 17 | message="$3" 18 | 19 | # Change message emoji and notification color depending on the subject indicating whether it is a trigger going in to problem state or recovering 20 | recoversub='^RECOVER(Y|ED)?$|^OK$|^Resolved.*' 21 | problemsub='^PROBLEM.*|^Problem.*' 22 | 23 | if [[ "$subject" =~ $recoversub ]]; then 24 | emoji=':smile:' 25 | color='#0C7BDC' 26 | elif [[ "$subject" =~ $problemsub ]]; then 27 | emoji=':frowning:' 28 | color='#FFC20A' 29 | else 30 | emoji=':question:' 31 | color='#CCCCCC' 32 | fi 33 | 34 | # Replace the above hard-coded Slack.com web-hook URL entirely, if one was passed via the optional 4th parameter 35 | url=${4-$url} 36 | 37 | # Use optional 5th parameter as proxy server for curl 38 | proxy=${5-""} 39 | if [[ "$proxy" != '' ]]; then 40 | proxy="-x $proxy" 41 | fi 42 | 43 | # Build JSON payload which will be HTTP POST'ed to the Slack.com web-hook URL 44 | payload="payload={\"channel\": \"${to//\"/\\\"}\", \ 45 | \"username\": \"${username//\"/\\\"}\", \ 46 | \"attachments\": [{\"fallback\": \"${subject//\"/\\\"}\", \"title\": \"${subject//\"/\\\"}\", \"text\": \"${message//\"/\\\"}\", \"color\": \"${color}\"}], \ 47 | \"icon_emoji\": \"${emoji}\"}" 48 | 49 | # Execute the HTTP POST request of the payload to Slack via curl, storing stdout (the response body) 50 | return=$(curl $proxy -sm 5 --data-urlencode "${payload}" $url -A 'zabbix-slack-alertscript / https://github.com/ericoc/zabbix-slack-alertscript') 51 | 52 | # If the response body was not what was expected from Slack ("ok"), something went wrong so print the Slack error to stderr and exit with non-zero 53 | if [[ "$return" != 'ok' ]]; then 54 | >&2 echo "$return" 55 | exit 1 56 | fi 57 | --------------------------------------------------------------------------------