[Nagiosplug-checkins] SF.net SVN: nagiosplug: [1962] nagiosplug/trunk/lib
dermoth at users.sourceforge.net
dermoth at users.sourceforge.net
Wed Mar 26 10:46:28 CET 2008
Revision: 1962
http://nagiosplug.svn.sourceforge.net/nagiosplug/?rev=1962&view=rev
Author: dermoth
Date: 2008-03-26 02:46:27 -0700 (Wed, 26 Mar 2008)
Log Message:
-----------
write ini-file lookup function + tests
Modified Paths:
--------------
nagiosplug/trunk/lib/parse_ini.c
nagiosplug/trunk/lib/parse_ini.h
nagiosplug/trunk/lib/tests/test_opts.c
Added Paths:
-----------
nagiosplug/trunk/lib/tests/plugins.ini
Modified: nagiosplug/trunk/lib/parse_ini.c
===================================================================
--- nagiosplug/trunk/lib/parse_ini.c 2008-03-24 07:02:28 UTC (rev 1961)
+++ nagiosplug/trunk/lib/parse_ini.c 2008-03-26 09:46:27 UTC (rev 1962)
@@ -30,6 +30,10 @@
#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 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 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);
}
- 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
+
+ /* 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);
+ }
+
+ /* 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;
+}
+
Modified: nagiosplug/trunk/lib/parse_ini.h
===================================================================
--- nagiosplug/trunk/lib/parse_ini.h 2008-03-24 07:02:28 UTC (rev 1961)
+++ nagiosplug/trunk/lib/parse_ini.h 2008-03-26 09:46:27 UTC (rev 1962)
@@ -13,10 +13,12 @@
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
Added: nagiosplug/trunk/lib/tests/plugins.ini
===================================================================
--- nagiosplug/trunk/lib/tests/plugins.ini (rev 0)
+++ nagiosplug/trunk/lib/tests/plugins.ini 2008-03-26 09:46:27 UTC (rev 1962)
@@ -0,0 +1,7 @@
+
+[check_disk]
+foo=bar
+
+[section1]
+foobar=baz
+
Modified: nagiosplug/trunk/lib/tests/test_opts.c
===================================================================
--- nagiosplug/trunk/lib/tests/test_opts.c 2008-03-24 07:02:28 UTC (rev 1961)
+++ nagiosplug/trunk/lib/tests/test_opts.c 2008-03-26 09:46:27 UTC (rev 1962)
@@ -59,7 +59,7 @@
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 @@
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();
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the Commits
mailing list