summaryrefslogtreecommitdiffstats
path: root/plugins/check_procs.c
diff options
context:
space:
mode:
authorChristian Kujau <lists@nerdbynature.de>2023-03-20 11:35:01 +0100
committerSven Nierlein <sven@nierlein.org>2023-03-21 11:49:08 +0100
commit9c495e2aefedca270f49987dc31bff983e614281 (patch)
treebf3a387a62c00a872291275e9a6b1ba1a6fbc2e2 /plugins/check_procs.c
parentc1070fa1ef06b965c116583e9557f06949bb27c0 (diff)
downloadmonitoring-plugins-9c495e2aefedca270f49987dc31bff983e614281.tar.gz
check_procs: Implement --exclude-process to exclude specific processes.
Signed-off-by: Christian Kujau <lists@nerdbynature.de>
Diffstat (limited to 'plugins/check_procs.c')
-rw-r--r--plugins/check_procs.c47
1 files changed, 45 insertions, 2 deletions
diff --git a/plugins/check_procs.c b/plugins/check_procs.c
index a025ee89..d672dd44 100644
--- a/plugins/check_procs.c
+++ b/plugins/check_procs.c
@@ -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;
93float pcpu; 94float pcpu;
94char *statopts; 95char *statopts;
95char *prog; 96char *prog;
97char *exclude_progs;
98char **exclude_progs_arr = NULL;
99char exclude_progs_counter = 0;
96char *args; 100char *args;
97char *input_filename = NULL; 101char *input_filename = NULL;
98regex_t re_args; 102regex_t re_args;
@@ -250,6 +254,25 @@ main (int argc, char **argv)
250 continue; 254 continue;
251 } 255 }
252 256
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
253 /* filter kernel threads (childs of KTHREAD_PARENT)*/ 276 /* filter kernel threads (childs 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 :-( */
@@ -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)
@@ -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)
@@ -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 seperated 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
@@ -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}