Simple monitoring and alerting with Monit on Ubuntu 22.04 LTS
On Ubuntu 22.04:
sudo apt update sudo apt install monit -y sudo systemctl enable monit --now
check filesystem root with path /dev/sda1 if space usage > 90% for 5 cycles then alert
Mailserver config example using Gmail in
set mailserver smtp.gmail.com port 587 username "your-username" password "12-digit-app-password" using tlsv13 with timeout 30 seconds set alert email@example.com
Then reload Monit:
sudo monit reload
Monit is a utility to monitor your services, systems, and processes and also can handle alarms as well. Not only that, monit is available on most official upstream repositories. It has a lightweight web user interface that allows you to review the monitored components in a simple visual representation.
So, in this article, I will show you how to install Monit, how to monitor things, and to set the alerting for them as well. This time, I will use a Ubuntu 22.04 VM instance.
Step 1. Do a repository update and install Monit:
sudo apt update sudo apt install monit -y
Step 2. Start monit service and enable it
sudo systemctl enable monit --now
Step 3. Open the
/etc/monit/monitrc file, then find the following lines and uncomment them to enable the Web UI (specify the SSL cert path if you have one):
set httpd port 2812 and use address localhost allow localhost allow admin:monit
Step 4. Reload monit.
sudo monit reload
Step 5. Test it by issuing
sudo monit status, it should output something like this:
You can access the Web UI by tunneling port 2812 to any free local port on your PC/Laptop.
Monit can monitor a variety of things, for instance:
- Monitor websites using HTTP or HTTPS protocol
- Monitor TCP ports
- Monitor remote hosts
- Monitor popular protocols like SMTP, FTP, LDAP, etc.
- Monitor SSL certificates
- Monitor processes
- Monitor files, directories, and disks
- Monitor network interfaces
- Monitor a program’s output
This time, I’ll show an example of how to monitor a remote host and local disk usage.
Monitor a remote host
Step 1. Go to
/etc/monit/conf.d and create a new file. The name could be anything, but for example, name it simply “host”
Step 2. Edit the file with the following content:
check host myhost with address IP-ADDRESS if failed ping then alert
It basically means that we monitor a host at the specified IP-ADDRESS and if it can’t be pinged then send an alert. Pretty straightforward right?
Step 3. Do a
sudo monit reload to allow Monit to reload its configurations without stopping the service.
Step 4. Check the monitoring using
sudo monit status myhost
Monitor a local disk
Step 1. Create a new config file in
/etc/monit/conf.d and name it a “disk”
Step 2. Fill in the file with the following configuration. You may change the disk to your actual disk path:
check filesystem root with path /dev/sda1 if space usage > 90% then alert
Step 3. Reload monit:
sudo monit reload
Step 4. Verify the monitor:
sudo monit status root
Configure email alerting
Step 1. Open
Step 2. Add the following lines and adjust the data accordingly
set maileserver your.smtp-server.com port the-port username "username" password "password" using tls set alert firstname.lastname@example.org
However, if you only have a public mailing service like Gmail for instance, you can use the following configuration:
set mailserver smtp.gmail.com port 587 username "username-without-atgmailcom" password "the-password" using tls with timeout 30 seconds set alert email@example.com
Step 3. Test the alert by simply reloading the Monit. Example email:
You can change the alerting format in the
monitrc file, just try to look around at the config.
For more examples of how to add other monitoring types, please visit Monit’s official documentation here: https://mmonit.com/monit/documentation/monit.html
It’s so easy to install and configure Monit and monitor things with it.