Integrating Slack with Energy Monitor

Energy Monitor sends host and service notifications to a Slack channel through an incoming webhook. The script /opt/energy-monitor/notify/notify-slack.py is installed with the package; the commands that call it are shipped in /opt/naemon/etc/naemon/conf.d/commands.cfg.rpmnew and have to be added to the configuration once.

Setting up

  1. In Slack create an incoming webhook for the target channel (Slack app settings, Incoming Webhooks) and copy the webhook URL.

  2. In /opt/naemon/etc/naemon/resource.cfg store the URL in the $USER24$ macro, the one used by the shipped commands:

    $USER24$=https://hooks.slack.com/services/XXXX/YYYY/ZZZZ
    
  3. Add two commands, under Manage -> Configuration -> Commands or in a configuration file:

    define command {
        command_name    notify-host-by-slack
        command_line    /opt/energy-monitor/notify/notify-slack.py --object_type host --webhook_url "$USER24$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostname "$HOSTNAME$" --hostaddress "$HOSTADDRESS$" --hostoutput "$HOSTOUTPUT$"
    }
    
    define command {
        command_name    notify-service-by-slack
        command_line    /opt/energy-monitor/notify/notify-slack.py --object_type service --webhook_url "$USER24$" --notificationtype "$NOTIFICATIONTYPE$" --servicestate "$SERVICESTATE$" --hostname "$HOSTNAME$" --servicedesc "$SERVICEDESC$" --serviceoutput "$SERVICEOUTPUT$"
    }
    
  4. Assign the commands to a contact: Manage -> Configuration -> Contacts, fields Host notification commands and Service notification commands. Add the contact to the contact groups of the hosts and services that should report to Slack.

  5. Save the configuration. Test with a custom notification on a host (Send custom host notification in the host commands) and check the channel.

What the message contains

The script builds one Slack message per notification: a title with the notification type, the host (and service) and the state, plus the check output. The colour of the message follows the state: green for UP and OK, yellow for WARNING, red for DOWN and CRITICAL, grey for UNKNOWN and every other state.

Script arguments

Argument

Meaning

--webhook_url

Slack incoming webhook URL

--object_type

host or service

--notificationtype

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

--hostname, --hostaddress

Host name and address

--hoststate, --hostoutput

Host state and check output (host notifications)

--servicestate, --servicedesc, --serviceoutput

Service state, description and check output (service notifications)

The script runs as the naemon user, so the server needs outbound HTTPS access to hooks.slack.com, directly or through the proxy configured for that user.