1833262 Members
2949 Online
110051 Solutions
New Discussion

script help please.

 
Ragni Singh
Super Advisor

script help please.

Hi all, In a delimma and need some serious help from a script guru. I have this script here that looks at the br_log file for sucess and failed backup messages. If the backup fails, It will higlight the columm read and if the backup was sucessful, It would display a green columm. Attached it my script and the ouput that I am hoping for it to display. Thanks for all your time and I will forever be greatful for your time.
#!/usr/bin/ksh
##################################################################

# Declare variables
PATH=/usr/bin:/usr/local/bin
SERVERS="/home/xxx/xxxxx"
PAGE="/opt/apache/htdocs/testing/testing-$(date +%m-%d-%Y).html"
TODAY="/opt/apache/htdocs/testing/testing.html"
TMP_PAGE1="/tmp/testing1.html"
TMP_PAGE2="/tmp/testing2.html"
DATE=$(date)
COLOR=""
FAIL_CODE=""
END=""

# Make sure we start clean
/usr/bin/rm -f $TMP_PAGE1 $TMP_PAGE2 $TODAY

# Start the page

cat <<-EOF > $PAGE


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

Backup completions


Last updated: $DATE



NOTES:




Please notify Engineering if any backup fails.



EOF

# Start the table

cat <<-EOF >> $PAGE





EOF

# Get the data

for i in `cat $SERVERS`
do

# Get the start time

ssh $i "grep -i "Starting" /var/sam/log/br_log 2> /dev/null | tail -1" > $TMP_PAGE2

echo "
" >> $TMP_PAGE2

# Get the end time - will also use later for COLOR selection

END=$(ssh $i "tail -2 /var/sam/log/br_log 2> /dev/null | grep -v -e 'Starting'")

echo $END >> $TMP_PAGE2

echo "
" >> $TMP_PAGE2
echo "
" >> $TMP_PAGE2

# Get the failure code and set the color

FAIL_CODE=$(ssh $i "tail -2 /var/sam/log/br_log 2> /dev/null | grep -i 'Completed' | tail -1")

case $FAIL_CODE in

"Completed" )
COLOR="GREEN"
;;

"ERROR" )
COLOR="RED"
;;

* )
COLOR="RED"
;;
esac

# Also set the COLOR based on $END

[ $(echo $END | grep -c ERROR) -eq 1 ] && COLOR="GREEN"

# Build the first column - This is where we set the color

echo "
" > $TMP_PAGE1

echo "" >> $TMP_PAGE1
echo "$i
" >> $TMP_PAGE1
echo "" >> $TMP_PAGE1
echo "
" >> $TMP_PAGE1

# Pull it together

cat $TMP_PAGE1 $TMP_PAGE2 >> $PAGE

done

# end the table

echo "
" >> $PAGE
echo "" >> $PAGE

#

Here is the log output from 2 systems.
01/30/03 02:01:05
01/30/03 02:01:05 Starting non-root full backup ...
01/30/03 03:25:18 Full Backup was successful
01/30/03 03:25:18 Full Backup Completed.
##################################################
# EXITING with 0 return code.... #
##################################################


systems2

01/28/03 12:00:34
01/28/03 12:00:34 Starting non-root full backup ...
01/28/03 12:01:17 ERROR: Full Backup FAILED !!! Contact System Admin
01/28/03 12:01:17 Exit with error !!!

##################################################


system1





3 REPLIES 3
Rodney Hills
Honored Contributor

Re: script help please.

FAIL_CODE=$(ssh $i "tail -2 /var/sam/log/br_log 2> /dev/null | grep -i 'Completed' | tail -1")
...
esace

I think is your problem...

Change to-
if $(ssh $i "tail -2 /var/sam/log/br_log 2> /dev/null | grep -q -i 'Completed'") ; then
COLOR="GREEN"
else
COLOR="RED"
fi

The if will test the status of the last command (grep in this case) and if Completed is found, then set COLOR to GREEN.

HTH

-- Rod Hills

There be dragons...
Sridhar Bhaskarla
Honored Contributor

Re: script help please.

Hi Sanjay,

Since there is no fixed field for the STATUS, you may want to use this code. You can extend the if statement further if you have other STATUS values in addition to "successful" and "FAILED".

Take out the case statement and replace it with the following
....
ssh $i "tail -2 /var/sam/log/br_log " |grep "successful" > /dev/null 2>&1
if [ $? = 0 ]
then
COLOR="GREEN"
else
COLOR="RED
fi
...

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Jose Mosquera
Honored Contributor

Re: script help please.

Hi,

I've worked in a similar case but ussing a custom script file, my logic have been the following:

fbackup -0 -v -i / -e /tmp -f /dev/rmt/0m 2> /tmp/fbackup.log
STATUS=$?
case in $STATUS
0) Your successful procedure
;;
4) Your warning procedure
;;
*) Your fail procedure
;;
esac

The main advantage of redirect the log file (/tmp/fbackup.log), is that you can manipulate it to extract the warning messages and send them in the case of a status 4.

Rgds.