bash script to check left space on device
space on every partition from your hardisk
#!/bin/bash
PROG_NAME="space_usage"
#Set usage
USED_SPACE="95"
#log file if you want to use log file
LOG_FILE="space.txt"
#email address
EMAIL="user@localhost"
SEND_EMAIL="N"
#crontab every night at 12
#0 0 * * * root space.sh
function log()
{
/usr/bin/logger -i -p kern.crit -t ${PROG_NAME} "UPSS: ${i[5]} has
used more than $USED_SPACE % space"
echo "UPSS: ${i[5]} has used more than $USED_SPACE % space" >> $LOG_FILE
#send email
if [ $SEND_EMAIL == 'Y' ]
then
mail -s "no more space on ${i[5]} " $EMAIL
fi
}
#grep mounted partitions
df -k -P --sync |grep -v "Mounted" | while read i; do i=( ${i} )
#echo mounted partitions
#echo ${i[5]}
USED=`df ${i[5]} | awk '{print $5}' |grep -v "Use%" | cut -f1 -d%`
#echo $USED
#IS GREATEAR OR EQUAL
if [ "$USED" -ge $USED_SPACE ]
then
#this is for debugging
echo "no more space on: " ${i[5]}
#log message in /var/log/messages
log ${i[5]}
#go to spaceless partition
cd ${i[5]}
#rm all log files
#rm -rf *.log
echo "removed all *.log files from partition(s)" ${i[5]}
else
echo "ok" ${i[5]}
fi
done
exit $?
Comments