Check_NTP issues

Rene Fertig me at renux.de
Fri May 5 15:18:08 CEST 2006


Am Freitag, 5. Mai 2006 14:36 schrieb Tiago Cruz:
> Now, I'm just try find a way to monitory all the servers and discover how
> have a unsynchronized clock, but I'm not sure that what I have to do :)

I've made a script (long before I began to use nagios) which is run by cron 
twice a day on every host which runs xntpd. It checks if ntpd is running, if 
the host has a reference clock and if it is synchronized to it and so on. It 
logs to syslog and send a Mail on failure.
Maybe it is useful to you:

-- snipp -----------------------------------------------------------------
#!/usr/bin/perl
# #!/usr/nsue/contrib/perl/bin/perl
# $Header: /usr/local/share/cvs/Tools/chktime,v 1.6 2006/03/08 14:14:36 
rfertig Exp $
# (c)2001 Rene Fertig <rfertig at telebel.net>
# Dieses Script prüft
# 1. ob der xntpd läuft
# 2a. ob dieser eine Reference-Clock hat
# 2b. ob eine Verbindung zu dieser aufgebaut werden kann
# 3. ob die System-Zeit von der Referenz-Zeit abweicht
# 4. ob die System-Zeit von der CMOS-Uhr abweicht
# Das Ergebnis wird an den syslog-Demon mit dem Facility local3.info
# übergeben.
# --------------------------------------------------------------------
use Getopt::Std;
use Sys::Hostname;
my $host = hostname();


$facility = "local3.info";

# Absender für die Mail
my $sender = "chktime";

# Mailprogramm:
my $mailer = "/usr/sbin/sendmail -t";

# BCC:
my $bcc = "admin\@company.tld";

# Reply-to
my $replyto = "admin\@company.tld";

# ---- Parameter verarbeiten ----------------------------------------
$usage = "USAGE: $0 [-d] [-m mail_to]\n\t-d\tdebug-mode\n\t-m\tsend 
error-messages via mail\n";

unless(getopts("m:dh?", \%option)) {
  die "$usage";
}

if(defined $option{m}) {
  $mailto = $option{m};
} else {
  $mailto = "";
}

if(defined $option{d}) {
  $debug = 1;
} else {
  $debug = 0;
}

if(defined $option{h} or defined $option{"?"}) {
  die "$usage";
}
# -------------------------------------------------------------------
my $error = 0;

if($peers = `ntpq -p`) {
  $_ = $peers;
  /\n\*([\w-]+)[\s\.]/;

  if($1 ne "") {
    $refclk = $1;
  } else {
    $refclk = "No RefClk!";
    $error = 1;
  }
} else {
  $refclk = "Unable to run ntpq! Is xntpd running?";
  $error = 1;
}

print "Reference-Clock: $refclk\n" if($debug);

if($refclk ne "No RefClk!") {
  open NDATE, "ntpdate -qd $refclk 2>/dev/null |"
    or die "�ger mit ntpdate: $!\n";

  while(<NDATE>) {
    chomp;
  #  print "NDATE: $_\n";
    SWITCH: {
      /^originate timestamp:/ && do {
                                      /^.+:\s+(\w+\.\w+)\s+([\w ,:.]+)$/;
                                      if($1 eq "00000000.00000000") {
                                        $reftime = "not available";
                                        $error = 1;
                                      } else {
                                        $reftime = $2;
                                      }
                                      last SWITCH;
                                    };

      /^transmit timestamp:/ && do {
                                      /^.+:\s+(\w+\.\w+)\s+([\w ,:.]+)$/;
                                      if($1 eq "00000000.00000000") {
                                        $systime = "not available";
                                        $error = 1;
                                      } else {
                                        $systime = $2;
                                      }
                                      last SWITCH;
                                    };

      /^offset /              && do {
                                      /^offset\s+([\w .-]+)$/;
                                      $offset = $1;
                                      last SWITCH;
                                    };

    }

  }
}


if($debug) {
  print "Ref-Time: $reftime\n";
  print "Sys-Time: $systime\n";
  print "Offset  : $offset sec.\n";
}

if($hwclock=`hwclock`) {
  # nix
} else {
  $hwclock="not available";
  $error = 1;
}

print "HW-Time: $hwclock\n" if($debug);

if($refclk ne "No RefClk!") {
  $logstr = "RefClk=$refclk; RefTime=$reftime; SysTime=$systime; 
Offset=$offset; HWClk=$hwclock;"
} else {
  $logstr = "RefClk=$refclk (cannot determine RefTime, SysTime and Offset); 
HWClk=$hwclock;"
}
system("/bin/logger -p $facility -t TIMECHECK \"$logstr\"") == 0
  or die "Upps, kann logger nicht ausführen! $?\n";

if($error && $mailto ne "") {
  open(MAIL,"|$mailer") or die "Upps, konnte Shell für Mail nicht �öffnen!";

  print MAIL "From: $sender\n";
  print MAIL "To: $mail_to\n";
  print MAIL "Bcc: $bcc\n";
  print MAIL "Reply-To: $replyto\n";
  print MAIL "Subject: chktime-failure at $host\n";
  print MAIL "\n";
  print MAIL $logstr;

  close MAIL;
}
-- snipp -----------------------------------------------------------------
Perhaps you have to correct some linebreaks which are made by the mailer.

Bye, René


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null





More information about the Users mailing list