Monit is a free and open source monitoring system for Linux. It’s one of the essential tools a sysadmin must have. Use the following examples to create your own checks and your own conditions that will suit your need.
So, let’s begin.
With Monit, you can check a certain filesystem. As we are using include /etc/monit.d/* (it’s just another way of configuring Monit, you can put all of this in /etc/monit.conf too), we need to first create the appropriate file for the check we are about to configure. You can also name the file to your liking, it does not have to be the same as the example.
Create and edit the file using your favorite text editor as follows:
# vim /etc/monit.d/fs_check
check filesystem my_fs with path /
# if failed permission 660 then unmonitor
# if failed uid root then unmonitor
# if failed gid disk then unmonitor
# if space usage > 80% for 5 times within 15 cycles then alert
if space usage > 50% then alert
if inode usage > 30000 then alert
if inode usage > 99% then stop
Monit can also do various checks for a certain file. We will setup an SSL certificate’s permissions check. The configuration is pretty straightforward, and it goes as follows:
# vim /etc/monit.d/cert_check
check file certificate with path "/root/ssl/mydomain.tld.crt"
if failed permission 0700 then alert
Another example where Monit can do a file size check. Create and edit the corresponding file as follows:
# vim /etc/monit.d/file_check
check file important with path "/root/packages/test"
if size > 1000 KB then alert
Monit can also monitor a remote machine. In this example we will configure Monit to check different ports on a certain machine:
# vim /etc/monit.d/host1
check host dns1 with address 220.127.116.11
if failed icmp type echo count 3 with timeout 3 seconds then alert
if failed port 53 protocol dns with timeout 5 seconds then alert
if failed port 22 protocol ssh with timeout 3 seconds then alert
if failed port 80 protocol http with timeout 5 seconds then alert
In the example below we are checking for certain ports as well as an URL:
# vim /etc/monit.d/host2
check host my_host with address 18.104.22.168
if failed port 22 protocol ssh with timeout 5 seconds then unmonitor
if failed port 80 protocol http with timeout 5 seconds then unmonitor
if failed url http://22.214.171.124/test.txt then alert
Monit can also check for an exit status of a custom script file:
# vim /etc/monit.d/program
check program stat_check with path "/root/packages/test.sh"
if status > 0 then alert
Of course, you don’t have to do any of this if you use one of our Software Installation Services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.