--- /dev/null
+# 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;