diff options
Diffstat (limited to 'plugins/check_snmp.c')
-rw-r--r-- | plugins/check_snmp.c | 92 |
1 files changed, 63 insertions, 29 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c index 2d9861bc..d2f2f8b5 100644 --- a/plugins/check_snmp.c +++ b/plugins/check_snmp.c | |||
@@ -63,6 +63,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; | |||
63 | #define L_CALCULATE_RATE CHAR_MAX+1 | 63 | #define L_CALCULATE_RATE CHAR_MAX+1 |
64 | #define L_RATE_MULTIPLIER CHAR_MAX+2 | 64 | #define L_RATE_MULTIPLIER CHAR_MAX+2 |
65 | #define L_INVERT_SEARCH CHAR_MAX+3 | 65 | #define L_INVERT_SEARCH CHAR_MAX+3 |
66 | #define L_OFFSET CHAR_MAX+4 | ||
66 | 67 | ||
67 | /* Gobble to string - stop incrementing c when c[0] match one of the | 68 | /* Gobble to string - stop incrementing c when c[0] match one of the |
68 | * characters in s */ | 69 | * characters in s */ |
@@ -138,12 +139,34 @@ char *output_delim; | |||
138 | char *miblist = NULL; | 139 | char *miblist = NULL; |
139 | int needmibs = FALSE; | 140 | int needmibs = FALSE; |
140 | int calculate_rate = 0; | 141 | int calculate_rate = 0; |
142 | double offset = 0.0; | ||
141 | int rate_multiplier = 1; | 143 | int rate_multiplier = 1; |
142 | state_data *previous_state; | 144 | state_data *previous_state; |
143 | double previous_value[MAX_OIDS]; | 145 | double previous_value[MAX_OIDS]; |
144 | int perf_labels = 1; | 146 | int perf_labels = 1; |
145 | 147 | ||
146 | 148 | ||
149 | static char *fix_snmp_range(char *th) | ||
150 | { | ||
151 | double left, right; | ||
152 | char *colon, *ret; | ||
153 | |||
154 | if ((colon = strchr(th, ':')) == NULL || *(colon + 1) == '\0') | ||
155 | return th; | ||
156 | |||
157 | left = strtod(th, NULL); | ||
158 | right = strtod(colon + 1, NULL); | ||
159 | if (right >= left) | ||
160 | return th; | ||
161 | |||
162 | if ((ret = malloc(strlen(th) + 2)) == NULL) | ||
163 | die(STATE_UNKNOWN, _("Cannot malloc")); | ||
164 | *colon = '\0'; | ||
165 | sprintf(ret, "@%s:%s", colon + 1, th); | ||
166 | free(th); | ||
167 | return ret; | ||
168 | } | ||
169 | |||
147 | int | 170 | int |
148 | main (int argc, char **argv) | 171 | main (int argc, char **argv) |
149 | { | 172 | { |
@@ -181,8 +204,8 @@ main (int argc, char **argv) | |||
181 | bindtextdomain (PACKAGE, LOCALEDIR); | 204 | bindtextdomain (PACKAGE, LOCALEDIR); |
182 | textdomain (PACKAGE); | 205 | textdomain (PACKAGE); |
183 | 206 | ||
184 | labels = malloc (labels_size); | 207 | labels = malloc (labels_size * sizeof(*labels)); |
185 | unitv = malloc (unitv_size); | 208 | unitv = malloc (unitv_size * sizeof(*unitv)); |
186 | for (i = 0; i < MAX_OIDS; i++) | 209 | for (i = 0; i < MAX_OIDS; i++) |
187 | eval_method[i] = CHECK_UNDEF; | 210 | eval_method[i] = CHECK_UNDEF; |
188 | 211 | ||
@@ -228,6 +251,10 @@ main (int argc, char **argv) | |||
228 | for (i=0; i<numoids; i++) { | 251 | for (i=0; i<numoids; i++) { |
229 | char *w = th_warn ? strndup(th_warn, strcspn(th_warn, ",")) : NULL; | 252 | char *w = th_warn ? strndup(th_warn, strcspn(th_warn, ",")) : NULL; |
230 | char *c = th_crit ? strndup(th_crit, strcspn(th_crit, ",")) : NULL; | 253 | char *c = th_crit ? strndup(th_crit, strcspn(th_crit, ",")) : NULL; |
254 | /* translate "2:1" to "@1:2" for backwards compatibility */ | ||
255 | w = w ? fix_snmp_range(w) : NULL; | ||
256 | c = c ? fix_snmp_range(c) : NULL; | ||
257 | |||
231 | /* Skip empty thresholds, while avoiding segfault */ | 258 | /* Skip empty thresholds, while avoiding segfault */ |
232 | set_thresholds(&thlds[i], | 259 | set_thresholds(&thlds[i], |
233 | w ? strpbrk(w, NP_THRESHOLDS_CHARS) : NULL, | 260 | w ? strpbrk(w, NP_THRESHOLDS_CHARS) : NULL, |
@@ -251,35 +278,36 @@ main (int argc, char **argv) | |||
251 | snmpcmd = strdup (PATH_TO_SNMPGET); | 278 | snmpcmd = strdup (PATH_TO_SNMPGET); |
252 | } | 279 | } |
253 | 280 | ||
254 | /* 9 arguments to pass before authpriv options + 1 for host and numoids. Add one for terminating NULL */ | 281 | /* 10 arguments to pass before authpriv options + 1 for host and numoids. Add one for terminating NULL */ |
255 | command_line = calloc (9 + numauthpriv + 1 + numoids + 1, sizeof (char *)); | 282 | command_line = calloc (10 + numauthpriv + 1 + numoids + 1, sizeof (char *)); |
256 | command_line[0] = snmpcmd; | 283 | command_line[0] = snmpcmd; |
257 | command_line[1] = strdup ("-t"); | 284 | command_line[1] = strdup ("-Le"); |
258 | xasprintf (&command_line[2], "%d", timeout_interval); | 285 | command_line[2] = strdup ("-t"); |
259 | command_line[3] = strdup ("-r"); | 286 | xasprintf (&command_line[3], "%d", timeout_interval); |
260 | xasprintf (&command_line[4], "%d", retries); | 287 | command_line[4] = strdup ("-r"); |
261 | command_line[5] = strdup ("-m"); | 288 | xasprintf (&command_line[5], "%d", retries); |
262 | command_line[6] = strdup (miblist); | 289 | command_line[6] = strdup ("-m"); |
263 | command_line[7] = "-v"; | 290 | command_line[7] = strdup (miblist); |
264 | command_line[8] = strdup (proto); | 291 | command_line[8] = "-v"; |
292 | command_line[9] = strdup (proto); | ||
265 | 293 | ||
266 | for (i = 0; i < numauthpriv; i++) { | 294 | for (i = 0; i < numauthpriv; i++) { |
267 | command_line[9 + i] = authpriv[i]; | 295 | command_line[10 + i] = authpriv[i]; |
268 | } | 296 | } |
269 | 297 | ||
270 | xasprintf (&command_line[9 + numauthpriv], "%s:%s", server_address, port); | 298 | xasprintf (&command_line[10 + numauthpriv], "%s:%s", server_address, port); |
271 | 299 | ||
272 | /* This is just for display purposes, so it can remain a string */ | 300 | /* This is just for display purposes, so it can remain a string */ |
273 | xasprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s", | 301 | xasprintf(&cl_hidden_auth, "%s -Le -t %d -r %d -m %s -v %s %s %s:%s", |
274 | snmpcmd, timeout_interval, retries, strlen(miblist) ? miblist : "''", proto, "[authpriv]", | 302 | snmpcmd, timeout_interval, retries, strlen(miblist) ? miblist : "''", proto, "[authpriv]", |
275 | server_address, port); | 303 | server_address, port); |
276 | 304 | ||
277 | for (i = 0; i < numoids; i++) { | 305 | for (i = 0; i < numoids; i++) { |
278 | command_line[9 + numauthpriv + 1 + i] = oids[i]; | 306 | command_line[10 + numauthpriv + 1 + i] = oids[i]; |
279 | xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); | 307 | xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); |
280 | } | 308 | } |
281 | 309 | ||
282 | command_line[9 + numauthpriv + 1 + numoids] = NULL; | 310 | command_line[10 + numauthpriv + 1 + numoids] = NULL; |
283 | 311 | ||
284 | if (verbose) | 312 | if (verbose) |
285 | printf ("%s\n", cl_hidden_auth); | 313 | printf ("%s\n", cl_hidden_auth); |
@@ -396,7 +424,7 @@ main (int argc, char **argv) | |||
396 | show = strstr (response, "Timeticks: "); | 424 | show = strstr (response, "Timeticks: "); |
397 | } | 425 | } |
398 | else | 426 | else |
399 | show = response; | 427 | show = response + 3; |
400 | 428 | ||
401 | iresult = STATE_DEPENDENT; | 429 | iresult = STATE_DEPENDENT; |
402 | 430 | ||
@@ -405,8 +433,8 @@ main (int argc, char **argv) | |||
405 | if (thlds[i]->warning || thlds[i]->critical || calculate_rate) { | 433 | if (thlds[i]->warning || thlds[i]->critical || calculate_rate) { |
406 | ptr = strpbrk (show, "0123456789"); | 434 | ptr = strpbrk (show, "0123456789"); |
407 | if (ptr == NULL) | 435 | if (ptr == NULL) |
408 | die (STATE_UNKNOWN,_("No valid data returned")); | 436 | die (STATE_UNKNOWN,_("No valid data returned (%s)\n"), show); |
409 | response_value[i] = strtod (ptr, NULL); | 437 | response_value[i] = strtod (ptr, NULL) + offset; |
410 | 438 | ||
411 | if(calculate_rate) { | 439 | if(calculate_rate) { |
412 | if (previous_state!=NULL) { | 440 | if (previous_state!=NULL) { |
@@ -595,6 +623,7 @@ process_arguments (int argc, char **argv) | |||
595 | {"next", no_argument, 0, 'n'}, | 623 | {"next", no_argument, 0, 'n'}, |
596 | {"rate", no_argument, 0, L_CALCULATE_RATE}, | 624 | {"rate", no_argument, 0, L_CALCULATE_RATE}, |
597 | {"rate-multiplier", required_argument, 0, L_RATE_MULTIPLIER}, | 625 | {"rate-multiplier", required_argument, 0, L_RATE_MULTIPLIER}, |
626 | {"offset", required_argument, 0, L_OFFSET}, | ||
598 | {"invert-search", no_argument, 0, L_INVERT_SEARCH}, | 627 | {"invert-search", no_argument, 0, L_INVERT_SEARCH}, |
599 | {"perf-oids", no_argument, 0, 'O'}, | 628 | {"perf-oids", no_argument, 0, 'O'}, |
600 | {0, 0, 0, 0} | 629 | {0, 0, 0, 0} |
@@ -745,9 +774,9 @@ process_arguments (int argc, char **argv) | |||
745 | break; | 774 | break; |
746 | case 'l': /* label */ | 775 | case 'l': /* label */ |
747 | nlabels++; | 776 | nlabels++; |
748 | if (nlabels >= labels_size) { | 777 | if (nlabels > labels_size) { |
749 | labels_size += 8; | 778 | labels_size += 8; |
750 | labels = realloc (labels, labels_size); | 779 | labels = realloc (labels, labels_size * sizeof(*labels)); |
751 | if (labels == NULL) | 780 | if (labels == NULL) |
752 | die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), (int)nlabels); | 781 | die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), (int)nlabels); |
753 | } | 782 | } |
@@ -757,13 +786,13 @@ process_arguments (int argc, char **argv) | |||
757 | if (ptr[0] == '\'') | 786 | if (ptr[0] == '\'') |
758 | labels[nlabels - 1] = ptr + 1; | 787 | labels[nlabels - 1] = ptr + 1; |
759 | while (ptr && (ptr = nextarg (ptr))) { | 788 | while (ptr && (ptr = nextarg (ptr))) { |
760 | if (nlabels >= labels_size) { | 789 | nlabels++; |
790 | if (nlabels > labels_size) { | ||
761 | labels_size += 8; | 791 | labels_size += 8; |
762 | labels = realloc (labels, labels_size); | 792 | labels = realloc (labels, labels_size * sizeof(*labels)); |
763 | if (labels == NULL) | 793 | if (labels == NULL) |
764 | die (STATE_UNKNOWN, _("Could not reallocate labels\n")); | 794 | die (STATE_UNKNOWN, _("Could not reallocate labels\n")); |
765 | } | 795 | } |
766 | nlabels++; | ||
767 | ptr = thisarg (ptr); | 796 | ptr = thisarg (ptr); |
768 | if (ptr[0] == '\'') | 797 | if (ptr[0] == '\'') |
769 | labels[nlabels - 1] = ptr + 1; | 798 | labels[nlabels - 1] = ptr + 1; |
@@ -774,9 +803,9 @@ process_arguments (int argc, char **argv) | |||
774 | case 'u': /* units */ | 803 | case 'u': /* units */ |
775 | units = optarg; | 804 | units = optarg; |
776 | nunits++; | 805 | nunits++; |
777 | if (nunits >= unitv_size) { | 806 | if (nunits > unitv_size) { |
778 | unitv_size += 8; | 807 | unitv_size += 8; |
779 | unitv = realloc (unitv, unitv_size); | 808 | unitv = realloc (unitv, unitv_size * sizeof(*unitv)); |
780 | if (unitv == NULL) | 809 | if (unitv == NULL) |
781 | die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), (int)nunits); | 810 | die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), (int)nunits); |
782 | } | 811 | } |
@@ -786,9 +815,9 @@ process_arguments (int argc, char **argv) | |||
786 | if (ptr[0] == '\'') | 815 | if (ptr[0] == '\'') |
787 | unitv[nunits - 1] = ptr + 1; | 816 | unitv[nunits - 1] = ptr + 1; |
788 | while (ptr && (ptr = nextarg (ptr))) { | 817 | while (ptr && (ptr = nextarg (ptr))) { |
789 | if (nunits >= unitv_size) { | 818 | if (nunits > unitv_size) { |
790 | unitv_size += 8; | 819 | unitv_size += 8; |
791 | unitv = realloc (unitv, unitv_size); | 820 | unitv = realloc (unitv, unitv_size * sizeof(*unitv)); |
792 | if (units == NULL) | 821 | if (units == NULL) |
793 | die (STATE_UNKNOWN, _("Could not realloc() units\n")); | 822 | die (STATE_UNKNOWN, _("Could not realloc() units\n")); |
794 | } | 823 | } |
@@ -809,6 +838,9 @@ process_arguments (int argc, char **argv) | |||
809 | if(!is_integer(optarg)||((rate_multiplier=atoi(optarg))<=0)) | 838 | if(!is_integer(optarg)||((rate_multiplier=atoi(optarg))<=0)) |
810 | usage2(_("Rate multiplier must be a positive integer"),optarg); | 839 | usage2(_("Rate multiplier must be a positive integer"),optarg); |
811 | break; | 840 | break; |
841 | case L_OFFSET: | ||
842 | offset=strtod(optarg,NULL); | ||
843 | break; | ||
812 | case L_INVERT_SEARCH: | 844 | case L_INVERT_SEARCH: |
813 | invert_search=1; | 845 | invert_search=1; |
814 | break; | 846 | break; |
@@ -1057,6 +1089,8 @@ print_help (void) | |||
1057 | printf (" %s\n", _("Enable rate calculation. See 'Rate Calculation' below")); | 1089 | printf (" %s\n", _("Enable rate calculation. See 'Rate Calculation' below")); |
1058 | printf (" %s\n", "--rate-multiplier"); | 1090 | printf (" %s\n", "--rate-multiplier"); |
1059 | printf (" %s\n", _("Converts rate per second. For example, set to 60 to convert to per minute")); | 1091 | printf (" %s\n", _("Converts rate per second. For example, set to 60 to convert to per minute")); |
1092 | printf (" %s\n", "--offset=OFFSET"); | ||
1093 | printf (" %s\n", _("Add/substract the specified OFFSET to numeric sensor data")); | ||
1060 | 1094 | ||
1061 | /* Tests Against Strings */ | 1095 | /* Tests Against Strings */ |
1062 | printf (" %s\n", "-s, --string=STRING"); | 1096 | printf (" %s\n", "-s, --string=STRING"); |