Check WordPress Folder Size with Monit + Email Alert

Monit is incredibly flexible. It can be used to send email alerts (guide with Mailgun) when processes crash and automatically restart them. On all the VPS and dedicated servers I set up for my clients on Codeable, Monit is a must for proactive monitoring.

I like to know how large WordPress installations are with all of the images and plugins used nowadays. This tutorial will show you how to display the size of your folders in Monit and alert you by email if they exceed a certain size.

Check Folder Size with Monit + Email Alert

Installation overview

  • Create the Monit check folder size script
  • Configure Monit to use the script

Create Monit Check Folder Size Script

Create a folder for scripts and the folder space check script

sudo mkdir -p /root/scripts
sudo nano /root/scripts/foldersizecheck.sh

Paste the script which checks your WordPress folder size with Monit.

#!/bin/bash

#capture first passed variable
FOLDER_PATH=$1

#capture second passed variable
REFERENCE_SIZE=$2

#calculate size of folder
SIZE=$(/usr/bin/du -s $FOLDER_PATH | /usr/bin/awk '{print $1}')

#convert size to MB
MBSIZE=$((SIZE / 1024))

#output size so Monit can capture it
echo "$FOLDER_PATH  -  $MBSIZE MB"

#provide status code for alert
if [[ $MBSIZE -gt $(( $REFERENCE_SIZE )) ]]; then
    exit 1
fi

Make the script executable

sudo chmod +x /root/scripts/foldersizecheck.sh

Run the script and pass the FOLDER_PATH variable (/var/www/guides.wp-bullet.com) as your WordPress folder and the REFERENCE_SIZE as 500 which is in megabytes.

sudo bash /root/scripts/foldersizecheck.sh /var/www/guides.wp-bullet.com/ 500

You will see this output with the size of your WordPress or WooCommerce folder, here 118

/var/www/guides.wp-bullet.com/  -  118 MB

Not we can configure Monit to use this custom folder size checking script.

Configure Monit to Check Folder Size

Assuming you have a modular system for creating Monit configurations, create the new snippet

sudo nano /etc/monit/conf.d/wordpressfoldersize

If the folder /var/www/guides.wp-bullet.com is greater than 500 MB we will get an email alert.

The every 200 cycles line means Monit will check every 200 x the Monit interval defined in /etc/monit/monitrc

check program WordPress
    with path "/root/scripts/foldersizecheck.sh /var/www/guides.wp-bullet.com/ 500"
    every 200 cycles
    if status != 0 then alert

Ctrl+X, Y and Enter to Save and Exit.

Test Monit syntax is valid with our new addition of disk space checking and alerting.

sudo monit -t

Reload Monit if there were no errors

sudo service monit reload

Check out your new custom program in the Monit web interface.

monit-folder-size-check

Sources

Workaround to Check Monit Folder
Division with Bash Variables

1 thought on “Check WordPress Folder Size with Monit + Email Alert”

Comments are closed.