How to install Monit on Ubuntu 20.04

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 Ubuntu 20.04 .

Step 1: Install Apache

You can see how to install Apache here: How To Install Apache on Ubuntu 20.04

Step 2: Install and use Monit

Install Monit:

+ On Ubuntu/Debian/Linux Mint:

    $ apt install monit

+ On RedHat/CentOS/Fedora/ :

    $ yum install monit

Monit is very easy to configure, in fact, the configuration files are quite readable because it is quite close to the regular language.
By default, Monit monitors running services every 2 minutes and logs history to a log file located at: /var/log/monit. To fix the monit run cycle, you can edit this line in the config file /etc/monit/monitrc

set daemon 120                  # check services at 2-minute intervals

Monit has its web interface running on port 2812. To enable the web interface we need to change the monit configuration file.
The main configuration files for monit are located in /etc/monit.conf (RedHat / CentOS / Fedora) and /etc/monit/monitrccho (Ubuntu / Debian / Linux Mint). Open this file and change as follows:

  $ nano /etc/monit/monitrc

Find the line config httpd :

From :

To:

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

Access Monit Web Interface:

Open your web browser and go to the below URL.

http://your.ip address:2812

Use the username and password mentioned in the previous step

Service monitoring with monit

To monitor a service, we need to add config to /etc/monit/monitrc file or create a config file in /etc/monit/conf.d/ directory (by default this directory is included in monit’s default config)

Below is an example of the Apache monitor:

 $ nano /etc/monit/conf.d/apache
check process apache with pidfile /var/run/httpd.pid
  start program = "/etc/init.d/httpd start" #declare start service syntax
  stop program = "/etc/init.d/httpd stop" #declare stop service syntax
  if total cpu > 60% for 2 cycles then alert
  if total cpu > 80% for 5 cycles then restart

With this config Monit will:

  • Sends an alert if Apache uses more than 60% of CPU in two cycles.
  • Restart Apache if CPU usage is above 80% in five cycles.

(Each cycle is 2 minutes according to config set daemon 120 above)

Some commonly used commands

Check the Monit config syntax:

$ monit -t

Restart monit:

$ sudo /etc/init.d/monit restart

Check monit log:

$ tail -f /var/log/monit.log

Check monitor status:

$ monit status

Conclusion

Monit is quite a useful and powerful tool for monitoring and autocorrecting.
In the next post, I will guide you to configure sending email alerts with Monit as well as sending alerts through other Media channels.

                 Good Luck!