Install Monit monitoring system on CentOS 8

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

Monit is a small open source utility for managing and monitoring Unix systems. Monit performs automatic maintenance and repair, and can perform meaningful causal actions in case of errors.

Monit is designed as an autonomous system and does not rely on plugins or any special libraries to run. Instead, it can be used directly, and can take advantage of the existing infrastructure that already exists on the system. For example, Monit can be easily integrated with init, upstart or systemd, and can use existing run-level scripts to manage services.

System monitoring tools need to be non-invasive, and once installed, you should be able to forget them. That is, until sshd or postfix disappears on the hosting server. When this happens, it is good to know that you have installed this additional layer of security and protection, just wait a few seconds and Monit will restart the sshd daemon.Source: Monit homepage

You can use Monit for the following purposes:

Monit demonstrates its strength in the following ways:

  • Active monitoring: If an error situation occurs, Monit can take action, for example; if sendmail is not running, Monit can automatically start sendmail again or apache uses excessive resources (for example, if a DoS attack is in progress), Monit can stop or restart apache and send You send an alert message.
  • System monitoring: Monit can be used to monitor general system resources on the local host, such as overall CPU usage, memory and load average.
  • Process monitoring: You can use Monit to monitor daemons or similar programs running on localhost. Monit is particularly useful for monitoring daemons, such as daemons started from /etc/init/ when the system starts.
  • File, directory and file system monitoring: You can also use Monit to monitor files, directories and file systems on localhost. Monit can monitor changes to these items, such as timestamp changes, checksum changes or size changes. This is also useful for security reasons-you can monitor the md5 or sha1 checksums of files that should not be changed, and get alerts or perform actions that should be changed.
  • Program and script monitoring: Monit can be used to test a program or script at a specific time, just like cron, but in addition to that, you can also test the exit value of the program and perform operations, or send an alert when the exit value indicates an error.
  • Cloud and host: Monitor the network connection with various servers on the local host or remote host. Supports TCP, UDP and Unix domain sockets. Network testing can be performed at the protocol level; Monit has built-in tests for major Internet protocols (such as HTTP, SMTP, etc.).

System Requirements

  • Memory and disk space: At least 1 MB of RAM and about 500KB of free disk space are required. You may need more RAM, depending on the number of services Monit should monitor.
  • ANSI-C compiler and build system: You will need to install the ANSI-C99 compiler to build Monit. It is recommended to use the GNU C Compiler (GCC) of the Free Software Foundation (FSF). In addition, your PATH must include basic build tools such as make.

Install Monit

I hope that we have enough understanding of this tool so that we can continue to set it up in the server so that we can see for ourselves the benefits of working behind the scenes once we start. We will get the code and install it through epel-repository.

Step 1: Update and install necessary software packages

Before enjoying the fun of Monit, we will ensure that all dependencies are satisfied by installing the following important software packages.

sudo dnf -y update
sudo dnf -y install zlib-devel pam-devel openssl-devel libtool bison flex autoconf gcc make git epel-release

Step 2: Install Monit

After solving the dependencies and applications we need, let’s make the most of the repository by extracting the repository and installing it through DNF.

sudo dnf -y install monit

Start monitoring

Starting Monit is as simple as running a command. As follows.

$ sudo monit

###You should see the message below in the end###
 New Monit id: a447ea6daa8857bcf3c5089d0d225e83
 Stored in '/root/.monit.id'
Starting Monit 5.26.0 daemon with http interface at [localhost]:2812

Find the status of Monit

After starting Monit, you can run the status command to understand the running status, as shown below:

$ sudo monit status
Monit 5.26.0 uptime: 1m

System 'master'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.00] [0.00] [0.00]
  cpu                          0.0%us 0.0%sy 0.0%wa
  memory usage                 306.4 MB [17.4%]    
  swap usage                   0 B [0.0%]
  uptime                       1h 59m
  boot time                    Wed, 30 Sep 2020 17:53:25
  data collected               Wed, 30 Sep 2020 19:51:59

The attached screenshot is shared below to make the picture clearer.

You can also make Monit start when the server restarts

$ sudo systemctl enable --now monit
$ sudo systemctl status monit
● monit.service - Pro-active monitoring utility for unix systems
   Loaded: loaded (/usr/lib/systemd/system/monit.service; enabled; vendor preset: disabled)        
   Active: active (running) since Wed 2020-09-30 20:27:05 UTC; 2min 4s ago
 Main PID: 83433 (monit)
    Tasks: 2 (limit: 11070)
   Memory: 960.0K
   CGroup: /system.slice/monit.service
           └─83433 /usr/bin/monit -I

Step 3: Monit configuration

As you might expect, Monit comes with a configuration that allows you to fine-tune the configuration and add what we need to monitor. Monit will use the monitrc control file in the /etc/monitrc directory for configuration. The file has been set to start Monit’s http server, so you can learn something interesting from the comfort of your browser. Look for that part in the code snippet below and change the IP address from localhost to 0.0.0.0, which will allow you to access the web interface from all IP addresses that can access the server.

sudo vim /etc/monitrc

set httpd port 2812
    #use address localhost => only accept connection from localhost (drop if you use M/Monit)
    use address 0.0.0.0     
    allow 0.0.0.0/0       
    allow admin:monit

Don’t forget to open port 2812 after finishing editing. Also restart the monitoring service.

sudo firewall-cmd --permanent --add-port=2812/tcp
sudo firewall-cmd --reload
sudo systemctl restart monit

After starting monit, point your browser to http://[IP-or-FQDN]: 2812, and log in with the username “admin” and password “monit” when prompted. The following page will appear.

Install Monit monitoring system on CentOS 8

Log in for the first time

Install Monit monitoring system on CentOS 8

Step 4: Add services to be monitored for Monit

Once Monit is running, it will be of no benefit to us if we do not add the services we are interested in so that they can monitor and restart in the event of failure. We will use Nginx Server for testing. So let’s install it and then add the configuration that will monitor its status.

sudo dnf install -y nginx
sudo systemctl start nginx

Nginx can now be monitored through our lovely monitoring Monit. In order to enable this function, we will add the following configuration to the /etc/monit.d directory, which can save the configuration files of various services being monitored.

$ sudo vim /etc/monit.d/nginx-monitor
check process nginx with pidfile /run/nginx.pid
start program "/usr/bin/systemctl start nginx.service"
stop program "/usr/bin/systemctl stop nginx.service"
if failed port 80 protocol http then restart

If you want to check whether the syntax in the configuration file is correct, Monit will provide you with this function. Just run the following command:

$ sudo monit -t
Control file syntax OK

As you might have guessed, we must reload Monit to adapt to the new configuration. this is very simple:

$ sudo monit reload
Reinitializing monit daemon

Then we can check its status

sudo monit status

The screenshot below shows that the new service (nginx) we just added has been registered and its status is currently “Ok”.

Install Monit monitoring system on CentOS 8

The web interface can also show you what is being monitored in it

Install Monit monitoring system on CentOS 8

Other services (such as sshd, httpd, syslogd, postfix, etc.) can be added to their own files in the /etc/monit.d/ directory in the same way.

Step 5: Test Monit

To confirm that Monit is working properly, we will intentionally stop the Nginx server, and then wait for it to try and restart it successfully. By default, Monit checks the service every 30 seconds. You can change this value in the configuration file (/etc/monitrc) to suit your requirements. Find the “set daemon 30” line in the file and change the number (in seconds) to more or less.

So let’s stop Nginx

sudo systemctl stop nginx

Then check the Monit log to observe its effect.

$ sudo tail -f /var/log/messages

Sep 30 21:12:55 master systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Sep 30 21:12:55 master systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Sep 30 21:13:24 master monit[85789]: 'nginx' process is not running
Sep 30 21:13:24 master monit[85789]: 'nginx' trying to restart
Sep 30 21:13:24 master monit[85789]: 'nginx' start: '/usr/bin/systemctl start nginx.service'
Sep 30 21:13:24 master systemd[1]: Starting The nginx HTTP and reverse proxy server...
Sep 30 21:13:24 master nginx[85809]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Sep 30 21:13:24 master nginx[85809]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Sep 30 21:13:24 master systemd[1]: Started The nginx HTTP and reverse proxy server.
Sep 30 21:13:54 master monit[85789]: 'nginx' process is running with pid 85812

Confirm that Nginx is started

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-09-30 21:13:24 UTC; 2min 42s ago
  Process: 85811 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 85809 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 85807 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 85812 (nginx)
    Tasks: 3 (limit: 11070)
   Memory: 5.3M
   CGroup: /system.slice/nginx.service
           ├─85812 nginx: master process /usr/sbin/nginx
           ├─85813 nginx: worker process
           └─85814 nginx: worker process

Monit proved to be working well by restarting Nginx within the configured time. This can be very useful, especially if the service keeps shutting down for no clear reason. Monit will fully support you at this time. You can configure more features so that you will find them when you open the Monit package.You can find more information on the Monit website

Final words

As the developers of Monit said, it has all the functions needed for system monitoring and error recovery. It’s like using a watchdog with a toolbox on the server. To reduce some burden, you can drink a cup of tea or take a nap on weekends. Use its advantages to transform your management tasks. We have finally reached the end, and hope this guide will help you.

You can also enjoy:

Install Sensu Go monitoring tool on CentOS 8 / RHEL 8

10 best open source Linux monitoring tools

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

Sidebar