- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- echo to a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2006 03:56 AM
09-27-2006 03:56 AM
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
Solved! Go to Solution.
- Tags:
- bdf
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2006 04:04 AM
09-27-2006 04:04 AM
Re: echo to a file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2006 04:11 AM
09-27-2006 04:11 AM
Re: echo to a file
cat /dev/null > $LOG
and remove the following command from your script...
echo "End Of File" >> $LOG
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2006 04:15 AM
09-27-2006 04:15 AM
Re: echo to a file
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2006 04:16 AM
09-27-2006 04:16 AM
Re: echo to a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2006 04:17 AM
09-27-2006 04:17 AM
Re: echo to a file
* 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2006 04:21 AM
09-27-2006 04:21 AM
Re: echo to a file
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2006 04:36 AM
09-27-2006 04:36 AM
Re: echo to a file
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2006 12:32 AM
10-02-2006 12:32 AM
Re: echo to a file
####### 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2006 01:25 AM
10-02-2006 01:25 AM
Solution#!/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