{"id":4286,"date":"2025-06-17T09:16:50","date_gmt":"2025-06-17T09:16:50","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=4286"},"modified":"2025-06-17T09:16:51","modified_gmt":"2025-06-17T09:16:51","slug":"filesystem-zfs-usage-alert-script-for-unix-linux","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/shell-scripts\/filesystem-zfs-usage-alert-script-for-unix-linux\/","title":{"rendered":"Filesystem &amp; ZFS Usage Alert Script for Unix\/Linux"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Keeping a close watch on disk space usage is critical for avoiding service disruptions in production environments. This shell script provides a <strong>unified solution<\/strong> to monitor both standard filesystems (<code>df<\/code>) and ZFS pools (<code>zpool<\/code>) across <strong>Unix\/Linux systems<\/strong> (e.g., RHEL, Ubuntu, Solaris, AIX).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd14 The script triggers an <strong>email alert<\/strong> when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Filesystem usage exceeds <strong>90%<\/strong><\/li>\n\n\n\n<li>ZFS pool usage exceeds <strong>80%<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It automatically filters out system or temporary mounts and works across <strong>single-node or multi-mount environments<\/strong>. The script is lightweight, cron-friendly, and easy to integrate into your daily monitoring toolkit.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Script: <code>disk_and_zfs_monitor.sh<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>################################################################################\n# Title   : Unified Filesystem and ZFS usage monitoring script for Unix\/Linux\n# Script  : disk_and_zfs_monitor.sh\n# Purpose : Alerts when filesystem or ZFS usage exceeds defined thresholds.\n#           Supports most Unix\/Linux systems.\n#\n# Features:\n#   - Automatically skips virtual\/system filesystems\n#   - Includes optional ZFS usage check if zpool command exists\n#   - Configurable thresholds and email recipient\n#   - One script for all environments (except Windows)\n#\n# Author  : W3Buddy\n# Version : 1.0\n################################################################################\n\n#!\/bin\/bash\n\n# --- CONFIGURATIONS ---\nEMAIL=\"info.w3buddy@gmail.com\"\nFS_THRESHOLD=90       # Filesystem alert threshold (%)\nZFS_THRESHOLD=80      # ZFS alert threshold (%)\n\nHOST=$(hostname)\n\n# --- Function: Check normal filesystems ---\ncheck_filesystems() {\n  df -hP | awk 'NR>1 &amp;&amp; $6!~\"^\/boot|\/dev|\/run|\/proc|\/sys|\/tmp|\/var\/tmp|\/snap|\/var\/lib\/docker|shm\" {print $6, $5}' | sed 's\/%\/\/' | while read fs usage\n  do\n    if &#91; \"$usage\" -ge \"$FS_THRESHOLD\" ]; then\n      echo -e \"\ud83d\udea8 Filesystem '$fs' usage is high: ${usage}% on $HOST\\n\\n$(df -h $fs)\\n\" | \\\n      mailx -s \"ALERT: Filesystem $fs on $HOST at ${usage}%\" \"$EMAIL\"\n    fi\n  done\n}\n\n# --- Function: Check ZFS pools (if zpool exists) ---\ncheck_zfs() {\n  if command -v zpool >\/dev\/null 2>&amp;1; then\n    zpool list -H | awk '{print $1, $5}' | sed 's\/%\/\/' | while read pool usage\n    do\n      if &#91; \"$usage\" -ge \"$ZFS_THRESHOLD\" ]; then\n        echo -e \"\ud83d\udea8 ZFS Pool '$pool' usage is high: ${usage}% on $HOST\\n\\n$(zpool list $pool)\\n\" | \\\n        mailx -s \"ALERT: ZPOOL $pool on $HOST at ${usage}%\" \"$EMAIL\"\n      fi\n    done\n  fi\n}\n\n# --- Main ---\ncheck_filesystems\ncheck_zfs<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f Setup Instructions<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># ----------------------------------------------\n# 1. Create the script file\n# ----------------------------------------------\nmkdir -p \/usr\/local\/scripts\ncd \/usr\/local\/scripts\nvi disk_and_zfs_monitor.sh\n\n# (Paste the full script above and save)\n\n# ----------------------------------------------\n# 2. Make it executable\n# ----------------------------------------------\nchmod +x disk_and_zfs_monitor.sh\n\n# ----------------------------------------------\n# 3. Test manually\n# ----------------------------------------------\n.\/disk_and_zfs_monitor.sh\n\n# ----------------------------------------------\n# 4. Schedule with cron (hourly or as needed)\n# ----------------------------------------------\ncrontab -e\n\n# Add the following entry:\n00 * * * * \/usr\/local\/scripts\/disk_and_zfs_monitor.sh >> \/var\/log\/fs_zfs_monitor.log 2>&amp;1\n\n# ----------------------------------------------\n# 5. Confirm cron is scheduled\n# ----------------------------------------------\ncrontab -l<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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). \ud83d\udd14 The script triggers an email alert when: It automatically filters out system [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,990],"class_list":["post-4286","cposts","type-cposts","status-publish","hentry","category-notes","category-shell-scripts"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/4286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=4286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=4286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}