Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell script questions 1

Status
Not open for further replies.
Mar 31, 2004
151
US
Hi all,

I use the following script to check free disk space. The bdf statement below finds capacity ($5). However, it
should also scan filsystem (/dev/vg03/lvol1) so that it will report both filesystem name and capacity.
Can you please help?

Example bdf -i output:
/dev/vol1 17776640 9496240 8151084 54% 38 517514 0% /u03

Code:
MAILADD=myself@mywebsite.com

# Define the hostname of the server
SRVNM=`uname -n`

# Command that finds disk space as percentage
bdf -i | awk 'FNR > 1 {sub(/%/,"",$5); print $5}' | while read CAPACITY
do
if test $CAPACITY -gt 10; then
	mail $MAILADD <<EOF
From: Yahoo
To: $MAILADD
Subject: WARNING: $SRVNM FULL
$CAPACITY% capacity on $SRVNM.

EOF
fi
done

# Exit
exit 0

Next, When I grep for three processes, here is the output

oracle 21649 1 0 21:00:18 ? 1:42 Process1

oracle 19950 1 0 Feb 14 ? 7:23 Process2

oracle 28098 1 0 08:06:40 ? 0:16 Process3

Now, Process2's $5 is different than the rest. Sometimes, it will be same. Therefore, is there a way to grep for
processname (Process1, Process2 ...). I am using the following script now, but it fails due to the above reason.

Code:
MAILADD=myself@mywebsite.com

# Server name
SRVNM=`uname -n`

# Check if the processes are active. Loop through the process dat file, take each process and check if it is active.
# When it founds an inactive process, mail will be sent.
while read PROG
do
ANSWER=`ps -ef | grep -v grep | grep $PROG | awk '{ print $8 }'`
if test "$ANSWER" = "$PROG"; then
            sleep 1
else
            mail $MAILADD <<EOF
From: $0
To: $MAILADD
Subject: Missing process on $SRVNM
Checking $PROG on $SRVNM... not found!

EOF
fi
done < $DIR/monprocs.dat

exit 0
 
ANSWER=`ps -ef | grep -v grep | grep $PROG | awk '{ print $8 }' | uniq`

man uniq.

I don't understand the first question.
 
1) bdf -i | awk '
FNR>1{sub(/%/,"",$5); print $1,$5}
' | while read FileSystem CAPACITY
2)ANSWER=`ps -e | awk "/$PROG/"'{print $NF}'`

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thank you both for the answers.

I was also trying to implement the above using df -k.

Sample df -k output is:
Code:
/BA/opt/bmc            (/dev/lvol1) :   401061 total allocated Kb
                                                    313194 free allocated Kb
                                                     87867 used allocated Kb
                                                        21 % allocation used
/home                  (/dev/lvol2) :  3054319 total allocated Kb
                                                   1372058 free allocated Kb
                                                   1682261 used allocated Kb
                                                        55 % allocation used
/opt                   (/dev/lvol3) :  2997801 total allocated Kb
                                                   2219607 free allocated Kb
                                                    778194 used allocated Kb
                                                        25 % allocation used

I have to retrieve both the filesystem name and capacity in this case also. I came up with this code. It won't work. I am not clear about loops in awk yet. Can you suggest a decent solution? Thank you again.

Code:
# Email address to which the error message will be sent to
#MAILADD=myself@mywebsite.com

# Define the hostname of the server
#SRVNM=`uname -n`

df -k | grep allocation | grep used  | awk {'print $1'} | while read CAPACITY
i=1
do
FILESYSTEM=`df -k | grep total | awk ' BEGIN { names[NR] = $1 } { print names[$i]}'`
echo "FS $FILESYSTEM"
#if test $CAPACITY -gt 10; then
#     	mail $MAILADD <<EOF
#From: Yahoo
#To: $MAILADD
#Subject: WARNING: $FILESYSTEM of $SRVNM FULL
#$FILESYSTEM at $CAPACITY% capacity on $SRVNM. 

#EOF
#fi
i=`expr $i + 1`
done

exit 0
 
Try something like this:
df -k | awk '
/total/{curFS=$1}/allocation/{print curFS,$1}
' | while read FILESYSTEM CAPACITY
do
echo "$FILESYSTEM is $CAPACITY % used"
done

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top