1753914 Members
9352 Online
108810 Solutions
New Discussion юеВ

Re: help on scripting

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

help on scripting

Hey guys,

I created a script to check file systems, I am struggling with getting the syntax correct where I create a control file with the limits then get the script to use this for $warn and $crit - ie:

filesystem warn crit
/home 75% 85%
/tmp 80% 90%
/usrdump 90% 96%

etc etc

how can I use the warn and crit variables in my script please?

#!/bin/ksh

# script to check the filesystem status

DIR=/sysadmin/scripts/admin
LOG=$DIR/msgfile.out

WARN=50
CRIT=70
>$DIR/msgfile.out

warning_func () {
echo "WARNING: Filesystem Alert" >> $LOG
df|awk 'NR>1 && !/\/proc$/ {print $7,$4}'|sed 's/\%//' |awk -v l1=$WARN -v l2=$CRIT '
{
if ( $2 > l1 )
if ( $2 < l2 )
print $1 " is at " $2"%"
}' >> $LOG
exit 1
}

critical_func () {
echo "CRITICAL: Filesystem Alert" >> $LOG
df|awk 'NR>1 && !/\/proc$/ {print $7,$4}'|sed 's/\%//' |awk -v l2=$CRIT '
{
if ( $2 > l2 )
print $1 " is at " $2"%"
}' >> $LOG
warning_func
}

for a in `df | awk 'NR>1 && !/\/proc$/ {print $7,$4}'| sed s/%//g | sed s/' '/:/g`
do

USED=`echo $a |cut -d":" -f2`

if [[ $USED -gt $CRIT ]] ; then

critical_func

fi
done

warning_func

Thanks guys.

chris
hello
6 REPLIES 6
Peter Godron
Honored Contributor
Solution

Re: help on scripting

Chris,
wrap your for a in `df ... loop into a read controlfile loop. You can then use $FS to differentiate by filesystem.

i.e.
while read -r FS WARN CRIT
do
for a in `df ...
done < controlfile
lawrenzo
Trusted Contributor

Re: help on scripting

thanks I will give this a go .....
hello
lawrenzo
Trusted Contributor

Re: help on scripting

can I ask what the -r means?

cheers
hello
Peter Godron
Honored Contributor

Re: help on scripting

Hi,
see "man read"
"-r Do not treat a backslash character in any special way. Consider each backslash to be part of the input line."
Bill Hassell
Honored Contributor

Re: help on scripting

A couple of notes: df is very difficult to use for monitoring filesystem space. The inode information is meaningless for all but HFS filesystems. You can use the Better-DF command (bdf) to find these details. Be sure to handle the possibility of a long source device file as bdf will split the long line into two pieces. I have attached a script which will monitor all the filesystems and report once each time a filesystem exceeds a limit, then once again when it grows beyond the limit by a specific amount (ie, 90%, grows 2% = 92 94 96, etc). This prevents email floods, especially after hours.


Bill Hassell, sysadmin
lawrenzo
Trusted Contributor

Re: help on scripting

ok thanks guys,

Great script btw Bill ...
hello