Still a newbie and learning as I go ... as you do
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
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?
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.
Thanks for the reply - 10 months?!? - doesn't time fly
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
##
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.
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.
Perderabo, thanks for the advice - worked a treat.