In this post you'll learn about setting up a Monit monitoring service for your websites and services. Monit is a free and open source service monitoring application which can perform various event-based actions. Monit can send email notifications, restart a service or application, or take other responsive actions. We set Monit up on a Ubuntu 14.04 VM, built on Hyper-V. And we use Monit to monitor several websites, and send out notifications on downtime.
Monit monitoring
Monit monitoring for websites and services, on an Ubuntu 14.04 LTS VM, hosted on a Windows Server 2012 R2 or Windows 8.1 Hyper-V.
Monit is a small Open Source utility for managing and monitoring Unix systems. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.
I recently read about Monit in a DigitalOcean tutorial, and it looked interesting. Interesting enough to start playing around with it. Lacking the extra physical computer, I decided to use a virtual Linux installation in Hyper-V. Even though it's on Windows 8.1, the same would apply for Windows Server 2012 R2.
Steps
Install Ubuntu Server 14.04.1 LTS VM on Hyper-V
Install Monit and additional services (like Postfix to send out notifications)
Configure Monit (website-, DNS- and SMTP-monitoring at the moment)
This achieves a couple things.
- Monit is isolated within a VM. Nowadays, anybody can run virtual machines in Hyper-V on their Windows 8.1 or Windows 10 workstation. However, since not everyone has an extra computer to run an additional Linux distribution, we use an Ubuntu VM in Hyper-V. The same applies to Windows Server 2012 R2 Hyper-V of course. (update: now we have Windows Subsystem for Linux, or WSL to host Monit!)
Configure email delivery, relay Postfix mail through Google Gmail SMTP
First, we have to make sure Monit can send out email notifications. Since we use Monit to monitor websites, we don't want to use our hosting provider's SMTP server. That may be down too in case of an event.
We install Postfix and configure Postfix to relay mail through Google Gmail SMTP servers. A Gmail-account is required.
Install all necessary packages for Postfix:
sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
If Postfix Configuration asks for a general type of mail configuration, choose Internet Site. Don't forget to set a valid FQDN hostname.
Now open your postfix config file:
vi /etc/postfix/main.cf
and add following lines to it:
# SASL SUPPORT FOR SERVERS## The following options set parameters needed by Postfix to enable# Cyrus-SASL support for authentication of mail servers.#relayhost = [smtp.gmail.com]:587smtp_sasl_auth_enable = yessmtp_sasl_password_maps = hash:/etc/postfix/sasl_passwdsmtp_sasl_security_options = noanonymoussmtp_tls_CAfile = /etc/postfix/cacert.pemsmtp_use_tls = yes
We specifiy our Gmail username and password in the file /etc/postfix/sasl_passwd
. Create that file using VI:
vi /etc/postfix/sasl_passwd
And add following line:
[smtp.gmail.com]:587 USERNAME@gmail.com:PASSWORD
Fix permission and update Postfix config to use sasl_passwd
file:
sudo chmod 400 /etc/postfix/sasl_passwdsudo postmap /etc/postfix/sasl_passwd
Next, validate certificates to avoid running into error. Just run following command:
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
Finally, reload postfix config for changes to take effect:
sudo /etc/init.d/postfix reload
If all went well, Postfix is now set up to send mail through Google Gmail SMTP servers. This guarantees email delivery when our hosting provider's SMTP server is unreachable or down. These steps came from a great tutorial on rtcamp.com.
Installing and configuring Monit
Installing Monit on our Ubuntu 14.04 VM is nothing more than:
sudo apt-get install monit
On Ubuntu 14.04, the Monit configuration files are located in /etc/monit/
and the main Monit configuration file is /etc/monit/monitrc
. Open up the Monit configuration file:
vi /etc/monit/monitrc
Locate and uncomment the following lines and change them to match your situation:
set mailserver localhost #Use localhost for email alert delivery.set mail-format { from: monit@$HOST subject: monit alert -- $EVENT $SERVICE message: $EVENT Service $SERVICE Date: $DATE Action: $ACTION Host: $HOST Description: $DESCRIPTION Your faithful employee, Monit}set alert your_gmail@address.com not on { instance, action }
Let's restart Monit: service restart monit
Configure Monit to monitor a remote website
The whole point of this tutorial is to set up remote monitoring of websites and services. In the folder /etc/monit/conf.d
we can add a configuration file:
sudo vi /etc/monit/conf.d/saotn-org-external
Monitor ICMP response and HTTP connectivity
In the configuration file /etc/monit/conf.d/saotn-org-external
we add checks for ICMP responses (ping), and HTTP connectivity:
check host www.saotn.org with address www.saotn.org if failed icmp type echo for 5 times within 5 cycles then alert# HTTP check if failed port 80 protocol http for 5 times within 5 cycles then alert
Configure Monit's command line interface
If we want to check the status of our monitored websites and services, we need to set up the Monit command line interface. Once again, open up /etc/monit/monitrc
, and uncomment the following lines to enable the web service locally:
set httpd port 2812 and
use address localhost
allow localhost
Restart Monit to make the configuration current. Now it is possible to check Monit status from the Bash command line. Use sudo monit status
to view the current monitoring status:
The Monit daemon 5.6 uptime: 1m
Remote Host 'www.saotn.org'
status Online with all services
monitoring status Monitored
icmp response time 0.014s [Echo Request]
port response time 0.013s to www.saotn.org:80 [DEFAULT via TCP]
data collected Sun, 15 Feb 2015 14:58:13
System 'ubuntu-01'
status Running
monitoring status Monitored
load average [0.24] [0.07] [0.06]
cpu 0.0%us 0.0%sy 0.0%wa
memory usage 117772 kB [5.7%]
swap usage 0 kb [0.0%]
data collected Sun, 15 Feb 2015 14:58:13
If needed, you can disable and re-enable Monit monitoring with the commands sudo monit unmonitor all
and sudo monit monitor all
Monit monitoring for DNS or SMTP
Monit can be easily used to monitor other remote services as well. Services like DNS or SMTP. To monitor SMTP mail servers, add an smtp-external
(or what ever) file to your /etc/monit/conf.d
file, and configure the host, address, and alert:
check host smtp-01 with address smtp-01
if failed port 587 type tcp protocol smtp then alert
check host smtp-02 with address smtp-02
if failed port 587 type tcp protocol smtp then alert
check host smtp-03 with address smtp-03
if failed port 587 type tcp protocol smtp then alert
Do the same in your nameservers-external
file:
check host dns-01 with address dns-01
if failed port 53 type udp protocol dns then alert
check host dns-02 with address dns-02
if failed port 53 type udp protocol dns then alert
check host dns-03 with address dns-03
if failed port 53 type udp protocol dns then alert
Monit reporting to M/Monit
M/Monit is the reporting service of multiple Monit instances or agents. M/Monit collects the data sent by Monit and displays it in a graphical web interface very neat. Once you have a Monit instance running, setting up M/Monit is as easy as one-two-three. All you literally have to do is following the Setup-guide.
Utilizing the powers of Monit and M/Monit, you can monitor local services/daemons, and restart them when an event happens. You can even monitor remote TCP-services, such as SMTP, DNS or HTTP/HTTPS.
If you want to use Monit in Windows Subsystem for Linux (WSL), then I recommend you read my post Setting up Monit monitoring in Windows Subsystem for Linux WSL.