diff options
author | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 (GMT) |
---|---|---|
committer | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 (GMT) |
commit | 44a321cb8a42d6c0ea2d96a1086a17f2134c89cc (patch) | |
tree | a1a4d9f7b92412a17ab08f34f04eec45433048b7 /plugins/check_time.c | |
parent | 54fd5d7022ff2d6a59bc52b8869182f3fc77a058 (diff) | |
download | monitoring-plugins-44a321cb8a42d6c0ea2d96a1086a17f2134c89cc.tar.gz |
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_time.c')
-rw-r--r-- | plugins/check_time.c | 370 |
1 files changed, 370 insertions, 0 deletions
diff --git a/plugins/check_time.c b/plugins/check_time.c new file mode 100644 index 0000000..86c414e --- /dev/null +++ b/plugins/check_time.c | |||
@@ -0,0 +1,370 @@ | |||
1 | /****************************************************************************** | ||
2 | * | ||
3 | * CHECK_TIME.C | ||
4 | * | ||
5 | * Program: Network time server plugin for Nagios | ||
6 | * License: GPL | ||
7 | * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org) | ||
8 | * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) | ||
9 | * | ||
10 | * $Id$ | ||
11 | * | ||
12 | * Description: | ||
13 | * | ||
14 | * This plugin will attempt to connect to the specified port | ||
15 | * on the host. Successul connects return STATE_OK, refusals | ||
16 | * and timeouts return STATE_CRITICAL, other errors return | ||
17 | * STATE_UNKNOWN. | ||
18 | * | ||
19 | * License Information: | ||
20 | * | ||
21 | * This program is free software; you can redistribute it and/or modify | ||
22 | * it under the terms of the GNU General Public License as published by | ||
23 | * the Free Software Foundation; either version 2 of the License, or | ||
24 | * (at your option) any later version. | ||
25 | * | ||
26 | * This program is distributed in the hope that it will be useful, | ||
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
29 | * GNU General Public License for more details. | ||
30 | * | ||
31 | * You should have received a copy of the GNU General Public License | ||
32 | * along with this program; if not, write to the Free Software | ||
33 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
34 | * | ||
35 | *****************************************************************************/ | ||
36 | |||
37 | #include "config.h" | ||
38 | #include "common.h" | ||
39 | #include "netutils.h" | ||
40 | #include "utils.h" | ||
41 | |||
42 | #define PROGNAME "check_time" | ||
43 | |||
44 | #define TIME_PORT 37 | ||
45 | #define UNIX_EPOCH 2208988800UL | ||
46 | |||
47 | unsigned long server_time, raw_server_time; | ||
48 | time_t diff_time; | ||
49 | int warning_time = 0; | ||
50 | int check_warning_time = FALSE; | ||
51 | int critical_time = 0; | ||
52 | int check_critical_time = FALSE; | ||
53 | unsigned long warning_diff = 0; | ||
54 | int check_warning_diff = FALSE; | ||
55 | unsigned long critical_diff = 0; | ||
56 | int check_critical_diff = FALSE; | ||
57 | int server_port = TIME_PORT; | ||
58 | char *server_address = NULL; | ||
59 | |||
60 | |||
61 | int process_arguments (int, char **); | ||
62 | int call_getopt (int, char **); | ||
63 | void print_usage (void); | ||
64 | void print_help (void); | ||
65 | |||
66 | |||
67 | int | ||
68 | main (int argc, char **argv) | ||
69 | { | ||
70 | int sd; | ||
71 | int result; | ||
72 | |||
73 | if (process_arguments (argc, argv) != OK) | ||
74 | usage ("Invalid command arguments supplied\n"); | ||
75 | |||
76 | /* initialize alarm signal handling */ | ||
77 | signal (SIGALRM, socket_timeout_alarm_handler); | ||
78 | |||
79 | /* set socket timeout */ | ||
80 | alarm (socket_timeout); | ||
81 | time (&start_time); | ||
82 | |||
83 | /* try to connect to the host at the given port number */ | ||
84 | if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) { | ||
85 | if (check_critical_time == TRUE) | ||
86 | result = STATE_CRITICAL; | ||
87 | else if (check_warning_time == TRUE) | ||
88 | result = STATE_WARNING; | ||
89 | else | ||
90 | result = STATE_UNKNOWN; | ||
91 | terminate (result, | ||
92 | "TIME UNKNOWN - could not connect to server %s, port %d\n", | ||
93 | server_address, server_port); | ||
94 | } | ||
95 | |||
96 | /* watch for the FTP connection string */ | ||
97 | result = recv (sd, &raw_server_time, sizeof (raw_server_time), 0); | ||
98 | |||
99 | /* close the connection */ | ||
100 | close (sd); | ||
101 | |||
102 | /* reset the alarm */ | ||
103 | time (&end_time); | ||
104 | alarm (0); | ||
105 | |||
106 | /* return a WARNING status if we couldn't read any data */ | ||
107 | if (result <= 0) { | ||
108 | if (check_critical_time == TRUE) | ||
109 | result = STATE_CRITICAL; | ||
110 | else if (check_warning_time == TRUE) | ||
111 | result = STATE_WARNING; | ||
112 | else | ||
113 | result = STATE_UNKNOWN; | ||
114 | terminate (result, | ||
115 | "TIME UNKNOWN - no data on recv() from server %s, port %d\n", | ||
116 | server_address, server_port); | ||
117 | } | ||
118 | |||
119 | result = STATE_OK; | ||
120 | |||
121 | if (check_critical_time == TRUE && (end_time - start_time) > critical_time) | ||
122 | result = STATE_CRITICAL; | ||
123 | else if (check_warning_time == TRUE | ||
124 | && (end_time - start_time) > warning_time) result = STATE_WARNING; | ||
125 | |||
126 | if (result != STATE_OK) | ||
127 | terminate (result, "TIME %s - %d second response time\n", | ||
128 | state_text (result), (int) (end_time - start_time)); | ||
129 | |||
130 | server_time = ntohl (raw_server_time) - UNIX_EPOCH; | ||
131 | if (server_time > end_time) | ||
132 | diff_time = server_time - end_time; | ||
133 | else | ||
134 | diff_time = end_time - server_time; | ||
135 | |||
136 | if (check_critical_diff == TRUE && diff_time > critical_diff) | ||
137 | result = STATE_CRITICAL; | ||
138 | else if (check_warning_diff == TRUE && diff_time > warning_diff) | ||
139 | result = STATE_WARNING; | ||
140 | |||
141 | printf ("TIME %s - %lu second time difference\n", state_text (result), | ||
142 | diff_time); | ||
143 | return result; | ||
144 | } | ||
145 | |||
146 | |||
147 | |||
148 | |||
149 | |||
150 | |||
151 | /* process command-line arguments */ | ||
152 | int | ||
153 | process_arguments (int argc, char **argv) | ||
154 | { | ||
155 | int c; | ||
156 | |||
157 | if (argc < 2) | ||
158 | usage ("\n"); | ||
159 | |||
160 | for (c = 1; c < argc; c++) { | ||
161 | if (strcmp ("-to", argv[c]) == 0) | ||
162 | strcpy (argv[c], "-t"); | ||
163 | else if (strcmp ("-wd", argv[c]) == 0) | ||
164 | strcpy (argv[c], "-w"); | ||
165 | else if (strcmp ("-cd", argv[c]) == 0) | ||
166 | strcpy (argv[c], "-c"); | ||
167 | else if (strcmp ("-wt", argv[c]) == 0) | ||
168 | strcpy (argv[c], "-W"); | ||
169 | else if (strcmp ("-ct", argv[c]) == 0) | ||
170 | strcpy (argv[c], "-C"); | ||
171 | } | ||
172 | |||
173 | c = 0; | ||
174 | while ((c += call_getopt (argc - c, &argv[c])) < argc) { | ||
175 | |||
176 | if (is_option (argv[c])) | ||
177 | continue; | ||
178 | |||
179 | if (server_address == NULL) { | ||
180 | if (argc > c) { | ||
181 | if (is_host (argv[c]) == FALSE) | ||
182 | usage ("Invalid host name/address\n"); | ||
183 | server_address = argv[c]; | ||
184 | } | ||
185 | else { | ||
186 | usage ("Host name was not supplied\n"); | ||
187 | } | ||
188 | } | ||
189 | } | ||
190 | |||
191 | return OK; | ||
192 | } | ||
193 | |||
194 | |||
195 | |||
196 | |||
197 | |||
198 | int | ||
199 | call_getopt (int argc, char **argv) | ||
200 | { | ||
201 | int c, i = 0; | ||
202 | |||
203 | #ifdef HAVE_GETOPT_H | ||
204 | int option_index = 0; | ||
205 | static struct option long_options[] = { | ||
206 | {"hostname", required_argument, 0, 'H'}, | ||
207 | {"warning-variance", required_argument, 0, 'w'}, | ||
208 | {"critical-variance", required_argument, 0, 'c'}, | ||
209 | {"warning-connect", required_argument, 0, 'W'}, | ||
210 | {"critical-connect", required_argument, 0, 'C'}, | ||
211 | {"port", required_argument, 0, 'p'}, | ||
212 | {"timeout", required_argument, 0, 't'}, | ||
213 | {"version", no_argument, 0, 'V'}, | ||
214 | {"help", no_argument, 0, 'h'}, | ||
215 | {0, 0, 0, 0} | ||
216 | }; | ||
217 | #endif | ||
218 | |||
219 | while (1) { | ||
220 | #ifdef HAVE_GETOPT_H | ||
221 | c = | ||
222 | getopt_long (argc, argv, "+hVH:w:c:W:C:p:t:", long_options, | ||
223 | &option_index); | ||
224 | #else | ||
225 | c = getopt (argc, argv, "+hVH:w:c:W:C:p:t:"); | ||
226 | #endif | ||
227 | |||
228 | i++; | ||
229 | |||
230 | if (c == -1 || c == EOF || c == 1) | ||
231 | break; | ||
232 | |||
233 | switch (c) { | ||
234 | case 'H': | ||
235 | case 'w': | ||
236 | case 'c': | ||
237 | case 'W': | ||
238 | case 'C': | ||
239 | case 'p': | ||
240 | case 't': | ||
241 | i++; | ||
242 | } | ||
243 | |||
244 | switch (c) { | ||
245 | case '?': /* print short usage statement if args not parsable */ | ||
246 | printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg); | ||
247 | print_usage (); | ||
248 | exit (STATE_UNKNOWN); | ||
249 | case 'h': /* help */ | ||
250 | print_help (); | ||
251 | exit (STATE_OK); | ||
252 | case 'V': /* version */ | ||
253 | print_revision (my_basename (argv[0]), "$Revision$"); | ||
254 | exit (STATE_OK); | ||
255 | case 'H': /* hostname */ | ||
256 | if (is_host (optarg) == FALSE) | ||
257 | usage ("Invalid host name/address\n"); | ||
258 | server_address = optarg; | ||
259 | break; | ||
260 | case 'w': /* warning-variance */ | ||
261 | if (is_intnonneg (optarg)) { | ||
262 | warning_diff = strtoul (optarg, NULL, 10); | ||
263 | check_warning_diff = TRUE; | ||
264 | } | ||
265 | else if (strspn (optarg, "0123456789:,") > 0) { | ||
266 | if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) { | ||
267 | check_warning_diff = TRUE; | ||
268 | check_warning_time = TRUE; | ||
269 | } | ||
270 | else { | ||
271 | usage ("Warning thresholds must be a nonnegative integer\n"); | ||
272 | } | ||
273 | } | ||
274 | else { | ||
275 | usage ("Warning threshold must be a nonnegative integer\n"); | ||
276 | } | ||
277 | break; | ||
278 | case 'c': /* critical-variance */ | ||
279 | if (is_intnonneg (optarg)) { | ||
280 | critical_diff = strtoul (optarg, NULL, 10); | ||
281 | check_critical_diff = TRUE; | ||
282 | } | ||
283 | else if (strspn (optarg, "0123456789:,") > 0) { | ||
284 | if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) == | ||
285 | 2) { | ||
286 | check_critical_diff = TRUE; | ||
287 | check_critical_time = TRUE; | ||
288 | } | ||
289 | else { | ||
290 | usage ("Critical thresholds must be a nonnegative integer\n"); | ||
291 | } | ||
292 | } | ||
293 | else { | ||
294 | usage ("Critical threshold must be a nonnegative integer\n"); | ||
295 | } | ||
296 | break; | ||
297 | case 'W': /* warning-connect */ | ||
298 | if (!is_intnonneg (optarg)) | ||
299 | usage ("Warning threshold must be a nonnegative integer\n"); | ||
300 | warning_time = atoi (optarg); | ||
301 | check_warning_time = TRUE; | ||
302 | break; | ||
303 | case 'C': /* critical-connect */ | ||
304 | if (!is_intnonneg (optarg)) | ||
305 | usage ("Critical threshold must be a nonnegative integer\n"); | ||
306 | critical_time = atoi (optarg); | ||
307 | check_critical_time = TRUE; | ||
308 | break; | ||
309 | case 'p': /* port */ | ||
310 | if (!is_intnonneg (optarg)) | ||
311 | usage ("Serevr port must be a nonnegative integer\n"); | ||
312 | server_port = atoi (optarg); | ||
313 | break; | ||
314 | case 't': /* timeout */ | ||
315 | if (!is_intnonneg (optarg)) | ||
316 | usage ("Timeout interval must be a nonnegative integer\n"); | ||
317 | socket_timeout = atoi (optarg); | ||
318 | break; | ||
319 | } | ||
320 | } | ||
321 | return i; | ||
322 | } | ||
323 | |||
324 | |||
325 | |||
326 | |||
327 | |||
328 | void | ||
329 | print_usage (void) | ||
330 | { | ||
331 | printf | ||
332 | ("Usage: check_time -H <host_address> [-p port] [-w variance] [-c variance]\n" | ||
333 | " [-W connect_time] [-C connect_time] [-t timeout]\n"); | ||
334 | } | ||
335 | |||
336 | |||
337 | |||
338 | |||
339 | |||
340 | void | ||
341 | print_help (void) | ||
342 | { | ||
343 | print_revision (PROGNAME, "$Revision$"); | ||
344 | printf | ||
345 | ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n" | ||
346 | "This plugin connects to a time port on the specified host.\n\n"); | ||
347 | print_usage (); | ||
348 | printf | ||
349 | ("Options:\n" | ||
350 | " -H, --hostname=ADDRESS\n" | ||
351 | " Host name argument for servers using host headers (use numeric\n" | ||
352 | " address if possible to bypass DNS lookup).\n" | ||
353 | " -w, --warning-variance=INTEGER\n" | ||
354 | " Time difference (sec.) necessary to result in a warning status\n" | ||
355 | " -c, --critical-variance=INTEGER\n" | ||
356 | " Time difference (sec.) necessary to result in a critical status\n" | ||
357 | " -W, --warning-connect=INTEGER\n" | ||
358 | " Response time (sec.) necessary to result in warning status\n" | ||
359 | " -C, --critical-connect=INTEGER\n" | ||
360 | " Response time (sec.) necessary to result in critical status\n" | ||
361 | " -t, --timeout=INTEGER\n" | ||
362 | " Seconds before connection times out (default: %d)\n" | ||
363 | " -p, --port=INTEGER\n" | ||
364 | " Port number (default: %d)\n" | ||
365 | " -h, --help\n" | ||
366 | " Print detailed help screen\n" | ||
367 | " -V, --version\n" | ||
368 | " Print version information\n\n", DEFAULT_SOCKET_TIMEOUT, TIME_PORT); | ||
369 | support (); | ||
370 | } | ||