diff options
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | lib/utils_cmd.c | 18 | ||||
-rw-r--r-- | lib/utils_cmd.h | 1 | ||||
-rw-r--r-- | plugins/Makefile.am | 2 | ||||
-rw-r--r-- | plugins/check_procs.c | 61 |
5 files changed, 36 insertions, 47 deletions
@@ -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 | ||
10 | 1.4.12 27th May 2008 | 11 | 1.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 aaf055a9..5ba2f55e 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 | |||
382 | int | ||
383 | cmd_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 d54b2b4d..8ebb5894 100644 --- a/lib/utils_cmd.h +++ b/lib/utils_cmd.h | |||
@@ -22,6 +22,7 @@ typedef struct output output; | |||
22 | /** prototypes **/ | 22 | /** prototypes **/ |
23 | int cmd_run (const char *, output *, output *, int); | 23 | int cmd_run (const char *, output *, output *, int); |
24 | int cmd_run_array (char *const *, output *, output *, int); | 24 | int cmd_run_array (char *const *, output *, output *, int); |
25 | int 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 */ |
27 | void cmd_init (void); | 28 | void cmd_init (void); |
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 67c49ca9..85e4a5dd 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
@@ -79,7 +79,7 @@ check_nwstat_LDADD = $(NETLIBS) | |||
79 | check_overcr_LDADD = $(NETLIBS) | 79 | check_overcr_LDADD = $(NETLIBS) |
80 | check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) | 80 | check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) |
81 | check_ping_LDADD = $(NETLIBS) popen.o | 81 | check_ping_LDADD = $(NETLIBS) popen.o |
82 | check_procs_LDADD = $(BASEOBJS) popen.o | 82 | check_procs_LDADD = $(BASEOBJS) |
83 | check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) | 83 | check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) |
84 | check_real_LDADD = $(NETLIBS) | 84 | check_real_LDADD = $(NETLIBS) |
85 | check_snmp_LDADD = $(BASEOBJS) popen.o | 85 | check_snmp_LDADD = $(BASEOBJS) popen.o |
diff --git a/plugins/check_procs.c b/plugins/check_procs.c index 875f867f..4aaeddb2 100644 --- a/plugins/check_procs.c +++ b/plugins/check_procs.c | |||
@@ -41,8 +41,8 @@ const char *copyright = "2000-2008"; | |||
41 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | 41 | const 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 ) |