diff options
Diffstat (limited to 'plugins/check_snmp.c')
-rw-r--r-- | plugins/check_snmp.c | 94 |
1 files changed, 61 insertions, 33 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c index d3968a27..56a586ad 100644 --- a/plugins/check_snmp.c +++ b/plugins/check_snmp.c | |||
@@ -46,6 +46,7 @@ const char *email = "devel@monitoring-plugins.org"; | |||
46 | #define DEFAULT_PRIV_PROTOCOL "DES" | 46 | #define DEFAULT_PRIV_PROTOCOL "DES" |
47 | #define DEFAULT_DELIMITER "=" | 47 | #define DEFAULT_DELIMITER "=" |
48 | #define DEFAULT_OUTPUT_DELIMITER " " | 48 | #define DEFAULT_OUTPUT_DELIMITER " " |
49 | #define DEFAULT_BUFFER_SIZE 100 | ||
49 | 50 | ||
50 | #define mark(a) ((a)!=0?"*":"") | 51 | #define mark(a) ((a)!=0?"*":"") |
51 | 52 | ||
@@ -64,6 +65,7 @@ const char *email = "devel@monitoring-plugins.org"; | |||
64 | #define L_RATE_MULTIPLIER CHAR_MAX+2 | 65 | #define L_RATE_MULTIPLIER CHAR_MAX+2 |
65 | #define L_INVERT_SEARCH CHAR_MAX+3 | 66 | #define L_INVERT_SEARCH CHAR_MAX+3 |
66 | #define L_OFFSET CHAR_MAX+4 | 67 | #define L_OFFSET CHAR_MAX+4 |
68 | #define L_IGNORE_MIB_PARSING_ERRORS CHAR_MAX+5 | ||
67 | 69 | ||
68 | /* Gobble to string - stop incrementing c when c[0] match one of the | 70 | /* Gobble to string - stop incrementing c when c[0] match one of the |
69 | * characters in s */ | 71 | * characters in s */ |
@@ -157,6 +159,9 @@ int perf_labels = 1; | |||
157 | char* ip_version = ""; | 159 | char* ip_version = ""; |
158 | double multiplier = 1.0; | 160 | double multiplier = 1.0; |
159 | char *fmtstr = ""; | 161 | char *fmtstr = ""; |
162 | bool fmtstr_set = false; | ||
163 | char buffer[DEFAULT_BUFFER_SIZE]; | ||
164 | bool ignore_mib_parsing_errors = false; | ||
160 | 165 | ||
161 | static char *fix_snmp_range(char *th) | 166 | static char *fix_snmp_range(char *th) |
162 | { | 167 | { |
@@ -304,42 +309,55 @@ main (int argc, char **argv) | |||
304 | } | 309 | } |
305 | 310 | ||
306 | /* 10 arguments to pass before context and authpriv options + 1 for host and numoids. Add one for terminating NULL */ | 311 | /* 10 arguments to pass before context and authpriv options + 1 for host and numoids. Add one for terminating NULL */ |
307 | command_line = calloc (10 + numcontext + numauthpriv + 1 + numoids + 1, sizeof (char *)); | 312 | |
308 | command_line[0] = snmpcmd; | 313 | unsigned index = 0; |
309 | command_line[1] = strdup ("-Le"); | 314 | command_line = calloc (11 + numcontext + numauthpriv + 1 + numoids + 1, sizeof (char *)); |
310 | command_line[2] = strdup ("-t"); | 315 | |
311 | xasprintf (&command_line[3], "%d", timeout_interval); | 316 | command_line[index++] = snmpcmd; |
312 | command_line[4] = strdup ("-r"); | 317 | command_line[index++] = strdup ("-Le"); |
313 | xasprintf (&command_line[5], "%d", retries); | 318 | command_line[index++] = strdup ("-t"); |
314 | command_line[6] = strdup ("-m"); | 319 | xasprintf (&command_line[index++], "%d", timeout_interval); |
315 | command_line[7] = strdup (miblist); | 320 | command_line[index++] = strdup ("-r"); |
316 | command_line[8] = "-v"; | 321 | xasprintf (&command_line[index++], "%d", retries); |
317 | command_line[9] = strdup (proto); | 322 | command_line[index++] = strdup ("-m"); |
323 | command_line[index++] = strdup (miblist); | ||
324 | command_line[index++] = "-v"; | ||
325 | command_line[index++] = strdup (proto); | ||
326 | |||
327 | xasprintf(&cl_hidden_auth, "%s -Le -t %d -r %d -m %s -v %s", | ||
328 | snmpcmd, timeout_interval, retries, strlen(miblist) ? miblist : "''", proto); | ||
329 | |||
330 | if (ignore_mib_parsing_errors) { | ||
331 | command_line[index++] = "-Pe"; | ||
332 | xasprintf(&cl_hidden_auth, "%s -Pe", cl_hidden_auth); | ||
333 | } | ||
334 | |||
318 | 335 | ||
319 | for (i = 0; i < numcontext; i++) { | 336 | for (i = 0; i < numcontext; i++) { |
320 | command_line[10 + i] = contextargs[i]; | 337 | command_line[index++] = contextargs[i]; |
321 | } | 338 | } |
322 | 339 | ||
323 | for (i = 0; i < numauthpriv; i++) { | 340 | for (i = 0; i < numauthpriv; i++) { |
324 | command_line[10 + numcontext + i] = authpriv[i]; | 341 | command_line[index++] = authpriv[i]; |
325 | } | 342 | } |
326 | 343 | ||
327 | xasprintf (&command_line[10 + numcontext + numauthpriv], "%s:%s", server_address, port); | 344 | xasprintf (&command_line[index++], "%s:%s", server_address, port); |
328 | 345 | ||
329 | /* This is just for display purposes, so it can remain a string */ | 346 | xasprintf(&cl_hidden_auth, "%s [context] [authpriv] %s:%s", |
330 | xasprintf(&cl_hidden_auth, "%s -Le -t %d -r %d -m %s -v %s %s %s %s:%s", | 347 | cl_hidden_auth, |
331 | snmpcmd, timeout_interval, retries, strlen(miblist) ? miblist : "''", proto, "[context]", "[authpriv]", | 348 | server_address, |
332 | server_address, port); | 349 | port); |
333 | 350 | ||
334 | for (i = 0; i < numoids; i++) { | 351 | for (i = 0; i < numoids; i++) { |
335 | command_line[10 + numcontext + numauthpriv + 1 + i] = oids[i]; | 352 | command_line[index++] = oids[i]; |
336 | xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); | 353 | xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); |
337 | } | 354 | } |
338 | 355 | ||
339 | command_line[10 + numcontext + numauthpriv + 1 + numoids] = NULL; | 356 | command_line[index++] = NULL; |
340 | 357 | ||
341 | if (verbose) | 358 | if (verbose) { |
342 | printf ("%s\n", cl_hidden_auth); | 359 | printf ("%s\n", cl_hidden_auth); |
360 | } | ||
343 | 361 | ||
344 | /* Set signal handling and alarm */ | 362 | /* Set signal handling and alarm */ |
345 | if (signal (SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR) { | 363 | if (signal (SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR) { |
@@ -420,7 +438,8 @@ main (int argc, char **argv) | |||
420 | } | 438 | } |
421 | else if (strstr (response, "INTEGER: ")) { | 439 | else if (strstr (response, "INTEGER: ")) { |
422 | show = multiply (strstr (response, "INTEGER: ") + 9); | 440 | show = multiply (strstr (response, "INTEGER: ") + 9); |
423 | if (fmtstr != "") { | 441 | |
442 | if (fmtstr_set) { | ||
424 | conv = fmtstr; | 443 | conv = fmtstr; |
425 | } | 444 | } |
426 | } | 445 | } |
@@ -594,8 +613,9 @@ main (int argc, char **argv) | |||
594 | len = sizeof(perfstr)-strlen(perfstr)-1; | 613 | len = sizeof(perfstr)-strlen(perfstr)-1; |
595 | strncat(perfstr, show, len>ptr-show ? ptr-show : len); | 614 | strncat(perfstr, show, len>ptr-show ? ptr-show : len); |
596 | 615 | ||
597 | if (type) | 616 | if (strcmp(type, "") != 0) { |
598 | strncat(perfstr, type, sizeof(perfstr)-strlen(perfstr)-1); | 617 | strncat(perfstr, type, sizeof(perfstr)-strlen(perfstr)-1); |
618 | } | ||
599 | 619 | ||
600 | if (warning_thresholds) { | 620 | if (warning_thresholds) { |
601 | strncat(perfstr, ";", sizeof(perfstr)-strlen(perfstr)-1); | 621 | strncat(perfstr, ";", sizeof(perfstr)-strlen(perfstr)-1); |
@@ -706,6 +726,7 @@ process_arguments (int argc, char **argv) | |||
706 | {"ipv6", no_argument, 0, '6'}, | 726 | {"ipv6", no_argument, 0, '6'}, |
707 | {"multiplier", required_argument, 0, 'M'}, | 727 | {"multiplier", required_argument, 0, 'M'}, |
708 | {"fmtstr", required_argument, 0, 'f'}, | 728 | {"fmtstr", required_argument, 0, 'f'}, |
729 | {"ignore-mib-parsing-errors", no_argument, false, L_IGNORE_MIB_PARSING_ERRORS}, | ||
709 | {0, 0, 0, 0} | 730 | {0, 0, 0, 0} |
710 | }; | 731 | }; |
711 | 732 | ||
@@ -853,6 +874,7 @@ process_arguments (int argc, char **argv) | |||
853 | break; | 874 | break; |
854 | case 'R': /* regex */ | 875 | case 'R': /* regex */ |
855 | cflags = REG_ICASE; | 876 | cflags = REG_ICASE; |
877 | // fall through | ||
856 | case 'r': /* regex */ | 878 | case 'r': /* regex */ |
857 | cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | 879 | cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; |
858 | strncpy (regex_expect, optarg, sizeof (regex_expect) - 1); | 880 | strncpy (regex_expect, optarg, sizeof (regex_expect) - 1); |
@@ -969,8 +991,11 @@ process_arguments (int argc, char **argv) | |||
969 | case 'f': | 991 | case 'f': |
970 | if (multiplier != 1.0) { | 992 | if (multiplier != 1.0) { |
971 | fmtstr=optarg; | 993 | fmtstr=optarg; |
994 | fmtstr_set = true; | ||
972 | } | 995 | } |
973 | break; | 996 | break; |
997 | case L_IGNORE_MIB_PARSING_ERRORS: | ||
998 | ignore_mib_parsing_errors = true; | ||
974 | } | 999 | } |
975 | } | 1000 | } |
976 | 1001 | ||
@@ -1169,33 +1194,33 @@ multiply (char *str) | |||
1169 | double val; | 1194 | double val; |
1170 | char *conv = "%f"; | 1195 | char *conv = "%f"; |
1171 | 1196 | ||
1197 | if(multiplier == 1) | ||
1198 | return(str); | ||
1199 | |||
1172 | if(verbose>2) | 1200 | if(verbose>2) |
1173 | printf(" multiply input: %s\n", str); | 1201 | printf(" multiply input: %s\n", str); |
1174 | 1202 | ||
1175 | val = strtod (str, &endptr); | 1203 | val = strtod (str, &endptr); |
1176 | if ((val == 0.0) && (endptr == str)) { | 1204 | if ((val == 0.0) && (endptr == str)) { |
1177 | if(multiplier != 1) { | 1205 | die(STATE_UNKNOWN, _("multiplier set (%.1f), but input is not a number: %s"), multiplier, str); |
1178 | die(STATE_UNKNOWN, _("multiplier set (%.1f), but input is not a number: %s"), multiplier, str); | ||
1179 | } | ||
1180 | return str; | ||
1181 | } | 1206 | } |
1182 | 1207 | ||
1183 | if(verbose>2) | 1208 | if(verbose>2) |
1184 | printf(" multiply extracted double: %f\n", val); | 1209 | printf(" multiply extracted double: %f\n", val); |
1185 | val *= multiplier; | 1210 | val *= multiplier; |
1186 | if (fmtstr != "") { | 1211 | if (fmtstr_set) { |
1187 | conv = fmtstr; | 1212 | conv = fmtstr; |
1188 | } | 1213 | } |
1189 | if (val == (int)val) { | 1214 | if (val == (int)val) { |
1190 | sprintf(str, "%.0f", val); | 1215 | snprintf(buffer, DEFAULT_BUFFER_SIZE, "%.0f", val); |
1191 | } else { | 1216 | } else { |
1192 | if(verbose>2) | 1217 | if(verbose>2) |
1193 | printf(" multiply using format: %s\n", conv); | 1218 | printf(" multiply using format: %s\n", conv); |
1194 | sprintf(str, conv, val); | 1219 | snprintf(buffer, DEFAULT_BUFFER_SIZE, conv, val); |
1195 | } | 1220 | } |
1196 | if(verbose>2) | 1221 | if(verbose>2) |
1197 | printf(" multiply result: %s\n", str); | 1222 | printf(" multiply result: %s\n", buffer); |
1198 | return str; | 1223 | return buffer; |
1199 | } | 1224 | } |
1200 | 1225 | ||
1201 | 1226 | ||
@@ -1272,7 +1297,7 @@ print_help (void) | |||
1272 | printf (" %s\n", "--rate-multiplier"); | 1297 | printf (" %s\n", "--rate-multiplier"); |
1273 | printf (" %s\n", _("Converts rate per second. For example, set to 60 to convert to per minute")); | 1298 | printf (" %s\n", _("Converts rate per second. For example, set to 60 to convert to per minute")); |
1274 | printf (" %s\n", "--offset=OFFSET"); | 1299 | printf (" %s\n", "--offset=OFFSET"); |
1275 | printf (" %s\n", _("Add/substract the specified OFFSET to numeric sensor data")); | 1300 | printf (" %s\n", _("Add/subtract the specified OFFSET to numeric sensor data")); |
1276 | 1301 | ||
1277 | /* Tests Against Strings */ | 1302 | /* Tests Against Strings */ |
1278 | printf (" %s\n", "-s, --string=STRING"); | 1303 | printf (" %s\n", "-s, --string=STRING"); |
@@ -1304,6 +1329,9 @@ print_help (void) | |||
1304 | printf (" %s\n", "-O, --perf-oids"); | 1329 | printf (" %s\n", "-O, --perf-oids"); |
1305 | printf (" %s\n", _("Label performance data with OIDs instead of --label's")); | 1330 | printf (" %s\n", _("Label performance data with OIDs instead of --label's")); |
1306 | 1331 | ||
1332 | printf (" %s\n", "--ignore-mib-parsing-errors"); | ||
1333 | printf (" %s\n", _("Tell snmpget to not print errors encountered when parsing MIB files")); | ||
1334 | |||
1307 | printf (UT_VERBOSE); | 1335 | printf (UT_VERBOSE); |
1308 | 1336 | ||
1309 | printf ("\n"); | 1337 | printf ("\n"); |