Install and Configure Monit on CentOS/RHEL/Ubuntu/Debian

Monit is a free open-source tool used to manage and monitor processes, programs, files, directories and filesystems on a local or remote LINUX/UNIX systems. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.

monit_banner

It starts a process automatically if it is stopped, restart a process if it doesn’t respond and stop the process it it consumes more resources. This is the main difference between Monit and other monitoring tools.

In this article, let us see how to install Monit on CentOS/RHEL/Scientific Linux/Ubuntu/Debian systems.

Install EPEL Repository

Monit is found in CentOS official repository. So we can install it from EPEL repository. To install EPEL repository, enter the following command:

[root@server ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Install Monit on CentOS/RHEL/Scientific Linux:

[root@server ~]# yum install monit -y

Install Monit on Ubuntu/Debian:

sk@sk:~$ sudo apt-get install monit

Configure Monit

The configuration file of monit on CentOS/RHEL is /etc/monit.conf  and on Ubuntu/Debian it is /etc/monit/monitrc. The default port of monit is 2812. You need to enable the port in the configuration file. You can access the monit web interface with URL http://localhost:2812.  The default username and password to access monit web interface are admin and monit. You can change username and password as your liking. The monit will monitor all services every two minutes that you defined in the configuration file.

Uncomment the following section in the configuration file:

In CentOS/RHEL systems:

[root@server ~]# vi /etc/monit.conf
 [...]
#Uncomment the following lines
set httpd port 2812 and
use address localhost  # only accept connection from localhost
allow localhost        # allow localhost to connect to the server and
allow admin:monit      # require user 'admin' with password 'monit'
allow @monit           # allow users of group 'monit' to connect (rw)
 allow @users readonly  # allow users of group 'users' to connect readonly
#
[...]

In Ubuntu/Debian systems:

sk@sk:~$ sudo vi /etc/monit/monitrc
[...]
#Uncomment the following lines
set httpd port 2812 and
use address localhost  # only accept connection from localhost
allow localhost        # allow localhost to connect to the server and
allow admin:monit      # require user 'admin' with password 'monit'
allow @monit           # allow users of group 'monit' to connect (rw)
allow @users readonly  # allow users of group 'users' to connect readonly
#
[...]

Once you done, start monit service and let it to start automatically on every reboot.

In CentOS/RHEL:

[root@server ~]# service monit start
 Starting monit: monit: generated unique Monit id 685652473acf3e05b92609d989df475d and stored to '/root/.monit.id'
 [  OK  ]
 [root@server ~]# chkconfig monit on

In Ubuntu/Debian:

sk@sk:~$ sudo /etc/init.d/monit start
 * Starting daemon monitor monit                                         [ OK ]

Access Monit web interface

In order to access monit web interface, Apache server should be installed and running.

Install httpd package to access the monit web console:

In CentOS/RHEL:

[root@server ~]# yum install httpd -y

Start httpd service:

[root@server ~]# service httpd start
[root@server ~]# chkconfig httpd on

In Ubuntu/Debian:

sk@sk:~$ sudo apt-get install apache2

Start httpd service:

sk@sk:~$ sudo /etc/init.d/apache2 start

Now navigate to http://localhost:2812/ from your browser. Enter the username as admin and password as monit.

Mozilla Firefox_001

Monit: sk - Mozilla Firefox_002

Add services to monit

Now add some services which you want to monitor in monit configuration file.

You can find some default entries for services in the monit configuration file under Services section. Just uncomment them to start monitor that service. Or add the required service entries at the bottom of the file.

For instance let us add apache service in Ubuntu/Debian systems. Open the file /etc/monit/monitrc and add the following lines at the bottom:

sk@sk:~$ sudo vi /etc/monit/monitrc
[...]
#Monitor Apache##
check process apache with pidfile /run/apache2.pid
start program = "/etc/init.d/apache2 start" with timeout 60 seconds
stop program  = "/etc/init.d/apache2 stop"

Once you added all the services, check the monit configuration file for any syntax error.

In CentOS/RHEL:

[root@server ~]# monit -t
Control file syntax OK

In Ubuntu/Debian:

sk@sk:~$ sudo monit -t

Now restart the monit service:

sk@sk:~$ sudo /etc/init.d/monit restart
 * Stopping daemon monitor monit                                         [ OK ] 
 * Starting daemon monitor monit                                         [ OK ]

Navigate to http://localhost:2812 from your browser and see the changes. The apache service entry will found there.

Monit: sk - Mozilla Firefox_003

Also Check log file to verify the services.

In CentOS/RHEL:

[root@server ~]# tail -f /var/log/monit 
[IST Jun  4 14:40:48] info     : monit HTTP server started
[IST Jun  4 14:40:48] info     : 'server.unixmen.com' Monit started
[IST Jun  4 14:41:00] error    : monit: Denied connection from non-authorized client [192.168.1.100]
[IST Jun  4 14:44:12] info     : Shutting down monit HTTP server
[IST Jun  4 14:44:12] info     : monit HTTP server stopped
[IST Jun  4 14:44:12] info     : monit daemon with pid [1091] killed
[IST Jun  4 14:44:12] info     : 'server.unixmen.com' Monit stopped
[IST Jun  4 14:44:13] info     : Starting monit HTTP server at [localhost:2812]
[IST Jun  4 14:44:13] info     : monit HTTP server started
[IST Jun  4 14:44:13] info     : 'server.unixmen.com' Monit started

In Ubuntu/Debian:

sk@sk:~$ sudo tail -f /var/log/monit.log 
[IST Jun  4 14:11:06] info     : monit HTTP server started
[IST Jun  4 14:11:06] info     : 'system_sk' Monit started
[IST Jun  4 15:04:56] info     : Shutting down monit HTTP server
[IST Jun  4 15:04:56] info     : monit HTTP server stopped
[IST Jun  4 15:04:56] info     : monit daemon with pid [1498] killed
[IST Jun  4 15:04:56] info     : 'system_sk' Monit stopped
[IST Jun  4 15:04:57] info     : Starting monit daemon with http interface at [localhost:2812]
[IST Jun  4 15:04:57] info     : Starting monit HTTP server at [localhost:2812]
[IST Jun  4 15:04:57] info     : monit HTTP server started
[IST Jun  4 15:04:57] info     : 'system_sk' Monit started

You can add more services such as ftp, ssh, nginx etc and monitor them periodically.

Refer the following monit official documentation page for details about adding services.

Testing Monit

Let us stop the apache service and see what will happen?

Stop the apache service using the following command:

sk@sk:~$ sudo /etc/init.d/apache2 stop
sk@sk:~$ sudo /etc/init.d/apache2 status
Apache2 is NOT running.

Open Monit web interface to see the changes.

Monit: sk - Mozilla Firefox_004

And refer the log file:

sk@sk:~$ sudo tail -f /var/log/monit.log 
[IST Jun  4 14:11:06] info     : monit HTTP server started
[IST Jun  4 14:11:06] info     : 'system_sk' Monit started
[IST Jun  4 15:04:56] info     : Shutting down monit HTTP server
[IST Jun  4 15:04:56] info     : monit HTTP server stopped
[IST Jun  4 15:04:56] info     : monit daemon with pid [1498] killed
[IST Jun  4 15:04:56] info     : 'system_sk' Monit stopped
[IST Jun  4 15:04:57] info     : Starting monit daemon with http interface at [localhost:2812]
[IST Jun  4 15:04:57] info     : Starting monit HTTP server at [localhost:2812]
[IST Jun  4 15:04:57] info     : monit HTTP server started
[IST Jun  4 15:04:57] info     : 'system_sk' Monit started
[IST Jun  4 15:16:57] error    : 'apache' process is not running

As you seen in the above result, the apache service is not running. Wait for two minutes and again refresh the monit web interface page. The Apache service will be automatically started after two minutes.

Monit: sk - Mozilla Firefox_005

See the log file:

sk@sk:~$ sudo tail -f /var/log/monit.log 
[IST Jun  4 15:04:56] info     : monit daemon with pid [1498] killed
[IST Jun  4 15:04:56] info     : 'system_sk' Monit stopped
[IST Jun  4 15:04:57] info     : Starting monit daemon with http interface at [localhost:2812]
[IST Jun  4 15:04:57] info     : Starting monit HTTP server at [localhost:2812]
[IST Jun  4 15:04:57] info     : monit HTTP server started
[IST Jun  4 15:04:57] info     : 'system_sk' Monit started
[IST Jun  4 15:16:57] error    : 'apache' process is not running
[IST Jun  4 15:16:57] info     : 'apache' trying to restart
[IST Jun  4 15:16:57] info     : 'apache' start: /etc/init.d/apache2
[IST Jun  4 15:18:57] info     : 'apache' process is running with pid 3602

That’s it.