monit – monitor your services

After I got my home server up running, I was looking for a easy way to monitor it services. Earlier I’ve used MRTG, Smokeping, Cacti and similar, but this time I was looking for something really easy and lightweight. The graphs in MRTG and similar is useful to have, but my main focus was to monitor the health of a service and alert if it’s not.

When I found Monit, I understood that monitor software with web front end, graphs and user management(Zabbix, Zenoss core, with more) was an total overkill for my task. Monit is easy to use, repairs the faulty service and have a nice web front end to see the current status.


Since Monit comes with Ubuntu, it’s easy:

apt-get install monit

Edit monit to start. Change startup=0 to startup=1

nano /etc/default/monit

Edit /etc/monit/monitrc. The file is self explainable and there is good documentation on My stripped file looks like this:

set daemon  120           # check services at 2-minute intervals
with start delay 60  # optional: delay the first check by 1 minute
set logfile syslog facility log_daemon
set mailserver localhost,               # primary mailserver
set mail-format { from: } # some mailservers bounce domains not found in DNS
set alert
set httpd port 2812 and
use address localhost  # only accept connection from localhost
allow localhost        # allow localhost to connect to the server and
allow monit:passwd   # allow user monit with password passwd
include /etc/monit/conf.d/* # include conf files

I then created files in /etc/monit/conf.d. Here is my localhost file.

check system localhost
if loadavg(5min) > 4 then alert
if loadavg(15min) > 2 then alert
# filesystems
check filesystem root with path /
if space usage > 80% then alert
check filesystem home with path /home
if space usage > 95% then alert
# cron
check process cron with pidfile /var/run/
start program = "/etc/init.d/cron start"
stop  program = "/etc/init.d/cron stop"
if 5 restarts within 5 cycles then timeout
# sshd
check process sshd with pidfile /var/run/
start program = "/etc/init.d/ssh start"
stop program = "/etc/init.d/ssh stop"
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
#  samba
check process smbd with pidfile /var/run/samba/
start program = "/etc/init.d/smbd start"
stop  program = "/etc/init.d/smbd stop"
if failed host port 139 type TCP  then restart
if 5 restarts within 5 cycles then timeout
#  nfs
check host server.lan with address
start = "/etc/init.d/nfs-kernel-server start"
stop = "/etc/init.d/nfs-kernel-server stop"
if failed port 2049 then restart

Services are automagically restarted and you are alerted by email if pid file does not exist, or there is no running service with that pid. If statements holds extra tests for services, and services without pid file can be watched by checking the service over network.

Also services on the network can be watched. Here is another server I’m watching:

check host with address
if failed port 143 proto imap with timeout 2 seconds then alert
if failed port 465 type tcpssl proto smtp with timeout 2 seconds then alert
if failed url with timeout 2 seconds then alert

Restart monit.

service monit restart

Look for errors.

tail /var/log/syslog

If you get error message /etc/monit/conf.d/host:12: Error: syntax error ‘=’, change all instances of ” with ".

Thats it!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>