|
1 | | -#!/bin/bash |
| 1 | +#!/usr/bin/env bash |
| 2 | +############################################################################### |
| 3 | +# Script Name : linux_disk_usage.sh |
| 4 | +# Description : Used to check the disk usage of mounted Linux filesystems and |
| 5 | +# return an alert if usage exceeds the specified thresholds. |
| 6 | +# (Default warning threshold is 75% and default error threshold |
| 7 | +# is 90%; thresholds can be changed via -w/--warning and -e/--error |
| 8 | +# flags or the WARNINGVALUE and ERRORVALUE environment variables.) |
| 9 | +# |
| 10 | +# Exit Codes : |
| 11 | +# 0 - OK (No thresholds exceeded) |
| 12 | +# 1 - Invalid input / script failure |
| 13 | +# 2 - WARNING (Warning threshold exceeded) |
| 14 | +# 3 - ERROR (Error threshold exceeded) |
| 15 | +# |
| 16 | +# Parameters : |
| 17 | +# -w, --warning <int> Warning threshold percentage (default: 75) |
| 18 | +# -e, --error <int> Error threshold percentage (default: 90) |
| 19 | +# |
| 20 | +# Environment : |
| 21 | +# WARNINGVALUE Alternative warning threshold |
| 22 | +# ERRORVALUE Alternative error threshold |
| 23 | +# |
| 24 | +# Examples : |
| 25 | +# ./linux_disk_usage.sh -w 70 -e 85 |
| 26 | +# WARNINGVALUE=70 ERRORVALUE=85 ./linux_disk_usage.sh |
| 27 | +# |
| 28 | +# Compatibility: |
| 29 | +# Linux (GNU coreutils) |
| 30 | +# |
| 31 | +# Repository : https://github.com/amidaware/community-scripts |
| 32 | +# Category : TRMM (nix):System Monitoring |
| 33 | +# Version : 1.0 |
| 34 | +# Last Updated: 2026-02-06 |
| 35 | +############################################################################### |
2 | 36 |
|
3 | | -# This script will check disk usage on Linux filesystems. It will give a warning if the usage is over the warning threshold and an error if the usage is over the error threshold |
4 | | -# The env variables "ERRORVALUE" and "WARNINGVALUE" should be passed in the script in the format: ERRORVALUE=xx and WARNINGVALUE=xx. |
5 | | -# If the env variables are not passed, the defaults of 90 75 will be applied |
| 37 | +# ------------------------------- Defaults ---------------------------------- # |
| 38 | +WARNINGVALUE="${WARNINGVALUE:-75}" |
| 39 | +ERRORVALUE="${ERRORVALUE:-90}" |
6 | 40 |
|
7 | | -if [ -z "$ERRORVALUE" ]; |
8 | | -then |
9 | | - ERRORVALUE=90 |
10 | | -fi |
| 41 | +# Filesystems to exclude (regex) |
| 42 | +EXCLUDE_FS_REGEX='^(tmpfs|devtmpfs|overlay|squashfs|ramfs|/dev/loop)' |
11 | 43 |
|
12 | | -if [ -z "$WARNINGVALUE" ]; |
13 | | -then |
14 | | - WARNINGVALUE=75 |
15 | | -fi |
| 44 | +# ----------------------------- Arg Parsing --------------------------------- # |
| 45 | +while [[ $# -gt 0 ]]; do |
| 46 | + case "$1" in |
| 47 | + -w|--warning) |
| 48 | + WARNINGVALUE="$2" |
| 49 | + shift 2 |
| 50 | + ;; |
| 51 | + -e|--error) |
| 52 | + ERRORVALUE="$2" |
| 53 | + shift 2 |
| 54 | + ;; |
| 55 | + *) |
| 56 | + echo "ERROR: Unknown argument: $1" |
| 57 | + exit 1 |
| 58 | + ;; |
| 59 | + esac |
| 60 | +done |
16 | 61 |
|
17 | | -# parse disk usage percentages |
18 | | -ERROROUTCODE=0 |
19 | | -WARNINGOUTCODE=0 |
20 | | -for line in $(df -hP | egrep '^/dev/[^loop]' | awk '{ print $1 "_:_" $5 }') |
21 | | -do |
22 | | - FILESYSTEM=$(echo "$line" | awk -F"_:_" '{ print $1 }') |
23 | | - DISK_USAGE=$(echo "$line" | awk -F"_:_" '{ print $2 }' | cut -d'%' -f1 ) |
24 | | - |
25 | | - if [ $DISK_USAGE -ge $ERRORVALUE ]; then |
26 | | - echo -e "Error!" "$FILESYSTEM is $DISK_USAGE% used. Error threshold is $ERRORVALUE%" |
27 | | - ERROROUTCODE=1 |
28 | | - elif [ $DISK_USAGE -ge $WARNINGVALUE ]; then |
29 | | - echo -e "Warning!" "$FILESYSTEM is $DISK_USAGE% used. Warning threshold is $WARNINGVALUE%" |
30 | | - WARNINGOUTCODE=1 |
31 | | - else |
32 | | - echo "$FILESYSTEM disk usage okay at $DISK_USAGE%" |
| 62 | +# ---------------------------- Validation ----------------------------------- # |
| 63 | +for val in "$WARNINGVALUE" "$ERRORVALUE"; do |
| 64 | + if ! [[ "$val" =~ ^[0-9]+$ ]]; then |
| 65 | + echo "ERROR: Threshold values must be integers" |
| 66 | + exit 1 |
33 | 67 | fi |
34 | 68 | done |
35 | 69 |
|
36 | | -if [ $ERROROUTCODE -gt 0 ]; then |
37 | | - echo "ERROR. One or more disks are critically low on space." |
| 70 | +if (( WARNINGVALUE >= ERRORVALUE )); then |
| 71 | + echo "ERROR: Warning threshold must be less than error threshold" |
38 | 72 | exit 1 |
39 | 73 | fi |
40 | | -if [ $WARNINGOUTCODE -gt 0 ]; then |
41 | | - echo "WARNING. One or more disks are getting low on space." |
42 | | - exit 2 |
43 | | -else |
44 | | - exit 0 |
45 | | -fi |
| 74 | + |
| 75 | +command -v df >/dev/null 2>&1 || { |
| 76 | + echo "ERROR: Required command 'df' not found" |
| 77 | + exit 1 |
| 78 | +} |
| 79 | + |
| 80 | +# ----------------------------- Execution ----------------------------------- # |
| 81 | +result=0 |
| 82 | + |
| 83 | +while IFS='|' read -r filesystem mountpoint used size usage; do |
| 84 | + usage="${usage%\%}" |
| 85 | + |
| 86 | + if (( usage >= ERRORVALUE )); then |
| 87 | + echo "ERROR: $filesystem mounted on $mountpoint is ${usage}% used (${used}/${size}) (>= ${ERRORVALUE}%)" |
| 88 | + result=3 |
| 89 | + elif (( usage >= WARNINGVALUE )); then |
| 90 | + [[ $result -lt 2 ]] && result=2 |
| 91 | + echo "WARNING: $filesystem mounted on $mountpoint is ${usage}% used (${used}/${size}) (>= ${WARNINGVALUE}%)" |
| 92 | + else |
| 93 | + echo "OK: $filesystem mounted on $mountpoint is ${usage}% used (${used}/${size})" |
| 94 | + fi |
| 95 | +done < <( |
| 96 | + df --output=source,fstype,target,used,size,pcent -h \ |
| 97 | + | tail -n +2 \ |
| 98 | + | awk -v exclude="$EXCLUDE_FS_REGEX" ' |
| 99 | + $1 ~ /^\/dev\// && |
| 100 | + $2 !~ exclude { |
| 101 | + print $1 "|" $3 "|" $4 "|" $5 "|" $6 |
| 102 | + } |
| 103 | + ' |
| 104 | +) |
| 105 | + |
| 106 | +exit "$result" |
0 commit comments