summaryrefslogtreecommitdiffstats
path: root/plugins/check_snmp.c
diff options
context:
space:
mode:
authorKarl DeBisschop <kdebisschop@users.sourceforge.net>2002-10-20 20:22:20 +0000
committerKarl DeBisschop <kdebisschop@users.sourceforge.net>2002-10-20 20:22:20 +0000
commitf275f7ff070f249144ccdcd92a8e81b8e44b2d19 (patch)
tree0f4593da916691ae3c1b2c95419253e921fd7c3a /plugins/check_snmp.c
parenta396d2177806ac4ee9f524a139b19ad29c76ca91 (diff)
downloadmonitoring-plugins-f275f7ff070f249144ccdcd92a8e81b8e44b2d19.tar.gz
fixed scanning for multiple OIDs, which was not working
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@144 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_snmp.c')
-rw-r--r--plugins/check_snmp.c151
1 files changed, 62 insertions, 89 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index 220be234..35d21479 100644
--- a/plugins/check_snmp.c
+++ b/plugins/check_snmp.c
@@ -94,7 +94,7 @@ int errcode, excode;
94 94
95char *server_address = NULL; 95char *server_address = NULL;
96char *community = NULL; 96char *community = NULL;
97char oid[MAX_INPUT_BUFFER] = ""; 97char *oid = NULL;
98char *label = NULL; 98char *label = NULL;
99char *units = NULL; 99char *units = NULL;
100char *port = NULL; 100char *port = NULL;
@@ -105,6 +105,7 @@ int nlabels = 0;
105int labels_size = 8; 105int labels_size = 8;
106int nunits = 0; 106int nunits = 0;
107int unitv_size = 8; 107int unitv_size = 8;
108int verbose = FALSE;
108unsigned long lower_warn_lim[MAX_OIDS]; 109unsigned long lower_warn_lim[MAX_OIDS];
109unsigned long upper_warn_lim[MAX_OIDS]; 110unsigned long upper_warn_lim[MAX_OIDS];
110unsigned long lower_crit_lim[MAX_OIDS]; 111unsigned long lower_crit_lim[MAX_OIDS];
@@ -144,10 +145,10 @@ main (int argc, char **argv)
144 usage ("Incorrect arguments supplied\n"); 145 usage ("Incorrect arguments supplied\n");
145 146
146 /* create the command line to execute */ 147 /* create the command line to execute */
147 asprintf 148 asprintf (&command_line, "%s -p %s -m ALL -v 1 %s -c %s %s",
148 (&command_line, 149 PATH_TO_SNMPGET, port, server_address, community, oid);
149 "%s -p %s -m ALL -v 1 %s -c %s %s", 150 if (verbose)
150 PATH_TO_SNMPGET, port, server_address, community, oid); 151 printf ("%s\n", command_line);
151 152
152 /* run the command */ 153 /* run the command */
153 child_process = spopen (command_line); 154 child_process = spopen (command_line);
@@ -161,8 +162,12 @@ main (int argc, char **argv)
161 printf ("Could not open stderr for %s\n", command_line); 162 printf ("Could not open stderr for %s\n", command_line);
162 } 163 }
163 164
165 asprintf (&output, "");
164 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) 166 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
165 output = strscat (output, input_buffer); 167 asprintf (&output, "%s%s", output, input_buffer);
168
169 if (verbose)
170 printf ("%s\n", output);
166 171
167 ptr = output; 172 ptr = output;
168 173
@@ -264,15 +269,12 @@ main (int argc, char **argv)
264 result = max_state (result, iresult); 269 result = max_state (result, iresult);
265 270
266 if (nlabels > 1 && i < nlabels && labels[i] != NULL) 271 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
267 asprintf 272 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
268 (&outbuff, 273 (i == 0) ? " " : output_delim,
269 "%s%s%s %s%s%s", 274 labels[i], mark (iresult), show, mark (iresult));
270 outbuff,
271 (i == 0) ? " " : output_delim,
272 labels[i], mark (iresult), show, mark (iresult));
273 else 275 else
274 asprintf (&outbuff, "%s%s%s%s%s", outbuff, 276 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
275 (i == 0) ? " " : output_delim, mark (iresult), show, mark (iresult)); 277 mark (iresult), show, mark (iresult));
276 278
277 if (nunits > 0 && i < nunits) 279 if (nunits > 0 && i < nunits)
278 asprintf (&outbuff, "%s %s", outbuff, unitv[i]); 280 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
@@ -310,52 +312,6 @@ main (int argc, char **argv)
310int 312int
311process_arguments (int argc, char **argv) 313process_arguments (int argc, char **argv)
312{ 314{
313 int c;
314
315 if (argc < 2)
316 return ERROR;
317
318 for (c = 1; c < argc; c++) {
319 if (strcmp ("-to", argv[c]) == 0)
320 strcpy (argv[c], "-t");
321 if (strcmp ("-wv", argv[c]) == 0)
322 strcpy (argv[c], "-w");
323 if (strcmp ("-cv", argv[c]) == 0)
324 strcpy (argv[c], "-c");
325 }
326
327 c = 0;
328 while (c += (call_getopt (argc - c, &argv[c]))) {
329 if (argc <= c)
330 break;
331 if (server_address == NULL)
332 server_address = strscpy (NULL, argv[c]);
333 }
334
335 if (community == NULL)
336 community = strscpy (NULL, "public");
337
338 if (delimiter == NULL)
339 delimiter = strscpy (NULL, DEFAULT_DELIMITER);
340
341 if (output_delim == NULL)
342 output_delim = strscpy (NULL, DEFAULT_OUTPUT_DELIMITER);
343
344 if (label == NULL)
345 label = strscpy (NULL, "SNMP");
346
347 if (units == NULL)
348 units = strscpy (NULL, "");
349
350 if (port == NULL)
351 port = strscpy(NULL,"161");
352
353 return c;
354}
355
356int
357call_getopt (int argc, char **argv)
358{
359 char *ptr; 315 char *ptr;
360 int c, i = 1; 316 int c, i = 1;
361 int j = 0, jj = 0; 317 int j = 0, jj = 0;
@@ -363,12 +319,7 @@ call_getopt (int argc, char **argv)
363#ifdef HAVE_GETOPT_H 319#ifdef HAVE_GETOPT_H
364 int option_index = 0; 320 int option_index = 0;
365 static struct option long_options[] = { 321 static struct option long_options[] = {
366 {"help", no_argument, 0, 'h'}, 322 STD_LONG_OPTS,
367 {"version", no_argument, 0, 'V'},
368 {"timeout", required_argument, 0, 't'},
369 {"critical", required_argument, 0, 'c'},
370 {"warning", required_argument, 0, 'w'},
371 {"hostname", required_argument, 0, 'H'},
372 {"community", required_argument, 0, 'C'}, 323 {"community", required_argument, 0, 'C'},
373 {"oid", required_argument, 0, 'o'}, 324 {"oid", required_argument, 0, 'o'},
374 {"object", required_argument, 0, 'o'}, 325 {"object", required_argument, 0, 'o'},
@@ -385,6 +336,22 @@ call_getopt (int argc, char **argv)
385 }; 336 };
386#endif 337#endif
387 338
339 if (argc < 2)
340 return ERROR;
341
342 /* reverse compatibility for very old non-POSIX usage forms */
343 for (c = 1; c < argc; c++) {
344 if (strcmp ("-to", argv[c]) == 0)
345 strcpy (argv[c], "-t");
346 if (strcmp ("-wv", argv[c]) == 0)
347 strcpy (argv[c], "-w");
348 if (strcmp ("-cv", argv[c]) == 0)
349 strcpy (argv[c], "-c");
350 }
351
352 /* initialize some args */
353 asprintf (&oid, "");
354
388 while (1) { 355 while (1) {
389#ifdef HAVE_GETOPT_H 356#ifdef HAVE_GETOPT_H
390 c = 357 c =
@@ -397,25 +364,6 @@ call_getopt (int argc, char **argv)
397 if (c == -1 || c == EOF) 364 if (c == -1 || c == EOF)
398 break; 365 break;
399 366
400 i++;
401 switch (c) {
402 case 't':
403 case 'c':
404 case 'w':
405 case 'H':
406 case 'C':
407 case 'o':
408 case 'd':
409 case 'D':
410 case 's':
411 case 'R':
412 case 'r':
413 case 'l':
414 case 'u':
415 case 'p':
416 i++;
417 }
418
419 switch (c) { 367 switch (c) {
420 case '?': /* help */ 368 case '?': /* help */
421 printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg); 369 printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
@@ -427,6 +375,9 @@ call_getopt (int argc, char **argv)
427 case 'V': /* version */ 375 case 'V': /* version */
428 print_revision (my_basename (argv[0]), "$Revision$"); 376 print_revision (my_basename (argv[0]), "$Revision$");
429 exit (STATE_OK); 377 exit (STATE_OK);
378 case 'v': /* verbose */
379 verbose = TRUE;
380 break;
430 case 't': /* timeout period */ 381 case 't': /* timeout period */
431 if (!is_integer (optarg)) { 382 if (!is_integer (optarg)) {
432 printf ("%s: Timeout Interval must be an integer!\n\n", 383 printf ("%s: Timeout Interval must be an integer!\n\n",
@@ -472,11 +423,10 @@ call_getopt (int argc, char **argv)
472 break; 423 break;
473 case 'o': /* object identifier */ 424 case 'o': /* object identifier */
474 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++) 425 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
475 ptr[0] = ' '; 426 ptr[0] = ' '; /* relpace comma with space */
476 strncpy (oid, optarg, sizeof (oid) - 1);
477 oid[sizeof (oid) - 1] = 0;
478 for (ptr = optarg, j = 1; (ptr = index (ptr, ' ')); ptr++) 427 for (ptr = optarg, j = 1; (ptr = index (ptr, ' ')); ptr++)
479 j++; 428 j++; /* count OIDs */
429 asprintf (&oid, "%s %s", oid, optarg);
480 break; 430 break;
481 case 'd': /* delimiter */ 431 case 'd': /* delimiter */
482 delimiter = strscpy (delimiter, optarg); 432 delimiter = strscpy (delimiter, optarg);
@@ -578,6 +528,29 @@ call_getopt (int argc, char **argv)
578 528
579 } 529 }
580 } 530 }
531
532 c = optind;
533 if (server_address == NULL)
534 server_address = strscpy (NULL, argv[c++]);
535
536 if (community == NULL)
537 community = strscpy (NULL, "public");
538
539 if (delimiter == NULL)
540 delimiter = strscpy (NULL, DEFAULT_DELIMITER);
541
542 if (output_delim == NULL)
543 output_delim = strscpy (NULL, DEFAULT_OUTPUT_DELIMITER);
544
545 if (label == NULL)
546 label = strscpy (NULL, "SNMP");
547
548 if (units == NULL)
549 units = strscpy (NULL, "");
550
551 if (port == NULL)
552 port = strscpy(NULL,"161");
553
581 return i; 554 return i;
582} 555}
583 556