summaryrefslogtreecommitdiffstats
path: root/plugins/check_time.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_time.c')
-rw-r--r--plugins/check_time.c370
1 files changed, 165 insertions, 205 deletions
diff --git a/plugins/check_time.c b/plugins/check_time.c
index f50ea42..41d2c49 100644
--- a/plugins/check_time.c
+++ b/plugins/check_time.c
@@ -1,32 +1,32 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_time plugin 3 * Monitoring check_time plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 1999-2007 Monitoring Plugins Development Team 6 * Copyright (c) 1999-2007 Monitoring Plugins Development Team
7* 7 *
8* Description: 8 * Description:
9* 9 *
10* This file contains the check_time plugin 10 * This file contains the check_time plugin
11* 11 *
12* This plugin will check the time difference with the specified host. 12 * This plugin will check the time difference with the specified host.
13* 13 *
14* 14 *
15* This program is free software: you can redistribute it and/or modify 15 * This program is free software: you can redistribute it and/or modify
16* it under the terms of the GNU General Public License as published by 16 * it under the terms of the GNU General Public License as published by
17* the Free Software Foundation, either version 3 of the License, or 17 * the Free Software Foundation, either version 3 of the License, or
18* (at your option) any later version. 18 * (at your option) any later version.
19* 19 *
20* This program is distributed in the hope that it will be useful, 20 * This program is distributed in the hope that it will be useful,
21* but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23* GNU General Public License for more details. 23 * GNU General Public License for more details.
24* 24 *
25* You should have received a copy of the GNU General Public License 25 * You should have received a copy of the GNU General Public License
26* along with this program. If not, see <http://www.gnu.org/licenses/>. 26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27* 27 *
28* 28 *
29*****************************************************************************/ 29 *****************************************************************************/
30 30
31const char *progname = "check_time"; 31const char *progname = "check_time";
32const char *copyright = "1999-2007"; 32const char *copyright = "1999-2007";
@@ -40,7 +40,7 @@ enum {
40 TIME_PORT = 37 40 TIME_PORT = 37
41}; 41};
42 42
43#define UNIX_EPOCH 2208988800UL 43#define UNIX_EPOCH 2208988800UL
44 44
45uint32_t raw_server_time; 45uint32_t raw_server_time;
46unsigned long server_time, diff_time; 46unsigned long server_time, diff_time;
@@ -56,39 +56,37 @@ int server_port = TIME_PORT;
56char *server_address = NULL; 56char *server_address = NULL;
57bool use_udp = false; 57bool use_udp = false;
58 58
59int process_arguments (int, char **); 59int process_arguments(int, char **);
60void print_help (void); 60void print_help(void);
61void print_usage (void); 61void print_usage(void);
62 62
63int 63int main(int argc, char **argv) {
64main (int argc, char **argv)
65{
66 int sd; 64 int sd;
67 int result = STATE_UNKNOWN; 65 int result = STATE_UNKNOWN;
68 time_t conntime; 66 time_t conntime;
69 67
70 setlocale (LC_ALL, ""); 68 setlocale(LC_ALL, "");
71 bindtextdomain (PACKAGE, LOCALEDIR); 69 bindtextdomain(PACKAGE, LOCALEDIR);
72 textdomain (PACKAGE); 70 textdomain(PACKAGE);
73 71
74 /* Parse extra opts if any */ 72 /* Parse extra opts if any */
75 argv=np_extra_opts (&argc, argv, progname); 73 argv = np_extra_opts(&argc, argv, progname);
76 74
77 if (process_arguments (argc, argv) == ERROR) 75 if (process_arguments(argc, argv) == ERROR)
78 usage4 (_("Could not parse arguments")); 76 usage4(_("Could not parse arguments"));
79 77
80 /* initialize alarm signal handling */ 78 /* initialize alarm signal handling */
81 signal (SIGALRM, socket_timeout_alarm_handler); 79 signal(SIGALRM, socket_timeout_alarm_handler);
82 80
83 /* set socket timeout */ 81 /* set socket timeout */
84 alarm (socket_timeout); 82 alarm(socket_timeout);
85 time (&start_time); 83 time(&start_time);
86 84
87 /* try to connect to the host at the given port number */ 85 /* try to connect to the host at the given port number */
88 if (use_udp) { 86 if (use_udp) {
89 result = my_udp_connect (server_address, server_port, &sd); 87 result = my_udp_connect(server_address, server_port, &sd);
90 } else { 88 } else {
91 result = my_tcp_connect (server_address, server_port, &sd); 89 result = my_tcp_connect(server_address, server_port, &sd);
92 } 90 }
93 91
94 if (result != STATE_OK) { 92 if (result != STATE_OK) {
@@ -98,34 +96,30 @@ main (int argc, char **argv)
98 result = STATE_WARNING; 96 result = STATE_WARNING;
99 else 97 else
100 result = STATE_UNKNOWN; 98 result = STATE_UNKNOWN;
101 die (result, 99 die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), server_address, server_port);
102 _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
103 server_address, server_port);
104 } 100 }
105 101
106 if (use_udp) { 102 if (use_udp) {
107 if (send (sd, "", 0, 0) < 0) { 103 if (send(sd, "", 0, 0) < 0) {
108 if (check_critical_time) 104 if (check_critical_time)
109 result = STATE_CRITICAL; 105 result = STATE_CRITICAL;
110 else if (check_warning_time) 106 else if (check_warning_time)
111 result = STATE_WARNING; 107 result = STATE_WARNING;
112 else 108 else
113 result = STATE_UNKNOWN; 109 result = STATE_UNKNOWN;
114 die (result, 110 die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), server_address, server_port);
115 _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
116 server_address, server_port);
117 } 111 }
118 } 112 }
119 113
120 /* watch for the connection string */ 114 /* watch for the connection string */
121 result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0); 115 result = recv(sd, (void *)&raw_server_time, sizeof(raw_server_time), 0);
122 116
123 /* close the connection */ 117 /* close the connection */
124 close (sd); 118 close(sd);
125 119
126 /* reset the alarm */ 120 /* reset the alarm */
127 time (&end_time); 121 time(&end_time);
128 alarm (0); 122 alarm(0);
129 123
130 /* return a WARNING status if we couldn't read any data */ 124 /* return a WARNING status if we couldn't read any data */
131 if (result <= 0) { 125 if (result <= 0) {
@@ -135,175 +129,150 @@ main (int argc, char **argv)
135 result = STATE_WARNING; 129 result = STATE_WARNING;
136 else 130 else
137 result = STATE_UNKNOWN; 131 result = STATE_UNKNOWN;
138 die (result, 132 die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), server_address, server_port);
139 _("TIME UNKNOWN - no data received from server %s, port %d\n"),
140 server_address, server_port);
141 } 133 }
142 134
143 result = STATE_OK; 135 result = STATE_OK;
144 136
145 conntime = (end_time - start_time); 137 conntime = (end_time - start_time);
146 if (check_critical_time&& conntime > critical_time) 138 if (check_critical_time && conntime > critical_time)
147 result = STATE_CRITICAL; 139 result = STATE_CRITICAL;
148 else if (check_warning_time && conntime > warning_time) 140 else if (check_warning_time && conntime > warning_time)
149 result = STATE_WARNING; 141 result = STATE_WARNING;
150 142
151 if (result != STATE_OK) 143 if (result != STATE_OK)
152 die (result, _("TIME %s - %d second response time|%s\n"), 144 die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime,
153 state_text (result), (int)conntime, 145 perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0,
154 perfdata ("time", (long)conntime, "s", 146 false, 0));
155 check_warning_time, (long)warning_time, 147
156 check_critical_time, (long)critical_time, 148 server_time = ntohl(raw_server_time) - UNIX_EPOCH;
157 true, 0, false, 0));
158
159 server_time = ntohl (raw_server_time) - UNIX_EPOCH;
160 if (server_time > (unsigned long)end_time) 149 if (server_time > (unsigned long)end_time)
161 diff_time = server_time - (unsigned long)end_time; 150 diff_time = server_time - (unsigned long)end_time;
162 else 151 else
163 diff_time = (unsigned long)end_time - server_time; 152 diff_time = (unsigned long)end_time - server_time;
164 153
165 if (check_critical_diff&& diff_time > critical_diff) 154 if (check_critical_diff && diff_time > critical_diff)
166 result = STATE_CRITICAL; 155 result = STATE_CRITICAL;
167 else if (check_warning_diff&& diff_time > warning_diff) 156 else if (check_warning_diff && diff_time > warning_diff)
168 result = STATE_WARNING; 157 result = STATE_WARNING;
169 158
170 printf (_("TIME %s - %lu second time difference|%s %s\n"), 159 printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time,
171 state_text (result), diff_time, 160 perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0,
172 perfdata ("time", (long)conntime, "s", 161 false, 0),
173 check_warning_time, (long)warning_time, 162 perfdata("offset", diff_time, "s", check_warning_diff, warning_diff, check_critical_diff, critical_diff, true, 0, false, 0));
174 check_critical_time, (long)critical_time,
175 true, 0, false, 0),
176 perfdata ("offset", diff_time, "s",
177 check_warning_diff, warning_diff,
178 check_critical_diff, critical_diff,
179 true, 0, false, 0));
180 return result; 163 return result;
181} 164}
182 165
183
184
185/* process command-line arguments */ 166/* process command-line arguments */
186int 167int process_arguments(int argc, char **argv) {
187process_arguments (int argc, char **argv)
188{
189 int c; 168 int c;
190 169
191 int option = 0; 170 int option = 0;
192 static struct option longopts[] = { 171 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
193 {"hostname", required_argument, 0, 'H'}, 172 {"warning-variance", required_argument, 0, 'w'},
194 {"warning-variance", required_argument, 0, 'w'}, 173 {"critical-variance", required_argument, 0, 'c'},
195 {"critical-variance", required_argument, 0, 'c'}, 174 {"warning-connect", required_argument, 0, 'W'},
196 {"warning-connect", required_argument, 0, 'W'}, 175 {"critical-connect", required_argument, 0, 'C'},
197 {"critical-connect", required_argument, 0, 'C'}, 176 {"port", required_argument, 0, 'p'},
198 {"port", required_argument, 0, 'p'}, 177 {"udp", no_argument, 0, 'u'},
199 {"udp", no_argument, 0, 'u'}, 178 {"timeout", required_argument, 0, 't'},
200 {"timeout", required_argument, 0, 't'}, 179 {"version", no_argument, 0, 'V'},
201 {"version", no_argument, 0, 'V'}, 180 {"help", no_argument, 0, 'h'},
202 {"help", no_argument, 0, 'h'}, 181 {0, 0, 0, 0}};
203 {0, 0, 0, 0}
204 };
205 182
206 if (argc < 2) 183 if (argc < 2)
207 usage ("\n"); 184 usage("\n");
208 185
209 for (c = 1; c < argc; c++) { 186 for (c = 1; c < argc; c++) {
210 if (strcmp ("-to", argv[c]) == 0) 187 if (strcmp("-to", argv[c]) == 0)
211 strcpy (argv[c], "-t"); 188 strcpy(argv[c], "-t");
212 else if (strcmp ("-wd", argv[c]) == 0) 189 else if (strcmp("-wd", argv[c]) == 0)
213 strcpy (argv[c], "-w"); 190 strcpy(argv[c], "-w");
214 else if (strcmp ("-cd", argv[c]) == 0) 191 else if (strcmp("-cd", argv[c]) == 0)
215 strcpy (argv[c], "-c"); 192 strcpy(argv[c], "-c");
216 else if (strcmp ("-wt", argv[c]) == 0) 193 else if (strcmp("-wt", argv[c]) == 0)
217 strcpy (argv[c], "-W"); 194 strcpy(argv[c], "-W");
218 else if (strcmp ("-ct", argv[c]) == 0) 195 else if (strcmp("-ct", argv[c]) == 0)
219 strcpy (argv[c], "-C"); 196 strcpy(argv[c], "-C");
220 } 197 }
221 198
222 while (true) { 199 while (true) {
223 c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts, 200 c = getopt_long(argc, argv, "hVH:w:c:W:C:p:t:u", longopts, &option);
224 &option);
225 201
226 if (c == -1 || c == EOF) 202 if (c == -1 || c == EOF)
227 break; 203 break;
228 204
229 switch (c) { 205 switch (c) {
230 case '?': /* print short usage statement if args not parsable */ 206 case '?': /* print short usage statement if args not parsable */
231 usage5 (); 207 usage5();
232 case 'h': /* help */ 208 case 'h': /* help */
233 print_help (); 209 print_help();
234 exit (STATE_UNKNOWN); 210 exit(STATE_UNKNOWN);
235 case 'V': /* version */ 211 case 'V': /* version */
236 print_revision (progname, NP_VERSION); 212 print_revision(progname, NP_VERSION);
237 exit (STATE_UNKNOWN); 213 exit(STATE_UNKNOWN);
238 case 'H': /* hostname */ 214 case 'H': /* hostname */
239 if (!is_host (optarg)) 215 if (!is_host(optarg))
240 usage2 (_("Invalid hostname/address"), optarg); 216 usage2(_("Invalid hostname/address"), optarg);
241 server_address = optarg; 217 server_address = optarg;
242 break; 218 break;
243 case 'w': /* warning-variance */ 219 case 'w': /* warning-variance */
244 if (is_intnonneg (optarg)) { 220 if (is_intnonneg(optarg)) {
245 warning_diff = strtoul (optarg, NULL, 10); 221 warning_diff = strtoul(optarg, NULL, 10);
246 check_warning_diff = true; 222 check_warning_diff = true;
247 } 223 } else if (strspn(optarg, "0123456789:,") > 0) {
248 else if (strspn (optarg, "0123456789:,") > 0) { 224 if (sscanf(optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
249 if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
250 check_warning_diff = true; 225 check_warning_diff = true;
251 check_warning_time = true; 226 check_warning_time = true;
227 } else {
228 usage4(_("Warning thresholds must be a positive integer"));
252 } 229 }
253 else { 230 } else {
254 usage4 (_("Warning thresholds must be a positive integer")); 231 usage4(_("Warning threshold must be a positive integer"));
255 }
256 }
257 else {
258 usage4 (_("Warning threshold must be a positive integer"));
259 } 232 }
260 break; 233 break;
261 case 'c': /* critical-variance */ 234 case 'c': /* critical-variance */
262 if (is_intnonneg (optarg)) { 235 if (is_intnonneg(optarg)) {
263 critical_diff = strtoul (optarg, NULL, 10); 236 critical_diff = strtoul(optarg, NULL, 10);
264 check_critical_diff = true; 237 check_critical_diff = true;
265 } 238 } else if (strspn(optarg, "0123456789:,") > 0) {
266 else if (strspn (optarg, "0123456789:,") > 0) { 239 if (sscanf(optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) == 2) {
267 if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
268 2) {
269 check_critical_diff = true; 240 check_critical_diff = true;
270 check_critical_time = true; 241 check_critical_time = true;
242 } else {
243 usage4(_("Critical thresholds must be a positive integer"));
271 } 244 }
272 else { 245 } else {
273 usage4 (_("Critical thresholds must be a positive integer")); 246 usage4(_("Critical threshold must be a positive integer"));
274 }
275 }
276 else {
277 usage4 (_("Critical threshold must be a positive integer"));
278 } 247 }
279 break; 248 break;
280 case 'W': /* warning-connect */ 249 case 'W': /* warning-connect */
281 if (!is_intnonneg (optarg)) 250 if (!is_intnonneg(optarg))
282 usage4 (_("Warning threshold must be a positive integer")); 251 usage4(_("Warning threshold must be a positive integer"));
283 else 252 else
284 warning_time = atoi (optarg); 253 warning_time = atoi(optarg);
285 check_warning_time = true; 254 check_warning_time = true;
286 break; 255 break;
287 case 'C': /* critical-connect */ 256 case 'C': /* critical-connect */
288 if (!is_intnonneg (optarg)) 257 if (!is_intnonneg(optarg))
289 usage4 (_("Critical threshold must be a positive integer")); 258 usage4(_("Critical threshold must be a positive integer"));
290 else 259 else
291 critical_time = atoi (optarg); 260 critical_time = atoi(optarg);
292 check_critical_time = true; 261 check_critical_time = true;
293 break; 262 break;
294 case 'p': /* port */ 263 case 'p': /* port */
295 if (!is_intnonneg (optarg)) 264 if (!is_intnonneg(optarg))
296 usage4 (_("Port must be a positive integer")); 265 usage4(_("Port must be a positive integer"));
297 else 266 else
298 server_port = atoi (optarg); 267 server_port = atoi(optarg);
299 break; 268 break;
300 case 't': /* timeout */ 269 case 't': /* timeout */
301 if (!is_intnonneg (optarg)) 270 if (!is_intnonneg(optarg))
302 usage2 (_("Timeout interval must be a positive integer"), optarg); 271 usage2(_("Timeout interval must be a positive integer"), optarg);
303 else 272 else
304 socket_timeout = atoi (optarg); 273 socket_timeout = atoi(optarg);
305 break; 274 break;
306 case 'u': /* udp */ 275 case 'u': /* udp */
307 use_udp = true; 276 use_udp = true;
308 } 277 }
309 } 278 }
@@ -311,64 +280,55 @@ process_arguments (int argc, char **argv)
311 c = optind; 280 c = optind;
312 if (server_address == NULL) { 281 if (server_address == NULL) {
313 if (argc > c) { 282 if (argc > c) {
314 if (!is_host (argv[c])) 283 if (!is_host(argv[c]))
315 usage2 (_("Invalid hostname/address"), optarg); 284 usage2(_("Invalid hostname/address"), optarg);
316 server_address = argv[c]; 285 server_address = argv[c];
317 } 286 } else {
318 else { 287 usage4(_("Hostname was not supplied"));
319 usage4 (_("Hostname was not supplied"));
320 } 288 }
321 } 289 }
322 290
323 return OK; 291 return OK;
324} 292}
325 293
326 294void print_help(void) {
327
328void
329print_help (void)
330{
331 char *myport; 295 char *myport;
332 xasprintf (&myport, "%d", TIME_PORT); 296 xasprintf(&myport, "%d", TIME_PORT);
333 297
334 print_revision (progname, NP_VERSION); 298 print_revision(progname, NP_VERSION);
335 299
336 printf ("Copyright (c) 1999 Ethan Galstad\n"); 300 printf("Copyright (c) 1999 Ethan Galstad\n");
337 printf (COPYRIGHT, copyright, email); 301 printf(COPYRIGHT, copyright, email);
338 302
339 printf ("%s\n", _("This plugin will check the time on the specified host.")); 303 printf("%s\n", _("This plugin will check the time on the specified host."));
340 304
341 printf ("\n\n"); 305 printf("\n\n");
342 306
343 print_usage (); 307 print_usage();
344 308
345 printf (UT_HELP_VRSN); 309 printf(UT_HELP_VRSN);
346 printf (UT_EXTRA_OPTS); 310 printf(UT_EXTRA_OPTS);
347 311
348 printf (UT_HOST_PORT, 'p', myport); 312 printf(UT_HOST_PORT, 'p', myport);
349 313
350 printf (" %s\n", "-u, --udp"); 314 printf(" %s\n", "-u, --udp");
351 printf (" %s\n", _("Use UDP to connect, not TCP")); 315 printf(" %s\n", _("Use UDP to connect, not TCP"));
352 printf (" %s\n", "-w, --warning-variance=INTEGER"); 316 printf(" %s\n", "-w, --warning-variance=INTEGER");
353 printf (" %s\n", _("Time difference (sec.) necessary to result in a warning status")); 317 printf(" %s\n", _("Time difference (sec.) necessary to result in a warning status"));
354 printf (" %s\n", "-c, --critical-variance=INTEGER"); 318 printf(" %s\n", "-c, --critical-variance=INTEGER");
355 printf (" %s\n", _("Time difference (sec.) necessary to result in a critical status")); 319 printf(" %s\n", _("Time difference (sec.) necessary to result in a critical status"));
356 printf (" %s\n", "-W, --warning-connect=INTEGER"); 320 printf(" %s\n", "-W, --warning-connect=INTEGER");
357 printf (" %s\n", _("Response time (sec.) necessary to result in warning status")); 321 printf(" %s\n", _("Response time (sec.) necessary to result in warning status"));
358 printf (" %s\n", "-C, --critical-connect=INTEGER"); 322 printf(" %s\n", "-C, --critical-connect=INTEGER");
359 printf (" %s\n", _("Response time (sec.) necessary to result in critical status")); 323 printf(" %s\n", _("Response time (sec.) necessary to result in critical status"));
360 324
361 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 325 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
362 326
363 printf (UT_SUPPORT); 327 printf(UT_SUPPORT);
364} 328}
365 329
366 330void print_usage(void) {
367 331 printf("%s\n", _("Usage:"));
368void 332 printf("%s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n", progname);
369print_usage (void) 333 printf(" [-W connect_time] [-C connect_time] [-t timeout]\n");
370{
371 printf ("%s\n", _("Usage:"));
372 printf ("%s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n",progname);
373 printf (" [-W connect_time] [-C connect_time] [-t timeout]\n");
374} 334}