Last active
September 6, 2024 06:57
-
-
Save duzun/bb9378b38f6b18b1b725ae4260ca726b to your computer and use it in GitHub Desktop.
Check disk space usage on all mounted partitions and email when over the thresholds.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Check disk space usage on all mounted partitions | |
# and email when over the thresholds. | |
# | |
# @author Dumitru Uzun ([email protected]) | |
# @version 1.0.0 | |
# @src https://gist.github.com/duzun/bb9378b38f6b18b1b725ae4260ca726b | |
# | |
# Usage: | |
# check_free_disk_space.sh | |
# Display disk usage for all partitions | |
# | |
# check_free_disk_space.sh [email protected] | |
# If usage over threshold, send a report to [email protected] | |
# | |
# The thresholds | |
THRESHOLD_USE=${THRESHOLD_USE:-85} | |
THRESHOLD_INODES=${THRESHOLD_INODES:-80} | |
# For internal usage: | |
declare -A MOUNT | |
declare -A FREE_SPACE | |
declare -A SPACE_USE | |
declare -A INODES | |
declare -a over_threashold=() | |
main() { | |
local email="${1}" | |
local key | |
init_df | |
for key in "${!MOUNT[@]}"; do | |
if [ "${SPACE_USE[$key]}" -gt "$THRESHOLD_USE" ] || \ | |
{ [ "${INODES[$key]}" != "-" ] && [ "${INODES[$key]}" -gt "$THRESHOLD_INODES" ]; }; | |
then | |
over_threashold+=("$key") | |
fi | |
done | |
show_info "${!MOUNT[@]}" | |
[ "${#over_threashold[@]}" -eq 0 ] && return; | |
echo "Over threshold: " | |
for key in "${over_threashold[@]}"; do | |
echo -e "\t${MOUNT[$key]} ($key)"; | |
done | sort | |
echo | |
if [ -n "$email" ]; then | |
send_email "$email" "${over_threashold[@]}" | |
else | |
return 1 | |
fi | |
} | |
show_info() { | |
local exiding | |
local key | |
if [ "$1" = "-e" ]; then | |
exiding=1 | |
shift | |
fi | |
for key in "$@"; do | |
echo | |
echo -e "${MOUNT[$key]} \t($key):" | |
echo -e "\tSpace: ${SPACE_USE[$key]}% used, ${FREE_SPACE[$key]} free" | |
[ "${INODES[$key]}" = "-" ] || \ | |
echo -e "\tInodes: ${INODES[$key]}% used" | |
if [ -n "$exiding" ]; then | |
if [ "${SPACE_USE[$key]}" -gt "$THRESHOLD_USE" ]; then | |
echo | |
echo "Disk Usage is:" | |
sizes_l1 "${MOUNT[$key]}" 2> /dev/null | |
echo | |
fi | |
if [ "${INODES[$key]}" != "-" ] && [ "${INODES[$key]}" -gt "$THRESHOLD_INODES" ]; then | |
echo | |
echo "Inodes usage is:" | |
nr_files_l1 "${MOUNT[$key]}" 2> /dev/null | |
echo | |
fi | |
fi | |
done | |
echo | |
} | |
init_df() { | |
local line | |
while read -r line; do | |
set -- $line | |
[ -z "${MOUNT[$1]}" ] && MOUNT[$1]=$6 | |
FREE_SPACE[$1]=$4 | |
SPACE_USE[$1]=${5//%/} | |
done < <(_df -h) | |
while read -r line; do | |
set -- $line | |
INODES[$1]=${5//%/} | |
done < <(_df -hi) | |
} | |
dev_ip() { | |
ip route show | grep -v br- | grep -v docker | grep src | \ | |
tr " " '\n' | grep -v '^$' | tail -1 | |
} | |
_df() { | |
df -x tmpfs -x devtmpfs -x overlay -x efivarfs \ | |
"$@" | tail -n +2 | sort | |
} | |
nr_files_l1() { | |
local dir="${1:?}" | |
dir="$(realpath "$dir")/" | |
# dir="${dir//\/\//\/}" | |
local len="${#dir}" | |
find "$dir" -xdev -type f | \ | |
cut -c "$len-" | cut -d "/" -f 2 | \ | |
sort | uniq -c | sort -nr | |
} | |
sizes_l1() { | |
local dir="${1:?}" | |
[ "$dir" = "/" ] || dir="$dir/" | |
du -xsh "$dir"* | |
} | |
send_email() { | |
local from ip | |
local email="${1:?}" | |
shift | |
from="$(id -nu)@$(hostname)" | |
ip=$(dev_ip) | |
sendmail -f "$from" "$email" << EOF | |
Subject: Disk Space Alert - $from $ENV | |
Your disk free space is critically low! | |
IP address of device: $ip | |
Hostname: $(hostname) | |
$(show_info -e "$@") | |
EOF | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment