Would like to see how others would do this...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Would like to see how others would do this...
# 1  
Old 05-31-2007
Would like to see how others would do this...

I get an output from a script like:

RLINK: rlk_ecims-dr_lloravol_rvg
STATE: ACTIVE
STATUS: has 1833 outstanding writes, occupying 66328 Kbytes (0%) on the SRLis behind by 00:00:08 hours
FLAGS: write enabled attached consistent connected asynchronous
PROTOCOL: UDP/IP


If I wanted to keep an eye on either the percentage or the time, how would you parse this output? More specifically, this is the output of Veritas Replicator. I would like an email if either A) the percentage is higher than 50% or B) more than 3 hours behind.
I would love to see how someone else would do this with a .sh script...
Thanks,
D
# 2  
Old 05-31-2007
here's an awk implementation. modify to suit your needs
Code:
awk 'BEGIN { threshold=50;
	     timetheshold=3;
	     sysadmin="root";
	     email="/usr/bin/mailx "
     }
     /STATUS/{ 
	       for(i=1;i<=NF;i++) {
                  if ( $i ~ /^\(/ ) { gsub(/[(%)]/,"",$i)
		       percent=$i
		  }
		  if ( $i ~ /behind/) { time=$(i+2) }
	       }
     }
     END{
           n=split(time,timearr,":")	   
	   if ( percent > threshold ||  int(timearr[1]) > timethreshold) { 
	       cmd = email sysadmin   
               print "Body text " | cmd  
               close(cmd)
	       print "Email sent"
	   }
	   
     }' "file"

# 3  
Old 06-01-2007
Wow, that was fast! I'll give this a whiz today and let you know. Thanks ghostdog74!
# 4  
Old 06-01-2007
Quote:
Originally Posted by lm_admin_dh
I get an output from a script like:

RLINK: rlk_ecims-dr_lloravol_rvg
STATE: ACTIVE
STATUS: has 1833 outstanding writes, occupying 66328 Kbytes (0%) on the SRLis behind by 00:00:08 hours
FLAGS: write enabled attached consistent connected asynchronous
PROTOCOL: UDP/IP


If I wanted to keep an eye on either the percentage or the time, how would you parse this output? More specifically, this is the output of Veritas Replicator. I would like an email if either A) the percentage is higher than 50% or B) more than 3 hours behind.
I would love to see how someone else would do this with a .sh script...
Thanks,
D

This is my version

Code:
#!/usr/bin/ksh

string=$(cat file2)
usage=50
time_limit=3

percentage=$(echo $string | sed 's/.*(\([0-9].*\)%).*/\1/')
hours=$(echo $string | sed 's/.*\([0-9][0-9]\):[0-9][0-9]:[0-9][0-9].*/\1/')

if [[ $percentage -gt $usage ]] then
echo "Disk usage beyond 50%" #send mail
fi if [[ $hours -gt $time_limit ]] then
echo "Time limit beyond 3 hours" #send mail
fi


Last edited by praveenkumar_l; 06-01-2007 at 11:02 AM..
# 5  
Old 06-01-2007
Praveen,
Coincidently, my solution is very similar to yours.
The issue that I see in your solution is the fact that you are loading
the entire file into one string.
This can cause problems in the performance for large files.
Here is my solution not loading the entire file into a string:
Code:
typeset -i mPctLimit=50
typeset -i mTimeLimit=3

mInpFile='b'
mTimeRE='[0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
mHHRE='[0-9][0-9]'
mMMSSRE='[0-9][0-9]:[0-9][0-9]'

mPerctLine=`sed -n '/.*%.*/p' ${mInpFile}`
mPerctValue=`echo ${mPerctLine} | sed 's/.*(\([0-9].*\)%).*/\1/'`
echo "mPerct = "$mPerctValue" limit = "${mPctLimit}
if [ ${mPerctValue} -gt ${mPctLimit} ];then
 echo "Above the percentage limit"
fi

mHoursLine=`sed -n "/.*${mTimeRE}.*/p" ${mInpFile}`
mHoursValue=`echo ${mHoursLine} | sed "s/.*\(${mHHRE}\):${mMMSSRE}.*/\1/"`
echo "mHours = "$mHoursValue" limit = "${mTimeLimit}
if [ ${mHoursValue} -gt ${mTimeLimit} ];then
 echo "Above the time limit"
fi

# 6  
Old 06-01-2007
A variation from praveenkumar_l's solution where precentage and hours are read from file with one sed command.
Code:
usage=50
time_limit=3

sed  -n 's/.*\([0-9]\+\)%.*\([0-9][0-9]\):[0-9][0-9]:[0-9][0-9].*/\1 \2/;T;p' out.txt | \
echo "${percentage}% ${hours}h"

if [[ $percentage -gt $usage ]] ;then
   echo "Disk usage beyond $usage%"
   #send mail
fi

if [[ $hours -gt $time_limit ]] ;then
   echo "Time limit beyond $time_limit hours"
   #send mail
fi

Jean-Pierre.
# 7  
Old 06-04-2007
Thanks "Shell Life" and "aigels". I did not expected the input to be big.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question