#!/bin/sh
# ~/bin/.sh/ping_bsn	by jhs@ See Also ~/bin/.csh/ping_dsl
#
# Purpose 
#	To log a net connection if outages suspected.
# 
# Initial Manual Set Up on slim.berklix.org :
#	mkdir /var/log/ping
#	touch /var/log/ping/mail.bsn.com
#	touch /var/log/ping/flat.berklix.org
#	chown jhs:staff /var/log/ping /var/log/ping/*
#	chmod 750 /var/log/ping
#	chmod 640 /var/log/ping/*
#	touch /var/run/ping
#	chown jhs:staff /var/run/ping
#	chmod 640 /var/run/ping
# Group staff includes np@

# Consider later
#	If np@ request, I could move log to be seen from http://slim.berklix.org
#	Improvement ideas welcome.
# 	Later I might want to start it from rc.local.
# 	Maybe there's some better monitor in ports/ ?
#	inc. maybe something that mails alert addresses.

# Started manually with:
#	sh
#	nohup
#	ping_bsn
# Stopped manually with:
#	kill `cat /var/run/ping`

echo $$ > /var/run/ping
while ( true ) ; do

	for target in flat.berklix.org mail.bsn.com ; do
		# Alternately testing 2 hosts also tests both hosts as
		# well as link to site.
		# tower.berklix.org can be added to above 'for' list
		# to test the 'if' syntax below is working, because tower
		# is a resolvable name, that is not currently on net,
		# so ping will deliberately fail to it.

		date=`date -u +%Y-%m-%dT%H:%M:%SZ`
		# http://www.w3.org/QA/Tips/iso-date	ISO 8601

		ping=`ping -c 1 $target | head -2 | tail -1`
		echo -n "$date _ " >> /var/log/ping/$target
		if [ "X$ping" = "X" ]; then 
			echo "DEAD" >> /var/log/ping/$target
		else
			echo "$ping" >> /var/log/ping/$target
		fi
		sleep 300
		done
	done

# When examining logs, if a ping fails, log lines will end: DEAD.
