How to Configure Monit

Monit is a free opensource supervision tool for Unix and Linux operation systems. It helps you to monitor system process using the web browser and also whenever requires it automatically do the maintenance or repair of the particular process in such a way that it can be brought back online.

Monit can also use for managing and monitoring of programs, files, directories, and devices for timestamps changes, checksum changes, or size changes; not limited to perform various TCP/IP network checks, protocol checks, and can utilize SSL for such checks.

This tutorial will guide you to install & configure monit.

NOTE: The following steps are performed on a Centos7 server.

STEP 1. Install Monit

sudo yum install monit

Start monit by using the following command:

monit 

The output will look like this

Starting Monit 5.25.1 daemon with http interface at [localhost]:2812

Now, check monit status

monit status 

Output:

Monit 5.25.1 uptime: 0m

System 'server.itzgeek.local'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.01] [0.05] [0.04]
  cpu                          0.0%us 0.0%sy 0.0%wa
  memory usage                 170.2 MB [29.0%]
  swap usage                   0 B [0.0%]
  uptime                       4m
  boot time                    Sun, 09 Feb 2020 04:55:42
  data collected               Sun, 09 Feb 2020 05:00:00
COPY

STEP 2. Configure and Enable monit web interface

The main configuration file of Monit is /etc/monitrc. We will make a few changes to the Monit configuration file according to our requirements

sudo vim /etc/monitrc

By default, monit is set to check the services at an interval of 30 seconds. This setting can be modified by changing the below line.

set daemon 30 

Monit also provides a web interface for monitoring and managing the configured services. By default, monit listens on 2812 port, but it needs to be set up.

In the same config file i.e /etc/monitrc, Look for httpd port 2812, modify the following entries.

DEFAULT CONFIG:


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'
    #with ssl {            # enable SSL/TLS and set path to server certificate
    #    pemfile: /etc/ssl/certs/monit.pem
    #}

MODIFIED CONFIG:

set httpd port 2812 and
    use address 0.0.0.0  # only accept connection from localhost
    allow 0.0.0.0/0        # allow localhost to connect to the server and
    allow admin:monit      # require user 'admin' with password 'monit'
    #with ssl {            # enable SSL/TLS and set path to server certificate
    #    pemfile: /etc/ssl/certs/monit.pem
    #}

From the above settings, monit will listen on 2812 and the admin user will able to access the web interface from any network.

Now, Restart monit service & enable it to start on boot:

systemctl restart monit
systemctl enable monit 

Configure the firewall to allow access to the Monit web interface, running on port 2812.

firewall-cmd --permanent --add-port=2812/tcp
firewall-cmd --reload

Test the web interface access:

Open your web browser and go to the below URL.

http://your.ip.addr.ess:2812

It will prompt for a username & password, supply the same username & password mentioned in above monit config file.

STEP 3. Configure Services for monitoring with monit

Once the web interface is up, we can start to set up the services that we want to monitor with monit.

Service monitoring configuration files can be placed under /etc/monit.d/ directory.

vim /etc/monit.d/sshdmonitor

This will monitor ssh service, if the ssh service goes down it will try to restart the service 3 time then it will time out

check process sshd with pidfile /var/run/sshd.pid
start program = "/usr/bin/systemctl start sshd.service"
stop program = "/usr/bin/systemctl stop sshd.service"
if failed port 2200 protocol ssh then restart
if 3 restarts within 3 cycles then timeout

Similary, we can configure it for other services also :-

Configuration for HTTP monitoring

vim /etc/monit.d/httpdmonitor

check process httpd with pidfile /var/run/httpd/httpd.pid
start program "/usr/bin/systemctl start httpd.service"
stop program "/usr/bin/systemctl stop httpd.service"
if failed port 80 protocol http then restart
if 3 restarts within 3 cycles then timeout

Configuration for MySQL monitoring

sudo vim /ect/monit.d/mysqlmontor

check process httpd with pidfile /var/run/httpd/httpd.pid
start program "/usr/bin/systemctl start httpd.service"
stop program "/usr/bin/systemctl stop httpd.service"
if failed port 80 protocol http then restart
if 3 restarts within 3 cycles then timeout

Configuration for Nrpe monitoring

check process nrpe with pidfile /var/run/nrpe/nrpe.pid
start program = "/usr/bin/systemctl start nrpe"
stop program = "/usr/bin/systemctl stop nrpe"
if 3 restarts within 3 cycles then timeout

Once, you have configured all services, test the syntax before restarting monit

monit -t 

Now, Reload monit to take effect of chnages:

monit reload 

Final step, Access the web interface. You should see the new services that we have configured above.

Leave a Reply

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