In this post we explain how to install and configure Monit on an Ubuntu server. Monit is a monitoring system for servers that allows us to control resource and service usage.
Monit is recommended for monitoring a single server; if you have several, there are other alternatives such as Zabbix or Nagios.
At the end, we include a link to the Monit website with all configuration documentation.
Install Monit on our server
# apt install monit
Enable Monit web interface
Create a file by running: vi /etc/monit/conf.d/web-interface with content:
set httpd port 2812 use address 46.183.XXX.XXX #Public IP of our Cloud server allow 0.0.0.0/0.0.0.0 allow 'admin':'monit' # user:password change these values for more secure ones
Configure SMTP server for alert emails
Skip this step if you don’t want to receive alerts by email. Create a file by running: vi /etc/monit/conf.d/smtp-server with content:
set mailserver mail.mydomain.com port 587 # change mail.mydomain.com to your server username User password "Password" # change User and Password using tls with timeout 30 seconds set alert alerts@mydomain.com # email address that will receive alerts
Configure alert email format
Skip this step if you don’t want to receive alerts by email. Create a file by running: vi /etc/monit/conf.d/mail-format with content:
set mail-format {
from: monit@mydomain.com # valid sender to avoid spam filters
subject: monit alert -- $EVENT $SERVICE
message: $EVENT Service $SERVICE
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION
Monit Clouding
}Monitor our disk
Create a file by running: vi /etc/monit/conf.d/disk with content:
check filesystem "root" with path /dev/sda1 if space usage > 80% for 8 cycles then alert if space usage > 99% then stop # prevent server from filling up if inode usage > 80% for 8 cycles then alert if inode usage > 99% then stop # prevent server from filling up
Monitor Apache Server
Create a file by running: vi /etc/monit/conf.d/apache with content:
check process apache with pidfile /run/apache2/apache2.pid start program = "/etc/init.d/apache2 start" with timeout 60 seconds stop program = "/etc/init.d/apache2 stop" if cpu > 60% for 2 cycles then alert if cpu > 80% for 5 cycles then restart if totalmem > 200.0 MB for 5 cycles then restart if children > 250 then restart if loadavg(5min) greater than 10 for 8 cycles then stop if 3 restarts within 5 cycles then unmonitor
Monitor SSH Server
Create a file by running: vi /etc/monit/conf.d/ssh with content:
check process sshd with pidfile /var/run/sshd.pid start program "/etc/init.d/ssh start" stop program "/etc/init.d/ssh stop" if failed port 22 protocol ssh then restart if 3 restarts within 5 cycles then unmonitor
Monitor MariaDB/MySQL Server
Create a file by running: vi /etc/monit/conf.d/mysql with content:
check process mysqld with pidfile /run/mysqld/mysqld.pid start program = "/etc/init.d/mariadb start" stop program = "/etc/init.d/mariadb stop" if failed host 127.0.0.1 port 3306 then restart if 3 restarts within 5 cycles then unmonitor
Monitor file size
Create a file by running: vi /etc/monit/conf.d/size-log with content:
check file error.log with path /var/log/mysql/error.log if size > 100 MB then alert
Open Clouding Firewall
To access Monit via web, open TCP port 2812 in the Clouding firewall.
Restart Monit
Once all services/resources are configured for monitoring, restart Monit:
# monit reload
You can now visit http://46.183.XXX.XXX:2812. It will ask for a username and password; use those configured in “Enable Monit web interface.”
Below is an example view of Monit simulating an unrecoverable Apache failure:
If you use VestaCP or HestiaCP
If you use VestaCP or HestiaCP, you must also open the port in the Firewall section:
Other monitoring examples
Based on the previous examples, you can monitor any service. Just locate its pid, for example using the SSH example for CRON, the pid would be: /run/crond.pid
You can consult the Monit documentation on its website.