Shell script to check disk usage on Linux and Unix


You can get a useful script written on Korn shell to check mount point usage and generate an alert by sending e-mail for  Linux and  Unix system.

Which Platform  tested:

  • Linux (change  “bdf” command  with “df”)
  • HP-UX

Script  Output:

# ksh /tmp/allx

Alert Date          Hostname            Alert Type          Mount Point

----------------    ----------------    ------------------  ------------------

12-07-2015-13:15    xx12kl00            Disk Usage           85%:/orxabkp  

#!/usr/bin/ksh
##Add mount points which you need to exclude with threshold.
SystemFile() {
cat <<_list
/orabkp 95
_list
}
##main Fonctuion to chek disk usage
SystemFile_control(){

ServiceList="$(mktemp)"
SystemFile > $ServiceList
srv=""
option=""
while read srv option
do
TMP=`echo $i|cut -d '%' -f 1,1`
TMPX=`echo $i`
TMPX2=`echo $i|cut -d ':' -f 2,2`
if [[ $TMP -ge 70 && $TMPX2 != $srv ]]
then
DEG=$(( $DEG + 1 ))
DEG_TMP=`echo "$DEG_TMP $TMPX "`
else
if [[ $TMP -ge $option ]]
then
DEG=$(( $DEG + 1 ))
DEG_TMP=`echo "$DEG_TMP $TMPX "`
fi
fi
done < $ServiceList
rm -f $ServiceList
}
hostNAME=`hostname`
NOW=$(date +"%m-%d-%Y-%H:%M")
MAIL_SUB=`echo "$hostNAME Disk Usage"`
DEG=0
##Sort all mount points for check usage of them.If you 'll use  this script  on  linux server  change "bdf" with "df"
for i in $(bdf|grep '%'|awk '{n = 2; for (--n; n >= 0; n--){ printf "%s:",$(NF-n)} print ""}'|grep -v Mounted|cut -d ':' -f 1,2)
do
SystemFile_control
done
##Send e-mail
if [[ $DEG != 0 ]]
then
echo "$hostNAME server disk usage problem!\n" >/tmp/allcheck
printf "%-20s%-20s%-20s%-20s\n" "Alert Date" "Hostname" "Alert Type" "Mount Point" >> /tmp/allcheck
printf "%-20s%-20s%-20s%-20s\n" "----------------" "----------------" "------------------" "------------------" >> /tmp/allcheck
printf "%-20s%-20s%-20s%-20s\n" "$NOW" "`hostname`" "Disk Usage" "$DEG_TMP" >> /tmp/allcheck
cat /tmp/allcheck|/usr/bin/mailx -s "$MAIL_SUB" abdurrahim.yildirim@casesup.com
fi

 

I'm a IT Infrastructure and Operations Architect with extensive experience and administration skills and works for Turk Telekom. I provide hardware and software support for the IT Infrastructure and Operations tasks.

205 Total Posts
Follow Me