×


Install Monit monitoring system on CentOS 8 - How to do it

Monit is a free, open-source process supervision tool for Unix and Linux.

Monit can restart a process automatically if process dies or monitor process characteristics, such as memory or cpu cycles and alert by email or execute and action.

Basically, 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.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform software installation tasks.

In this context, we shall look into the steps to perform Monit monitoring system Installation.


How to 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


How to configure Monit ?

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.


How to 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.


How to perform Monit 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 urgent assistance to install Monit on Centos? – We're available 24*7. ]


Conclusion

This article covers how to #install Monit on #Centos 8 system. Also, we dealt with how to configure Monit on Centos, adjusting the configuration and setting up alerts to notify the users.

#Monit is a free, open-source process supervision tool for Unix and Linux. With Monit, system status can be viewed directly from the command line, or via the native HTTP(S) web server. Monit is able to do automatic maintenance, repair, and run meaningful causal actions in error situations.


To enable web interface you need to make changes in monit configuration file. 

The main configuration file of monit located at /etc/monit. conf under (RedHat/CentOS/Fedora) and /etc/monit/monitrc file for (Ubuntu/Debian/Linux Mint). 

Monit is very easy to use nearly out of the box. By default, it is set up to check that services are running every 2 minutes and stores its log file in “/var/log/monit.


To Install Monit to monitor your server running CentOS:

1. Install EPEL repository: yum install epel-release yum update.

2. Install Monit: yum install monit.

3. Activate Monit to start automatically during the system boot and start it: systemctl enable monit systemctl start monit.


To #Install #Monit Package on #Ubuntu:

Run the commands given below;

sudo apt update

sudo apt install monit


After installing Monit, the commands below can be used to stop, start and enable Monit service;

sudo systemctl stop monit.service

sudo systemctl start monit.service

sudo systemctl enable monit.service