Introduction
Monit is a simple yet powerful monitoring tool that can automatically restart services/processes and send email alerts.
It has a clean web interface that runs on its own HTTP server. The steps described below should work on Debian and other Debian-based distros, including Ubuntu.
Installation and Configuration
Install Monit and stop it.
First, install Monit: apt install monit
Then stop it while we configure it: systemctl stop monit
Basic Configuration
By default, Monit configuration files are in /etc/monit
.
monitrc is the main configuration file, others may be added in /etc/monit/conf.d
and /etc/monit/conf-enabled
Monit can be configured to monitor any process/service/file. For this tutorial I’ll configure it to monitor system resources, network connectivity and bandwidth, and the apache2 service.
First, move to the configuration directory: cd /etc/monit
Then, make a backup of monitrc: cp monitrc monitrc.old
And clear the config file: echo "" > monitrc
We can now safely modify monitrc and we have a backup in case something goes wrong with the configuration. After every configuration change, it is recommended to run monit -t
to check the monitrc file and others config files for errors. The output should be “Control file syntax OK”.
Open monitrc with a text editor of your choice.
If you want monit to have a web interface for monitoring, add the following lines:
set httpd port 2813
allow username:password
Make sure you replace ‘username’ and ‘password’ with values of your choice. Make sure to choose a strong password.
Monit will now be able to serve a monitoring interface via http on port 2813. To access it, you would use http://your-ip:2813 and enter your login credentials. At the moment, the interface will be inaccessible.
We now need to set up some things that allow monit to work properly. These are an ID file where monit stores a hash, a state file where it stores state information, a directory where events will be stored, and a log file where it can write logs. In this tutorial, we will use default locations. You generally will not need to change these. Add the following to the monitrc file:
set logfile /var/log/monit.log
set idfile /var/lib/monit/id
set statefile /var/lib/monit/state
set eventqueue
basedir /var/lib/monit/events # set the base directory where events will be stored
slots 100 # optionally limit the queue size
You can choose how often you want monit to perform checks. For example, for checks to be performed every 30 seconds, add the following:
set daemon 30
Email/Alert Config
For emails, you need an email server running. Assuming you have that already and that you want email alerts, add the following lines (replacing “your-email@your-domain.your-tld” with your email address)
set mailserver localhost
set alert your-email@your-domain.your-tld
Monitoring of system and network resources.
For system load, CPU usage, memory (RAM) usage, network connectivity, bandwidth and swap file/partition usage to be monitored and alerts to be sent in case of excessive load/consumption, the following is needed:
check system $HOST
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if cpu usage > 95% for 10 cycles then alert
if memory usage > 85% then alert
if swap usage > 25% then alert
check network public with interface ens3
if changed link then alert
if saturation > 90% then alert
if total uploaded > 1 GB in last hour then alert
Let’s explain these lines.
The first line tells monit to check the system and give it a name, $HOST, which equals the hostname of your VPS.
The second and third lines tells monit to send alerts if system load is higher than 2 for 5 minutes, or higher than 4 for one minute. If your server is under constant heavy load, you may choose to modify these values.
The following 3 lines instruct monit to send alert if CPU, memory, swap usage are higher than 95, 85, and 25% respectively. You can also modify these values to your liking, but the ones provided here and considered reasonable.
The last 4 lines direct monit to check the network interface (You may need to replace ‘ens3’ with your external network interface name) And to send alerts if connectivity goes down, or if the interface is saturated above 90%, or if more than 1GB was uploaded in the last hour. Change ‘1 GB’ according to your network activity.
Apache Monitoring
For apache, monitoring is simple: Check that apache is running and listening on the defined port. If not, restart it.
check process apache with pidfile /run/apache2/apache2.pid
group www
start program = "/bin/systemctl start apache2"
stop program = "/bin/systemctl stop apache2"
if failed host localhost port 80
protocol HTTP request "/" then restart
Final checks and deployment
Almost done!
Since we stopped monit, we need to start it with systemctl start monit
Check that our configuration is fine with monit -t
If you followed this tutorial correctly, you should now be able to access the interface and receive alerts. The web interface should now look like the following: