Monit System Monitoring
Monit can be used to monitor general system resources

Monit System Monitoring is a system for automatic management and pro-active monitoring of Information Technology Systems. 

  • Monit has built-in a lightweight HTTP(S) interface you can use to browse the Monit server and check the status of all monitored services. From the web-interface you can start, stop and restart processes and disable or enable monitoring of services.

Main Features

  • M/Monit uses Monit as an agent and can manage and monitor all your hosts and services. M/Monit can start a service if it does not run, restart a service if it does not respond and suspend a service if it uses too much resources.
  • Monitor system attributes such as CPU, Load, Memory, Disk usage, Files, Directories and Filesystems for changes on all your hosts. Conditional rules can be set and if a value goes outside a defined scope, specific actions can be executed and a notification sent.
  • Information is collected from the monitored systems and stored in a database. Drill-down and filter functions exist to investigate collected data. Status and events from each monitored system are automatically updated and displayed in charts and tables.

We will be installing Monit onto a CentOS 7 linux server.

The current stable version of Monit is 5.26.0 via YUM.

We will only be using Monit for local monitoring and will not be using the Monit web interface.

1. First we need to install the EPEL repository and then install Monit.

[hme@hme ~]# sudo yum install epel-release -y

Resolving Dependencies

--> Running transaction check

---> Package epel-release.noarch 0:7-11 will be installed

--> Finished Dependency Resolution

Dependencies Resolved

================================================================================

 Package                Arch             Version         Repository        Size

================================================================================

Installing:

 epel-release           noarch           7-11            extras            15 k

Transaction Summary

================================================================================

Install  1 Package

Total download size: 15 k

Installed size: 24 k

Downloading packages:

epel-release-7-11.noarch.rpm                               |  15 kB   00:00     

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

  Installing : epel-release-7-11.noarch                                     1/1 

  Verifying  : epel-release-7-11.noarch                                     1/1 

Installed:

  epel-release.noarch 0:7-11                                                    

Complete!

[hme@hme ~]# sudo yum install monit -y

Resolving Dependencies

--> Running transaction check

---> Package monit.x86_64 0:5.26.0-1.el7 will be installed

--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================

 Package                   Arch                       Version                           Repository                Size

=======================================================================================================================

Installing:

 monit                     x86_64                     5.26.0-1.el7                      epel                     321 k

Transaction Summary

=======================================================================================================================

Install  1 Package

Total download size: 321 k

Installed size: 822 k

Downloading packages:

monit-5.26.0-1.el7.x86_64.rpm                                                                   | 321 kB  00:00:00     

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

  Installing : monit-5.26.0-1.el7.x86_64                                                                           1/1 

  Verifying  : monit-5.26.0-1.el7.x86_64                                                                           1/1 

Installed:

  monit.x86_64 0:5.26.0-1.el7                                                                                          

Complete!

2. We can run the monit -V command to confirm the Monit version installed.

[hme@hme ~]# monit -V

This is Monit version 5.26.0

Built with ssl, with ipv6, with compression, with pam and with large files

Copyright (C) 2001-2019 Tildeslash Ltd. All Rights Reserved.

3. We can start and enable Monit with the following commands.

hme@hme ~]# sudo systemctl enable monit

Created symlink from /etc/systemd/system/multi-user.target.wants/monit.service to /usr/lib/systemd/system/monit.service.

[hme@hme ~]# sudo systemctl restart monit

[hme@hme ~]# sudo systemctl status monit

● monit.service - Pro-active monitoring utility for unix systems

   Loaded: loaded (/usr/lib/systemd/system/monit.service; enabled; vendor preset: disabled)

   Active: active (running) since Mon 2021-02-01 08:27:08 UTC; 3s ago

 Main PID: 26469 (monit)

   CGroup: /system.slice/monit.service

           └─26469 /usr/bin/monit -I

Feb 01 08:27:08 hme.hosting-made-easy.co.uk systemd[1]: Started Pro-active monitoring utility for unix systems.

Feb 01 08:27:08 hme.hosting-made-easy.co.uk monit[26469]: Starting Monit 5.26.0 daemon with http interface at [loca...2812

Feb 01 08:27:08 hme.hosting-made-easy.co.uk monit[26469]: 'hme.hosting-made-easy.co.uk' Monit 5.26.0 started

Feb 01 08:27:08 hme.hosting-made-easy.co.uk monit[26469]: 'hme.hosting-made-easy.co.uk' swap usage of 41.3% matches....0%]

Hint: Some lines were ellipsized, use -l to show in full.

4. We can check the Monit monitoring status with the following command.

From the screenshot below, we can see that Monit shows us the Status, Monitoring Status, Monitoring Mode, Load Average, CPU, Memory Usage, Swap Usage, Uptime, On-Reboot, Boot Time, and Data Collected.

[hme@hme ~]# sudo monit status

Monit 5.26.0 uptime: 0m

System 'hme.hosting-made-easy.co.uk'

  status                                  OK

  monitoring status            Monitored

  monitoring mode             active

  on reboot                            start

  load average                      [0.36] [0.14] [0.08]

  cpu                                       0.0%us 0.0%sy 0.0%wa

  memory usage                  248.1 MB [25.0%]

  swap usage                        211.3 MB [41.3%]

  uptime                                2d 12h 45m

  boot time                           Fri, 29 Jan 2021 19:54:10

  data collected                   Mon, 01 Feb 2021 08:39:51

5. We next need to configure Monit, the main config file location is /etc/monitrc

Bt default Monit checks the services every 30 seconds with set daemon 30, this can be changed to what every time period suits your application.

###############################################################################

## Global section

###############################################################################

##

## Start Monit in the background (run as a daemon):

#

set daemon  30              # check services at 30 seconds intervals

#   with start delay 240    # optional: delay the first check by 4-minutes (by

#                           # default Monit check immediately after Monit start)

#

#

6. We next need change the user and password, we don't have to as Monit is only running locally, but still good practice too.

If you would like to have access to Monit from outside of the server you would change the use address localhost to use address < your server ip >

set httpd port 2812 and

    use address localhost  # only accept connection from localhost (drop if you use M/Monit)

    allow localhost        # allow localhost to connect to the server and

    allow username:password      # require user 'admin' with password 'monit'

    #with ssl {            # enable SSL/TLS and set path to server certificate

    #    pemfile: /etc/ssl/certs/monit.pem

    #}

7. We next need to setup the email alerts to allow Monit to send emails out when we have server issues

## Monit by default uses the following format for alerts if the mail-format

statement is missing::

#

set mail-format {

from:    Monit <monit@$HOST>

subject: monit alert --  $EVENT $SERVICE

message: $EVENT Service $SERVICE

                 Date:        $DATE

                 Action:      $ACTION

                 Host:        $HOST

                 Description: $DESCRIPTION

            Your faithful employee,

            Monit

 }

##

#

set mailserver mail.example.com port 587                      # your email server

set mail-format { from: youremail@example.com }.      # email that will send all alerts

#

set alert youremail@example.com                                     # email that will receive all alerts

8. Once your Monit configuration files changes are complete, save and check your syntax with the following command.

[hme@hme ~]# sudo monit -t

Control file syntax OK

[hme@hme ~]# sudo monit reload

Reinitializing monit daemon

9. We next need to setup the services to be monitored, these files are created and located in /etc/monit.d/

[hme@hme ~]# cd /etc/monit.d/

[hme@hme monit.d]# 

10. The first file we create is the system monitoring configuration

[hme@hme monit.d]# sudo nano system

check system host$

if memory > 80% for 2 cycles then exec alert else if succeeded then exec alert

if swap > 30% for 2 cycles then exec alert else if succeeded then exec alert

if cpu > 80% for 2 cycles then exec alert else if succeeded then exec "alert

if loadavg (5min) > 3 for 2 cycles then exec alert else if succeeded then alert

11. The second file we create is the web server monitoring configuration

[hme@hme monit.d]# sudo nano apache

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

  start program "/usr/bin/systemctl start httpd.service" with timeout 15 seconds

  stop program "/usr/bin/systemctl stop httpd.service"

  if failed port 443 then restart

[hme@hme ~]# sudo monit -t

Control file syntax OK

[hme@hme ~]# sudo monit reload

Reinitializing monit daemon

12. The third file we create is the PHP-FPM monitoring configuration

[hme@hme monit.d]# sudo nano php-fpm

check process php-fpm with pidfile /var/run/php-fpm/php-fpm.pid

  group phpcgi

  start program = "/usr/sbin/service php-fpm start" with timeout 15 seconds

  stop program  = "/usr/sbin/service php-fpm stop"

  if failed unixsocket /var/run/php-fpm-www.conf.sock then restart

  if 3 restarts within 5 cycles then timeout

[hme@hme ~]# sudo monit -t

Control file syntax OK

[hme@hme ~]# sudo monit reload

Reinitializing monit daemon

13. The fourth file we create is the MariaDB monitoring configuration

[hme@hme monit.d]# sudo nano mariadb

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

    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 failed host 127.0.0.1 port 3306 then exec alert else if succeeded then exec alert

        if 5 restarts within 5 cycles then timeout

[hme@hme ~]# sudo monit -t

Control file syntax OK

[hme@hme ~]# sudo monit reload

Reinitializing monit daemon

14. Once your Monit configuration files changes are complete, save and check your syntax with the following command.

[hme@hme ~]# sudo monit -t

Control file syntax OK

[hme@hme ~]# sudo monit reload

Reinitializing monit daemon

And that’s the basics of getting Monit System Monitoring up and running on your CentOS 7 server. 

Monit website

Monit Git Repository


Looking for a custom solution?

Our technicians can provide you with the best custom made solutions on the market, no

matter whether you’re a small business or large enterprise.
Get In Touch
We provide cloud based enterprise hosting, server and storage solutions of unmatched quality. Feel free to visit or contact us for a custom quote.
Copyright © 2021 Hosting Made Easy. All Rights Reserved.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram