Install and Configure Monit on Centos

About Monit

Monit is a free, open source process supervision tool for Linux like operating systems. With Monit, system status can be viewed directly from the command line, or via the web browser. Monit is able to do automatic maintenance, repair, and run meaningful causal actions in error situations.

 

Install Monit

yum -y install monit

Start monit by using the following command.

monit

Check the monit status.

monit status

The output will be as below:

Once monit is installed, you can add programs and processes to the configuration file.

The main configuration file is /etc/monitrc

vi /etc/monitrc

By default, monit is set to check the services at an interval of 1 min. This can be altered as per your need by changing the below line.

set daemon  60

Change log file by editing the following line:

set logfile syslog

Set up monit web interface

Monit provides a web interface for process monitoring. By default, monit listens on 2812 port, but it needs to be set up.

Open monit configuration file /etc/monitrc.

vi /etc/monitrc

Look for httpd port 2812, modify the following entries.

FROM:

set httpd port 2812 and

use address localhost # only accept connection from localhost

allow localhost # allow localhost to connect to the server and

allow admin:monit # require user ‘admin’ with password ‘monit’

allow @monit # allow users of group ‘monit’ to connect (rw)

allow @users readonly # allow users of group ‘users’ to connect readonly

TO:

set httpd port 2812

allow 0.0.0.0/0.0.0.0

allow localhost

allow admin:monit

From the settings, monit will listen on 2812; admin user with password monit will able to access the web interface from any network.

Restart monit.

systemctl restart monit

Auto-start Monit on start-up.

systemctl enable monit

Accessing monit web interface

Open web browser and go to the below address:

http://ip-addr:2812

Use the username and password mentioned before.

Monit home page will look like this:

Configure services for monitoring with monit

To configure the services which we want to monitor with monit we have to add seperate configuration files under the directory /etc/monit.d/ in which default configuration looks for extra configuration files.

Configuration file for HTTP:

vi /etc/monit.d/httpdmonitor

and edit the file as shown below:

check process httpd with pidfile /var/run/httpd/httpd.pid

start program “/usr/bin/systemctl start httpd.service”

stop program “/usr/bin/systemctl stop httpd.service”

if failed port 80 protocol http then restart

 
Configuration file for ssh:
vi /etc/monit.d/sshdmonitor

and edit the file as shown below:

check sshd with pidfile /var/run/sshd.pid

start program “/usr/bin/systemctl start sshd.service”

stop program “/usr/bin/systemctl stop sshd.service”

if failed port 22 protocol ssh then restart

Configuration file for Mysql:

vi /etc/monit.d/mysql

and edit the file as below:

check process mysqld with pidfile /var/run/mysqld/mysqld.pid

start program = “/etc/init.d/mysql start”

stop program = “/etc/init.d/mysql stop”

if failed port 3306 protocol mysql then restart

Configuration file for Disk monitoring:

vi /etc/monit.d/diskmonitor

and edit the file as below:

check filesystem disk1 with path /dev/xvda1

if SPACE usage > 50% then alert

Here my filesystem is /dev/xvda1. You have to confirm your filesystem before entering the name.

You can do that by using the command:

df -h

The output will be as below:

Configuration file for syslog:

vi /etc/monit.d/syslogmonitor

and edit the file as below:

check process syslogd with pidfile /var/run/syslogd.pid

start program = “/usr/bin/systemctl start rsyslog.service”

stop program = “/usr/bin/systemctl stop rsyslog.service”

Once configured, test the monit syntax

monit -t

ouput of the above command shows:

Control file syntax OK

The above message indicate that all the configuration files are correctly entered.

Reload monit, to take effect of changes:

monit reload

We can see the new services that we configured just now through the web interface.

Mail Alerting with Monit

There are predefined alerting templates available in Monit to alert admins when the particular service fails.

Edit the configuration file.

vi /etc/monitrc

You can update the below alerting template as per your requirement:

set mail-format {

      from: monit@$HOST

  subject: monit alert — $EVENT $SERVICE

  message: $EVENT Service $SERVICE

                 Date:        $DATE

                 Action:      $ACTION

                 Host:        $HOST

                 Description: $DESCRIPTION

            Your faithful employee,

            Monit Team

}

Set the recipient address here.

set alert admin@spotfixcrew.com

Use your email-id in place of “admin@spotfixcrew.com”

Note: You will receive alerts on all type of actions

If you do not like to alert on user-initiated service restart, then add the below configuration:

set alert admin@spotfixcrew.com not on { instance, action }

Finally, set the mail server configuration so that you can receive mails:

set mailserver  mx.spotfixcrew.com  port 25

Reload the service:

monit reload

Leave a Reply

Your email address will not be published. Required fields are marked *