summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorRichard Leitner <me@g0hl1n.net>2013-05-29 16:49:59 +0200
committerHolger Weiss <holger@zedat.fu-berlin.de>2013-08-18 00:55:34 +0200
commit3931bd9500c6e71141e8be835a3b795a20ee0afc (patch)
tree3cd284b9ca8cefe3e2b2fa0afa8d09d86e71b022 /plugins
parentf73e9f7b22f9e0992ea43ad1ded87d781aef43e0 (diff)
downloadmonitoring-plugins-3931bd9500c6e71141e8be835a3b795a20ee0afc.tar.gz
Fixed SF.net bug #3552839, check_procs: added -k option to ignore kernel threads
This commit fixes sourceforge.net bug #3552839. It adds a -k|--no-kthreads option to ignore kernel thread processes. Please note: currently this feature only works for GNU/Linux systems (due to the fact I have no other systems to test/develop on) Sorry for that, but I'm sure this can be accomplished by somebody else ;-)
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_procs.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/plugins/check_procs.c b/plugins/check_procs.c
index 6acedc70..9de3cc25 100644
--- a/plugins/check_procs.c
+++ b/plugins/check_procs.c
@@ -65,6 +65,10 @@ int options = 0; /* bitmask of filter criteria to test against */
65#define PCPU 256 65#define PCPU 256
66#define ELAPSED 512 66#define ELAPSED 512
67#define EREG_ARGS 1024 67#define EREG_ARGS 1024
68
69#define KTHREAD_PARENT "kthreadd" /* the parent process of kernel threads:
70 ppid of procs are compared to pid of this proc*/
71
68/* Different metrics */ 72/* Different metrics */
69char *metric_name; 73char *metric_name;
70enum metric { 74enum metric {
@@ -90,6 +94,7 @@ regex_t re_args;
90char *fmt; 94char *fmt;
91char *fails; 95char *fails;
92char tmp[MAX_INPUT_BUFFER]; 96char tmp[MAX_INPUT_BUFFER];
97int kthread_filter = 0;
93 98
94FILE *ps_input = NULL; 99FILE *ps_input = NULL;
95 100
@@ -105,6 +110,7 @@ main (int argc, char **argv)
105 int procuid = 0; 110 int procuid = 0;
106 pid_t procpid = 0; 111 pid_t procpid = 0;
107 pid_t procppid = 0; 112 pid_t procppid = 0;
113 pid_t kthread_ppid = 0;
108 int procvsz = 0; 114 int procvsz = 0;
109 int procrss = 0; 115 int procrss = 0;
110 int procseconds = 0; 116 int procseconds = 0;
@@ -202,6 +208,21 @@ main (int argc, char **argv)
202 /* Ignore self */ 208 /* Ignore self */
203 if (mypid == procpid) continue; 209 if (mypid == procpid) continue;
204 210
211 /* filter kernel threads (childs of KTHREAD_PARENT)*/
212 /* TODO adapt for other OSes than GNU/Linux
213 sorry for not doing that, but I've no other OSes to test :-( */
214 if (kthread_filter == 1) {
215 /* get pid KTHREAD_PARENT */
216 if (kthread_ppid == 0 && !strcmp(procprog, KTHREAD_PARENT) )
217 kthread_ppid = procpid;
218
219 if (kthread_ppid == procppid) {
220 if (verbose >= 2)
221 printf ("Ignore kernel thread: pid=%d ppid=%d prog=%s args=%s\n", procpid, procppid, procprog, procargs);
222 continue;
223 }
224 }
225
205 if ((options & STAT) && (strstr (statopts, procstat))) 226 if ((options & STAT) && (strstr (statopts, procstat)))
206 resultsum |= STAT; 227 resultsum |= STAT;
207 if ((options & ARGS) && procargs && (strstr (procargs, args) != NULL)) 228 if ((options & ARGS) && procargs && (strstr (procargs, args) != NULL))
@@ -344,6 +365,7 @@ process_arguments (int argc, char **argv)
344 {"verbose", no_argument, 0, 'v'}, 365 {"verbose", no_argument, 0, 'v'},
345 {"ereg-argument-array", required_argument, 0, CHAR_MAX+1}, 366 {"ereg-argument-array", required_argument, 0, CHAR_MAX+1},
346 {"input-file", required_argument, 0, CHAR_MAX+2}, 367 {"input-file", required_argument, 0, CHAR_MAX+2},
368 {"no-kthreads", required_argument, 0, 'k'},
347 {0, 0, 0, 0} 369 {0, 0, 0, 0}
348 }; 370 };
349 371
@@ -352,7 +374,7 @@ process_arguments (int argc, char **argv)
352 strcpy (argv[c], "-t"); 374 strcpy (argv[c], "-t");
353 375
354 while (1) { 376 while (1) {
355 c = getopt_long (argc, argv, "Vvht:c:w:p:s:u:C:a:z:r:m:P:", 377 c = getopt_long (argc, argv, "Vvhkt:c:w:p:s:u:C:a:z:r:m:P:",
356 longopts, &option); 378 longopts, &option);
357 379
358 if (c == -1 || c == EOF) 380 if (c == -1 || c == EOF)
@@ -496,6 +518,9 @@ process_arguments (int argc, char **argv)
496 } 518 }
497 519
498 usage4 (_("Metric must be one of PROCS, VSZ, RSS, CPU, ELAPSED!")); 520 usage4 (_("Metric must be one of PROCS, VSZ, RSS, CPU, ELAPSED!"));
521 case 'k': /* linux kernel thread filter */
522 kthread_filter = 1;
523 break;
499 case 'v': /* command */ 524 case 'v': /* command */
500 verbose++; 525 verbose++;
501 break; 526 break;
@@ -671,6 +696,8 @@ print_help (void)
671 printf (" %s\n", _("Only scan for processes with args that contain the regex STRING.")); 696 printf (" %s\n", _("Only scan for processes with args that contain the regex STRING."));
672 printf (" %s\n", "-C, --command=COMMAND"); 697 printf (" %s\n", "-C, --command=COMMAND");
673 printf (" %s\n", _("Only scan for exact matches of COMMAND (without path).")); 698 printf (" %s\n", _("Only scan for exact matches of COMMAND (without path)."));
699 printf (" %s\n", "-k, --no-kthreads");
700 printf (" %s\n", _("Only scan for non kernel threads (works on Linux only)."));
674 701
675 printf(_("\n\ 702 printf(_("\n\
676RANGEs are specified 'min:max' or 'min:' or ':max' (or 'max'). If\n\ 703RANGEs are specified 'min:max' or 'min:' or ':max' (or 'max'). If\n\
@@ -705,5 +732,5 @@ print_usage (void)
705 printf ("%s\n", _("Usage:")); 732 printf ("%s\n", _("Usage:"));
706 printf ("%s -w <range> -c <range> [-m metric] [-s state] [-p ppid]\n", progname); 733 printf ("%s -w <range> -c <range> [-m metric] [-s state] [-p ppid]\n", progname);
707 printf (" [-u user] [-r rss] [-z vsz] [-P %%cpu] [-a argument-array]\n"); 734 printf (" [-u user] [-r rss] [-z vsz] [-P %%cpu] [-a argument-array]\n");
708 printf (" [-C command] [-t timeout] [-v]\n"); 735 printf (" [-C command] [-k] [-t timeout] [-v]\n");
709} 736}