In this post, we will explain how to install and configure Monit on a Centos server. Monit is a monitoring system for our servers that lets us track resource usage and services.
Monit is recommended for monitoring a single server; if you have several servers, there are other alternatives such as Zabbix or Nagios.
At the very end, we include a link to the Monit website, where you can find all the configuration documentation.
Install Monit on our server
# rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# yum install monit
# systemctl start monit.service
# systemctl enable monit.serviceEnable the Monit web interface
Create a file by running: vi /etc/monit.d/web-interface with the following contents:
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' # username:password replace these credentials with more secure onesConfigure the SMTP server to send alerts
Skip this step if you do not want to receive email notifications. Create a file by running: vi /etc/monit.d/smtp-server with the following contents:
set mailserver mail.midominio.com port 587 # replace mail.midominio.com with your server
username Usuario password "Contraseña" # change Usuario and Contraseña
using TLSV1 with timeout 30 seconds
set alert alertas@midominio.com #email address that will receive alertsConfigure the alert email format
Skip this step if you do not want to receive email notifications. Create a file by running: vi /etc/monit.d/mail-format with the following contents:
set mail-format {
from: monit@midominio.com #specify a valid sender to prevent antispam filtering of alerts
filtren alertas
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.d/disco with the following contents:
check filesystem "root" with path /dev/sda1
if space usage > 80% for 8 cycles then alert
if space usage > 99% then stop #stops our server to prevent it from filling up
if inode usage > 80% for 8 cycles then alert
if inode usage > 99% then stop #stops our server to prevent it from filling up Monitor the httpd server
Create a file by running: vi /etc/monit.d/httpd with the following contents:
check process apache with pidfile /run/httpd/httpd.pid
start program = "/etc/init.d/httpd start" with timeout 60 seconds
stop program = "/etc/init.d/httpd 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 unmonitorMonitor the SSH server
Create a file by running: vi /etc/monit.d/ssh with the following contents:
check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/sshd start"
stop program "/etc/init.d/sshd stop"
if failed port 22 protocol ssh then restart
if 3 restarts within 5 cycles then unmonitorMonitor the MariaDB/MySQL server
Create a file by running: vi /etc/monit.d/mysql with the following contents:
check process mysqld with pidfile /run/mysqld/mysqld.pid
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"
if failed host 127.0.0.1 port 3306 then restart
if 3 restarts within 5 cycles then unmonitorMonitor the size of a file
Create a file by running: vi /etc/monit.d/size-log with the following contents:
check file error.log with path /var/log/mysql/error.log
if size > 100 MB then alertOpen the Clouding firewall
To access Monit via the web, we need to open TCP port 2812 in the Clouding firewall.
Restart Monit
Once we have configured all the services/resources to monitor, we can restart Monit:
# monit reloadWe can now visit http://46.183.XXX.XXX:2812, which will ask for a username and password; use the ones configured in “Enable the Monit web interface”
Here you can see what Monit looks like. We have simulated an unrecoverable apache failure to see how it stops monitoring:
If you use VestaCP
If you use VestaCP, you will need to access it and also open the port in the Firewall section:
Other monitoring options
Based on the examples above, you can monitor any service; you just need to locate the corresponding pid. For example, we can use the SSH example for CRON; the pid would be: /run/crond.pid
You can consult the Monit documentation on its website.