diff options
-rw-r--r-- | lib/parse_ini.c | 41 | ||||
-rw-r--r-- | lib/parse_ini.h | 39 |
2 files changed, 71 insertions, 9 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 70da7f1..e318ec3 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c | |||
@@ -30,6 +30,9 @@ | |||
30 | #include "utils_base.h" | 30 | #include "utils_base.h" |
31 | #include <ctype.h> | 31 | #include <ctype.h> |
32 | 32 | ||
33 | /* FIXME: N::P dies if section is not found */ | ||
34 | /* FIXME: N::P dies if config file is not found */ | ||
35 | |||
33 | /* np_ini_info contains the result of parsing a "locator" in the format | 36 | /* np_ini_info contains the result of parsing a "locator" in the format |
34 | * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example) | 37 | * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example) |
35 | */ | 38 | */ |
@@ -45,16 +48,21 @@ typedef struct { | |||
45 | static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); | 48 | static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); |
46 | /* internal function that converts a single line into options format */ | 49 | /* internal function that converts a single line into options format */ |
47 | static int add_option(FILE *f, np_arg_list **optlst); | 50 | static int add_option(FILE *f, np_arg_list **optlst); |
51 | /* internal function to find default file */ | ||
52 | static char* default_file(void); | ||
48 | 53 | ||
49 | /* parse_locator decomposes a string of the form | 54 | /* parse_locator decomposes a string of the form |
50 | * [stanza][@filename] | 55 | * [stanza][@filename] |
51 | * into its seperate parts | 56 | * into its seperate parts |
52 | */ | 57 | */ |
53 | static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){ | 58 | static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){ |
54 | size_t locator_len, stanza_len; | 59 | size_t locator_len=0, stanza_len=0; |
55 | 60 | ||
56 | locator_len=strlen(locator); | 61 | /* if locator is NULL we'll use default values */ |
57 | stanza_len=strcspn(locator, "@"); | 62 | if(locator){ |
63 | locator_len=strlen(locator); | ||
64 | stanza_len=strcspn(locator, "@"); | ||
65 | } | ||
58 | /* if a non-default stanza is provided */ | 66 | /* if a non-default stanza is provided */ |
59 | if(stanza_len>0){ | 67 | if(stanza_len>0){ |
60 | i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1)); | 68 | i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1)); |
@@ -65,7 +73,7 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in | |||
65 | } | 73 | } |
66 | /* if there is no @file part */ | 74 | /* if there is no @file part */ |
67 | if(stanza_len==locator_len){ | 75 | if(stanza_len==locator_len){ |
68 | i->file=strdup(NP_DEFAULT_INI_PATH); | 76 | i->file=default_file(); |
69 | } else { | 77 | } else { |
70 | i->file=strdup(&(locator[stanza_len+1])); | 78 | i->file=strdup(&(locator[stanza_len+1])); |
71 | } | 79 | } |
@@ -75,7 +83,7 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in | |||
75 | } | 83 | } |
76 | } | 84 | } |
77 | 85 | ||
78 | /* this is the externally visible function used by plugins */ | 86 | /* this is the externally visible function used by extra_opts */ |
79 | np_arg_list* np_get_defaults(const char *locator, const char *default_section){ | 87 | np_arg_list* np_get_defaults(const char *locator, const char *default_section){ |
80 | FILE *inifile=NULL; | 88 | FILE *inifile=NULL; |
81 | np_arg_list *defaults=NULL; | 89 | np_arg_list *defaults=NULL; |
@@ -89,7 +97,7 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){ | |||
89 | } else { | 97 | } else { |
90 | inifile=fopen(i.file, "r"); | 98 | inifile=fopen(i.file, "r"); |
91 | } | 99 | } |
92 | if(inifile==NULL) die(STATE_UNKNOWN, _("Config file error")); | 100 | if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file")); |
93 | if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */ | 101 | if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */ |
94 | /* We got nothing, try the default section */ | 102 | /* We got nothing, try the default section */ |
95 | rewind(inifile); | 103 | rewind(inifile); |
@@ -287,3 +295,24 @@ static int add_option(FILE *f, np_arg_list **optlst){ | |||
287 | return 0; | 295 | return 0; |
288 | } | 296 | } |
289 | 297 | ||
298 | static char* default_file(void){ | ||
299 | char *np_env=NULL; | ||
300 | |||
301 | /* FIXME: STUB */ | ||
302 | return ""; | ||
303 | #if 0 | ||
304 | if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) { | ||
305 | /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in | ||
306 | * every PATHs defined (colon-separated). | ||
307 | */ | ||
308 | } | ||
309 | if !file_found | ||
310 | search NP_DEFAULT_INI_NAGIOS_PATH[1-4] for NP_DEFAULT_INI_FILENAME1; | ||
311 | if !file_found | ||
312 | search NP_DEFAULT_INI_PATH[1-3] for NP_DEFAULT_INI_FILENAME2; | ||
313 | if !file_found | ||
314 | return empty string (or null if we want to die); | ||
315 | return file name; | ||
316 | #endif | ||
317 | } | ||
318 | |||
diff --git a/lib/parse_ini.h b/lib/parse_ini.h index fea745c..61149a2 100644 --- a/lib/parse_ini.h +++ b/lib/parse_ini.h | |||
@@ -13,10 +13,43 @@ typedef struct np_arg_el { | |||
13 | struct np_arg_el *next; | 13 | struct np_arg_el *next; |
14 | } np_arg_list; | 14 | } np_arg_list; |
15 | 15 | ||
16 | /* NP_DEFAULT_INI_PATH: compile-time default location for ini file */ | 16 | /* NP_DEFAULT_INI_PATH: compile-time default location for ini file |
17 | #ifndef NP_DEFAULT_INI_PATH | 17 | #ifndef NP_DEFAULT_INI_PATH |
18 | # define NP_DEFAULT_INI_PATH "/etc/nagios-plugins/plugins.ini" | 18 | # define NP_DEFAULT_INI_PATH "/etc/nagios-plugins.ini" |
19 | #endif /* NP_DEFAULT_INI_PATH */ | 19 | #endif NP_DEFAULT_INI_PATH */ |
20 | |||
21 | /* Filenames (see below) */ | ||
22 | #ifndef NP_DEFAULT_INI_FILENAME1 | ||
23 | # define NP_DEFAULT_INI_FILENAME1 "plugins.ini" | ||
24 | #endif /* NP_DEFAULT_INI_FILENAME1 */ | ||
25 | #ifndef NP_DEFAULT_INI_FILENAME2 | ||
26 | # define NP_DEFAULT_INI_FILENAME2 "nagios-plugins.ini" | ||
27 | #endif /* NP_DEFAULT_INI_FILENAME2 */ | ||
28 | |||
29 | /* Config paths ending in nagios (search for NP_DEFAULT_INI_FILENAME1) */ | ||
30 | #ifndef NP_DEFAULT_INI_NAGIOS_PATH1 | ||
31 | # define NP_DEFAULT_INI_NAGIOS_PATH1 "/etc/nagios" | ||
32 | #endif /* NP_DEFAULT_INI_NAGIOS_PATH1 */ | ||
33 | #ifndef NP_DEFAULT_INI_NAGIOS_PATH2 | ||
34 | # define NP_DEFAULT_INI_NAGIOS_PATH2 "/usr/local/nagios/etc" | ||
35 | #endif /* NP_DEFAULT_INI_NAGIOS_PATH2 */ | ||
36 | #ifndef NP_DEFAULT_INI_NAGIOS_PATH3 | ||
37 | # define NP_DEFAULT_INI_NAGIOS_PATH3 "/usr/local/etc/nagios" | ||
38 | #endif /* NP_DEFAULT_INI_NAGIOS_PATH3 */ | ||
39 | #ifndef NP_DEFAULT_INI_NAGIOS_PATH4 | ||
40 | # define NP_DEFAULT_INI_NAGIOS_PATH4 "/etc/opt/nagios" | ||
41 | #endif /* NP_DEFAULT_INI_NAGIOS_PATH4 */ | ||
42 | |||
43 | /* Config paths not ending in nagios (search for NP_DEFAULT_INI_FILENAME2) */ | ||
44 | #ifndef NP_DEFAULT_INI_PATH1 | ||
45 | # define NP_DEFAULT_INI_PATH1 "/etc" | ||
46 | #endif /* NP_DEFAULT_INI_PATH1 */ | ||
47 | #ifndef NP_DEFAULT_INI_PATH2 | ||
48 | # define NP_DEFAULT_INI_PATH2 "/usr/local/etc" | ||
49 | #endif /* NP_DEFAULT_INI_PATH2 */ | ||
50 | #ifndef NP_DEFAULT_INI_PATH3 | ||
51 | # define NP_DEFAULT_INI_PATH3 "/etc/opt" | ||
52 | #endif /* NP_DEFAULT_INI_PATH3 */ | ||
20 | 53 | ||
21 | /* np_load_defaults: load the default configuration (if present) for | 54 | /* np_load_defaults: load the default configuration (if present) for |
22 | * a plugin from the ini file | 55 | * a plugin from the ini file |