Extend Nagios command with a simple shell script for Slack Incoming WebHooks

  19 Aug 2014


To minimize external service integrations in Slack, we try to use WebHooks as much as possible. So, I have written a simple Nagios command and the snippet looks like this:

How to use it as nagios command? First, just download it:

curl -O https://gist.githubusercontent.com/zdk/aeb450aed2835ff52735/raw/974b1b99cd09b3cb5a5426c0067fcb96dea7abde/slack.sh

And, change these following values in slack.sh file you just downloaded to match your nagios and slack setting details:

    WEBHOST_NAGIOS="<NAGIOS INSTALLED HOST>"
    SLACK_CHANNEL="#<CHANNEL NAME>"
    WEBHOOK_URL="<Webhook URL>"

WEBHOST_NAGIOS is an IP or FQDN for the host you installed nagios web interface and other parts you can find it in Slack Integrations page for your team. SLACK_CHANNEL is the channel where the bot will post in. WEBHOOK_URL is Slack Web Hook url (grab it from Integration page at Slack Web Hook setup instruction section)

Then

cp slack.sh /usr/local/bin/slack_nagios.sh

And

sudo chmod 755 /usr/local/bin/slack_nagios.sh

In /etc/nagios3 directory, edit commands.cfg file. (This is where we add more commands for Nagios)

    ###############################
    # SLACK NOTIFICATION COMMANDS
    ###############################

    # 'notify-service-by-slack' command definition
    define command {
           command_name     notify-service-by-slack
           command_line      /usr/local/bin/slack_webhook_nagios.sh > /var/log/nagios3/slack_webhook_nagios.log 2>&1
           }

    # 'notify-host-by-slack' command definition
    define command {
           command_name     notify-host-by-slack
           command_line      /usr/local/bin/slack_webhook_nagios.sh > /var/log/nagios3/slack_webhook_nagios.log 2>&1
           }

Finally, we edit /etc/nagios3/conf.d/contacts_nagios2.cfg which you can specify who you want to send notification for and what kind of notifcation you want to send, our case are email and slack webhook. Add notify-host-by-slack and notify-service-by-slack in service_notification_command and host_notification_command respectively.

    define contact{
            contact_name                    root
            alias                           Root
            service_notification_period     24x7
            host_notification_period        24x7
            service_notification_options    w,u,c,r
            host_notification_options       d,r
            service_notification_commands   notify-service-by-email,notify-service-by-slack
            host_notification_commands      notify-host-by-email,notify-host-by-slack
            email                           root@localhost
    }

Restart Nagios as usual.

service nagios3 restart

And wait for some alerts! (if your system is too stable, just try to stop ssh service)

comments powered by Disqus