monit-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[monit-dev] [monit] r322 committed - Added the "procmatch" command to mo


From: monit
Subject: [monit-dev] [monit] r322 committed - Added the "procmatch" command to monit CLI to allow to test the...
Date: Tue, 15 Feb 2011 19:12:52 +0000

Revision: 322
Author: address@hidden
Date: Tue Feb 15 11:12:25 2011
Log: Added the "procmatch" command to monit CLI to allow to test the
pattern for process matching based check with processes visible
to monit.
  Example usage:
      $ monit procmatch "iChatAgent"



http://code.google.com/p/monit/source/detail?r=322

Modified:
 /trunk/CHANGES.txt
 /trunk/monit.pod
 /trunk/monitor.c
 /trunk/process.c
 /trunk/process.h

=======================================
--- /trunk/CHANGES.txt  Sat Jan 15 14:05:49 2011
+++ /trunk/CHANGES.txt  Tue Feb 15 11:12:25 2011
@@ -9,6 +9,16 @@

 Version 5.2.4

+NEW FEATURES AND FUNCTIONS:
+
+* Added the "procmatch" command to monit CLI to allow to test the
+  pattern for process matching based check with processes visible
+  to monit.
+  Example usage:
+      $ monit procmatch "iChatAgent"
+
+* Reduced monit memory footprint.
+
 BUGFIXES:

 * FreeBSD, NetBSD, OpenBSD, MacOSX, Solaris filesystem check fix:
@@ -23,10 +33,6 @@
 * If protocol test failed, print request path in the event if it is
   set in monit configuration file. Thanks to Marco for report.

-IMPROVEMENTS:
-
-* Reduced memory usage.
-


 Version 5.2.3
=======================================
--- /trunk/monit.pod    Wed Jan 19 10:40:32 2011
+++ /trunk/monit.pod    Tue Feb 15 11:12:25 2011
@@ -2954,11 +2954,13 @@
 defined.
 <regex> is alternative process specification using pattern matching
 to process name (command line) from process table instead of pidfile.
-The pattern to match can be obtained using 'ps' utility. The first
-match is used so this form of check is useful for unique pattern
-matching - the pidfile should be used where possible as it defines
-expected pid exactly (pattern matching won't be useful for Apache
-in most cases for example).
+The first match is used so this form of check is useful for unique
+pattern matching - the pidfile should be used where possible as it
+defines expected pid exactly (pattern matching won't be useful for
+Apache in most cases for example).
+The pattern can be obtained using I<monit procmatch ".*"> CLI command
+which lists all processes visible to Monit or using the I<ps> utility.
+The "procmatch" CLI command can be used to test your pattern as well.
 If Monit runs in passive mode or the start methods is not defined,
 Monit will just send alerts on errors.

=======================================
--- /trunk/monitor.c    Wed Jan 19 10:40:32 2011
+++ /trunk/monitor.c    Tue Feb 15 11:12:25 2011
@@ -440,6 +440,12 @@
     status(LEVEL_NAME_FULL);
   } else if (IS(action, "summary")) {
     status(LEVEL_NAME_SUMMARY);
+  } else if (IS(action, "procmatch")) {
+    if (! service) {
+      printf("Invalid syntax - usage: procmatch \"<pattern>\"\n");
+      exit(1);
+    }
+    process_testmatch(service);
   } else if (IS(action, "quit")) {
     kill_daemon(SIGTERM);
   } else if (IS(action, "validate")) {
@@ -685,21 +691,22 @@
   printf(" -V            Print version number and patchlevel\n");
   printf(" -h            Print this text\n");
printf("Optional action arguments for non-daemon mode are as follows:\n");
-  printf(" start all      - Start all services\n");
-  printf(" start name     - Only start the named service\n");
-  printf(" stop all       - Stop all services\n");
-  printf(" stop name      - Only stop the named service\n");
-  printf(" restart all    - Stop and start all services\n");
-  printf(" restart name   - Only restart the named service\n");
-  printf(" monitor all    - Enable monitoring of all services\n");
- printf(" monitor name - Only enable monitoring of the named service\n");
-  printf(" unmonitor all  - Disable monitoring of all services\n");
- printf(" unmonitor name - Only disable monitoring of the named service\n");
-  printf(" reload         - Reinitialize monit\n");
- printf(" status - Print full status information for each service\n"); - printf(" summary - Print short status information for each service\n");
-  printf(" quit           - Kill monit daemon process\n");
- printf(" validate - Check all services and start if not running\n");
+  printf(" start all           - Start all services\n");
+  printf(" start name          - Only start the named service\n");
+  printf(" stop all            - Stop all services\n");
+  printf(" stop name           - Only stop the named service\n");
+  printf(" restart all         - Stop and start all services\n");
+  printf(" restart name        - Only restart the named service\n");
+  printf(" monitor all         - Enable monitoring of all services\n");
+ printf(" monitor name - Only enable monitoring of the named service\n");
+  printf(" unmonitor all       - Disable monitoring of all services\n");
+ printf(" unmonitor name - Only disable monitoring of the named service\n");
+  printf(" reload              - Reinitialize monit\n");
+ printf(" status - Print full status information for each service\n"); + printf(" summary - Print short status information for each service\n");
+  printf(" quit                - Kill monit daemon process\n");
+ printf(" validate - Check all services and start if not running\n");
+  printf(" procmatch <pattern> - Test process matching pattern\n");
   printf("\n");
printf("(Action arguments operate on services defined in the control file)\n");
 }
=======================================
--- /trunk/process.c    Wed Jan 19 10:40:32 2011
+++ /trunk/process.c    Tue Feb 15 11:12:25 2011
@@ -340,3 +340,53 @@
   return;
 }

+
+void process_testmatch(char *pattern) {
+#ifdef HAVE_REGEX_H
+  regex_t *regex_comp;
+  int reg_return;
+#endif
+  int ptreesize    = 0;
+  int oldptreesize = 0;
+  ProcessTree_T *ptree = NULL;
+  ProcessTree_T *oldptree = NULL;
+
+#ifdef HAVE_REGEX_H
+  NEW(regex_comp);
+ if ((reg_return = regcomp(regex_comp, pattern, REG_NOSUB|REG_EXTENDED))) {
+    char errbuf[STRLEN];
+    regerror(reg_return, regex_comp, errbuf, STRLEN);
+    regfree(regex_comp);
+    FREE(regex_comp);
+    printf("Regex %s parsing error: %s\n", pattern, errbuf);
+    exit(1);
+  }
+#endif
+  initprocesstree(&ptree, &ptreesize, &oldptree, &oldptreesize);
+  if (Run.doprocess) {
+    int i, count = 0;
+    printf("List of processes matching pattern \"%s\":\n", pattern);
+    printf("------------------------------------------\n");
+    for (i = 0; i < ptreesize; i++) {
+      int match = FALSE;
+      if (ptree[i].cmdline && ! strstr(ptree[i].cmdline, "procmatch")) {
+#ifdef HAVE_REGEX_H
+ match = regexec(regex_comp, ptree[i].cmdline, 0, NULL, 0) ? FALSE : TRUE;
+#else
+        match = strstr(ptree[i].cmdline, pattern) ? TRUE : FALSE;
+#endif
+        if (match) {
+          printf("\t%s\n", ptree[i].cmdline);
+          count++;
+        }
+      }
+    }
+    printf("------------------------------------------\n");
+    printf("Total matches: %d\n", count);
+    if (count > 1)
+ printf("WARNING: multiple processes matched the pattern. The check is FIRST-MATCH based, please refine the pattern\n");
+  }
+  delprocesstree(&ptree, ptreesize);
+}
+
+
=======================================
--- /trunk/process.h    Wed Jan 19 10:40:32 2011
+++ /trunk/process.h    Tue Feb 15 11:12:25 2011
@@ -41,10 +41,10 @@
int update_process_data(Service_T s, ProcessTree_T *, int treesize, pid_t pid);
 int init_process_info(void);
 int update_system_load(ProcessTree_T *, int);
-
 int  findprocess(int, ProcessTree_T *, int);
 int  initprocesstree(ProcessTree_T **, int*, ProcessTree_T **, int*);
 void delprocesstree(ProcessTree_T **, int);
+void process_testmatch(char *);

 #endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]