Randomly Expressed

About

Welcome to my blog “randomly expressed”. I created this website to publish helpful tips. It’s mainly technology driven, but I will blog about other topics. I am a Unix sysadmin that is always looking to learn new things. My goal is to be able to share knowledge that others may find useful. xkcd.com

Continue Reading »

Contact

Connect With US

Connect with us on the following social networking sites.

Most Popular Posts.

Add Some Content to This Area

You should either deactivate this panel on the Theme Settings page, or add some content via the Widgets page in your WordPress dashboard.

Nagios Slack messages

By on February 10, 2017 in Technology with No Comments


Configure your Nagios server to post alerts in your Slack channel. Having Nagios alerts come in via Slack is faster than getting Nagios alerts via email.
If you want your Slack messages formatted so they look better in your channel replace the SLACK_MESSAGE variable in the script with the line below.
SLACK_MESSAGE="\\\$1\\\"
Place the following bash script in the following directory /usr/local/nagios/libexec/slack-msg on your Nagios server.

#!/bin/bash

function post_to_slack () {
  # format message as a code block
  SLACK_MESSAGE="$1"
  SLACK_URL=https://hooks.slack.com/services/***************

  case "$2" in
    OK)
      SLACK_ICON=':white_check_mark:'
      ;;
    WARNING)
      SLACK_ICON=':warning:'
      ;;
    CRITICAL)
      SLACK_ICON=':bangbang:'
      ;;
    *)
      SLACK_ICON=':x:'
      ;;
  esac


    curl -s -X POST --data "payload={\"text\": \"${SLACK_ICON} ${SLACK_MESSAGE}\"}" ${SLACK_URL}
}

post_to_slack "$1" "$2"

exit 0

Next place the following command in your Nagios command directory. I have it setup here /usr/local/nagios/etc/objects/commands/check-notify-slack-cmd.cfg

define command{
	command_name 	slack-msg
	command_line 	$USER1$/slack-msg  "'$HOSTNAME$' '$SERVICEDESC$' '$SERVICESTATE$' '$SERVICEOUTPUT$'" '$SERVICESTATE$'
}

Finally add an event hanlder line to your service file that will use the slack-msg command.
event_handler slack-msg
The following is an example of a nrpe disk check service file for Nagios where I have added an event handler for slack. The path is /usr/local/nagios/etc/objects/services/check-nrpe-disk-svc.cfg:

define service {
        use                             generic-service
        hostgroup                       prod-servers
        register                        1
        service_description             DISK::SPACE
        is_volatile                     0
        check_period                    24x7
        max_check_attempts              3
        normal_check_interval           11
        retry_check_interval            11
        contact_groups                  systems,NOC
        notification_interval           31
        notification_period             24x7
        notification_options            c,u,r
        notes_url                       http://confluence.abc.com:8090/display/NOC/Server+Alerts
        event_handler                   slack-msg
        check_command                   check_nrpe!check_disk
}

Facebook Comments

Tagged With: ,

Post a Comment

Your email address will not be published. Required fields are marked *

Top