Visit Our UNIX and Linux User Community


Can I improve this script ???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can I improve this script ???
# 1  
Old 12-20-2001
Question Can I improve this script ???

Hi all,

Still a newbie and learning as I go ... as you do Smilie
Have created this script to report on disc usage and I've just included the ChkSpace function this morning.

It's the first time I've read a file (line-by-bloody-line) and would like to know if I can improve this script ?

FYI - I created the $1.temp file as it was the only way to remove blank lines that I could find.

Cheers,
Cameron

Code:
TVL-AU: cat daily_df
#!/bin/bash
#######################################################
# >> NOTE: Ensure that you comment ANY amendments. <<
#######################################################
#
# SCRIPT NAME : daily_df
#               Creation of daily volume report.
# ----------------------------------------------------
# PURPOSE:
# This macro is intended to be run via CRONTAB every
# evening at 23:55.
#
# ----------------------------------------------------
# AMENDMENTS:
# -DATE- -VER- -WHO- - COMMENTS                      -
# 281101  001   CJY  Initial script.
# 211201  002   CJY  Check mounts and send an email if
#                    any mount reaches 95% if disc
#                    space.
# 241201  003   CJY  Improvements to script.
#
# ----------------------------------------------------
# >> NOTE: Ensure that you comment ANY amendments. <<
#######################################################

# - Environment setup ----------------------------------#

## File location/assignments.
  xdfdir=/amadeus_stats/disc/
  xdfThold=95        # %Threshold required to send email.
  xemail="tvl_ops_reports@???.com.au"
##xemail="cyoung@???.com.au"
  xemailsub="WARNING: Everest-AU Space Shortage."

# - Functions ------------------------------------------#
#
function ChkSpace {

  toobig=0
  grep "/" $1 > $1.temp

  while read f1 f2 f3 f4 f5 xspc ; do
    xspc=${xspc%%%}
    if [ $xspc -ge $xdfThold ] ; then
        toobig=$toobig+1
    fi
  done < $1.temp

  if [ $toobig -gt 0 ] ; then
    # Send email of mount
    mail -s "$xemailsub" $xemail << -END
               THERE ARE MOUNTS SHORT OF SPACE.
     System Performance will be affected if not investigated.
----------------------------------------------------------------------
`cat $1`
----------------------------------------------------------------------
 End-of-Message.
-END
  fi

  rm $1.temp

}  ## End-of-function ChkSpace

# - START-OF-SCRIPT ------------------------------------#
##

set `date '+%Y %b %d'`

if ! [ -d $xdfdir$2$1 ]; then
  echo "Creating New Directory ... "$xdfdir$2$1
  mkdir $xdfdir$2$1
  chown operator:group $xdfdir$2$1
  chmod 744 $xdfdir$2$1

  ## Remove files older than 1 yr.
  find $xdfdir/???20?? -name \*\_df -mtime +366 -type f -exec rm {} \;
  ## Remove directories older than 1 yr.
  find $xdfdir -name ???20?? -mtime +366 -type d -exec rm {} \;
fi

df -v > $xdfdir$2$1/$2$3\_df
chown operator:group $xdfdir$2$1/$2$3\_df
chmod 644 $xdfdir$2$1/$2$3\_df

ChkSpace $xdfdir$2$1/$2$3\_df

##
# - END-OF-SCRIPT --------------------------------------#


Last edited by Cameron; 12-24-2001 at 09:57 AM..
# 2  
Old 12-21-2001
As posted, this will not run. I suspect that you left out a big chunk of code from the middle as posted. But I'll do my best with what I've got.

I can't tell for sure, but it looks like you might send out a mail message per file system that is too full. You don't want to do that.

The shell can split fields for you. You should let it. This is much faster than using cut processes. The shell can also strip characters from either end of a variable. To strip a trailing % from a variable, we can do ${var%%%}.

So the inner loop would look like:
Code:
toobig=0
while read xmnt x2 x3 x4 x5 xspc ; do
      xspc=${xspc%%%}
      if [ $xspc -ge $xdfThold ] ; then
           ((toobig=toobig+1))
      fi
done

if [ $toobig -gt 0 ] ; then
    mail -s "$xemailsub" $xemail < $xdfdir$2$1/$2$3\_df
fi

Except use better names for the fields.
# 3  
Old 12-24-2001
Perderabo,

Thanks for the tip.
I've reposted the changes in my initial post.
The reason you couldn't read the script correctly prior was due to some < & > symbols I had in my comments header & the redirection for the mail.

Is it necessary to enclose the variable like ${var-name}.
Is this a standard?

Last edited by Cameron; 12-24-2001 at 10:01 AM..
# 4  
Old 10-20-2002
Quote:
toobig=0
while read xmnt x2 x3 x4 x5 xspc ; do
xspc=${xspc%%%}
if [ $xspc -ge $xdfThold ] ; then
((toobig=toobig+1))
fi
done
Perderabo / Anyone,

Thanks for the help with this (some time ago now).
Having trouble trying to implement this script using ksh.

In the line "if [ $xspc -ge $xdfThold ] ; then" the script returns the error ... /usr/local/bin/space_chk[49]: 1%: more tokens expected.

Any ideas on how I might resolve this?
# 5  
Old 10-21-2002
Hmmm, I musta missed your first reply. I can't remember what I was doing last Christmas eve. (I guess that explains it though...) We have a rule against bumping a thread if you don't immediately get an answer. But you needn't wait 10 months.

Doing ${X} is only absolutely required in something like "echo ${X}zzzzz". Without the brackets the variable name will be wrong. It's also required to invoke any of the shells advanced processing. ${x%%%} will strip a % character. But, $x%%% will just add three of them. But I often use the brackets just to make my scripts more readable.

You can get around the error message by doing:
if [ "$a" -gt "$b" ]
or switching to double brackets:
if [[ $a -gt $b ]]

But this will just make the error go away. The source of the problem is that the variables don't contain the right values. You need to echo their values just before the if statement. One of them must be empty which is why your test wants more tokens. And the other contains 1% which seems odd since you are stripping it. Or maybe one of the variables is something like "1% plus garbage" so the now internal % isn't stripped and the "plus garbage" is confusing the test. This is why you need to echo them is see what's happening.

Looking at the rest of your script, I can't really follow what's happening, but it's common for directories to be old. If you have /directory/subdirectory/files, as you add or remove files to the subdirectory, you are not affecting the mtime of directory. And rm will not remove a directory. You need to use rmdir.

Also you can't do
if ! [ -d jjjj ]

switch that to
if [ ! -d jjjj ]
# 6  
Old 10-21-2002
Hi Perderabo,

Thanks for the reply - 10 months?!? - doesn't time fly Smilie

Below is a copy of my script (sad as it is) which I should have really posted initially - it's to check the disc space on the system (intended to be checked every 5 minutes via cron). The help you gave me was invaluble, but I was using bash for that task, this system does not have bash installed (doh!).

Hope the following is of help, as I'm going around the twist trying to get this to work...
Code:
# File location/assignments.

# %Threshold required to send email.
xdfThold=95%
xtmp=/usr/local/bin/space_chk.tmp
echo $xtmp
xemail="cyoung@????.com.au"
xemailsub="WARNING: MDProd Space Shortage."

# - START-OF-SCRIPT ------------------------------------#
##

toobig=0
echo "$toobig = "$toobig
df -k | grep "/" > $xtmp
cat $xtmp

while read f1 f2 f3 f4 xspace xmounts
  do
    echo "1-"$f1 "2-"$f2 "3-"$f3 "4-"$f4 "5-"$xspace "6-"$xmounts
    echo ">"$xspace"<"

    #xspace=${xspace%%%}
    
    friend gave me this idea(below) - sadly, no success either.
    #xspace=`echo $xspace | awk -F%'{ print $1 }'`   

    echo ">"$xspace"<"
    if [ "$xspace" -ge "$xdfThold" ]
      then
        if [ "$xmounts" != "/proc" ]
          then
            echo "It's ok.."
          else
            toobig=$toobig + 1
        fi
    fi
  done < $xtmp

if [ $toobig -gt 0 ] ; then
  # Send email of mount
  mailx -s "$xemailsub" $xemail <<END
!!!! WARNING !!!! WARNING !!!! WARNING !!!! WARNING !!!! WARNING !!!!
                THERE ARE MOUNTS SHORT OF SPACE.
     System Performance will be affected if not investigated.
----------------------------------------------------------------------
date ; df -k
----------------------------------------------------------------------
.End-of-Message.
END
fi

##

And the output from the above...
Code:
# space_chk
/usr/local/bin/space_chk.tmp
0 = 0
root_domain#root        209080       92893      108224    47%    /
/proc                        0           0           0   100%    /proc
usr_domain#usr         1555360     1269513      285847    82%    /usr
data_domain#apps      30720000     6181410     3063952    67%    /u01
data_domain#app7      30720000     2706775     3063952    47%    /u07
data_domain#data02    30720000    10503493     3063952    78%    /u02
data_domain#data03    30720000     7210616     3063952    71%    /u03
data_domain#data04    30720000     1002608     3063952    25%    /u04
scratch_domain#fs0     8886760     6375532     2482408    72%    /scratch
data_domain#tmp       30720000         290     3063952     1%    /tmp
1-root_domain#root 2-209080 3-92893 4-108224 5-47% 6-/
>47%<
>47%<
/usr/local/bin/space_chk[56]: 47%: more tokens expected
1-/proc 2-0 3-0 4-0 5-100% 6-/proc
>100%<
>100%<
/usr/local/bin/space_chk[56]: 100%: more tokens expected
1-usr_domain#usr 2-1555360 3-1269513 4-285847 5-82% 6-/usr
>82%<
>82%<
/usr/local/bin/space_chk[56]: 82%: more tokens expected
1-data_domain#apps 2-30720000 3-6181410 4-3063952 5-67% 6-/u01
>67%<
>67%<
/usr/local/bin/space_chk[56]: 67%: more tokens expected
1-data_domain#app7 2-30720000 3-2706775 4-3063952 5-47% 6-/u07
>47%<
>47%<
/usr/local/bin/space_chk[56]: 47%: more tokens expected
1-data_domain#data02 2-30720000 3-10503493 4-3063952 5-78% 6-/u02
>78%<
>78%<
/usr/local/bin/space_chk[56]: 78%: more tokens expected
1-data_domain#data03 2-30720000 3-7210616 4-3063952 5-71% 6-/u03
>71%<
>71%<
/usr/local/bin/space_chk[56]: 71%: more tokens expected
1-data_domain#data04 2-30720000 3-1002608 4-3063952 5-25% 6-/u04
>25%<
>25%<
/usr/local/bin/space_chk[56]: 25%: more tokens expected
1-scratch_domain#fs0 2-8886760 3-6375532 4-2482408 5-72% 6-/scratch
>72%<
>72%<
/usr/local/bin/space_chk[56]: 72%: more tokens expected
1-data_domain#tmp 2-30720000 3-290 4-3063952 5-1% 6-/tmp
>1%<
>1%<
/usr/local/bin/space_chk[56]: 1%: more tokens expected
#

I was asked to create the script as the File System filled on mounts /u01, /u02, /u03, /u04, /u07 and /tmp (all 101%)without any notice given - to my surprise too.

Last edited by Cameron; 10-21-2002 at 09:20 PM..
# 7  
Old 10-22-2002
Check this out. It is a perl script that I wrote for the same exact task. Maybe it can help you out?

Code:
#!/usr/bin/perl

# auswipe, 5 Apr 2002
# Tested under OpenBSD 2.9
# auswipe sez : "No guarantees!"

$upperLimit = 80;              # Upper Limit in % for a File System
$mailTarget = "joe\@blow.com"; # Target for the automatic e-mail message

open(STATS, "df -h\|grep '%'|") || die "$!";

my @fileStats = <STATS>;

foreach $entryLine (@fileStats) {
  chomp($entryLine);
  $entryLine =~ s/\s+/ /g;
  my @fsStats = split(/ /, $entryLine);
  $fsStats[4] =~ s/%//g;
  if ($fsStats[4] >= $upperLimit) {
    open(MAIL, "|mail -s \"FileSystem $fsStats[0] Getting Full!\" $mailTarget ") || die "Can't open m
ail!";
    select(MAIL);
    print << "EOF";

    The FileSystem $fsStats[0] is getting full.
    Currently, the file system is ${fsStats[4]}% full. The upper
    limit has been placed at ${upperLimit}%.

    Please attend to this matter.

    -auswipe

    This message has been created automatically. Please do not respond to this message.
EOF
    close(MAIL);

  };
};
close(STATS);


Previous Thread | Next Thread
Test Your Knowledge in Computers #249
Difficulty: Easy
Senator Albert Gore, Jr. authored the High Performance Computing and Communication Act of 1991, creating what Gore referred to as the information superhighway.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Improve script

Gents, Is there the possibility to improve this script to be able to have same output information. I did this script, but I believe there is a very short code to get same output here my script awk -F, '{if($10>0 && $10<=15) print $6}' tmp1 | sort -k1n | awk '{a++} END { for (n in a )... (23 Replies)
Discussion started by: jiam912
23 Replies

2. Shell Programming and Scripting

Improve script and get new output file

Gents, Using the following script, I got the changes as desired in the output file called (spread_2611.x01.new). Complete file as input (spread_2611.x01). Can you please have a look to my script and improve it please. :b: Also I would like to I get a additional selecting only the records... (21 Replies)
Discussion started by: jiam912
21 Replies

3. Shell Programming and Scripting

How to improve an script?

Gents. I have 2 different scripts for the same purpose: raw2csv_1 Script raw2csv_1 finish the process in less that 1 minute raw2csv_2 Script raw2csv_2 finish the process in more that 6 minutes. Can you please check if there is any option to improve the raw2csv_2. To finish the job... (4 Replies)
Discussion started by: jiam912
4 Replies

4. Shell Programming and Scripting

Improve sftp script

Dear all, I have written two scripts to transfer files to another server outside the company. One is a batch script , and the other script calls the batch script, send the files and archive the file sent. The problem is, that I want to get the list of files which have been uploaded the the... (10 Replies)
Discussion started by: arrals_vl
10 Replies

5. UNIX for Dummies Questions & Answers

How to improve the performance of this script?

Hi , i wrote a script to convert dates to the formate i want .it works fine but the conversion is tkaing lot of time . Can some one help me tweek this script #!/bin/bash file=$1 ofile=$2 cp $file $ofile mydates=$(grep -Po '+/+/+' $ofile) # gets 8/1/13 mydates=$(echo "$mydates" | sort |... (5 Replies)
Discussion started by: vikatakavi
5 Replies

6. Shell Programming and Scripting

Var Check Script (Help improve if possible)

I am working on a script to check the var on all of my systems. Can someone help me fix it to work better or give me suggestions. #!/bin/ksh IN=/path/to/list_of_workstations.txt while read hostnames do if ping $hostnames 1 | grep alive > /dev/null then percent=`ssh -q... (3 Replies)
Discussion started by: whotippedmycow
3 Replies

7. UNIX for Dummies Questions & Answers

[please] improve my shell/SQL*Plus script

Hi We generate with PL/SQL *.csv files, archive them and mail to the customer. Here is my script (Solaris 10, ksh): #!/bin/ksh # Unix Shell Script Structure for PL/SQL queries with SQL*Plus . ~/.profile scriptdir=/opt/ora/scripts queryname1=example... (1 Reply)
Discussion started by: slashdotweenie
1 Replies

8. Shell Programming and Scripting

Want to improve the performance of script

Hi All, I have written a script as follows which is taking lot of time in executing/searching only 3500 records taken as input from one file in log file of 12 GB Approximately. Working of script is read the csv file as an input having 2 arguments which are transaction_id,mobile_number and search... (6 Replies)
Discussion started by: poweroflinux
6 Replies

9. Shell Programming and Scripting

Improve the performance of a shell script

Hi Friends, I wrote the below shell script to generate a report on alert messages recieved on a day. But i for processing around 4500 lines (alerts) the script is taking aorund 30 minutes to process. Please help me to make it faster and improve the performace of the script. i would be very... (10 Replies)
Discussion started by: apsprabhu
10 Replies

10. Shell Programming and Scripting

Any way to improve performance of this script

I have a data file of 2 gig I need to do all these, but its taking hours, any where i can improve performance, thanks a lot #!/usr/bin/ksh echo TIMESTAMP="$(date +'_%y-%m-%d.%H-%M-%S')" function showHelp { cat << EOF >&2 syntax extreme.sh FILENAME Specify filename to parse EOF... (3 Replies)
Discussion started by: sirababu
3 Replies

Featured Tech Videos