Bash Script to Monitor Space by Directory.

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Thu Dec 10 23:52:45 UTC 2009


On Thu, 10 Dec 2009, Dennis Antle wrote:

> Thanks Kevin,
> I'm currently using cron to dump the results of du into a text file each night. Format of the text file is   
> 
> size    /directory/path
> size    /directory/path/subdir/
> 
> Where I run into trouble is when compareing yesterdays text file to
> todays to detect if a directory has grown by over a GB. If a
> directory has grown more than a GB then I want the script to email
> me.
>
> I have a working script, but it takes to long to process on large
> volumes. So what I need is an efficient way to loop through the
> lines in the text files.

   The time is taken by all those calls to sed (let read do the
   splitting or use shell parameter expansion) and using expr for
   arithmetic (the shell can do that itself). 

> FILE2=$(date --date="1 days ago" +%Y%m%d).arc
> showopts () {
>   while getopts ":pq:" optname
>     do
>       case "$optname" in
>         "f")
>             FILE2=./$OPTARG
>           ;;
>         *)
>         # Should not occur
>           echo "Unknown error while processing options"
>           ;;
>       esac
>     done
>   return $OPTIND
> }
> FILE=$(date +%Y%m%d).arc
> declare growth=0
> while read line1;

while read size1 dir1

> do
>         size1=`echo $line1 | sed 's/\s.*//'`
>         dir1=`echo $line1 | sed 's/\S*\s*//'`
>         while read line2;

while read size2 dir2

>         do
>                 size2=`echo $line2 | sed 's/\s.*//'`
>                 dir2=`echo $line2 | sed 's/\S*\s*//'`
>                 if [ "$dir1" = "$dir2" ];
>                 then
>                         growth=`expr $size1 - $size2`

growth=$(( $size1 - $size2 ))

>                         if [ $growth -ge 1000 ];
>                         then
>                                 echo $dir1 " |growth:" $growth
>                         fi
>                 fi
> 
>         done < $FILE2
> done < $FILE

    Much faster than looping through $FILE2 for every line of $FILE would be to use join:

join  -1 2 -2 2 ~/txt ~/txt2 |
 while read dir size1 size2
 do
    : make your comparison here
 done


> --- On Wed, 12/9/09, Kevin Cozens <kevin-4dS5u2o1hCn3fQ9qLvQP4Q at public.gmane.org> wrote:
> 
> 
> From: Kevin Cozens <kevin-4dS5u2o1hCn3fQ9qLvQP4Q at public.gmane.org>
> Subject: Re: [TLUG]: Bash Script to Monitor Space by Directory.
> To: tlug-lxSQFCZeNF4 at public.gmane.org
> Received: Wednesday, December 9, 2009, 3:54 PM
> 
> 
> Dennis Antle wrote:
> > So I need to develop a bash script to monitor changes in size by directory, on the SAN volume.
> > I'm looking for advise as to how to go about this? 
> 
> You could use the du command to get the information you need.
> --
> The Toronto Linux Users Group.      Meetings: http://gtalug.org/
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
> 
> 
> 
>       __________________________________________________________________
> Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail.  Click on Options in Mail and switch to New Mail today or register for free at http://mail.yahoo.ca
> --
> The Toronto Linux Users Group.      Meetings: http://gtalug.org/
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
> 

-- 
   Chris F.A. Johnson, webmaster         <http://woodbine-gerrard.com>
   ===================================================================
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list