Resources
Monit: http://mmonit.com/monit/
Wiki: http://mmonit.com/monit/documentation/monit.html
Man: http://linux.die.net/man/1/monit
Installation
Other packes I needed for my box
yum install pam-devel
yum install openssl-devel
Get and install Monit
cd /usr/local/src
wget http://mmonit.com/monit/dist/monit-5.5.tar.gz
tar -zxvf monit-5.5.tar.gz
cd monit-5.5
./configure (If this fails, fix as necessary, then re-unpack the tar and try again)
make
make install
Setup monitrc
cp monitrc /etc/
vi /etc/monitrc
# At the end of monitrc add or uncomment: include /etc/monit.d/*
# Also set the 'set daemon' line at the beginning to your preferred interval.
mkdir /etc/monit.d
Create the service files (this will repeat for every service you monitor)
vi /etc/monit.d/apache
#Now Inside the apache file:
check process httpd with pidfile /var/run/httpd.pid
start program = "/etc/init.d/httpd start" with timeout 20 seconds
stop program = "/etc/init.d/httpd stop"
if failed host 127.0.0.1 port 80 protocol http
and request "/index.html"
then restart
if 5 restarts within 5 cycles then timeout
check system localhost
if memory usage > 85% then alert
if cpu usage (user) > 80% for 3 cycles then alert
if cpu usage (system) > 80% for 3 cycles then alert
To see more services, check this out.
You can see in the above code we used alert. To setup alerts go into the /etc/monitrc file and do 2 things. First change the set mailserver
line to localhost (or your mailserver). Then change the set alert
line to include your email. Thats it.
Now setup the init.d file
cp contrib/rc.monit /etc/init.d/monit
chmod 755 /etc/init.d/monit
You may need to fix the line inside the above file thats pointing at /usr/bin/monit
to /usr/local/bin/monit
.
After this is in place you should have the service monit restart
command available.
To start Monit at boot, edit vim /etc/rc.d/rc.local
and add in the next line
/usr/local/bin/monit -c /etc/monitrc -p /var/run/monit.pid -l /var/log/monit.log
Go ahead and run the above line in the console to see if monit works. If so, a call to service httpd stop
should cause monit to restart apache.
Finished
Monit should be all setup. You can check with service monit status
or ps aux | grep monit
.
Another cool feature of monit is the web interface. Go to http://localhost:2812/ and enter the username and password form your monitrc file, it should look something like this, feel free to change it:
set httpd port 2812
allow hauk:password
Main Files
/etc/monitrc - Monit's control file
/etc/monit.d/* - all services Monit will track
/etc/init.d/monit - service control file
/usr/local/src/monit-5.5 - source code
/usr/local/bin/monit