Friday 20 January 2012

how to Install and configure Monit on Linux server RHEL

The Installation and configuration details are listed below.
............................................................

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.

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

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

2. Install the monit rpm
rpm -Uvh monit-5.2.5-1.el5.rf.i386.rpm

yum install monit

chkconfig --levels 235 monit on
if chkconfig is unreconizable and installed then enter the command on you terminal and check again it will work.
su - presss enter
chkconfig --levels 235 monit on

3. Configure monit
vi /etc/monit.conf

set daemon 120 # check services at 2-minute intervals
set logfile /var/log/monit/access.log
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 IP.x.x.x # 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
mkdir /var/monit

5. Configure service to be monitor

vim /etc/monit.d/monitrc

check process proftpd with pidfile /var/run/proftpd.pid
start program = "/etc/init.d/proftpd start"
stop program = "/etc/init.d/proftpd stop"
if failed port 21 protocol ftp 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 port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

check process mysql with pidfile /var/run/mysqld/mysqld.pid
group database
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

check process apache with pidfile /var/run/httpd.pid
group www
start program = "/etc/init.d/httpd start"
stop program = "/etc/init.d/httpd stop"
if failed host www.serverweb.com port 80 protocol http
and request "/monit/token" then restart
if cpu is greater than 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 500 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
if 3 restarts within 5 cycles then timeout

check process postfix with pidfile /var/spool/postfix/pid/master.pid
group mail
start program = "/etc/init.d/postfix start"
stop program = "/etc/init.d/postfix stop"
if failed port 25 protocol smtp then restart
if 5 restarts within 5 cycles then timeout

check process postgresql with pidfile /opt/postgres/data/postmaster.pid
group database
start program = "/etc/init.d/postgresql start"
stop program = "/etc/init.d/postgresql stop"
if failed unixsocket /tmp/.s.PGSQL.5432 protocol pgsql then restart
if failed unixsocket /tmp/.s.PGSQL.5432 protocol pgsql then alert
if failed host localhost port 5432 protocol pgsql then restart
if failed host localhost port 5432 protocol pgsql then alert
if 5 restarts within 5 cycles then timeout

:wq!

6: mkdir /var/www/serverweb/monit
echo "hello" > /var/www/serverweb/monit/token

7:mkdir /var/log/monit

8:Finally, we can start monit:
/etc/init.d/monit start

9: access the web interface www.serverweb.com:2812
username: admin
password: monit

10: you can add the application data to monitor to /etc/monit.d/monitrc
as i have did it in my server.

11: Create a possward for the user to login through web interface root# htpasswd -c /etc/monitpasswd admin

IMPORTANT NOTE
(To configure postgresql server and services you have to follow the steps below).

postgresql support:

Step # 1: Becoming a superuser

You need to login as database super user under postgresql server. Again the simplest way to connect as the postgres user is to change to the postgres unix user on the database server using su command as follows:
su - postgres
Step #3: Now connect to database server

Type the following command
$ psql template1
OR
$ psql -d template1 -U postgres
Output:

Welcome to psql 7.4.16, the PostgreSQL interactive terminal.
Type: \\copyright for distribution terms
\\h for help with SQL commands
\\? for help on internal slash commands
\\g or terminate with semicolon to execute query
\\q to quit
template1=#

Step #4: Add a user called monit

Type the following command to create a user called monit with a password called myPassword

template1=# CREATE USER monit WITH PASSWORD 'myPassword';

Step #5: Add a database called monit

Type the following command

template1=# CREATE DATABASE monit;

Now grant all privileges on database

template1=# GRANT ALL PRIVILEGES ON DATABASE monit to monit;
Type \q to quit:

template1=# \q

Step #6: Test tom user login

In order to login as monit you need to type following commands. Login as monit or use su command:
$ su - monit
$ psql -d monit -U monit

12: 1. Create DB user `root'.
2. Create a database 'root' owned by root. It doesn't need to contain any data.
3. Add these descriptions to pg_hba.conf;

host root root 127.0.0.1/32 trust <= for test via TCP port
local root root ident sameuser <= for test via UNIX socket
now the monit can restart the postgresql if there is any exception occurs in postgresql responding.

13: /etc/ini.d/monit restart

14:open your browser and access the link http://www.severweb.com:2812

That's all about monit installation and configuration

Regards & Thanks
Imran Ullah

No comments:

Post a Comment