=== From Wiki ===
Monit is a utility for managing and monitoring processes, files, directories and devices on a Unix system. It’s a daemon that wakes up every once in a while, goes through a list of things it is configured to check, and if something is not as it should be, takes a corrective action.
Monit tool is available in EPEL repository. Here i have already installed EPEL repository, if you need to install then please type epel in search box and follow the steps to perform the same.
Operating System: CentOS 6.4
Hostname: server
IP Address: 192.168.1.6
Step 1: Install prerequisites package
[root@server ~]# yum install -y monit httpd
Step 2: Monit configuration
[root@server ~]# vi /etc/monit.conf
Uncomment the following lines
....
....
set daemon 120
....
....
set httpd port 2812 and
use address localhost
allow localhost
allow admin:monit
....
....
Save and exit (:wq!)
Step 3: Start the monit and apache daemon
[root@server ~]# /etc/init.d/monit start
Starting monit: [ OK ]
[root@server ~]# /etc/init.d/httpd start
[root@server ~]# chkconfig --level 35 monit on
[root@server ~]# chkconfig --level 35 httpd on
Now open your web browser and type http://localhost:2812 or http://ipaddress:2812, you will be asked to enter the username and passowrd.
Username: admin
Password: monit
Check the monit log for more details.
[root@server ~]# tail -f /var/log/monit
[IST Apr 17 10:14:48] info : Starting monit HTTP server at [localhost:2812]
[IST Apr 17 10:14:48] info : monit HTTP server started
[IST Apr 17 10:14:48] info : Monit started
[IST Apr 17 10:15:48] info : Monit has not changed
Step 4: Add service to the monit
Now it’s the time to add your critical service to monitor. Here i am going to use my apache server and this will be monitor by monit tool for every 2 mins.
[root@server ~]# vi /etc/monit.conf
Add the below lines in EOF
check process apache with pidfile /var/run/httpd.pid
start program = "/etc/init.d/httpd start"
stop program = "/etc/init.d/httpd stop"
Save and exit (:wq!)
Now check your monit web interface, the newly added apache service is start monitor by monit.
Step 5: Monit Testing
Stop the apache daemon to test our monit tool is working or not.
[root@server ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
[root@server ~]# /etc/init.d/httpd status
httpd is stopped
[root@server ~]# tail -f /var/log/monit
[IST Apr 17 10:30:15] info : Starting monit HTTP server at [localhost:2812]
[IST Apr 17 10:30:15] info : monit HTTP server started
[IST Apr 17 10:30:15] info : Monit started
[IST Apr 17 10:31:15] info : Monit has not changed
[IST Apr 17 10:32:15] error : 'apache' process is not running
Check the same in web interface, the apache will show the “Does not exist” status. Wait for 2 mins our monit tool will automatically start the apache service.
After 2 mins check the apache status and log
[root@server ~]# /etc/init.d/httpd status
httpd (pid 1704) is running...
[root@server ~]# tail -f /var/log/monit
[IST Apr 17 10:30:15] info : Starting monit HTTP server at [localhost:2812]
[IST Apr 17 10:30:15] info : monit HTTP server started
[IST Apr 17 10:30:15] info : Monit started
[IST Apr 17 10:31:15] info : Monit has not changed
[IST Apr 17 10:32:15] error : 'apache' process is not running
[IST Apr 17 10:32:15] info : 'apache' trying to restart
[IST Apr 17 10:32:15] info : 'apache' start: /etc/init.d/httpd
[IST Apr 17 10:33:15] info : 'apache' process is running with pid 1704
That’s it!! Monit has started the apache services automatically without any human intervention.
Hope you enjoy this post…. Thanks 🙂