summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2008-07-08 09:31:04 (GMT)
committerTon Voon <tonvoon@users.sourceforge.net>2008-07-08 09:31:04 (GMT)
commitb93562e6e235eabbe620dc969702bb381d3a0a07 (patch)
treeced6323b0cb4f863b30a33cfd1dbe029b588efec
parent830f3cc38a0b7ddcb80dc322f7f60f7f73ac9bfe (diff)
downloadmonitoring-plugins-b93562e6e235eabbe620dc969702bb381d3a0a07.tar.gz
check_procs captures stderr and adds to plugin output. This is
from a merge of branch nagiosplug/branches/new_threshold_syntax 1979:1982 git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2019 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r--NEWS1
-rw-r--r--lib/utils_cmd.c18
-rw-r--r--lib/utils_cmd.h1
-rw-r--r--plugins/Makefile.am2
-rw-r--r--plugins/check_procs.c61
5 files changed, 36 insertions, 47 deletions
diff --git a/NEWS b/NEWS
index 8185b55..6422152 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,7 @@ This file documents the major additions and syntax changes between releases.
6 Updated Nagios::Plugin to 0.27 6 Updated Nagios::Plugin to 0.27
7 Fix Debian bug #479013: check_dig's -l is mandatory now (sf.net #1986306) 7 Fix Debian bug #479013: check_dig's -l is mandatory now (sf.net #1986306)
8 check_dig now returns CRITICAL instead of WARNING when no answer section is found 8 check_dig now returns CRITICAL instead of WARNING when no answer section is found
9 check_procs now captures stderr in external command and adds to plugin output
9 10
101.4.12 27th May 2008 111.4.12 27th May 2008
11 Added ./check_nt -v INSTANCES to count number of instances (Alessandro Ren) 12 Added ./check_nt -v INSTANCES to count number of instances (Alessandro Ren)
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c
index aaf055a..5ba2f55 100644
--- a/lib/utils_cmd.c
+++ b/lib/utils_cmd.c
@@ -45,6 +45,7 @@
45#include "common.h" 45#include "common.h"
46#include "utils_cmd.h" 46#include "utils_cmd.h"
47#include "utils_base.h" 47#include "utils_base.h"
48#include <fcntl.h>
48 49
49#ifdef HAVE_SYS_WAIT_H 50#ifdef HAVE_SYS_WAIT_H
50# include <sys/wait.h> 51# include <sys/wait.h>
@@ -377,3 +378,20 @@ cmd_run_array (char *const *argv, output * out, output * err, int flags)
377 378
378 return _cmd_close (fd); 379 return _cmd_close (fd);
379} 380}
381
382int
383cmd_file_read ( char *filename, output *out, int flags)
384{
385 int fd;
386 if(out)
387 memset (out, 0, sizeof(output));
388
389 if ((fd = open(filename, O_RDONLY)) == -1) {
390 die( STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno) );
391 }
392
393 if(out)
394 out->lines = _cmd_fetch_output (fd, out, flags);
395
396 return 0;
397}
diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h
index d54b2b4..8ebb589 100644
--- a/lib/utils_cmd.h
+++ b/lib/utils_cmd.h
@@ -22,6 +22,7 @@ typedef struct output output;
22/** prototypes **/ 22/** prototypes **/
23int cmd_run (const char *, output *, output *, int); 23int cmd_run (const char *, output *, output *, int);
24int cmd_run_array (char *const *, output *, output *, int); 24int cmd_run_array (char *const *, output *, output *, int);
25int cmd_file_read (char *, output *, int);
25 26
26/* only multi-threaded plugins need to bother with this */ 27/* only multi-threaded plugins need to bother with this */
27void cmd_init (void); 28void cmd_init (void);
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 67c49ca..85e4a5d 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -79,7 +79,7 @@ check_nwstat_LDADD = $(NETLIBS)
79check_overcr_LDADD = $(NETLIBS) 79check_overcr_LDADD = $(NETLIBS)
80check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) 80check_pgsql_LDADD = $(NETLIBS) $(PGLIBS)
81check_ping_LDADD = $(NETLIBS) popen.o 81check_ping_LDADD = $(NETLIBS) popen.o
82check_procs_LDADD = $(BASEOBJS) popen.o 82check_procs_LDADD = $(BASEOBJS)
83check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) 83check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS)
84check_real_LDADD = $(NETLIBS) 84check_real_LDADD = $(NETLIBS)
85check_snmp_LDADD = $(BASEOBJS) popen.o 85check_snmp_LDADD = $(BASEOBJS) popen.o
diff --git a/plugins/check_procs.c b/plugins/check_procs.c
index 875f867..4aaeddb 100644
--- a/plugins/check_procs.c
+++ b/plugins/check_procs.c
@@ -41,8 +41,8 @@ const char *copyright = "2000-2008";
41const char *email = "nagiosplug-devel@lists.sourceforge.net"; 41const char *email = "nagiosplug-devel@lists.sourceforge.net";
42 42
43#include "common.h" 43#include "common.h"
44#include "popen.h"
45#include "utils.h" 44#include "utils.h"
45#include "utils_cmd.h"
46#include "regex.h" 46#include "regex.h"
47 47
48#include <pwd.h> 48#include <pwd.h>
@@ -129,8 +129,9 @@ main (int argc, char **argv)
129 int expected_cols = PS_COLS - 1; 129 int expected_cols = PS_COLS - 1;
130 int warn = 0; /* number of processes in warn state */ 130 int warn = 0; /* number of processes in warn state */
131 int crit = 0; /* number of processes in crit state */ 131 int crit = 0; /* number of processes in crit state */
132 int i = 0; 132 int i = 0, j = 0;
133 int result = STATE_UNKNOWN; 133 int result = STATE_UNKNOWN;
134 output chld_out, chld_err;
134 135
135 setlocale (LC_ALL, ""); 136 setlocale (LC_ALL, "");
136 bindtextdomain (PACKAGE, LOCALEDIR); 137 bindtextdomain (PACKAGE, LOCALEDIR);
@@ -153,41 +154,27 @@ main (int argc, char **argv)
153 mypid = getpid(); 154 mypid = getpid();
154 155
155 /* Set signal handling and alarm timeout */ 156 /* Set signal handling and alarm timeout */
156 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) { 157 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
157 usage4 (_("Cannot catch SIGALRM")); 158 die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
158 } 159 }
159 alarm (timeout_interval); 160 (void) alarm ((unsigned) timeout_interval);
160 161
161 if (verbose >= 2) 162 if (verbose >= 2)
162 printf (_("CMD: %s\n"), PS_COMMAND); 163 printf (_("CMD: %s\n"), PS_COMMAND);
163 164
164 if (input_filename == NULL) { 165 if (input_filename == NULL) {
165 ps_input = spopen (PS_COMMAND); 166 result = cmd_run( PS_COMMAND, &chld_out, &chld_err, 0);
166 if (ps_input == NULL) { 167 if (chld_err.lines > 0) {
167 printf (_("Could not open pipe: %s\n"), PS_COMMAND); 168 printf ("%s: %s", _("System call sent warnings to stderr"), chld_err.line[0]);
168 return STATE_UNKNOWN; 169 exit(STATE_WARNING);
169 } 170 }
170 child_stderr = fdopen (child_stderr_array[fileno (ps_input)], "r");
171 if (child_stderr == NULL)
172 printf (_("Could not open stderr for %s\n"), PS_COMMAND);
173 } else { 171 } else {
174 ps_input = fopen(input_filename, "r"); 172 result = cmd_file_read( input_filename, &chld_out, 0);
175 if (ps_input == NULL) {
176 die( STATE_UNKNOWN, _("Error opening %s\n"), input_filename );
177 }
178 } 173 }
179 174
180 /* flush first line */ 175 /* flush first line: j starts at 1 */
181 fgets (input_buffer, MAX_INPUT_BUFFER - 1, ps_input); 176 for (j = 1; j < chld_out.lines; j++) {
182 while ( input_buffer[strlen(input_buffer)-1] != '\n' ) 177 input_line = chld_out.line[j];
183 fgets (input_buffer, MAX_INPUT_BUFFER - 1, ps_input);
184
185 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, ps_input)) {
186 asprintf (&input_line, "%s", input_buffer);
187 while ( input_buffer[strlen(input_buffer)-1] != '\n' ) {
188 fgets (input_buffer, MAX_INPUT_BUFFER - 1, ps_input);
189 asprintf (&input_line, "%s%s", input_line, input_buffer);
190 }
191 178
192 if (verbose >= 3) 179 if (verbose >= 3)
193 printf ("%s", input_line); 180 printf ("%s", input_line);
@@ -283,27 +270,9 @@ main (int argc, char **argv)
283 } 270 }
284 } 271 }
285 272
286 /* If we get anything on STDERR, at least set warning */
287 if (input_filename == NULL) {
288 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
289 if (verbose)
290 printf ("STDERR: %s", input_buffer);
291 result = max_state (result, STATE_WARNING);
292 printf (_("System call sent warnings to stderr\n"));
293 }
294
295 (void) fclose (child_stderr);
296
297 /* close the pipe */
298 if (spclose (ps_input)) {
299 printf (_("System call returned nonzero status\n"));
300 result = max_state (result, STATE_WARNING);
301 }
302 }
303
304 if (found == 0) { /* no process lines parsed so return STATE_UNKNOWN */ 273 if (found == 0) { /* no process lines parsed so return STATE_UNKNOWN */
305 printf (_("Unable to read output\n")); 274 printf (_("Unable to read output\n"));
306 return result; 275 return STATE_UNKNOWN;
307 } 276 }
308 277
309 if ( result == STATE_UNKNOWN ) 278 if ( result == STATE_UNKNOWN )