# Using telegram as an alert system for Zabbix
This is a very simple and quick external script to allow Zabbix to report alerts to telegram.
There are three main steps to follow in order to get this working:
- Create and configure a Telegram bot.
- Add an external script to Zabbix.
- Configure a new type of media in Zabbix.
As a first step you'll need to create a new bot for Telegram as described in this link.
Upon creation you'll be given an API token. Be sure to save it somewhere.
Open Telegram and start a chat with your newly created bot.
Next you'll need the chat id for that conversation. In order to obtain it do a GET request to this URL: https://api.telegram.org/bot<YOUR_API_TOKEN>/getUpdates
. Be sure to replace <YOUR_API_TOKEN>
with your actual API token.
You can use a regular browser or a curl
call to get the output where you'll find the chat id. Save it somewhere.
# Creating and external script in Zabbix
Add the following script to your Zabbix external scripts directory; be sure to give it execute permisions.
#!/bin/bash
################################################################################
# Bash script para notificaciones a TELEGRAM desde Zabbix
# 13/09/2018 12:05
#
# @author: Héctor Luaces Novo <[email protected]>
#################################################################################
################################################################################
# Datos TELEGRAM
################################################################################
readonly TELEGRAM_API="YOUR_TELEGRAM_API"
readonly TELEGRAM_CHAT_ID="YOUR_CHAT_ID"
readonly TELEGRAM_ENDPOINT="https://api.telegram.org"
readonly TELEGRAM_FUNCTION="sendMessage"
################################################################################
# Datos Zabbix
################################################################################
# {ALERT.SENDTO}
readonly TO="$1"
# {ALERT.SUBJECT}
readonly SUBJECT="$2"
# {ALERT.MESSAGE}
readonly BODY="$3"
################################################################################
# Funciones
################################################################################
function main()
{
FECHA="$(date --iso-8601=seconds)"
TEXT="*($(hostname))* $SUBJECT: $BODY"
curl -i -X POST "$TELEGRAM_ENDPOINT/bot$TELEGRAM_API/$TELEGRAM_FUNCTION" \
-d "parse_mode=markdown&chat_id=$TELEGRAM_CHAT_ID&text=$TEXT"
exit $?
}
main "$@"
Be sure to replace the TELEGRAM_API
and TELEGRAM_CHAT_ID
variables with the ones obtained when you created your bot.
Access the Zabbix front-end and go to Administration → Media types → Create a media type
.
Create a new one with the following parameters:
- Name: Telegram
- Type: script
- Script parameters:
- {ALERT.SENDTO}
- {ALERT.SUBJECT}
- {ALERT.MESSAGE}
- Enabled: checked
After that simply go to Administration → Users
and add the media group to the users that need that reporting.