How to Install and use Monit on Linux

Introduction

In UNIX/Linux-based systems, Monit is a free, open-source tool that automatically manages processes, files, directories, checksums, permissions, filesystems, and services like Apache, Nginx, MySQL, FTP, SSH, SMTP, and others. It also gives system administrators excellent and practical monitoring functionality.

A native HTTP(S) web server or the command line interface can be used to immediately view the system status and setup procedures via the monit’s user-friendly web interfaces web server, such as Apache or Nginx, must be installed on your system in order to access and view the Monit web interface.

How Monit Can Help

A process can be started by Monit if it is not already running, restarted if it is not responding, and stopped if it consumes too many resources. Additionally, Monit may monitor timestamps, checksum, file size, and other changes in files, directories, and filesystems.

You can use Monit to track the TCP/IP port, server protocols, and ping of distant hosts. Monit maintains its log file and notifies users of any urgent error circumstances and recovery progress.
Monit is especially helpful for keeping an eye on daemon processes, like those launched from /etc/init.d/ at system startup. Take mail servers, print servers, database servers, application servers, HTTP servers, and sshd as examples.

Features consist of:

  • Observing procedures.
  • Tracking down files.
  • Keeping an eye on directories.
  • Monitoring equipment.
  • We are monitoring a remote host can be useful if a service depends on a remote service, like a database server or an application server, while monit is primarily an application for monitoring and fixing services on localhost.
  • Protocol evaluation.
  • A web interface.

Proactive

If an error occurs, Monit can take action. For instance, if Sendmail is not operating, Monit can restart Sendmail automatically. Similarly, if apache is consuming too many resources (for example, if a DoS assault is underway), Monit can stop or restart apache and send you a warning message. Process attributes, such as how much memory or CPU time a process is using, can also be tracked by Monit.

Processes

Monit can be used to keep an eye on daemon processes and other similar software operating locally. Sendmail, sshd, apache, and MySQL are a few examples of daemon processes that can be monitored with the help of monit, such as those launched from /etc/init/ at system startup.

Files, Dirs, and Filesystems

Monit may also keep track of localhost files, directories, and filesystems. Monit can watch these elements for timestamps, checksums, or size changes. You can utilize this for security purposes as well. If a file’s md5 or sha1 checksum should change, you can receive a notification or take action.

Cloud and Hosts

Keep track of the network connections to multiple servers, local or on other hosts. There is support for TCP, UDP, and Unix Domain Sockets. Protocol-level network tests are possible; Monit includes built-in tests for the major Internet protocols, including HTTP, SMTP, and others. You can set Monit to send any data and test the server’s response even if the server does not support a protocol.

Programs and scripts

Like cron, Monit may be used to test programs or scripts at predetermined intervals. However, you can also use Monit to check a program’s exit value and take action or send an alert if it signals a problem. This means you can do any kind of check using Monit, for which you can write a script.

System

Finally, Monit can be used to monitor localhost’s system resources in general, including average load, memory consumption, and CPU usage.

Install Monit

In most Linux varieties, Monit may be quickly installed using the package manager. Before installing it, CentOS/RHEL users must activate the epel repository in their system.

CentOS, RHEL

yum install monit

Fedora 22+

dnf install monit

Debian and Ubuntu

sudo apt-get install monit

apt install monit

Arch Linux

pacman -S monit

Enable Monit Web Interface

(Optional For Fans Of CLI) Additionally, Monit offered a web interface for viewing the status of processes and services. Edit the configuration file (for RHEL-based systems, /etc/monit.conf, and Debian-based systems, /etc/monit/monitrc) in your preferred editor and uncomment the following lines to activate the monit web interface.

set httpd port 2812 and
use address localhost
allow localhost
allow admin:monit
allow @monit
allow @users readonly

Launch the monit service, etc.

service monit start

monit start

According to the setup above, monit will launch on port 2812 and will only be reachable from localhost. Simply alter the first line of the configuration shown above with your preferred port to modify the default port.

Now navigate to 2812 on localhost in your browser to open the Monit web interface.

Monitor Apache Service with Monit

Let’s begin by adding services to Monit so that it can keep track of them and take appropriate action when conditions are met. Apache is the first service we are installing. Add the following values to the end of your monit configuration file.

On CentOS, RHEL location: /etc/monit.conf

check process apache with pidfile /var/run/httpd.pid

start program = "/etc/init.d/httpd start" with timeout 60 seconds
stop program = "/etc/init.d/httpd stop"

On Debian/Ubuntu location: /etc/monit/monitrc

check process apache with pidfile /run/apache2.pid

start program = "/etc/init.d/apache2 start" with timeout 60 seconds
stop program = "/etc/init.d/apache2 stop"

Now use the service to restart the monit service. Use the command line to restart the monit and verify the status of the service.

monit summary

Configure Monit To Monitor MySQL/MariaDB

Let us add the MySQL server to the Monit configuration file to monitor it. Depending on the version of MySQL you have installed, the PID file’s location and init script may change.
check process mysqld with pidfile /var/run/mysqld/mysqld.pid

start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"

Test and confirm the configuration
Use the command below to check the file’s syntax after adding the necessary services to the monit monitoring configuration file.

monit -t

restart monit

Restart the monit service now.

service monit restart

View the following summary of Monit’s monitoring procedures and services:

monit summary

Use the following command to view a thorough description of each service:

monit status

Conclusion

On Linux systems, resources and services are managed and monitored by the open-source utility application Monit. CPU use, memory usage, server uptime, network connections, and server application services are a few of the services and resources that Monit manages. Resuming services that stop or experience operational issues also ensures that all running services are always in good condition. Numerous factors, including a power outage or a service crash, are capable of causing these problems. Monit helps keep your system up and running efficiently by controlling and monitoring these resources.

Additionally, Monit can be configured to generate notifications when certain conditions are met, such as a high CPU use rate or a low quantity of free RAM. This gives you the ability to respond if something goes wrong. Monit is a helpful tool for keeping your Linux system operating properly.

You can also use the Xitoring free plan to monitor Linux servers.

Leave a Reply

Your email address will not be published. Required fields are marked *