In a previous post, Valentina showed us how to configure Slack Notifications… and here I will similarly show you how to configure Telegram notifications on NetEye.
A very easy way to create a bot on Telegram is through … another bot 😉
BotFather is a Telegram bot dedicated to the creation of new bots. It’s helpful for the first steps and for general settings, such as the definition of the name, the username, the description and the commands to be set.
By typing to Telegram’s @BotFather you can start the activation procedure of our bot with the /start option:
Next we can type /newbot and specify the name of the bot we want to create:
Create the username and take note of the token provided by BotFather for accessing the API via HTTP.
Token example format: 10XXXXXX:AAXXXXXX-y2TDkdgy8Fr6XXXXXX
Now create a new group and add the previously created Telegram Bot along with the recipients for NetEye notifications:
After creating the group, write at least one message in the group channel (which must contain at least us and the bot created initially)
To use the notification commands we need the chat_id, which is the unique group/channel id of the group where the message has been sent.
We can use the curl command in this way calling api.telegram.org:
curl https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/getUpdates
Here’s an example:
[root@neteye4 ~]# curl -i -X GET https://api.telegram.org/bot10XXXXXX:AAXXXXXX-y2TDkdgy8Fr6XXXXXX/getUpdates HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Mon, 23 Mar 2020 14:47:45 GMT Content-Type: application/json Content-Length: 380 Connection: keep-alive Strict-Transport-Security: max-age=31536000; includeSubDomains; preload Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, OPTIONS Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection {"ok":true,"result":[{"update_id":414892163, "message":{"message_id":6,"from":{"id":8933484,"is_bot":false,"first_name":"my_first_name","last_name":"my_last_name","username":"my_username"},"chat":{"id":-488860198,"title":"NetEye Telegram","type":"group","all_members_are_administrators":true},"date":1584974858,"text":"/setprivacy","entities":[{"offset":0,"length":11,"type":"bot_command"}]}}]}
In the example above, the chat-id is -488860198.
Pay attention, the chat-id must start with a “–“!
To create the notification commands on NetEye you need to clone this git repository in NetEye plugin repository:
# cd /neteye/shared/monitoring/plugins/ # git clone https://github.com/sysadmama/icinga2-notification-telegram
We can now send a test notification. In the example below you will find a full example, but remember to substitute the telegram_bot (-p), telegram_chat_id (-q) and telegram_bot_token (-r) options.
[root@neteye4 ~]# su -s /bin/bash icinga -c "'/neteye/shared/monitoring/plugins/host-by-telegram.sh' '-4' 'neteye.mydomain.com' '-6' '' '-b' 'root' '-c' 'TELEGRAM NETEYE NOTIFICATION' '-d' '2020-03-23 12:11:01 +0100' -i 'http://neteye.mydomain.com' '-l' 'neteye.mydomain.com' '-n' 'neteye.mydomain.com' '-o' 'PING CRITICAL - Packet loss = 100%' '-p' 'neteye_telegram_bot' '-q' '-488860198' '-r' ' 10XXXXXX:AAXXXXXX-y2TDkdgy8Fr6XXXXXX ' -s 'DOWN'"
And then import the telegram-host-notification and telegram-service-notification commands into Icinga Director, importing the basket you will find in our NetEye 4 repository.
For each command created, update the three custom properties related to telegram_bot, telegram_bot_token and telegram_chat_id:
You can now customize the notification definition to better satisfy your needs using, for example, a custom variable to filter the hosts for which you want to have notifications on Telegram.
Update related to steps for telegram configuration.
You need to update and allow privacy settings to BOT member:
Get your bots:
/mybots
Assign your bot:
/setjoingroups
@mybotname
Set privacy permissions:
Enable
/setprivacy
@mybotyname