--- 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)-1,PF_ERR(pf)) != NULL)
+				strip(plugin_output);
+			while(fgets(temp_buffer,sizeof(temp_buffer)-1,PF_OUT(pf)));
+
+			// If still nothing from either stdout or stderr, then set default return string
+			if (!plugin_output[0])
+				strcpy(plugin_output,"(No output!)");
+#endif
 
 			/* close the process */
-			pclose_result=pclose(fp);
+			pclose_result=pfclose(pf);
 
 			/* reset the alarm */
 			alarm(0);