summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 3419b16)
raw | patch | inline | side by side (parent: 3419b16)
author | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | |
Mon, 24 Mar 2008 06:28:46 +0000 (06:28 +0000) | ||
committer | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | |
Mon, 24 Mar 2008 06:28:46 +0000 (06:28 +0000) |
Allow NULL locator (default file/section)
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1960 f882894a-f735-0410-b71e-b25c423dba1c
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1960 f882894a-f735-0410-b71e-b25c423dba1c
lib/parse_ini.c | patch | blob | history | |
lib/parse_ini.h | patch | blob | history |
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index 70da7f14dc7570a6b4cc0156e33c66dbacb38cac..e318ec32155b99384d79c6e2d455effba92a6446 100644 (file)
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
#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)
*/
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));
}
/* 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]));
}
}
}
-/* 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;
} 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);
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
+}
+
diff --git a/lib/parse_ini.h b/lib/parse_ini.h
index fea745c51973538afbb807d53d111b1d98fcac1c..61149a216d1a6b33467b496d118e74bab8c6a3f5 100644 (file)
--- a/lib/parse_ini.h
+++ b/lib/parse_ini.h
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