#!/bin/bash PATH=$PATH:/sbin [ "$TERM" = "dumb" -a ! -t 0 ] && run=cron || run=local [ "`ps --no-headers o comm $PPID`" = "sshd" ] && run=SSH debug=0 [ "$run" = "SSH" ] && debug=1 [ "$run" = "local" ] && debug=2 recipient=username # Add increment1% if we're above threshold1%, but add increment2% if we're above threshold2% full threshold1=85 increment1=5 threshold2=90 increment2=10 [ -n "$1" ] && debug=$1 # Scan for all Volume Groups vgs --noheadings | tr -s ' ' | cut -d' ' -f2 | while read vg do [ "$debug" -gt "0" ] && echo "Checking Volume Group $vg" # Scan for Logical Volumes in each Group lvs --noheadings $vg | tr -s ' ' | cut -d' ' -f2 | while read lv do [ "$debug" -gt "0" ] && echo "Checking Logical Volume $lv" # Check if LV is mounted percent=`df -h | grep "/dev/mapper/$vg-$lv " | sed 's/.* \([0-9]\+\)%.*/\1/'` if [ -n "$percent" ] # Yes we have a mounted LV then [ "$debug" -gt "0" ] && echo "Logical Volume $vg/$lv is mounted and is $percent% full" # Is it more than threshold1% full? if [ "$percent" -gt "$threshold1" ] then # Add increment1% if we're above threshold1%, but add increment2% if we're above threshold2% full incr=$increment1 [ "$percent" -gt "$threshold2" ] && incr=$increment2 if ps ax | grep -v grep | grep -q "resize2fs /dev/mapper/$vg-$lv " then echo "resize operation already running" | mail -s "Would increase /dev/$vg/$lv by $incr%" $recipient else ( time ( df -h | grep "/dev/mapper/$vg-$lv "; echo; lvextend -l +$incr%LV -r /dev/$vg/$lv 2>&1; df -h | grep "/dev/mapper/$vg-$lv "; echo; vgs ) ) |& mail -s "Increasing /dev/$vg/$lv by $incr%" $recipient fi else [ "$debug" -gt "0" ] && echo "Partition is ${percent}% full" | mail -s "/dev/$vg/$lv does not need extending at present." $recipient fi else [ "$debug" -gt "0" ] && echo "Logical Volume $vg/$lv is not mounted" fi done done