[Nagios-devel] spopen and stderr

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
Guest

[Nagios-devel] spopen and stderr

Post by Guest »

Hi,
I'm trying to write a plugin and having some trouble w/ spopen. I seems to
be reporting stdout fine, but stderr seems to be lost. My code looks like:

/* open the child process */
child_process = spopen (cmd);
if (child_process == NULL) {
printf ("Could not open pipe: %s\n", cmd);
exit (STATE_UNKNOWN);
}

/* open the child process stderr */
child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
if (child_stderr == NULL) {
printf ("Could not open stderr for %s\n", cmd);
}

/* get results from remote command */
while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
asprintf (&result_text, "%s%s", result_text, input_buffer);

/* WARNING if output found on stderr */
if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
printf ("STDERR: %s\n", input_buffer);
return STATE_WARNING;
}

/* close child stderr */
(void) fclose (child_stderr);

/* close child */
result = spclose (child_process);

cmd contains the correct command to run. When I run it from the command
line, there's a bunch of output to stderr, such that:
$ cmd_myprog > /tmp/out 2> /tmp/err
^C
$ wc -l /tmp/err /tmp/out
4 /tmp/err
0 /tmp/out
4 total
$

So, stderr is getting the output, but stdout gets nothing. When I run this
from my plugin using spopen, I get:
$ ./check_myprog
CRITICAL - Plugin timed out after 15 seconds
$

What I don't understand is, shouldn't the code above output the information
from STDERR of the spopen'ed program? In particular, shouldn't
child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
printf ("STDERR: %s\n", input_buffer);
return STATE_WARNING;
}
print out the same lines of code that went into /tmp/err in my commandline
execution? Or have I misunderstood the purpose of spopen()?

thanks
dave







This post was automatically imported from historical nagios-devel mailing list archives
Original poster: [email protected]
Locked