Keeping Services Alive with Monit

On CentOS install monit as follows:

yum install monit

Make sure it starts automatically:

/sbin/chkconfig --levels 235 monit on

The main configuration file is at /etc/monit.conf but this file loads in any files under /etc/monit.d which is where we will place our files.

First comment out the following sections in /etc/monit.conf as we will be defining this in the next step:

### set daemon  60

###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:monit      # require user 'admin' with password 'monit'
###    allow @monit           # allow users of group 'monit' to connect (rw)
###    allow @users readonly  # allow users of group 'users' to connect readonly

vi /etc/monit.d/main.conf

set daemon  30
set logfile syslog facility log_daemon
set mailserver localhost
set mail-format { from: hostname@address.com }
set alert jonny@address.com
set httpd port 2812 and
use address hostname.address.com  # only accept connection from localhost
allow admin:opensesame    # require user 'admin' with password
allow @monit           # allow users of group 'monit' to connect (rw)
allow @users readonly  # allow users of group 'users' to connect readonly

vi /etc/monit.d/httpd.conf

check process apache with pidfile /var/run/httpd.pid
group apache
start program = "/etc/init.d/httpd start"
stop program  = "/etc/init.d/httpd stop"
if failed host hostname.address.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

Create the monit token file referred to above:

touch /var/www/monit-token

Start monit:

/etc/init.d/monit start

Visit the web interface e.g. http://hostname.address.com:2812

Ensure that monitoring of the apache processes is enabled:

 

Leave a Reply

  • (will not be published)

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