Install Monit in CentOS

Monit is a free open source utility for managing and monitoring, processes, programs, files, directories and filesystems on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations. You can use Monit to monitor daemon processes or similar programs running on localhost. Monit is particular useful for monitoring daemon processes, such as those started at system boot time from /etc/init.d/. For instance sendmail, sshd, apache and mysql.

You can also use Monit to monitor files, directories and filesystems on localhost. Monit can monitor these items for changes, such as timestamps changes, checksum changes or size changes. This is also useful for security reasons – you can monitor the md5 or sha1 checksum of files that should not change and get an alert or perform an action if they should change.

Monit can monitor network connections to various servers, either on localhost or on remote hosts. TCP, UDP and Unix Domain Sockets are supported. Network test can be performed on a protocol level; Monit has built-in tests for the main Internet protocols, such as HTTP, SMTP etc. Even if a protocol is not supported you can still test the server as you can configure Monit to send any data and test the response from the server.

Monit can be used to test programs or scripts at certain times, much like cron, but in addition, you can test the exit value of a program and perform an action or send an alert if the exit value indicate an error. This means that you can use Monit to perform any type of check you can write a script for.

Finally, Monit can be used to monitor general system resources on localhost such as overall CPU usage, Memory and Load Average.

Monit provides a built-in HTTP(S) interface and you can use a browser to access the Monit server.

Installing Monit

yum install openssl*
wget http://mmonit.com/monit/dist/monit-5.3.2.tar.gz
tar -zxf monit-5.3.2.tar.gz
cd monit-5.3.2
./configure
make && make install
cp monitrc /etc/
cd ..
rm -rf monit-5.3.2*
echo “include /etc/monit.d/*” >> /etc/monitrc
chmod 700 /etc/monitrc
mkdir /etc/monit.d/
nano /etc/monit.d/apache

And copy/paste the following for servers without cPanel and save the file

check process httpd 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 127.0.0.1 port 80 protocol http
and request “/test.html”
then restart
if 5 restarts within 5 cycles then timeout

If its a cPanel server then please enter this code and save the file

check process httpd with pidfile /usr/local/apache/logs/httpd.pid
group nobody
start program = “/scripts/restartsrv httpd”
stop program = “/scripts/restartsrv httpd”
if failed host 127.0.0.1 port 80 protocol http
and request “/test.html”
then restart
if 5 restarts within 5 cycles then timeout

Note: Next step may change according to your system configuration. The location of apache on cPanel server is

/usr/local/apache and htdocs will be /usr/local/apache/htdocs

So you should use this command provided below

echo “Test file” > /usr/local/apache/htdocs/test.html

If you are not using cPanel, then the apache location may be /etc/httpd and htdocs may be /var/www/html. In that case, change the code accordingly

echo “Test file” > /var/www/html/test.html

Add monit to rc.local so that it starts automatically when your server starts.

echo “/usr/local/bin/monit -d 60 -v -c /etc/monitrc  -p /var/run/monit.pid -l /var/log/monit.log” >> /etc/rc.local

Finally, execute monit now.

/usr/local/bin/monit -d 60 -v -c /etc/monitrc  -p /var/run/monit.pid -l /var/log/monit.log

You will see a message “Starting monit daemon” at the end if everything goes perfect.

That should start monit as deamon and check the ports on every 60 seconds.

Extending monit

You can monitor other services using monit such as mysql, exim etc. To do so, create a new file at /etc/monit.d/ and save it with the appropriate commands to check and restart code.

For example, if you want to monitor mysql, you can use the below commands

touch /etc/monit.d/mysql
echo “check process mysqld with pidfile /var/lib/mysql/`hostname`.pid” > /etc/monit.d/mysql
echo “start program = “/etc/init.d/mysql start”” >> /etc/monit.d/mysql
echo “stop program = “/etc/init.d/mysql stop”” >> /etc/monit.d/mysql
echo “if failed host 127.0.0.1 port 3306 then restart” >> /etc/monit.d/mysql
echo “if 5 restarts within 5 cycles then timeout” >> /etc/monit.d/mysql

Those commands will add the following lines to /etc/monit.d/mysql

check process mysqld with pidfile /var/lib/mysql/uk1.techwithus.com.pid
start program = “/etc/init.d/mysql start”
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

Be sure to re-execute monit after adding this.

Your comments are valuable! Post a comment if you have a doubt 🙂




January 25, 2012
/
Previous Post Next Post

Tharun recommends you to read these fantastic articles