Monit - A Tool for Managing and Monitoring Linux Systems

Channel: Monitoring Tools Linux
Abstract: # dnf install monit [On Fedora Linux]you can type the following command to start the monit service. # systemctl monit restart

Monit is a free open source and very useful tool that automatically monitors and manages processes, files, directories, checksums, permissions, filesystems, and services like Apache, Nginx, MySQL, FTP, SSH, SMTP, and so on in a UNIX/Linux based systems and provides an excellent and helpful monitoring functionality to system administrators.

The monit has a user-friendly web interface where you can directly view the system status and setup up processes using a native HTTP(S) web server or via the command line interface. This means you must have web server like Apache or Nginx installed on your system to access and view the monit web interface.

[ You might also like: 20 Command Line Tools to Monitor Linux Performance ]

What Monit Can Do

Monit has the ability to start a process if it is not running, restart a process if not responding, and stop a process if uses high resources. Additionally, you can also use Monit to monitor files, directories, and filesystems for changes, checksum changes, file size changes, or timestamp changes.

With Monit, you can able to monitor remote hosts’ TCP/IP port, server protocols, and ping. Monit keeps its own log file and alerts about any critical error conditions and recovery status.

This article is written to describe a simple guide on Monit installation and configuration on RHEL-based and Debian-based Linux distributions.

Step 1: Installing Monit in Linux

By default, the Monit monitoring program is not available from the default system base repositories, you need to add and enable a third-party epel repository to install the monit package under RHEL-based distributions such as CentOS, Rocky Linux, and AlmaLinux.

--------- On RHEL 9 based Systems --------- 
# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm 

--------- On RHEL 8 based Systems --------- 
# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

--------- On RHEL 7 based Systems ---------
# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Once you’ve added the epel repository, install the Monit package by running the following yum command.

# yum install monit
OR
# dnf install monit  [On Fedora Linux]
Install Monit in RHEL

For Ubuntu/Debian/Linux Mint user’s can easily install using the apt command as shown.

$ sudo apt install monit
Step 2: Configuring Monit in Linux

Monit is very easy to configure, in fact, the configuration files are created to be very easily readable and making them easier for users to understand. It is designed to monitor the running services every 2 minutes and keeps the logs in 「/var/log/monit「.

Monit has a web interface that runs on port 2812 using a web server. To enable the web interface you need to make changes in the monit configuration file.

The main configuration file of monit located at /etc/monit.conf under (RedHat/CentOS/Fedora) and /etc/monit/monitrc file for (Ubuntu/Debian/Linux Mint).

Open this file using your choice of editor.

# vi /etc/monitrc
Or
$ sudo nano /etc/monit/monitrc

Next, uncomment the following section and add the IP address or domain name of your server, allow anyone to connect and change the monit user and password or you can use default ones.

set httpd port 2812 and
     use address 0.0.0.0  # only accept connections from localhost
     allow 0.0.0.0/0        # 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
Configure Monit in Linux

Once you’ve configured it, you need to start, enable and verify the monit service to reload the new configuration settings.

# systemctl start monit
# systemctl enable monit
# systemctl status monit
Start Monit in RHEL

Now, you will be able to access the monit web interface by navigating to the following URLs.

http://localhost:2812
OR
http://ip-address:2812
Or
http://example.com:2812

Then enter the user name as 「admin」 and password as 「monit「. You should get a screen similar to the one below.

Monit Login Monit Service Manager Monit System Status Step 3: Adding Linux Services to Monit Monitoring

Once the monit web interfaces are correctly set up, start adding the programs that you want to monitor into the /etc/monitrc under (RedHat/CentOS/Fedora) and /etc/monit/monitrc file for (Ubuntu/Debian/Linux Mint) at the bottom.

Following are some useful configuration examples for monit, which can be very helpful to see how a service is running, where it keeps its profile, how to start and stop a service, etc.

Monitor Apache in Monit
check process httpd with pidfile /var/run/httpd.pid
group apache
start program = "/usr/bin/systemctl httpd start"
stop program = "/usr/bin/systemctl httpd stop"
if failed host 127.0.0.1 port 80
protocol http then restart
if 5 restarts within 5 cycles then timeout
Monitor Apache2 in Monit
check process apache with pidfile /run/apache2.pid
start program = "/usr/bin/systemctl apache2 start" with timeout 60 seconds
stop program  = "/usr/bin/systemctl apache2 stop"
Monitor Nginx in Monit
check process nginx with pidfile /var/run/nginx.pid
start program = "/usr/bin/systemctl nginx start"
stop program = "/usr/bin/systemctl nginx stop"
Monitor MySQL in Monit
check process mysqld with pidfile /var/run/mysqld/mysqld.pid
group mysql
start program = "/usr/bin/systemctl mysqld start"
stop program = "/usr/bin/systemctl mysqld stop"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout
Monitor SSH in Monit
check process sshd with pidfile /var/run/sshd.pid
start program "/usr/bin/systemctl sshd start"
stop program "/usr/bin/systemctl sshd stop"
if failed host 127.0.0.1 port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

Once you’ve configured all programs for monitoring, check monit syntax for errors. If found any errors fix them, it’s not so tough to figure out what went wrong. When you get a message like 「Control file syntax OK「, or if you see no errors, you can proceed ahead.

# monit -t
Or
$ sudo monit -t

After fixing all possible errors, you can type the following command to start the monit service.

# systemctl monit restart
OR
$ sudo systemctl monit restart

This is how looks monit after adding all Linux services for monitoring.

Linux Monitoring Services with Monit

Ref From: tecmint

Related articles