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

  1. Create a bot in Telegram with @BotFather (/newbot) and copy the bot token.

  2. Find the chat identifier of the recipient: start a conversation with the bot (or add the bot to a group) and read the chat.id value from https://api.telegram.org/bot<TOKEN>/getUpdates.

  3. Put the token in /opt/energy-monitor/notify/notify-telegram.conf:

    token = 123456789:AAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    

    The file accepts these keys:

    Key

    Meaning

    token

    Bot token. A --token argument on the command line overrides it

    contact

    Default chat identifier, used when the command passes no --contact

    proxy_url

    http://user:password@host:port or socks5://host:port when the server reaches the Internet through a proxy

    monitoring_system_name, monitoring_url

    Name and address of the installation, shown in the message

    message_length

    long, medium or short message template

  4. Put 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.

  5. 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$"
    }
    
  6. 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.

  7. Test with Send custom host notification on a host and check the Telegram chat.

Script arguments

Argument

Meaning

--config

Path of the configuration file, when it is not next to the script

-t, --token

Bot token, overrides the file

--contact

Chat identifier of the recipient

-o, --object_type

host or service

--notificationtype

PROBLEM, RECOVERY, ACKNOWLEDGEMENT, CUSTOM and the other Naemon notification types

--hostname, --hostaddress

Host name and address

--hoststate, --servicestate

State of the host or service

--servicedesc

Service description (service notifications)

--output

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.