how to check disk space

Hi All,

pls go thru the below code and help me.

when i check "df -k" in my solaris system .. it will show like below..

fd                         0       0       0     0%    /dev/fd
/dev/dsk/c0t0d0s3    20171281 2319266 17650303    12%    /var
/dev/dsk/c0t0d0s4    10085260  443854 9540554     5%    /tmp
swap                 14565160      48 14565112     1%    /var/run
/dev/dsk/c0t2d0s5    30020038 7779608 21940230    27%    /backup
/dev/dsk/c0t0d0s6    30254974  492780 29459645     2%    /opt
/dev/dsk/c0t2d0s4    80681549 34435439 45439295    44%    /opt/product
/dev/dsk/c0t2d0s3    30254974  549801 29402624     2%    /opt/ulcm
/dev/dsk/c0t0d0s7    9848740 1349728 8400525    14%    /export/home

when i want to check the disk space.. if any disk reach more than or equal to 50% i want to print NOT OK. if its less than 50% i need to print "OK"

pls check the below code is correct... its not working.. kindly suggest me

code :

d=`df -k |awk '{print $5}' | egrep "[5-9][0-9]" | cut -c-2`
if [ "$d" == "50" ]
then
echo "DISK SPACE STATUS :NOT OK" >> /backup/stats/healthcheck/SCP1_BLU_HCsummary_$dt.txt
else
echo "DISK SPACE STATUS :OK" >> /backup/stats/healthcheck/SCP1_BLU_HCsummary_$dt.txt
fi

can anyone help on this issue pls..

thanks

Hi, steve, try this:

df -k | awk ' { if ($2>0 && $3/$2>=0.5) print "NOT OK"; else print "OK" } '

HI kevin,

i'm getting below error while i execute that in my solaris system.

bash-3.00$ df -k | awk ' { if ($2>0 && $3/$2>=0.5) print "NOT OK"; else print "OK" } '
awk: division by zero
 record number 1

Pls check and suggest me.

I guess the awk of your version does not support "short circuit". try this:

df -k | awk ' { if ($2>0) if ($3/$2>=0.5) print "NOT OK"; else print "OK" } '

Dear kevin

I'm getting same error.. is thr any other way to make it?

pls help/

bash-3.00$ df -k | awk '{ if ($2>0) if ($3/$2>=0.5) print "NOT OK"; else print "OK" }'
awk: division by zero
 record number 1

It's really strange. do you have more than one awk version on your system, try each of them with the following code.

df -k | awk ' $2>0 { if ($3/$2>=0.5) print "NOT OK"; else print "OK" } '

kevin,

.. its not working .. i tried with more awk versions. i'm getting same error in my solaris system,.

so.. could u modify below code.

#d=`df -k |awk '{print $5}' | egrep "[5-9][0-9]" | cut -c-2`
#if [ "$d" == "50" ]
#then
#echo "DISK SPACE STATUS :NOT OK"
#else
#echo "DISK SPACE STATUS :OK"
#fi

the marked part i want to convert into interger and check greater than or equal to...

could u make it? pls

OK, final solution:

df -k | awk ' { p=substr($5,1,index($5,"%")-1); if(p+0>=50) print "NOT OK"; else print "OK" }'

---------- Post updated at 09:24 AM ---------- Previous update was at 09:21 AM ----------

If you want to test equality of numbers in shell, you can use:

if [ "$d" -eq "50" ]

"if [ "$d" == "50" ]" is for testing equality of strings.

Thanks kevin... its working yar..... thank u so much.. but its showing like this..

bash-3.00$ df -k | awk ' { p=substr($5,1,index($5,"%")-1); if(p+0>=50) print "NOT OK"; else print "OK" }'
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
NOT OK
NOT OK
OK
NOT OK

because its checking all colums ..

bash-3.00$ df -k
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/dsk/c0t0d0s0    20171281 2580778 17388791    13%    /
/devices                   0       0       0     0%    /devices
ctfs                       0       0       0     0%    /system/contract
proc                       0       0       0     0%    /proc
mnttab                     0       0       0     0%    /etc/mnttab
swap                 11135968    1520 11134448     1%    /etc/svc/volatile
objfs                      0       0       0     0%    /system/object
sharefs                    0       0       0     0%    /etc/dfs/sharetab
/dev/dsk/c0t0d0s1    30254974 3235704 26716721    11%    /usr
/platform/SUNW,Sun-Fire-T200/lib/libc_psr/libc_psr_hwcap1.so.1
                     20171281 2580778 17388791    13%    /platform/sun4v/lib/libc_psr.so.1
/platform/SUNW,Sun-Fire-T200/lib/sparcv9/libc_psr/libc_psr_hwcap1.so.1
                     20171281 2580778 17388791    13%    /platform/sun4v/lib/sparcv9/libc_psr.so.1
fd                         0       0       0     0%    /dev/fd
/dev/dsk/c0t0d0s2    10085260 1686141 8298267    17%    /var
/dev/dsk/c0t2d0s0    10085260 2073979 7910429    21%    /tmp
swap                 11134496      48 11134448     1%    /var/run
/dev/dsk/c0t2d0s2    20171281  555909 19413660     3%    /opt
/dev/dsk/c0t2d0s5    9846556 2804805 6943286    29%    /backup
/dev/dsk/c0t0d0s4    70359009 55329888 14325531    80%    /oracle
/dev/dsk/c0t2d0s1    5043342 3772847 1220062    76%    /export/home
/dev/dsk/c0t2d0s4    30254974  628971 29323454     3%    /opt/ulcm
/dev/dsk/c0t2d0s3    65553610 55899965 8998109    87%    /opt/product

could u make it .. total output? if anyone is not oki.. i want to print NOT OK.

So you want to know which partition is "NOT OK", right?

df -k | awk ' { p=substr($5,1,index($5,"%")-1); if(p+0>=50) print $NF " is NOT OK, over " p "% of its available space is used."; else print $NF " is OK." }'

Thanks kevin...

i want to print only NOT OK partition like below

ex:
/oracle is NOT OK, over 80% of its available space is used.
/export/home is NOT OK, over 76% of its available space is used.
/opt/product is NOT OK, over 87% of its available space is used

thank you so much...

Then remove the "else print ..." part from the code.

There are at least two errors with your script.

  1. The if-line compares with exactly 50. But if your disk is 51%, it will not discover that.

  2. Your df-command line will display several lines, if several disks are above 50%, and then your script will not work

Hi, markkusi

For doubt 1, I don't quite understand what you mean. In my script I stripped "%" for the 5th column, and compare the number I get with 50, equals to or above it, it just works like a charm. why don't you think it would discovery that if the disk is used 51%?

For doubt 2, I am still confused.

I didn't have a Solaris system to test my script, so I just copied the input sample Steve posted, it all worked before I posted each response.

My remarks was about your first message:

d=`df -k |awk '{print $5}' | egrep "[5-9][0-9]" | cut -c-2`
if [ "$d" == "50" ]
then
echo "DISK SPACE STATUS :NOT OK" >> /backup/stats/healthcheck/SCP1_BLU_HCsummary_$dt.txt
else
echo "DISK SPACE STATUS :OK" >> /backup/stats/healthcheck/SCP1_BLU_HCsummary_$dt.txt
fi

The line: "if [ "$d" == "50" ]" is true only when the diskspace is exactly 50 % and then you get "DISK NOT OK".

When the disk is more (or less) then 50 %, you will get "DISK OK".

OK, I am Kevin, I am not Steve.