Guest User

diskspace.sh

a guest
Dec 30th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.62 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. # Low Disk Space Notifier
  4. #   By Sophie F.
  5. #
  6.  
  7. # Threshold percent
  8. # If percent usage > than $percent, notify
  9. percent=97
  10. # Email address to receive alert email
  11. mailto="root@Zenobia"
  12.  
  13. # List of all block devices
  14.     df_output=$(df -Ph | grep -E "/dev/sd.*|/dev/mmcblk.*")
  15. # df command output heading
  16.     df_heading=$(df -lh | head -1)
  17. # Block devices (/dev/... column)
  18.     dev_name=($(echo -e "$df_output" | awk '{ print $1 }'))
  19. # Mount points column
  20.     mounts=($(echo -e "$df_output" | awk '{ print $6 }'))
  21. # Percent free space column
  22.     free_space=($(echo -e "$df_output" | awk '{ print $5 }' | tr -d '%'))
  23.  
  24. # For each line in $free_space, if $j > $percent, warn user
  25.     for j in "${free_space[@]}"; do
  26.         if [[ "$j" > "$percent" ]]; then
  27.             # Used to match each line with used space > $percent
  28.             line_num=$(echo "$df_output" | grep -inw "$j" | cut -d: -f1)
  29.             # df line of drive info, with line number removed
  30.             j_df_line=$(echo "$df_output" | sed -n $line_num'p' | cut -d: -f2-)
  31.             # Percentage of used spaced
  32.             j_used_space=$(echo -e "$df_output" | sed -n $line_num'p' | awk '{ print $5 }' | tr -d '%')
  33.             # Percentage of free space
  34.             j_free_space=$((100-$j_used_space))
  35.             # Mount point of device
  36.             j_mount_point=$(echo -e "$df_output" | sed -n $line_num'p' | awk '{ print $6 }')
  37.             # Name of device (e.g. /dev/sda)
  38.             j_dev_name=$(echo -e "$df_output" | sed -n $line_num'p' | awk '{ print $1 }' )
  39.        
  40.             echo "$0: $(hostname): $j_dev_name has $j_free_space% free space!"
  41.             echo -e "$df_heading\n$j_df_line\n\n$(date)" | mail -s "$0: $(hostname): Low disk space on $j_dev_name" "$mailto"
  42.         fi
  43.     done
  44. exit 0
Advertisement
Add Comment
Please, Sign In to add comment