Integrating Telegram with Energy Monitor
Energy Monitor sends host and service notifications to a Telegram user or group through a
Telegram bot. The script /opt/energy-monitor/notify/notify-telegram.py and its
configuration file notify-telegram.conf are installed with the package in
/opt/energy-monitor/notify; the commands that call the script are shipped in
/opt/naemon/etc/naemon/conf.d/commands.cfg.rpmnew and have to be added to the
configuration once.
Setting up
Create a bot in Telegram with @BotFather (
/newbot) and copy the bot token.Find the chat identifier of the recipient: start a conversation with the bot (or add the bot to a group) and read the
chat.idvalue fromhttps://api.telegram.org/bot<TOKEN>/getUpdates.Put the token in
/opt/energy-monitor/notify/notify-telegram.conf:token = 123456789:AAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The file accepts these keys:
Key
Meaning
tokenBot token. A
--tokenargument on the command line overrides itcontactDefault chat identifier, used when the command passes no
--contactproxy_urlhttp://user:password@host:portorsocks5://host:portwhen the server reaches the Internet through a proxymonitoring_system_name,monitoring_urlName and address of the installation, shown in the message
message_lengthlong,mediumorshortmessage templatePut the chat identifier in the Pager field of the contact (Manage -> Configuration -> Contacts). The shipped commands pass it with the
$CONTACTPAGER$macro, so one contact per recipient is enough.Add two commands, under Manage -> Configuration -> Commands or in a configuration file:
define command { command_name notify-host-by-telegram command_line /opt/energy-monitor/notify/notify-telegram.py --object_type host --contact "$CONTACTPAGER$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostname "$HOSTNAME$" --hostaddress "$HOSTADDRESS$" --output "$HOSTOUTPUT$" } define command { command_name notify-service-by-telegram command_line /opt/energy-monitor/notify/notify-telegram.py --object_type service --contact "$CONTACTPAGER$" --notificationtype "$NOTIFICATIONTYPE$" --servicestate "$SERVICESTATE$" --hostname "$HOSTNAME$" --servicedesc "$SERVICEDESC$" --output "$SERVICEOUTPUT$" }
Assign the commands to the contact in the fields Host notification commands and Service notification commands, add the contact to the contact groups of the monitored objects and save the configuration.
Test with Send custom host notification on a host and check the Telegram chat.
Script arguments
Argument |
Meaning |
|---|---|
|
Path of the configuration file, when it is not next to the script |
|
Bot token, overrides the file |
|
Chat identifier of the recipient |
|
|
|
PROBLEM, RECOVERY, ACKNOWLEDGEMENT, CUSTOM and the other Naemon notification types |
|
Host name and address |
|
State of the host or service |
|
Service description (service notifications) |
|
Check output |
The script runs as the naemon user and calls api.telegram.org over HTTPS; without direct
Internet access set proxy_url in the configuration file.