Quota threshold

Hi,
I am trying to make a script in which the user is notified once the disk space of the environment increases a particular threshold.
I have made a script for it but I am facing an error while executing it.

Could any one here guide me further??

Script

#!/bin/sh
warninglimit=350000
lowlimit=250000
filesystems="/export /home/t2/"
for fs in $filesystems
do
size=`df -k $fs|grep $fs|awk '{ print $4; }'`
if [ $size -le $lowlimit ]
then
mailx -s "URGENT: Low disk space for $fs ($size)" -r abc@sd.com
break
fi
if [ $size -le $warninglimit ]
then
mailx -s "WARNING: Low disk space for $fs ($size)" -r abc@sd.com
fi
done

Error-
df: open of /export failed
test.sh[10]: test: Specify a parameter with this command.
test.sh[15]: test: Specify a parameter with this command.

Any help what so ever may be very useful for me.

Thanks ...

Taran

As it already says, the df for /export fails. So the variable $size will not contain anything useful.

Checking the line where you define $size, we see your variable $fs, being part of the list $filesystems.

The problem is, that the "for" doesn't get the values of $filesystems as single elements - it takes them as one. And there is no filesystem called "/export /home/ts".

So add the line

IFS=" "

to help for/do/done to parse the value of $filesystems as single values.

Example:

root@isau02:/etc/apt> filesystems="/export /home/t2/"
root@isau02:/etc/apt> for bla in ${filesystems}; do echo ${bla}; done
/export /home/t2/                                        # it takes it as a single value, ignoring the blank
root@isau02:/etc/apt> IFS=" "
root@isau02:/etc/apt> for bla in $filesystems; do echo $bla; done
/export
/home/t2/

You can debug your shell scripts easily by echoing variables in between and use options like "set -x" or "set -xv".

Hi,

thanks for replying...
I got the point that something is wrong with my loop.

Could you guide me the way around this problem which could solve my purpose...

Thanks

I did not just reply, I also guided you. Which part wasn't clear?

Hi,

I didn't understand your solution for the problem.

Kindly be a bit more comprehensive...

Thanks

There is no problem with your loop itself.
The value of your variable $filesystems is interpreted as 1 value, not 2 which are separated by a blank. So your for-loop will take it as 1 value instead of 2. To fix this, you can add the following line

IFS=" "

in the head of your script and try again.

Hi ,

Thanks for the reply.

I again tried by implementing your comment ,but still I am getting the same error...

One question for clarification: Do you want to test 2 filesystems (/export and /home/t2) or one (/export/home/t2)?

Put this line in the head of your script

set -x

and start it again. Then put the output here.

Just another dude chiming in to help.

  1. Taranjeet - I must second Zaxxon's request for more output (using set -x) - it really helps when you include your work in progress (output, descriptions) with your follow posts. It helps optimize the feedback loop, and also helps the helpers understand where you are coming from.
  2. Zaxxon I'm not so sure the problem is the for loop eating all arguments, I use that for quite a bit. Besides, T reported a "could not open" message, but on my system I saw "No such file or directory", I think it was a permissions problem.
  3. I think the best description of the problem with the script is it is not handling error conditions well. I've reposted your script with a few changes (in bold) that will hopefully both guide you to understanding and fix the problems you saw.
#!/bin/sh
set -x # turn on execution trace
warninglimit=350000
lowlimit=250000
filesystems="/export /home/t2/"
for fs in $filesystems
do
    # try the df separately and catch any errors
    dfoutput=`df -k $fs`
    if [ $? -ne 0 ] ; then
        echo "Error running 'df -k $fs', skipping" >&2
        continue
    fi
    size=`echo $dfoutput |grep $fs|awk '{ print $4; }'`
    if [ "$size" -le $lowlimit ]
    then
        echo mailx -s "URGENT: Low disk space for $fs ($size)" -r abc@sd.com
        # why do you want to stop
        # why not keep going?
        break
    fi
    if [ $size -le $warninglimit ]
    then
        echo mailx -s "WARNING: Low disk space for $fs ($size)" -r abc@sd.com
    fi
done

One thing I'll point out, notice the added double quotes around $size in the following line. This is another way to suppress the 'test' error messages you got, but it really just avoids the problem of the 'df' failing.

if [ "$size" -le $lowlimit ]

Hi All,

Thanks a lot for your suggestions.

I tried implementing the last one ,but it is still throwing an error while doing export.
I noted one thing very peculiar here,that it is not allowing me to export but it is still displaying the QUOTA of the environment.

Found it really strange.

Any suggestions/comments??

Thanks..

+ warninglimit=350000
+ lowlimit=250000
+ filesystems=/export /home/t2/
+ + df -k /export
df: open of /export failed
dfoutput=
+ [ 1 -ne 0 ]
+ echo Error running 'df -k /export', skipping
+ 1>& 2
Error running 'df -k /export', skipping
+ continue
+ + df -k /home/t2/
dfoutput=/home/t2 (/dev/vgt2_flat1/lvt2_home) : 71696384 total allocated Kb
19766688 free allocated Kb
51929696 used allocated Kb
72 % allocation used
+ [ 0 -ne 0 ]
+ + echo /home/t2 (/dev/vgt2_flat1/lvt2_home) : 71696384 total allocated Kb 19766688 free allocated Kb 51929696 used allocated Kb 72 % allocation used
+ awk { print $4; }
+ grep /home/t2/
size=
+ [ -le 250000 ]
+ echo mailx -s URGENT: Low disk space for /home/t2/ () -r abc@sd.com
mailx -s URGENT: Low disk space for /home/t2/ () -r abc@sd.com
+ break

Ok, try this one:

df -k | grep \/export

Show the output please.

@qneill
Just tried it out - you get no error when issuing df for a filesystem you don't have any rights for. I am about to see it exists at all on his system.

Hi ,
It didn't return any output.....

No output? Should come at least some error text. Anyway it seems you even don't have a filesystem with the mount point /export :eek:
Can you confirm this?

Sorry guys,there was no filesystem as export in my unix box.
I have changed the script and it executed all right.
But the problem now is that I didn't recieve any mail after the execution of the program.
The message which i echoed got displayed on my screen but I didn't get any mail.

Anyone here have any idea about what might be the reason for the same.

Anything wrong with my code or any permissions required??

As i am new to this mail stuff.

Plese guide me further.

You can't send a mail with no message like this. Put an echo | in front of it or echo some senseful message text via the pipe into mail.

Example:

echo | mailx -s "URGENT: Low disk space for /home/t2/ ()" -r abc@sd.com

or

echo "the subject says it all!"| mailx -s "URGENT: Low disk space for /home/t2/ ()" -r abc@sd.com

You should also enclose the subject wit double quotes - not sure if you did in your original script but I forgot them here and added them.

Check your junk emails. Sometimes the mails from the servers are delivered in the junk folder.

Hi Zaxxon ,

I think we have now hit the bulls eye..
Will let you know when I am finished with it.
Really appreciate your help in this regard..Thanks a lot....

Also, I looked at the code I posted, I had changed your "mailx ...." line into "echo mailx ...." so that particular version is just going to show you the output, not actually do anything. Taking the "echo" out will make it actually do something.
--
Quentin

Hi All,

Thanks for your help.

I am able to send mail alright ,but the problem is apart from the specified email id say abc@sd.com the mail is alos shot to root@sd.com,/@sd.com,/home@sd.com.
I don't want to send the email to any other person apart from the mail id specified in the script.

Can anyone here guide me for the same?

Thanks...