How-to – use Telegram with Nagios

0
2971
# '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 text="***** Nagios *****%0A%0ANotification Type: $NOTIFICATIONTYPE$%0AHost: $HOSTNAME$%0AState: $HOSTSTATE$%0AAddress: $HOSTADDRESS$%0AInfo: $HOSTOUTPUT$%0A%0ADate/Time:
$LONGDATETIME$%0A" https://api.telegram.org/bot$BOT-TOKEN$/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 text="***** Nagios *****%0A%0ANotification Type: $NOTIFICATIONTYPE$%0A%0AService: $SERVICEDESC$%0AHost: $HOSTALIAS$%0AAddress: $HOSTADDRESS$%0AState: $SERVICESTATE$%0A%0
ADate/Time: $LONGDATETIME$%0A%0AAdditional Info:%0A%0A$SERVICEOUTPUT$%0A" https://api.telegram.org/bot$BOT-TOKEN$/sendMessage
}
  • Modify the /usr/local/nagios/etc/contacts.cfg and add the followings lines:
define contact {
contact_name telegram-contact
alias Andreas Neumann
service_notification_options w,u,c,r
service_notification_period 24x7
host_notification_period 24x7
service_notification_commands notify-service-by-telegram
host_notification_commands notify-host-by-telegram
host_notification_options d,r
pager $CHAT-ID$
}
  • Create a new chat group on your Telegram and add the Bot to this group.
  • Bring the Chat-ID code using this command:
    curl -X POST https://api.telegram.org/botYOUR-BOT-TOKEN-ID/getUpdates
curl -X POST https://api.telegram.org/bot110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw/getUpdates

This is an example of the result, in this case the Group name create is “GroupName” and the id is -108820942:

{"ok":true,"result":[{"update_id":638422092,
"message":{"message_id":9,"from":{"id":268963852,"first_name":"User"},"chat":{"id":268963852,"first_name":"TestUser"},"date":1437389925,"text":"c"}},{"update_id":638422093, 
"message":{"message_id":2,"from":{"id":74288568,"first_name":"Name","last_name":"LastName","username":"USer"},"chat":{"id":-108820942,"title":"GroupName","type":"group"},"date":1457371074,"new_chat_participant":{"id":192659968,"first_name":"SopraCroom","username":"SopraCroomBot"}}},{"update_id":836565897,
  • Modify the /usr/local/nagios/etc/resources.cfg and add the lines to declare the $CHAT-ID$ and $BOT-TOKEN$ variables:
$CHAT-ID$ -108820942
$BOT-TOKEN$ 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
  • Restart Nagios service