Operating System - HP-UX
1833589 Members
3720 Online
110061 Solutions
New Discussion

Where is this script failing?

 
SOLVED
Go to solution
MAD_2
Super Advisor

Where is this script failing?

I have recently created a simple script that checks for the status of some external Oracle listeners.

Part of the script should check for one system's Oracle listener (an AS/400) up status between Sunday 5 PM and Saturday 6 PM (Pretty much most of the week!).
The other part should check for a backup system (MS/SQL) Oracle listener up status between 5 PM Sat and 6 PM Sun.

The problem is that I was paged after the AS/400 listener was turned off on Sunday morning, sometime around 1 AM. This shouldn't happened, where did I go wrong in the attached script? Thanks!

It has to be something very simple... I am sure when told where the problem is I will be ready to bang my head on the wall. But I would appreciate the always wise advise given here.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
16 REPLIES 16
john korterman
Honored Contributor

Re: Where is this script failing?

Hi,

I do not know tnsping, but could it have been momentarily unavailable at the time in question?
You might also consider changing your grep, as you will get $?=0 for e.g. "connection OK" as well as for "connection absolutely not OK".

regards,
John K.
it would be nice if you always got a second chance
MAD_2
Super Advisor

Re: Where is this script failing?

John:

Thanks for your reply but neither of your two scenarios ocurred.
1. The listener in question (AS/400) was turned down when the email notification started. The thing is, no email notification should have gone out, because it should only send these if that listener is down and it is later than 6 PM on Sunday or earlier than 5 PM Saturday, or weekdays. It was around 1 AM Sunday when it did it.

1. tnsping will return something without the OK if the listener is not up, some TNS error, I can't remember exactly. So the grep should not matter, if the listener is not up, there will be no "OK".
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
john korterman
Honored Contributor

Re: Where is this script failing?

Hi again,
spaces in the right places, e.g.
$DAYOFWK = Sat
$DAYOFWK = Sun
will probably work out miracles.

regards,
John K.
it would be nice if you always got a second chance
john korterman
Honored Contributor

Re: Where is this script failing?

Hi again,

second thoughts: spaces alone might not do it. I am not familiar with your if-syntax and would have preferred it differently. Please, try this small script out for yourself:

#if [ "$1" = Sat -a "$2" -lt 18 ] || [ "$1" = Sun -a "$2" -gt 17 ]
if [ $1 = Sat ] && [ $2 -lt 18 ] || [ $1 = Sun ] && [ $2 -gt 17 ]
then
echo true: $1 $2
else
echo par 1 not Sat combined with par 2 being smaller than 18
echo or par1 not Sun combined with par 2 being bigger than 17
fi
#end of script

Try running it with either the first or the second line uncommented and compare. There is a difference for e.g. par1=Sat and par2=17
Try also running it like this:
# sh -x <script> Sat 17

regards,
John K.
it would be nice if you always got a second chance
Shahril M
Frequent Advisor

Re: Where is this script failing?

Hi,

If I understand correctly, you want to check the MS/SQL between 5 PM Sat and 6 PM Sun, and check the Oracle at other times.

So, why don't you use this algo:
if 'between 5 PM Sat and 6 PM Sun'
check MS/SQL
else
check Oracle
fi

Also, you may want to include more brackets in your "if" conditions to eliminate ambiguity.

Rgds,
Shahril

MAD_2
Super Advisor

Re: Where is this script failing?

Shahril,

Actually, I can't use an else. The reason is simple:

A. I want to check the MS/SQL Oracle listener between 5 PM Sat and 6 PM Sun, but also...
B. I want to check the AS/400 Oracle listener between 5 PM Sun and 6 PM Sat.

So, as you can see, there is a time on Saturday when both of them should be up, between 5 and 6 PM, that's why the two IF statements. Also, this window where both need to be up may have to increase.

What I would like to hear is for someone to explain why the statements I used are not working, where the error is, or if there is another way to write them that would work.

Thanks!
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
Shahril M
Frequent Advisor

Re: Where is this script failing?

Hi,
I think you should award the points to John. I tried his space tip, and it worked for me.

Your problem seems that the Oracle listener was checked on Sun 1am when it should not have. This was because your [ $HOUR -lt 18 ] condition resolved to TRUE.

With my system time as 8pm on Monday, I used:
if [ $DAYOFWK = Mon ] && [ $HOUR -lt 18 ] || [ $DAYOFWK = Tue ] && [ $HOUR -gt 17 ] || [ $DAYOFWK = Sat ] || [ $DAYOFWK = Sun ] || [ $DAYOFWK = Wed ] || [ $DAYOFWK = Thu ] || [ $DAYOFWK = Fri ]
This is similar to being 8pm on a Saturday based on your requirements.
And it did NOT run my TRUE statement as expected.

1. You can use the flwg substitution command to make it easier for you:
s/=/ = /g

2. Based on the script you uploaded, your Wednesday condition had a typo:
$DAYOFW=Wed should be $DAYOFWK = Wed


Rgds,
Shahril
RolandH
Honored Contributor

Re: Where is this script failing?

I think your second if statement sould be this


....
# Between Sun 5:00 PM on Sun and Sat 6:00 PM check Oracle AS/400 listener
if [ $DAYOFWK=Sat ] && [ $HOUR -gt 17 ] || [ $DAYOFWK=Sun ] && [ $HOUR -lt 18 ] || [ $DAYOFWK=Mon ] || [ $DAYOFWK=Tue ] || [ $DAYOFW=Wed ] || [ $DAYOFWK=Thu ] || [ $DAYOFWK=Fri ]
then ...

Change the statement four the $HOUR variable they are placed in your if statement in the wrong order.

Roland
Sometimes you lose and sometimes the others win
RolandH
Honored Contributor

Re: Where is this script failing?

A small mistake in my first post. Now you check from Sat 18:00PM to Sun 17:00PM.
Correct is this:

# Between Sun 5:00 PM on Sun and Sat 6:00 PM check Oracle AS/400 listener
if [ $DAYOFWK=Sat ] && [ $HOUR -gt 16 ] || [ $DAYOFWK=Sun ] && [ $HOUR -lt 19 ] || [ $DAYOFWK=Mon ] || [ $DAYOFWK=Tue ] || [ $DAYOFW=Wed ] || [ $DAYOFWK=Thu ] || [ $DAYOFWK=Fri ]
....


HTH
Roland

Sometimes you lose and sometimes the others win
john korterman
Honored Contributor

Re: Where is this script failing?

Hi again,

If you try the tester script, checkif, in my former posting, it will show this when the first line is used:
# sh -x ./checkif Sat 17
+ [ Sat = Sat -a 17 -lt 18 ]
+ echo true: Sat 17
true: Sat 17

Using the second line instead shows this:
# sh -x ./checkif Sat 17
+ [ Sat = Sat ]
+ [ 17 -lt 18 ]
+ [ 17 -gt 17 ]
+ echo par 1 not Sat combined with par 2 being smaller than 18
par 1 not Sat combined with par 2 being smaller than 18
+ echo or par1 not Sun combined with par 2 being bigger than 17
or par1 not Sun combined with par 2 being bigger than 17

it makes three evaluations, which I believe is unintended.

regards,
John K.
it would be nice if you always got a second chance
MAD_2
Super Advisor

Re: Where is this script failing?

So, any other ideas?
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
Patrick Wallek
Honored Contributor

Re: Where is this script failing?

Why not dispense with the IF statements entirely and just use cron to run the script when it needs to run. To make it even easier, split the script into 2 different scripts where 1 will check the AS400 Oracle listenter and the other will check the backup system Oracle listener.

Use lines in cron like:

# Check Sunday 5PM thru Saturday 6PM
00 17-23 * * 0 /script/to/run
00 0-23 * * 1-5 /script/to/run
00 0-18 * * 6 /script/to/run

# Check between 5PM Sat and 6PM Sun
00 17-23 * * 6 /script/to/run1
00 0-18 * * 0 /script/to/run1

You can change the minutes column (first column) to run however often you like.
Vitek Pepas
Valued Contributor

Re: Where is this script failing?

Are you using cron to run the script?
Make sure you are not executing the wrong file by providing full path.
After you add missing spaces around '=' in 'if' statement conditions your script should work OK.
MAD_2
Super Advisor

Re: Where is this script failing?

I believe John may have provided the solution. I have ran a couple of tests where I provided the value for DAYOFWK and HOUR, it seemed to work. The spaces between the "=" was probably the issue.

Also, thanks for noticing that error on the DAYOFWK for Wed Shahril.

John, if you post another message I'll give you the 10 after I finish running another few tests to make sure I do not run into more problems.

Oh, by the way, I am running within cron, and I do not want to split it into two scripts because there are already enough entries there to keep crowding it with more. If I can combine these two into one, I'll leave it that way, and if could integrate it into another script I also would do that; however, its frequency is not compatible with other entries I have there already. This check will run every 30 min.

Thanks everyone for you input! It's always appreciated.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
john korterman
Honored Contributor
Solution

Re: Where is this script failing?

Hi again,

did somebody mention my name?
It is always nice to hear about progress, but do not bang anything!

regards,
John K.
it would be nice if you always got a second chance
MAD_2
Super Advisor

Re: Where is this script failing?

Thanks everyone...

By the way, I also ended up changing my statement from the form (This is only the AS/400 portion, which I want it to check between Sun 5 PM, the rest of the week, until Sat 6 PM):

if [ $DAYOFWK = Sat ] && [$HOUR -lt 19 ] || [ $DAYOFWK = Sun ] && [$HOUR -gt 16 ] || [ $DAYOFWK = Mon ] || [ $DAYOFWK = Tue ] || [ $DAYOFWK = Wed ] || [ $DAYOFWK = Thu ] || [ $DAYOFWK = Fri ]

To the following, also recommended by John:

if [ $DAYOFWK = Sat -a $HOUR -lt 19 ] || [ $DAYOFWK = Sun -a $HOUR -gt 16 ] || [ $DAYOFWK = Mon ] || [ $DAYOFWK = Tue ] || [ $DAYOFWK = Wed ] || [ $DAYOFWK = Thu ] || [ $DAYOFWK = Fri ]

For some reason, the script was still giving me false notifications with the first one, and I am still unsure as of why, maybe the syntax is totally wrong.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with