Monit is a utility for managing and monitoring processes on a Unix-based system. It provisions automatic maintenance and repair tasks in errors situations, such as:
- start a process if it does not run,
- restart a process if it does not respond and,
- stop a process if it uses too much resources.
In this tutorial, we will be installing and configuring Monit on an Alibaba Cloud Elastic Compute Service (ECS) instance with CentOS 7.
Prerequisites
-
You must have Alibaba Cloud Elastic Compute Service (ECS) activated and verified your valid payment method. If you are a new user, you can get a free account in your Alibaba Cloud account.
-
Your ECS instance OS should be CentOS 7. Monit supports different Linux distros, but this guide will use CentOS 7.
-
Processes that Monit will be monitoring, should already be configured. For example, your LEMP or LAMP stack should already be active and configured.
Installing Monit
Monit is not available in the default CentOS 7 repositories. Therefore, before installing Monit, you need to install the EPEL repository, using the following command:
yum -y install epel-release
Then, import the EPEL GPG-key, using the following command:
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
It is also important to ensure that the system is up to date, and that the EPEL package list is loaded prior installation of Monit. The following command takes care of this:
yum -y update
You may now install Monit:
yum -y install monit
Finally, create the system startup links. With the enable command, you tell the system to start the Monit service on system boot.
systemctl enable monit
And to start the service use:
systemctl start monit
To confirm the service is running, you may use the following command:
systemctl status monit
You should get active (running) status.
Configuring Monit
Now, it is necessary to provide Monit with the services to be monitored, and the required actions on different events. There are many options and rules of actions available with Monit. However, my recommendation is to keep it as simple as possible.
Use nano to edit the configuration file.
nano /etc/monit.d/monitrc
This is how the configuration file looks like in my system.
Monit logs to syslog or to its own log file. This is enabled on the first line:
set logfile syslog facility log_daemon
In case you want to access the log file:
nano /var/log/monit.log.
Then, we start Monit in the background (that is, run as a daemon), that will check services every 30 seconds. Optionally you may delay the first check. In this example, the first check is after 4 minutes of service start.
set daemon 30
with start delay 240
You may also configure the system send email alerts (note that the underscore (_) below should be dots (.) but the system is taking these as links which are not allowed )
set mailserver mail_yourserver_com
set mail-format { from: admin@yourserver_com }
set alert admin@yourserver_com
Finally, you add the services you need to monitor. Most blocks are similar.
Let’s begin with the web server. In this case, we are using Nginx.
check process nginx with pidfile /var/run/nginx.pid
start program = "/usr/bin/systemctl start nginx"
stop program = "/usr/bin/systemctl stop nginx"
if failed port 80 protocol http then restart
if failed port 443 protocol https then restart
In the first line, the process (ngnix in this case) and its pid location are identified. This is followed by the process start and stop commands.
Then, optionally, you may also indicate ports to be monitored. In this case, ports 80 and 443 are monitored, and the service will be restarted if any of these failed.
Let’s look at the MariaDB process monitoring. It is very similar to the Nginx block. However, two new options are included for this service:
- timeout of service (last line of block)
- block group (second line of block)
check process mysql with pidfile /var/run/mariadb/mariadb.pid
group database
start program = "/usr/bin/systemctl start mariadb"
stop program = "/usr/bin/systemctl stop mariadb"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout
With the timeout option, you tell the system that if the service restarts x amount of times within a time range, to timeout the service. This, of course, will send an email alert. This is to prevent using system resources on a service that is likely to continue to fail, and thus require further administration.
Service entries in the control file, can be grouped together by the group statement. With this statement it is possible to group similar service entries together and manage them as a unit. Monit provides functions to start, stop, restart, monitor and unmonitor a group of services.
In this case, an administrator may be able to execute a single command to all services associated with the database group:
monit -g <groupname> <command like: start, stop, or restart>
Finally, note that a service can be added to multiple groups by using more than one group statement.
Once you have all the services you required, you may save and close the file (CNTR+X and Enter).
Then, check for syntax errors on the config file, with the following command:
monit -t
If no syntax errors, reload the service, so changes take effect:
monit reload
You may now check the status of the monitored services with the following command:
monit status
Congratulations! You have successfully installed Monit on your server, and key services are being monitored using Monit on your Alibaba Cloud Elastic Compute Service (ECS) CentOS 7 server.