├── .gitignore ├── LICENSE ├── README.md ├── discord.sh └── doc ├── discord-zabbix-notification.png ├── zabbix-discord-user-media-config.png └── zabbix-media-type-config.png /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 atomy (Marcel Lamm) 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 | # Overview 2 | This is an alert-script for notifications to [Discord](https://discordapp.com/), issued from [Zabbix](https://www.zabbix.com/). 3 | It uses Discord-webhooks. 4 | 5 | ![Image of Discord-Notifcation](https://github.com/atomy/zabbix-discord/blob/master/doc/discord-zabbix-notification.png) 6 | 7 | # Prerequisites 8 | - Discord Webhook [Howto Setup Discord Webhook](https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks) 9 | 10 | # Installation 11 | 1. set env *WEBHOOK_URL* to your previously setup discord-web-hook-url in `discord.sh` (e.g. `https://discordapp.com/api/webhooks/xxx/yyy`) 12 | 2. (optional) set env *ZABBIX_URL* in bash-script `discord.sh` to aim towards your zabbix installation (e.g. `https://zabbix.your-domain-bleh.foo/`), this will be used to link the discord-messages to your zabbix installation 13 | 3. configure `AlertScriptsPath` of your zabbix installation - [Zabbix Howto](https://www.zabbix.com/documentation/3.4/manual/config/notifications/media/script) 14 | 4. add the `discord.sh` script to your zabbix `AlertScriptsPath` directory 15 | 5. make sure zabbix can access that file and it also has execution permission (`chmod +x discord.sh`) 16 | 6. configure your custom alertscript in zabbix - [Zabbix Howto](https://www.zabbix.com/documentation/3.4/manual/config/notifications/media/script), 17 | script parameters are (as shown in zabbix documentation): 18 | ``` 19 | {ALERT.SENDTO} 20 | {ALERT.SUBJECT} 21 | {ALERT.MESSAGE} 22 | ``` 23 | 7. add media to a zabbix-user to be notified that way, the "Send to"-field is the discord-channel name here, e.g. #zabbix. 24 | 8. (optional) probably re-configure zabbix-action for different message-format 25 | 26 | # Additional Info 27 | I build a regexp into the script to detect if it's either a *PROBLEM*, *OK* or *Resolved* (never got things like {EVENT.VALUE} working) 28 | 29 | ## Example webhook for discord 30 | ```$ curl -X POST --data '{ "embeds": [{"title": "", "url": "", "description": "", "type": "link", "thumbnail": {"url": "https://storage.googleapis.com/material-design/publish/material_v_12/assets/0Bx4BSt6jniD7dFBUNHdrY05jQ3c/style-logos-product-intro-definition.png"}}] }' -H "Content-Type: application/json" https://discordapp.com/api/webhooks/xxx/yyyy``` 31 | 32 | ## Zabbix Media type config 33 | ![Image of Discord-Notifcation](https://github.com/atomy/zabbix-discord/blob/master/doc/zabbix-media-type-config.png) 34 | 35 | ## Zabbix User media config 36 | ![Image of Discord-Notifcation](https://github.com/atomy/zabbix-discord/blob/master/doc/zabbix-discord-user-media-config.png) 37 | -------------------------------------------------------------------------------- /discord.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | IN_TO=$1 6 | IN_SUBJECT=$2 7 | IN_BODY=$3 8 | 9 | # tweak to your needs 10 | ZABBIX_URL='https://zabbix.your-domain-bleh.foo/' 11 | WEBHOOK_URL='https://discordapp.com/api/webhooks/xxx/yyy' 12 | 13 | json_escape() { 14 | printf '%s' "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))' 15 | } 16 | 17 | CURL_TO=$(json_escape "${IN_TO}") 18 | CURL_SUBJECT=$(json_escape "${IN_SUBJECT}") 19 | CURL_BODY=$(json_escape "${IN_BODY}") 20 | 21 | if [[ "${IN_SUBJECT}" =~ ^(PROBLEM:|Problem:).*$ ]] ; then 22 | echo "We received a PROBLEM!" 23 | HTTP_RESPONSE=`curl --write-out %{http_code} --silent --output /dev/null -X POST --data '{ "embeds": [{"title": '"${CURL_SUBJECT}"', "url": "'"${ZABBIX_URL}"'", "description": '"${CURL_BODY}"', "type": "link", "thumbnail": {"url": "http://icons.iconarchive.com/icons/paomedia/small-n-flat/96/sign-warning-icon.png"}}] }' -H "Content-Type: application/json" ${WEBHOOK_URL}` 24 | elif [[ "${IN_SUBJECT}" =~ ^(OK:|Resolved:).*$ ]] ; then 25 | echo "We received OKAY!" 26 | HTTP_RESPONSE=`curl --write-out %{http_code} --silent --output /dev/null -X POST --data '{ "embeds": [{"title": '"${CURL_SUBJECT}"', "url": "'"${ZABBIX_URL}"'", "description": '"${CURL_BODY}"', "type": "link", "thumbnail": {"url": "http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/sign-check-icon.png"}}] }' -H "Content-Type: application/json" ${WEBHOOK_URL}` 27 | else 28 | echo "Dunno what this was: ${IN_SUBJECT}" 29 | HTTP_RESPONSE=`curl --write-out %{http_code} --silent --output /dev/null -X POST --data '{ "embeds": [{"title": '"${CURL_SUBJECT}"', "url": "'"${ZABBIX_URL}"'", "description": '"${CURL_BODY}"', "type": "link", "thumbnail": {"url": "http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/sign-question-icon.png"}}] }' -H "Content-Type: application/json" ${WEBHOOK_URL}` 30 | fi 31 | 32 | if [[ "${HTTP_RESPONSE}" != "204" ]] ; then 33 | echo "Received HTTP-Code: ${HTTP_RESPONSE}" 34 | exit 1 35 | else 36 | exit 0 37 | fi -------------------------------------------------------------------------------- /doc/discord-zabbix-notification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomy/zabbix-discord/3cc95bf2876bb23566dea641482958d7c89f1d6c/doc/discord-zabbix-notification.png -------------------------------------------------------------------------------- /doc/zabbix-discord-user-media-config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomy/zabbix-discord/3cc95bf2876bb23566dea641482958d7c89f1d6c/doc/zabbix-discord-user-media-config.png -------------------------------------------------------------------------------- /doc/zabbix-media-type-config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomy/zabbix-discord/3cc95bf2876bb23566dea641482958d7c89f1d6c/doc/zabbix-media-type-config.png --------------------------------------------------------------------------------