fbpx

Server Setup

Monitor Servers with Monit on CentOS 6

by , , revisited on


We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

Monit is a great web app for monitoring websites. It can monitor services and automatically restart them on failure for maximum uptime. Each time Monit performs a restart action, it will alert you via email notification.

Let’s detail the steps on how to install and configure Monit on your web server. You need a virtual or dedicated hosting plan.

Note that our guides are always targeted to CentOS (Redhat) users. The following steps should get you a running instance of Monit in minutes. Let’s get started.

Monit on CentOS 6

Monit is not available via standard repository. We don’t want to mess with 3rd party repository which might introduce inconsistency to our packages. So let’s install the latest Monit version by compiling it from source files. Grab the latest source files download link at official Monit website: scroll down to Downloads section and copy link for “Source code”.

In case you’re not familiar with compiling from source, refer to our quick intro into compiling software in Linux.

Install prerequisites

For successful compilation, we need development libraries for OpenSSL and PAM.

yum -y install pam-devel openssl-devel

Install Monit on CentOS 6

The following commands fetch Monit source archive from their website, extract, and remove the archive. The very last composite command compiles and installs Monit.

cd /usr/local/src
wget http://mmonit.com/monit/dist/monit-5.8.1.tar.gz
tar xvf monit-5.8.1.tar.gz && rm -rf monit-5.8.1.tar.gz
chown -R root.root monit-5.8.1
cd monit-5.8.1
./configure && make && make install

Create a copy of startup file bundled with Monit, make sure that it has correct permissions and enable Monit startup at boot time:

cp ./system/startup/rc.monit  /etc/init.d/monit
chmod 0755 /etc/init.d/monit
chkconfig --add monit
chkconfig --level 235 monit on  

Ensure that the startup file uses correct path for Monit executable:

echo "MONIT=/usr/local/bin/monit" > /etc/sysconfig/monit

Adjust Monit configuration

At this point Monit does not have configuration to run with. It also needs credentials that we will use to login into its web interface.

Set shell variable to the email address where you want to receive notifications.

EMAIL="myemail@domain.com"

Set shell variable to password which will be used for accessing Monit web interface

PASS="mypass"

The following command writes configuration file for Monit on CentOS 6. For hackers out there, you might want to copy configuration file (monitrc) provided with Monit instead of using the command below. This will allow you to see the vast array of configuration options. If you go that route, make sure to uncomment inclusion of monit.d/*.mon files as this is useful in keeping your configuration separate.

But for the sake of this tutorial, our configuration is simple: watch services every 60 seconds, use a separate log file and send all alerts to the email address, that we have set in shell’s session.

cat << _EOF_ > /etc/monitrc
# number of seconds between monit checks
set daemon 60

# log file
set logfile /var/log/monit.log

# email address for alerts
set alert ${EMAIL} 

# set mailserver
set mailserver localhost

# port for http access
set httpd port 2812

# username:password
allow admin:${PASS} 

# service files
include /etc/monit.d/*.mon
_EOF_

Unset password from shell:

unset PASS

Monit configuration for watched services

Next, we should create a special directory, which will hold rules for monitoring various services and server areas.

mkdir /etc/monit.d

Set public IP address into shell variable first. This is for easy updating of monit config files:

PUBLIC_IP=$(grep `hostname` /etc/hosts|grep -Ev "^#"|head -1|cut -f 1|cut -d' ' -f1)

Verify that PUBLIC_IP contains external IP of your network adapter, by simple echo $PUBLIC_IP. If not, you will need to set it manually, i.e.

PUBLIC_IP="82.209.102.5"

Monitor Apache web server

This adds apache.mon rule file into our monit.d services directory. This will monitor port 80 (this is the port Apache listens to). Additionally, it will monitor CPU. If access to port 80 failed, Monit will restart Apache. And if CPU usage is too intensive for prolonged period of time, Apache is also restarted automatically.

cat << _EOF_ > /etc/monit.d/apache.mon
check process httpd with pidfile "/var/run/httpd.pid"
start program = "/etc/init.d/httpd start"
stop program = "/etc/init.d/httpd stop"
if failed host ${PUBLIC_IP} port 80 protocol http then restart
if cpu > 90% for 2 cycles then alert
if cpu > 90% for 5 cycles then restart
if 5 restarts within 5 cycles then timeout
_EOF_

Monitor MySQL database

Let’s add mysql.mon for monitoring MySQL server instance. Same as with Apache rules, Monit will restart MySQL if CPU usage has been intensive longer than usual, and it will monitor port 3306, which is specific for MySQL.

cat << _EOF_ > /etc/monit.d/mysql.mon
check process mysqld with pidfile "/var/run/mysqld/mysqld.pid"
start program = "/etc/init.d/mysqld start"
stop program = "/etc/init.d/mysqld stop"
if cpu > 90% for 2 cycles then alert
if cpu > 90% for 5 cycles then restart
if failed port 3306 protocol mysql then restart
if 5 restarts within 5 cycles then timeout
_EOF_

At this point, you are ready to run Monit and have it monitor Apache and MySQL. In case of automatic restarts, Monit will let you know at your email address.

service monit start

Now Monit is running and pro-actively monitoring services.

Note on restarting monitored services

If you plan on restarting monitored services, it’s preferable to use Monit, i.e.: monit restart httpd

You can access monit interface at http://yourserverip:2812 providing username admin and the password we’ve set earlier.

Happy monitoring!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.