home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / infoserv / gopher / Unix / GopherTools / gopherbeeper / snooper.Z / snooper
Encoding:
Text File  |  1993-10-06  |  4.3 KB  |  208 lines

  1. #!/usr/local/bin/perl
  2.  
  3. $Snoophosts   = "/usr/lib/snoophosts";
  4. $Snoopresult  = "/usr/lib/snoopresult";
  5.  
  6. #$Snoophosts   = "/user2/lindner/snoop/snoophosts";
  7. #$Snoopresult  = "/user2/lindner/snoop/snoopresult";
  8.  
  9. $Humans       = "gopher@your.site.here";
  10. $BeeperNo     = "xxx-xxxx";
  11.  
  12.  
  13. push(@message, "\n");
  14. sub OpenSock { 
  15.     local($them,$port,$query) = @_;
  16.     $them = 'localhost' unless them;
  17.     $port = 43 unless $port;
  18.  
  19.     $AF_INET = 2;
  20.     $SOCK_STREAM = 1;
  21.  
  22.     $SIG{'INT'} = 'dokill';
  23.  
  24.     $sockaddr = 'S n a4 x8';
  25.  
  26.     chop($hostname = `hostname`);
  27.  
  28.     ($name,$aliases,$proto) = getprotobyname('tcp');
  29.     ($name,$aliases,$port) = getservbyname($port,'tcp')
  30.         unless $port =~ /^\d+$/;;
  31.     ($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
  32.     ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
  33.  
  34.     $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
  35.     $that = pack($sockaddr, $AF_INET, $port, $thataddr);
  36.  
  37.     # Make the socket filehandle.
  38.     socket(SOCK, $AF_INET, $SOCK_STREAM, $proto) || return $!;
  39.  
  40.     # Give the socket an address.
  41.     bind(SOCK, $this) || return $!;
  42.  
  43.     # Call up the server.
  44.     connect(SOCK,$that) || return $!;
  45.  
  46.     # Set socket to be command buffered.
  47.     select(SOCK); $| = 1; select(STDOUT);
  48.  
  49.     # send the whois query
  50.     print SOCK "$query\r\n";
  51.     return "is up";
  52. }
  53.  
  54. open(Snoophosts, "<$Snoophosts") || die "No hosts to snoop on\n";
  55.  
  56. $SeverityLevel = 0;
  57.  
  58. while (<Snoophosts>) {
  59.     chop;
  60.     next if /^#/;
  61.  
  62.     ($Host, $Port, $Importance) = split;
  63.  
  64.         $key = "$Host $Port";
  65.  
  66.     $Importance{$key} = $Importance;
  67. }
  68.  
  69. #
  70. # Read the old status.
  71. #
  72.  
  73. system("touch $Snoopresult");
  74. open(Snoopresult, "+<$Snoopresult");
  75. chop($OldSeverity = <Snoopresult>);
  76.  
  77. while (<Snoopresult>) {
  78.     chop;
  79.     m/^(\S* \S*) (.*)$/;
  80.     $key = $1;
  81.     $status = $2;
  82.  
  83.     $OldStatus{$key} = $status;
  84. }    
  85. #
  86. # Test each host/port
  87. #
  88.  
  89. foreach (keys(%Importance)) {
  90.     $key = $_;
  91.  
  92.     $Host = $_;   $Host =~ s/ .*$//;
  93.     $Port = $_;   $Port =~ s/^.* //;
  94.  
  95.     
  96.     $result = &OpenSock($Host, $Port, "");
  97.     if ($result ne "is up") {
  98.         $SeverityLevel += $Importance{$_};
  99.         }
  100.     if ($result ne $OldStatus{$key}) {
  101.         if ($result eq "is up") {
  102.             push(@message, "$Host, port $Port came back up\n");
  103.         } else {
  104.             push(@message, "** $Host, port $Port went down: $result\n");
  105.         }
  106.     }
  107.  
  108.     $Newstatus{$key} = $result;
  109. }
  110.  
  111. #
  112. # Write out the new results file
  113. #
  114. seek(Snoopresult, 0, 0);
  115. truncate(Snoopresult,0);
  116.  
  117. print Snoopresult $SeverityLevel . "\n";
  118. foreach $key (keys(%Importance)) {
  119.     print Snoopresult "$key $Newstatus{$key}\n";
  120. }
  121.         
  122.  
  123. #
  124. # Okay, now lets inform the humans
  125. #
  126.  
  127. if ($SeverityLevel == 0 && $OldSeverity != 0) {    
  128.     push(@message, "All machines are up again\n");
  129.     &DialBeeper(0);
  130. } else {
  131.         if ($#message > 0) {
  132.             push(@message, "Some machines are still down\n");
  133.         }
  134. }
  135.  
  136. #
  137. # Mail to the humans a status report at the end of the day.
  138. #
  139.  
  140. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  141.  
  142. if ($hour == 16 && $min < 15) {
  143.     open(Mail,"|mail $Humans");
  144.     print Mail "Subject: Gopher is alive\n\n";
  145.     foreach $key (keys(%Importance)) {
  146.                 print Mail "$key $Newstatus{$key}\n";
  147.     }
  148. }
  149.  
  150. if ($#message > 0) {
  151.     open(Mail, "|mail $Humans");
  152.     print Mail "Subject: *** Gopher Trouble Update ***\n";
  153.     print Mail "Priority: Urgent\n\n";
  154.     print Mail @message;
  155.  
  156.     print Mail "\nCurrently down Machines\n";
  157.     print Mail "-----------------------\n\n";
  158.     foreach $key (keys(%Importance)) {
  159.         if (!($Newstatus{$key} =~ /is up/)) {
  160.             print Mail "$key $Newstatus{$key}\n";
  161.         }
  162.     }
  163. }
  164.  
  165. #
  166. # Ring the beeper
  167. #
  168.  
  169. if ($SeverityLevel > 0 && $OldSeverity != $SeverityLevel) {
  170.     $severe = $SeverityLevel/10;
  171.     $severe = 1 if ($severe == 1);
  172.     $severe = 9 if ($severe > 9);    
  173.  
  174.     $severe =~ s/\..*$//;
  175.     &DialBeeper($severe);
  176. }
  177.  
  178. #
  179. #  Procedure to dial the beeper
  180. #
  181.  
  182. sub DialBeeper {
  183.     local($Severity) = @_;
  184.      
  185.     ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  186.     if ($hour < 10) { $hour = "0" . $hour;}
  187.     local($Num)= "$hour$min";
  188.  
  189.     while ($i <5) {
  190.         $Num = $Num . $Severity;
  191.         $i ++;
  192.       }
  193.  
  194. #
  195. # fixed this to explicitly use the ATDT command because cu has been
  196. # intermittently pulse dialing (ATD rather than ATDT)
  197. #                -mpm
  198. #    open(BeeperCmd, "|cu -s 1200 $BeeperNo,,,,,$Num,>/dev/null  2>&1");
  199.     open(BeeperCmd, "|cu -s 1200 -l tty0 dir > /dev/null    2>&1");
  200.     print BeeperCmd "ATDT $BeeperNo,,,,,$Num,,\r";
  201. #
  202. #
  203. # end of patch (-mpm)
  204. #
  205.     print BeeperCmd "\r~.\r\r";
  206.     close BeeperCmd;
  207. }
  208.