Filesystem & ZFS Usage Alert Script for Unix/Linux

ADVERTISEMENT

Keeping a close watch on disk space usage is critical for avoiding service disruptions in production environments. This shell script provides a unified solution to monitor both standard filesystems (df) and ZFS pools (zpool) across Unix/Linux systems (e.g., RHEL, Ubuntu, Solaris, AIX).

🔔 The script triggers an email alert when:

  • Filesystem usage exceeds 90%
  • ZFS pool usage exceeds 80%

It automatically filters out system or temporary mounts and works across single-node or multi-mount environments. The script is lightweight, cron-friendly, and easy to integrate into your daily monitoring toolkit.

Script: disk_and_zfs_monitor.sh

################################################################################
# Title   : Unified Filesystem and ZFS usage monitoring script for Unix/Linux
# Script  : disk_and_zfs_monitor.sh
# Purpose : Alerts when filesystem or ZFS usage exceeds defined thresholds.
#           Supports most Unix/Linux systems.
#
# Features:
#   - Automatically skips virtual/system filesystems
#   - Includes optional ZFS usage check if zpool command exists
#   - Configurable thresholds and email recipient
#   - One script for all environments (except Windows)
#
# Author  : W3Buddy
# Version : 1.0
################################################################################

#!/bin/bash

# --- CONFIGURATIONS ---
EMAIL="info.w3buddy@gmail.com"
FS_THRESHOLD=90       # Filesystem alert threshold (%)
ZFS_THRESHOLD=80      # ZFS alert threshold (%)

HOST=$(hostname)

# --- Function: Check normal filesystems ---
check_filesystems() {
  df -hP | awk 'NR>1 && $6!~"^/boot|/dev|/run|/proc|/sys|/tmp|/var/tmp|/snap|/var/lib/docker|shm" {print $6, $5}' | sed 's/%//' | while read fs usage
  do
    if [ "$usage" -ge "$FS_THRESHOLD" ]; then
      echo -e "🚨 Filesystem '$fs' usage is high: ${usage}% on $HOST\n\n$(df -h $fs)\n" | \
      mailx -s "ALERT: Filesystem $fs on $HOST at ${usage}%" "$EMAIL"
    fi
  done
}

# --- Function: Check ZFS pools (if zpool exists) ---
check_zfs() {
  if command -v zpool >/dev/null 2>&1; then
    zpool list -H | awk '{print $1, $5}' | sed 's/%//' | while read pool usage
    do
      if [ "$usage" -ge "$ZFS_THRESHOLD" ]; then
        echo -e "🚨 ZFS Pool '$pool' usage is high: ${usage}% on $HOST\n\n$(zpool list $pool)\n" | \
        mailx -s "ALERT: ZPOOL $pool on $HOST at ${usage}%" "$EMAIL"
      fi
    done
  fi
}

# --- Main ---
check_filesystems
check_zfs

⚙️ Setup Instructions

# ----------------------------------------------
# 1. Create the script file
# ----------------------------------------------
mkdir -p /usr/local/scripts
cd /usr/local/scripts
vi disk_and_zfs_monitor.sh

# (Paste the full script above and save)

# ----------------------------------------------
# 2. Make it executable
# ----------------------------------------------
chmod +x disk_and_zfs_monitor.sh

# ----------------------------------------------
# 3. Test manually
# ----------------------------------------------
./disk_and_zfs_monitor.sh

# ----------------------------------------------
# 4. Schedule with cron (hourly or as needed)
# ----------------------------------------------
crontab -e

# Add the following entry:
00 * * * * /usr/local/scripts/disk_and_zfs_monitor.sh >> /var/log/fs_zfs_monitor.log 2>&1

# ----------------------------------------------
# 5. Confirm cron is scheduled
# ----------------------------------------------
crontab -l

ADVERTISEMENT