# 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...

# $Id: check_ping,v 1.10 2001/08/06 23:08:59 sljohnson Exp $

use Spong::SafeExec qw(safe_exec);

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 ) {
        my $myping = $PING;
        my $pingok = 0;
        $myping =~ s/HOST/$host/g;

        $message = safe_exec($myping);
        { local $/; undef $/;

          if( $message =~ /bytes from/ )  { $pingok = 1; }
          if( $message =~ /is alive/ )    { $pingok = 1; }
          if( $message =~ /octets from/ ) { $pingok = 1; }
        }

        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;
