Telegram Notifications with Nagios

I’m using a little Nagios core server at home to monitor my systems (are updates available for my NAS or my Raspberry Pis and so on). Since I didn’t want to set up mail notifications for this, I was on the search for some tutorials how to set up Telegram notifications easily.

I found some tutorials, but none of them were really complete from start to finish, so I will just make one on my own.

Step 1 – Set up a Telegram bot

First we need a bot to send out our notifications, so we just set up one.

To do so, go to your Telegram (I recommend using the web interface for this), search for the user @BotFather and start a conversation with the bot.

To create a new bot, use the command /newbot

Give the bot a name (this is not the username of the bot, we will come to this later). So make it something like “Nagios notifications”

Next you will need to provide a username for the bot. It must end in “bot”, and this will be the ID of the bot (just like BotFather). This will make the bot searchable by anyone, so make it something only you know. The username needs to be unique, so adding a timestamp or at least the day will help. We will name the bot “NagiosNotification20191219_bot” for example.

Now your bot is almost done and you will get a confirmation from the botfather, which will look something like this:

Here you will get the URL to the bot and most important your API-Key (the one in the screenshot won’t work, so don’t even try to use it).

The bot itself is done now. For the bot to know where to send the notifications we need our own Telegram ID.

To get the ID, head over to the URL https://api.telegram.org/bot<BOTAPI-KEY>/getUpdates. You will first see this page:

Send a message to your bot (not the /start message, but another after that) and refresh the API page.

Now you should get something like this:

The highlighted ID is your user ID which we will need later in the nagios configuration, so it’s best to note it down right now.

Step 2 – configure nagios

Next up we need to configure nagios to send the notifications via this new telegram bot. For that, we will define two new commands, one for the host state and one for service states.

Open up your commands.cfg in the nagios\objects\etc directory and paste in the following:

# 'notify-host-by-telegram' command definition
define command{
command_name notify-host-by-telegram
command_line /usr/bin/curl -X POST --data chat_id=$CONTACTPAGER$ --data parse_mode="markdown" --data text="%60$HOSTNAME$%60 %0A%0A$NOTIFICATIONTYPE$ %0A%60$HOSTSTATE$%60 %0A%60$HOSTADDRESS$%60 %0A%60$HOSTOUTPUT$%60" https://api.telegram.org/bot<BOTAPI-KEY>/sendMessage
}

# 'notify-service-by-telegram' command definition
define command{
command_name notify-service-by-telegram
command_line /usr/bin/curl -X POST --data chat_id=$CONTACTPAGER$ --data parse_mode="markdown" --data text="%60$HOSTNAME$%60 %0A%0A$NOTIFICATIONTYPE$ %0A%60$SERVICEDESC$%60 %0A%60$HOSTADDRESS$%60 %0A%60$SERVICESTATE$%60 %0A%60$SERVICEOUTPUT$%60" https://api.telegram.org/bot<BOTAPI-KEY>/sendMessage
}

Please note that you need to provide your own bot API key in the command (printed in bold). And you can change the text which is sent to your liking of course.

Safe the file and open the next one – contacts.cfg

As you might have seen in the code, we are using the pager ID of the contact to send the notification.

define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email root@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
pager YOUR-TELEGRAM-ID-HERE
service_notification_commands notify-service-by-telegram
host_notification_commands notify-host-by-telegram
}

Here you need to paste in your Telegram ID, which we got from the getUpdates page in step 1.

Save the file and restart your nagios service. Now everything is properly set up and you should get notifications via telegram.

Step 3 – optional set up

Multiple notification ways

If you want to get notifications not only via telegram, but also via mail (which is the standard method in nagios) you could alter the service_notification_commands and host_notification_commands to the following:

service_notification_commands notify-service-by-telegram, notify-service-by-email
host_notification_commands notify-host-by-telegram, notify-host-by-email

This might also come in handy as a backup notification system when your mailserver is down.

Telegram behind a proxy

If you are behind a proxy (mostly in an enterprise environment) and your nagios server isn’t directly connected to the internet, you need to change the curl command:

command_line /usr/bin/curl -x '[user:password@]proxy-address[:port]' -X POST ...

Remember to restart your nagios service once you have done any changes on the configuration files or else it won’t use the changed configuration.

Script to check config and restart service

I wrote myself a little script to check the nagios configuration for any errors and also prompt me to restart the service if there are no errors.

#!/bin/bash
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
export RC=$?

if [ "$RC" = "0" ]; then
clear
echo "No errors found."
echo "Restart nagios service (y)es/(n)o"
read restart
if [ "$restart" = "y" ]; then
echo "Nagios service is being restarted. Please wait..."
systemctl stop nagios.service
sleep 5
killall nagios
sleep 5
systemctl start nagios.service
echo "Restarted nagios service"
else
echo "Nagios will not be restarted"
fi
fi

You might need to alter the commands to start and stop your nagios service depending on your OS. Just save it as check_nagioscfg (or any other name) in \usr\bin, make it executable and you are good to go.