How To Install Monit on CentOS 7 / RHEL 7

6

Monit is an opensource process monitoring tool for Linux operating system which 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.

It logs to its log file and notifies the user via customizable messages.

This guide will help you to install Monit on CentOS 7 / RHEL 7.

THIS DOCUMENT IS ALSO AVAILABLE FOR

Install Monit

Enable the EPEL repository to download the latest version of Monit.

yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Install the Monit using the YUM command.

yum -y install monit

Start monit by using the following command.

monit

Output:

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

Check the 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

Configure Monit

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

vi /etc/monitrc

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

set daemon  30

Alerts can be configured by.

set mailserver mx.itzgeek.local port 25

Alert templates can be found in the configuration file itself. See extras at the bottom of this article.
Logs setting can be changed by using the following file.

set log /var/log/monit

Enable Monit Web Interface

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.

Edit the monit configuration file.

vi /etc/monitrc

Look for httpd port 2812, modify the following entries.

FROM:

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
    #}

TO:

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.

Reload monit.

systemctl restart monit

Auto-start Monit on start-up.

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

Access Monit Web Interface

Open your web browser and go to the below URL.

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

OR

http://your.fq.dn:2812

Use the username and password mentioned in the previous step.

Install Monit on CentOS 7 - Monit Authentication
Install Monit on CentOS 7 – Monit Authentication

The Monit home page will look like below.

Install Monit on CentOS 7 - Monit DashBoard
Install Monit on CentOS 7 – Monit DashBoard

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.

You can place the service monitoring configuration files under /etc/monit.d/ directory.

Configure for SSH service.

vi /etc/monit.d/sshdmonitor

Use the below information.

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 22 protocol ssh then restart

Configure service for syslog.

vi /etc/monit.d/syslogmonitor

Use the below information.

check process syslogd with pidfile /var/run/syslogd.pid
start program = "/usr/bin/systemctl start rsyslog.service"
stop program = "/usr/bin/systemctl stop rsyslog.service"

Configuration service for HTTP.

vi /etc/monit.d/httpdmonitor

Use the below information.

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

Once configured, test the monit syntax

monit -t

Output:

Control file syntax OK

Reload it, to take effect of changes.

monit reload

Access the web interface. You should see the new services that we configured just now.

Install Monit on CentOS 7 - Monitoring with Services
Install Monit on CentOS 7 – Monitoring with Services

Clicking on the services will give you more information about the particular service. Here, you can perform actions like start, stop, and restart service, and also disable the monitoring.

Install Monit on CentOS 7 - Service Information
Install Monit on CentOS 7 – Service Information

Test Monitoring

Now stop the HTTP service for our testing.

systemctl stop httpd

Wait for 30 seconds, monit will start the syslog automatically. You can find it in the monit log.

cat /var/log/monit.log

Output:

[UTC Feb  9 05:18:00] error    : 'httpd' process is not running
[UTC Feb  9 05:18:00] info     : 'httpd' trying to restart
[UTC Feb  9 05:18:00] info     : 'httpd' start: '/usr/bin/systemctl start httpd.service'
[UTC Feb  9 05:18:31] info     : 'httpd' process is running with pid 1959

Mail Alerting with Monit

There are predefined alerting templates are available in Monit to alert system admins when the particular service fails.

Edit the configuration file.

vi /etc/monitrc

You can update the below alerting template as per your requirement.

set mail-format {
   from:    Monit <monit@$HOST>
   subject: monit alert --  $EVENT $SERVICE
   message: $EVENT Service $SERVICE
                 Date:        $DATE
                 Action:      $ACTION
                 Host:        $HOST
                 Description: $DESCRIPTION

            Your faithful employee,
            Monit @ ITzGeek
}

Set the recipient address here. Note: You will receive alerts on all type of actions

set alert root@localhost

If you like not to alert you on user-initiated service restart, then add the below configuration.

set alert root@localhost not on { instance, action }

Finally, set the mail server configuration so that you can receive mails. Here, I am using the local relay so that we can read mail alert over the terminal to verify the testing.

set mailserver localhost

Reload the service.

monit reload

Example Alert

The Monit has sent emails to the root user about the HTTP process.

cat /var/spool/mail/root

Mail:

From [email protected]  Sun Feb  9 05:22:48 2020
Return-Path: <[email protected]>
X-Original-To: root@localhost
Delivered-To: [email protected]
Received: from server.itzgeek.local (localhost [IPv6:::1])
        by server.itzgeek.local (Postfix) with ESMTP id 670E21082B34
        for <root@localhost>; Sun,  9 Feb 2020 05:22:48 +0000 (UTC)
From: "Monit" <[email protected]>
To: [email protected]
Subject: monit alert --  Does not exist httpd
Date: Sun, 09 Feb 2020 05:22:48 GMT
X-Mailer: Monit 5.25.1
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Message-Id: <[email protected]>

Does not exist Service httpd
                 Date:        Sun, 09 Feb 2020 05:22:48
                 Action:      restart
                 Host:        server.itzgeek.local
                 Description: process is not running

            Your faithful employee,
            Monit @ ITzGeek

From [email protected]  Sun Feb  9 05:23:18 2020
Return-Path: <[email protected]>
X-Original-To: root@localhost
Delivered-To: [email protected]
Received: from server.itzgeek.local (localhost [IPv6:::1])
        by server.itzgeek.local (Postfix) with ESMTP id B22AF1082B37
        for <root@localhost>; Sun,  9 Feb 2020 05:23:18 +0000 (UTC)
From: "Monit" <[email protected]>
To: [email protected]
Subject: monit alert --  Exists httpd
Date: Sun, 09 Feb 2020 05:23:18 GMT
X-Mailer: Monit 5.25.1
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Message-Id: <[email protected]>

Exists Service httpd
                 Date:        Sun, 09 Feb 2020 05:23:18
                 Action:      alert
                 Host:        server.itzgeek.local
                 Description: process is running with pid 2139

            Your faithful employee,
            Monit @ ITzGeek

OR

Use the Mutt (yum install -y mutt) email client.

Install Monit on CentOS 7 - HTTP Service not running
Install Monit on CentOS 7 – HTTP Service not running
Install Monit on CentOS 7 - HTTP Service started by Monit
Install Monit on CentOS 7 – HTTP Service started by Monit

Conclusion

That’s All. We have successfully configured Monit on CentOS 7 / RHEL 7.

You might also like