check_procs: Implement --exclude-process to exclude specific processes.

Signed-off-by: Christian Kujau <lists@nerdbynature.de>
This commit is contained in:
Christian Kujau 2023-03-20 11:35:01 +01:00 committed by Sven Nierlein
parent c1070fa1ef
commit 9c495e2aef

View file

@ -70,6 +70,7 @@ int options = 0; /* bitmask of filter criteria to test against */
#define PCPU 256
#define ELAPSED 512
#define EREG_ARGS 1024
#define EXCLUDE_PROGS 2048
#define KTHREAD_PARENT "kthreadd" /* the parent process of kernel threads:
ppid of procs are compared to pid of this proc*/
@ -93,6 +94,9 @@ int rss;
float pcpu;
char *statopts;
char *prog;
char *exclude_progs;
char **exclude_progs_arr = NULL;
char exclude_progs_counter = 0;
char *args;
char *input_filename = NULL;
regex_t re_args;
@ -250,6 +254,25 @@ main (int argc, char **argv)
continue;
}
/* Ignore excluded processes by name */
if(options & EXCLUDE_PROGS) {
int found = 0;
int i = 0;
for(i=0; i < (exclude_progs_counter); i++) {
if(!strcmp(procprog, exclude_progs_arr[i])) {
found = 1;
}
}
if(found == 0) {
resultsum |= EXCLUDE_PROGS;
} else
{
if(verbose >= 3)
printf("excluding - by ignorelist\n");
}
}
/* filter kernel threads (childs of KTHREAD_PARENT)*/
/* TODO adapt for other OSes than GNU/Linux
sorry for not doing that, but I've no other OSes to test :-( */
@ -409,6 +432,7 @@ process_arguments (int argc, char **argv)
{"input-file", required_argument, 0, CHAR_MAX+2},
{"no-kthreads", required_argument, 0, 'k'},
{"traditional-filter", no_argument, 0, 'T'},
{"exclude-process", required_argument, 0, 'X'},
{0, 0, 0, 0}
};
@ -417,7 +441,7 @@ process_arguments (int argc, char **argv)
strcpy (argv[c], "-t");
while (1) {
c = getopt_long (argc, argv, "Vvhkt:c:w:p:s:u:C:a:z:r:m:P:T",
c = getopt_long (argc, argv, "Vvhkt:c:w:p:s:u:C:a:z:r:m:P:T:X:",
longopts, &option);
if (c == -1 || c == EOF)
@ -490,6 +514,23 @@ process_arguments (int argc, char **argv)
prog);
options |= PROG;
break;
case 'X':
if(exclude_progs)
break;
else
exclude_progs = optarg;
xasprintf (&fmt, _("%s%sexclude progs '%s'"), (fmt ? fmt : ""), (options ? ", " : ""),
exclude_progs);
char *p = strtok(exclude_progs, ",");
while(p){
exclude_progs_arr = realloc(exclude_progs_arr, sizeof(char*) * ++exclude_progs_counter);
exclude_progs_arr[exclude_progs_counter-1] = p;
p = strtok(NULL, ",");
}
options |= EXCLUDE_PROGS;
break;
case 'a': /* args (full path name with args) */
/* TODO: allow this to be passed in with --metric */
if (args)
@ -745,6 +786,8 @@ print_help (void)
printf (" %s\n", _("Only scan for processes with args that contain the regex STRING."));
printf (" %s\n", "-C, --command=COMMAND");
printf (" %s\n", _("Only scan for exact matches of COMMAND (without path)."));
printf (" %s\n", "-X, --exclude-process");
printf (" %s\n", _("Exclude processes which match this comma seperated list"));
printf (" %s\n", "-k, --no-kthreads");
printf (" %s\n", _("Only scan for non kernel threads (works on Linux only)."));
@ -786,5 +829,5 @@ print_usage (void)
printf ("%s\n", _("Usage:"));
printf ("%s -w <range> -c <range> [-m metric] [-s state] [-p ppid]\n", progname);
printf (" [-u user] [-r rss] [-z vsz] [-P %%cpu] [-a argument-array]\n");
printf (" [-C command] [-k] [-t timeout] [-v]\n");
printf (" [-C command] [-X process_to_exclude] [-k] [-t timeout] [-v]\n");
}