/*
 * WatchForFailingSitesAndPage
 *
 * Copyright  1993 by Christopher A. Wichura (caw@miroc.chi.il.us)
 * All rights reserved.
 *
 * This script will monitor the UUSPOOL:LOGFILE generated by AmigaUUCP
 * and keep track of how many times attempts to dial a site have failed.
 * If the number of failed attempts exceeds the maximum set for the
 * site (see configuration below) then a page will be spooled by
 * shelling out to 'spoolpage'.
 *
 * This script should be started in a fashion similar to:
 *
 *    RunWSH >nil: watch UUSPOOL:LOGFILE | WatchForFailingSitesAndPage
 * or
 *    Run >nil: watch >"APIPE:rx WatchForFailingSitesAndPage" UUSPOOL:LOGFILE
 */

/*
 * This section contains configuration information for your particular
 * site.  First you must supply the hostname of your machine.  Then,
 * for each site that you dial, you must have an entry in the
 * pageFailLevel and pageWho stems.  These tell this script a) how many
 * failures may occur for the site before a page is to be generated,
 * and b) who to send the page message to, respectively.
 */

/* name of machine this script is running on */
hostname = 'miroc.chi.il.us'

/* this indicates how often we should send a warning page out.  every
   pageFailLevel. failures a page will be generated. */

pageFailLevel.clout = 3
pageFailLevel.amiserv = 3

/* the pageWho stem tells this script who to send pages to when it wants to
   send something out.  you can specifiy multiple targets by separating
   then with spaces. */

pageWho.clout = 'caw'
pageWho.amiserv = 'caw brianv'

/*
 * No general user modifiable code is found below this point.
 * Make changes here at your own risk.
 */

do until eof(stdin)
	line = readln(stdin)
	call doLine(line)
end

exit

/*
 * The doLine procedure actually looks at the line read from the logfile
 * and decides if anything needs to be done.
 */

doLine: procedure expose hostname pageFailLevel. pageWho. siteFailCount.
	parse arg . prog','machine','who message

	/*
	 * If the message wasn't generated by uucico then we can forget
	 * about it.
	 */

	if strip(prog) ~= 'uucico' then
		return 0

	/*
	 * Get the variables we use into a clean state.
	 */

	machine = strip(machine)
	machineUC = upper(machine)
	who = strip(who)
	message = strip(message)

	/*
	 * Look for SUCCEEDED call messages.  reset fail count when we
	 * see one.
	 */

	if left(message, 14) = 'SUCCEEDED call' then do
		siteFailCount.machineUC = 0
		return 0
	end

	/*
	 * Look for FAILED call messages.  If found, update the fail
	 * count and send a page if needed.
	 */

	if left(message, 11) = 'FAILED call' then do
		failCount = siteFailCount.machineUC
		if ~datatype(failCount, 'N') then
			failCount = 1
		else
			failCount = failCount + 1

		failLevel = pageFailLevel.machineUC

		if (failCount // failLevel) = 0 then do
			msg = 'This page was generated automatically by'
			msg = msg hostname ||'.  Attempts by uucico to dial'
			msg = msg '`'machine'''' 'have failed' failCount
			msg = msg 'consecutive times in a row.'

			address command 'spoolpage' pageWho.machineUC 'message' msg
		end

		siteFailCount.machineUC = failCount
		return 0
	end

	/*
	 * Not a message we are interested in so ignore it.
	 */

	return 0

