From: Stephen L Johnson <sjohnson@monsters.org>
Date: Mon, 31 May 1999 18:42:23 +0000 (+0000)
Subject: Initial import
X-Git-Tag: start~45
X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=608620e3bd80116d7b669ee052d37eb94449231e;p=spong.git

Initial import
---

diff --git a/src/lib/Spong/Network/plugins/check_ping b/src/lib/Spong/Network/plugins/check_ping
new file mode 100755
index 0000000..66cb749
--- /dev/null
+++ b/src/lib/Spong/Network/plugins/check_ping
@@ -0,0 +1,59 @@
+# Register the function the plugin registry
+$PLUGINS{'ping'} = \&check_ping;
+
+# This routine checks connectivity.  It first trys to "ping" the machine via
+# a TCP echo using the Net::Ping module, if it can't reach it via that 
+# mechanism, then it resorts to the command line ping program.  Using the
+# Net::Ping speeds things up quite a bit...
+
+sub check_ping { 
+   my( $host ) = @_;
+   my( $color, $rt, $summary, $message ) = ( "green", "", "", "" );
+   my( @down );
+
+   if( @{$HOSTS{$host}->{'ip_addr'}} ) {
+      @hostlist = @{$HOSTS{$host}->{'ip_addr'}};
+   } else {
+      @hostlist = ( $host );
+   }
+
+   foreach $host ( @hostlist ) {
+      if( ! pingecho( $host, 3 ) ) {
+	 # Try again with a IMCP ping, for those goofy machines that don't run
+	 # the echo service...  This is a bit slower...  Make sure your ping
+	 # program only sends a finite number of pings...
+      
+	 my $myping = $PING;
+	 my $pingok = 0;
+	 $myping =~ s/HOST/$host/g;
+
+	 $SIG{'ALRM'} = sub { die };
+	 alarm(5);
+
+	 eval <<'_EOM_';
+	 open( PING, "$myping 2>&1 |") || warn "can't call ping: $!";
+	 while( <PING> ) { 
+	    $message .= $_; 
+	    if( /bytes from/ ) { $pingok = 1; }
+            if( /is alive/ )   { $pingok = 1; }
+	 }
+_EOM_
+	 alarm(0);
+	 close PING;
+
+	 if( ! $pingok ) { 
+	    $color = "red"; 	   
+	    $message .= "\n";
+	    push( @down, $host );
+	 }
+      }
+   }
+
+   $summary = "ping failed for " . join( ',', @down ) if $color eq "red";
+   $summary = "ping ok"                               if $color eq "green";
+   
+   &debug( "ping - $host - $color, $summary" );
+   return( $color, $summary, $message );
+}
+
+1;