Unix space checking script

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Unix space checking script
# 8  
Old 02-04-2011
Thanks alot scottn
so how about this ?

Code:
if [ -d directory ]
then
df -hs directoryname
else 
echo Directory not found
fi

# 9  
Old 02-04-2011
-s is not an option to df (there's nothing to summarise).

Your script also expects two arguments (call them $1 and $2), and you need to make sense of the df output to get what you need.

Code:
if [ -d "$1" ]; then
  FREE=$(df -k "$1" | <some parsing to get free space field>)
  [ $FREE -le $2 ] && echo Not enough space
else
  echo Directory does not exist

(I don't think using -h is so great. It will change depending on how much space is available (could be in GB, MB, etc.))

bash (search for positional parameters)
df

Things that may help you extract the information you need:
awk
sed
This User Gave Thanks to Scott For This Post:
# 10  
Old 02-04-2011
I tried re-doing it, please tell me what you think and if you can find mistakes
I do a space check as follows basing my alert on available,

Code:
for dspace in `df -k /filesystem |tail -1 |awk '{print $1}'`
do
if [ $dspace -le "80" ]
then 
echo $dspace % available 
else 
df -k /filesystem > /tmp/fsspace 
cat /usr/local/bin/fswarn.txt /tmp/fsspace > /usr/local/bin/fswarning.txt 
/usr/bin/mailx -s "FS exceeded 80% and is at $dspace % FULL needs attention" mymail@mydomain.tld < /usr/local/bin/export
warning.txt
fi

# 11  
Old 02-04-2011
I think you're doing great Smilie

There's a couple of things.

Code:
for dspace in `df -k /filesystem |tail -1 |awk '{print $1}'`

Your df here takes the last line (tail -1) and prints the first field (awk '{print $1}'. That should leave you with a single word, so the for-loop isn't really necessary.

And the output of df, which varies from system to system, probably wouldn't show the space free in the first field.

On my system (OS X), for example:
Code:
/Users/scott/scripts/tmp $ df -k .                           
Filesystem   1024-blocks      Used Available Capacity  Mounted on
/dev/disk0s2   976426672 838053612 138117060    86%    /

the available space would be in "$4", and the used space as a percentage in "$5".

You're also "catting" a couple of files in the else part of your if statement. Where do they come from?

Assuming that /usr/local/fswarn.txt exists, and that /usr/local/bin/exportwarning.txt should be /usr/local/bin/fswarning.txt (!), you could simplify that a bit.

Code:
fsfile=/tmp/fsspace.txt  # set it here, so I don't have to change it in more than one place
df -k $1 > $fsfile
dspace=$(tail -1 $fsfile | awk '{print 100-$5}')  #100% - % used = % available
if [ $dspace -lt $2 ]; then
  echo "$dspace % available"
else
   cat /usr/local/bin/fswarn.txt /tmp/fsspace | /usr/bin/mailx -s "FS exceeded $2% ....."  mymail@mydomain.tld
fi

It still needs work (primarily checking that the arguments are given, and valid, but I'm sure you can handle that Smilie
This User Gave Thanks to Scott For This Post:
# 12  
Old 02-04-2011
first of all thx a lot scottn Smilie your help if really great !!!
Your giving me confidence to continue trying to do this Smilie
So here is attempt number 3 I wrote that when waiting for a previous answer and don't want it to go to waste so here goes :

Assuming parameter '1' is a mount point and parameter '2' is a percent "free", then :

Code:
if [ -d "$1" ]; then
  FREE=$(df -k $1 | |awk 'NR==2{print 100-substr($5,1,length($5)-1);}')
  [ $FREE -le $2 ] && echo "Not enough space, ${FREE}% left"
else
  echo "Directory $1 does not exist"
fi

# 13  
Old 02-04-2011
Getting there!

NR==2 isn't perfect. Some df's can output on three lines.

On Linux, using -P (the so-called "POSIX" option) would show the output on two lines.

i.e.
Code:
# ssh root@oraserv1
/ # cd /oracle
# df .
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/ora001vg-ora001lv
                       8252856   4752396   3081236  61% /oracle

/oracle # df -P .
Filesystem         1024-blocks      Used Available Capacity Mounted on
/dev/mapper/ora001vg-ora001lv   8252856   4752396   3081236      61% /oracle

tail is good. sed can also get the last line:
Code:
/oracle # df . | sed '$!d'  # from the link, search "print the last line of a file"
                       8252856   4752396   3081236  61% /oracle

And with awk, there's no need to remove the %. It'll do that for you when it know's it's a number.
Code:
$ echo 22% | awk '{print 100-$1}'  
78

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX script to find alerts for swap space

Can someone get me script to find swap space for linux servers (1 Reply)
Discussion started by: Moon1234
1 Replies

2. Shell Programming and Scripting

Help needed to create a UNIX Space monitoring script

Hi All, Its urgent.. pls help me out.. I want to create a KSH which should generate a report with the list of users and the files larger than 5 GB created by them in a direcorty and send autogenerated e-mail to them. my input would be users list,directory path and the file size (say 5 GB) ... (11 Replies)
Discussion started by: anman0523
11 Replies

3. Solaris

UNIX Checking Tape Space

Hello. My name is Alex and I am new to the UNIX environment. One of the things that I do on a daily basis is I perform backups to tape on a Sun Ultra 25. I use DAT72 tape. The tape that is currently in the tape drive has about five database backups within it already. I was just hoping... (19 Replies)
Discussion started by: daddy.torres
19 Replies

4. UNIX for Advanced & Expert Users

Checking missing data's sequence (shell script | UNIX command)

Dear All members, i have some trouble here, i want to ask your help. The case is: I have some data, it's like: -ABCD1234 -ABCD1235 -ABCD1237 -BCDE1111 -BCDE1112 -BCDE1114 there is some missing data's sequence (the format is: ABCD = name 1234 = sequence). I want to print the... (2 Replies)
Discussion started by: septian.tri
2 Replies

5. UNIX for Advanced & Expert Users

Checking the space for /archlog mount point script

I have the below shell script which is checking /archlog mount point space on cappire(solaris 10) server. When the space usage is above 80% it should e-mail. When i tested this script it is working as expected. #!/usr/bin/ksh export MAIL_LIST="tlr.voora@zamn.com" export ARCH_STATUS=`df -k... (1 Reply)
Discussion started by: dreams5617
1 Replies

6. Shell Programming and Scripting

Checking the space for /archlog mount point script

I have the below shell script which is checking /archlog mount point space on cappire(solaris 10) server. When the space usage is above 80% it should e-mail. When i tested this script it is working as expected. -------------------------------------------------------------------------... (0 Replies)
Discussion started by: dreams5617
0 Replies

7. Shell Programming and Scripting

Unix script for Checking sequence

Hello, I need Unix script for Checking sequence and get output in a file for missing sequences information. We are moving archive log to a server for DR .if any files miss from sequence DR will fails. so we need script to monitor sequence of files which are FTP from the Production servers .... (2 Replies)
Discussion started by: Rata_raj
2 Replies

8. UNIX for Dummies Questions & Answers

Problem in checking space of mount.Please help me

Hi everyone, I am writing a script. As a part of this script, I wanted to check the space of few mounts, If the space usage percentage of the mount crosses over a certain limit then i wanted to display a warning message. The following is the command df -k | awk ' { if (($6 == "/export/temp")... (4 Replies)
Discussion started by: Sheethal
4 Replies

9. Shell Programming and Scripting

Checking availability of Web pages through Unix script

Hi Guru's, I need to check the availability of a web page for every one hour through a script. Following is the requirement. 1. Go to http://vsn.srivsn.com 2. If any error encountered in openeing the home page, it should trigger an email with the appropriate error message. 3. If page opens... (6 Replies)
Discussion started by: srivsn
6 Replies

10. UNIX for Dummies Questions & Answers

checking for a space in a file name

hi members... another new probs... i need to check all the zip files in the server and check for a space or "`" in the file name and if found need to remove them and form a zip file. i tried using grep command to check for a space but with out success also when i tried checking for "`" as... (6 Replies)
Discussion started by: madhu_aqua14
6 Replies
Login or Register to Ask a Question