# Register the routine with the plugin registry
$PLUGINS{'ntp'} = \&check_ntp;

# This check will check the status of the NTP service on a system. It uses
# the ntpdate command to query the service. If the offset is more then 1 
# second, it is a yellow status. If the host is not synchonized, it is red
# status

# $Id: check_ntp,v 1.5 2001/08/07 18:41:05 sljohnson Exp $

use Spong::SafeExec qw(safe_exec);

sub check_ntp {
    my( $host ) = @_;
    my( $color, $summary, $message ) = ( "green", "ntp ok", "" );

    $cmd = "$NTPDATE $host 2>&1";

    my $message = safe_exec($cmd);

    if ( $? != 0 ) {
       $color = "red"; $summary = "NTP server down";
       { 
          local $/;
          undef $/;
          if ($message =~
                /server (.*), stratum (.*), offset (.*), delay (.*)/) {
              my $offset = $3; my $stratum = $2;
              if ( $stratum eq "16" ) {
                  $color = 'yellow';
                  $summary = "Server is running, but not synchronized";
              } elsif (abs($offset) >= 1.0) {
                 $color = "yellow"; $summary = "server is $offset seconds off";
              }
          }
       }
    }
  
   &debug( "ntp - $host - $color, $summary" );
   return( $color, $summary, $message );
}



1;

