How to use Monit on Ubuntu for monitoring common web services and to reduce your downtime

  Follow me: Follow Bruce Kirkpatrick by email subscription Bruce Kirkpatrick on Twitter Bruce Kirkpatrick on Facebook
Fri, Nov 29, 2013 at 3:45PM

I just setup monit for our production server, and boy was it is easy.

Why use monit if you already have other monitoring?  Well, monit can auto-restart your services, run scripts, send emails and monitor at any frequency you wish, for free!   With monit properly configured, it is likely it will fix your problem before other monitoring tools even notice there is a problem.   Monit is also able to see your system in detail, whereas many remote monitoring services are only able to run ping and http tests against your services. 

As a new user to monit, I haven't explored all it can do, but in just a couple hours I was able to get a configuration working that monitors all my core services and auto-restarts / emails me.  

For monitoring Railo, I detect if requests timeout multiple times before restarting.  Sometimes Railo can run out of memory or hit a hang condition, and if it stops responding for a few minutes, I assume it is safe to kill the service and start it again.   If it isn't actually hanging, then this will cause any data that is partially written to be lost or corrupt, so be careful!

Installing monit on Ubuntu 13:

sudo apt-get install monit

Setup a custom configuration file:

vi /etc/monit/conf.d/custom.txt

Here is the configuration I'm using - tailor it to your needs:

#set the default interval that monit checks your services
set daemon 45

#setup the mail server
set mailserver localhost

#enable the built in web server for browsing the services you're monitoring:
set httpd port 2812 and
use address localhost
allow localhost

# setup to email address to send mail to:

set alert root@localhost
set mail-format { from: use-a@real-mail-domain.com }

check process sshd with pidfile /var/run/sshd.pid
start program = "/etc/init.d/ssh start"
stop program = "/etc/init.d/ssh stop"

check process postfix with pidfile /var/spool/postfix/pid/master.pid
start program = "/etc/init.d/postfix start"
stop program = "/etc/init.d/postfix stop"

check process php5-fpm with pidfile /var/run/php5-fpm.pid
start program = "/etc/init.d/php5-fpm start"
stop program = "/etc/init.d/php5-fpm stop"

check process mysqld with pidfile /var/run/mysqld/mysqld.pid
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"

check process nginx with pidfile /opt/nginx/logs/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"

check process railo_ctl with pidfile /opt/railo/tomcat/work/tomcat.pid
start program = "/etc/init.d/railo_ctl start" with timeout 90 seconds
stop program = "/etc/init.d/railo_ctl forcequit"
if failed host 127.0.0.1 port 8888 with protocol http and request "/railo-context/admin/server.cfm" with timeout 5 seconds for 3 times within 4 cycles then restart

# kvm virtual machine or other pingable network resource - Replace x.x.x.x with the actual IP address and vmname with any label you wish to give this rule.
check host vmname with address x.x.x.x
start program = "/usr/bin/virsh start vmname"
stop program = "/usr/bin/virsh stop vmname"
if failed icmp type echo count 2 with timeout 5 seconds then restart

After you're done configuring, run:

monit reload

Now you can visit http://127.0.0.1:2812/ to view your monitoring information.  I recommend you tunnel port 2812 through an SSH connection or enable the monit SSL feature for its web service.

Doing some maintenance and need to stop monit from auto-starting your services? Just stop the service:

service monit stop

Need to prevent monit from automatically starting at boot?

update-rc.d monit disable

Need to allow monit to auto-start again?

update-rc.d monit enable

Monit is easy

That's all there is to getting started on a basic level with monit.


Bookmark & Share



Popular tags on this blog

Performance |