How To Install Monit on CentOS 8 / RHEL 8

Monit is an open-source process monitoring tool for Linux operating system which helps you to monitor the system process. Whenever the service/process goes down, it automatically does the maintenance and repair of the particular process and ensures it is brought back online.

Monit can also be used for managing and monitoring of programs, files, directories, filesystems, 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 8 / RHEL 8.

Install Monit

Install necessary build tools as the Monit is yet to be packaged as rpm for CentOS 8 / RHEL 8.

dnf install -y git gcc glibc make glibc-devel kernel-headers autoconf automake libtool bison flex libzip-devel pam-devel openssl openssl-devel

Clone the Monit source code for compiling.

git clone https://bitbucket.org/tildeslash/monit.git

Compile the Monit with series of following commands.

cd monit

./bootstrap

./configure

make

make install

Configure Monit

Functionality

Copy the monitrc file from the downloaded source to /etc directory.

cp monitrc /etc/

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 interval can be modified by changing the below line.

set daemon 30

You can set an email server to receive alerts in your inbox.

set mailserver mx.itzgeek.local port 25
Email alerting can be customized using built-in templates found in the configuration file itself. See mail alerting with Monit at the bottom of this article for more information.

Logs setting can be changed by using the following file.

set log /var/log/monit.log

Ensure to uncomment the below line. We will place service monitoring files here.

include /etc/monit.d/*

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’s 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'
    #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 (drop if you use M/Monit)
    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 port 2812 on all interfaces. The admin user will able to access the web interface from any network.

Systemd Setup

We will now create a systemd file to auto start Monit on system startup.

vi /lib/systemd/system/monit.service

Use the below settings.

 # This file is systemd template for monit service. To
 # register monit with systemd, place the monit.service file
 # to the /lib/systemd/system/ directory and then start it
 # using systemctl (see below).
 #
 # Enable monit to start on boot:
 #         systemctl enable monit.service
 #
 # Start monit immediately:
 #         systemctl start monit.service
 #
 # Stop monit:
 #         systemctl stop monit.service
 #
 # Status:
 #         systemctl status monit.service

 [Unit]
 Description=Pro-active monitoring utility for unix systems
 After=network.target
 Documentation=man:monit(1) https://mmonit.com/wiki/Monit/HowTo

 [Service]
 Type=simple
 KillMode=process
 ExecStart=/usr/local/bin/monit -I
 ExecStop=/usr/local/bin/monit quit
 ExecReload=/usr/local/bin/monit reload
 Restart = on-abnormal
 StandardOutput=null

 [Install]
 WantedBy=multi-user.target                        

Reload the systemd daemon.

systemctl daemon-reload

Start the Monit service.

systemctl start monit

Auto-start Monit on start-up.

systemctl enable monit

Firewall

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 8 – Login to Monit

Monit home page will look like this:

Install Monit on CentOS 8 – 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.

mkdir /etc/monit.d/

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

Configure service for syslog.

vi /etc/monit.d/syslogmonitor

Use the information below.

check process SysLog with pidfile /var/run/rsyslogd.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 information below.

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 Monit to take effect of changes.

systemctl reload monit

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

Install Monit on CentOS 8 – List of Services

Clicking on a service 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 8 – 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 Jan 27 20:37:03] error    : 'HTTPD' process is not running
[EST Jan 27 20:37:03] info     : 'HTTPD' trying to restart
[EST Jan 27 20:37:03] info     : 'HTTPD' start: '/usr/bin/systemctl start httpd.service'
[EST Jan 27 20:37:38] info     : 'HTTPD' process is running with pid 25270

Mail Alerting with Monit

There are predefined alerting templates available in Monit to alert system administrators when the particular service fails. For this demo, we will use the local relay (email server) to alert the root user.

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
}

Set the recipient address here to receive alerts on all type of actions

set alert root@localhost

Set the recipient address here to not to alert on user-initiated service restarts.

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

Finally, set the mail server configuration so that you can receive mails.

set mailserver localhost

Here, I am using the local relay dnf install -y sendmail && systemctl start sendmail so that we can read email alerts over the terminal to confirm the alerting mechanism.

Reload the service.

systemctl reload monit

View Alert

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

cat /var/spool/mail/root

Output:

To: root@centos8.itzgeek.local
Subject: monit alert --  Does not exist HTTPD
Date: Tue, 28 Jan 2020 01:44:05 GMT
X-Mailer: Monit 5.27.0
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Message-Id: <1580175845.eb39323a1e0f7245@centos8.itzgeek.local>

Does not exist Service HTTPD
                Date:        Mon, 27 Jan 2020 20:44:05
                Action:      restart
                Host:        centos8.itzgeek.local
                Description: process is not running

           Your faithful employee,
           Monit @ ITzGeek

From monit@centos8.itzgeek.local  Mon Jan 27 20:44:47 2020
Return-Path: <monit@centos8.itzgeek.local>
Received: from centos8.itzgeek.local (localhost [127.0.0.1])
        by centos8.itzgeek.local (8.15.2/8.15.2) with ESMTP id 00S1ilKL026285
        for <root@localhost>; Mon, 27 Jan 2020 20:44:47 -0500
From: "Monit" <monit@centos8.itzgeek.local>
To: root@centos8.itzgeek.local
Subject: monit alert --  Exists HTTPD
Date: Tue, 28 Jan 2020 01:44:47 GMT
X-Mailer: Monit 5.27.0
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Message-Id: <1580175887.6d2991903dbcbafe@centos8.itzgeek.local>

Exists Service HTTPD
                Date:        Mon, 27 Jan 2020 20:44:47
                Action:      alert
                Host:        centos8.itzgeek.local
                Description: process is running with pid 26069

           Your faithful employee,
           Monit @ ITzGeek

OR

Use mutt dnf install -y mutt, an email client to view alerts.

Email Alerting HTTP Service Not Running
Email Alerting HTTP Service Started by Monit

Conclusion

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

centos 8monitoring-toolsrhel 8
Comments (1)
Add Comment
  • ExoRank.com

    Awesome post! Keep up the great work! 🙂