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.c94
1 files changed, 68 insertions, 26 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index 7c3bc4b..bae3830 100644
--- a/plugins/check_snmp.c
+++ b/plugins/check_snmp.c
@@ -57,7 +57,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
57#define WARN_STRING 16 57#define WARN_STRING 16
58#define WARN_REGEX 32 58#define WARN_REGEX 32
59 59
60#define MAX_OIDS 8 60#define OID_COUNT_STEP 8
61 61
62/* Longopts only arguments */ 62/* Longopts only arguments */
63#define L_CALCULATE_RATE CHAR_MAX+1 63#define L_CALCULATE_RATE CHAR_MAX+1
@@ -112,6 +112,7 @@ char *privproto = NULL;
112char *authpasswd = NULL; 112char *authpasswd = NULL;
113char *privpasswd = NULL; 113char *privpasswd = NULL;
114char **oids = NULL; 114char **oids = NULL;
115size_t oids_size = NULL;
115char *label; 116char *label;
116char *units; 117char *units;
117char *port; 118char *port;
@@ -121,19 +122,22 @@ int invert_search=0;
121char **labels = NULL; 122char **labels = NULL;
122char **unitv = NULL; 123char **unitv = NULL;
123size_t nlabels = 0; 124size_t nlabels = 0;
124size_t labels_size = 8; 125size_t labels_size = OID_COUNT_STEP;
125size_t nunits = 0; 126size_t nunits = 0;
126size_t unitv_size = 8; 127size_t unitv_size = OID_COUNT_STEP;
127int numoids = 0; 128int numoids = 0;
128int numauthpriv = 0; 129int numauthpriv = 0;
129int verbose = 0; 130int verbose = 0;
130int usesnmpgetnext = FALSE; 131int usesnmpgetnext = FALSE;
131char *warning_thresholds = NULL; 132char *warning_thresholds = NULL;
132char *critical_thresholds = NULL; 133char *critical_thresholds = NULL;
133thresholds *thlds[MAX_OIDS]; 134thresholds **thlds;
134double response_value[MAX_OIDS]; 135size_t thlds_size = OID_COUNT_STEP;
136double *response_value;
137size_t response_size = OID_COUNT_STEP;
135int retries = 0; 138int retries = 0;
136int eval_method[MAX_OIDS]; 139int *eval_method;
140size_t eval_size = OID_COUNT_STEP;
137char *delimiter; 141char *delimiter;
138char *output_delim; 142char *output_delim;
139char *miblist = NULL; 143char *miblist = NULL;
@@ -142,7 +146,8 @@ int calculate_rate = 0;
142double offset = 0.0; 146double offset = 0.0;
143int rate_multiplier = 1; 147int rate_multiplier = 1;
144state_data *previous_state; 148state_data *previous_state;
145double previous_value[MAX_OIDS]; 149double *previous_value;
150size_t previous_size = OID_COUNT_STEP;
146int perf_labels = 1; 151int perf_labels = 1;
147 152
148 153
@@ -150,16 +155,18 @@ static char *fix_snmp_range(char *th)
150{ 155{
151 double left, right; 156 double left, right;
152 char *colon, *ret; 157 char *colon, *ret;
153 if (!(colon = strchr(th, ':'))) 158
159 if ((colon = strchr(th, ':')) == NULL || *(colon + 1) == '\0')
154 return th; 160 return th;
155 *colon = 0;
156 161
157 left = strtod(th, NULL); 162 left = strtod(th, NULL);
158 right = strtod(colon + 1, NULL); 163 right = strtod(colon + 1, NULL);
159 if (right >= left) { 164 if (right >= left)
160 return th; 165 return th;
161 } 166
162 ret = malloc(strlen(th) + strlen(colon + 1) + 2); 167 if ((ret = malloc(strlen(th) + 2)) == NULL)
168 die(STATE_UNKNOWN, _("Cannot malloc"));
169 *colon = '\0';
163 sprintf(ret, "@%s:%s", colon + 1, th); 170 sprintf(ret, "@%s:%s", colon + 1, th);
164 free(th); 171 free(th);
165 return ret; 172 return ret;
@@ -204,8 +211,11 @@ main (int argc, char **argv)
204 211
205 labels = malloc (labels_size * sizeof(*labels)); 212 labels = malloc (labels_size * sizeof(*labels));
206 unitv = malloc (unitv_size * sizeof(*unitv)); 213 unitv = malloc (unitv_size * sizeof(*unitv));
207 for (i = 0; i < MAX_OIDS; i++) 214 thlds = malloc (thlds_size * sizeof(*thlds));
208 eval_method[i] = CHECK_UNDEF; 215 response_value = malloc (response_size * sizeof(*response_value));
216 previous_value = malloc (previous_size * sizeof(*previous_value));
217 eval_method = calloc (eval_size, sizeof(*eval_method));
218 oids = calloc(oids_size, sizeof (char *));
209 219
210 label = strdup ("SNMP"); 220 label = strdup ("SNMP");
211 units = strdup (""); 221 units = strdup ("");
@@ -223,13 +233,14 @@ main (int argc, char **argv)
223 233
224 np_set_args(argc, argv); 234 np_set_args(argc, argv);
225 235
236 time(&current_time);
237
226 if (process_arguments (argc, argv) == ERROR) 238 if (process_arguments (argc, argv) == ERROR)
227 usage4 (_("Could not parse arguments")); 239 usage4 (_("Could not parse arguments"));
228 240
229 if(calculate_rate) { 241 if(calculate_rate) {
230 if (!strcmp(label, "SNMP")) 242 if (!strcmp(label, "SNMP"))
231 label = strdup("SNMP RATE"); 243 label = strdup("SNMP RATE");
232 time(&current_time);
233 i=0; 244 i=0;
234 previous_state = np_state_read(); 245 previous_state = np_state_read();
235 if(previous_state!=NULL) { 246 if(previous_state!=NULL) {
@@ -238,6 +249,10 @@ main (int argc, char **argv)
238 while((ap = strsep(&previous_string, ":")) != NULL) { 249 while((ap = strsep(&previous_string, ":")) != NULL) {
239 if(verbose>2) 250 if(verbose>2)
240 printf("State for %d=%s\n", i, ap); 251 printf("State for %d=%s\n", i, ap);
252 while (i >= previous_size) {
253 previous_size += OID_COUNT_STEP;
254 previous_value = realloc(previous_value, previous_size * sizeof(*previous_value));
255 }
241 previous_value[i++]=strtod(ap,NULL); 256 previous_value[i++]=strtod(ap,NULL);
242 } 257 }
243 } 258 }
@@ -253,6 +268,11 @@ main (int argc, char **argv)
253 w = w ? fix_snmp_range(w) : NULL; 268 w = w ? fix_snmp_range(w) : NULL;
254 c = c ? fix_snmp_range(c) : NULL; 269 c = c ? fix_snmp_range(c) : NULL;
255 270
271 while (i >= thlds_size) {
272 thlds_size += OID_COUNT_STEP;
273 thlds = realloc(thlds, thlds_size * sizeof(*thlds));
274 }
275
256 /* Skip empty thresholds, while avoiding segfault */ 276 /* Skip empty thresholds, while avoiding segfault */
257 set_thresholds(&thlds[i], 277 set_thresholds(&thlds[i],
258 w ? strpbrk(w, NP_THRESHOLDS_CHARS) : NULL, 278 w ? strpbrk(w, NP_THRESHOLDS_CHARS) : NULL,
@@ -432,6 +452,10 @@ main (int argc, char **argv)
432 ptr = strpbrk (show, "0123456789"); 452 ptr = strpbrk (show, "0123456789");
433 if (ptr == NULL) 453 if (ptr == NULL)
434 die (STATE_UNKNOWN,_("No valid data returned (%s)\n"), show); 454 die (STATE_UNKNOWN,_("No valid data returned (%s)\n"), show);
455 while (i >= response_size) {
456 response_size += OID_COUNT_STEP;
457 response_value = realloc(response_value, response_size * sizeof(*response_value));
458 }
435 response_value[i] = strtod (ptr, NULL) + offset; 459 response_value[i] = strtod (ptr, NULL) + offset;
436 460
437 if(calculate_rate) { 461 if(calculate_rate) {
@@ -459,7 +483,7 @@ main (int argc, char **argv)
459 } 483 }
460 484
461 /* Process this block for string matching */ 485 /* Process this block for string matching */
462 else if (eval_method[i] & CRIT_STRING) { 486 else if (eval_size > i && eval_method[i] & CRIT_STRING) {
463 if (strcmp (show, string_value)) 487 if (strcmp (show, string_value))
464 iresult = (invert_search==0) ? STATE_CRITICAL : STATE_OK; 488 iresult = (invert_search==0) ? STATE_CRITICAL : STATE_OK;
465 else 489 else
@@ -467,7 +491,7 @@ main (int argc, char **argv)
467 } 491 }
468 492
469 /* Process this block for regex matching */ 493 /* Process this block for regex matching */
470 else if (eval_method[i] & CRIT_REGEX) { 494 else if (eval_size > i && eval_method[i] & CRIT_REGEX) {
471 excode = regexec (&preg, response, 10, pmatch, eflags); 495 excode = regexec (&preg, response, 10, pmatch, eflags);
472 if (excode == 0) { 496 if (excode == 0) {
473 iresult = (invert_search==0) ? STATE_OK : STATE_CRITICAL; 497 iresult = (invert_search==0) ? STATE_OK : STATE_CRITICAL;
@@ -485,9 +509,9 @@ main (int argc, char **argv)
485 /* Process this block for existence-nonexistence checks */ 509 /* Process this block for existence-nonexistence checks */
486 /* TV: Should this be outside of this else block? */ 510 /* TV: Should this be outside of this else block? */
487 else { 511 else {
488 if (eval_method[i] & CRIT_PRESENT) 512 if (eval_size > i && eval_method[i] & CRIT_PRESENT)
489 iresult = STATE_CRITICAL; 513 iresult = STATE_CRITICAL;
490 else if (eval_method[i] & WARN_PRESENT) 514 else if (eval_size > i && eval_method[i] & WARN_PRESENT)
491 iresult = STATE_WARNING; 515 iresult = STATE_WARNING;
492 else if (response && iresult == STATE_DEPENDENT) 516 else if (response && iresult == STATE_DEPENDENT)
493 iresult = STATE_OK; 517 iresult = STATE_OK;
@@ -727,23 +751,36 @@ process_arguments (int argc, char **argv)
727 */ 751 */
728 needmibs = TRUE; 752 needmibs = TRUE;
729 } 753 }
730 if (!oids) oids = calloc(MAX_OIDS, sizeof (char *)); 754 for (ptr = strtok(optarg, ", "); ptr != NULL; ptr = strtok(NULL, ", "), j++) {
731 for (ptr = strtok(optarg, ", "); ptr != NULL && j < MAX_OIDS; ptr = strtok(NULL, ", "), j++) { 755 while (j >= oids_size) {
756 oids_size += OID_COUNT_STEP;
757 oids = realloc(oids, oids_size * sizeof (*oids));
758 }
732 oids[j] = strdup(ptr); 759 oids[j] = strdup(ptr);
733 } 760 }
734 numoids = j; 761 numoids = j;
735 if (c == 'E' || c == 'e') { 762 if (c == 'E' || c == 'e') {
736 jj++; 763 jj++;
737 ii++; 764 ii++;
765 while (j+1 >= eval_size) {
766 eval_size += OID_COUNT_STEP;
767 eval_method = realloc(eval_method, eval_size * sizeof(*eval_method));
768 memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8);
769 }
770 if (c == 'E')
771 eval_method[j+1] |= WARN_PRESENT;
772 else if (c == 'e')
773 eval_method[j+1] |= CRIT_PRESENT;
738 } 774 }
739 if (c == 'E')
740 eval_method[j+1] |= WARN_PRESENT;
741 else if (c == 'e')
742 eval_method[j+1] |= CRIT_PRESENT;
743 break; 775 break;
744 case 's': /* string or substring */ 776 case 's': /* string or substring */
745 strncpy (string_value, optarg, sizeof (string_value) - 1); 777 strncpy (string_value, optarg, sizeof (string_value) - 1);
746 string_value[sizeof (string_value) - 1] = 0; 778 string_value[sizeof (string_value) - 1] = 0;
779 while (jj >= eval_size) {
780 eval_size += OID_COUNT_STEP;
781 eval_method = realloc(eval_method, eval_size * sizeof(*eval_method));
782 memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8);
783 }
747 eval_method[jj++] = CRIT_STRING; 784 eval_method[jj++] = CRIT_STRING;
748 ii++; 785 ii++;
749 break; 786 break;
@@ -759,6 +796,11 @@ process_arguments (int argc, char **argv)
759 printf (_("Could Not Compile Regular Expression")); 796 printf (_("Could Not Compile Regular Expression"));
760 return ERROR; 797 return ERROR;
761 } 798 }
799 while (jj >= eval_size) {
800 eval_size += OID_COUNT_STEP;
801 eval_method = realloc(eval_method, eval_size * sizeof(*eval_method));
802 memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8);
803 }
762 eval_method[jj++] = CRIT_REGEX; 804 eval_method[jj++] = CRIT_REGEX;
763 ii++; 805 ii++;
764 break; 806 break;
@@ -1125,7 +1167,7 @@ print_help (void)
1125 printf ("\n"); 1167 printf ("\n");
1126 printf ("%s\n", _("Notes:")); 1168 printf ("%s\n", _("Notes:"));
1127 printf (" %s\n", _("- Multiple OIDs (and labels) may be indicated by a comma or space-delimited ")); 1169 printf (" %s\n", _("- Multiple OIDs (and labels) may be indicated by a comma or space-delimited "));
1128 printf (" %s %i %s\n", _("list (lists with internal spaces must be quoted). Maximum:"), MAX_OIDS, _("OIDs.")); 1170 printf (" %s\n", _("list (lists with internal spaces must be quoted)."));
1129 1171
1130 printf(" -%s", UT_THRESHOLDS_NOTES); 1172 printf(" -%s", UT_THRESHOLDS_NOTES);
1131 1173