------=_20061024150818_29082
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
Hi All,
Like everyone else, we've had our fair share of mysterious plugin "(No
output!)" messages.
Sometimes this error is due to the plugin failing and only writing
diagnostics to stderr (and nothing is written to stdout.)
Also, as everyone knows, Nagios only reads the first line of
newline-terminated output from a plugin and throws the rest away. But,
what if the first line is just a new-line and the good stuff is on a
subsequent line?
Yes, I know - fix your plugin to output only to the first line.
But being a lazy programmer, I am thinking, why not just have Nagios be a
little more forgiving and inquisitive and keep searching stdout until it
finds the first non-empty line? Is that so bad? Or is it a feature?
Well, you can be the judge.
Anyway, I put together this patch for checks.c - it modifies the plugin
output handling logic in the following manner:
1. As usual, it reads plugin output from stdout.
However, if the first line is empty, it keeps reading until it gets a
non-empty line or EOF.
If it gets a non-empty line, then that first non-empty line becomes the
plugin output.
2. However, if it really gets no output from stdout (i.e., nothing or all
empty lines,) then it reads the plugin's stderr and returns the first
non-empty line it finds.
3. If it gets nothing from stderr as well, then it finally returns
everyone's favorite diagnostic: "(No output!)"
You know, we should really change that diagnostic to: "(No output! Have a
nice day!)"
Anyway, I'd be very interested in any alternate suggestions, good
comments, insightful observations or even witty reparte'.
BTW, in order to provide the ability to read both stdout and stderr from a
plugin sub-process, I've written my own version of the standard C popen(3)
function called pfopen(). I did this because this is the problem with the
standard popen(3) function - it only return stdout to the parent process,
which may only give you half the story since it ignores all potentially
usefull diagnostic info from the stderr of the child process.
If you use the patch, then pfopen.c should be added to the "base"
directory and pfopen.h should be added to the "include" directory. Both
patch files (for "Makefile.in" and "checks.c",) are applied to those file
in the "base" directory.
Regards,
Bob Ingraham
------=_20061024150818_29082
Content-Type: text/plain; name="checks.c.diff"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="checks.c.diff"
--- checks.c-orig 2006-06-20 18:59:19.000000000 -0600
+++ checks.c 2006-09-06 10:18:22.000000000 -0600
@@ -29,6 +29,7 @@
#include "../include/nagios.h"
#include "../include/broker.h"
#include "../include/perfdata.h"
+#include "../include/pfopen.h"
/*#define DEBUG_CHECKS*/
@@ -110,6 +111,7 @@
int wait_result=0;
host *temp_host=NULL;
FILE *fp;
+ PFILE *pf;
int pclose_result=0;
int time_is_valid=TRUE;
#ifdef EMBEDDEDPERL
@@ -481,23 +483,42 @@
/* run the plugin check command */
- fp=popen(processed_command,"r");
- if(fp==NULL)
+ pf=pfopen(processed_command,"r");
+ if(pf==NULL)
_exit(STATE_UNKNOWN);
+#if 0
/* default return string in case nothing was returned */
strcpy(plugin_output,"(No output!)");
/* grab the plugin output and clean it */
- fgets(plugin_output,sizeof(plugin_output)-1,fp);
+ fgets(plugin_output,sizeof(plugin_output)-1,PF_OUT(pf));
strip(plugin_output);
/* ADDED 01/04/2004 */
/* ignore any additional lines of output */
- while(fgets(temp_buffer,sizeof(temp_buffer)-1,fp));
+ while(fgets(temp_buffer,sizeof(temp_buffer)-1,PF_OUT(pf)));
+#else
+ // Initialize plugin output buffer
+ plugin_output[0] = '\0';
+
+ // Consume plugin's stdout
+ while (!plugin_output[0] && fgets(plugin_output,sizeof(plugin_output)-1,PF_OUT(pf)) != NULL)
+ strip(plugin_output);
+ while(fgets(temp_buffer,sizeof(temp_buffer)-1,PF_OUT(pf)));
+
+ // If no plugin output from stdout, then try stderr
+ while (!plugin_output[0] && fgets(plugin_output,sizeof(plugin_output)-
...[email truncated]...
This post was automatically imported from historical nagios-devel mailing list archives
Original poster: [email protected]