1826422 Members
3620 Online
109692 Solutions
New Discussion

echo to a file

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

echo to a file

Hello,

I am writing a script to check the filesystems then write the output to a logfile if they have gone over a certain threshold:

#!/bin/ksh

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

WARN=50
CRIT=80


for a in COMMAND=`df | tail +2 | awk '{print $7,$4}' | sed s/%//g | sed s/' '/:/g |grep -v /proc`
do

FILESYS=`echo $COMMAND | cut -d":" -f1`
USED=`echo $COMMAND | cut -d":" -f2`

if [ $USED -gt $WARN ] ; then
echo " ######### WARNING ########### " > $LOGFILE
echo " $FILESYS filesystem is at $USED " >> $LOG
fi
done


for a in COMMAND=`df | tail +2 | awk '{print $7,$4}' | sed s/%//g | sed s/' '/:/g |grep -v /proc`


FILESYS=`echo $COMMAND | cut -d":" -f1`
USED=`echo $COMMAND | cut -d":" -f2`

if [ $USED -gt $CRIT ] ; then
echo "\n ######### CRITICAL ########### " >> $LOG
echo " $FILESYS filesystem is at $USED " >> $LOG
fi
done
echo "End Of File" >> $LOG

The issue I have is formatting the logfile - I would like the output like so:

####### WARNING ###########

/ filesystem is at 68%
/home filesystem is at 55%
etc

####### CRITICAL ###########

/var filesystem is at 94%
/tmp filesystem is at 88%

etc

End of File

however I do not want any output to the file if there is no critical or warning alerts.

any idea's??

Thanks a million

Chris
hello
9 REPLIES 9
Peter Godron
Honored Contributor

Re: echo to a file

Chris,
you describe what the output SHOULD look like, but not how it currently look like.
Can you please give an example of a non-acceptable output file.
Sandman!
Honored Contributor

Re: echo to a file

You can zero out the logfile when the script starts so that entries from the last run do not fill it up in case there are no critical or warning alerts...

cat /dev/null > $LOG

and remove the following command from your script...

echo "End Of File" >> $LOG
James R. Ferguson
Acclaimed Contributor

Re: echo to a file

Hi Chris:

If a well-formatted output is your goal, consider replacing simple 'echo' statements for formatted 'printf' ones. For example:

# printf "filesystem %s is at %d%%\n" "/var" 50

...note the double "%" character to yield:

filesystem /var is at 50%

See the manpages for 'printf(1)' for more information.

Regards!

...JRF...

A. Clay Stephenson
Acclaimed Contributor

Re: echo to a file

Your fundamental problem is that
echo "End of File" >> ${LOG} is done unconditionally so you simply need to add a bit of logic.

#!/usr/bin/sh

typeset -i ECHOED=0 # flag, initialize to 0

for a in COMMAND=`df | tail +2 | awk '{print $7,$4}' | sed s/%//g | sed s/' '/:/g |grep -v /proc`
do

FILESYS=`echo $COMMAND | cut -d":" -f1`
USED=`echo $COMMAND | cut -d":" -f2`

if [ $USED -gt $WARN ] ; then
echo " ######### WARNING ########### " > $LOGFILE
echo " $FILESYS filesystem is at $USED " >> $LOG
ECHOED=1 #set flag to 1
fi
done


for a in COMMAND=`df | tail +2 | awk '{print $7,$4}' | sed s/%//g | sed s/' '/:/g |grep -v /proc`


FILESYS=`echo $COMMAND | cut -d":" -f1`
USED=`echo $COMMAND | cut -d":" -f2`

if [ $USED -gt $CRIT ] ; then
echo "\n ######### CRITICAL ########### " >> $LOG
echo " $FILESYS filesystem is at $USED " >> $LOG
ECHOED=1 #set flag to 1
fi
done
# if flag != 0 then echo
if [[ ${ECHOED} -ne 0 ]]
then
echo "End Of File" >> $LOG
fi

If it ain't broke, I can fix that.
spex
Honored Contributor

Re: echo to a file

From a quick glance at the script, present output:
* has the "WARNING" and "CRITICAL" headings appear once for each match,
* has messages that are not sorted by disk utilization.

I will rewrite the script for you after I get back from Wal-mart, unless someone else does it first.

One thing to point out--I notice you have:
echo " ######### WARNING ########### " > $LOGFILE

instead of:

echo " ######### WARNING ########### " > $LOG

PCS
Peter Godron
Honored Contributor

Re: echo to a file

Chris,
also are you sure about your df command, as I can't line up your output with the output of df. I get something like:
/home (/dev/vg00/lvol5 ): 400112 blocks 6294 i-nodes

Are you running this on HPUX or another platform?
James R. Ferguson
Acclaimed Contributor

Re: echo to a file

Hi Chris:

I agree with Peter, I suspect that you're running on a Linux or AIX platform from the exclusion of the '/proc' filesystem. No matter for the purposes here, however.

You can greatly reduce the number of separate processes that run to collect your data. For your principal COMMAND, you could do:

# COMMAND=`df|awk 'NR>1 && !/\/proc$/ {print $7,$4}'|sed -e s/%//g -e s/' '/:/g`

This eliminates a 'tail' process, a 'grep' and reduces two 'sed' processes to one.

Regards!

...JRF...
lawrenzo
Trusted Contributor

Re: echo to a file

the output in the msgfile.out should look like


####### WARNING ############

/ is at 55%
/usr is at 60%


####### CRITICAL #########

/tmp is at 90%
/home is at 95%

End of File

Thanks for the suggestions so far
hello
Yang Qin_1
Honored Contributor
Solution

Re: echo to a file

df on my HP server does not produce the same output as youlisted. I use bdf:

#!/bin/ksh

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

echo "####### WARNING ###########" > $LOG

bdf | grep -v "\/proc" | awk ' {print $5 " " $6} ' | sed 's/\%//'|awk '
{
if ( $1 > 50 )
if ( $1 < 80 )
print $2 " is at " $1 "%"
} ' >> $LOG

echo "####### CRITICAL ###########" >> $LOG

bdf | grep -v "\/proc" | awk ' {print $5 " " $6} ' | sed 's/\%//'|awk '
{
if ( $1 > 80 )
print $2 " is at " $1 "%"
} ' >> $LOG

exit


Yang