Can I improve this script ???

Hi all,

Still a newbie and learning as I go ... as you do :slight_smile:
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

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 --------------------------------------#

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:

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.

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?

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?

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 ]

Hi Perderabo,

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

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...

# 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...

# 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.

Check this out. It is a perl script that I wrote for the same exact task. Maybe it can help you out?

#!/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);

Thanks Auswipe, will try it tomorrow and I do appreciate the consideration.
However I don't know a scrap of perl and would really like to complete what I started with this script - I hope you understand.

Will more than likely start learning perl & awk after my holidays - say mid December.

I think that your ksh has a bug. This should work:
xspace=${xspace%%\%}
and the above code will continue to work once if your ksh if updated.

And don't set xdfThold to 95%, set it to just 95.

Also make sure that the first line is:
#! /usr/bin/ksh

or whatever the path to your ksh is.

Perfectly understantable.

Cameron, from what I've gathered from other posts, you're using Mandrake Linux, correct?

Most likely what ksh you have installed is not "real" ksh. It's pdksh, which isn't quite as good as David Korn's ksh.

I usually dump that from my systems and install ksh93 instead.

Thanks LivinFree, I'll keep that in mind. I've not enjoyed Mandrake (at home) for the past three months. Sadly I went through three 10G HD's (dodgy) in the space of four months so I'm without a Linux/Unix system at home. The above posting is work related; on the various SCO 5.0.5 / Irix6.5 / Compaq True-64 / Slackware(*yuck-o*) systems.

The better part of my life suggested that I get some new HD's after we are married (2nd November) - so I'm having to wait. :wink:

Perderabo, thanks for the advice - worked a treat.