Home /  MMonit /  Pushover Notification

Introduction | M/Monit | Monit

In this example we use pushover.net, an online service which can send alerts to your iPhone or Android Phone. To send notification, you simply use curl, which is a command-line tool for sending HTTP requests. Curl is probably already installed on your system.

You will need to obtain an API key and setup an application token over at pushover.net and download the client app on your phone (not free, but inexpensive and sending messages are free).

M/Monit

Once you have your pushover account setup, go into M/Monit -> Admin -> Alerts and copy the following "script" into the execute program box:

 curl -s \
   -F "token=your_mmonit_or_monit_app_token" \
   -F "user=your_pushover_net_user_token" \
   --form-string "priority=0" \
   --form-string "message=[$MONIT_HOST] $MONIT_SERVICE - \
   $MONIT_DESCRIPTION" https://api.pushover.net/1/messages.json

Monit environment variables are used to compose the alert message. An overview of available environment variables can be found in the inline help text in the alerts page and also in the Monit manual. The screenshot below illustrate the setup and how a notification might look on your phone.

High Priority Alerts

Since February 2020, pushover.net has had the option to send messages with different priorities. This is useful for leveraging critical alerts on iOS and other functions described in the pushover.net API Documentation. Set the priority option with --form-string "priority=0". Change the default value from 0 for the curl examples to 1.

If you want to use different pushover.net priorities for your mmonit alerts, add the above example once for every priority value you need. Don't forget to change the if condition of the rules to avoid duplicate alerts with different priority!

Critical Pushover Alerts

Pushover.net provides alerts that have to be acknowledged (priority 2). For using these Feature change the curl command as follow:

 curl -s \
   -F "token=your_mmonit_or_monit_app_token" \
   -F "user=your_pushover_net_user_token" \
   --form-string "priority=2" \
   --form-string "retry=60"
   --from-string "expire=600"
   --form-string "message=[$MONIT_HOST] $MONIT_SERVICE - \
   $MONIT_DESCRIPTION" https://api.pushover.net/1/messages.json

With sending the retry and expire property to the pushover.net API you are telling pushover.net how often and for how long it tries to get acknowledgment from the user (above example every 60 seconds for 10 minutes).

Monit

You can do the same from Monit if you don't use M/Monit. Below is a program check in Monit which will send an alert using pushover if the check fails. But first, let's create a simple script which Monit can use to send Pushover notification:

#!/bin/sh

pushover_priority="0"

if [ $1 ]; then
  pushover_priority="$1"
fi

/usr/bin/curl -s \
  -F "token=your_mmonit_or_monit_app_token" \
  -F "user=your_pushover_net_user_token" \
  --form-string "priority=$pushover_priority" \
  --form-string "message=[$MONIT_HOST] $MONIT_SERVICE - $MONIT_DESCRIPTION" \
  https://api.pushover.net/1/messages.json

Save the script above to a file called, for instance, pushover.sh. We can now use this script in our Monit control file (.monitrc) to get pushover alerts if a check fails :

  check program check-mysql with path "/opt/monit/check_mysql.sh"
     if status != 0 then exec /path/to/pushover.sh

Note also that the content of the environment variable $MONIT_DESCRIPTION is anything your check program script (check_mysql.sh above) outputs to stdout/stderr. Which makes it possible to compose a detailed description of what went wrong.

You can use different pushover.net priorities for your monit alerts, call the script with the priority value as the first argument.

  check program check-mysql with path "/opt/monit/check_mysql.sh"
     if status != 0 then exec "/path/to/pushover.sh 1"