Monitoring Apache and MYSQL With Monit


Monitoring Apache and MYSQL With Monit Using Centos

Monit is a linux package that can be used to monitor and restart processes automatically if they go down. This brief article will explain how to install and configure Monit.


Installing Monit

 root# yum install monit 

Now that you have installed Monit, the fun begins. The main files you will access and configure are the main configuation file located at /etc/monit.conf and its log file located at /var/log/monit. All editing will take place with configuration file while the log files will show you what it has been doing and can be used for troubleshooting. 

Configuring Monit

The main configuration file is located at '/etc/monit.conf'. You can open it in your desired editor like Vi or Nano. A shortened configuration file is shown below. This file only checks for the Apache and mySQL services. More often than not, one of these two services would be the likely culprit that brings down your website(s). Take a look at the configuration file. After the file, there is a description about 'how it works'. When you make any changes to the monit.conf file, you need to reload the configuration file with the 'reload monit' command or restart it with the '/etc/init.d/monit restart' command.

 root# vi /etc/monit.conf 
## Start monit in the background (run as a daemon) and check services at
## 2-minute intervals.
#
set daemon  120
#
#
## Set syslog logging with the 'daemon' facility. If the FACILITY option is
## omitted, monit will use 'user' facility by default. If you want to log to
## a stand alone log file instead, specify the path to a log file
#
set logfile syslog facility log_daemon
#
#
###############################################################################
## Services
###############################################################################
#
#
  check file apache_bin with path /usr/local/apache/bin/httpd
    if failed checksum and
       expect the sum 8f7f419955cefa0b38a2ba316cba3657 then unmonitor
    if failed permission 755 then unmonitor
    if failed uid root then unmonitor
    if failed gid root then unmonitor
#    alert security@foo.bar on {
#           checksum, permission, uid, gid, unmonitor
#        } with the mail-format { subject: Alarm! }
    group nobody
#
#
## Check that a process is running, in this case Apache, and that it respond
## to HTTP and HTTPS requests. Check its resource usage such as cpu and memory,
## and number of children. If the process is not running, monit will restart
## it by default. In case the service was restarted very often and the
## problem remains, it is possible to disable monitoring using the TIMEOUT
## statement. This service depends on another service (apache_bin) which
## is defined above.
#
  check process apache with pidfile /var/run/httpd.pid
    group apache
    start program = "/scripts/restartsrv httpd"
    stop program  = "/scripts/restartsrv httpd"
    #restart = "/etc/init.d/httpd restart"
    #if cpu > 60% for 2 cycles then alert
    #if cpu > 80% for 5 cycles then restart
    #if totalmem > 200.0 MB for 5 cycles then restart
    #if children > 250 then restart
    #if loadavg(5min) greater than 10 for 8 cycles then stop
    if failed host 127.0.0.1 port 80 protocol http
       then restart
    #if failed port 443 type tcpssl protocol http
    #   with timeout 15 seconds
    #   then restart
    if 5 restarts within 5 cycles then timeout
    #depends on apache_bin
   group nobody
#
#
check process mysqld with pidfile /var/lib/mysql/vps.example.com.pid
#group database
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
#
#
###############################################################################
## Includes
###############################################################################
##
## It is possible to include additional configuration parts from other files or
## directories.
#
#  include /etc/monit.d/*
#
#

# set daemon mode timeout to 1 minute
# set daemon 60
# Include all files from /etc/monit.d/
include /etc/monit.d/*

How It Works
Okay, the above code shows 2 blocks for Apache and one for mySQL. Too keep it simple, Monit looks for the adequate process ids for the Apache and mySQL services on your server every 2 minutes. If it does not find them, the service(s) are restarted.