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.
- 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.
Installation of Monit
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
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
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: a447ea6daa8857bcf3c5089d0d225e83 Stored in '/root/.monit.id' Starting Monit 5.26.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.26.0 uptime: 1m System 'master' status OK monitoring status Monitored monitoring mode active on reboot start load average [0.00] [0.00] [0.00] cpu 0.0%us 0.0%sy 0.0%wa memory usage 306.4 MB [17.4%] swap usage 0 B [0.0%] uptime 1h 59m boot time Wed, 30 Sep 2020 17:53:25 data collected Wed, 30 Sep 2020 19:51:59
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 --now monit $ sudo 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 Wed 2020-09-30 20:27:05 UTC; 2min 4s ago Main PID: 83433 (monit) Tasks: 2 (limit: 11070) Memory: 960.0K CGroup: /system.slice/monit.service └─83433 /usr/bin/monit -I
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: Stopping The nginx HTTP and reverse proxy server... Sep 30 21:12:55 master systemd: Stopped The nginx HTTP and reverse proxy server. Sep 30 21:13:24 master monit: 'nginx' process is not running Sep 30 21:13:24 master monit: 'nginx' trying to restart Sep 30 21:13:24 master monit: 'nginx' start: '/usr/bin/systemctl start nginx.service' Sep 30 21:13:24 master systemd: Starting The nginx HTTP and reverse proxy server... Sep 30 21:13:24 master nginx: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Sep 30 21:13:24 master nginx: nginx: configuration file /etc/nginx/nginx.conf test is successful Sep 30 21:13:24 master systemd: Started The nginx HTTP and reverse proxy server. Sep 30 21:13:54 master monit: '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
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: