Monit monitoring tool can notify webmasters whenever the service/process goes down. It automatically does the maintenance and repair of the particular process and ensures it is brought back online.
As a part of our Server Management Services, we help our Customers with software installations regularly.
Let us today discuss the steps to perform this task.
Install Monit on Centos 8
Like in most other installations, the first step here is to update and install necessary packages. We have to make sure all dependencies have been met by installing the following important packages.
$ sudo dnf -y update
$ sudo dnf -y install zlib-devel pam-devel openssl-devel libtool bison flex autoconf gcc make git epel-release
Now, let us move on to the monit install:
$ sudo dnf -y install monit
After the install, let us start monit with the command below:
$ sudo monit
###You should see the message below in the end###
New Monit id: a447ea6daa8857bcf3c5089d0d225e83
Stored in '/root/.monit.id'
Starting Monit 5.26.0 daemon with http interface at [localhost]:2812
After starting Monit, we can check the status as follows:
$ sudo monit status
Monit 5.26.0 uptime: 1m
System 'master'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
load average [0.00] [0.00] [0.00]
cpu 0.0%us 0.0%sy 0.0%wa
memory usage 306.4 MB [17.4%]
swap usage 0 B [0.0%]
uptime 1h 59m
boot time Wed, 30 Sep 2020 17:53:25
data collected Wed, 30 Sep 2020 19:51:59
Further, we can as well enable Monit to start when the server restarts.
$ sudo systemctl enable --now monit
Monit configuration
Monit uses the monitrc control file located in /etc/monitrc directory for its configuration.
The file is setup to start Monit’s http server. Look for the section in the snippet below and change the IP from localhost to 0.0.0.0 which will allow us to access the web interface from all IPs that can reach the server.
$ sudo vim /etc/monitrc
set httpd port 2812
#use address localhost => only accept connection from localhost (drop if you use M/Monit)
use address 0.0.0.0
allow 0.0.0.0/0
allow admin:monit
Also, open the port 2812 once we are done editing. and then restart Monit service as well.
$ sudo firewall-cmd --permanent --add-port=2812/tcp
$ sudo firewall-cmd --reload
$ sudo systemctl restart monit
After starting Monit, open the link http://[IP-or-domain_name]:2812 on the browser. Log in with the username “admin” and password “monit” when prompted.
Add services for Monit to monitor
Once we have Monit running, we need to add the services to the monitoring.
For instance, to enable the monitoring of nginx, we shall add the following configuration in /etc/monit.d directory that can hold configuration files for various services being monitored.
$ sudo vim /etc/monit.d/nginx-monitor
check process nginx with pidfile /run/nginx.pid
start program "/usr/bin/systemctl start nginx.service"
stop program "/usr/bin/systemctl stop nginx.service"
if failed port 80 protocol http then restart
To check whether the syntax in the configuration file is correct, we can do so using the command given below:
$ sudo monit -t
Control file syntax OK
Now reload Monit for it to accommodate the new configuration.
$ sudo monit reload
Reinitializing monit daemon
The web interface will now show the details of those being monitored.
Other services such as sshd, httpd, syslogd, postfix, and much more can be added in the same fashion in their own files within /etc/monit.d/ directory.
Alert configuration
Monit checks services at 30 seconds intervals by default. we can change this value in the configuration file (/etc/monitrc) to befit our requirements. Look for the line “set daemon 30” in the file and change the number (in seconds) to more or less.
There are predefined alerting templates available in Monit to alert system administrators when the particular service fails.
For instance to use the local relay (email server) to alert the root user, edit the /etc/monitrc file with the below template:
set mail-format {
from: Monit <monit@$HOST>
subject: monit alert — $EVENT $SERVICE
message: $EVENT Service $SERVICE
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION
}
To set the recipient address to receive alerts on all type of actions use the format below:
set alert root@localhost
Likewise, to set the recipient address to not alert on user-initiated service restarts, use the format below:
set alert root@localhost not on { instance, action }
Finally, set the mail server configuration so that you can receive mails.
set mailserver localhost
Here, replace the localhost with the corresponding mail server name to receive the mails on an external server. Now, reload the service for the changes to take effect.
systemctl reload monit
[Need any further assistance to install Monit on Centos? – We’re available 24*7]
Conclusion
In short, install of Monit on Centos involves steps to adjust the configuration and setting up alerts to notify the users. Today, we saw how our Support Engineers perform this task.
0 Comments