We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Install Monit on CentOS 8/Rocky 8/Alma 8/RHEL 8

Learn how to – Install Monit on CentOS 8/Rocky 8/Alma 8/RHEL 8.

Monit is a small Open Source utility for managing and monitoring Unix systems. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.

Monit is designed as an autonomous system and does not depend on plugins nor any special libraries to run. Instead it works right out of the box and can utilize existing infrastructure already on your system. For instance, Monit will easily integrate with init, upstart or systemd and can use existing run-level scripts to manage services.

A system monitoring tool needs to be non-intrusive and you should be able to forget about it once it’s installed. That is, until sshd or postfix dies on your hosted server. When this happens, it is good to know that you have installed this extra layer of security and protection, where you just wait a few seconds and Monit will restart the sshd daemon. Source: Monit Main Page

What you can use Monit for:

Monit gives a display of its prowess in the following ways:

  • Proactive monitoring: Monit can act if an error situation should occur, e.g.; if sendmail is not running, Monit can start sendmail again automatically or if apache is using too much resources (e.g. if a DoS attack is in progress) Monit can stop or restart apache and send you an alert message.
  • System monitoring: Monit can be used to monitor general system resources on localhost such as overall CPU usage, Memory and Load Average.
  • Processes monitoring: You can use Monit to monitor daemon processes or similar programs running on localhost. Monit is particularly useful for monitoring daemon processes, such as those started at system boot time from /etc/init/
  • Files, Dirs and Filesystems monitoring: You can also use Monit to monitor files, directories and filesystems on localhost. Monit can monitor these items for changes, such as timestamps changes, checksum changes or size changes. This is also useful for security reasons – you can monitor the md5 or sha1 checksum of files that should not change and get an alert or perform an action if they should change.
  • Programs and scripts monitoring: Monit can be used to test programs or scripts at certain times, much like cron, but in addition, you can test the exit value of a program and perform an action or send an alert if the exit value indicates an error.
  • Cloud and Hosts: Monitor network connections to various servers, either on localhost or on remote hosts. TCP, UDP and Unix Domain Sockets are supported. Network tests can be performed on a protocol level; Monit has built-in tests for the main Internet protocols, such as HTTP, SMTP etc.

System Requirements:

  • Memory and Disk space: A minimum of 1 megabytes RAM are required and around 500KB of free disk space. You may need more RAM depending on how many services Monit should monitor.
  • ANSI-C Compiler and Build System: You will need an ANSI-C99 compiler installed to build Monit. The GNU C compiler (GCC) from the Free Software Foundation (FSF) is recommended. In addition, your PATH must contain basic build tools such as make.

Install Monit on CentOS 8/Rocky 8/Alma 8/RHEL 8

Hoping the rendition about this tool suffices, we can go ahead and set it up in our server so that we can see first hand the benefits that accrues once it starts working behind the scenes. We shall fetch the code and install it via epel-repository.

Step 1: Update and install necessary packages

Before we enjoy the delight of Monit, we shall make sure all dependencies have been met by installing the following important 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 on CentOS 8/Rocky 8/Alma 8/RHEL 8

After the dependencies and applications we need have been settled in, let us put our repositories to good use by fetching it and installing it via DNF.

sudo dnf -y install monit

Start Monit

Starting Monit is as simple as running one command. This is illustrated below.

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

Find Monit’s status

After firing up Monit, you can find out how it is faring by running the status command as follows:

$ sudo monit status
Monit 5.30.0 uptime: 4m

System 'rocky8.mylab.io'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.02] [0.04] [0.00]
  cpu                          0.7%usr 0.6%sys 0.0%nice 0.0%iowait 0.2%hardirq 0.1%softirq 0.0%steal 0.0%guest 0.0%guestnice
  memory usage                 969.3 MB [12.5%]
  swap usage                   0 B [0.0%]
  uptime                       2h 0m
  boot time                    Fri, 14 Oct 2022 11:42:48
  filedescriptors              2592 [0.3% of 788794 limit]
  data collected               Fri, 14 Oct 2022 13:43:06

An accompanying screenshot is shared below for a clearer picture.

You can as well enable Monit to start when the server restarts

$ sudo systemctl enable enable monit 
$ sudo systemctl restart monit  && 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 Fri 2022-10-14 13:46:33 EAT; 2min 4s ago
 Main PID: 83433 (monit)
    Tasks: 2 (limit: 11070)
   Memory: 960.0K
   CGroup: /system.slice/monit.service
           └─83433 /usr/bin/monit -I
Oct 14 13:46:33 rocky8.mylab.io systemd[1]: Started Pro-active monitoring utility for unix systems.

Step 3: Monit configuration

As you would expect, Monit comes with a configuration that enables you to fine tune your configuration and to also add what we need to monitor. Monit will use the monitrc control file located in /etc/monitrc directory for it’s configuration. The file is setup to start Monit’s http server so you have something interesting to look at from the comfort of your browser. Look for the section in the snippet below and change the IP from localhost to 0.0.0.0 which will allow you to access the web interface from all IPs that can reach your 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

Do not forget to open port 2812 once you are done editing. Restart monit service as well.

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

After you have fired up monit, point your browser to http://[IP-or-FQDN]:2812 and log in with the username “admin” and password “monit” when prompted. Pages like below will ensue.

First time log in

Step 4: Add services for Monit to monitor

Once we have Monit running, it will do us no good if we do not add the services we are interested in for it to monitor and restart whenever they fail. We shall test with Nginx Server. So let us install it then add configuration that will monitor its status.

sudo dnf install -y nginx
sudo systemctl start nginx

Nginx is now ready to be monitored by our cute monitoring Monit. To enable that functionality, we shall add the following configuration in /etc/monit.d directory that can hold configuration files for 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 would like to check that the Syntax in your configuration file is okay, Monit provides that ability for you. Just run the command below:

$ sudo monit -t
Control file syntax OK

As you might have guessed, we have to reload Monit for it to accommodate the new configuration. That is as simple as:

$ sudo monit reload
Reinitializing monit daemon

Then we can check its status

sudo monit status

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

The web interface can also show you what is being monitored therein

Other services such as sshd, httpd, syslogd, postfix and much more can be added in the same fashion in their own files within /etc/monit.d/ directory.

Step 5: Putting Monit to the test

To confirm that Monit works, we shall intentionally stop Nginx server then wait to see if it will attempt and succeed in restarting it. Monit checks services at 30 seconds intervals by default. You can change this value in the configuration file (/etc/monitrc) to befit your requirements. Look for the line “set daemon 30” in the file and change the number (in seconds) to more or less.

So let us stop Nginx

sudo systemctl stop nginx

And then check Monit logs to observe what it does.

$ 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

Confirming 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

And Monit proved to be working pretty well by restarting Nginx within the time it was configured. This can be very helpful especially in scenarios where a service keeps going off for no clear reason. Monit will fully have your back on such times. You can configure much more features that you will find as you unpack Monit. You can find out much more from Monit website

Last Words

As the developers of Monit say, with all features needed for system monitoring and error recovery. It’s like having a watchdog with a toolbox on your server. Offload some of the burden to monit and enjoy a cup of tea or a peaceful nap over the weekend. Leverage on its advantages and transform your administrative tasks. We have finally reached the end and we hope the guide was helpful to you.

You can also enjoy:

Cart

Your Cart is Empty

Back To Shop