Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Broker monitor script with notification

Status
Not open for further replies.

sjcrane

MIS
Jan 18, 2006
41
0
0
US
I need some assistance writing a script to monitor a RPC broker and notify if it is down.

I have a broker that runs on port 9250. When it is up it looks like the following:

# netstat -Aan|grep 9250
f1000e00025863b8 tcp4 0 0 172.16.2.1.9250 172.16.80.40.1185 ESTABLISHED
f1000e000ecafbb8 tcp4 0 0 *.9250 *.* LISTEN
f1000e00003a5bb8 tcp4 0 0 172.16.2.1.9250 172.16.81.105.1572 ESTABLISHED
f1000e000027cbb8 tcp4 0 0 172.16.2.1.9250 10.4.62.89.1123 ESTABLISHED
f1000e00021e03b8 tcp4 0 0 172.16.2.1.9250 10.4.68.210.1356 ESTABLISHED
f1000e000224bbb8 tcp4 0 0 172.16.2.1.9250 10.4.65.79.2659 ESTABLISHED
f1000e000047bbb8 tcp4 0 0 172.16.2.1.9250 172.16.2.143.1098 ESTABLISHED
f1000e00022783b8 tcp4 0 0 172.16.2.1.9250 172.16.5.173.1037 ESTABLISHED



When it is down it looks like the following:

# netstat -Aan|grep 9250
#

I would like to run a script from CRON every 2 minutes that looks for the broker to be up but if not, notify me via email...

So:


If broker is up = do nothing
If broker is down = email notification

mail -s "Broker 9250 is down" <email address> < /tmp/restart_broker.txt

So it emails that it is down and provides instructions on how to restart.

Any help would be greatly appreciated...
 
I'm tired and going to bed. But quickly:

if return code is 1 then email
if return code is 0 then do nothing

You will get a 1 if netstat returns nothing
You will get a 0 if netstat returns data

Code:
#!/bin/ksh

netstat -Aan|grep 6000 1>/dev/null
RC=$?
echo RCa $RC
if [[ ${RC} -eq 1 ]]
then
        echo "Mailing"
elif [[ ${RC} -eq 0 ]]
then
        echo exiting
fi

netstat -Aan|grep 9250 1>/dev/null
RC=$?
echo RCb $RC
if [[ ${RC} -eq 1 ]]
then
        echo "Mailing"
elif [[ ${RC} -eq 0 ]]
then
        echo exiting
fi

exit 0

which on my server will print:
$ ./x
RCa 0
exiting
RCb 1
Mailing

You will need to change the 'echo Mailing' to mail you, but you can do that. This was very quick and took 2 minutes. It can be fluffed up with more bells and whistles, but as I said, I'm going to bed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top