From ee03f1415acc9f4f8901593d9045244fbb3bbbb0 Mon Sep 17 00:00:00 2001
From: Ton Voon <tonvoon@users.sourceforge.net>
Date: Wed, 12 Jul 2006 12:15:42 +0000
Subject: Moving check_disk functions into utils_disk.c and testing them

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

diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 81645b8..28793cd 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -55,7 +55,7 @@ AM_INSTALL_PROGRAM_FLAGS = @INSTALL_OPTS@
 
 check_apt_LDADD = $(BASEOBJS) runcmd.o
 check_dig_LDADD = $(NETLIBS) runcmd.o 
-check_disk_LDADD = $(BASEOBJS) popen.o
+check_disk_LDADD = $(BASEOBJS) popen.o utils_disk.o
 check_dns_LDADD = $(NETLIBS) runcmd.o
 check_dummy_LDADD = $(BASEOBJS)
 check_fping_LDADD = $(NETLIBS) popen.o
@@ -98,7 +98,7 @@ urlize_LDADD = $(BASEOBJS) popen.o
 
 check_apt_DEPENDENCIES = check_apt.c $(BASEOBJS) runcmd.o $(DEPLIBS)
 check_dig_DEPENDENCIES = check_dig.c $(NETOBJS) runcmd.o $(DEPLIBS)
-check_disk_DEPENDENCIES = check_disk.c $(BASEOBJS) popen.o $(DEPLIBS)
+check_disk_DEPENDENCIES = check_disk.c $(BASEOBJS) popen.o utils_disk.o $(DEPLIBS) 
 check_dns_DEPENDENCIES = check_dns.c $(NETOBJS) runcmd.o $(DEPLIBS)
 check_dummy_DEPENDENCIES = check_dummy.c $(DEPLIBS)
 check_fping_DEPENDENCIES = check_fping.c $(NETOBJS) popen.o $(DEPLIBS)
diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index 8a824c1..f0a679d 100644
--- a/plugins/check_disk.c
+++ b/plugins/check_disk.c
@@ -37,10 +37,6 @@ const char *revision = "$Revision$";
 const char *copyright = "1999-2006";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
- /*
-  * Additional inode code by Jorgen Lundman <lundman@lundman.net>
-  */
-
 
 #include "common.h"
 #if HAVE_INTTYPES_H
@@ -50,12 +46,14 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
 #include "popen.h"
 #include "utils.h"
 #include <stdarg.h>
-#include "../lib/fsusage.h"
-#include "../lib/mountlist.h"
+#include "fsusage.h"
+#include "mountlist.h"
 #if HAVE_LIMITS_H
 # include <limits.h>
 #endif
 
+#include "utils_disk.h"
+
 /* If nonzero, show inode information. */
 static int inode_format;
 
@@ -77,8 +75,7 @@ static int show_local_fs = 0;
 /* static int require_sync = 0; */
 
 /* A filesystem type to display. */
-
-struct name_list
+struct parameter_list
 {
   char *name;
   int found;
@@ -89,7 +86,7 @@ struct name_list
   double c_dfp;
   double w_idfp;
   double c_idfp;
-  struct name_list *name_next;
+  struct parameter_list *name_next;
 };
 
 /* Linked list of filesystem types to display.
@@ -103,7 +100,7 @@ struct name_list
    Some filesystem types:
    4.2 4.3 ufs nfs swap ignore io vm efs dbg */
 
-/* static struct name_list *fs_select_list; */
+/* static struct parameter_list *fs_select_list; */
 
 /* Linked list of filesystem types to omit.
    If the list is empty, don't exclude any types.  */
@@ -112,9 +109,7 @@ static struct name_list *fs_exclude_list;
 
 static struct name_list *dp_exclude_list;
 
-static struct name_list *path_select_list;
-
-static struct name_list *dev_select_list;
+static struct parameter_list *path_select_list;
 
 /* Linked list of mounted filesystems. */
 static struct mount_entry *mount_list;
@@ -132,11 +127,14 @@ enum
  #pragma alloca
 #endif
 
+/* Linked list of mounted filesystems. */
+static struct mount_entry *mount_list;
+
 int process_arguments (int, char **);
 void print_path (const char *mypath);
 int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *);
 int check_disk (double usp, uintmax_t free_disk, double uisp);
-int walk_name_list (struct name_list *list, const char *name);
+int walk_parameter_list (struct parameter_list *list, const char *name);
 void print_help (void);
 void print_usage (void);
 
@@ -154,9 +152,6 @@ int verbose = 0;
 int erronly = FALSE;
 int display_mntp = FALSE;
 
-/* Linked list of mounted filesystems. */
-static struct mount_entry *mount_list;
-
 
 
 int
@@ -174,7 +169,7 @@ main (int argc, char **argv)
 
   struct mount_entry *me;
   struct fs_usage fsp;
-  struct name_list *temp_list;
+  struct parameter_list *temp_list;
 
   output = strdup (" - free space:");
   details = strdup ("");
@@ -199,8 +194,8 @@ main (int argc, char **argv)
    */
   if(path_select_list){
     for (me = mount_list; me; me = me->me_next) {
-      walk_name_list(path_select_list, me->me_mountdir);
-      walk_name_list(path_select_list, me->me_devname);
+      walk_parameter_list(path_select_list, me->me_mountdir);
+      walk_parameter_list(path_select_list, me->me_devname);
     }
     /* now pretend we never saw anything, but keep found_len.
      * thus future searches will only match the best match */
@@ -214,12 +209,12 @@ main (int argc, char **argv)
     /* if there's a list of paths to select, the current mount
      * entry matches in path or device name, get fs usage */
     if (path_select_list &&
-         (walk_name_list (path_select_list, me->me_mountdir) ||
-          walk_name_list (path_select_list, me->me_devname) ) ) {
+         (walk_parameter_list (path_select_list, me->me_mountdir) ||
+          walk_parameter_list (path_select_list, me->me_devname) ) ) {
       get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
     /* else if there's a list of paths/devices to select (but
      * we didn't match above) skip to the next mount entry */
-    } else if (dev_select_list || path_select_list) {
+    } else if (path_select_list) {
       continue;
     /* skip remote filesystems if we're not interested in them */
     } else if (me->me_remote && show_local_fs) {
@@ -228,12 +223,12 @@ main (int argc, char **argv)
     } else if (me->me_dummy && !show_all_fs) {
       continue;
     /* skip excluded fstypes */
-    } else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type)) {
+    } else if (fs_exclude_list && np_find_name (fs_exclude_list, me->me_type)) {
       continue;
     /* skip excluded fs's */  
     } else if (dp_exclude_list && 
-             (walk_name_list (dp_exclude_list, me->me_devname) ||
-              walk_name_list (dp_exclude_list, me->me_mountdir))) {
+             (np_find_name (dp_exclude_list, me->me_devname) ||
+              np_find_name (dp_exclude_list, me->me_mountdir))) {
       continue;
     /* otherwise, get fs usage */
     } else {
@@ -312,11 +307,9 @@ int
 process_arguments (int argc, char **argv)
 {
   int c;
-  struct name_list *se;
-  struct name_list **pathtail = &path_select_list;
-  struct name_list **fstail = &fs_exclude_list;
-  struct name_list **dptail = &dp_exclude_list;
-  struct name_list *temp_list;
+  struct parameter_list *se;
+  struct parameter_list **pathtail = &path_select_list;
+  struct parameter_list *temp_list;
   int result = OK;
   struct stat *stat_buf;
 
@@ -351,13 +344,7 @@ process_arguments (int argc, char **argv)
   if (argc < 2)
     return ERROR;
 
-  se = (struct name_list *) malloc (sizeof (struct name_list));
-  se->name = strdup ("iso9660");
-  se->name_next = NULL;
-  se->found = 0;
-  se->found_len = 0;
-  *fstail = se;
-  fstail = &se->name_next;
+  np_add_name(&fs_exclude_list, "iso9660");
 
   for (c = 1; c < argc; c++)
     if (strcmp ("-to", argv[c]) == 0)
@@ -468,7 +455,7 @@ process_arguments (int argc, char **argv)
       show_local_fs = 1;      
       break;
     case 'p':                 /* select path */
-      se = (struct name_list *) malloc (sizeof (struct name_list));
+      se = (struct parameter_list *) malloc (sizeof (struct parameter_list));
       se->name = optarg;
       se->name_next = NULL;
       se->w_df = w_df;
@@ -483,43 +470,10 @@ process_arguments (int argc, char **argv)
       pathtail = &se->name_next;
       break;
     case 'x':                 /* exclude path or partition */
-      se = (struct name_list *) malloc (sizeof (struct name_list));
-      se->name = optarg;
-      se->name_next = NULL;
-
-                        /* If you don't clear the w_fd etc values here, they
-                         * get processed when you walk the list and assigned
-                         * to the global w_df!
-                         */
-                        se->w_df = 0;
-                        se->c_df = 0;
-                        se->w_dfp = 0;
-                        se->c_dfp = 0;
-			se->w_idfp = 0;
-			se->c_idfp = 0;
-      se->found = 0;
-      se->found_len = 0;
-      *dptail = se;
-      dptail = &se->name_next;
+      np_add_name(&dp_exclude_list, optarg);
       break;
     case 'X':                 /* exclude file system type */
-      se = (struct name_list *) malloc (sizeof (struct name_list));
-      se->name = optarg;
-      se->name_next = NULL;
-                        /* If you don't clear the w_fd etc values here, they
-                         * get processed when you walk the list and assigned
-                         * to the global w_df!
-                         */
-                        se->w_df = 0;
-                        se->c_df = 0;
-                        se->w_dfp = 0;
-                        se->c_dfp = 0;
-			se->w_idfp = 0;
-			se->c_idfp = 0;
-      se->found = 0;
-      se->found_len = 0;
-      *fstail = se;
-      fstail = &se->name_next;
+      np_add_name(&fs_exclude_list, optarg);
       break;
     case 'v':                 /* verbose */
       verbose++;
@@ -561,7 +515,7 @@ process_arguments (int argc, char **argv)
     c_dfp = (100.0 - atof (argv[c++]));
 
   if (argc > c && path == NULL) {
-    se = (struct name_list *) malloc (sizeof (struct name_list));
+    se = (struct parameter_list *) malloc (sizeof (struct parameter_list));
     se->name = strdup (argv[c++]);
     se->name_next = NULL;
     se->w_df = w_df;
@@ -683,7 +637,7 @@ check_disk (double usp, uintmax_t free_disk, double uisp)
 
 
 int
-walk_name_list (struct name_list *list, const char *name)
+walk_parameter_list (struct parameter_list *list, const char *name)
 {
   int name_len;
   name_len = strlen(name);
@@ -695,7 +649,7 @@ walk_name_list (struct name_list *list, const char *name)
         ! strncmp(list->name, name, name_len)) {
       list->found = 1;
       list->found_len = name_len;
-      /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
+      /* if required for parameter_lists that have not saved w_df, etc (eg exclude lists) */
       if (list->w_df) w_df = list->w_df;
       if (list->c_df) c_df = list->c_df;
       if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
diff --git a/plugins/tests/Makefile.am b/plugins/tests/Makefile.am
index 8ed3fc1..741499d 100644
--- a/plugins/tests/Makefile.am
+++ b/plugins/tests/Makefile.am
@@ -7,9 +7,9 @@ check_PROGRAMS = @EXTRA_TEST@
 
 INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/intl 
 
-EXTRA_PROGRAMS = test_utils
+EXTRA_PROGRAMS = test_utils test_disk
 
-EXTRA_DIST = test_utils.t
+EXTRA_DIST = test_utils.t test_disk.t
 
 LIBS = @LIBINTL@
 
@@ -18,6 +18,11 @@ test_utils_CFLAGS = -g -I..
 test_utils_LDFLAGS = -L/usr/local/lib -ltap
 test_utils_LDADD = ../utils.o
 
+test_disk_SOURCES = test_disk.c
+test_disk_CFLAGS = -g -I..
+test_disk_LDFLAGS = -L/usr/local/lib -ltap
+test_disk_LDADD = ../utils_disk.o
+
 test: ${noinst_PROGRAMS}
 	perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS)
 
diff --git a/plugins/tests/test_disk.c b/plugins/tests/test_disk.c
new file mode 100644
index 0000000..19cc3ac
--- /dev/null
+++ b/plugins/tests/test_disk.c
@@ -0,0 +1,85 @@
+/******************************************************************************
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+ $Id$
+ 
+******************************************************************************/
+
+#include "common.h"
+#include "utils_disk.h"
+#include "tap.h"
+
+int
+main (int argc, char **argv)
+{
+	struct name_list *exclude_filesystem=NULL;
+	struct name_list *exclude_fstype=NULL;
+
+	plan_tests(8);
+
+	ok( np_find_name(exclude_filesystem, "/var") == FALSE, "/var not in list");
+	np_add_name(&exclude_filesystem, "/var");
+	ok( np_find_name(exclude_filesystem, "/var") == TRUE, "is in list now");
+	ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list");
+	np_add_name(&exclude_filesystem, "/home");
+	ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now");
+	ok( np_find_name(exclude_filesystem, "/var") == TRUE, "/var still in list");
+
+	ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list");
+	np_add_name(&exclude_fstype, "iso9660");
+	ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now");
+
+	ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables");
+
+
+	
+	
+	/*
+	range = parse_range_string("6");
+	ok( range != NULL, "'6' is valid range");
+	ok( range->start == 0, "Start correct");
+	ok( range->start_infinity == FALSE, "Not using negative infinity");
+	ok( range->end == 6, "End correct");
+	ok( range->end_infinity == FALSE, "Not using infinity");
+	free(range);
+
+	range = parse_range_string("-7:23");
+	ok( range != NULL, "'-7:23' is valid range");
+	ok( range->start == -7, "Start correct");
+	ok( range->start_infinity == FALSE, "Not using negative infinity");
+	ok( range->end == 23, "End correct");
+	ok( range->end_infinity == FALSE, "Not using infinity");
+	free(range);
+
+	range = parse_range_string(":5.75");
+	ok( range != NULL, "':5.75' is valid range");
+	ok( range->start == 0, "Start correct");
+	ok( range->start_infinity == FALSE, "Not using negative infinity");
+	ok( range->end == 5.75, "End correct");
+	ok( range->end_infinity == FALSE, "Not using infinity");
+	free(range);
+
+	range = parse_range_string("~:-95.99");
+	ok( range != NULL, "~:-95.99' is valid range");
+	ok( range->start_infinity == TRUE, "Using negative infinity");
+	ok( range->end == -95.99, "End correct (with rounding errors)");
+	ok( range->end_infinity == FALSE, "Not using infinity");
+	free(range);
+	*/
+
+	return exit_status();
+}
+
diff --git a/plugins/tests/test_disk.t b/plugins/tests/test_disk.t
new file mode 100755
index 0000000..d32567a
--- /dev/null
+++ b/plugins/tests/test_disk.t
@@ -0,0 +1,6 @@
+#!/usr/bin/perl
+use Test::More;
+if (! -e "./test_disk") {
+	plan skip_all => "./test_disk not compiled - please install tap library to test";
+}
+exec "./test_disk";
diff --git a/plugins/utils_disk.c b/plugins/utils_disk.c
new file mode 100644
index 0000000..6380df3
--- /dev/null
+++ b/plugins/utils_disk.c
@@ -0,0 +1,62 @@
+/****************************************************************************
+* Utils for check_disk
+*
+* License: GPL
+* Copyright (c) 1999-2006 nagios-plugins team
+*
+* Last Modified: $Date$
+*
+* Description:
+*
+* This file contains utilities for check_disk. These are tested by libtap
+*
+* License Information:
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation; either version 2 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*
+* $Id$
+* 
+*****************************************************************************/
+
+#include "common.h"
+#include "utils_disk.h"
+
+void
+np_add_name (struct name_list **list, const char *name)
+{
+  struct name_list *new_entry;
+  new_entry = (struct name_list *) malloc (sizeof *new_entry);
+  new_entry->name = (char *) name;
+  new_entry->next = *list;
+  *list = new_entry;
+}
+
+/* Returns TRUE if name is in list */
+int
+np_find_name (struct name_list *list, const char *name)
+{
+  const struct name_list *n;
+
+  if (list == NULL || name == NULL) {
+    return FALSE;
+  }
+  for (n = list; n; n = n->next) {
+    if (!strcmp(name, n->name)) {
+      return TRUE;
+    }
+  }
+  return FALSE;
+}
+
diff --git a/plugins/utils_disk.h b/plugins/utils_disk.h
new file mode 100644
index 0000000..f173c91
--- /dev/null
+++ b/plugins/utils_disk.h
@@ -0,0 +1,12 @@
+/* Header file for utils_disk */
+
+
+struct name_list
+{
+  char *name;
+  struct name_list *next;
+};
+
+void np_add_name (struct name_list **list, const char *name);
+int np_find_name (struct name_list *list, const char *name);
+
-- 
cgit v0.10-9-g596f

