Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- # Low Disk Space Notifier
- # By Sophie F.
- #
- # Threshold percent
- # If percent usage > than $percent, notify
- percent=97
- # Email address to receive alert email
- mailto="root@Zenobia"
- # List of all block devices
- df_output=$(df -Ph | grep -E "/dev/sd.*|/dev/mmcblk.*")
- # df command output heading
- df_heading=$(df -lh | head -1)
- # Block devices (/dev/... column)
- dev_name=($(echo -e "$df_output" | awk '{ print $1 }'))
- # Mount points column
- mounts=($(echo -e "$df_output" | awk '{ print $6 }'))
- # Percent free space column
- free_space=($(echo -e "$df_output" | awk '{ print $5 }' | tr -d '%'))
- # For each line in $free_space, if $j > $percent, warn user
- for j in "${free_space[@]}"; do
- if [[ "$j" > "$percent" ]]; then
- # Used to match each line with used space > $percent
- line_num=$(echo "$df_output" | grep -inw "$j" | cut -d: -f1)
- # df line of drive info, with line number removed
- j_df_line=$(echo "$df_output" | sed -n $line_num'p' | cut -d: -f2-)
- # Percentage of used spaced
- j_used_space=$(echo -e "$df_output" | sed -n $line_num'p' | awk '{ print $5 }' | tr -d '%')
- # Percentage of free space
- j_free_space=$((100-$j_used_space))
- # Mount point of device
- j_mount_point=$(echo -e "$df_output" | sed -n $line_num'p' | awk '{ print $6 }')
- # Name of device (e.g. /dev/sda)
- j_dev_name=$(echo -e "$df_output" | sed -n $line_num'p' | awk '{ print $1 }' )
- echo "$0: $(hostname): $j_dev_name has $j_free_space% free space!"
- echo -e "$df_heading\n$j_df_line\n\n$(date)" | mail -s "$0: $(hostname): Low disk space on $j_dev_name" "$mailto"
- fi
- done
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment