How to Install and Configure Monit on Linux Server for Process Monitoring

Monit is a open source utility for managing and monitoring, processes, files, directories and devices on a Unix system. Monit can perform automatic maintenance and repair and can execute useful actions in error situations.

E.g.
monit can start a process if it does not run,
monit can restart a process if it does not respond,
monit can stop a process if it uses to much resources.

You may use monit to monitor files, directories and devices for changes, such as timestamp changes, checksum changes or size changes.

Steps
1. Download the latest monit from rpmforge repository.
For x86 (32-bit) systems

http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/monit-5.2.5-1.el5.rf.i386.rpm

For x64 (64-bit) systems

http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/monit-5.2.5-1.el5.rf.x86_64.rpm

[root@server ~]# wget http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/monit-5.2.5-1.el5.rf.i386.rpm
--2011-08-14 00:12:58--  http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/monit-5.2.5-1.el5.rf.i386.rpm
Resolving apt.sw.be... 193.1.193.67
Connecting to apt.sw.be|193.1.193.67|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 573934 (560K) [application/x-redhat-package-manager]
Saving to: `monit-5.2.5-1.el5.rf.i386.rpm'

100%[=============================================================>] 573,934     20.9K/s   in 34s

2011-08-14 00:13:38 (16.6 KB/s) - `monit-5.2.5-1.el5.rf.i386.rpm' saved [573934/573934]

2. Install the monit rpm

[root@server ~]# rpm -Uvh monit-5.2.5-1.el5.rf.i386.rpm
Preparing...                ########################################### [100%]
   1:monit                  ########################################### [100%]

3. Configure monit

[root@server ~]# vi /etc/monit.conf

 set daemon  120           # check services at 2-minute intervals
 set logfile syslog facility log_daemon
 set mailserver mail.server.local,               # primary mailserver
 set alert admin@server.local 

 set httpd port 2812 and
     allow localhost        # allow localhost to connect to the server and
     allow 192.168.2.2        # allow client ip address to connect to the server and
     allow admin:monit      # require user 'admin' with password 'monit'

4. Create /var/monit directory to place id and state

[root@server ~]# mkdir /var/monit

5. Configure service to be monitor

[root@server ~]# vi /etc/monit.d/localhost
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 127.0.0.1 port 80 protocol http then restart
        if 5 restarts within 5 cycles then timeout

check process sshd with pidfile /var/run/sshd.pid
        start program "/etc/init.d/sshd start"
        stop program "/etc/init.d/sshd stop"
        if failed host 127.0.0.1 port 22 protocol ssh then restart
        if 5 restarts within 5 cycles then timeout

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 failed host 127.0.0.1 port 3306 then restart
        if 5 restarts within 5 cycles then timeout


6. Set monit to auto start on boot up.

[root@server ~]# vi /etc/default/monit
# You must set this variable to for monit to start
startup=1

or

[root@server ~]# chkconfig monit on

7. Start monit service

[root@server ~]# service monit start
Starting monit: monit: generated unique Monit id 50baacc26c17a307fe4cbdc1616c0bf8 and stored to '/var/monit/id'
Starting monit daemon with http interface at [*:2812]
                                                           [  OK  ]

or

[root@server ~]# /etc/init.d/monit start
Starting monit: Starting monit daemon with http interface at [*:2812]
                                                           [  OK  ]

8. Monitor monit from your browser

Monit Web interface will run on the port number 2812. Now point your browser to http://serverip:2812/