How to Install Monit on CentOS 7 / RHEL 7 – A Process Monitoring Tool

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 set up monit on CentOS / RHEL.

Install Monit on CentOS 7

Configure EPEL repo to download the latest version of Monit.

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Install the Monit using the YUM command.

 yum -y install monit

Output:

Loaded plugins: fastestmirror
base                                                                                | 3.6 kB  00:00:00
epel/x86_64/metalink                                                                | 7.8 kB  00:00:00
epel                                                                                | 4.7 kB  00:00:00
extras                                                                              | 3.4 kB  00:00:00
updates                                                                             | 3.4 kB  00:00:00
(1/5): extras/7/x86_64/primary_db                                                   | 166 kB  00:00:01
(2/5): epel/x86_64/group_gz                                                         | 266 kB  00:00:04
(3/5): updates/7/x86_64/primary_db                                                  | 6.0 MB  00:00:05
(4/5): epel/x86_64/updateinfo                                                       | 882 kB  00:00:05
(5/5): epel/x86_64/primary_db                                                       | 6.2 MB  00:01:38
Determining fastest mirrors
 * base: ftp.iitm.ac.in
 * epel: mirror.rise.ph
 * extras: ftp.iitm.ac.in
 * updates: ftp.iitm.ac.in
Resolving Dependencies
--> Running transaction check
---> Package monit.x86_64 0:5.14-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================
 Package                Arch                    Version                        Repository             Size
===========================================================================================================
Installing:
 monit                  x86_64                  5.14-1.el7                     epel                  269 k

Transaction Summary
===========================================================================================================
Install  1 Package

Total download size: 269 k
Installed size: 689 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/monit-5.14-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for monit-5.14-1.el7.x86_64.rpm is not installed
monit-5.14-1.el7.x86_64.rpm                                                         | 269 kB  00:00:01
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : monit-5.14-1.el7.x86_64                                                                 1/1
  Verifying  : monit-5.14-1.el7.x86_64                                                                 1/1

Installed:
  monit.x86_64 0:5.14-1.el7

Complete!
</epel@fedoraproject.org>

Start monit by using the following command.

monit

Output:

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

Check the monit status.

monit status

Output:

The Monit daemon 5.14 uptime: 0m

System 'server.itzgeek.local'
  status                            Running
  monitoring status                 Monitored
  load average                      [0.09] [0.23] [0.14]
  cpu                               0.0%us 0.0%sy 0.0%wa
  memory usage                      129.5 MB [13.0%]
  swap usage                        0 B [0.0%]
  data collected                    Mon, 19 Feb 2018 06:59:55

Configure Monit

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

vi /etc/monitrc

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

set daemon  60

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 logfile syslog

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.

Open monit configuration file /etc/monitrc.

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'
allow @monit           # allow users of group 'monit' to connect (rw)
allow @users readonly  # allow users of group 'users' to connect readonly

TO:

set httpd port 2812
allow 0.0.0.0/0.0.0.0
allow admin:monit

From the above settings, monit will listen on 2812; 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

Firewall

Configure the firewall to allow access to 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

Monit home page will look like this:

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 other services that you want to monitor. You can place the 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 - Monit DashBoard with Services
Install Monit on CentOS 7 – Monit DashBoard 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 the 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:

[EST Feb 19 07:41:13] error : 'httpd' process is not running
[EST Feb 19 07:41:13] info : 'httpd' trying to restart
[EST Feb 19 07:41:13] info : 'httpd' start: /usr/bin/systemctl
[EST Feb 19 07:41:43] info : 'httpd' process is running with pid 1356

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@$HOST
  subject: IMPORTANT!!!! monit alert -- $EVENT $SERVICE
  message: $EVENT Service $SERVICE
                 Date:        $DATE
                 Action:      $ACTION
                 Host:        $HOST
                 Description: $DESCRIPTION

            Your faithful employee,
            Monit Team
}

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

set alert root@localhost

If you like to not to alert 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 mails to the root user about the HTTP process.

cat /var/spool/mail/root

Mail:

From monit@server.itzgeek.local  Mon Feb 19 08:00:15 2018
Return-Path: <monit@server.itzgeek.local>
X-Original-To: root@localhost
Delivered-To: root@localhost.itzgeek.local
Received: from server.itzgeek.local (localhost [IPv6:::1])
        by server.itzgeek.local (Postfix) with SMTP id DAA0B400E522
        for <root@localhost>; Mon, 19 Feb 2018 08:00:15 -0500 (EST)
From: monit@server.itzgeek.local
To: root@localhost.itzgeek.local
Subject: </root@localhost></monit@server.itzgeek.local>IMPORTANT!!!! monit alert --  Does not exist httpd
Date: Mon, 19 Feb 2018 13:00:15 GMT
X-Mailer: Monit 5.14
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
Message-Id: <1519045215.1681379615@server.itzgeek.local>
Status: RO
Content-Length: 287
Lines: 8

Does not exist Service httpd
                 Date:        Mon, 19 Feb 2018 08:00:15
                 Action:      restart
                 Host:        server.itzgeek.local
                 Description: process is not running

            Your faithful employee,
            Monit Team

From monit@server.itzgeek.local  Mon Feb 19 08:00:46 2018
Return-Path: <monit@server.itzgeek.local>
X-Original-To: root@localhost
Delivered-To: root@localhost.itzgeek.local
Received: from server.itzgeek.local (localhost [IPv6:::1])
        by server.itzgeek.local (Postfix) with SMTP id 44E6C409903B
        for <root@localhost>; Mon, 19 Feb 2018 08:00:46 -0500 (EST)
From: monit@server.itzgeek.local
To: root@localhost.itzgeek.local
Subject: </root@localhost></monit@server.itzgeek.local>IMPORTANT!!!! monit alert --  Exists httpd
Date: Mon, 19 Feb 2018 13:00:46 GMT
X-Mailer: Monit 5.14
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
Message-Id: <1519045246.908363165@server.itzgeek.local>
Status: RO
Content-Length: 287
Lines: 8

Exists Service httpd
                 Date:        Mon, 19 Feb 2018 08:00:46
                 Action:      alert
                 Host:        server.itzgeek.local
                 Description: process is running with pid 1491

            Your faithful employee,
            Monit Team

OR

Use 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

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

Further Reading

You might also like