How to Install Monit on Ubuntu 11.10 Server to Monitor Apache Web Server, MySQL, SSH
Monit is a Linux Daemon designed to monitor systems internally, can email you when they occur, and provides a nice web interface on port 2812. Additionally, it checks on an interval (every 2 minutes by default) that helps keep your server awake.
It also has built-in functionality to mitigate problems automatically, such as restarting services.
Important Note: The config file that gets installed by default is quite out of date and requires some tinkering, which I will talk about in detail below.
Written: March 12, 2012
Applies to: Ubuntu 11.10 Server
- Install Monit
1sudo apt-get install monit - Edit the Monit Config File
1sudo nano /etc/monit/monitrc - Enable the web interface
set httpd port 2812
# use address localhost # only accept connection from localhost
allow localhost # allow localhost to connect to the server and
allow 192.168.1.0/255.255.255.0 # allow any host on 192.168.1.* subnet
allow admin:pa$$w0rd # require user 'admin' with password 'pa$$w0rd' - Optional: Enable Monitoring of General System Resources
check system hostname.domain.com
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if memory usage > 75% then alert
if swap usage > 25% then alert
if cpu usage (user) > 70% then alert
if cpu usage (system) > 30% then alert
if cpu usage (wait) > 20% then alert - Optional: Enable MySQL Monitoring
Monit uses .pid files to monitor running processes. If you plan to monitor MySQL, this file may not exist. To check, do
1sudo cat /var/run/mysqld/mysqld.pid
If you see a message like this: cat: /var/run/mysqld/mysqld.pid: No such file or directory, modify your mysql my.cnf file as follows:
1sudo nano /etc/mysql/my.cnf
Under the mysqld heading, add the line
pid-file = /var/run/mysqld/mysqld.pid
then doAdd a new section to the Monit config file for MySQL.1sudo restart mysql
#mysql
check process mysql with pidfile /var/run/mysqld/mysqld.pid
group database
start program = "/etc/init.d/mysql start" with timeout 60 seconds
stop program = "/etc/init.d/mysql stop"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout - Optional: Enable NGINX Web Server Monitoring
#nginx
check process nginx with pidfile /opt/nginx/logs/nginx.pid
- Optional: Enable Apache 2 Web Server Monitoring
Find the Apache process section, and modify it as follows, paying special attention to pidfile, start program and stop programcheck process apache2 with pidfile /var/run/apache2.pid
start program = "/etc/init.d/apache2 start" with timeout 60 seconds
stop program = "/etc/init.d/apache2 stop"
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 200.0 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
# if failed host www.tildeslash.com port 80 protocol http
# and request "/somefile.html"
# then restart
# if failed port 443 type tcpssl protocol http
# with timeout 15 seconds
# then restart
if 3 restarts within 5 cycles then timeout
# depends on apache_bin
group server - Optional: Set up SSH Monitoring
#ssh
check process sshd with pidfile /var/run/sshd.pid
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 - Optional: Set up your mail server settings to get email alerts
set mailserver smtp.domain.com
username "smtp" password "pa$$w0rd" - Comment out the Includes directive
# include /etc/monit/conf.d/* - Save the File
- Check your syntax
Fix any problems found – it’s not too tough to figure out what’s going on.
1sudo monit -t
When you see Control file syntax OK, or if you see no errors, you’re good to proceed. - Start (or restart) Monit
or1sudo service monit start1sudo service monit restart - Visit the web interface
http://localhost:2812 if you’re running Ubuntu Desktop, or
http://hostname.domain.com:2812
from a host on the configured network - Sign in with your admin:pa$$w0rd credentials
Acknowledgements and Further Resources
Thanks very much to the following people for helping me along:
- Monit Install and Configuration Guide
- http://www.ubuntugeek.com/monitoring-ubuntu-services-using-monit.html
From 2007, but Monit hasn’t changed much since then - http://www.saltycrane.com/blog/2008/09/how-monitor-apache-web-server-using-monit/
From 2008, has updated info for Apache 2 - http://danasmera.com/monit-mysql-monitoring-problem
From Aug, 2011, has updated info for MySQL

