Monitor Server Resources with Monit

Introduction

In this post I will intoroduce Monit and show a few ways how to monitor various server resources like Disk usage etc.

About Monit

Monit is a free and open source process monitoring tool that can be used to monitor system process.

You can monitor, for size and permission changes, following:

  • files,
  • directories,
  • devices.

Monit also able to monitor common services like:

  • Apache,
  • Nginx,
  • FTP,
  • SSH,
  • MySQL,
  • etc.

You can monitor remote hosts by:

  • TCP/IP ports,
  • protocols,
  • ping.

Monit has an ability to stop a specific process that has high resource usage. Monit keep watch on your server and send alerts about any critical error conditions and recovery status.

Installation and Setup

This is how to install and configure Monit on a Debian/Ubuntu server.

Install monit:

By default, Monit is on Debian repository, and we can install it with command:

apt-get install monit

Enable and Start Monit

Before Monit is started the first time, you can test the control file for syntax errors:

 $ monit -t
 $ Control file syntax OK

If there was an error, Monit will print an error message to the console, including the line number and where the error was found.

After installatin start Monit service and enable it to start on boot time:

systemctl start monit
systemctl enable monit

Display monit details

We can see status of all the services with the following command:

monit status

After instalalation, we will add processes to config file

Monit default configuration file monitrc is located at /etc/monit/ directory and other process and server specific configuration files are located at /etc/monit/conf-available/ directory. It is possible to include additional configuration parts from other files or directories.

nano /etc/monit/monitrc

Check if monit is running on port 2812

We will check port with netstat command.

netstat -tlnp | grep :2812

Monitoring Disk Usage

Adding file to conf.d to configurate monit for disk monitoring

$ nano /etc/monit/conf.d/disk

Adding script to disk file so we want to for monitoring disk space

check filesystem rootfs with path /
if space usage > 80% then alert

Monitoring ICMP

Add new file for ICMP monitoring

$ nano /etc/monit/conf.d/icmp

Add script to icmp file

 check host www.example.ba with address www.example.ba
       if failed
          icmp type echo count 30 with timeout 60 seconds
       then alert

Add service monitoring

We will add Nginx service

Add file for nginx monitoring

$ nano /etc/monit/conf.d/nginx

Add script to nginx file

check process NginX with pidfile /run/nginx.pid
       start "/etc/init.d/nginx start"
       stop  "/etc/init.d/nginx stop"
       if failed host 127.0.0.1 port 80 then alert
       if failed host 127.0.0.1 port 80 then restart
       if 5 restarts within 5 cycles then timeout

Add Memory and Cpu monitoring

Add file to monit conf folder:

$ nano /etc/monit/conf.d/ramcpu

Add script to ramcpu file:

check System domain.com
    if memory usage > 75% then alert
    if cpu usage (user) > 80% then alert
    if cpu usage (system) > 30% then alert
    if cpu usage (wait) > 20% then alert

Setting up email notifications

Setting account because we want to got mails

set mailserver smtp.gmail.com port 587
    username "username@mail.com" password "pasword"
    using tlsv1
    with timeout 30 second

Formatting mail

set mail-format {
   from:    Monit <monit@$HOST>
   subject: monit alert --  Nomimono Server Full Disk
   message: $EVENT Service $SERVICE
                 Date:        $DATE
                 Action:      $ACTION
                 Host:        $HOST
                 Description: $DESCRIPTION

            Sent from monit
 }

set alert (mail of recipient)

Reload monit

$ systemctl reload monit.service

Testing disk usage

To test monit we have to generate big files until the disk usage is over 80%

$ cat /dev/urandom > big_file

or

$ fallocate -l 50G big_file

This way, we manually triggered the monit disk usage check and we should get an email notification about disk usage being over 80%.