Code

write ini-file lookup function + tests
authorThomas Guyot-Sionnest <dermoth@users.sourceforge.net>
Wed, 26 Mar 2008 09:46:27 +0000 (09:46 +0000)
committerThomas Guyot-Sionnest <dermoth@users.sourceforge.net>
Wed, 26 Mar 2008 09:46:27 +0000 (09:46 +0000)
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1962 f882894a-f735-0410-b71e-b25c423dba1c

lib/parse_ini.c
lib/parse_ini.h
lib/tests/plugins.ini [new file with mode: 0644]
lib/tests/test_opts.c

index e318ec32155b99384d79c6e2d455effba92a6446..67d8367f5503c21a7fd5cd3217bcdd60f055ac6b 100644 (file)
 #include "utils_base.h"
 #include <ctype.h>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
 /* FIXME: N::P dies if section is not found */
 /* FIXME: N::P dies if config file is not found */
 
@@ -50,6 +54,8 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
 static int add_option(FILE *f, np_arg_list **optlst);
 /* internal function to find default file */
 static char* default_file(void);
+/* internal function to stat() files */
+static int test_file(const char* env, int len, const char* file, char* temp_file);
 
 /* parse_locator decomposes a string of the form
  *     [stanza][@filename]
@@ -296,23 +302,71 @@ static int add_option(FILE *f, np_arg_list **optlst){
 }
 
 static char* default_file(void){
-       char *np_env=NULL;
+       struct stat sb;
+       char *np_env=NULL, *default_file=NULL;
+       char temp_file[MAX_INPUT_BUFFER];
+       size_t len;
 
-       /* FIXME: STUB */
-       return "";
-#if 0
        if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
+               /* skip ant starting colon... */
+               while(*np_env==':') np_env++;
                /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
                 * every PATHs defined (colon-separated).
                 */
+               while((len=strcspn(np_env,":"))>0){
+                       /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */
+                       if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
+                          test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){
+                               default_file=strdup(temp_file);
+                               break;
+                       }
+
+                       /* Move on to the next token */
+                       np_env+=len;
+                       while(*np_env==':') np_env++;
+               } /* while(...) */
+       } /* if(getenv("NAGIOS_CONFIG_PATH")) */
+
+       /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */
+       if(!default_file){
+               if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
+                  test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
+                  test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
+                  test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1)
+                       default_file=strdup(temp_file);
+       }
+
+       /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */
+       if(!default_file){
+               if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
+                  test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
+                  test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1)
+                       default_file=strdup(temp_file);
        }
-       if !file_found
-               search NP_DEFAULT_INI_NAGIOS_PATH[1-4] for NP_DEFAULT_INI_FILENAME1;
-       if !file_found
-               search NP_DEFAULT_INI_PATH[1-3] for NP_DEFAULT_INI_FILENAME2;
-       if !file_found
-               return empty string (or null if we want to die);
-       return file name;
-#endif
+
+       /* Return default_file or empty string (should return NULL if we want to
+        * die there...
+        */
+       if(default_file)
+               return default_file;
+       return "";
+}
+
+/* put together len bytes from env and the filename and test for its
+ * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed.
+ */
+static int test_file(const char* env, int len, const char* file, char* temp_file){
+       struct stat sb;
+
+       /* test for len + filelen + '/' + '\0' */
+       if((len+strlen(file)+2)>MAX_INPUT_BUFFER)       return -1;
+
+       strncpy(temp_file,env,len);
+       temp_file[len]='\0';
+       strncat(temp_file,"/",len+1);
+       strncat(temp_file,file,len+strlen(file)+1);
+
+       if(stat(temp_file, &sb) != -1) return 1;
+       return 0;
 }
 
index 61149a216d1a6b33467b496d118e74bab8c6a3f5..309dce5213fecf3214fb6d9e6cfa5a670c3b17c9 100644 (file)
@@ -13,10 +13,12 @@ typedef struct np_arg_el {
        struct np_arg_el *next;
 } np_arg_list;
 
-/* NP_DEFAULT_INI_PATH: compile-time default location for ini file
-#ifndef NP_DEFAULT_INI_PATH
-# define NP_DEFAULT_INI_PATH "/etc/nagios-plugins.ini"
-#endif NP_DEFAULT_INI_PATH */
+/* FIXME: This is in plugins/common.c. Should be eventually moved to lib/
+ * (although for this particular one  a configure settings should be ideal)
+ */
+#ifndef MAX_INPUT_BUFFER
+# define MAX_INPUT_BUFFER 8192
+#endif /* MAX_INPUT_BUFFER */
 
 /* Filenames (see below) */
 #ifndef NP_DEFAULT_INI_FILENAME1
diff --git a/lib/tests/plugins.ini b/lib/tests/plugins.ini
new file mode 100644 (file)
index 0000000..5e20ded
--- /dev/null
@@ -0,0 +1,7 @@
+
+[check_disk]
+foo=bar
+
+[section1]
+foobar=baz
+
index 12726059c252108e5de501e18c3b983c950c6e5a..769a2df2c1ffeddb8abd873720f99cd78c5799ef 100644 (file)
@@ -59,7 +59,7 @@ main (int argc, char **argv)
        char **argv_test=NULL, **argv_known=NULL;
        int i, argc_test, argc_new;
 
-       plan_tests(8);
+       plan_tests(11);
 
        argv_test=(char **)malloc(2*sizeof(char **));
        argv_test[0] = "prog_name";
@@ -203,6 +203,64 @@ main (int argc, char **argv)
        ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 3");
        my_free(&argc_new,argv_test);
 
+       setenv("NAGIOS_CONFIG_PATH", ".", 1);
+       argv_test=(char **)malloc(6*sizeof(char **));
+       argv_test[0] = "prog_name";
+       argv_test[1] = "arg1";
+       argv_test[2] = "--extra-opts";
+       argv_test[3] = "--arg3";
+       argv_test[4] = "val2";
+       argv_test[5] = NULL;
+       argc_test=5;
+       argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
+       argv_known[0] = "prog_name";
+       argv_known[1] = "--foo=bar";
+       argv_known[2] = "arg1";
+       argv_known[3] = "--arg3";
+       argv_known[4] = "val2";
+       argv_known[5] = NULL;
+       argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
+       ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 1");
+       my_free(&argc_new,argv_test);
+
+       argv_test=(char **)malloc(6*sizeof(char **));
+       argv_test[0] = "prog_name";
+       argv_test[1] = "arg1";
+       argv_test[2] = "--extra-opts=section1";
+       argv_test[3] = "--arg3";
+       argv_test[4] = "val2";
+       argv_test[5] = NULL;
+       argc_test=5;
+       argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
+       argv_known[0] = "prog_name";
+       argv_known[1] = "--foobar=baz";
+       argv_known[2] = "arg1";
+       argv_known[3] = "--arg3";
+       argv_known[4] = "val2";
+       argv_known[5] = NULL;
+       argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
+       ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 2");
+       my_free(&argc_new,argv_test);
+
+       argv_test=(char **)malloc(6*sizeof(char **));
+       argv_test[0] = "prog_name";
+       argv_test[1] = "arg1";
+       argv_test[2] = "--extra-opts=section2";
+       argv_test[3] = "--arg3";
+       argv_test[4] = "val2";
+       argv_test[5] = NULL;
+       argc_test=5;
+       argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
+       argv_known[0] = "prog_name";
+       argv_known[1] = "--foo=bar";
+       argv_known[2] = "arg1";
+       argv_known[3] = "--arg3";
+       argv_known[4] = "val2";
+       argv_known[5] = NULL;
+       argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
+       ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 3");
+       my_free(&argc_new,argv_test);
+
        return exit_status();
 }