Icinga2 Telegram Push Notifications

Tutorial: How to send Icinga2 Push Notifications to the Smartphone Chat App Telegram? 1) Get your chat id 2) Check if you can communicate with your smartphone over the telegram

Icinga2 Notifications to Telegram
Icinga2 Notifications to Telegram

Currently my icinga2 notification is running with pushover.net.

I wanted to change it to the messenger app telegram because it has a nice API and it is also my primary chat app.

Another good point at telegram is a push notification like pushover but you don’t have to pay for the service. It is totally free.

BotFather is a telegram chat bot. With this bot you can configure your own telegram notification and get the api key to send messages for icinga2.

Search for „@BotFather“ in telegram and you will find that bot.

https://web.telegram.org/#/im

When you send „/start“ to the bot you will become a list of the possible commands. (@botfather)

Then you write „/newbot“ to the BotFather. He answers with the question of how do i call the new bot.

I called him „icinga2bot“. Then he asks for a username. You can also set it to „icinga2bot“.

At this point you get the http api key.

It looks like that: 29872349285:AAFabbabababbababababbaGC4

Get your chat id

Now you must write your new bot a random message. Not BotFather! The name you defined a step before.

Search your bot username in the telegram web chat. When you find your bot write him a random text like „foobar“.

bildschirmfoto-vom-2016-10-07-11-10-57

Execute this command at your ssh console or visit the url in the browser: (replace your api key and don’t delete „bot“. It must stand there)

curl –silent „https://api.telegram.org/botYOURAPIKEY/getUpdates“

With this command you get the new messages from your created bot.

Here you can see my „foobar“ message with the chat id we need:

Thats the text you have sent your own bot before. Just over the API.

"message":{"message_id":2,"from":{"id":THISISYOURCHATID,"first_name":"yourname","username":"yourusername"},"chat":
{"id":YOURCHATID,"first_name":"yourname","username":"yourusername","type":"private"},"date":1475831439,"text":"foobar"}}]}

Telegram API

Check if you can communicate with your smartphone over the telegram api:

/usr/bin/curl --silent --output /dev/null --data-urlencode "chat_id=YOURCHATID" --data-urlencode "text=Hallo welt"  "https://api.telegram.org/botYOURAPIKEY/sendMessage"

It works! „Hallo welt“ was sent over the telegram API.

icinga2 telegram push notifications

Icinga2 integration

Add the notification user (insert your own chatid)

vim /etc/icinga2/zones.d/master/users.conf
object User "mattionline" {
 import "generic-user"
 display_name = "mattionline"
 vars.telegram_chat_id = "YOURCHATID"
}

Add the apply notification template for the services and hosts

vim /etc/icinga2/zones.d/master/notifications.conf
apply Notification "telegram-icingaadmin" to Host {
  import "mail-host-notification"
  command = "telegram-host-notification"
  users = [ "mattionline" ]
  interval = 0
  assign where host.name
}
apply Notification "telegram-icingaadmin" to Service {
  import "mail-service-notification"
  command = "telegram-service-notification"
  users = [ "mattionline" ]
  interval = 0
  assign where host.name
}

Create the text template which is sent to your device. (insert your api key)

vim /etc/icinga2/zones.d/master/commands/telegram.conf
object NotificationCommand "telegram-host-notification" {
  import "plugin-notification-command"
  command = [ SysconfDir + "/icinga2/scripts/telegram-host-notification.sh" ]
  env = {
    NOTIFICATIONTYPE = "$notification.type$"
    HOSTNAME = "$host.name$"
    HOSTALIAS = "$host.display_name$"
    HOSTADDRESS = "$address$"
    HOSTSTATE = "$host.state$"
    LONGDATETIME = "$icinga.long_date_time$"
    HOSTOUTPUT = "$host.output$"
    NOTIFICATIONAUTHORNAME = "$notification.author$"
    NOTIFICATIONCOMMENT = "$notification.comment$"
    HOSTDISPLAYNAME = "$host.display_name$"
    TELEGRAM_BOT_TOKEN = "YOURAPIKEY"
    TELEGRAM_CHAT_ID = "$user.vars.telegram_chat_id$"
    // optional
    ICINGAWEB2_URL = "https://icinga.example.com/icingaweb2"
  }
}
object NotificationCommand "telegram-service-notification" {
  import "plugin-notification-command"
  command = [ SysconfDir + "/icinga2/scripts/telegram-service-notification.sh" ]
  env = {
    NOTIFICATIONTYPE = "$notification.type$"
    SERVICEDESC = "$service.name$"
    HOSTNAME = "$host.name$"
    HOSTALIAS = "$host.display_name$"
    HOSTADDRESS = "$address$"
    SERVICESTATE = "$service.state$"
    LONGDATETIME = "$icinga.long_date_time$"
    SERVICEOUTPUT = "$service.output$"
    NOTIFICATIONAUTHORNAME = "$notification.author$"
    NOTIFICATIONCOMMENT = "$notification.comment$"
    HOSTDISPLAYNAME = "$host.display_name$"
    SERVICEDISPLAYNAME = "$service.display_name$"
    TELEGRAM_BOT_TOKEN = "YOURAPIKEY"
    TELEGRAM_CHAT_ID = "$user.vars.telegram_chat_id$"
    // optional
    ICINGAWEB2_URL = "https://icinga.example.com/icingaweb2"
  }
}

Copy the telegram-host-notification.sh and the telegram-service-notification.sh from https://github.com/lazyfrosch/icinga2-telegram to your scripts folder

Don’t forget the chmod +x

Restart icinga2 and it should work.

Be in mind that the notification is just sent in the hard state. That means that just if the check fails 5 times you are getting a message. (less time span, 1 minute check if you need it)

The result

icinga2 - telegram push notifications

15 Kommentare zu „Icinga2 Telegram Push Notifications“

  1. Hi,

    I have configured my icinga2 as above (fresh install on ubuntu vm at home) and manged to get it working to some degree.

    When I manually run the curl command I get the message straight away.

    When I manually run the telegram-host-notification.sh script I also get a notification straight away.

    When I rely on Icinga to send the notification however, it seems inconsistent and sends sometimes but not others. I have not done a lot of testing to be able to tell, but I almost think the notifications are extremely delayed.

    Any ideas?

      1. Hi Marthias,

        Firstly, let me say, this was a great tutorial and easy to follow. Thanks for sharing!

        Re: my issue

        I did know of that behaviour and took it into consideration but I don’t think it is that. One way of testing I have tried is, in icingaweb2 clicking „send notification“ and adding some text then clicking send. On one occasion, it sent within about 15 seconds, and I received the message. However, I tried to send another 7 messages, and neither came through even after waiting 9 hours.

        Strange.

        1. I whnat to sent al telegram for every error, not only hard state.. and from the webpage it’s working great but recieve noting automaticly when go in some state

  2. Seems great, but can’t get this to work… Where should you do this:

    „Execute this command at your console:“

    Which console? Do you need telegram-cli? Tried it, doesn’t accept the command as well.

    Would be great to get this to work.

  3. Small addition, when I run the command just on my Linux server, I get this (which is not enough):

    # curl –silent „https://api.telegram.org/bot541129773:AAFIgfwelhvbbjGY8nTG8zzX0M/getUpdates“

    {„ok“:true,“result“:[]}

  4. allgemein benötige ich zurzeit die notification funktion noch nicht
    der definierte user unter /etc/icinga2/zones.d/master/users.conf
    muss der auch im overview contacts ersichtlich sein?
    gruss
    vinc

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Nach oben scrollen