Tuesday, July 28, 2015

Installation and Configuration

MONIT
You can use Monit to monitor daemon processes or similar programs running on localhost. Monit is particularly useful for monitoring daemon processes, such as those started at system boot time. For instance sendmail, sshd, apache and mysql. In contrast to many other monitoring systems, Monit can act if an error situation should occur, e.g.; if sendmail is not running, monit can start sendmail again automatically or if apache is using too many resources (e.g. if a DoS attack is in progress) Monit can stop or restart apache and send you an alert message. Monit can also monitor process characteristics, such as how much memory or cpu cycles a process is using.

Here I describe the monit installation and configuration on CentOS 6.4.
Download the monit package:
Here I use the version 
monit-5.5.tar.gz
My install location 
/opt/install

Before install monit, install the dependencies:
yum install gcc pam-devel openssl-devel make

It takes some time to finish

After the successful installation, unzip the monit package
          $ tar xvfz monit-5.5.tar.gz

Enter into the Monit folder

          $ cd monit-5.5

          $ ./configure             # By default it install the monit

          $ make 

          $ make install

Then copy the monit config file and paste into /etc folder

         $ cp monitrc /etc/monitrc

Permission define for the config file

        $ chmod 700 /etc/monitrc

Make the monit entry for the system boot time

        $ vim /etc/inittab

        mo:2345:respawn:/usr/local/bin/monit -Ic /etc/monitrc         #Enter into the last line of the inittab file

Check the configuration settings

        $ /usr/local/bin/monit -t /etc/monitrc

It should return 

"Control file syntax OK"  # monit installation successful

       $  /usr/local/bin/monit -v -c /etc/monitrc        # it returns the parameter settings of the installed monit

http://<<hostname/IP address>:2812 # To get the WebUI of monit. 
                                                           #  By default username: admin & password : monit

If you have any webUI problem, then the 2812 port must not be opened!

Monit Configuration:

Edit /etc/monitrc

Example monitrc file

##General Section
line # 19  
set daemon  60              # check services at 1-minute intervals

line #116-118
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'

## Mail Alert section
set mail-format {
 from: xyz@mydomain.com
 Subject:Monit:[$SERVICE] @ [$HOST] $DESCRIPTION
 message: $SERVICE

  Host  : $HOST
  Date  : $DATE
  Action  : $ACTION 
  Description : $DESCRIPTION
 
 Your's faithfully,
 Monit Team
}

#Check system service
check system <<host name>>
 if cpu usage (user) > 70% then alert
 if cpu usage (system) > 30% then alert
 if cpu usage (wait) > 20% then alert
 if memory usage > 80% for 2 cycles then alert  
                                         # monit check the memory for 2 min(each cycle 60 seconds) and then alert

#Additional service to include

## Monitor Tomcat Service
check process Tomcat with pidfile /opt/cluster/tomcat/tomcat.pid
group Tomcat
start program = "/opt/cluster/tomcat/bin/startup.sh" with timeout 60 seconds
stop program = "/opt/cluster/tomcat/bin/shutdown.sh"
if 5 restarts within 5 cycles then timeout
if Memory Usage > 70%  then alert

The above configurations are described for default mail alerts. If you need customised mail alerts follow below..


  • Monit has options to execute the mail alerts through shell scripts.
  • Create and shell script files in a folder and give the path at monitrc file. 

Sample mail script file:

##
#!/bin/bash

recipients="xyz@mydomain.com","abcd@mydomain.com"
subject="Monit:[Hostname] CPU Load reaches more than 70%"
from="admin@mydomain.com"

message_txt="Hi,

  Host : <<HOSTNAME>>
 
  Action : Alert
 
  Description:- CPU Load reaches more than 70% 
 By,
 Monit Team."

/usr/sbin/sendmail "$recipients" << EOF
subject:$subject
from:$from
$message_txt
EOF

save the file as cpuload and give 777 permission to access.

Now we add the file path at monitrc config file.

 if cpu usage (user) > 70% then exec "<file path>/cpuload" 

# here use exec function instead of alert to execute our custom shell script for mail alert. similarly you can use multiple scripts for each alert and give the correct path for the alerts.