Index: plugins/check_snmp.c =================================================================== --- plugins/check_snmp.c (revision 2160) +++ plugins/check_snmp.c (working copy) @@ -34,7 +34,7 @@ #include "common.h" #include "utils.h" -#include "popen.h" +#include "utils_cmd.h" #define DEFAULT_COMMUNITY "public" #define DEFAULT_PORT "161" @@ -91,14 +91,14 @@ regmatch_t pmatch[10]; char timestamp[10] = ""; char errbuf[MAX_INPUT_BUFFER] = ""; -char perfstr[MAX_INPUT_BUFFER] = ""; +char perfstr[MAX_INPUT_BUFFER] = "| "; int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; int eflags = 0; int errcode, excode; char *server_address = NULL; char *community = NULL; -char *authpriv = NULL; +char **authpriv = NULL; char *proto = NULL; char *seclevel = NULL; char *secname = NULL; @@ -106,10 +106,11 @@ char *privproto = NULL; char *authpasswd = NULL; char *privpasswd = NULL; -char *oid; +char **oids = NULL; char *label; char *units; char *port; +char *snmpcmd; char string_value[MAX_INPUT_BUFFER] = ""; char **labels = NULL; char **unitv = NULL; @@ -117,6 +118,8 @@ size_t labels_size = 8; size_t nunits = 0; size_t unitv_size = 8; +int numoids = 0; +int numauthpriv = 0; int verbose = FALSE; int usesnmpgetnext = FALSE; unsigned long long lower_warn_lim[MAX_OIDS]; @@ -139,18 +142,16 @@ { int i = 0; int iresult = STATE_UNKNOWN; - int found = 0; - int result = STATE_DEPENDENT; - char input_buffer[MAX_INPUT_BUFFER]; - char *command_line = NULL; + int result = STATE_UNKNOWN; + char **command_line = NULL; char *cl_hidden_auth = NULL; + char *oidname = NULL; char *response = NULL; char *outbuff; - char *output; char *ptr = NULL; - char *p2 = NULL; char *show = NULL; char type[8] = ""; + output chld_out, chld_err; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); @@ -162,12 +163,10 @@ eval_method[i] = CHECK_UNDEF; i = 0; - oid = strdup (""); label = strdup ("SNMP"); units = strdup (""); port = strdup (DEFAULT_PORT); outbuff = strdup (""); - output = strdup (""); delimiter = strdup (" = "); output_delim = strdup (DEFAULT_OUTPUT_DELIMITER); /* miblist = strdup (DEFAULT_MIBLIST); */ @@ -180,92 +179,71 @@ if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); - /* create the command line to execute */ - if(usesnmpgetnext == TRUE) { - asprintf(&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", - PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto, - authpriv, server_address, port, oid); - asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", - PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto, - "[authpriv]", server_address, port, oid); + /* Create the command array to execute */ + if(usesnmpgetnext == TRUE) { + snmpcmd = strdup (PATH_TO_SNMPGETNEXT); }else{ + snmpcmd = strdup (PATH_TO_SNMPGET); + } + + /* 10 is the number of arguments to pass before authpriv options and OIDs. Add one for terminating NULL */ + command_line = calloc (10 + numauthpriv + numoids + 1, sizeof (char *)); + command_line[0] = snmpcmd; + command_line[1] = strdup ("-t"); + asprintf (&command_line[2], "%d", timeout_interval); + command_line[3] = strdup ("-r"); + asprintf (&command_line[4], "%d", retries); + command_line[5] = strdup ("-m"); + command_line[6] = strdup (miblist); + command_line[7] = "-v"; + command_line[8] = strdup (proto); + asprintf (&command_line[9], "%s:%s", server_address, port); - asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", - PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto, - authpriv, server_address, port, oid); - asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", - PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto, - "[authpriv]", server_address, port, oid); + for (i = 0; i < numauthpriv; i++) { + command_line[10 + numoids + i] = authpriv[i]; } - if (verbose) - printf ("%s\n", command_line); + /* This is just for display purposes, so it can remain a string */ + asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s:%s %s", + snmpcmd, timeout_interval, retries, miblist, proto, + server_address, port, "[authpriv]"); - - /* run the command */ - child_process = spopen (command_line); - if (child_process == NULL) { - printf (_("Could not open pipe: %s\n"), cl_hidden_auth); - exit (STATE_UNKNOWN); + for (i = 0; i < numoids; i++) { + command_line[10 + i] = oids[i]; + asprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); } -#if 0 /* Removed May 29, 2007 */ - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); - if (child_stderr == NULL) { - printf (_("Could not open stderr for %s\n"), cl_hidden_auth); - } -#endif - - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) - asprintf (&output, "%s%s", output, input_buffer); - if (verbose) - printf ("%s\n", output); + printf ("%s\n", cl_hidden_auth); - ptr = output; + /* Run the command */ + result = cmd_run_array (command_line, &chld_out, &chld_err, 0); - strncat(perfstr, "| ", sizeof(perfstr)-strlen(perfstr)-1); - while (ptr) { - char *foo, *ptr2; - unsigned int copylen; + if (chld_err.lines > 0) { + printf ("%s problem - error output from command\nCMD: %s\n", label, cl_hidden_auth); + for (i = 0; i < chld_err.lines; i++) { + printf ("%s\n", chld_err.line[i]); + } + exit (STATE_WARNING); + } - foo = strstr (ptr, delimiter); - copylen = foo-ptr; - if (copylen > sizeof(perfstr)-strlen(perfstr)-1) - copylen = sizeof(perfstr)-strlen(perfstr)-1; - ptr2 = ptr; - ptr = foo; + /* Return UNKNOWN or worse if no output is returned */ + if (chld_out.lines == 0) + die (max_state_alt (result, STATE_UNKNOWN), _("%s problem - No data received from host\nCMD: %s\n"), + label, + cl_hidden_auth); - if (ptr == NULL) - break; - - ptr += strlen (delimiter); - ptr += strspn (ptr, " "); - - found++; - - if (ptr[0] == '"') { - ptr++; - response = strpcpy (response, ptr, "\""); - ptr = strpbrk (ptr, "\""); - ptr += strspn (ptr, "\"\n"); + if (verbose) { + for (i = 0; i < chld_out.lines; i++) { + printf ("%s\n", chld_out.line[i]); } - else { - response = strpcpy (response, ptr, "\n"); - ptr = strpbrk (ptr, "\n"); - ptr += strspn (ptr, "\n"); - while - (strstr (ptr, delimiter) && - strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) { - response = strpcat (response, ptr, "\n"); - ptr = strpbrk (ptr, "\n"); - } - if (ptr && strstr (ptr, delimiter) == NULL) { - asprintf (&response, "%s%s", response, ptr); - ptr = NULL; - } - } + } + for (i = 0; i < chld_out.lines; i++) { + ptr = chld_out.line[i]; + oidname = strpcpy (oidname, ptr, delimiter); + response = strstr (ptr, delimiter); + /* We strip out the datatype indicator for PHBs */ /* Clean up type array - Sol10 does not necessarily zero it out */ @@ -289,7 +267,6 @@ show = strstr (response, "STRING: ") + 8; else show = response; - p2 = show; iresult = STATE_DEPENDENT; @@ -306,10 +283,10 @@ eval_method[i] & WARN_LE || eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) { - p2 = strpbrk (p2, "0123456789"); - if (p2 == NULL) + ptr = strpbrk (show, "0123456789"); + if (ptr == NULL) die (STATE_UNKNOWN,_("No valid data returned")); - response_value[i] = strtoul (p2, NULL, 10); + response_value[i] = strtoul (ptr, NULL, 10); iresult = check_num (i); asprintf (&show, "%llu", response_value[i]); } @@ -364,10 +341,8 @@ if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL) asprintf (&outbuff, "%s %s", outbuff, unitv[i]); - i++; - if (is_numeric(show)) { - strncat(perfstr, ptr2, copylen); + strncat(perfstr, oidname, sizeof(perfstr)-strlen(perfstr)-1); strncat(perfstr, "=", sizeof(perfstr)-strlen(perfstr)-1); strncat(perfstr, show, sizeof(perfstr)-strlen(perfstr)-1); @@ -375,29 +350,6 @@ strncat(perfstr, type, sizeof(perfstr)-strlen(perfstr)-1); strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1); } - - } /* end while (ptr) */ - - if (found == 0) - die (STATE_UNKNOWN, - _("%s problem - No data received from host\nCMD: %s\n"), - label, - cl_hidden_auth); - -#if 0 /* Removed May 29, 2007 */ - /* WARNING if output found on stderr */ - if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) - result = max_state (result, STATE_WARNING); - - /* close stderr */ - (void) fclose (child_stderr); -#endif - - /* close the pipe */ - if (spclose (child_process)) { - if (result == STATE_OK) - result = STATE_UNKNOWN; - asprintf (&outbuff, "%s (%s)", outbuff, _("snmpget returned an error status")); } /* if (nunits == 1 || i == 1) */ @@ -563,12 +515,12 @@ */ needmibs = TRUE; } - - for (ptr = optarg; (ptr = index (ptr, ',')); ptr++) - ptr[0] = ' '; /* relpace comma with space */ - for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++) - j++; /* count OIDs */ - asprintf (&oid, "%s %s", (oid?oid:""), optarg); + oids = calloc(MAX_OIDS, sizeof (char *)); + for (ptr = strtok(optarg, ", "); ptr != NULL; ptr = strtok(NULL, ", ")) { + oids[j] = strdup(ptr); + j++; + } + numoids = j; if (c == 'E' || c == 'e') { jj++; ii++; @@ -728,16 +680,25 @@ if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */ asprintf(&proto, DEFAULT_PROTOCOL); - asprintf(&authpriv, "%s%s", "-c ", community); + numauthpriv = 2; + authpriv = calloc (numauthpriv, sizeof (char *)); + authpriv[0] = strdup ("-c"); + authpriv[1] = strdup (community); } else if ( strcmp (proto, "2c") == 0 ) { /* snmpv2c args */ - asprintf(&authpriv, "%s%s", "-c ", community); + numauthpriv = 2; + authpriv = calloc (numauthpriv, sizeof (char *)); + authpriv[0] = strdup ("-c"); + authpriv[1] = strdup (community); } else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */ asprintf(&proto, "%s", "3"); if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) { - asprintf(&authpriv, "%s", "-l noAuthNoPriv" ); + numauthpriv = 2; + authpriv = calloc (numauthpriv, sizeof (char *)); + authpriv[0] = strdup ("-l"); + authpriv[1] = strdup ("noAuthNoPriv"); } else if ( strcmp(seclevel, "authNoPriv") == 0 ) { if ( secname == NULL || authpasswd == NULL) { @@ -745,7 +706,16 @@ print_usage (); exit (STATE_UNKNOWN); } - asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd); + numauthpriv = 8; + authpriv = calloc (numauthpriv, sizeof (char *)); + authpriv[0] = strdup ("-l"); + authpriv[1] = strdup ("authNoPriv"); + authpriv[3] = strdup ("-a"); + authpriv[4] = strdup (authproto); + authpriv[5] = strdup ("-u"); + authpriv[6] = strdup (secname); + authpriv[7] = strdup ("-A"); + authpriv[8] = strdup (authpasswd); } else if ( strcmp(seclevel, "authPriv") == 0 ) { if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) { @@ -753,7 +723,20 @@ print_usage (); exit (STATE_UNKNOWN); } - asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x %s -X %s ", authproto, secname, authpasswd, privproto, privpasswd); + numauthpriv = 12; + authpriv = calloc (numauthpriv, sizeof (char *)); + authpriv[0] = strdup ("-l"); + authpriv[1] = strdup ("authPriv"); + authpriv[3] = strdup ("-a"); + authpriv[4] = strdup (authproto); + authpriv[5] = strdup ("-u"); + authpriv[6] = strdup (secname); + authpriv[7] = strdup ("-A"); + authpriv[8] = strdup (authpasswd); + authpriv[9] = strdup ("-x"); + authpriv[10] = strdup (privproto); + authpriv[11] = strdup ("-X"); + authpriv[12] = strdup (privpasswd); } } Index: plugins/Makefile.am =================================================================== --- plugins/Makefile.am (revision 2160) +++ plugins/Makefile.am (working copy) @@ -93,7 +93,7 @@ check_procs_LDADD = $(BASEOBJS) check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) check_real_LDADD = $(NETLIBS) -check_snmp_LDADD = $(BASEOBJS) popen.o +check_snmp_LDADD = $(BASEOBJS) check_smtp_LDADD = $(SSLOBJS) $(NETLIBS) $(SSLLIBS) check_ssh_LDADD = $(NETLIBS) check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) popen.o @@ -135,7 +135,7 @@ check_procs_DEPENDENCIES = check_procs.c $(BASEOBJS) popen.o $(DEPLIBS) check_radius_DEPENDENCIES = check_radius.c $(NETOBJS) $(DEPLIBS) check_real_DEPENDENCIES = check_real.c $(NETOBJS) $(DEPLIBS) -check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) popen.o $(DEPLIBS) +check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) $(DEPLIBS) check_smtp_DEPENDENCIES = check_smtp.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS) check_ssh_DEPENDENCIES = check_ssh.c $(NETOBJS) $(DEPLIBS) check_swap_DEPENDENCIES = check_swap.c $(BASEOBJS) popen.o $(DEPLIBS)