Monit is an application service monitor. It can tell you if your MySQL or Apache server has crashed, and even give it a swift kick to get it up and going again.
Compared to other service monitors I've looked at, Monit has good
documentation, and a straightforward configuration.
It's available as a Debian package and there is an RPM here:
http://dag.wieers.com/rpm/packages/monit/On Debian, it won't start automatically after installing:
apt-get install monit
Reading package lists… Done
Building dependency tree… Done
The following NEW packages will be installed:
monit
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0B/255kB of archives.
After unpacking 680kB of additional disk space will be used.
Selecting previously deselected package monit.
(Reading database … 58858 files and directories currently installed.)
Unpacking monit (from .../monit_1%3a4.8.1-2.1_i386.deb) …
Setting up monit (4.8.1-2.1) …
Starting daemon monitor: monit won't be started/stopped
unless it it's configured
please configure monit and then edit /etc/default/monit
and set the "startup" variable to 1 in order to allow
monit to startIn /usr/share/doc/monit/README.Debian, your find some information about creating a monit_delay script to prevent monit from restarting services that are slow to come up on reboot.
Now all you need to do is go uncomment some things in the configuration file. You'll find that in /etc/monit/monitrc, or if you use the RPM, it's /etc/monit.conf
There are many options for configuring monit, here is what I uncommented to get a simple configuration working:
set daemon 120
set logfile syslog facility log_daemon
set mailserver localhost
set alert root@localhost
set httpd port 2812 and
use address localhost # only accept connection from localhost
allow localhost # allow localhost to connect to the server and
allow admin:stR0Ngpsswd # require user 'admin' with password 'monit'
Instead of putting services in this same file, put them in /etc/monit.d, that way it's easy to turn off/on monitoring for a particular service.
Restart Apache
For testing Apache's status, it's standard practice to create an empty file or 'token' for Monit to request.
On Debian
mkdir /var/www/monit/
touch /var/www/monit/token
Paste this in your Apache configuration:
SetEnvIf Request_URI "^/monit/token$" dontlog
CustomLog logs/access.log common env=!dontlog
Then paste this in /etc/monit.d/apache.conf
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
if failed host 127.0.0.1 port 80
protocol HTTP request /monit/token then restart
if 5 restarts within 5 cycles then timeoutThen edit /etc/default/monit
# You must set this variable for monit to start
startup=1
Startup monit
check that it's running and then for fun, lets stop apache:
ps aux | grep monit
root 11583 0.0 0.7 20400 1248 ? Sl 04:20 0:00 /usr/sbin/monit -d 180 -c /etc/monit/monitrc -s /var/lib/monit/monit.state
/etc/init.d/apache2 stop
In syslog you'll see something like this:
Jun 18 04:32:41 rimu monit[11583]: HTTP error: Server returned status 404
Jun 18 04:32:41 rimu monit[11583]: 'apache2' failed protocol test [HTTP] at INET[127.0.0.1:80] via TCP
Jun 18 04:32:41 rimu monit[11583]: 'apache2' trying to restart
Jun 18 04:32:41 rimu monit[11583]: 'apache2' stop: /etc/init.d/apache2
Jun 18 04:32:42 rimu monit[11583]: 'apache2' start: /etc/init.d/apache2
You can find many other configuration examples for MySQL, Mongrel and other popular applications, just google them up.
watch nginx
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
if failed port 80 protocol HTTP request / then restart
if 5 restarts with 5 cycles then timeout
watch mongrels
http://www.igvita.com/blog/2006/11/07/monit-makes-mongrel-play-nice/
http://software.pmade.com/blogs/ramblings/2006/12/27/mongrel-cluster-and-monit
http://rubyforge.org/pipermail/mongrel-users/2006-September/001359.html
watch a VirtualHost or remote URL
check host arbitrary_name with address real_url
if failed port 80 proto http then alert
See the man page for more examples.