From 0f71b7d07a8badd9aa38ef8d847bd982ab2db72c Mon Sep 17 00:00:00 2001
From: Matthias Eble <psychotrahe@users.sourceforge.net>
Date: Fri, 30 Mar 2007 08:53:58 +0000
Subject: check_disk: added regex functionality -r and -R. see np-devel mail
 (2007-02-10)

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1658 f882894a-f735-0410-b71e-b25c423dba1c

diff --git a/lib/utils_disk.c b/lib/utils_disk.c
index 877b80d..cfd6a97 100644
--- a/lib/utils_disk.c
+++ b/lib/utils_disk.c
@@ -155,3 +155,13 @@ np_seen_name(struct name_list *list, const char *name)
   return FALSE;
 }
 
+int
+np_regex_match_mount_entry (struct mount_entry* me, regex_t* re) 
+{
+  if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 ||
+      regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) {
+    return true;
+  } else {
+    return false;
+  }
+}
diff --git a/lib/utils_disk.h b/lib/utils_disk.h
index 331c16d..6263339 100644
--- a/lib/utils_disk.h
+++ b/lib/utils_disk.h
@@ -2,6 +2,7 @@
 
 #include "mountlist.h"
 #include "utils_base.h"
+#include "regex.h"
 
 struct name_list
 {
@@ -32,3 +33,4 @@ struct parameter_list *np_add_parameter(struct parameter_list **list, const char
 struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name);
 int search_parameter_list (struct parameter_list *list, const char *name);
 void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact);
+int np_regex_match_mount_entry (struct mount_entry* me, regex_t* re);
diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index 1009564..2b12e56 100644
--- a/plugins/check_disk.c
+++ b/plugins/check_disk.c
@@ -56,6 +56,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
 #if HAVE_LIMITS_H
 # include <limits.h>
 #endif
+#include "regex.h"
 
 
 /* If nonzero, show inode information. */
@@ -437,11 +438,16 @@ double calculate_percent(uintmax_t value, uintmax_t total) {
 int
 process_arguments (int argc, char **argv)
 {
-  int c;
+  int c, err;
   struct parameter_list *se;
   struct parameter_list *temp_list;
+  struct mount_entry *me;
   int result = OK;
   struct stat *stat_buf;
+  regex_t re;
+  int cflags = REG_NOSUB | REG_EXTENDED;
+  char errbuf[MAX_INPUT_BUFFER];
+  bool fnd = false;
 
   int option = 0;
   static struct option longopts[] = {
@@ -460,6 +466,10 @@ process_arguments (int argc, char **argv)
     {"exclude_device", required_argument, 0, 'x'},
     {"exclude-type", required_argument, 0, 'X'},
     {"group", required_argument, 0, 'g'},
+    {"eregi-path", required_argument, 0, 'R'},
+    {"eregi-partition", required_argument, 0, 'R'},
+    {"ereg-path", required_argument, 0, 'r'},
+    {"ereg-partition", required_argument, 0, 'r'},
     {"mountpoint", no_argument, 0, 'M'},
     {"errors-only", no_argument, 0, 'e'},
     {"exact-match", no_argument, 0, 'E'},
@@ -481,7 +491,7 @@ process_arguments (int argc, char **argv)
       strcpy (argv[c], "-t");
 
   while (1) {
-    c = getopt_long (argc, argv, "+?VqhveCt:c:w:K:W:u:p:x:X:mklg:ME", longopts, &option);
+    c = getopt_long (argc, argv, "+?VqhveCt:c:w:K:W:u:p:x:X:mklg:R:r:ME", longopts, &option);
 
     if (c == -1 || c == EOF)
       break;
@@ -626,6 +636,44 @@ process_arguments (int argc, char **argv)
         die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set group value before using -p\n"));
       group = optarg;
       break;
+    case 'R':
+      cflags |= REG_ICASE;
+    case 'r':
+      if (! (warn_freespace_units || crit_freespace_units || warn_freespace_percent || 
+             crit_freespace_percent || warn_usedspace_units || crit_usedspace_units ||
+             warn_usedspace_percent || crit_usedspace_percent || warn_usedinodes_percent ||
+             crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent )) {
+        die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -r/-R\n"));
+      }
+
+      err = regcomp(&re, optarg, cflags);
+      if (err != 0) {
+        regerror (err, &re, errbuf, MAX_INPUT_BUFFER);
+        die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), _("Could not compile regular expression"), errbuf);
+      }
+          
+      for (me = mount_list; me; me = me->me_next) {
+        if (np_regex_match_mount_entry(me, &re)) {
+	  fnd = true;
+          if (verbose > 3)
+	    printf("%s %s matching expression %s\n", me->me_devname, me->me_mountdir, optarg);
+
+          /* add parameter if not found. overwrite thresholds if path has already been added  */
+          if (! (se = np_find_parameter(path_select_list, me->me_mountdir))) {
+            se = np_add_parameter(&path_select_list, me->me_mountdir);
+          }
+          se->group = group;
+          set_all_thresholds(se);
+        }
+      }
+
+      if (!fnd)
+        die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"),
+            _("Regular expression did not match any path or disk"), optarg);
+
+      fnd = false;
+      path_selected = true;
+      break;
     case 'M': /* display mountpoint */
       display_mntp = TRUE;
       break;
@@ -824,6 +872,10 @@ print_help (void)
   printf ("    %s\n", _("Only check local filesystems"));
   printf (" %s\n", "-p, --path=PATH, --partition=PARTITION");
   printf ("    %s\n", _("Path or partition (may be repeated)"));
+  printf (" %s\n", "-r, --ereg-path=PATH, --ereg-partition=PARTITION");
+  printf ("    %s\n", _("Regular expression for path or partition (may be repeated)"));
+  printf (" %s\n", "-R, --eregi-path=PATH, --eregi-partition=PARTITION");
+  printf ("    %s\n", _("Case insensitive regular expression for path/partition (may be repeated)"));
   printf (" %s\n", "-g, --group=NAME");
   printf ("    %s\n", _("Group pathes. Thresholds apply to (free-)space of all partitions together"));
   printf (" %s\n", "-x, --exclude_device=PATH <STRING>");
-- 
cgit v0.10-9-g596f

