How To Make monit Send SMS Alerts When Your Server Goes Down
Version 1.0
Author: Falko Timme
This tutorial explains how you can configure monit to send alert messages per SMS to your mobile phone when a service fails. Because monit can send only emails but not SMS, we will use an email-to-sms gateway where monit will send its emails to, and the email-to-sms gateway will convert the emails to SMS messages.
This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!
1 Preliminary Note
I'm assuming that you have already set up monit, e.g. as shown here: https://www.howtoforge.com/server_monitoring_with_munin_monit_debian_etch or here: https://www.howtoforge.com/server_monitoring_with_munin_monit_fedora_7.
In this tutorial I want to monitor a web server (with the web site www.mywebsite.com) to see if it's still running or not. I therefore use monit on a second server (monit.example.com) so that monit can send alerts if the web server goes down for whatever reason (imagine you installed monit on the web server - if the server goes down, monit cannot send out any alerts...).
The SMS service I'm using here is SMS77.de (if you want to use that service, you must register first and pay at least 5 EUR to your prepaid account). As explained on http://www.sms77.de/email2sms.html (in German), the emails that you send to the gateway need the following format:
Subject: Text of the SMS
Text of the Mail: Email2SMS-Key#Recipient#SMS Type#Sender
You can specify the email2sms key in your SMS77.de account settings; the recipient is the phone number of your mobile phone (if you want to send the message to multiple recipients, separate the phone numbers by commas); as SMS type we use basicplus which costs only 3,5 cents (EUR cents) per SMS. The sender is optional so I leave it out.
I will tell monit to format its alert messages so that they match the required format.
You can, of course, use any other email-to-sms gateway. This is just an example of how to do it, and I have no relationship to SMS77.de.
2 Configuring monit
Open /etc/monit/monitrc on monit.example.com and add the following stanza to it (at the end of the file):
vi /etc/monit/monitrc
[...] check host www.mywebsite.com with address www.mywebsite.com if failed port 80 protocol http and request "/index.php" for 4 cycles then alert if failed icmp type echo count 5 with timeout 15 seconds for 4 cycles then alert alert [email protected] { connection, timeout, icmp } with mail-format { from: [email protected] subject: $SERVICE $EVENT at $DATE on $HOST message: 123456#0170123456789#basicplus } |
As you see, with this configuration monit will request the file index.php from www.mywebsite.com, and in addition to that it will try to ping the host www.mywebsite.com. To make sure that monit doesn't send out too many false positives, it will send an alert only if the tests have failed for four consecutive times (for 4 cycles) - this should indicate that there's really a problem.
Alerts will be sent to the email-to-sms gateway email address, but only if there was a connection error, a timeout, or lost pings (a list of all available events can be found here: http://www.tildeslash.com/monit/doc/manual.php#alert_message_layout), and the mail-format section specifies the format of the alert email. The variables that you can use in the email are also listed on http://www.tildeslash.com/monit/doc/manual.php#alert_message_layout.
Please adjust this to your email-to-sms gateway!
Restart monit afterwards:
/etc/init.d/monit restart
That's it!