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.c124
1 files changed, 50 insertions, 74 deletions
diff --git a/plugins/check_time.c b/plugins/check_time.c
index 86c414e..c381780 100644
--- a/plugins/check_time.c
+++ b/plugins/check_time.c
@@ -40,6 +40,11 @@
40#include "utils.h" 40#include "utils.h"
41 41
42#define PROGNAME "check_time" 42#define PROGNAME "check_time"
43#define REVISION "$Revision$"
44#define COPYRIGHT "1999-2002"
45#define AUTHOR "Ethan Galstad"
46#define EMAIL "nagios@nagios.org"
47#define SUMMARY "Check time on the specified host.\n"
43 48
44#define TIME_PORT 37 49#define TIME_PORT 37
45#define UNIX_EPOCH 2208988800UL 50#define UNIX_EPOCH 2208988800UL
@@ -59,7 +64,6 @@ char *server_address = NULL;
59 64
60 65
61int process_arguments (int, char **); 66int process_arguments (int, char **);
62int call_getopt (int, char **);
63void print_usage (void); 67void print_usage (void);
64void print_help (void); 68void print_help (void);
65 69
@@ -93,7 +97,7 @@ main (int argc, char **argv)
93 server_address, server_port); 97 server_address, server_port);
94 } 98 }
95 99
96 /* watch for the FTP connection string */ 100 /* watch for the connection string */
97 result = recv (sd, &raw_server_time, sizeof (raw_server_time), 0); 101 result = recv (sd, &raw_server_time, sizeof (raw_server_time), 0);
98 102
99 /* close the connection */ 103 /* close the connection */
@@ -154,52 +158,6 @@ process_arguments (int argc, char **argv)
154{ 158{
155 int c; 159 int c;
156 160
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
198int
199call_getopt (int argc, char **argv)
200{
201 int c, i = 0;
202
203#ifdef HAVE_GETOPT_H 161#ifdef HAVE_GETOPT_H
204 int option_index = 0; 162 int option_index = 0;
205 static struct option long_options[] = { 163 static struct option long_options[] = {
@@ -216,41 +174,42 @@ call_getopt (int argc, char **argv)
216 }; 174 };
217#endif 175#endif
218 176
177 if (argc < 2)
178 usage ("\n");
179
180 for (c = 1; c < argc; c++) {
181 if (strcmp ("-to", argv[c]) == 0)
182 strcpy (argv[c], "-t");
183 else if (strcmp ("-wd", argv[c]) == 0)
184 strcpy (argv[c], "-w");
185 else if (strcmp ("-cd", argv[c]) == 0)
186 strcpy (argv[c], "-c");
187 else if (strcmp ("-wt", argv[c]) == 0)
188 strcpy (argv[c], "-W");
189 else if (strcmp ("-ct", argv[c]) == 0)
190 strcpy (argv[c], "-C");
191 }
192
219 while (1) { 193 while (1) {
220#ifdef HAVE_GETOPT_H 194#ifdef HAVE_GETOPT_H
221 c = 195 c =
222 getopt_long (argc, argv, "+hVH:w:c:W:C:p:t:", long_options, 196 getopt_long (argc, argv, "hVH:w:c:W:C:p:t:", long_options,
223 &option_index); 197 &option_index);
224#else 198#else
225 c = getopt (argc, argv, "+hVH:w:c:W:C:p:t:"); 199 c = getopt (argc, argv, "hVH:w:c:W:C:p:t:");
226#endif 200#endif
227 201
228 i++; 202 if (c == -1 || c == EOF)
229
230 if (c == -1 || c == EOF || c == 1)
231 break; 203 break;
232 204
233 switch (c) { 205 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 */ 206 case '?': /* print short usage statement if args not parsable */
246 printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg); 207 usage3 ("Unknown argument", optopt);
247 print_usage ();
248 exit (STATE_UNKNOWN);
249 case 'h': /* help */ 208 case 'h': /* help */
250 print_help (); 209 print_help ();
251 exit (STATE_OK); 210 exit (STATE_OK);
252 case 'V': /* version */ 211 case 'V': /* version */
253 print_revision (my_basename (argv[0]), "$Revision$"); 212 print_revision (PROGNAME, REVISION);
254 exit (STATE_OK); 213 exit (STATE_OK);
255 case 'H': /* hostname */ 214 case 'H': /* hostname */
256 if (is_host (optarg) == FALSE) 215 if (is_host (optarg) == FALSE)
@@ -318,7 +277,20 @@ call_getopt (int argc, char **argv)
318 break; 277 break;
319 } 278 }
320 } 279 }
321 return i; 280
281 c = optind;
282 if (server_address == NULL) {
283 if (argc > c) {
284 if (is_host (argv[c]) == FALSE)
285 usage ("Invalid host name/address\n");
286 server_address = argv[c];
287 }
288 else {
289 usage ("Host name was not supplied\n");
290 }
291 }
292
293 return OK;
322} 294}
323 295
324 296
@@ -329,8 +301,12 @@ void
329print_usage (void) 301print_usage (void)
330{ 302{
331 printf 303 printf
332 ("Usage: check_time -H <host_address> [-p port] [-w variance] [-c variance]\n" 304 ("Usage:\n"
333 " [-W connect_time] [-C connect_time] [-t timeout]\n"); 305 " %s -H <host_address> [-p port] [-w variance] [-c variance]\n"
306 " [-W connect_time] [-C connect_time] [-t timeout]\n"
307 " %s (-h | --help) for detailed help\n"
308 " %s (-V | --version) for version information\n",
309 PROGNAME, PROGNAME, PROGNAME);
334} 310}
335 311
336 312
@@ -340,10 +316,10 @@ print_usage (void)
340void 316void
341print_help (void) 317print_help (void)
342{ 318{
343 print_revision (PROGNAME, "$Revision$"); 319 print_revision (PROGNAME, REVISION);
344 printf 320 printf
345 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n" 321 ("Copyright (c) %s %s <%s>\n\n%s\n",
346 "This plugin connects to a time port on the specified host.\n\n"); 322 COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
347 print_usage (); 323 print_usage ();
348 printf 324 printf
349 ("Options:\n" 325 ("Options:\n"