We will leave the optional web interface out as we do not need it…
RPMforge Repository has to be present because monit is not a standard CentOS package.
First, we install monit via yum:
yum install monit
Then, we set the monit service to autostart on boot:
chkconfig --levels 235 monit on
Next, we create a config file to monitor httpd:
nano /etc/monit.d/monitrc
with the following content (Apache & SSHd):
set daemon 120 set logfile syslog facility log_daemon set mailserver localhost set mail-format from: [email protected] set alert [email protected] check process sshd with pidfile /var/run/sshd.pid start program "/etc/init.d/sshd start" stop program "/etc/init.d/sshd stop" if failed port 22 protocol ssh then restart if 5 restarts within 5 cycles then timeout check process apache with pidfile /var/run/httpd.pid group www start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/httpd stop" if failed host www.your-site.com port 80 protocol http and request "/monit/token" then restart if cpu is greater than 60% for 2 cycles then alert if cpu > 80% for 5 cycles then restart if totalmem > 500 MB for 5 cycles then restart if children > 250 then restart if loadavg(5min) greater than 10 for 8 cycles then stop if 3 restarts within 5 cycles then timeout
When using NGINX instead of Apache:
check process nginx with pidfile /var/run/nginx.pid group www start program = "/etc/init.d/nginx start" stop program = "/etc/init.d/nginx stop" if failed host www.example.com port 80 protocol http and request "/monit/token" then restart if cpu is greater than 60% for 2 cycles then alert if cpu > 80% for 5 cycles then restart if totalmem > 500 MB for 5 cycles then restart if children > 250 then restart if loadavg(5min) greater than 10 for 8 cycles then stop if 3 restarts within 5 cycles then timeout
Now, we create the directory for the monit id file:
mkdir /var/monit
and the token file for the service check:
mkdir /path/to/your/site/monit echo "hello" > /path/to/your/site/monit/token touch /path/to/your/site/monit/index.html
Finally, start monit:
service monit start