How to monitor services with Monit on CentOS 7 / RHEL 7 and automatically restart a service on failure or crash

If you’re experiencing issues such as MariaDB/Apache/SSH or other services crashing and not restarting after the failure, and require time to investigate the issue, Monit is perhaps the best way to monitor and restart services in case they fail. This guide is to walk you through the process of installing and configuring Monit on CentOS 7 / RHEL 7.

INSTALL MONIT

Use YUM package manager:

Note: if the command above fails, make sure you’ve enabled epel repository in your system

Output should look like this:

1-8-2015 7-57-24 AM

 

ENABLE MONIT WEB INTERFACE

Open Monit configuration file to setup web interface:

Locate following code and comment it out:

Right below add:

It should look like this:

1-8-2015 8-45-10 AM

Save the changes.

 

START MONIT SERVICE

Execute following command:

or this one:

To auto start Monit after reboot (on start-up), run following command:

You should now be able to can access the Monit web interface by using http://your-ip-address:2812. To do so, use the username ‘admin’ and password ‘monit’ (configured earlier).

If the page doesn’t load, it’s possible your CentOS 7 / RHEL 7 system has enabled FirewallD and you need to create an exception for port 2812. To do so, execute following command:

Note: remove –permanent if you want this only as a temporary firewall change (removed on reboot)

In order to apply the firewall rule, you’d need to restart the firewall service first:

 

You should now be able to access Monit web interface.

 

MONIT SERVICE MONITORING AND CONFIGURATION

Now that the web interface is working, we can configure Monit by placing service configuration files into /etc/monit.d/ directory.

I’ll explain the process on MariaDB service monitoring.

1. Navigate to etc/monit.d/ and create a new file called ‘mariadbmonitor’

2. Enter following content into above newly created file:

3. Save the ‘mariadbmonitor’ file

4. Run following command for Monit change to take effect:

5. Wait for 30 seconds and reload Monit web interface at http://your-ip-address:2812, you’ll now see that Monit is actively monitoring your MariaDB service

6. To test the change. Manually stop your MariaDB service using following command:

7. Wait for 30 seconds and if you see that Monit has automatically started MariaDB again, you’ve successfully completed installation and configuration of Monit on your CentOS 7 / RHEL 7 system.

 

MONIT CONFIGURATION FOR APACHE & SSH

To monitor Apache, navigate to etc/monit.d/ and create a new file called ‘apachemonitor’ with following content:

To monitor SSH, navigate to etc/monit.d/ and create a new file called ‘sshdmonitor’ with following content:

 

Now that Monit is successfully installed and configured to monitor Apache, MariaDB & SSH, you can visit Monit web interface at http://your-ip-address:2812, it should look like this:

1-8-2015 9-27-01 AM

You can also get the output from the command line by using following commands:

To see the status use:

Output should look like this:

 

 

1-8-2015 9-29-54 AM

To access a simplified summary page and confirm that processes are being monitored:

Use command:

Output will look something like this:

1-8-2015 9-30-08 AM

 

 

Facebook Comments