How to Install and Setup Monit (Linux Process and Services Monitoring) Program
Monit is a free open source and very useful tool that automatically monitors and manages server process, files, directories, checksums, permissions, filesystems and services like Apache, Nginx, MySQL, FTP, SSH, Sendmail and so on in a UNIX/Linux based systems and provides an excellent and helpful monitoring functionality to system administrators.
The monit has user friendly web interface where you can directly view the system status and setup up processes using 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 monit web interface.
Read Also : 10 Linux Performance Monitoring Tools
What Monit can do
Monit has a 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 a RHEL, CentOS, Fedora, Ubuntu, Linux Mint and Debian Linux Operating Systems, but it should be easily compatible to Scientific Linux as well.
Step 1: Installing Monit
By default, Monit tool is not available from the system base repositories, you need to add and enable third party epel repository to install monit package under your RHEL/CentOS systems. Once you’ve added epel repository, install package by running the following yum command. For Ubuntu/Debian/Linux Mint user’s can easily install using apt-get command as shown.
On RedHat/CentOS/Fedora/
# yum install monit
On Ubuntu/Debian/Linux Mint
$ sudo apt-get install monit
Step 2: Configuring Monit
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 in every 2 minutes and keeps the logs in “/var/log/monit“.
Monit has it’s web interface that runs on port 2812 using web server. To enable web interface you need to make changes in 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/monit.conf
$ sudo vi /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 monit user and password or you can use default ones.
set httpd port 2812 and use address localhost # only accept connection from localhost allow localhost # 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
Once you’ve configured it, you need to start the monit service to reload the new configuration settings.
# /etc/init.d/monit start
$ sudo /etc/init.d/monit start
Now, you will able to access the monit web interface by navigating to the “http://localhost:2812” or “http://example.com:2812“. Then enter user name as “admin” and password as “monit“. You should get screen similar to below.
Step 3: Adding Monitoring Services
Once monit web interface correctly setup, start adding the programs that you want to monitor into the /etc/monit.conf 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, that can be very helpful to see how a service is running, where it keeps its pidfile and how to start and stop a service etc.
Apache
check process httpd with pidfile /var/run/httpd.pid group apache start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/httpd stop" if failed host 127.0.0.1 port 80 protocol http then restart if 5 restarts within 5 cycles then timeout
Apache2
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"
Nginx
check process nginx with pidfile /var/run/nginx.pid start program = "/etc/init.d/nginx start" stop program = "/etc/init.d/nginx stop"
MySQL
check process mysqld with pidfile /var/run/mysqld/mysqld.pid group mysql start program = "/etc/init.d/mysqld start" stop program = "/etc/init.d/mysqld stop" if failed host 127.0.0.1 port 3306 then restart if 5 restarts within 5 cycles then timeout
SSHD
check process sshd with pidfile /var/run/sshd.pid start program "/etc/init.d/sshd start" stop program "/etc/init.d/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’s went wrong. When you get message like “Control file syntax OK“, or if you see no errors, you can proceed ahead.
# monit -t
$ sudo monit -t
After fixing all possible errors, you can type the following command to start the monit service.
# /etc/init.d/monit restart
$ sudo /etc/init.d/monit restart
You can verify that monit service is started by checking log file.
# tail -f /var/log/monit
$ sudo tail -f /var/log/monit.log
Sample Output
[BDT Apr 3 03:06:04] info : Starting monit HTTP server at [localhost:2812] [BDT Apr 3 03:06:04] info : monit HTTP server started [BDT Apr 3 03:06:04] info : 'tecmint.com' Monit started [BDT Apr 3 03:06:04] error : 'nginx' process is not running [BDT Apr 3 03:06:04] info : 'nginx' trying to restart [BDT Apr 3 03:06:04] info : 'nginx' start: /etc/init.d/nginx
Monit Screenshot
This is how looks monit after adding all process for monitoring.
Do it monitor multi remote servers ?
@Quang,
Unfortunately, no its build to monitor remote servers, for that there is Nagios, you can install and monitor multiple remote Linux and Windows machines. For installation check out this article – https://www.tecmint.com/install-nagios-in-linux/
yes, but i see monit has useful function auto restart service when fail or not running, that a great !!!
have nagios-plugin do that?
@QuangPH,
No nagios plugin doesn’t provide auto restart of services functionality..
This tutorial is very good. I’m using Debian8, Nginx and ISPConfig. The article does not cite whether it needs or works with Apache or Nginx. So, will it work to monitor any service, either one or the other, and the rest, correct?
Not able to install the monit package.. can you please help?
Last login: Wed Apr 20 14:00:19 2016 from 192.168.124.1
[[email protected] ~]# yum install monit -y
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Local-Repo | 2.9 kB 00:00
Setting up Install Process
No package monit available.
Error: Nothing to do
[[email protected] ~]# yum install monit* -y –nogpgcheck
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
No package monit* available.
Error: Nothing to do
[[email protected] ~]# rpm -q monit*
package monit* is not installed
[[email protected] ~]# rpm -ivh monit*
error: File not found by glob: monit*
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# yum –enablerepo=epel install monit
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
[[email protected] ~]# yum –enablerepo=epel install monit -y –nogpgcheck
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
@Asif,
First register your RHEL distributions to RHN subscription management portal to install and get updates for new packages, here is the guide on how to register RHEL distribution to RHN network https://www.tecmint.com/enable-redhat-subscription-reposiories-and-updates-for-rhel-7/
I am looking for a tool that lists/logs all “read / write / sent / received” activities of any process in detail additionally.
Is that possible with this tool?
It would also be nice to restrict a critical process instead of deleting it like security tools like to do. I am also looking for a tool that monitors installation activities and maybe backups modified system values or at least let me know. Could anyone give me an advisement?
I need also be sure about any access for example keyboard and mouse or other peripherals. Those psycho attacks are state of the art and there is no easy way to get sure about it so far or I just haven t found the right tool yet.
I will have a look if anyone has a suggestion for me later on. Thanks for reading :)
If “yum install monit” doesn’t work, try with “yum –enablerepo=epel install monit”
Nice tutorial, just a small typo here: ” following command to stat the monit service”
@Octopus,
Thanks for pointing out that typo, corrected in the article..
Thanks for this article btw ;)
Typo:
tail -f /var/log/monit
must be
tail -f /var/log/monit.log
Right?