#!/bin/sh
#
# getping.sh - ping servers and produce a html report. Unix/Linux.
#
# 24-Jul-2004 ver 1.10
#
# USAGE: getping.sh > website.html # eg, triggered by crontab
#
# Configuration is through variables in this script. By default it pings
# servers found in /etc/hosts, which can be changed to an explicit list
# in the "hostlist" variable, or to a file containing a text list of
# hosts by updating the "hostfile" variable. (hostfile is easiest).
#
# The website is colour coded, and is generated by some lines of shell code.
# The HTML can be customised to taste, by editing the subroutines.
#
# Standard Disclaimer: this is freeware, use at your own risk.
#
# 25-Jun-2003 Brendan Gregg Created this.
#
# --- Config Variables ---
#
# hostlist - hosts to ping, eg hostlist="venus earth mars"
#
hostlist=""
# hostfile - text file which is a list of hosts to ping, eg hostfile="prod.txt"
#
hostfile=""
ping=/usr/sbin/ping # Location of ping
hostsdb=/etc/hosts # System hosts file, default hosts to ping
timeout=5 # Default ping timeout, seconds
PATH=/bin:$PATH
#
# --- Subroutines ---
#
# htmlhead - print HTML header.
#
htmlhead() {
echo "
Get Ping
Ping began at `date`
"
}
# htmltail - print HTML footer.
#
htmltail() {
echo "
Completed at `date`
"
}
# ping2html - process the output of ping to be in HTML.
#
ping2html() {
sed 's/.*no answer.*/ \ &<\/FONT>/
s/.*is alive.*/ \ &<\/FONT>/
s/.*unknown host.*/ \ &<\/FONT>/
s/$/
/'
}
# Note: The HTML choosen here is fairly basic. An improvement that may spring
# to mind is to add a table for the results - however this stops some browsers
# drawing partial results.
#
# --- Check ping exists ---
#
if [ ! -x $ping ]
then
ping=/bin/ping
if [ ! -x $ping ]; then
echo >&2 "ERROR1: Can't find ping. See script line 77."
exit 1
fi
fi
#
# --- Fetch hosts to ping ---
#
if [ "$hostfile" != "" ]; then
if [ ! -r $hostfile ]; then
echo >&2 "ERROR2: $hostfile, is not readable."
exit 2
fi
hosts=`cat $hostfile` # Use hostfile for list of hosts
fi
if [ "$hostlist" != "" ]; then # hostlist gets preference
hosts="$hostlist" # ("if" is easier to follow than
fi # Bourne parameter substitution)
if [ "$hosts" = "" ]; then # if unset, fetch hosts from default
if [ ! -r "$hostsdb" ]; then
echo >&2 "ERROR3: $hostsdb, is not readable."
exit 2
fi
hosts=`awk '/^[0-9]/ { print $1 }' $hostsdb`
fi
#
# --- MAIN - Ping hosts, make website ---
#
htmlhead
for host in $hosts
do
echo "$host,
"
$ping $host $timeout 2>&1 | ping2html
done
htmltail