how to install and configure monit on Linux server

Summary:  In this article i tried to explain how to install and configure monit on linux server.

The Installation and configuration details are listed below.

Install and Configure Monit on Linux Server for Process Monitoring
Monit is a open source tool for managing and monitoring, processes, files, directories and devices on a Unix system. Monit perform useful action in error situations through automatic maintenance and repair.

Few examples are listed below.
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 can use monit to monitor devices for changes like timestamp, files and directories. If the size changes or checksum updates.

Step-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

Step-2: Install the monit rpm

rpm -Uvh monit-5.2.5-1.el5.rf.i386.rpm

Step-3: Install Monit through yum

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

Step-4: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’

Step-5: Create /var/monit directory to place id and state

mkdir /var/monit

Step-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!

Step-7: Create a weblink directory for monit

mkdir /var/www/serverweb/monit
echo “hello” > /var/www/serverweb/monit/token

Step-8: Create Log Dirctory for monit

mkdir /var/log/monit

Step-9:Finally, we can start monit:

/etc/init.d/monit start

Step-10: Access the web interface www.serverweb.com:2812
username: admin
password: monit

Step-11: You can add the application data to monitor to /etc/monit.d/monitrc
as i did it on my server.
Step-12: 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-13: 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-14: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-15: 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-16: 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-17: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

Create DB user `root’.
Create a database ‘root’ owned by root. It doesn’t need to contain any data.
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.

Step-18: Restart the Monit service

/etc/ini.d/monit restart

Step-19: Open your browser and access the link http://www.severweb.com:2812

That’s all about monit installation and configuration

Leave a Reply

Your email address will not be published. Required fields are marked *