Drücke "Enter", um den Text zu überspringen.

icinga2 – telegram push notifications

How to get push notifications from icinga2 via telegram:

icinga2-telegram-push-notification

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.

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!

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:

"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"}}]}

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

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

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

10 Comments

  1. Rion Oktober 25, 2016

    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?

    • mathias Oktober 25, 2016

      Icinga2 sends the message when the check is going in the „hard state“ mode. This means that after 5 tries the notification will be send. This could be your delay „problem“

      • Rion Oktober 25, 2016

        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.

        • mathias Oktober 26, 2016

          Have the emails been sent when you do it automatically (not manually triggering the mail)?
          Whats in your mail.log at this point?

  2. Henk März 26, 2018

    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. Henk März 26, 2018

    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. Henk März 26, 2018

    Sorry! Got it to work ;-). Misread, I was still talking to the botfather and not my created bot…

    • mathias März 26, 2018

      I have tried it again and also stuck there 😀
      Now i have written it more clearly

      • Henk März 27, 2018

        Haha I see. Great guide! Thanks.

  5. vinc Juli 31, 2018

    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

Schreibe einen Kommentar

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