How can we help?
Categories
< All Topics
Print

Simple monitoring and alerting with Monit on Ubuntu 22.04 LTS

TLDR Guide

On Ubuntu 22.04:

 sudo apt update
 sudo apt install monit -y
 sudo systemctl enable monit --now

Example config:

check filesystem root with path /dev/sda1
    if space usage > 90% for 5 cycles then alert

Mailserver config example using Gmail in /etc/monit/monitrc:

set mailserver smtp.gmail.com port 587
    username "your-username" password "12-digit-app-password"
    using tlsv13
    with timeout 30 seconds

set alert your-username+alerting@gmail.com

Then reload Monit:

 sudo monit reload

Introduction

Monit is a utility to monitor your services, systems, and processes and can handle alarms. Not only that, monit is available on most official upstream repositories. Its lightweight web user interface allows you to review the monitored components in a simple visual representation.

So, in this article, I will show you how to install Monit, monitor things, and set the alerting for them as well. This time, I will use a Ubuntu 22.04 VM instance.

Installation Steps

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:

Monit status output

You can access the Web UI by tunneling port 2812 to any free local port on your PC/Laptop.

Monit’s Web UI

Monitoring things

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
  • Etc.

This time, I’ll show an example of monitoring 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 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 status

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

Disk monitoring status

Configure email alerting

Step 1. Open /etc/monit/monitrc

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 alert-recipient@example.com

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 alert-recipient@example.com

Step 3. Test the alert by simply reloading the Monit. Example email:

Alert email from monit

You can change the alerting format in the monitrc file; try looking around at the config.

Additional Notes

For more examples of how to add other monitoring types, please visit Monit’s official documentation here: https://mmonit.com/monit/documentation/monit.html

Conclusion

It’s so easy to install and configure Monit and monitor things.

Find more tips and tutorials article in ServerStadium’s Knowledge Base. Even better, you can register in ServerStadium and try our powerful VM.

Table of Contents