From d87395ec3d4bec777ab92fc74c8370877171259f Mon Sep 17 00:00:00 2001 From: Robin Sonefors Date: Tue, 29 Jan 2013 13:18:32 +0100 Subject: check_snmp: Dynamically grow all data structures Before this patch, there was a constant, MAX_OIDS, that determined the amount of slots most (but not all - see labels) array data structures would have. It was set to 8. Some users would like to use more than that, but rather than bumping the constant, let's use the same type of logic we already use for labels - grow the space 8 slots at a time. This will allow us to potentially support an infinite amount of oids - or at least as many as the packetsize on the SNMP server allows, which is usually significantly smaller than infinity, yet often larger than 8. Signed-off-by: Robin Sonefors --- plugins/check_snmp.c | 79 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c index 7c5d0ec5..28354b0d 100644 --- a/plugins/check_snmp.c +++ b/plugins/check_snmp.c @@ -57,7 +57,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; #define WARN_STRING 16 #define WARN_REGEX 32 -#define MAX_OIDS 8 +#define OID_COUNT_STEP 8 /* Longopts only arguments */ #define L_CALCULATE_RATE CHAR_MAX+1 @@ -111,6 +111,7 @@ char *privproto = NULL; char *authpasswd = NULL; char *privpasswd = NULL; char **oids = NULL; +size_t oids_size = NULL; char *label; char *units; char *port; @@ -120,19 +121,22 @@ int invert_search=0; char **labels = NULL; char **unitv = NULL; size_t nlabels = 0; -size_t labels_size = 8; +size_t labels_size = OID_COUNT_STEP; size_t nunits = 0; -size_t unitv_size = 8; +size_t unitv_size = OID_COUNT_STEP; int numoids = 0; int numauthpriv = 0; int verbose = 0; int usesnmpgetnext = FALSE; char *warning_thresholds = NULL; char *critical_thresholds = NULL; -thresholds *thlds[MAX_OIDS]; -double response_value[MAX_OIDS]; +thresholds **thlds; +size_t thlds_size = OID_COUNT_STEP; +double *response_value; +size_t response_size = OID_COUNT_STEP; int retries = 0; -int eval_method[MAX_OIDS]; +int *eval_method; +size_t eval_size = OID_COUNT_STEP; char *delimiter; char *output_delim; char *miblist = NULL; @@ -140,7 +144,8 @@ int needmibs = FALSE; int calculate_rate = 0; int rate_multiplier = 1; state_data *previous_state; -double previous_value[MAX_OIDS]; +double *previous_value; +size_t previous_size = OID_COUNT_STEP; int perf_labels = 1; @@ -202,8 +207,11 @@ main (int argc, char **argv) labels = malloc (labels_size * sizeof(*labels)); unitv = malloc (unitv_size * sizeof(*unitv)); - for (i = 0; i < MAX_OIDS; i++) - eval_method[i] = CHECK_UNDEF; + thlds = malloc (thlds_size * sizeof(*thlds)); + response_value = malloc (response_size * sizeof(*response_value)); + previous_value = malloc (previous_size * sizeof(*previous_value)); + eval_method = calloc (eval_size, sizeof(*eval_method)); + oids = calloc(oids_size, sizeof (char *)); label = strdup ("SNMP"); units = strdup (""); @@ -236,6 +244,10 @@ main (int argc, char **argv) while((ap = strsep(&previous_string, ":")) != NULL) { if(verbose>2) printf("State for %d=%s\n", i, ap); + while (i >= previous_size) { + previous_size += OID_COUNT_STEP; + previous_value = realloc(previous_value, previous_size * sizeof(*previous_value)); + } previous_value[i++]=strtod(ap,NULL); } } @@ -251,6 +263,11 @@ main (int argc, char **argv) w = w ? fix_snmp_range(w) : NULL; c = c ? fix_snmp_range(c) : NULL; + while (i >= thlds_size) { + thlds_size += OID_COUNT_STEP; + thlds = realloc(thlds, thlds_size * sizeof(*thlds)); + } + /* Skip empty thresholds, while avoiding segfault */ set_thresholds(&thlds[i], w ? strpbrk(w, NP_THRESHOLDS_CHARS) : NULL, @@ -429,6 +446,10 @@ main (int argc, char **argv) ptr = strpbrk (show, "0123456789"); if (ptr == NULL) die (STATE_UNKNOWN,_("No valid data returned (%s)\n"), show); + while (i >= response_size) { + response_size += OID_COUNT_STEP; + response_value = realloc(response_value, response_size * sizeof(*response_value)); + } response_value[i] = strtod (ptr, NULL); if(calculate_rate) { @@ -456,7 +477,7 @@ main (int argc, char **argv) } /* Process this block for string matching */ - else if (eval_method[i] & CRIT_STRING) { + else if (eval_size > i && eval_method[i] & CRIT_STRING) { if (strcmp (show, string_value)) iresult = (invert_search==0) ? STATE_CRITICAL : STATE_OK; else @@ -464,7 +485,7 @@ main (int argc, char **argv) } /* Process this block for regex matching */ - else if (eval_method[i] & CRIT_REGEX) { + else if (eval_size > i && eval_method[i] & CRIT_REGEX) { excode = regexec (&preg, response, 10, pmatch, eflags); if (excode == 0) { iresult = (invert_search==0) ? STATE_OK : STATE_CRITICAL; @@ -482,9 +503,9 @@ main (int argc, char **argv) /* Process this block for existence-nonexistence checks */ /* TV: Should this be outside of this else block? */ else { - if (eval_method[i] & CRIT_PRESENT) + if (eval_size > i && eval_method[i] & CRIT_PRESENT) iresult = STATE_CRITICAL; - else if (eval_method[i] & WARN_PRESENT) + else if (eval_size > i && eval_method[i] & WARN_PRESENT) iresult = STATE_WARNING; else if (response && iresult == STATE_DEPENDENT) iresult = STATE_OK; @@ -723,23 +744,36 @@ process_arguments (int argc, char **argv) */ needmibs = TRUE; } - if (!oids) oids = calloc(MAX_OIDS, sizeof (char *)); - for (ptr = strtok(optarg, ", "); ptr != NULL && j < MAX_OIDS; ptr = strtok(NULL, ", "), j++) { + for (ptr = strtok(optarg, ", "); ptr != NULL; ptr = strtok(NULL, ", "), j++) { + while (j >= oids_size) { + oids_size += OID_COUNT_STEP; + oids = realloc(oids, oids_size * sizeof (*oids)); + } oids[j] = strdup(ptr); } numoids = j; if (c == 'E' || c == 'e') { jj++; ii++; + while (j+1 >= eval_size) { + eval_size += OID_COUNT_STEP; + eval_method = realloc(eval_method, eval_size * sizeof(*eval_method)); + memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8); + } + if (c == 'E') + eval_method[j+1] |= WARN_PRESENT; + else if (c == 'e') + eval_method[j+1] |= CRIT_PRESENT; } - if (c == 'E') - eval_method[j+1] |= WARN_PRESENT; - else if (c == 'e') - eval_method[j+1] |= CRIT_PRESENT; break; case 's': /* string or substring */ strncpy (string_value, optarg, sizeof (string_value) - 1); string_value[sizeof (string_value) - 1] = 0; + while (jj >= eval_size) { + eval_size += OID_COUNT_STEP; + eval_method = realloc(eval_method, eval_size * sizeof(*eval_method)); + memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8); + } eval_method[jj++] = CRIT_STRING; ii++; break; @@ -755,6 +789,11 @@ process_arguments (int argc, char **argv) printf (_("Could Not Compile Regular Expression")); return ERROR; } + while (jj >= eval_size) { + eval_size += OID_COUNT_STEP; + eval_method = realloc(eval_method, eval_size * sizeof(*eval_method)); + memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8); + } eval_method[jj++] = CRIT_REGEX; ii++; break; @@ -1116,7 +1155,7 @@ print_help (void) printf ("\n"); printf ("%s\n", _("Notes:")); printf (" %s\n", _("- Multiple OIDs (and labels) may be indicated by a comma or space-delimited ")); - printf (" %s %i %s\n", _("list (lists with internal spaces must be quoted). Maximum:"), MAX_OIDS, _("OIDs.")); + printf (" %s %i %s\n", _("list (lists with internal spaces must be quoted).")); printf(" -%s", UT_THRESHOLDS_NOTES); -- cgit v1.2.3 From a20611d4357c3c3ebe0a62776a1642e7904c1410 Mon Sep 17 00:00:00 2001 From: Robin Sonefors Date: Tue, 29 Jan 2013 15:08:44 +0100 Subject: check_snmp: Close potential for using uninitialized memory This was reported by clang, and in order for it to understand attributes properly, I had to modify the macro that checks for non-gcc compilers. Signed-off-by: Robin Sonefors --- plugins/check_snmp.c | 3 ++- plugins/common.h | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c index 28354b0d..c73562ba 100644 --- a/plugins/check_snmp.c +++ b/plugins/check_snmp.c @@ -229,13 +229,14 @@ main (int argc, char **argv) np_set_args(argc, argv); + time(¤t_time); + if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); if(calculate_rate) { if (!strcmp(label, "SNMP")) label = strdup("SNMP RATE"); - time(¤t_time); i=0; previous_state = np_state_read(); if(previous_state!=NULL) { diff --git a/plugins/common.h b/plugins/common.h index c0dc2f41..f024b2ae 100644 --- a/plugins/common.h +++ b/plugins/common.h @@ -208,9 +208,9 @@ enum { # define bindtextdomain(Domainname, Dirname) /* empty */ #endif -/* For non-GNU compilers to ignore __attribute__ */ -#ifndef __GNUC__ -# define __attribute__(x) /* do nothing */ +/* For non-GNU/non-clang compilers to ignore __attribute__ */ +#if !defined(__GNUC__) && !defined(__CLANG__) +# define __attribute__(noreturn) /* do nothing */ #endif #endif /* _COMMON_H_ */ -- cgit v1.2.3