How To Install Monit On Centos / Redhat

by lifeLinux on May 13, 2011

What is Monit ?

Monit is a free open source utility for managing and monitoring, processes, files, directories and filesystems on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.

What Monit can do

Monit can start a process if it does not run, restart a process if it does not respond and stop a process if it uses too much resources. You can use Monit to monitor files, directories and filesystems for changes, such as timestamp changes, checksum changes or size changes. You can also monitor remote hosts; Monit can ping a remote host and can check TCP/IP port connections and server protocols. Monit is controlled via an easy to use control file based on a free-format, token-oriented syntax. Monit logs to syslog or to its own log file and notifies you about error conditions and recovery status via customizable alert.

Install Monit

The first, you need to enable EPEL (Extra Packages for Enterprise Linux) to install monit package. Login as root and type the following command:

[root@lifelinux ~]# vi /etc/yum.repos.d/epel.repo

Add or uncomment the following content at end of the file

[epel]
name=Extra Packages for Enterprise Linux 5 - $basearch
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-5&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0

Save and close the file. And type the following command

[root@lifelinux ~]# yum clean all

To install monit, type the following command

[root@lifelinux ~]# yum install monit

Turn on monit when system start up

[root@lifelinux ~]# chkconfig --levels 235 monit on

Configure Monit

The configuration file of monit in Centos or RedHat is /etc/monit.conf. Type the following command to edit

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

Sampe configuration file

set daemon 60
set logfile /var/log/monit.log
set mailserver localhost
set mail-format { from: [email protected]
subject: $SERVICE $EVENT at $DATE
message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
}
set alert [email protected]
include /etc/monit.d/*

Now to monitor Apache, create a file /etc/monit.d/httpd, enter

[root@lifelinux ~]# vi /etc/monit.d/httpd

Add following content

check process httpd with pidfile /var/run/httpd.pid
group apache
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

MySQL server restart configuration directives

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

SSH server configuration directives

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

Type the following command to start monit

[root@lifelinux ~]# /etc/init.d/monit start

You can verify that monit is started from /var/log/monit.log log file:

[root@lifelinux ~]# tail -f /var/log/monit.log

Sample ouputs

[ICT May 12 14:51:18] info     : 'system_server2.domain.com' Monit started

Further readings

Monit home page
Monit documenation
Monit configuration examples

Related Posts:

Previous post:

Next post: