#!/bin/bash

if [ -f /etc/portstatus.conf ]; then
	. /etc/portstatus.conf
fi
PSPATH=${PSPATH:-"/var/state/portstatus/"}				; if [ ! -d "$PSPATH" ]; then mkdir -p $PSPATH ; fi
HOSTSFILE=${HOSTSFILE:-"/var/state/portstatus/hosts"}	; if [ ! -f "$HOSTSFILE" ]; then touch $HOSTSFILE ; fi
WARNEMAIL=${WARNEMAIL:-"root@localhost"}
	
clear
for ONEIP in `cat $PSPATH/hosts | grep '##' | sed -e 's/#.*//' | awk '{print $1}'` ; do
	HOSTLINE=`egrep "^$ONEIP[^0-9]" $PSPATH/hosts | tail --lines=1`
	HOSTNAME=`echo $HOSTLINE | sed -e 's/#.*//' | awk '{print $2}'`
	HOSTNAME=${HOSTNAME:-$ONEIP}
	CHECKSERVICES=`echo $HOSTLINE | sed -e 's/.*##//' -e 's/,/ /'`
	echo $HOSTNAME - `stat $PSPATH/$ONEIP/HOST-REACHABLE 2>/dev/null | grep '^Modify' | sed -e 's/.*(//' -e 's/)//'`
	for SERVICE in $CHECKSERVICES ; do
		MTIME=`stat $PSPATH/$ONEIP/$SERVICE 2>/dev/null | grep '^Modify' | sed -e 's/.*(//' -e 's/)//'`
		case $MTIME in
		00000.00:00:*)	:								;;
		00000.00:01:*)	:								;;
		00000.00:02:*)
			echo -e "\t$SERVICE\t$MTIME"
														;;
		00000.00:03:*)
			echo -e "\t$SERVICE\t$MTIME"
														;;
		*)
			echo -e "\t$SERVICE\t$MTIME"
			echo -n -e "\a"
			mail -s "$HOSTNAME - $SERVICE - $MTIME" $WARNEMAIL </dev/null 2>/dev/null
														;;
		esac
	done
done
