From a1fea1eafd63ca47cc1227c2ba3e0264e56c6f0d Mon Sep 17 00:00:00 2001
From: Matthias Eble <psychotrahe@users.sourceforge.net>
Date: Fri, 30 Mar 2007 08:48:50 +0000
Subject: check_disk -C is more flexible now. See np-devel post (2007-02-10)

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

diff --git a/lib/utils_disk.c b/lib/utils_disk.c
index 74708c0..fd768b2 100644
--- a/lib/utils_disk.c
+++ b/lib/utils_disk.c
@@ -73,39 +73,54 @@ np_add_parameter(struct parameter_list **list, const char *name)
   return new_path;
 }
 
+/* returns a pointer to the struct found in the list */
+struct parameter_list *
+np_find_parameter(struct parameter_list *list, const char *name)
+{
+  struct parameter_list *temp_list;
+  for (temp_list = list; temp_list; temp_list = temp_list->name_next) {
+    if (! strcmp(temp_list->name, name))
+        return temp_list;
+  }
+        
+  return NULL;
+}
+
 void
 np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact)
 {
   struct parameter_list *d;
   for (d = desired; d; d= d->name_next) {
-    struct mount_entry *me;
-    size_t name_len = strlen(d->name);
-    size_t best_match_len = 0;
-    struct mount_entry *best_match = NULL;
+    if (! d->best_match) {
+      struct mount_entry *me;
+      size_t name_len = strlen(d->name);
+      size_t best_match_len = 0;
+      struct mount_entry *best_match = NULL;
 
-    for (me = mount_list; me; me = me->me_next) {
-      size_t len = strlen (me->me_mountdir);
-      if ((exact == FALSE && (best_match_len <= len && len <= name_len && 
-        (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
-	|| (exact == TRUE && strcmp(me->me_mountdir, d->name)==0))
-      {
-        best_match = me;
-        best_match_len = len;
-      } else {
-        len = strlen (me->me_devname);
-        if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
-          (len == 1 || strncmp (me->me_devname, d->name, len) == 0)))
-          || (exact == TRUE && strcmp(me->me_devname, d->name)==0))
+      for (me = mount_list; me; me = me->me_next) {
+        size_t len = strlen (me->me_mountdir);
+        if ((exact == FALSE && (best_match_len <= len && len <= name_len && 
+          (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
+      || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0))
         {
           best_match = me;
           best_match_len = len;
+        } else {
+          len = strlen (me->me_devname);
+          if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
+            (len == 1 || strncmp (me->me_devname, d->name, len) == 0)))
+            || (exact == TRUE && strcmp(me->me_devname, d->name)==0))
+          {
+            best_match = me;
+            best_match_len = len;
+          }
         }
       }
-    }
-    if (best_match) {
-      d->best_match = best_match;
-    } else {
-      d->best_match = NULL;	/* Not sure why this is needed as it should be null on initialisation */
+      if (best_match) {
+        d->best_match = best_match;
+      } else {
+        d->best_match = NULL;	/* Not sure why this is needed as it should be null on initialisation */
+      }
     }
   }
 }
diff --git a/lib/utils_disk.h b/lib/utils_disk.h
index 8bf4f11..55de938 100644
--- a/lib/utils_disk.h
+++ b/lib/utils_disk.h
@@ -28,5 +28,6 @@ void np_add_name (struct name_list **list, const char *name);
 int np_find_name (struct name_list *list, const char *name);
 int np_seen_name (struct name_list *list, const char *name);
 struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name);
+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);
diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index 88a1769..7bd044a 100644
--- a/plugins/check_disk.c
+++ b/plugins/check_disk.c
@@ -121,6 +121,7 @@ static struct mount_entry *mount_list;
 
 int process_arguments (int, char **);
 void print_path (const char *mypath);
+void set_all_thresholds (struct parameter_list *path);
 int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *);
 void print_help (void);
 void print_usage (void);
@@ -148,6 +149,7 @@ char *warn_usedinodes_percent = NULL;
 char *crit_usedinodes_percent = NULL;
 char *warn_freeinodes_percent = NULL;
 char *crit_freeinodes_percent = NULL;
+bool path_selected = false;
 
 
 int
@@ -190,33 +192,34 @@ main (int argc, char **argv)
   /* If a list of paths has not been selected, find entire
      mount list and create list of paths
    */
-  if (! path_select_list) {
+  if (path_selected == false) { 
     for (me = mount_list; me; me = me->me_next) {
-      path = np_add_parameter(&path_select_list, me->me_mountdir);
+      if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) {
+        path = np_add_parameter(&path_select_list, me->me_mountdir);
+      }
       path->best_match = me;
-      set_thresholds(&path->freespace_units, warn_freespace_units, crit_freespace_units);
-      set_thresholds(&path->freespace_percent, warn_freespace_percent, crit_freespace_percent);
-      set_thresholds(&path->usedspace_units, warn_usedspace_units, crit_usedspace_units);
-      set_thresholds(&path->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent);
-      set_thresholds(&path->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent);
-      set_thresholds(&path->freeinodes_percent, warn_freeinodes_percent, crit_freeinodes_percent);
+      set_all_thresholds(path);
     }
-  } else {
-    np_set_best_match(path_select_list, mount_list, exact_match);
+  }
+  np_set_best_match(path_select_list, mount_list, exact_match);
 
-    /* Error if no match found for specified paths */
-    temp_list = path_select_list;
-    while (temp_list) {
-      if (! temp_list->best_match) {
-        die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name);
-      }
-      temp_list = temp_list->name_next;
+  /* Error if no match found for specified paths */
+  temp_list = path_select_list;
+  while (temp_list) {
+    if (! temp_list->best_match) {
+      die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name);
     }
+    temp_list = temp_list->name_next;
   }
+  
 
   /* Process for every path in list */
   for (path = path_select_list; path; path=path->name_next) {
 
+    if (verbose > 3 && path->freespace_percent->warning != NULL && path->freespace_percent->critical != NULL)
+      printf("Thresholds(pct) for %s warn: %f crit %f\n",path->name, path->freespace_percent->warning->end,
+                                                         path->freespace_percent->critical->end);
+
     /* reset disk result */
     disk_result = STATE_UNKNOWN;
 
@@ -548,13 +551,14 @@ process_arguments (int argc, char **argv)
              crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent )) {
         die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -p\n"));
       }
-      se = np_add_parameter(&path_select_list, optarg);
-      set_thresholds(&se->freespace_units, warn_freespace_units, crit_freespace_units);
-      set_thresholds(&se->freespace_percent, warn_freespace_percent, crit_freespace_percent);
-      set_thresholds(&se->usedspace_units, warn_usedspace_units, crit_usedspace_units);
-      set_thresholds(&se->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent);
-      set_thresholds(&se->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent);
-      set_thresholds(&se->freeinodes_percent, warn_freeinodes_percent, crit_freeinodes_percent);
+
+      /* add parameter if not found. overwrite thresholds if path has already been added  */
+      if (! (se = np_find_parameter(path_select_list, optarg))) {
+          se = np_add_parameter(&path_select_list, optarg);
+      }
+
+      set_all_thresholds(se);
+      path_selected = true;
       break;
     case 'x':                 /* exclude path or partition */
       np_add_name(&dp_exclude_list, optarg);
@@ -578,6 +582,17 @@ process_arguments (int argc, char **argv)
       display_mntp = TRUE;
       break;
     case 'C':
+       /* add all mount entries to path_select list if no partitions have been explicitly defined using -p */
+       if (path_selected == false) {
+         struct mount_entry *me;
+         struct parameter_list *path;
+         for (me = mount_list; me; me = me->me_next) {
+           if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) 
+             path = np_add_parameter(&path_select_list, me->me_mountdir);
+           path->best_match = me;
+           set_all_thresholds(path);
+         }
+      }
       warn_freespace_units = NULL;
       crit_freespace_units = NULL;
       warn_usedspace_units = NULL;
@@ -590,6 +605,8 @@ process_arguments (int argc, char **argv)
       crit_usedinodes_percent = NULL;
       warn_freeinodes_percent = NULL;
       crit_freeinodes_percent = NULL;
+    
+      path_selected = false;
       break;
     case 'V':                 /* version */
       print_revision (progname, revision);
@@ -612,12 +629,7 @@ process_arguments (int argc, char **argv)
 
   if (argc > c && path == NULL) {
     se = np_add_parameter(&path_select_list, strdup(argv[c++]));
-    set_thresholds(&se->freespace_units, warn_freespace_units, crit_freespace_units);
-    set_thresholds(&se->freespace_percent, warn_freespace_percent, crit_freespace_percent);
-    set_thresholds(&se->usedspace_units, warn_usedspace_units, crit_usedspace_units);
-    set_thresholds(&se->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent);
-    set_thresholds(&se->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent);
-    set_thresholds(&se->freeinodes_percent, warn_freeinodes_percent, crit_freeinodes_percent);
+    set_all_thresholds(se);
   }
 
   if (units == NULL) {
@@ -665,6 +677,16 @@ print_path (const char *mypath)
 }
 
 
+void
+set_all_thresholds (struct parameter_list *path) 
+{
+    set_thresholds(&path->freespace_units, warn_freespace_units, crit_freespace_units);
+    set_thresholds(&path->freespace_percent, warn_freespace_percent, crit_freespace_percent);
+    set_thresholds(&path->usedspace_units, warn_usedspace_units, crit_usedspace_units);
+    set_thresholds(&path->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent);
+    set_thresholds(&path->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent);
+    set_thresholds(&path->freeinodes_percent, warn_freeinodes_percent, crit_freeinodes_percent);
+}
 
 /* TODO: Remove?
 
-- 
cgit v0.10-9-g596f

