Monitoring With Monit

On my quest to explore simple server monitoring solutions, I stumbled upon Monit.

I’ve used Monit to monitor various micro services.

Monit is a helpful program that automatically monitors and manages server programs to ensure that they not only stay online consistently, but that the file size, checksum, or permissions are always correct. Additionally monit comes with a basic web interface through which all of the processes can be set up. This tutorial will cover the most basic setup and configuration.

Digital Ocean

 

Background

I’ve been looking simple monitoring solution for a couple linux ubuntu servers I work with and came across Monit. I looked into Nagios, Supervisord and Circus to managing a few of my python applications but I settled for Monit. There are obvious pros and cons for using each one but for me the ease of use from installing and setting up the monitoring services was easier.

I had tried SupervisorD previously but the installation and configuration of it was tedious. I looked in Nagios but it seemed a bit over the top for what I needed. I didn’t go to in-depth with Circus but it was on my list to try next if Monit did not work out. I also noticed that the last commit to Circus was done in 2016 and Monit has an active enterprise support behind it.

Installing Monit

Installing Monit on Ubuntu was very easy using sudo apt-get install monit . I followed article from DigitalOcean to setup my Monit instance.

One great thing that comes with Monit is it’s simple web interface to easily view the list of items (processes, services, applications etc.) you are monitoring. This needs to be manually configured it is quite easy to do:

Go to and open the config file: sudo nano /etc/monit/monitrc. In the file, scroll down to the bottom of the file until you see the httpd port 2812 . A couple things to make sure of: whatever your server ip is that needs to be entered in the use address argument. I initially used the default localhost and 127.0.0.1 which did not work and the allow argument let anyone to login (you can restrict this if you want).

Access URL:   http://12.34.56.789:2812

set httpd port 2812
 use address 12.34.56.789 # only accept connection from localhost
 allow 0.0.0.0/0.0.0.0    # allow localhost to connect to the server and
 allow admin:monit        # require user 'admin' with password 'monit'

Monit comes with the start and stop arguments but each argument has a timeout clause in it which times out after a few seconds (60 seconds is default I think). This is great for daemons and other scripts that need a controlled start and stop however my backup script starts and waits till the defined times to execute.

Monitoring An Application

My python script would occasionally crash due to exceed memory limits, server starts etc. and had no way of restarting automatically. I wanted Monit to restart the script if that happened and to constantly check if it is running (by checking the PID). Monit checks the service every 2 minutes by default. The following is what to do just that. I added it to the very bottom of the Monit config file:

check process my_python_script
  matching "my_python_script.py"
  if does not exist then exec "/usr/bin/python /path/to/the/script.py &"

This block of code tells Monit to find the matching PID name and if it does not exist to start restart the python backup script ( exec /path/ . . . ).

There are many services, scripts or processes you could add to the config file and monitor them. Monit also has an alert which sends out emails depending on the conditions set.


Resources Used: