Mobile Nagios notifications with Pushover

All my services are being monitored from my Nagios instance.

Some of my services are important so I wanted to receive notifications on my mobile whenever something goes wrong.

Of course sending an SMS is possible, but I think it something from the past. So I started searching for a replacement which doesn't cost a lot (one time 4,29 Euro).

After some testing with several services I ended up with the Pushover service (http://pushover.net)

 

Now this needs to part of my Nagios instance.

These steps are just to setup the account, so they are not specific and not part of this manual...

  1. Create an account on the Pushover.net website.
  2. Install the app on your mobile device.
  3. Add your mobile device to the account.
  4. Create an application so you can create an API token/key.

 

This API token/key is used to send notifications/Pushover messages to your account on pushover.net

Create in your Nagios libexec folder, the following file: notify_by_pushover.sh

#!/bin/bash

# Notify by Pushover
# by Jedda Wignall
# http://jedda.me

# v1.2.1 - 17 Mar 2013
# Now parses title and message for sound processing.

# v1.2 - 18 Dec 2012
# Added parsing of title for specific warning, critical, and OK sounds.

# v1.1 - 02 Dec 2012
# Added notification sounds.

# v1.0 - 21 Aug 2012
# Initial release.

# This script sends a Pushover (http://pushover.net/) notification to your account. I use it in a Nagios setup
# to send network monitoring notifications, but it could be used or adapted for nearly any scenario.

# IMPORTANT
# You will need to create a Pushover 'Application' for this script in your account, and use the provided API key
# as an argument. You can register an app once logged into Pushover at the follwing link:
# https://pushover.net/apps/build

# Takes the following REQUIRED arguments:

# -u Your Pushover user key.
# -a Your Pushover application key.
# -t The notification title.
# -m The notification body.

# and the following OPTIONAL arguments:

# -p Notification priority. Set to 1 to ignore quiet times.
# -s Notification sound. You must use one of the parameters listed at https://pushover.net/api#sounds.
# -w Warning notification sound. The script will look for the text 'WARNING' in the notification title, and use this sound if found.
# -c Critical notification sound. The script will look for the text 'CRITICAL' in the notification title, and use this sound if found.
# -o OK notification sound. The script will look for the text 'OK' in the notification title, and use this sound if found.

# Example:
# ./notify_by_pushover.sh -u r5j3mjYjd -a noZ9HuR5T -s 'spacealarm' -t "server.example.com" -m "DISK WARNING - free space: /dev/disk0s2 4784 MB"


while getopts "u:a:t:m:p:s:w:c:o:" optionName; do
case "$optionName" in
u) userKey=( "$OPTARG" );;
a) appToken=( "$OPTARG" );;
t) title=( "$OPTARG" );;
m) message=( "$OPTARG" );;
p) priority=( "$OPTARG" );;
s) sound=( "$OPTARG" );;
w) warnSound=( "$OPTARG" );;
c) critSound=( "$OPTARG" );;
o) okSound=( "$OPTARG" );;

esac
done

if [ "$priority" != "" ]; then
priorityString="priority=$priority"
else
priorityString="priority=0"
fi

if echo $title $message | grep -q 'WARNING' && [ "$warnSound" != "" ] ;then
sound=$warnSound
elif echo $title $message | grep -q 'CRITICAL' && [ "$critSound" != "" ] ;then
sound=$critSound
elif echo $title $message | grep -q 'OK' && [ "$okSound" != "" ] ;then
sound=$okSound
fi

curl -F "token=$appToken" \
-F "user=$userKey" \
-F "title=$title" \
-F "message=$message" \
-F "sound=$sound" \
-F "$priorityString" \
https://api.pushover.net/1/messages

exit 0

All the credits for this script are to the creator at http://jedda.me

Give the file execute rights:

chmod +x notify_by_pushover.sh

Add the notification handler to Nagios:

Add these lines to the commands file:

# 'notify-host-pushover' command definition
define command {
  command_name    notify-host-pushover
   command_line   $USER1$/notify_by_pushover.sh -u $CONTACTADDRESS1$ -a $CONTACTADDRESS2$ -c 'persistent' -w 'siren' -t "Nagios" -m "$NOTIFICATIONTYPE$ Host $HOSTNAME$ $HOSTSTATE$"
}

# 'notify-service-pushover' command definition
define command {
  command_name   notify-service-pushover
  command_line   $USER1$/notify_by_pushover.sh -u $CONTACTADDRESS1$ -a $CONTACTADDRESS2$ -c 'persistent' -w 'siren' -t "Nagios" -m "$HOSTNAME$ $SERVICEDESC$ : $SERVICESTATE$ Additional info: $SERVICEOUTPUT$"
}

Add a new contact template to the template file:

define contact {
        name generic-pushover
        host_notifications_enabled      1
        service_notifications_enabled   1
        host_notification_period        24x7
        service_notification_period     24x7
        host_notification_options       d,r
        service_notification_options    c,r
        host_notification_commands      notify-host-pushover
        service_notification_commands   notify-service-pushover
        can_submit_commands             1
        retain_status_information       1
        retain_nonstatus_information    1
        register
}

Of course change whatever you need in these settings.

 

Add a new contact in your contacts file:

define contact{
        use                 generic-pushover
        contact_name        admin_pushover
        alias               Admin Pushover
        address1            <user key>
        address2            <api key>
}

For the line "address1" use the "User Key" from the Pushover website.

For the line "address2" use the "API Key" from newly created Nagios Application at the Pushover website.

 

Add the Pushover contact to the host or service check, for example this is one of my Dell Hardware checks in VMWare:

# Check DELL Server hardware
define service {
#       use                     generic-service
        host_name                       vmware02.colo
        active_checks_enabled           1
        passive_checks_enabled          0
        check_freshness                 0
        notifications_enabled           1
        event_handler_enabled           1
        flap_detection_enabled          0
        retain_status_information       1
        retain_nonstatus_information    1
        check_period                    24x7
        service_description             HARDWARE
        check_command                   check_esxi_hardware!nagios!password
        normal_check_interval           15
        retry_check_interval            5
        max_check_attempts              2
        contacts                        admin_pushover, nagiosadmin
        notification_period             24x7
        notification_options            w,c
        notification_interval           60
}

This check sends 24x7 the notifications to the default contact "nagiosadmin" (email) and my "admin_pushover" contact.

Now do an Nagios syntax check to check if no typo's are made:

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Restart Nagios:

sudo service nagios restart

 

Change the variables in my examples with your own, and good luck :-)