Zabbix-Notification-Telegram Documentation
Send alerts from Zabbix to Telegram
https://github.com/xxsokolov/Zabbix-Notification-Telegram
Source:Key Features
- Send graphs and last values in one message
- Flexable configuration of templates in message body
- Transfer data from action in XML format
- Links generation in message body
- Tag generation in message body
- Emoji mapping for status and importance of the event
- Image watermarks
- Generation and updates of cache file (privat, group -> supergroup)
Part 1, Backend
Installation
Zabbix use userzabbix
by default, this user doesn't have home folder and profile after default installation. For this reason we will setup everything from root
and then apply required permissions. If you have different setup and and your user have full functional shell enveronment then login as this user su - <zabbix_user>
- cd to directory
cd /usr/lib/zabbix/alertscripts/
- Clone latest repository, pay attention on the dot in the end
git clone https://github.com/xxsokolov/Zabbix-Notification-Telegram.git .
- Install virtual enveronment
sudo apt install virtualenv
- Create virtual environment
virtualenv venv --python=python3
- Activate virtual environment
source venv/bin/activate
- Install requirements
pip install -r .requirements
- deactivate source
deactivate
- Copy configuration file
cp zbxTelegram_config.example.py zbxTelegram_config.py
- Set permissions ( default user is zabbix by default )
chown -R zabbix:zabbix /usr/lib/zabbix/alertscripts/.[^.]*
- Make main script executable
chmod +x zbxTelegram.py
Follow instruction how to create bot and give bot permissions to send private messages for your user or a group as prefared.
- Edit configuration file
nano zbxTelegram_config.py
Configuration
zbxTelegram_config.py configuration
tg_proxy
= Use proxy True/False; default true True
tg_proxy_server
= Proxy server address
tg_token
= Telegram bot token for API access
zabbix_api_url
= Path to zabbix API (backslash '/' in the end is mandatory) example zabbix_api_url = http://127.0.0.1/
zabbix_api_login
= read-only user with read permissions to specific hosts, see blow in UI configuration section.
zabbix_api_pass
= read-only user password
watermark
= Send watermarks True/False; default True
To test config and API availability
From console:
./zbxTelegram.py @username test test
@username can be a group ID, like ‘-1092834323’
After testing from console and get 100% positive feedback we need to set permissions one more time:
chown -R zabbix:zabbix /usr/lib/zabbix/alertscripts/.[^.]*
If any errors present fix it, usually need to fix permissions for alertscripts folder.
check script log /lib/zabbix/alertscripts/zbxTelegram_files/znt.log
and var/log/zabbix/
for more details.
Part 2, Zabbix configuration from UI
Configuration Media types
Go to Zabbix front-end, Administration > Media Type
Name: ZNT
Type: Script
Script name: zbxTelegram.py
Script parameters:
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
Configure user media
For alerts we need to create user with read-only permissions. Zabbix allow permissions only trough groups. Create read-only group, then create user and add to read only group. Give read permissions only for required hosts.
Go to: Administration > Users
Choose your read-only user
and go to Media
tab
Add Telegram identity, group for example, or user name.
Now test media type from Zabbix UI
If any errors present fix it, check script log /lib/zabbix/alertscripts/zbxTelegram_files/znt.log
and var/log/zabbix/
for more details.
Setup Actions
Go to: Configurations > Action
Name: Anything you want, “Critical Alerts” for example.
Add required trigger:
It is possible to add as much triggers as we need and set the rules:
Activate Operations tab:
Here we can setup different actions:
All as example:
Send to User = your read-only user
( it is possible to add more users )
- Default subjects for
Operations/Recovery/Update
{Problem} {TRIGGER.SEVERITY} {{TRIGGER.SEVERITY}}: {EVENT.NAME}
{Resolved} {TRIGGER.SEVERITY} {{TRIGGER.SEVERITY}} {EVENT.NAME}
{Update} {TRIGGER.SEVERITY} {{TRIGGER.SEVERITY}} {EVENT.NAME}
Strings structure:
{Problem}
– mapping of values (Problem\Resolved\Update)
check the end of zbxTelegram_config.py
to get more ideas.
{{TRIGGER.SEVERITY}}
- mapping of values in Severity, check the end of zbxTelegram_config.py
to get more ideas.
- Default message
Inside messages we use XML syntax, default example inside actions.example file, please check.
The most simple working solution is just copy example content from actions.example in to Default message
.
actions.example: https://raw.githubusercontent.com/xxsokolov/Zabbix-Notification-Telegram/master/actions.example
Default message structure contain two sections:
<body>
<messages>
Actual message
</messages>
</body>
<settings>
Configuration
</settings>
<graphs></graphs>
- attach graph (True/False)
<graphlinks>True</graphlinks>
- attach url to history (True/False)
<triggerlinks>True</triggerlinks>
- attach link to trigger (True/False)
<tag>True</tag>
- attach tags (True/False)
<graphs_period></graphs_period>
graph time frame in seconds
<itemid></itemid>
- send Item ID {ITEM.ID1}
<triggerid></triggerid>
send triggers ID {TRIGGER.ID}
<eventid></eventid>
send event ID {EVENT.ID}
<title></title>
graph header {HOST.HOST} - {EVENT.NAME}
<triggerurl></triggerurl>
- send URL from trigger {TRIGGER.URL}
<tags></tags>
send tags from trigger {EVENT.TAGS}
- Press update, then activate your action.