diff options
Diffstat (limited to 'plugins/check_snmp.c')
| -rw-r--r-- | plugins/check_snmp.c | 60 |
1 files changed, 40 insertions, 20 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c index 4cd38051..2d9861bc 100644 --- a/plugins/check_snmp.c +++ b/plugins/check_snmp.c | |||
| @@ -141,6 +141,7 @@ int calculate_rate = 0; | |||
| 141 | int rate_multiplier = 1; | 141 | int rate_multiplier = 1; |
| 142 | state_data *previous_state; | 142 | state_data *previous_state; |
| 143 | double previous_value[MAX_OIDS]; | 143 | double previous_value[MAX_OIDS]; |
| 144 | int perf_labels = 1; | ||
| 144 | 145 | ||
| 145 | 146 | ||
| 146 | int | 147 | int |
| @@ -169,6 +170,7 @@ main (int argc, char **argv) | |||
| 169 | char *state_string=NULL; | 170 | char *state_string=NULL; |
| 170 | size_t response_length, current_length, string_length; | 171 | size_t response_length, current_length, string_length; |
| 171 | char *temp_string=NULL; | 172 | char *temp_string=NULL; |
| 173 | char *quote_string=NULL; | ||
| 172 | time_t current_time; | 174 | time_t current_time; |
| 173 | double temp_double; | 175 | double temp_double; |
| 174 | time_t duration; | 176 | time_t duration; |
| @@ -253,9 +255,9 @@ main (int argc, char **argv) | |||
| 253 | command_line = calloc (9 + numauthpriv + 1 + numoids + 1, sizeof (char *)); | 255 | command_line = calloc (9 + numauthpriv + 1 + numoids + 1, sizeof (char *)); |
| 254 | command_line[0] = snmpcmd; | 256 | command_line[0] = snmpcmd; |
| 255 | command_line[1] = strdup ("-t"); | 257 | command_line[1] = strdup ("-t"); |
| 256 | asprintf (&command_line[2], "%d", timeout_interval); | 258 | xasprintf (&command_line[2], "%d", timeout_interval); |
| 257 | command_line[3] = strdup ("-r"); | 259 | command_line[3] = strdup ("-r"); |
| 258 | asprintf (&command_line[4], "%d", retries); | 260 | xasprintf (&command_line[4], "%d", retries); |
| 259 | command_line[5] = strdup ("-m"); | 261 | command_line[5] = strdup ("-m"); |
| 260 | command_line[6] = strdup (miblist); | 262 | command_line[6] = strdup (miblist); |
| 261 | command_line[7] = "-v"; | 263 | command_line[7] = "-v"; |
| @@ -265,16 +267,16 @@ main (int argc, char **argv) | |||
| 265 | command_line[9 + i] = authpriv[i]; | 267 | command_line[9 + i] = authpriv[i]; |
| 266 | } | 268 | } |
| 267 | 269 | ||
| 268 | asprintf (&command_line[9 + numauthpriv], "%s:%s", server_address, port); | 270 | xasprintf (&command_line[9 + numauthpriv], "%s:%s", server_address, port); |
| 269 | 271 | ||
| 270 | /* This is just for display purposes, so it can remain a string */ | 272 | /* This is just for display purposes, so it can remain a string */ |
| 271 | asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s", | 273 | xasprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s", |
| 272 | snmpcmd, timeout_interval, retries, strlen(miblist) ? miblist : "''", proto, "[authpriv]", | 274 | snmpcmd, timeout_interval, retries, strlen(miblist) ? miblist : "''", proto, "[authpriv]", |
| 273 | server_address, port); | 275 | server_address, port); |
| 274 | 276 | ||
| 275 | for (i = 0; i < numoids; i++) { | 277 | for (i = 0; i < numoids; i++) { |
| 276 | command_line[9 + numauthpriv + 1 + i] = oids[i]; | 278 | command_line[9 + numauthpriv + 1 + i] = oids[i]; |
| 277 | asprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); | 279 | xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); |
| 278 | } | 280 | } |
| 279 | 281 | ||
| 280 | command_line[9 + numauthpriv + 1 + numoids] = NULL; | 282 | command_line[9 + numauthpriv + 1 + numoids] = NULL; |
| @@ -369,14 +371,14 @@ main (int argc, char **argv) | |||
| 369 | if (dq_count) { /* unfinished line */ | 371 | if (dq_count) { /* unfinished line */ |
| 370 | /* copy show verbatim first */ | 372 | /* copy show verbatim first */ |
| 371 | if (!mult_resp) mult_resp = strdup(""); | 373 | if (!mult_resp) mult_resp = strdup(""); |
| 372 | asprintf (&mult_resp, "%s%s:\n%s\n", mult_resp, oids[i], show); | 374 | xasprintf (&mult_resp, "%s%s:\n%s\n", mult_resp, oids[i], show); |
| 373 | /* then strip out unmatched double-quote from single-line output */ | 375 | /* then strip out unmatched double-quote from single-line output */ |
| 374 | if (show[0] == '"') show++; | 376 | if (show[0] == '"') show++; |
| 375 | 377 | ||
| 376 | /* Keep reading until we match end of double-quoted string */ | 378 | /* Keep reading until we match end of double-quoted string */ |
| 377 | for (line++; line < chld_out.lines; line++) { | 379 | for (line++; line < chld_out.lines; line++) { |
| 378 | ptr = chld_out.line[line]; | 380 | ptr = chld_out.line[line]; |
| 379 | asprintf (&mult_resp, "%s%s\n", mult_resp, ptr); | 381 | xasprintf (&mult_resp, "%s%s\n", mult_resp, ptr); |
| 380 | 382 | ||
| 381 | COUNT_SEQ(ptr, bk_count, dq_count) | 383 | COUNT_SEQ(ptr, bk_count, dq_count) |
| 382 | while (dq_count && ptr[0] != '\n' && ptr[0] != '\0') { | 384 | while (dq_count && ptr[0] != '\n' && ptr[0] != '\0') { |
| @@ -422,11 +424,11 @@ main (int argc, char **argv) | |||
| 422 | /* Convert to per second, then use multiplier */ | 424 | /* Convert to per second, then use multiplier */ |
| 423 | temp_double = temp_double/duration*rate_multiplier; | 425 | temp_double = temp_double/duration*rate_multiplier; |
| 424 | iresult = get_status(temp_double, thlds[i]); | 426 | iresult = get_status(temp_double, thlds[i]); |
| 425 | asprintf (&show, conv, temp_double); | 427 | xasprintf (&show, conv, temp_double); |
| 426 | } | 428 | } |
| 427 | } else { | 429 | } else { |
| 428 | iresult = get_status(response_value[i], thlds[i]); | 430 | iresult = get_status(response_value[i], thlds[i]); |
| 429 | asprintf (&show, conv, response_value[i]); | 431 | xasprintf (&show, conv, response_value[i]); |
| 430 | } | 432 | } |
| 431 | } | 433 | } |
| 432 | 434 | ||
| @@ -470,26 +472,37 @@ main (int argc, char **argv) | |||
| 470 | 472 | ||
| 471 | /* Prepend a label for this OID if there is one */ | 473 | /* Prepend a label for this OID if there is one */ |
| 472 | if (nlabels >= (size_t)1 && (size_t)i < nlabels && labels[i] != NULL) | 474 | if (nlabels >= (size_t)1 && (size_t)i < nlabels && labels[i] != NULL) |
| 473 | asprintf (&outbuff, "%s%s%s %s%s%s", outbuff, | 475 | xasprintf (&outbuff, "%s%s%s %s%s%s", outbuff, |
| 474 | (i == 0) ? " " : output_delim, | 476 | (i == 0) ? " " : output_delim, |
| 475 | labels[i], mark (iresult), show, mark (iresult)); | 477 | labels[i], mark (iresult), show, mark (iresult)); |
| 476 | else | 478 | else |
| 477 | asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim, | 479 | xasprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim, |
| 478 | mark (iresult), show, mark (iresult)); | 480 | mark (iresult), show, mark (iresult)); |
| 479 | 481 | ||
| 480 | /* Append a unit string for this OID if there is one */ | 482 | /* Append a unit string for this OID if there is one */ |
| 481 | if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL) | 483 | if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL) |
| 482 | asprintf (&outbuff, "%s %s", outbuff, unitv[i]); | 484 | xasprintf (&outbuff, "%s %s", outbuff, unitv[i]); |
| 483 | 485 | ||
| 484 | /* Write perfdata with whatever can be parsed by strtod, if possible */ | 486 | /* Write perfdata with whatever can be parsed by strtod, if possible */ |
| 485 | ptr = NULL; | 487 | ptr = NULL; |
| 486 | strtod(show, &ptr); | 488 | strtod(show, &ptr); |
| 487 | if (ptr > show) { | 489 | if (ptr > show) { |
| 488 | if (nlabels >= (size_t)1 && (size_t)i < nlabels && labels[i] != NULL) | 490 | if (perf_labels && nlabels >= (size_t)1 && (size_t)i < nlabels && labels[i] != NULL) |
| 489 | temp_string=labels[i]; | 491 | temp_string=labels[i]; |
| 490 | else | 492 | else |
| 491 | temp_string=oidname; | 493 | temp_string=oidname; |
| 492 | strncat(perfstr, temp_string, sizeof(perfstr)-strlen(perfstr)-1); | 494 | if (strpbrk (temp_string, " ='\"") == NULL) { |
| 495 | strncat(perfstr, temp_string, sizeof(perfstr)-strlen(perfstr)-1); | ||
| 496 | } else { | ||
| 497 | if (strpbrk (temp_string, "'") == NULL) { | ||
| 498 | quote_string="'"; | ||
| 499 | } else { | ||
| 500 | quote_string="\""; | ||
| 501 | } | ||
| 502 | strncat(perfstr, quote_string, sizeof(perfstr)-strlen(perfstr)-1); | ||
| 503 | strncat(perfstr, temp_string, sizeof(perfstr)-strlen(perfstr)-1); | ||
| 504 | strncat(perfstr, quote_string, sizeof(perfstr)-strlen(perfstr)-1); | ||
| 505 | } | ||
| 493 | strncat(perfstr, "=", sizeof(perfstr)-strlen(perfstr)-1); | 506 | strncat(perfstr, "=", sizeof(perfstr)-strlen(perfstr)-1); |
| 494 | len = sizeof(perfstr)-strlen(perfstr)-1; | 507 | len = sizeof(perfstr)-strlen(perfstr)-1; |
| 495 | strncat(perfstr, show, len>ptr-show ? ptr-show : len); | 508 | strncat(perfstr, show, len>ptr-show ? ptr-show : len); |
| @@ -510,7 +523,7 @@ main (int argc, char **argv) | |||
| 510 | 523 | ||
| 511 | current_length=0; | 524 | current_length=0; |
| 512 | for(i=0; i<total_oids; i++) { | 525 | for(i=0; i<total_oids; i++) { |
| 513 | asprintf(&temp_string,"%.0f",response_value[i]); | 526 | xasprintf(&temp_string,"%.0f",response_value[i]); |
| 514 | if(temp_string==NULL) | 527 | if(temp_string==NULL) |
| 515 | die(STATE_UNKNOWN,_("Cannot asprintf()")); | 528 | die(STATE_UNKNOWN,_("Cannot asprintf()")); |
| 516 | response_length = strlen(temp_string); | 529 | response_length = strlen(temp_string); |
| @@ -583,6 +596,7 @@ process_arguments (int argc, char **argv) | |||
| 583 | {"rate", no_argument, 0, L_CALCULATE_RATE}, | 596 | {"rate", no_argument, 0, L_CALCULATE_RATE}, |
| 584 | {"rate-multiplier", required_argument, 0, L_RATE_MULTIPLIER}, | 597 | {"rate-multiplier", required_argument, 0, L_RATE_MULTIPLIER}, |
| 585 | {"invert-search", no_argument, 0, L_INVERT_SEARCH}, | 598 | {"invert-search", no_argument, 0, L_INVERT_SEARCH}, |
| 599 | {"perf-oids", no_argument, 0, 'O'}, | ||
| 586 | {0, 0, 0, 0} | 600 | {0, 0, 0, 0} |
| 587 | }; | 601 | }; |
| 588 | 602 | ||
| @@ -600,7 +614,7 @@ process_arguments (int argc, char **argv) | |||
| 600 | } | 614 | } |
| 601 | 615 | ||
| 602 | while (1) { | 616 | while (1) { |
| 603 | c = getopt_long (argc, argv, "nhvVt:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:L:U:a:x:A:X:", | 617 | c = getopt_long (argc, argv, "nhvVOt:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:L:U:a:x:A:X:", |
| 604 | longopts, &option); | 618 | longopts, &option); |
| 605 | 619 | ||
| 606 | if (c == -1 || c == EOF) | 620 | if (c == -1 || c == EOF) |
| @@ -798,6 +812,9 @@ process_arguments (int argc, char **argv) | |||
| 798 | case L_INVERT_SEARCH: | 812 | case L_INVERT_SEARCH: |
| 799 | invert_search=1; | 813 | invert_search=1; |
| 800 | break; | 814 | break; |
| 815 | case 'O': | ||
| 816 | perf_labels=0; | ||
| 817 | break; | ||
| 801 | } | 818 | } |
| 802 | } | 819 | } |
| 803 | 820 | ||
| @@ -852,7 +869,7 @@ validate_arguments () | |||
| 852 | die(STATE_UNKNOWN, _("No OIDs specified\n")); | 869 | die(STATE_UNKNOWN, _("No OIDs specified\n")); |
| 853 | 870 | ||
| 854 | if (proto == NULL) | 871 | if (proto == NULL) |
| 855 | asprintf(&proto, DEFAULT_PROTOCOL); | 872 | xasprintf(&proto, DEFAULT_PROTOCOL); |
| 856 | 873 | ||
| 857 | if ((strcmp(proto,"1") == 0) || (strcmp(proto, "2c")==0)) { /* snmpv1 or snmpv2c */ | 874 | if ((strcmp(proto,"1") == 0) || (strcmp(proto, "2c")==0)) { /* snmpv1 or snmpv2c */ |
| 858 | numauthpriv = 2; | 875 | numauthpriv = 2; |
| @@ -862,7 +879,7 @@ validate_arguments () | |||
| 862 | } | 879 | } |
| 863 | else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */ | 880 | else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */ |
| 864 | if (seclevel == NULL) | 881 | if (seclevel == NULL) |
| 865 | asprintf(&seclevel, "noAuthNoPriv"); | 882 | xasprintf(&seclevel, "noAuthNoPriv"); |
| 866 | 883 | ||
| 867 | if (strcmp(seclevel, "noAuthNoPriv") == 0) { | 884 | if (strcmp(seclevel, "noAuthNoPriv") == 0) { |
| 868 | numauthpriv = 2; | 885 | numauthpriv = 2; |
| @@ -875,7 +892,7 @@ validate_arguments () | |||
| 875 | } | 892 | } |
| 876 | 893 | ||
| 877 | if (authproto == NULL ) | 894 | if (authproto == NULL ) |
| 878 | asprintf(&authproto, DEFAULT_AUTH_PROTOCOL); | 895 | xasprintf(&authproto, DEFAULT_AUTH_PROTOCOL); |
| 879 | 896 | ||
| 880 | if (secname == NULL) | 897 | if (secname == NULL) |
| 881 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "secname"); | 898 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "secname"); |
| @@ -896,7 +913,7 @@ validate_arguments () | |||
| 896 | authpriv[7] = strdup (authpasswd); | 913 | authpriv[7] = strdup (authpasswd); |
| 897 | } else if ( strcmp(seclevel, "authPriv") == 0 ) { | 914 | } else if ( strcmp(seclevel, "authPriv") == 0 ) { |
| 898 | if (privproto == NULL ) | 915 | if (privproto == NULL ) |
| 899 | asprintf(&privproto, DEFAULT_PRIV_PROTOCOL); | 916 | xasprintf(&privproto, DEFAULT_PRIV_PROTOCOL); |
| 900 | 917 | ||
| 901 | if (privpasswd == NULL) | 918 | if (privpasswd == NULL) |
| 902 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "privpasswd"); | 919 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "privpasswd"); |
| @@ -1063,6 +1080,9 @@ print_help (void) | |||
| 1063 | printf (" %s\n", "-e, --retries=INTEGER"); | 1080 | printf (" %s\n", "-e, --retries=INTEGER"); |
| 1064 | printf (" %s\n", _("Number of retries to be used in the requests")); | 1081 | printf (" %s\n", _("Number of retries to be used in the requests")); |
| 1065 | 1082 | ||
| 1083 | printf (" %s\n", "-O, --perf-oids"); | ||
| 1084 | printf (" %s\n", _("Label performance data with OIDs instead of --label's")); | ||
| 1085 | |||
| 1066 | printf (UT_VERBOSE); | 1086 | printf (UT_VERBOSE); |
| 1067 | 1087 | ||
| 1068 | printf ("\n"); | 1088 | printf ("\n"); |
