[Nagiosplug-checkins] SF.net SVN: nagiosplug: [1960] nagiosplug/trunk/lib

dermoth at users.sourceforge.net dermoth at users.sourceforge.net
Mon Mar 24 07:28:47 CET 2008


Revision: 1960
          http://nagiosplug.svn.sourceforge.net/nagiosplug/?rev=1960&view=rev
Author:   dermoth
Date:     2008-03-23 23:28:46 -0700 (Sun, 23 Mar 2008)

Log Message:
-----------
Replace broken usage of NAGIOS_CONFIG_PATH with a stub function (that will try to find a config file in the future...)
Allow NULL locator (default file/section)

Modified Paths:
--------------
    nagiosplug/trunk/lib/parse_ini.c
    nagiosplug/trunk/lib/parse_ini.h

Modified: nagiosplug/trunk/lib/parse_ini.c
===================================================================
--- nagiosplug/trunk/lib/parse_ini.c	2008-03-20 11:53:29 UTC (rev 1959)
+++ nagiosplug/trunk/lib/parse_ini.c	2008-03-24 06:28:46 UTC (rev 1960)
@@ -30,6 +30,9 @@
 #include "utils_base.h"
 #include <ctype.h>
 
+/* FIXME: N::P dies if section is not found */
+/* FIXME: N::P dies if config file is not found */
+
 /* np_ini_info contains the result of parsing a "locator" in the format
  * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
  */
@@ -45,16 +48,21 @@
 static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
 /* internal function that converts a single line into options format */
 static int add_option(FILE *f, np_arg_list **optlst);
+/* internal function to find default file */
+static char* default_file(void);
 
 /* parse_locator decomposes a string of the form
  * 	[stanza][@filename]
  * into its seperate parts
  */
 static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
-	size_t locator_len, stanza_len;
+	size_t locator_len=0, stanza_len=0;
 
-	locator_len=strlen(locator);
-	stanza_len=strcspn(locator, "@");
+	/* if locator is NULL we'll use default values */
+	if(locator){
+		locator_len=strlen(locator);
+		stanza_len=strcspn(locator, "@");
+	}
 	/* if a non-default stanza is provided */
 	if(stanza_len>0){
 		i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
@@ -65,7 +73,7 @@
 	}
 	/* if there is no @file part */
 	if(stanza_len==locator_len){
-		i->file=strdup(NP_DEFAULT_INI_PATH);
+		i->file=default_file();
 	} else {
 		i->file=strdup(&(locator[stanza_len+1]));
 	}
@@ -75,7 +83,7 @@
 	}
 }
 
-/* this is the externally visible function used by plugins */
+/* this is the externally visible function used by extra_opts */
 np_arg_list* np_get_defaults(const char *locator, const char *default_section){
 	FILE *inifile=NULL;
 	np_arg_list *defaults=NULL;
@@ -89,7 +97,7 @@
 		} else {
 			inifile=fopen(i.file, "r");
 		}
-		if(inifile==NULL) die(STATE_UNKNOWN, _("Config file error"));
+		if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
 		if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */
 			/* We got nothing, try the default section */
 			rewind(inifile);
@@ -287,3 +295,24 @@
 	return 0;
 }
 
+static char* default_file(void){
+	char *np_env=NULL;
+
+	/* FIXME: STUB */
+	return "";
+#if 0
+	if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
+		/* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
+		 * every PATHs defined (colon-separated).
+		 */
+	}
+	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
+}
+

Modified: nagiosplug/trunk/lib/parse_ini.h
===================================================================
--- nagiosplug/trunk/lib/parse_ini.h	2008-03-20 11:53:29 UTC (rev 1959)
+++ nagiosplug/trunk/lib/parse_ini.h	2008-03-24 06:28:46 UTC (rev 1960)
@@ -13,11 +13,44 @@
 	struct np_arg_el *next;
 } np_arg_list;
 
-/* NP_DEFAULT_INI_PATH: compile-time default location for ini file */
+/* NP_DEFAULT_INI_PATH: compile-time default location for ini file
 #ifndef NP_DEFAULT_INI_PATH
-# define NP_DEFAULT_INI_PATH "/etc/nagios-plugins/plugins.ini"
-#endif /* NP_DEFAULT_INI_PATH */
+# define NP_DEFAULT_INI_PATH "/etc/nagios-plugins.ini"
+#endif NP_DEFAULT_INI_PATH */
 
+/* Filenames (see below) */
+#ifndef NP_DEFAULT_INI_FILENAME1
+# define NP_DEFAULT_INI_FILENAME1 "plugins.ini"
+#endif /* NP_DEFAULT_INI_FILENAME1 */
+#ifndef NP_DEFAULT_INI_FILENAME2
+# define NP_DEFAULT_INI_FILENAME2 "nagios-plugins.ini"
+#endif /* NP_DEFAULT_INI_FILENAME2 */
+
+/* Config paths ending in nagios (search for NP_DEFAULT_INI_FILENAME1) */
+#ifndef NP_DEFAULT_INI_NAGIOS_PATH1
+# define NP_DEFAULT_INI_NAGIOS_PATH1 "/etc/nagios"
+#endif /* NP_DEFAULT_INI_NAGIOS_PATH1 */
+#ifndef NP_DEFAULT_INI_NAGIOS_PATH2
+# define NP_DEFAULT_INI_NAGIOS_PATH2 "/usr/local/nagios/etc"
+#endif /* NP_DEFAULT_INI_NAGIOS_PATH2 */
+#ifndef NP_DEFAULT_INI_NAGIOS_PATH3
+# define NP_DEFAULT_INI_NAGIOS_PATH3 "/usr/local/etc/nagios"
+#endif /* NP_DEFAULT_INI_NAGIOS_PATH3 */
+#ifndef NP_DEFAULT_INI_NAGIOS_PATH4
+# define NP_DEFAULT_INI_NAGIOS_PATH4 "/etc/opt/nagios"
+#endif /* NP_DEFAULT_INI_NAGIOS_PATH4 */
+
+/* Config paths not ending in nagios (search for NP_DEFAULT_INI_FILENAME2) */
+#ifndef NP_DEFAULT_INI_PATH1
+# define NP_DEFAULT_INI_PATH1 "/etc"
+#endif /* NP_DEFAULT_INI_PATH1 */
+#ifndef NP_DEFAULT_INI_PATH2
+# define NP_DEFAULT_INI_PATH2 "/usr/local/etc"
+#endif /* NP_DEFAULT_INI_PATH2 */
+#ifndef NP_DEFAULT_INI_PATH3
+# define NP_DEFAULT_INI_PATH3 "/etc/opt"
+#endif /* NP_DEFAULT_INI_PATH3 */
+
 /* np_load_defaults: load the default configuration (if present) for
  * a plugin from the ini file
  */


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