Code

Fixed typo in help for -M
[nagiosplug.git] / plugins / check_disk.c
index b966fabe50639e5b16127a6fe14e8767c8ab5248..88a176926f1454b82697d667e93c4599f9f766bc 100644 (file)
@@ -39,6 +39,9 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 
 #include "common.h"
+#ifdef HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
 #if HAVE_INTTYPES_H
 # include <inttypes.h>
 #endif
@@ -121,6 +124,7 @@ void print_path (const char *mypath);
 int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *);
 void print_help (void);
 void print_usage (void);
+double calculate_percent(uintmax_t, uintmax_t);
 
 double w_dfp = -1.0;
 double c_dfp = -1.0;
@@ -142,6 +146,8 @@ char *warn_usedspace_percent = NULL;
 char *crit_usedspace_percent = NULL;
 char *warn_usedinodes_percent = NULL;
 char *crit_usedinodes_percent = NULL;
+char *warn_freeinodes_percent = NULL;
+char *crit_freeinodes_percent = NULL;
 
 
 int
@@ -152,11 +158,14 @@ main (int argc, char **argv)
   char *output;
   char *details;
   char *perf;
-  float inode_space_pct;
+  char *preamble;
+  double inode_space_pct;
   uintmax_t total, available, available_to_root, used;
   double dfree_pct = -1, dused_pct = -1;
   double dused_units, dfree_units, dtotal_units;
-  double dused_inodes_percent;
+  double dused_inodes_percent, dfree_inodes_percent;
+  double warning_high_tide;
+  double critical_high_tide;
   int temp_result;
 
   struct mount_entry *me;
@@ -164,7 +173,8 @@ main (int argc, char **argv)
   struct parameter_list *temp_list, *path;
   struct name_list *seen = NULL;
 
-  output = strdup (" - free space:");
+  preamble = strdup (" - free space:");
+  output = strdup ("");
   details = strdup ("");
   perf = strdup ("");
 
@@ -189,6 +199,7 @@ main (int argc, char **argv)
       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);
     }
   } else {
     np_set_best_match(path_select_list, mount_list, exact_match);
@@ -205,6 +216,10 @@ main (int argc, char **argv)
 
   /* Process for every path in list */
   for (path = path_select_list; path; path=path->name_next) {
+
+    /* reset disk result */
+    disk_result = STATE_UNKNOWN;
+
     me = path->best_match;
 
     /* Filters */
@@ -239,91 +254,96 @@ main (int argc, char **argv)
       available_to_root = fsp.fsu_bfree;
       used = total - available_to_root;
 
-      /* I don't understand the below, but it is taken from coreutils' df */
-      /* Is setting dused_pct, in the best possible way */
-      if (used <= TYPE_MAXIMUM(uintmax_t) / 100) {
-        uintmax_t u100 = used * 100;
-        uintmax_t nonroot_total = used + available;
-        dused_pct = u100 / nonroot_total + (u100 % nonroot_total != 0);
-      } else {
-        /* Possible rounding errors - see coreutils' df for more explanation */
-        double u = used;
-        double a = available;
-        double nonroot_total = u + a;
-        if (nonroot_total) {
-          long int lipct = dused_pct = u * 100 / nonroot_total;
-          double ipct = lipct;
-
-          /* Like 'pct = ceil (dpct);', but without ceil - from coreutils again */
-          if (ipct - 1 < dused_pct && dused_pct <= ipct + 1)
-            dused_pct = ipct + (ipct < dused_pct);
-        }
-      }
-
+      dused_pct = calculate_percent( used, used + available ); /* used + available can never be > uintmax */
+     
       dfree_pct = 100 - dused_pct;
       dused_units = used*fsp.fsu_blocksize/mult;
       dfree_units = available*fsp.fsu_blocksize/mult;
       dtotal_units = total*fsp.fsu_blocksize/mult;
-      dused_inodes_percent = (fsp.fsu_files - fsp.fsu_ffree) * 100 / fsp.fsu_files;
+      dused_inodes_percent = calculate_percent(fsp.fsu_files - fsp.fsu_ffree, fsp.fsu_files);
+      dfree_inodes_percent = 100 - dused_inodes_percent;
 
       if (verbose >= 3) {
-        printf ("For %s, used_pct=%g free_pct=%g used_units=%g free_units=%g total_units=%g used_inodes_pct=%g\n", 
-          me->me_mountdir, dused_pct, dfree_pct, dused_units, dfree_units, dtotal_units, dused_inodes_percent);
+        printf ("For %s, used_pct=%g free_pct=%g used_units=%g free_units=%g total_units=%g used_inodes_pct=%g free_inodes_pct=%g\n", 
+          me->me_mountdir, dused_pct, dfree_pct, dused_units, dfree_units, dtotal_units, dused_inodes_percent, dfree_inodes_percent);
       }
 
       /* Threshold comparisons */
 
       temp_result = get_status(dfree_units, path->freespace_units);
       if (verbose >=3) printf("Freespace_units result=%d\n", temp_result);
-      result = max_state( result, temp_result );
+      disk_result = max_state( disk_result, temp_result );
 
       temp_result = get_status(dfree_pct, path->freespace_percent);
       if (verbose >=3) printf("Freespace%% result=%d\n", temp_result);
-      result = max_state( result, temp_result );
+      disk_result = max_state( disk_result, temp_result );
 
       temp_result = get_status(dused_units, path->usedspace_units);
       if (verbose >=3) printf("Usedspace_units result=%d\n", temp_result);
-      result = max_state( result, temp_result );
+      disk_result = max_state( disk_result, temp_result );
 
       temp_result = get_status(dused_pct, path->usedspace_percent);
       if (verbose >=3) printf("Usedspace_percent result=%d\n", temp_result);
-      result = max_state( result, temp_result );
+      disk_result = max_state( disk_result, temp_result );
 
       temp_result = get_status(dused_inodes_percent, path->usedinodes_percent);
       if (verbose >=3) printf("Usedinodes_percent result=%d\n", temp_result);
-      result = max_state( result, temp_result );
+      disk_result = max_state( disk_result, temp_result );
 
-      
+      temp_result = get_status(dfree_inodes_percent, path->freeinodes_percent);
+      if (verbose >=3) printf("Freeinodes_percent result=%d\n", temp_result);
+      disk_result = max_state( disk_result, temp_result );
 
+      result = max_state(result, disk_result);
 
+      /* What a mess of units. The output shows free space, the perf data shows used space. Yikes!
+         Hack here. Trying to get warn/crit levels from freespace_(units|percent) for perf
+         data. Assumption that start=0. Roll on new syntax...
+      */
 
-                        /* Moved this computation up here so we can add it
-                         * to perf */
-                        inode_space_pct = (float)fsp.fsu_ffree*100/fsp.fsu_files;
+      /* *_high_tide must be reinitialized at each run */
+      warning_high_tide = UINT_MAX;
+      critical_high_tide = UINT_MAX;
 
+      if (path->freespace_units->warning != NULL) {
+        warning_high_tide = dtotal_units - path->freespace_units->warning->end;
+      }
+      if (path->freespace_percent->warning != NULL) {
+        warning_high_tide = abs( min( (double) warning_high_tide, (double) (1.0 - path->freespace_percent->warning->end/100)*dtotal_units ));
+      }
+      if (path->freespace_units->critical != NULL) {
+        critical_high_tide = dtotal_units - path->freespace_units->critical->end;
+      }
+      if (path->freespace_percent->critical != NULL) {
+        critical_high_tide = abs( min( (double) critical_high_tide, (double) (1.0 - path->freespace_percent->critical->end/100)*dtotal_units ));
+      }
 
       asprintf (&perf, "%s %s", perf,
                 perfdata ((!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
                           dused_units, units,
-                          FALSE, 0, /* min ((uintmax_t)dtotal_units-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*dtotal_units)), */
-                          FALSE, 0, /* min ((uintmax_t)dtotal_units-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*dtotal_units)), */
-                          FALSE, 0, /* inode_space_pct, */
-                          FALSE, 0));; /* dtotal_units)); */
+                         (warning_high_tide != UINT_MAX ? TRUE : FALSE), warning_high_tide,
+                         (critical_high_tide != UINT_MAX ? TRUE : FALSE), critical_high_tide,
+                         TRUE, 0,
+                         TRUE, dtotal_units));
 
       if (disk_result==STATE_OK && erronly && !verbose)
         continue;
 
       if (disk_result!=STATE_OK || verbose>=0) {
-        asprintf (&output, ("%s %s %.0f %s (%.0f%% inode=%.0f%%);"),
+        asprintf (&output, "%s %s %.0f %s (%.0f%%",
                   output,
                   (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
                   dfree_units,
                   units,
-            dfree_pct,
-            inode_space_pct);
+                  dfree_pct);
+        if (dused_inodes_percent < 0) {
+          asprintf(&output, "%s inode=-);", output);
+        } else {
+          asprintf(&output, "%s inode=%.0f%%);", output, dfree_inodes_percent );
+        }
       }
 
-      /* Need to do a similar one
+      /* TODO: Need to do a similar debug line
       asprintf (&details, _("%s\n\
 %.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
                 details, dfree_units, dtotal_units, units, dfree_pct, inode_space_pct,
@@ -339,11 +359,34 @@ main (int argc, char **argv)
     asprintf (&output, "%s%s", output, details);
 
 
-  printf ("DISK %s%s|%s\n", state_text (result), output, perf);
+  printf ("DISK %s%s%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf);
   return result;
 }
 
 
+double calculate_percent(uintmax_t value, uintmax_t total) {
+  double pct = -1;
+  /* I don't understand the below, but it is taken from coreutils' df */
+  /* Seems to be calculating pct, in the best possible way */
+  if (value <= TYPE_MAXIMUM(uintmax_t) / 100 
+    && total != 0) {
+    uintmax_t u100 = value * 100;
+    pct = u100 / total + (u100 % total != 0);
+  } else {
+    /* Possible rounding errors - see coreutils' df for more explanation */
+    double u = value;
+    double t = total;
+    if (t) {
+      long int lipct = pct = u * 100 / t;
+      double ipct = lipct;
+
+      /* Like 'pct = ceil (dpct);', but without ceil - from coreutils again */
+      if (ipct - 1 < pct && pct <= ipct + 1)
+        pct = ipct + (ipct < pct);
+    }
+  }
+  return pct;
+}
 
 /* process command-line arguments */
 int
@@ -446,10 +489,18 @@ process_arguments (int argc, char **argv)
       break;
 
     case 'W':                  /* warning inode threshold */
-      warn_usedinodes_percent = optarg;
+      if (*optarg == '@') {
+        warn_freeinodes_percent = optarg;
+      } else {
+        asprintf(&warn_freeinodes_percent, "@%s", optarg);
+      }
       break;
     case 'K':                  /* critical inode threshold */
-      crit_usedinodes_percent = optarg;
+      if (*optarg == '@') {
+        crit_freeinodes_percent = optarg;
+      } else {
+        asprintf(&crit_freeinodes_percent, "@%s", optarg);
+      }
       break;
     case 'u':
       if (units)
@@ -494,7 +545,7 @@ process_arguments (int argc, char **argv)
       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)) {
+             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);
@@ -503,6 +554,7 @@ process_arguments (int argc, char **argv)
       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);
       break;
     case 'x':                 /* exclude path or partition */
       np_add_name(&dp_exclude_list, optarg);
@@ -536,6 +588,8 @@ process_arguments (int argc, char **argv)
       crit_usedspace_percent = NULL;
       warn_usedinodes_percent = NULL;
       crit_usedinodes_percent = NULL;
+      warn_freeinodes_percent = NULL;
+      crit_freeinodes_percent = NULL;
       break;
     case 'V':                 /* version */
       print_revision (progname, revision);
@@ -563,6 +617,7 @@ process_arguments (int argc, char **argv)
     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);
   }
 
   if (units == NULL) {
@@ -607,8 +662,6 @@ print_path (const char *mypath)
     printf ("\n");
   else
     printf (_(" for %s\n"), mypath);
-
-  //return;
 }
 
 
@@ -701,9 +754,9 @@ print_help (void)
   printf ("    %s\n", _("Path or partition (may be repeated)"));
   printf (" %s\n", "-x, --exclude_device=PATH <STRING>");
   printf ("    %s\n", _("Ignore device (only works if -p unspecified)"));
-  printf (" %s\n", _("-X, --exclude-type=TYPE <STRING>"));
+  printf (" %s\n", "-X, --exclude-type=TYPE <STRING>");
   printf ("    %s\n", _("Ignore all filesystems of indicated type (may be repeated)"));
-  printf (" %s\n", "-m, --mountpoint");
+  printf (" %s\n", "-M, --mountpoint");
   printf ("    %s\n", _("Display the mountpoint instead of the partition"));
   printf (" %s\n", "-E, --exact-match");
   printf ("    %s\n", _("For paths or partitions specified with -p, only check for exact paths"));