check_ntp.pl
changeset 0 1798a8c4895a
child 3 cae04ac746e1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/check_ntp.pl	Fri Mar 04 13:42:14 2011 +0100
@@ -0,0 +1,185 @@
+#! /usr/bin/perl -w
+
+#    Copyright (C) 2011  Christian Arnold
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+#    Christian Arnold <arnold@schlittermann.de>
+
+use strict;
+use File::Basename;
+use Getopt::Long;
+use Net::NTP;
+use Pod::Usage;
+use if $ENV{DEBUG} => "Smart::Comments";
+
+my %ERRORS = (
+    OK        => 0,
+    WARNING   => 1,
+    CRITICAL  => 2,
+    UNKNOWN   => 3,
+    DEPENDENT => 4
+);
+
+sub get_time_delta($$);
+sub report($$);
+sub version($$);
+
+my $ME      = basename $0;
+my $VERSION = "2.0";
+
+my %opt = (
+    host     => "de.pool.ntp.org",
+    port     => "ntp",
+    warning  => 10,
+    critical => 60
+);
+
+MAIN: {
+    Getopt::Long::Configure('bundling');
+    GetOptions(
+        "w|warning=i"  => \$opt{warning},
+        "c|critical=i" => \$opt{critical},
+        "H|host=s"     => \$opt{host},
+        "p|port=s"     => \$opt{port},
+        "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
+        "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
+        "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
+    ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
+
+    ### %opt
+
+    if ( $opt{warning} > $opt{critical} ) {
+        print
+          "NTP CRITICAL: warning (-w) can't be greater than critical (-c)\n";
+        exit $ERRORS{CRITICAL};
+    }
+
+    my ( $delta, $rc ) = get_time_delta( $opt{host}, $opt{port} );
+    report( $delta, $rc );
+}
+
+sub get_time_delta($$) {
+    my ( $host, $port ) = @_;
+    my %response = ();
+
+    eval { %response = get_ntp_response( $host, $port ); };
+    print "NTP CRITICAL: $@" and exit $ERRORS{CRITICAL} if $@;
+
+    my $l_time = $response{"Originate Timestamp"};
+    my $r_time = $response{"Receive Timestamp"};
+    my $delta  = $l_time - $r_time;
+
+    my $rc = "OK";
+  SWITCH: {
+        $delta < -$opt{warning}  and $rc = "WARNING",  last;
+        $delta < -$opt{critical} and $rc = "CRITICAL", last;
+        $delta > $opt{warning}   and $rc = "WARNING",  last;
+        $delta > $opt{critical}  and $rc = "CRITICAL", last;
+    }
+
+    $delta = sprintf "%.1fs", $delta;
+
+    return ( $delta, $rc );
+}
+
+sub report($$) {
+    my ( $delta, $rc ) = @_;
+
+    print "NTP $rc: time difference $delta\n";
+    exit $ERRORS{$rc};
+}
+
+sub version($$) {
+    my ( $progname, $version ) = @_;
+
+    print <<_VERSION;
+$progname version $version
+Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
+
+$ME comes with ABSOLUTELY NO WARRANTY. This is free software,
+and you are welcome to redistribute it under certain conditions.
+See the GNU General Public Licence for details.
+_VERSION
+}
+
+__END__
+
+=head1 NAME
+
+check_ntp - nagios plugin to compare the system time with the ntp server time
+
+=head1 SYNOPSIS
+
+check_ntp [-H|--host string]
+          [-p|--port string]
+          [-w|--warning int]
+          [-c|--critical int]
+          [-h|--help]
+          [-m|--man]
+          [-V|--version]
+
+=head1 OPTIONS
+
+=over
+
+=item B<-H>|B<--host> I<string>
+
+NTP server (default: de.pool.ntp.org)
+
+=item B<-p>|B<--port> I<string>
+
+NTP port (default: ntp)
+
+=item B<-w>|B<--warning> I<int>
+
+Time difference in seconds to generate warning alert (default: 10)
+
+=item B<-c>|B<--critical> I<int>
+
+Time difference in seconds to generate critical alert (default: 60)
+
+=item B<-h>|B<--help>
+
+Print detailed help screen.
+
+=item B<-m>|B<--man>
+
+Print manual page.
+
+=item B<-V>|B<--version>
+
+Print version information.
+
+=back
+
+=head1 DESCRIPTION
+
+This plugin compares the system time with the ntp server time.
+
+=head1 VERSION
+
+This man page is current for version 2.0 of check_ntp.
+
+=head1 AUTHOR
+
+Written by Christian Arnold L<arnold@schlittermann.de>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
+This is free software, and you are welcome to redistribute it under certain conditions.
+See the GNU General Public Licence for details.
+
+=cut