summaryrefslogtreecommitdiffstats
path: root/plugins/check_snmp.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_snmp.c')
-rw-r--r--plugins/check_snmp.c185
1 files changed, 85 insertions, 100 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index fc19120..cb13bc9 100644
--- a/plugins/check_snmp.c
+++ b/plugins/check_snmp.c
@@ -175,7 +175,7 @@ int errcode, excode;
175#endif 175#endif
176 176
177char *server_address = NULL; 177char *server_address = NULL;
178char *community = NULL; 178char *community = DEFAULT_COMMUNITY;
179char *authpriv = NULL; 179char *authpriv = NULL;
180char *proto = NULL; 180char *proto = NULL;
181char *seclevel = NULL; 181char *seclevel = NULL;
@@ -184,8 +184,8 @@ char *authproto = NULL;
184char *authpasswd = NULL; 184char *authpasswd = NULL;
185char *privpasswd = NULL; 185char *privpasswd = NULL;
186char *oid = ""; 186char *oid = "";
187char *label = NULL; 187char *label = "SNMP";
188char *units = NULL; 188char *units = "";
189char *port = DEFAULT_PORT; 189char *port = DEFAULT_PORT;
190char string_value[MAX_INPUT_BUFFER] = ""; 190char string_value[MAX_INPUT_BUFFER] = "";
191char **labels = NULL; 191char **labels = NULL;
@@ -203,9 +203,9 @@ unsigned long response_value[MAX_OIDS];
203int check_warning_value = FALSE; 203int check_warning_value = FALSE;
204int check_critical_value = FALSE; 204int check_critical_value = FALSE;
205int eval_method[MAX_OIDS]; 205int eval_method[MAX_OIDS];
206char *delimiter = NULL; 206char *delimiter = DEFAULT_DELIMITER;
207char *output_delim = NULL; 207char *output_delim = DEFAULT_OUTPUT_DELIMITER;
208char *miblist = NULL; 208char *miblist = DEFAULT_MIBLIST;
209 209
210 210
211int 211int
@@ -219,7 +219,7 @@ main (int argc, char **argv)
219 char *command_line = NULL; 219 char *command_line = NULL;
220 char *response = NULL; 220 char *response = NULL;
221 char *outbuff = ""; 221 char *outbuff = "";
222 char *output = NULL; 222 char *output = "";
223 char *ptr = NULL; 223 char *ptr = NULL;
224 char *p2 = NULL; 224 char *p2 = NULL;
225 char *show = NULL; 225 char *show = NULL;
@@ -251,7 +251,6 @@ main (int argc, char **argv)
251 printf ("Could not open stderr for %s\n", command_line); 251 printf ("Could not open stderr for %s\n", command_line);
252 } 252 }
253 253
254 asprintf (&output, "");
255 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) 254 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
256 asprintf (&output, "%s%s", output, input_buffer); 255 asprintf (&output, "%s%s", output, input_buffer);
257 256
@@ -293,6 +292,7 @@ main (int argc, char **argv)
293 } 292 }
294 } 293 }
295 294
295 /* We strip out the datatype indicator for PHBs */
296 if (strstr (response, "Gauge: ")) 296 if (strstr (response, "Gauge: "))
297 show = strstr (response, "Gauge: ") + 7; 297 show = strstr (response, "Gauge: ") + 7;
298 else if (strstr (response, "Gauge32: ")) 298 else if (strstr (response, "Gauge32: "))
@@ -305,12 +305,7 @@ main (int argc, char **argv)
305 305
306 iresult = STATE_DEPENDENT; 306 iresult = STATE_DEPENDENT;
307 307
308 if (eval_method[i] & CRIT_PRESENT) { 308 /* Process this block for integer comparisons */
309 iresult = STATE_CRITICAL;
310 } else if (eval_method[i] & WARN_PRESENT) {
311 iresult = STATE_WARNING;
312 }
313
314 if (eval_method[i] & CRIT_GT || 309 if (eval_method[i] & CRIT_GT ||
315 eval_method[i] & CRIT_LT || 310 eval_method[i] & CRIT_LT ||
316 eval_method[i] & CRIT_GE || 311 eval_method[i] & CRIT_GE ||
@@ -327,9 +322,9 @@ main (int argc, char **argv)
327 response_value[i] = strtoul (p2, NULL, 10); 322 response_value[i] = strtoul (p2, NULL, 10);
328 iresult = check_num (i); 323 iresult = check_num (i);
329 asprintf (&show, "%lu", response_value[i]); 324 asprintf (&show, "%lu", response_value[i]);
330 /*asprintf (&show, "%s", response); */
331 } 325 }
332 326
327 /* Process this block for string matching */
333 else if (eval_method[i] & CRIT_STRING) { 328 else if (eval_method[i] & CRIT_STRING) {
334 if (strcmp (response, string_value)) 329 if (strcmp (response, string_value))
335 iresult = STATE_CRITICAL; 330 iresult = STATE_CRITICAL;
@@ -337,6 +332,7 @@ main (int argc, char **argv)
337 iresult = STATE_OK; 332 iresult = STATE_OK;
338 } 333 }
339 334
335 /* Process this block for regex matching */
340 else if (eval_method[i] & CRIT_REGEX) { 336 else if (eval_method[i] & CRIT_REGEX) {
341#ifdef HAVE_REGEX_H 337#ifdef HAVE_REGEX_H
342 excode = regexec (&preg, response, 10, pmatch, eflags); 338 excode = regexec (&preg, response, 10, pmatch, eflags);
@@ -352,21 +348,25 @@ main (int argc, char **argv)
352 iresult = STATE_CRITICAL; 348 iresult = STATE_CRITICAL;
353 } 349 }
354#else 350#else
355 printf ("SNMP UNKNOWN: call for regex which was not a compiled option"); 351 printf ("%s UNKNOWN: call for regex which was not a compiled option", label);
356 exit (STATE_UNKNOWN); 352 exit (STATE_UNKNOWN);
357#endif 353#endif
358 } 354 }
355
356 /* Process this block for existence-nonexistence checks */
359 else { 357 else {
360 if (response && iresult == STATE_DEPENDENT) 358 if (eval_method[i] & CRIT_PRESENT)
361 iresult = STATE_OK;
362 else if (eval_method[i] & CRIT_PRESENT)
363 iresult = STATE_CRITICAL; 359 iresult = STATE_CRITICAL;
364 else if (eval_method[i] & WARN_PRESENT) 360 else if (eval_method[i] & WARN_PRESENT)
365 iresult = STATE_WARNING; 361 iresult = STATE_WARNING;
362 else if (response && iresult == STATE_DEPENDENT)
363 iresult = STATE_OK;
366 } 364 }
367 365
366 /* Result is the worst outcome of all the OIDs tested */
368 result = max_state (result, iresult); 367 result = max_state (result, iresult);
369 368
369 /* Prepend a label for this OID if there is one */
370 if (nlabels > 1 && i < nlabels && labels[i] != NULL) 370 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
371 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff, 371 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
372 (i == 0) ? " " : output_delim, 372 (i == 0) ? " " : output_delim,
@@ -375,7 +375,8 @@ main (int argc, char **argv)
375 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim, 375 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
376 mark (iresult), show, mark (iresult)); 376 mark (iresult), show, mark (iresult));
377 377
378 if (nunits > 0 && i < nunits) 378 /* Append a unit string for this OID if there is one */
379 if (nunits > 0 && i < nunits && unitv[i] != NULL)
379 asprintf (&outbuff, "%s %s", outbuff, unitv[i]); 380 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
380 381
381 i++; 382 i++;
@@ -413,7 +414,7 @@ process_arguments (int argc, char **argv)
413{ 414{
414 char *ptr; 415 char *ptr;
415 int c = 1; 416 int c = 1;
416 int j = 0, jj = 0; 417 int j = 0, jj = 0, ii = 0;
417 418
418#ifdef HAVE_GETOPT_H 419#ifdef HAVE_GETOPT_H
419 int option_index = 0; 420 int option_index = 0;
@@ -479,34 +480,52 @@ process_arguments (int argc, char **argv)
479 case 'v': /* verbose */ 480 case 'v': /* verbose */
480 verbose = TRUE; 481 verbose = TRUE;
481 break; 482 break;
483
484 /* Connection info */
485 case 'C': /* group or community */
486 community = strscpy (community, optarg);
487 break;
488 case 'H': /* Host or server */
489 server_address = strscpy (server_address, optarg);
490 break;
491 case 'p': /* TCP port number */
492 port = strscpy(port, optarg);
493 break;
494 case 'm': /* List of MIBS */
495 miblist = strscpy(miblist, optarg);
496 break;
497 case 'P': /* SNMP protocol version */
498 proto = strscpy(proto, optarg);
499 break;
500 case 'L': /* security level */
501 seclevel = strscpy(seclevel,optarg);
502 break;
503 case 'U': /* security username */
504 secname = strscpy(secname, optarg);
505 break;
506 case 'a': /* auth protocol */
507 asprintf (&authproto, optarg);
508 break;
509 case 'A': /* auth passwd */
510 authpasswd = strscpy(authpasswd, optarg);
511 break;
512 case 'X': /* priv passwd */
513 privpasswd = strscpy(privpasswd, optarg);
514 break;
482 case 't': /* timeout period */ 515 case 't': /* timeout period */
483 if (!is_integer (optarg)) 516 if (!is_integer (optarg))
484 usage2 ("Timeout Interval must be an integer", optarg); 517 usage2 ("Timeout Interval must be an integer", optarg);
485 timeout_interval = atoi (optarg); 518 timeout_interval = atoi (optarg);
486 break; 519 break;
487 case 'e': /* PRELIMINARY - may change */ 520
488 eval_method[j] |= WARN_PRESENT; 521 /* Test parameters */
489 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
490 ptr[0] = ' '; /* relpace comma with space */
491 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
492 eval_method[++j] |= WARN_PRESENT;
493 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
494 break;
495 case 'E': /* PRELIMINARY - may change */
496 eval_method[j] |= WARN_PRESENT;
497 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
498 ptr[0] = ' '; /* relpace comma with space */
499 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
500 eval_method[++j] |= CRIT_PRESENT;
501 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
502 break;
503 case 'c': /* critical time threshold */ 522 case 'c': /* critical time threshold */
504 if (strspn (optarg, "0123456789:,") < strlen (optarg)) { 523 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
505 printf ("Invalid critical threshold: %s\n", optarg); 524 printf ("Invalid critical threshold: %s\n", optarg);
506 print_usage (); 525 print_usage ();
507 exit (STATE_UNKNOWN); 526 exit (STATE_UNKNOWN);
508 } 527 }
509 for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) { 528 for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
510 if (lu_getll (&lower_crit_lim[jj], ptr) == 1) 529 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
511 eval_method[jj] |= CRIT_LT; 530 eval_method[jj] |= CRIT_LT;
512 if (lu_getul (&upper_crit_lim[jj], ptr) == 1) 531 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
@@ -520,37 +539,36 @@ process_arguments (int argc, char **argv)
520 print_usage (); 539 print_usage ();
521 exit (STATE_UNKNOWN); 540 exit (STATE_UNKNOWN);
522 } 541 }
523 for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) { 542 for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
524 if (lu_getll (&lower_warn_lim[jj], ptr) == 1) 543 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
525 eval_method[jj] |= WARN_LT; 544 eval_method[ii] |= WARN_LT;
526 if (lu_getul (&upper_warn_lim[jj], ptr) == 1) 545 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
527 eval_method[jj] |= WARN_GT; 546 eval_method[ii] |= WARN_GT;
528 (ptr = index (ptr, ',')) ? ptr++ : ptr; 547 (ptr = index (ptr, ',')) ? ptr++ : ptr;
529 } 548 }
530 break; 549 break;
531 case 'H': /* Host or server */
532 server_address = strscpy (server_address, optarg);
533 break;
534 case 'C': /* group or community */
535 community = strscpy (community, optarg);
536 break;
537 case 'o': /* object identifier */ 550 case 'o': /* object identifier */
551 case 'e': /* PRELIMINARY - may change */
552 case 'E': /* PRELIMINARY - may change */
538 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++) 553 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
539 ptr[0] = ' '; /* relpace comma with space */ 554 ptr[0] = ' '; /* relpace comma with space */
540 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++) 555 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
541 j++; /* count OIDs */ 556 j++; /* count OIDs */
542 asprintf (&oid, "%s %s", (oid?oid:""), optarg); 557 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
543 break; 558 if (c == 'E' || c == 'e') {
544 case 'd': /* delimiter */ 559 jj++;
545 delimiter = strscpy (delimiter, optarg); 560 ii++;
546 break; 561 }
547 case 'D': /* output-delimiter */ 562 if (c == 'E')
548 output_delim = strscpy (output_delim, optarg); 563 eval_method[j+1] |= WARN_PRESENT;
564 else if (c == 'e')
565 eval_method[j+1] |= CRIT_PRESENT;
549 break; 566 break;
550 case 's': /* string or substring */ 567 case 's': /* string or substring */
551 strncpy (string_value, optarg, sizeof (string_value) - 1); 568 strncpy (string_value, optarg, sizeof (string_value) - 1);
552 string_value[sizeof (string_value) - 1] = 0; 569 string_value[sizeof (string_value) - 1] = 0;
553 eval_method[jj] = CRIT_STRING; 570 eval_method[jj++] = CRIT_STRING;
571 ii++;
554 break; 572 break;
555 case 'R': /* regex */ 573 case 'R': /* regex */
556#ifdef HAVE_REGEX_H 574#ifdef HAVE_REGEX_H
@@ -567,12 +585,21 @@ process_arguments (int argc, char **argv)
567 printf ("Could Not Compile Regular Expression"); 585 printf ("Could Not Compile Regular Expression");
568 return ERROR; 586 return ERROR;
569 } 587 }
570 eval_method[jj] = CRIT_REGEX; 588 eval_method[jj++] = CRIT_REGEX;
589 ii++;
571#else 590#else
572 printf ("SNMP UNKNOWN: call for regex which was not a compiled option"); 591 printf ("%s UNKNOWN: call for regex which was not a compiled option", label);
573 exit (STATE_UNKNOWN); 592 exit (STATE_UNKNOWN);
574#endif 593#endif
575 break; 594 break;
595
596 /* Format */
597 case 'd': /* delimiter */
598 delimiter = strscpy (delimiter, optarg);
599 break;
600 case 'D': /* output-delimiter */
601 output_delim = strscpy (output_delim, optarg);
602 break;
576 case 'l': /* label */ 603 case 'l': /* label */
577 label = optarg; 604 label = optarg;
578 nlabels++; 605 nlabels++;
@@ -635,30 +662,6 @@ process_arguments (int argc, char **argv)
635 unitv[nunits - 1] = ptr; 662 unitv[nunits - 1] = ptr;
636 } 663 }
637 break; 664 break;
638 case 'p': /* TCP port number */
639 port = strscpy(port, optarg);
640 break;
641 case 'm': /* List of MIBS */
642 miblist = strscpy(miblist, optarg);
643 break;
644 case 'P': /* SNMP protocol version */
645 proto = strscpy(proto, optarg);
646 break;
647 case 'L': /* security level */
648 seclevel = strscpy(seclevel,optarg);
649 break;
650 case 'U': /* security username */
651 secname = strscpy(secname, optarg);
652 break;
653 case 'a': /* auth protocol */
654 asprintf (&authproto, optarg);
655 break;
656 case 'A': /* auth passwd */
657 authpasswd = strscpy(authpasswd, optarg);
658 break;
659 case 'X': /* priv passwd */
660 privpasswd = strscpy(privpasswd, optarg);
661 break;
662 665
663 } 666 }
664 } 667 }
@@ -693,24 +696,6 @@ int
693validate_arguments () 696validate_arguments ()
694{ 697{
695 698
696 if (community == NULL)
697 asprintf (&community, DEFAULT_COMMUNITY);
698
699 if (delimiter == NULL)
700 asprintf (&delimiter, DEFAULT_DELIMITER);
701
702 if (output_delim == NULL)
703 asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
704
705 if (miblist == NULL)
706 asprintf (&miblist, DEFAULT_MIBLIST);
707
708 if (label == NULL)
709 asprintf (&label, "SNMP");
710
711 if (units == NULL)
712 asprintf (&units, "");
713
714 /* Need better checks to verify seclevel and authproto choices */ 699 /* Need better checks to verify seclevel and authproto choices */
715 700
716 if (seclevel == NULL) 701 if (seclevel == NULL)