|
1 #! /usr/bin/perl -w |
|
2 |
|
3 # Copyright (C) 2011 Christian Arnold |
|
4 # |
|
5 # This program is free software: you can redistribute it and/or modify |
|
6 # it under the terms of the GNU General Public License as published by |
|
7 # the Free Software Foundation, either version 3 of the License, or |
|
8 # (at your option) any later version. |
|
9 # |
|
10 # This program is distributed in the hope that it will be useful, |
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 # GNU General Public License for more details. |
|
14 # |
|
15 # You should have received a copy of the GNU General Public License |
|
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 # |
|
18 # Christian Arnold <arnold@schlittermann.de> |
|
19 |
|
20 use strict; |
|
21 use File::Basename; |
|
22 use Getopt::Long; |
|
23 use Net::NTP; |
|
24 use Pod::Usage; |
|
25 use if $ENV{DEBUG} => "Smart::Comments"; |
|
26 |
|
27 my %ERRORS = ( |
|
28 OK => 0, |
|
29 WARNING => 1, |
|
30 CRITICAL => 2, |
|
31 UNKNOWN => 3, |
|
32 DEPENDENT => 4 |
|
33 ); |
|
34 |
|
35 sub get_time_delta($$); |
|
36 sub report($$); |
|
37 sub version($$); |
|
38 |
|
39 my $ME = basename $0; |
|
40 my $VERSION = "2.0"; |
|
41 |
|
42 my %opt = ( |
|
43 host => "de.pool.ntp.org", |
|
44 port => "ntp", |
|
45 warning => 10, |
|
46 critical => 60 |
|
47 ); |
|
48 |
|
49 MAIN: { |
|
50 Getopt::Long::Configure('bundling'); |
|
51 GetOptions( |
|
52 "w|warning=i" => \$opt{warning}, |
|
53 "c|critical=i" => \$opt{critical}, |
|
54 "H|host=s" => \$opt{host}, |
|
55 "p|port=s" => \$opt{port}, |
|
56 "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, |
|
57 "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, |
|
58 "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } |
|
59 ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); |
|
60 |
|
61 ### %opt |
|
62 |
|
63 if ( $opt{warning} > $opt{critical} ) { |
|
64 print |
|
65 "NTP CRITICAL: warning (-w) can't be greater than critical (-c)\n"; |
|
66 exit $ERRORS{CRITICAL}; |
|
67 } |
|
68 |
|
69 my ( $delta, $rc ) = get_time_delta( $opt{host}, $opt{port} ); |
|
70 report( $delta, $rc ); |
|
71 } |
|
72 |
|
73 sub get_time_delta($$) { |
|
74 my ( $host, $port ) = @_; |
|
75 my %response = (); |
|
76 |
|
77 eval { %response = get_ntp_response( $host, $port ); }; |
|
78 print "NTP CRITICAL: $@" and exit $ERRORS{CRITICAL} if $@; |
|
79 |
|
80 my $l_time = $response{"Originate Timestamp"}; |
|
81 my $r_time = $response{"Receive Timestamp"}; |
|
82 my $delta = $l_time - $r_time; |
|
83 |
|
84 my $rc = "OK"; |
|
85 SWITCH: { |
|
86 $delta < -$opt{warning} and $rc = "WARNING", last; |
|
87 $delta < -$opt{critical} and $rc = "CRITICAL", last; |
|
88 $delta > $opt{warning} and $rc = "WARNING", last; |
|
89 $delta > $opt{critical} and $rc = "CRITICAL", last; |
|
90 } |
|
91 |
|
92 $delta = sprintf "%.1fs", $delta; |
|
93 |
|
94 return ( $delta, $rc ); |
|
95 } |
|
96 |
|
97 sub report($$) { |
|
98 my ( $delta, $rc ) = @_; |
|
99 |
|
100 print "NTP $rc: time difference $delta\n"; |
|
101 exit $ERRORS{$rc}; |
|
102 } |
|
103 |
|
104 sub version($$) { |
|
105 my ( $progname, $version ) = @_; |
|
106 |
|
107 print <<_VERSION; |
|
108 $progname version $version |
|
109 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. |
|
110 |
|
111 $ME comes with ABSOLUTELY NO WARRANTY. This is free software, |
|
112 and you are welcome to redistribute it under certain conditions. |
|
113 See the GNU General Public Licence for details. |
|
114 _VERSION |
|
115 } |
|
116 |
|
117 __END__ |
|
118 |
|
119 =head1 NAME |
|
120 |
|
121 check_ntp - nagios plugin to compare the system time with the ntp server time |
|
122 |
|
123 =head1 SYNOPSIS |
|
124 |
|
125 check_ntp [-H|--host string] |
|
126 [-p|--port string] |
|
127 [-w|--warning int] |
|
128 [-c|--critical int] |
|
129 [-h|--help] |
|
130 [-m|--man] |
|
131 [-V|--version] |
|
132 |
|
133 =head1 OPTIONS |
|
134 |
|
135 =over |
|
136 |
|
137 =item B<-H>|B<--host> I<string> |
|
138 |
|
139 NTP server (default: de.pool.ntp.org) |
|
140 |
|
141 =item B<-p>|B<--port> I<string> |
|
142 |
|
143 NTP port (default: ntp) |
|
144 |
|
145 =item B<-w>|B<--warning> I<int> |
|
146 |
|
147 Time difference in seconds to generate warning alert (default: 10) |
|
148 |
|
149 =item B<-c>|B<--critical> I<int> |
|
150 |
|
151 Time difference in seconds to generate critical alert (default: 60) |
|
152 |
|
153 =item B<-h>|B<--help> |
|
154 |
|
155 Print detailed help screen. |
|
156 |
|
157 =item B<-m>|B<--man> |
|
158 |
|
159 Print manual page. |
|
160 |
|
161 =item B<-V>|B<--version> |
|
162 |
|
163 Print version information. |
|
164 |
|
165 =back |
|
166 |
|
167 =head1 DESCRIPTION |
|
168 |
|
169 This plugin compares the system time with the ntp server time. |
|
170 |
|
171 =head1 VERSION |
|
172 |
|
173 This man page is current for version 2.0 of check_ntp. |
|
174 |
|
175 =head1 AUTHOR |
|
176 |
|
177 Written by Christian Arnold L<arnold@schlittermann.de> |
|
178 |
|
179 =head1 COPYRIGHT |
|
180 |
|
181 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. |
|
182 This is free software, and you are welcome to redistribute it under certain conditions. |
|
183 See the GNU General Public Licence for details. |
|
184 |
|
185 =cut |