This post shows you how to use Telegram for Nagios notifications. First create a Telegram Bot by talking to the BotFather. The Telegram Bot will be the sender of the Nagios alerts.
You’ll receive an API token that also includes the UserID of the Bot:
- Token: 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw
- UserID: 200194008
Download the nagios_telegram.py script that will send the alerts via Telegram:
wget -O /usr/local/bin/nagios_telegram.py https://raw.githubusercontent.com/pommi/telegram_nagios/master/telegram_nagios.py chmod 755 /usr/local/bin/nagios_telegram.py
This is the configuration you need in Nagios (of course replace the token with your own):
# commands to send host/service notifications define command { command_name notify-host-by-telegram command_line /usr/local/bin/telegram_nagios.py --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "$CONTACTPAGER$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostname "$HOSTNAME$" --hostaddress "$HOSTADDRESS$" --output "$HOSTOUTPUT$" } define command { command_name notify-service-by-telegram command_line /usr/local/bin/telegram_nagios.py --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type service --contact "$CONTACTPAGER$" --notificationtype "$NOTIFICATIONTYPE$" --servicestate "$SERVICESTATE$" --hostname "$HOSTNAME$" --servicedesc "$SERVICEDESC$" --output "$SERVICEOUTPUT$" } # 2 example contact definitions define contact { contact_name John Doe pager 12345678 service_notification_commands notify-service-by-telegram host_notification_commands notify-host-by-telegram } define contact { contact_name Telegram Group Chat pager -23456789 service_notification_commands notify-service-by-telegram host_notification_commands notify-host-by-telegram }
The Telegram Nagios plugin is able to send alerts to a single contact or to a group chat. As you can see Telegram GroupIDs are negative numbers.
How to get your UserID or GroupID?
Download and install this Telegram CLI: https://github.com/vysheng/tg. The CLI makes it easier to discover your UserID and GroupIDs.
$ telegram-cli ... > get_self User John Doe @johndoe (#12345678): phone: XXXXXXXXXXX offline (was online [2016/03/15 11:57:46])
There is your UserID (#12345678). First start a conversation with the Bot you just created to be able to receive messages (Nagios alerts) from the Bot and to be able to invite it to a Telegram group chat.
To receive Nagios alerts in a Telegram group chat, create a group chat and invite the Bot. You need at least 2 other users in the group.
$ telegram-cli ... > create_group_chat "Nagios Alerts" user#200194008 user#12345678 user#33333333 [21:28] Nagios Alerts John Doe created chat Nagios Alerts. 3 users > chat_info Nagios_Alerts Chat Nagios Alerts updated photo admin members Chat Nagios Alerts (id 23456789) members: Nagios Bot invited by John Doe at [2016/03/08 21:28:59] ... John Doe invited by John Doe at [2016/03/08 21:28:59] admin
There is the GroupID (id 23456789) of the Nagios Alerts group chat which needs to be configured in the Nagios configuration as a negative number (-23456789).
Let’s send some test messages!
telegram_nagios.py --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type service --contact "-23456789" --servicestate "OK" --hostname "hostname.domain.tld" --servicedesc "load" --output "OK - load average: 0.02 0.01 0.01" telegram_nagios.py --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type service --contact "-23456789" --servicestate "WARNING" --hostname "hostname.domain.tld" --servicedesc "load" --output "WARNING - load average: 3.48 4.19 2.74" telegram_nagios.py --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type service --contact "-23456789" --servicestate "CRITICAL" --hostname "hostname.domain.tld" --servicedesc "load" --output "CRITICAL - load average: 233.29 154.35 15.05" telegram_nagios.py --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "-23456789" --hoststate "UNREACHABLE" --hostname "hostname.domain.tld" --hostaddress "2001:DB8::1" --output "Network Unreachable (hostname.domain.tld)" telegram_nagios.py --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "-23456789" --hoststate "DOWN" --hostname "hostname.domain.tld" --hostaddress "2001:DB8::1" --output "PING CRITICAL - Packet loss = 100%" telegram_nagios.py --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "-23456789" --hoststate "UP" --hostname "hostname.domain.tld" --hostaddress "2001:DB8::1" --output "PING OK - Packet loss = 0%, RTA = 3.74 ms
And here is the result
Hi , if this codes still working at nagios xi? or anyone kindly help to intergrate nagios xi to telegram.
Thanks and regards..
John
Thank you for this tutorial!
I have problem in this part:
define command {
command_name notify-host-by-telegram
command_line /usr/local/bin/telegram_nagios.py --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "$CONTACTPAGER$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostname "$HOSTNAME$" --hostaddress "$HOSTADDRESS$" --output "$HOSTOUTPUT$"
}
I made the changes as my bot but always returns me this error:
Traceback (most recent call last):
File "/usr/local/bin/nagios_telegram.py", line 74, in
main()
File "/usr/local/bin/nagios_telegram.py", line 66, in main
user_id = int(args.contact)
ValueError: invalid literal for int() with base 10: '$'
Running the tests at the end of your tutorial works perfectly. If I modify the $ CONTACTPAGER $ for the group id, I get in the telegram the hostname of my server …
linux01 $ ($): $
What might be happening?
Thank you in advance!
@John: I’m sorry. I don’t know about Nagios XI.
@Robert: Did you set the “pager” for the contact correctly in your Nagios config and did you reload Nagios afterwards? Can you show more lines of what your are doing when you say “I get in the telegram the hostname of my server” (I do not completely understand what is happening here)?
Thank you for attention @pommi
I set the pager in contact and did restart the nagios service.
What am I doing to test is this:
I’m getting the code that is in command.cfg in nagios and testing manually.
/usr/local/bin/nagios_telegram.py --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "$CONTACTPAGER$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostname "$HOSTNAME$" --hostaddress "$HOSTADDRESS$" --output "$HOSTOUTPUT$"
From what I understand, for some reason “$CONTACTPAGER$” is not being recognized as a variable and is giving the following error:
Traceback (most recent call last):
File "/usr/local/bin/nagios_telegram.py", line 74, in
main()
File "/usr/local/bin/nagios_telegram.py", line 66, in main
user_id = int(args.contact)
ValueError: invalid literal for int() with base 10: '$'
If I change the “$CONTACTPAGER$” to the id of the group, I can to get the message in the telegram but not in the expected way:
linux01 $ ($): $
linux01 is the hostname of my server.
I’m stuck on this question without knowing what to do 🙁
Thank you in advance!
This worked great! Thank you for the tutorial.
I worked Telegram into a Docker Compose project for monitoring my services.
https://github.com/dmp1ce/decompose-nagios/tree/master/skel
Hi Robert, Thanks for your explanation. When testing from the shell you need to replace all the “$VALUES$”. The “$VALUE$” is there for nagios to replace it by actual content. You can see examples in my blog post, how to send test messages. The reason you get “linux01 $ ($): $”, is because your shell interprets all things starting with a $-sign as environment variables. For example “$HOSTADDRESS”, which is empty so you get “$” (“$HOSTADDRESS$” minus “$HOSTADDRESS”).
P.S. You might want to “/revoke” the API key you just published in your comment. 😉 I replaced it with the example from the blog post.
Trying to install this on OS X (10.11). It requires a TelegramBot:
(/usr/local/bin)($) ./nagios_telegram.py
Traceback (most recent call last):
File “./nagios_telegram.py”, line 4, in
from twx.botapi import TelegramBot
I was able to install the telegram-cli. I am using python 2.7.10:
(/usr/local/bin)($) python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Any way to make this working?
nagios_telegram.py requires twx.botapi, a Telegram Bot API client python library. You need to install that.
Hi Pommi,
Thank you for you post.
I installed all the pre requisites ans execute the scipt to test the send, but i did not give any error.
How can i troubleshoot it to debug it ?
/telegram_nagios.py –token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw –object_type service –contact “+54xxxxx” –servicestate “OK” –hostname “hostname.domain.tld” –servicedesc “load” –output “OK – load average: 0.02 0.01 0.01”
Hi Ale, I’ve removed your token/API key, because it shouldn’t be published online 😉
What looks strange in your case is the – – contact option. It shouldn’t start with a +, it’s not a phone number. It is a Telegram GroupID or UserID. When using a UserID make sure you first start a conversation with your bot first. A bot cannot initiate a conversation to prevent spam on Telegram.
Hi Pommi,
First Thank you to Fix my Error 🙂 I really forget to clear it when i post it. Thank you Again.
For the other hand i will test to change the phone number and put your recommendation.
Thank you a lot.
Hi Pommi,
I am trying to send message to a group, but i have this error:
Error(error_code=400, description=”API doesn’t answer”)
The command i Use is:
telegram_nagios.py –token 444444444:AAXXXXXXXXXXXXXXXXX –object_type service –contact “-4444444” –servicestate “OK” –hostname “hostname.domain.tld” –servicedesc “load” –output “OK – load average: 0.02 0.01 0.01”
Pommi,
Thanks for this bot! It’s great!
Hi pommi
Thanks for this, I use it for our Nagios Core on Ubuntu server an after a bit of work it’s ok
Great job
Well done Pommi!!!
Thank you very much.
Alessandro
Hello. I’m running into an issue where when Nagios wants to send a notification I get an “ImportError: No module named twx.botapi” error. But when I send a test message I receive it with no problem. I cant make it work. What am I doing wrong?
Thanks
Hi Nicolas, that means that the twx.botapi python library is not available for the nagios user. Did you install the twx.botapi python library system wide?
Hi,
This is the error I get when running the script :
# ./telegram_nagios.py
Traceback (most recent call last):
File “./telegram_nagios.py”, line 4, in
from twx.botapi import TelegramBot
File “/usr/lib/python2.7/site-packages/twx/botapi/__init__.py”, line 1, in
from . botapi import *
File “/usr/lib/python2.7/site-packages/twx/botapi/botapi.py”, line 9, in
from requests import Request, Session
File “/usr/lib/python2.7/site-packages/requests/__init__.py”, line 61, in
from .packages.urllib3.exceptions import DependencyWarning
ImportError: cannot import name DependencyWarning
Could you tell what is the problem or missing module ?
Regards,
Laurent
Hi, I have the same problem
Traceback (most recent call last):
File “./nagios_telegram.py”, line 4, in
from twx.botapi import TelegramBot
ImportError: No module named twx.botapi
Requirement already satisfied: python-telegram-bot in /usr/lib/python2.6/site-packages
Requirement already satisfied: future>=0.15.2 in /usr/lib/python2.6/site-packages (from python-telegram-bot)
Requirement already satisfied: certifi in /usr/lib/python2.6/site-packages (from python-telegram-bot)
Requirement already satisfied: importlib in /usr/lib/python2.6/site-packages (from future>=0.15.2->python-telegram-bot)
Requirement already satisfied: argparse in /usr/lib/python2.6/site-packages (from future>=0.15.2->python-telegram-bot)
Hi Jose,
Yours is due to the missing module twx.botapi, please do :
pip install twx.botapi
After this, you will maybe have the same problem as I’m having.
Regards,
Laurent
Im triying to install it on a CentoOS7 server that i use to monitorize vm-machines, can anyone explain me how i install the twx.botapi here, i can’t find the way and i have all already configured.
Thanks in advance
Hello,
I try to followed the installation progress and seem everything is fine. But when i tried to send test message, I don’t receive it on my telegram group chat. Anyone suggestion please !!!!!
How to get the nice notifications with emojis?
@Thorsten: It’s part of telegram_nagios.py. Emoji are just unicode characters.
@pommi Why my notification always show like $ ($) : $ $
why the output that appears like this $ ($): $$. and why notification alert only to the service, not on host?
*From StackOverflow
In order to get the group/user chat id, do as follows:
1. Add the Telegram BOT to the group/chat.
2. Send him a any message.
3. Get the list of updates for your BOT:
https://api.telegram.org/bot/getUpdates
Ex:
https://api.telegram.org/bot123456789:jbd78sadvbdy63d37gda37bd8/getUpdates
5. Look for the “chat” object:
{“update_id”:8393,”message”:{“message_id”:3,”from”:{“id”:7474,”first_name”:”AAA”},”chat”:{“id”:,”title”:””},”date”:25497,”new_chat_participant”:{“id”:71,”first_name”:”NAME”,”username”:”YOUR_BOT_NAME”}}}
This is a sample of the response when you add your BOT into a group.
Use the “id” of the “chat” object to send your messages.
Hi,
Thanks for this. After a little bit of fiddling, and figuring out how to install twx.botapi, I have this working on CentOS 7.5.
Could someone point me in the right direction on how to reformat the messages sent from a service as they don’t look as clean as the host messages…
Host Messages:
? ss-fsl-durham (xxx.xxx.xxx.xxx): (Host check timed out after 30.01 seconds)
✅ ss-fsl-durham (xxx.xxx.xxx.xxx): PING OK – Packet loss = 60%, RTA = 94.25 ms
Service Messages:
? ss-fsl-durham/Chrome: chrome.exe: Running
How/where can I remove the ‘/’ that is just before ‘Chrome’?
Thanks
P.S. for those struggling with twx.botapi this is how I got it working on my CentOS install. You will have to adapt for your OS.
[nagios@monitor ~]# cd
[nagios@monitor ~]# yum install jansson-devel lua-devel readline-devel libconfig-devel libevent-devel openssl-devel python-pip
[nagios@monitor ~]# pip install –upgrade pip
[nagios@monitor ~]# git clone –recursive https://github.com/vysheng/tg.git
[nagios@monitor ~]# cd tg
[nagios@monitor ~]# ./configure
[nagios@monitor ~]# make
[nagios@monitor ~]# pip install twx.botapi
Hello to all,
I proceed configuration of this tool this morning at RedHat 7.3 Enterprise Linux. I had one issue with the configuration so I would like to share them with you.
1) Steps from Victor (nice name by the way 🙂 ) did not work I could not get any output from https://api.telegram.org/bot/getUpdates
Fix: remove BOT from group chat and then add BOT again
Rest of guide from Victor is working perfectly and there is no need to install https://github.com/vysheng/tg.git
I was searching for something useful to integrate IM into the process of monitoring our environment.
@pommi Thank you very much for this article and for python script as well. You are amazing.
Hello Everyone. Please, i search examples files host.cfg and service.cfg. How to add notify-host-by-telegram and notify-service-by-telegram that files. i need add commad event_handler notify-host-by-telegram ??? Please help me.
Hi, anyone know how I can add this part of the email notification command to Telegram?
https://blog.ls-al.com/nagios-email-notifications-with-comments/
Info:\n\n$SERVICEOUTPUT$\n\nACK Comment: $SERVICEACKCOMMENT$\n\nComment: $NOTIFICATIONCOMMENT$\n”
I’d like to receive also the comments by telegram
Hi,
I receive this error:
[1565006489] wproc: host=localhost; service=Service Status – httpd; contact=Grupo Chat Telegram
[1565006489] wproc: early_timeout=0; exited_ok=1; wait_status=256; error_code=0;
[1565006489] wproc: stderr line 01: Traceback (most recent call last):
[1565006489] wproc: stderr line 02: File “/usr/local/nagios/libexec//telegram_nagios.py”, line 4, in
[1565006489] wproc: stderr line 03: from twx.botapi import TelegramBot
[1565006489] wproc: stderr line 04: ImportError: No module named twx.botapi
I have the module installed in root and in nagios:
nagios@localhost var]$ pip install twx.botapi
Requirement already satisfied: twx.botapi in /usr/local/lib/python2.7/site-packages (3.6.2)
Requirement already satisfied: requests in /usr/local/lib/python2.7/site-packages (from twx.botapi) (2.21.0)
Requirement already satisfied: attrs in /usr/local/lib/python2.7/site-packages (from twx.botapi) (19.1.0)
Requirement already satisfied: enum34 in /usr/local/lib/python2.7/site-packages (from twx.botapi) (1.1.6)
Requirement already satisfied: urllib3=1.21.1 in /usr/local/lib/python2.7/site-packages (from requests->twx.botapi) (1.24.1)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/site-packages (from requests->twx.botapi) (2018.11.29)
Requirement already satisfied: chardet=3.0.2 in /usr/local/lib/python2.7/site-packages (from requests->twx.botapi) (3.0.4)
Requirement already satisfied: idna=2.5 in /usr/local/lib/python2.7/site-packages (from requests->twx.botapi) (
[root@localhost var]# pip install twx.botapi
Requirement already satisfied: twx.botapi in /usr/local/lib/python2.7/site-packages (3.6.2)
Requirement already satisfied: requests in /usr/local/lib/python2.7/site-packages (from twx.botapi) (2.21.0)
Requirement already satisfied: attrs in /usr/local/lib/python2.7/site-packages (from twx.botapi) (19.1.0)
Requirement already satisfied: enum34 in /usr/local/lib/python2.7/site-packages (from twx.botapi) (1.1.6)
Requirement already satisfied: urllib3=1.21.1 in /usr/local/lib/python2.7/site-packages (from requests->twx.botapi) (1.24.1)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/site-packages (from requests->twx.botapi) (2018.11.29)
Requirement already satisfied: chardet=3.0.2 in /usr/local/lib/python2.7/site-packages (from requests->twx.botapi) (3.0.4)
Requirement already satisfied: idna=2.5 in /usr/local/lib/python2.7/site-packages (from requests->twx.botapi) (2.8)
I execute the command by hand and it works but when nagios tries to execute it when it receives an error it fails with the error indicated above
regards