diff options
Diffstat (limited to 'plugins/check_procs.c')
-rw-r--r-- | plugins/check_procs.c | 103 |
1 files changed, 73 insertions, 30 deletions
diff --git a/plugins/check_procs.c b/plugins/check_procs.c index f7917c34..c17c6996 100644 --- a/plugins/check_procs.c +++ b/plugins/check_procs.c | |||
@@ -1,34 +1,34 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring check_procs plugin | 3 | * Monitoring check_procs plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2000-2008 Monitoring Plugins Development Team | 6 | * Copyright (c) 2000-2008 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
10 | * This file contains the check_procs plugin | 10 | * This file contains the check_procs plugin |
11 | * | 11 | * |
12 | * Checks all processes and generates WARNING or CRITICAL states if the | 12 | * Checks all processes and generates WARNING or CRITICAL states if the |
13 | * specified metric is outside the required threshold ranges. The metric | 13 | * specified metric is outside the required threshold ranges. The metric |
14 | * defaults to number of processes. Search filters can be applied to limit | 14 | * defaults to number of processes. Search filters can be applied to limit |
15 | * the processes to check. | 15 | * the processes to check. |
16 | * | 16 | * |
17 | * | 17 | * |
18 | * This program is free software: you can redistribute it and/or modify | 18 | * This program is free software: you can redistribute it and/or modify |
19 | * it under the terms of the GNU General Public License as published by | 19 | * it under the terms of the GNU General Public License as published by |
20 | * the Free Software Foundation, either version 3 of the License, or | 20 | * the Free Software Foundation, either version 3 of the License, or |
21 | * (at your option) any later version. | 21 | * (at your option) any later version. |
22 | * | 22 | * |
23 | * This program is distributed in the hope that it will be useful, | 23 | * This program is distributed in the hope that it will be useful, |
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26 | * GNU General Public License for more details. | 26 | * GNU General Public License for more details. |
27 | * | 27 | * |
28 | * You should have received a copy of the GNU General Public License | 28 | * You should have received a copy of the GNU General Public License |
29 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 29 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
30 | * | 30 | * |
31 | * | 31 | * |
32 | *****************************************************************************/ | 32 | *****************************************************************************/ |
33 | 33 | ||
34 | const char *progname = "check_procs"; | 34 | const char *progname = "check_procs"; |
@@ -50,7 +50,7 @@ const char *email = "devel@monitoring-plugins.org"; | |||
50 | 50 | ||
51 | int process_arguments (int, char **); | 51 | int process_arguments (int, char **); |
52 | int validate_arguments (void); | 52 | int validate_arguments (void); |
53 | int convert_to_seconds (char *); | 53 | int convert_to_seconds (char *); |
54 | void print_help (void); | 54 | void print_help (void); |
55 | void print_usage (void); | 55 | void print_usage (void); |
56 | 56 | ||
@@ -70,6 +70,7 @@ int options = 0; /* bitmask of filter criteria to test against */ | |||
70 | #define PCPU 256 | 70 | #define PCPU 256 |
71 | #define ELAPSED 512 | 71 | #define ELAPSED 512 |
72 | #define EREG_ARGS 1024 | 72 | #define EREG_ARGS 1024 |
73 | #define EXCLUDE_PROGS 2048 | ||
73 | 74 | ||
74 | #define KTHREAD_PARENT "kthreadd" /* the parent process of kernel threads: | 75 | #define KTHREAD_PARENT "kthreadd" /* the parent process of kernel threads: |
75 | ppid of procs are compared to pid of this proc*/ | 76 | ppid of procs are compared to pid of this proc*/ |
@@ -93,6 +94,9 @@ int rss; | |||
93 | float pcpu; | 94 | float pcpu; |
94 | char *statopts; | 95 | char *statopts; |
95 | char *prog; | 96 | char *prog; |
97 | char *exclude_progs; | ||
98 | char **exclude_progs_arr = NULL; | ||
99 | char exclude_progs_counter = 0; | ||
96 | char *args; | 100 | char *args; |
97 | char *input_filename = NULL; | 101 | char *input_filename = NULL; |
98 | regex_t re_args; | 102 | regex_t re_args; |
@@ -230,9 +234,9 @@ main (int argc, char **argv) | |||
230 | procseconds = convert_to_seconds(procetime); | 234 | procseconds = convert_to_seconds(procetime); |
231 | 235 | ||
232 | if (verbose >= 3) | 236 | if (verbose >= 3) |
233 | printf ("proc#=%d uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", | 237 | printf ("proc#=%d uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", |
234 | procs, procuid, procvsz, procrss, | 238 | procs, procuid, procvsz, procrss, |
235 | procpid, procppid, procpcpu, procstat, | 239 | procpid, procppid, procpcpu, procstat, |
236 | procetime, procprog, procargs); | 240 | procetime, procprog, procargs); |
237 | 241 | ||
238 | /* Ignore self */ | 242 | /* Ignore self */ |
@@ -250,7 +254,26 @@ main (int argc, char **argv) | |||
250 | continue; | 254 | continue; |
251 | } | 255 | } |
252 | 256 | ||
253 | /* filter kernel threads (childs of KTHREAD_PARENT)*/ | 257 | /* Ignore excluded processes by name */ |
258 | if(options & EXCLUDE_PROGS) { | ||
259 | int found = 0; | ||
260 | int i = 0; | ||
261 | |||
262 | for(i=0; i < (exclude_progs_counter); i++) { | ||
263 | if(!strcmp(procprog, exclude_progs_arr[i])) { | ||
264 | found = 1; | ||
265 | } | ||
266 | } | ||
267 | if(found == 0) { | ||
268 | resultsum |= EXCLUDE_PROGS; | ||
269 | } else | ||
270 | { | ||
271 | if(verbose >= 3) | ||
272 | printf("excluding - by ignorelist\n"); | ||
273 | } | ||
274 | } | ||
275 | |||
276 | /* filter kernel threads (children of KTHREAD_PARENT)*/ | ||
254 | /* TODO adapt for other OSes than GNU/Linux | 277 | /* TODO adapt for other OSes than GNU/Linux |
255 | sorry for not doing that, but I've no other OSes to test :-( */ | 278 | sorry for not doing that, but I've no other OSes to test :-( */ |
256 | if (kthread_filter == 1) { | 279 | if (kthread_filter == 1) { |
@@ -265,7 +288,7 @@ main (int argc, char **argv) | |||
265 | } | 288 | } |
266 | } | 289 | } |
267 | 290 | ||
268 | if ((options & STAT) && (strstr (statopts, procstat))) | 291 | if ((options & STAT) && (strstr (procstat, statopts))) |
269 | resultsum |= STAT; | 292 | resultsum |= STAT; |
270 | if ((options & ARGS) && procargs && (strstr (procargs, args) != NULL)) | 293 | if ((options & ARGS) && procargs && (strstr (procargs, args) != NULL)) |
271 | resultsum |= ARGS; | 294 | resultsum |= ARGS; |
@@ -292,9 +315,9 @@ main (int argc, char **argv) | |||
292 | 315 | ||
293 | procs++; | 316 | procs++; |
294 | if (verbose >= 2) { | 317 | if (verbose >= 2) { |
295 | printf ("Matched: uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", | 318 | printf ("Matched: uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", |
296 | procuid, procvsz, procrss, | 319 | procuid, procvsz, procrss, |
297 | procpid, procppid, procpcpu, procstat, | 320 | procpid, procppid, procpcpu, procstat, |
298 | procetime, procprog, procargs); | 321 | procetime, procprog, procargs); |
299 | } | 322 | } |
300 | 323 | ||
@@ -320,7 +343,7 @@ main (int argc, char **argv) | |||
320 | result = max_state (result, i); | 343 | result = max_state (result, i); |
321 | } | 344 | } |
322 | } | 345 | } |
323 | } | 346 | } |
324 | /* This should not happen */ | 347 | /* This should not happen */ |
325 | else if (verbose) { | 348 | else if (verbose) { |
326 | printf(_("Not parseable: %s"), input_buffer); | 349 | printf(_("Not parseable: %s"), input_buffer); |
@@ -332,7 +355,7 @@ main (int argc, char **argv) | |||
332 | return STATE_UNKNOWN; | 355 | return STATE_UNKNOWN; |
333 | } | 356 | } |
334 | 357 | ||
335 | if ( result == STATE_UNKNOWN ) | 358 | if ( result == STATE_UNKNOWN ) |
336 | result = STATE_OK; | 359 | result = STATE_OK; |
337 | 360 | ||
338 | /* Needed if procs found, but none match filter */ | 361 | /* Needed if procs found, but none match filter */ |
@@ -352,9 +375,9 @@ main (int argc, char **argv) | |||
352 | if (metric != METRIC_PROCS) { | 375 | if (metric != METRIC_PROCS) { |
353 | printf (_("%d crit, %d warn out of "), crit, warn); | 376 | printf (_("%d crit, %d warn out of "), crit, warn); |
354 | } | 377 | } |
355 | } | 378 | } |
356 | printf (ngettext ("%d process", "%d processes", (unsigned long) procs), procs); | 379 | printf (ngettext ("%d process", "%d processes", (unsigned long) procs), procs); |
357 | 380 | ||
358 | if (strcmp(fmt,"") != 0) { | 381 | if (strcmp(fmt,"") != 0) { |
359 | printf (_(" with %s"), fmt); | 382 | printf (_(" with %s"), fmt); |
360 | } | 383 | } |
@@ -409,6 +432,7 @@ process_arguments (int argc, char **argv) | |||
409 | {"input-file", required_argument, 0, CHAR_MAX+2}, | 432 | {"input-file", required_argument, 0, CHAR_MAX+2}, |
410 | {"no-kthreads", required_argument, 0, 'k'}, | 433 | {"no-kthreads", required_argument, 0, 'k'}, |
411 | {"traditional-filter", no_argument, 0, 'T'}, | 434 | {"traditional-filter", no_argument, 0, 'T'}, |
435 | {"exclude-process", required_argument, 0, 'X'}, | ||
412 | {0, 0, 0, 0} | 436 | {0, 0, 0, 0} |
413 | }; | 437 | }; |
414 | 438 | ||
@@ -417,7 +441,7 @@ process_arguments (int argc, char **argv) | |||
417 | strcpy (argv[c], "-t"); | 441 | strcpy (argv[c], "-t"); |
418 | 442 | ||
419 | while (1) { | 443 | while (1) { |
420 | c = getopt_long (argc, argv, "Vvhkt:c:w:p:s:u:C:a:z:r:m:P:T", | 444 | c = getopt_long (argc, argv, "Vvhkt:c:w:p:s:u:C:a:z:r:m:P:T:X:", |
421 | longopts, &option); | 445 | longopts, &option); |
422 | 446 | ||
423 | if (c == -1 || c == EOF) | 447 | if (c == -1 || c == EOF) |
@@ -440,7 +464,7 @@ process_arguments (int argc, char **argv) | |||
440 | break; | 464 | break; |
441 | case 'c': /* critical threshold */ | 465 | case 'c': /* critical threshold */ |
442 | critical_range = optarg; | 466 | critical_range = optarg; |
443 | break; | 467 | break; |
444 | case 'w': /* warning threshold */ | 468 | case 'w': /* warning threshold */ |
445 | warning_range = optarg; | 469 | warning_range = optarg; |
446 | break; | 470 | break; |
@@ -490,6 +514,23 @@ process_arguments (int argc, char **argv) | |||
490 | prog); | 514 | prog); |
491 | options |= PROG; | 515 | options |= PROG; |
492 | break; | 516 | break; |
517 | case 'X': | ||
518 | if(exclude_progs) | ||
519 | break; | ||
520 | else | ||
521 | exclude_progs = optarg; | ||
522 | xasprintf (&fmt, _("%s%sexclude progs '%s'"), (fmt ? fmt : ""), (options ? ", " : ""), | ||
523 | exclude_progs); | ||
524 | char *p = strtok(exclude_progs, ","); | ||
525 | |||
526 | while(p){ | ||
527 | exclude_progs_arr = realloc(exclude_progs_arr, sizeof(char*) * ++exclude_progs_counter); | ||
528 | exclude_progs_arr[exclude_progs_counter-1] = p; | ||
529 | p = strtok(NULL, ","); | ||
530 | } | ||
531 | |||
532 | options |= EXCLUDE_PROGS; | ||
533 | break; | ||
493 | case 'a': /* args (full path name with args) */ | 534 | case 'a': /* args (full path name with args) */ |
494 | /* TODO: allow this to be passed in with --metric */ | 535 | /* TODO: allow this to be passed in with --metric */ |
495 | if (args) | 536 | if (args) |
@@ -542,11 +583,11 @@ process_arguments (int argc, char **argv) | |||
542 | if ( strcmp(optarg, "PROCS") == 0) { | 583 | if ( strcmp(optarg, "PROCS") == 0) { |
543 | metric = METRIC_PROCS; | 584 | metric = METRIC_PROCS; |
544 | break; | 585 | break; |
545 | } | 586 | } |
546 | else if ( strcmp(optarg, "VSZ") == 0) { | 587 | else if ( strcmp(optarg, "VSZ") == 0) { |
547 | metric = METRIC_VSZ; | 588 | metric = METRIC_VSZ; |
548 | break; | 589 | break; |
549 | } | 590 | } |
550 | else if ( strcmp(optarg, "RSS") == 0 ) { | 591 | else if ( strcmp(optarg, "RSS") == 0 ) { |
551 | metric = METRIC_RSS; | 592 | metric = METRIC_RSS; |
552 | break; | 593 | break; |
@@ -559,7 +600,7 @@ process_arguments (int argc, char **argv) | |||
559 | metric = METRIC_ELAPSED; | 600 | metric = METRIC_ELAPSED; |
560 | break; | 601 | break; |
561 | } | 602 | } |
562 | 603 | ||
563 | usage4 (_("Metric must be one of PROCS, VSZ, RSS, CPU, ELAPSED!")); | 604 | usage4 (_("Metric must be one of PROCS, VSZ, RSS, CPU, ELAPSED!")); |
564 | case 'k': /* linux kernel thread filter */ | 605 | case 'k': /* linux kernel thread filter */ |
565 | kthread_filter = 1; | 606 | kthread_filter = 1; |
@@ -642,7 +683,7 @@ convert_to_seconds(char *etime) { | |||
642 | seconds = 0; | 683 | seconds = 0; |
643 | 684 | ||
644 | for (ptr = etime; *ptr != '\0'; ptr++) { | 685 | for (ptr = etime; *ptr != '\0'; ptr++) { |
645 | 686 | ||
646 | if (*ptr == '-') { | 687 | if (*ptr == '-') { |
647 | hyphcnt++; | 688 | hyphcnt++; |
648 | continue; | 689 | continue; |
@@ -745,6 +786,8 @@ print_help (void) | |||
745 | printf (" %s\n", _("Only scan for processes with args that contain the regex STRING.")); | 786 | printf (" %s\n", _("Only scan for processes with args that contain the regex STRING.")); |
746 | printf (" %s\n", "-C, --command=COMMAND"); | 787 | printf (" %s\n", "-C, --command=COMMAND"); |
747 | printf (" %s\n", _("Only scan for exact matches of COMMAND (without path).")); | 788 | printf (" %s\n", _("Only scan for exact matches of COMMAND (without path).")); |
789 | printf (" %s\n", "-X, --exclude-process"); | ||
790 | printf (" %s\n", _("Exclude processes which match this comma separated list")); | ||
748 | printf (" %s\n", "-k, --no-kthreads"); | 791 | printf (" %s\n", "-k, --no-kthreads"); |
749 | printf (" %s\n", _("Only scan for non kernel threads (works on Linux only).")); | 792 | printf (" %s\n", _("Only scan for non kernel threads (works on Linux only).")); |
750 | 793 | ||
@@ -775,7 +818,7 @@ be the total number of running processes\n\n")); | |||
775 | printf (" %s\n", "check_procs -w 50000 -c 100000 --metric=VSZ"); | 818 | printf (" %s\n", "check_procs -w 50000 -c 100000 --metric=VSZ"); |
776 | printf (" %s\n\n", _("Alert if VSZ of any processes over 50K or 100K")); | 819 | printf (" %s\n\n", _("Alert if VSZ of any processes over 50K or 100K")); |
777 | printf (" %s\n", "check_procs -w 10 -c 20 --metric=CPU"); | 820 | printf (" %s\n", "check_procs -w 10 -c 20 --metric=CPU"); |
778 | printf (" %s\n", _("Alert if CPU of any processes over 10%% or 20%%")); | 821 | printf (" %s\n", _("Alert if CPU of any processes over 10\% or 20\%")); |
779 | 822 | ||
780 | printf (UT_SUPPORT); | 823 | printf (UT_SUPPORT); |
781 | } | 824 | } |
@@ -786,5 +829,5 @@ print_usage (void) | |||
786 | printf ("%s\n", _("Usage:")); | 829 | printf ("%s\n", _("Usage:")); |
787 | printf ("%s -w <range> -c <range> [-m metric] [-s state] [-p ppid]\n", progname); | 830 | printf ("%s -w <range> -c <range> [-m metric] [-s state] [-p ppid]\n", progname); |
788 | printf (" [-u user] [-r rss] [-z vsz] [-P %%cpu] [-a argument-array]\n"); | 831 | printf (" [-u user] [-r rss] [-z vsz] [-P %%cpu] [-a argument-array]\n"); |
789 | printf (" [-C command] [-k] [-t timeout] [-v]\n"); | 832 | printf (" [-C command] [-X process_to_exclude] [-k] [-t timeout] [-v]\n"); |
790 | } | 833 | } |