Re: Ping script

From: McHugh, Jerome (jmchugh@TOWERRECORDS.COM)
Date: Mon Jun 17 2002 - 20:36:41 EDT


I wrote this to use ping, but recommend fping. Somewhat over commented, but
here you go.

Jerome McHugh
Senior Systems Administrator, Lead
Systems and Database Administration
Information Systems & Technology
MTS Inc. Tower Records
 <mailto:jmchugh@towerrecords.com>
 <mailto:jmchugh@towerrecords.com> jmchugh@towerrecords.com

------------------------------------------cut-------------------------------
------------
#!/bin/ksh
############################################################################
####
# Jerome P. McHugh
# EXAMPLE SCRIPT - NO LIABILITY FOR THE AUTHOR
# MODIFY AS NECESSARY
# Korn shell script to ping a list of sites and fire an email alert if
# the number of retrys exceed a threshold
############################################################################
####
############################################################################
####
# Functions functions
############################################################################
####
function do_alert {
# use standard Unix mail to send alert to text pager
print -- "
$(date)
This Message generated from System: ${HostMonitorSystem}
This Message generated by Script: ${ScName}
located in Directory: ${BaseDir}
ping failed ${AlertThreshold} times to site: ${SiteName}
" > ${MailText}
# Mail the message ( make sure to use a valid email pager address )
mail -s "Network Alert !!!" ${PagerAddress} < ${MailText} && rm ${MailText}
return 0
}
############################################################################
####
function do_work {
# throw standard out and standard err away
# only interested in success or fail
exec 1>/dev/null;exec 2>/dev/null
# Try to ping the node and store the exit code in variable Scode
ping -c 1 ${SiteName};Scode=$?
# return standard out and standard err
# maybe a bad thing to do if you're not on a tty modify to meet your needs
exec 1>/dev/tty;exec 2>/dev/tty
return ${Scode}
}
############################################################################
####
# Main main
############################################################################
####
ScName=$( basename $0 ) # Store the name of this script
BaseDir=$( dirname $0 ) # Store the path to this script
HostMonitorSystem=$(hostname) # Store the name of the site running this
script
PID=$$ # Get process ID for unique file name extension
MailText=/tmp/mail.txt.${PID} # Path to writable temp file for mail text
# MAKE SURE TO PUT A VALID EMAIL or TEXT PAGER ADDRESS HERE
PagerAddress=your_pager_address@some_paging_service.net
AlertThreshold=5 # number of failed attempts before firing alert
RetryInterval=10 # number of seconds before retrying node on failure
# Point the variable SiteList to a path/file file containing a list of
# network nodes or dotted decimal IP addresses (one per line)
SiteList=~user_name/dir/dir/some_text_file
SiteList=~jerry/data/the.list
# Start a loop through a list of Network nodes read from a text file
for SiteName in $(<${SiteList})
do
print "Processing: ${SiteName}..."
ErrCount=0 # Set the error counter to zero for processing next node
# Start a loop limited by the AlertThreshold value
while :
do
do_work;Rcode=$? # do the work...see the function
# check the results
case ${Rcode} in
0 )
# The ping worked go to the next node
break
;;
* )
# The ping did not work increment the Error counter
((ErrCount=ErrCount+1))
# If the counter equals or exceeds the threshold perform
# the do_alert function and then continue to the next node
[[ ${ErrCount} -ge ${AlertThreshold} ]] && {
do_alert
break
}
sleep ${RetryInterval}
;;
esac
done
done
exit 0
------------------------------------------cut-------------------------------
------------
 <mailto:jmchugh@towerrecords.com>

-----Original Message-----
From: Tony Osmolowski [mailto:tosmolowski@intlcheck.com]
Sent: Monday, June 17, 2002 11:34 AM
To: aix-l@Princeton.EDU
Subject: Ping script

Hi everyone,

Does anyone have a ping script on hand that if no replies are given, to send
a page to a pager?

Thanks,

--
Tony Osmolowski
Network Administrator


This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:16:00 EDT