diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_procs.c | 26 | ||||
-rw-r--r-- | plugins/t/check_procs.t | 44 |
2 files changed, 58 insertions, 12 deletions
diff --git a/plugins/check_procs.c b/plugins/check_procs.c index 690b8554..7dae8454 100644 --- a/plugins/check_procs.c +++ b/plugins/check_procs.c | |||
@@ -43,6 +43,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; | |||
43 | #include "common.h" | 43 | #include "common.h" |
44 | #include "popen.h" | 44 | #include "popen.h" |
45 | #include "utils.h" | 45 | #include "utils.h" |
46 | #include "regex.h" | ||
46 | 47 | ||
47 | #include <pwd.h> | 48 | #include <pwd.h> |
48 | 49 | ||
@@ -69,6 +70,7 @@ int options = 0; /* bitmask of filter criteria to test against */ | |||
69 | #define RSS 128 | 70 | #define RSS 128 |
70 | #define PCPU 256 | 71 | #define PCPU 256 |
71 | #define ELAPSED 512 | 72 | #define ELAPSED 512 |
73 | #define EREG_ARGS 1024 | ||
72 | /* Different metrics */ | 74 | /* Different metrics */ |
73 | char *metric_name; | 75 | char *metric_name; |
74 | enum metric { | 76 | enum metric { |
@@ -89,6 +91,7 @@ float pcpu; | |||
89 | char *statopts; | 91 | char *statopts; |
90 | char *prog; | 92 | char *prog; |
91 | char *args; | 93 | char *args; |
94 | regex_t re_args; | ||
92 | char *fmt; | 95 | char *fmt; |
93 | char *fails; | 96 | char *fails; |
94 | char tmp[MAX_INPUT_BUFFER]; | 97 | char tmp[MAX_INPUT_BUFFER]; |
@@ -211,6 +214,8 @@ main (int argc, char **argv) | |||
211 | resultsum |= STAT; | 214 | resultsum |= STAT; |
212 | if ((options & ARGS) && procargs && (strstr (procargs, args) != NULL)) | 215 | if ((options & ARGS) && procargs && (strstr (procargs, args) != NULL)) |
213 | resultsum |= ARGS; | 216 | resultsum |= ARGS; |
217 | if ((options & EREG_ARGS) && procargs && (regexec(&re_args, procargs, (size_t) 0, NULL, 0) == 0)) | ||
218 | resultsum |= EREG_ARGS; | ||
214 | if ((options & PROG) && procprog && (strcmp (prog, procprog) == 0)) | 219 | if ((options & PROG) && procprog && (strcmp (prog, procprog) == 0)) |
215 | resultsum |= PROG; | 220 | resultsum |= PROG; |
216 | if ((options & PPID) && (procppid == ppid)) | 221 | if ((options & PPID) && (procppid == ppid)) |
@@ -231,6 +236,12 @@ main (int argc, char **argv) | |||
231 | continue; | 236 | continue; |
232 | 237 | ||
233 | procs++; | 238 | procs++; |
239 | if (verbose >= 2) { | ||
240 | printf ("Matched: uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", | ||
241 | procuid, procvsz, procrss, | ||
242 | procpid, procppid, procpcpu, procstat, | ||
243 | procetime, procprog, procargs); | ||
244 | } | ||
234 | 245 | ||
235 | if (metric == METRIC_VSZ) | 246 | if (metric == METRIC_VSZ) |
236 | i = check_thresholds (procvsz); | 247 | i = check_thresholds (procvsz); |
@@ -326,6 +337,9 @@ process_arguments (int argc, char **argv) | |||
326 | char *user; | 337 | char *user; |
327 | struct passwd *pw; | 338 | struct passwd *pw; |
328 | int option = 0; | 339 | int option = 0; |
340 | int err; | ||
341 | int cflags = REG_NOSUB | REG_EXTENDED; | ||
342 | char errbuf[MAX_INPUT_BUFFER]; | ||
329 | static struct option longopts[] = { | 343 | static struct option longopts[] = { |
330 | {"warning", required_argument, 0, 'w'}, | 344 | {"warning", required_argument, 0, 'w'}, |
331 | {"critical", required_argument, 0, 'c'}, | 345 | {"critical", required_argument, 0, 'c'}, |
@@ -342,6 +356,7 @@ process_arguments (int argc, char **argv) | |||
342 | {"help", no_argument, 0, 'h'}, | 356 | {"help", no_argument, 0, 'h'}, |
343 | {"version", no_argument, 0, 'V'}, | 357 | {"version", no_argument, 0, 'V'}, |
344 | {"verbose", no_argument, 0, 'v'}, | 358 | {"verbose", no_argument, 0, 'v'}, |
359 | {"ereg-argument-array", required_argument, 0, CHAR_MAX+1}, | ||
345 | {0, 0, 0, 0} | 360 | {0, 0, 0, 0} |
346 | }; | 361 | }; |
347 | 362 | ||
@@ -450,6 +465,15 @@ process_arguments (int argc, char **argv) | |||
450 | asprintf (&fmt, "%s%sargs '%s'", (fmt ? fmt : ""), (options ? ", " : ""), args); | 465 | asprintf (&fmt, "%s%sargs '%s'", (fmt ? fmt : ""), (options ? ", " : ""), args); |
451 | options |= ARGS; | 466 | options |= ARGS; |
452 | break; | 467 | break; |
468 | case CHAR_MAX+1: | ||
469 | err = regcomp(&re_args, optarg, cflags); | ||
470 | if (err != 0) { | ||
471 | regerror (err, &re_args, errbuf, MAX_INPUT_BUFFER); | ||
472 | die (STATE_UNKNOWN, "PROCS %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | ||
473 | } | ||
474 | asprintf (&fmt, "%s%sregex args '%s'", (fmt ? fmt : ""), (options ? ", " : ""), optarg); | ||
475 | options |= EREG_ARGS; | ||
476 | break; | ||
453 | case 'r': /* RSS */ | 477 | case 'r': /* RSS */ |
454 | if (sscanf (optarg, "%d%[^0-9]", &rss, tmp) == 1) { | 478 | if (sscanf (optarg, "%d%[^0-9]", &rss, tmp) == 1) { |
455 | asprintf (&fmt, "%s%sRSS >= %d", (fmt ? fmt : ""), (options ? ", " : ""), rss); | 479 | asprintf (&fmt, "%s%sRSS >= %d", (fmt ? fmt : ""), (options ? ", " : ""), rss); |
@@ -716,6 +740,8 @@ print_help (void) | |||
716 | printf (" %s\n", _("Only scan for processes with user name or ID indicated.")); | 740 | printf (" %s\n", _("Only scan for processes with user name or ID indicated.")); |
717 | printf (" %s\n", "-a, --argument-array=STRING"); | 741 | printf (" %s\n", "-a, --argument-array=STRING"); |
718 | printf (" %s\n", _("Only scan for processes with args that contain STRING.")); | 742 | printf (" %s\n", _("Only scan for processes with args that contain STRING.")); |
743 | printf (" %s\n", "--ereg-argument-array=STRING"); | ||
744 | printf (" %s\n", _("Only scan for processes with args that contain the regex STRING.")); | ||
719 | printf (" %s\n", "-C, --command=COMMAND"); | 745 | printf (" %s\n", "-C, --command=COMMAND"); |
720 | printf (" %s\n", _("Only scan for exact matches of COMMAND (without path).")); | 746 | printf (" %s\n", _("Only scan for exact matches of COMMAND (without path).")); |
721 | 747 | ||
diff --git a/plugins/t/check_procs.t b/plugins/t/check_procs.t index b8c2e8a5..2a41ac5c 100644 --- a/plugins/t/check_procs.t +++ b/plugins/t/check_procs.t | |||
@@ -6,20 +6,40 @@ | |||
6 | # | 6 | # |
7 | 7 | ||
8 | use strict; | 8 | use strict; |
9 | use Test; | 9 | use Test::More; |
10 | use NPTest; | 10 | use NPTest; |
11 | 11 | ||
12 | use vars qw($tests); | ||
13 | BEGIN {$tests = 12; plan tests => $tests} | ||
14 | |||
15 | my $t; | 12 | my $t; |
16 | 13 | ||
17 | $t += checkCmd( "./check_procs -w 100000 -c 100000", 0, '/^PROCS OK: [0-9]+ process(es)?$/' ); | 14 | if (`uname -s` eq "SunOS\n") { |
18 | $t += checkCmd( "./check_procs -w 100000 -c 100000 -s Z", 0, '/^PROCS OK: [0-9]+ process(es)? with /' ); | 15 | plan skip_all => "Ignoring tests on solaris because of pst3"; |
19 | $t += checkCmd( "./check_procs -w 0 -c 10000000", 1, '/^PROCS WARNING: [0-9]+ process(es)?$/' ); | 16 | } else { |
20 | $t += checkCmd( "./check_procs -w 0 -c 0", 2, '/^PROCS CRITICAL: [0-9]+ process(es)?$/' ); | 17 | plan tests => 12; |
21 | $t += checkCmd( "./check_procs -w 0 -c 0 -s S", 2, '/^PROCS CRITICAL: [0-9]+ process(es)? with /' ); | 18 | } |
22 | $t += checkCmd( "./check_procs -w 0 -c 10000000 -p 1", 1, '/^PROCS WARNING: [0-9]+ process(es)? with PPID = 1/' ); | 19 | |
20 | my $result; | ||
21 | |||
22 | $result = NPTest->testCmd( "./check_procs -w 100000 -c 100000" ); | ||
23 | is( $result->return_code, 0, "Checking less than 10000 processes" ); | ||
24 | like( $result->output, '/^PROCS OK: [0-9]+ process(es)?$/', "Output correct" ); | ||
25 | |||
26 | $result = NPTest->testCmd( "./check_procs -w 100000 -c 100000 -s Z" ); | ||
27 | is( $result->return_code, 0, "Checking less than 100000 zombie processes" ); | ||
28 | like( $result->output, '/^PROCS OK: [0-9]+ process(es)? with /', "Output correct" ); | ||
29 | |||
30 | $result = NPTest->testCmd( "./check_procs -w 0 -c 100000" ); | ||
31 | is( $result->return_code, 1, "Checking warning if processes > 0" ); | ||
32 | like( $result->output, '/^PROCS WARNING: [0-9]+ process(es)?$/', "Output correct" ); | ||
33 | |||
34 | $result = NPTest->testCmd( "./check_procs -w 0 -c 0" ); | ||
35 | is( $result->return_code, 2, "Checking critical if processes > 0" ); | ||
36 | like( $result->output, '/^PROCS CRITICAL: [0-9]+ process(es)?$/', "Output correct" ); | ||
37 | |||
38 | $result = NPTest->testCmd( "./check_procs -w 0 -c 0 -s S" ); | ||
39 | is( $result->return_code, 2, "Checking critical if sleeping processes" ); | ||
40 | like( $result->output, '/^PROCS CRITICAL: [0-9]+ process(es)? with /', "Output correct" ); | ||
41 | |||
42 | $result = NPTest->testCmd( "./check_procs -w 0 -c 100000 -p 1" ); | ||
43 | is( $result->return_code, 1, "Checking warning for processes by parentid = 1" ); | ||
44 | like( $result->output, '/^PROCS WARNING: [0-9]+ process(es)? with PPID = 1/', "Output correct" ); | ||
23 | 45 | ||
24 | exit(0) if defined($Test::Harness::VERSION); | ||
25 | exit($tests - $t); | ||