diff options
-rw-r--r-- | lib/utils_cmd.c | 18 | ||||
-rw-r--r-- | lib/utils_cmd.h | 1 | ||||
-rw-r--r-- | plugins/check_procs.c | 61 |
3 files changed, 34 insertions, 46 deletions
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/check_procs.c b/plugins/check_procs.c index bebde983..7f35370b 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 | #include "utils_base.h" | 47 | #include "utils_base.h" |
48 | 48 | ||
@@ -144,7 +144,7 @@ main (int argc, char **argv) | |||
144 | int pos; /* number of spaces before 'args' in `ps` output */ | 144 | int pos; /* number of spaces before 'args' in `ps` output */ |
145 | int cols; /* number of columns in ps output */ | 145 | int cols; /* number of columns in ps output */ |
146 | int expected_cols = PS_COLS - 1; | 146 | int expected_cols = PS_COLS - 1; |
147 | int i = 0; /* Temporary values */ | 147 | int i = 0, j = 0; /* Temporary values */ |
148 | double rss_sum = 0; | 148 | double rss_sum = 0; |
149 | double vsz_sum = 0; | 149 | double vsz_sum = 0; |
150 | double cpu_sum = 0; | 150 | double cpu_sum = 0; |
@@ -153,6 +153,7 @@ main (int argc, char **argv) | |||
153 | double cpu_max = 0; | 153 | double cpu_max = 0; |
154 | int multiple_process_output_flag = 0; | 154 | int multiple_process_output_flag = 0; |
155 | int number_threshold_failure_flag = 0; | 155 | int number_threshold_failure_flag = 0; |
156 | output chld_out, chld_err; | ||
156 | 157 | ||
157 | 158 | ||
158 | setlocale (LC_ALL, ""); | 159 | setlocale (LC_ALL, ""); |
@@ -173,41 +174,27 @@ main (int argc, char **argv) | |||
173 | mypid = getpid(); | 174 | mypid = getpid(); |
174 | 175 | ||
175 | /* Set signal handling and alarm timeout */ | 176 | /* Set signal handling and alarm timeout */ |
176 | if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) { | 177 | if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
177 | usage4 (_("Cannot catch SIGALRM")); | 178 | die (STATE_UNKNOWN, _("Cannot catch SIGALRM")); |
178 | } | 179 | } |
179 | alarm (timeout_interval); | 180 | (void) alarm ((unsigned) timeout_interval); |
180 | 181 | ||
181 | if (verbose >= 3) | 182 | if (verbose >= 3) |
182 | printf (_("CMD: %s\n"), PS_COMMAND); | 183 | printf (_("CMD: %s\n"), PS_COMMAND); |
183 | 184 | ||
184 | if (input_filename == NULL) { | 185 | if (input_filename == NULL) { |
185 | ps_input = spopen (PS_COMMAND); | 186 | result = cmd_run( PS_COMMAND, &chld_out, &chld_err, 0); |
186 | if (ps_input == NULL) { | 187 | if (chld_err.lines > 0) { |
187 | printf (_("Could not open pipe: %s\n"), PS_COMMAND); | 188 | printf ("%s: %s", _("System call sent warnings to stderr"), chld_err.line[0]); |
188 | return STATE_UNKNOWN; | 189 | exit(STATE_WARNING); |
189 | } | 190 | } |
190 | child_stderr = fdopen (child_stderr_array[fileno (ps_input)], "r"); | ||
191 | if (child_stderr == NULL) | ||
192 | printf (_("Could not open stderr for %s\n"), PS_COMMAND); | ||
193 | } else { | 191 | } else { |
194 | ps_input = fopen(input_filename, "r"); | 192 | result = cmd_file_read( input_filename, &chld_out, 0); |
195 | if (ps_input == NULL) { | ||
196 | die( STATE_UNKNOWN, _("Error opening %s\n"), input_filename ); | ||
197 | } | ||
198 | } | 193 | } |
199 | 194 | ||
200 | /* flush first line */ | 195 | /* flush first line: j starts at 1 */ |
201 | fgets (input_buffer, MAX_INPUT_BUFFER - 1, ps_input); | 196 | for (j = 1; j < chld_out.lines; j++) { |
202 | while ( input_buffer[strlen(input_buffer)-1] != '\n' ) | 197 | input_line = chld_out.line[j]; |
203 | fgets (input_buffer, MAX_INPUT_BUFFER - 1, ps_input); | ||
204 | |||
205 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, ps_input)) { | ||
206 | asprintf (&input_line, "%s", input_buffer); | ||
207 | while ( input_buffer[strlen(input_buffer)-1] != '\n' ) { | ||
208 | fgets (input_buffer, MAX_INPUT_BUFFER - 1, ps_input); | ||
209 | asprintf (&input_line, "%s%s", input_line, input_buffer); | ||
210 | } | ||
211 | 198 | ||
212 | if (verbose >= 3) | 199 | if (verbose >= 3) |
213 | printf ("%s", input_line); | 200 | printf ("%s", input_line); |
@@ -353,27 +340,9 @@ main (int argc, char **argv) | |||
353 | } | 340 | } |
354 | } | 341 | } |
355 | 342 | ||
356 | /* If we get anything on STDERR, at least set warning */ | ||
357 | if (input_filename == NULL) { | ||
358 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
359 | if (verbose) | ||
360 | printf ("STDERR: %s", input_buffer); | ||
361 | result = max_state (result, STATE_WARNING); | ||
362 | printf (_("System call sent warnings to stderr\n")); | ||
363 | } | ||
364 | |||
365 | (void) fclose (child_stderr); | ||
366 | |||
367 | /* close the pipe */ | ||
368 | if (spclose (ps_input)) { | ||
369 | printf (_("System call returned nonzero status\n")); | ||
370 | result = max_state (result, STATE_WARNING); | ||
371 | } | ||
372 | } | ||
373 | |||
374 | if (found == 0) { /* no process lines parsed so return STATE_UNKNOWN */ | 343 | if (found == 0) { /* no process lines parsed so return STATE_UNKNOWN */ |
375 | printf (_("Unable to read output\n")); | 344 | printf (_("Unable to read output\n")); |
376 | return result; | 345 | return STATE_UNKNOWN; |
377 | } | 346 | } |
378 | 347 | ||
379 | if ( result == STATE_UNKNOWN ) | 348 | if ( result == STATE_UNKNOWN ) |