Nagios notifications via Telegram

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 script that will send the alerts via Telegram:

wget -O /usr/local/bin/
chmod 755 /usr/local/bin/

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/ --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/ --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: 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! --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" --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" --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" --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "-23456789" --hoststate "UNREACHABLE" --hostname "hostname.domain.tld" --hostaddress "2001:DB8::1" --output "Network Unreachable (hostname.domain.tld)" --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "-23456789" --hoststate "DOWN" --hostname "hostname.domain.tld" --hostaddress "2001:DB8::1" --output "PING CRITICAL - Packet loss = 100%" --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 sunglasses


14 thoughts on “Nagios notifications via Telegram

  1. John

    Hi , if this codes still working at nagios xi? or anyone kindly help to intergrate nagios xi to telegram.
    Thanks and regards..

  2. Robert

    Thank you for this tutorial!

    I have problem in this part:

    define command {
    command_name notify-host-by-telegram
    command_line /usr/local/bin/ --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/", line 74, in
    File "/usr/local/bin/", line 66, in main
    user_id = int(
    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!

  3. pommi Post author

    @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)?

  4. Robert

    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/ --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/", line 74, in
    File "/usr/local/bin/", line 66, in main
    user_id = int(
    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!

  5. pommi Post author

    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.

  6. Stephan

    Trying to install this on OS X (10.11). It requires a TelegramBot:
    (/usr/local/bin)($) ./
    Traceback (most recent call last):
    File “./”, 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?

  7. pommi Post author requires twx.botapi, a Telegram Bot API client python library. You need to install that.

  8. Ale

    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 ?

    / –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”

  9. pommi Post author

    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.

  10. Ale

    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.

  11. Ale

    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: –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”

Leave a Reply

Your email address will not be published. Required fields are marked *