Shell Script To Check Disk Usage Is Out Of Space


#Shell Script To Check Disk Usage Is Out Of Space

    #!/bin/bash

    threshold=”20″ # This set threshold value

    i=2 #Counter, will be used later, set to 2, since first line in df output is description.

    result=`df -kh |grep -v “Filesystem” | awk ‘{ print $5 }’ | sed ‘s/%//g’` # Getting list of percentage of all disks, df -kh show all disk usage, grep -v – without description line, awk ‘{ print $5 }’ – we need only 5th value from line and sed ‘s/%//g’ – to remove % from result.

    for percent in $result; do # for every value in result we start loop.

    if ((percent > threshold)) # compare, if current value bigger than threshold, if yes next lines.
    then

    partition=`df -kh | head -$i | tail -1| awk ‘{print $1}’` # taking name of partition, here we use counter. Df list of all partitions, head – take only $i lines from top, tail -1 take only last line, awk ‘{print $1}’ – take only first value in line.

    echo “$partition at $(hostname -f) is ${percent}% full” #print to console – what partition and how much used in %.

    fi # end of if loop

    let i=$i+1 # counter increased by 1.

    done # end of for loop.

Output:-


    Filesystem Size Used Avail Use% Mounted on
    /dev/sda1 52G 4.7G 45G 10% /
    tmpfs 1.9G 0 1.9G 0% /lib/init/rw
    udev 1.9G 192K 1.9G 1% /dev
    tmpfs 1.9G 2.6M 1.9G 1% /dev/shm
    /dev/sda6 92G 22G 66G 25% /home

    Server:~/$ ./df_script.sh
    /dev/sda6 at yevhen.lviv.example.com is 25% full

No comments:

Post a Comment

Thank You for your Comments, We will read and response you soon...