<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="index" title="Text File" newcontext="true">
<p>
#!/usr/local/bin/perl
</p>
<p>$Snoophosts   = &quot;/usr/lib/snoophosts&quot;;
$Snoopresult  = &quot;/usr/lib/snoopresult&quot;;
</p>
<p>#$Snoophosts   = &quot;/user2/lindner/snoop/snoophosts&quot;;
#$Snoopresult  = &quot;/user2/lindner/snoop/snoopresult&quot;;
</p>
<p>$Humans       = &quot;gopher@your.site.here&quot;;
$BeeperNo     = &quot;xxx-xxxx&quot;;
</p>
<p></p>
<p>push(@message, &quot;\n&quot;);
sub OpenSock {
    local($them,$port,$query) = @_;
    $them = &#x27;localhost&#x27; unless them;
    $port = 43 unless $port;
</p>
<p>    $AF_INET = 2;
    $SOCK_STREAM = 1;
</p>
<p>    $SIG{&#x27;INT&#x27;} = &#x27;dokill&#x27;;
</p>
<p>    $sockaddr = &#x27;S n a4 x8&#x27;;
</p>
<p>    chop($hostname = `hostname`);
</p>
<p>    ($name,$aliases,$proto) = getprotobyname(&#x27;tcp&#x27;);
    ($name,$aliases,$port) = getservbyname($port,&#x27;tcp&#x27;)
        unless $port =~ /^\d+$/;;
    ($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
    ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
</p>
<p>    $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
    $that = pack($sockaddr, $AF_INET, $port, $thataddr);
</p>
<p>    # Make the socket filehandle.
    socket(SOCK, $AF_INET, $SOCK_STREAM, $proto) || return $!;
</p>
<p>    # Give the socket an address.
    bind(SOCK, $this) || return $!;
</p>
<p>    # Call up the server.
    connect(SOCK,$that) || return $!;
</p>
<p>    # Set socket to be command buffered.
    select(SOCK); $| = 1; select(STDOUT);
</p>
<p>    # send the whois query
    print SOCK &quot;$query\r\n&quot;;
    return &quot;is up&quot;;
}
</p>
<p>open(Snoophosts, &quot;&lt;$Snoophosts&quot;) || die &quot;No hosts to snoop on\n&quot;;
</p>
<p>$SeverityLevel = 0;
</p>
<p>while (&lt;Snoophosts&gt;) {
	chop;
	next if /^#/;
</p>
<p>	($Host, $Port, $Importance) = split;
</p>
<p>        $key = &quot;$Host $Port&quot;;
</p>
<p>	$Importance{$key} = $Importance;
}
</p>
<p>#
# Read the old status.
#
</p>
<p>system(&quot;touch $Snoopresult&quot;);
open(Snoopresult, &quot;+&lt;$Snoopresult&quot;);
chop($OldSeverity = &lt;Snoopresult&gt;);
</p>
<p>while (&lt;Snoopresult&gt;) {
	chop;
	m/^(\S* \S*) (.*)$/;
	$key = $1;
	$status = $2;
</p>
<p>	$OldStatus{$key} = $status;
}
#
# Test each host/port
#
</p>
<p>foreach (keys(%Importance)) {
	$key = $_;
</p>
<p>	$Host = $_;   $Host =~ s/ .*$//;
	$Port = $_;   $Port =~ s/^.* //;
</p>
<p></p>
<p>	$result = &amp;OpenSock($Host, $Port, &quot;&quot;);
	if ($result ne &quot;is up&quot;) {
		$SeverityLevel += $Importance{$_};
        }
	if ($result ne $OldStatus{$key}) {
		if ($result eq &quot;is up&quot;) {
			push(@message, &quot;$Host, port $Port came back up\n&quot;);
		} else {
			push(@message, &quot;** $Host, port $Port went down: $result\n&quot;);
		}
	}
</p>
<p>	$Newstatus{$key} = $result;
}
</p>
<p>#
# Write out the new results file
#
seek(Snoopresult, 0, 0);
truncate(Snoopresult,0);
</p>
<p>print Snoopresult $SeverityLevel . &quot;\n&quot;;
foreach $key (keys(%Importance)) {
	print Snoopresult &quot;$key $Newstatus{$key}\n&quot;;
}
</p>
<p></p>
<p>#
# Okay, now lets inform the humans
#
</p>
<p>if ($SeverityLevel == 0 &amp;&amp; $OldSeverity != 0) {
	push(@message, &quot;All machines are up again\n&quot;);
	&amp;DialBeeper(0);
} else {
        if ($#message &gt; 0) {
            push(@message, &quot;Some machines are still down\n&quot;);
        }
}
</p>
<p>#
# Mail to the humans a status report at the end of the day.
#
</p>
<p>($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
</p>
<p>if ($hour == 16 &amp;&amp; $min &lt; 15) {
	open(Mail,&quot;|mail $Humans&quot;);
	print Mail &quot;Subject: Gopher is alive\n\n&quot;;
	foreach $key (keys(%Importance)) {
       	 	print Mail &quot;$key $Newstatus{$key}\n&quot;;
	}
}
</p>
<p>if ($#message &gt; 0) {
	open(Mail, &quot;|mail $Humans&quot;);
	print Mail &quot;Subject: *** Gopher Trouble Update ***\n&quot;;
	print Mail &quot;Priority: Urgent\n\n&quot;;
	print Mail @message;
</p>
<p>	print Mail &quot;\nCurrently down Machines\n&quot;;
	print Mail &quot;-----------------------\n\n&quot;;
	foreach $key (keys(%Importance)) {
		if (!($Newstatus{$key} =~ /is up/)) {
			print Mail &quot;$key $Newstatus{$key}\n&quot;;
		}
	}
}
</p>
<p>#
# Ring the beeper
#
</p>
<p>if ($SeverityLevel &gt; 0 &amp;&amp; $OldSeverity != $SeverityLevel) {
	$severe = $SeverityLevel/10;
	$severe = 1 if ($severe == 1);
	$severe = 9 if ($severe &gt; 9);
</p>
<p>	$severe =~ s/\..*$//;
	&amp;DialBeeper($severe);
}
</p>
<p>#
#  Procedure to dial the beeper
#
</p>
<p>sub DialBeeper {
	local($Severity) = @_;
</p>
<p>	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	if ($hour &lt; 10) { $hour = &quot;0&quot; . $hour;}
	local($Num)= &quot;$hour$min&quot;;
</p>
<p>	while ($i &lt;5) {
		$Num = $Num . $Severity;
		$i ++;
  	}
</p>
<p>#
# fixed this to explicitly use the ATDT command because cu has been
# intermittently pulse dialing (ATD rather than ATDT)
#				-mpm
#	open(BeeperCmd, &quot;|cu -s 1200 $BeeperNo,,,,,$Num,&gt;/dev/null  2&gt;&amp;1&quot;);
	open(BeeperCmd, &quot;|cu -s 1200 -l tty0 dir &gt; /dev/null	2&gt;&amp;1&quot;);
	print BeeperCmd &quot;ATDT $BeeperNo,,,,,$Num,,\r&quot;;
#
#
# end of patch (-mpm)
#
	print BeeperCmd &quot;\r~.\r\r&quot;;
	close BeeperCmd;
}
</p>
</card>
</wml>
