X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fconfigfile.c;h=ac5e8edcb94f2bfda66c5eefab67dfab3c66e6db;hb=0f9c7ce19f32c64ae54f0132763d9f38cf57b5b1;hp=b09bee8dbf66997ab96bea8ef33740d6d4ae362c;hpb=ec36b4bdca9dbe957f39bef619cdc8c48103a86c;p=collectd.git diff --git a/src/configfile.c b/src/configfile.c index b09bee8d..ac5e8edc 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -35,6 +35,14 @@ # include #endif /* HAVE_WORDEXP_H */ +#if HAVE_FNMATCH_H +# include +#endif /* HAVE_FNMATCH_H */ + +#if HAVE_LIBGEN_H +# include +#endif /* HAVE_LIBGEN_H */ + #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str)) /* @@ -90,7 +98,7 @@ static cf_value_map_t cf_value_map[] = {"PluginDir", dispatch_value_plugindir}, {"LoadPlugin", dispatch_loadplugin} }; -static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map); +static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map); static cf_global_option_t cf_global_options[] = { @@ -98,13 +106,14 @@ static cf_global_option_t cf_global_options[] = {"PIDFile", NULL, PIDFILE}, {"Hostname", NULL, NULL}, {"FQDNLookup", NULL, "true"}, - {"Interval", NULL, "10"}, + {"Interval", NULL, NULL}, {"ReadThreads", NULL, "5"}, + {"WriteThreads", NULL, "5"}, {"Timeout", NULL, "2"}, {"PreCacheChain", NULL, "PreCache"}, {"PostCacheChain", NULL, "PostCache"} }; -static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options); +static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options); static int cf_default_typesdb = 1; @@ -265,7 +274,8 @@ static int dispatch_loadplugin (const oconfig_item_t *ci) name = ci->values[0].value.string; /* default to the global interval set before loading this plugin */ - ctx.interval = plugin_get_interval (); + memset (&ctx, 0, sizeof (ctx)); + ctx.interval = cf_get_default_interval (); /* * XXX: Magic at work: @@ -534,7 +544,8 @@ static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src) } /* int cf_ci_append_children */ #define CF_MAX_DEPTH 8 -static oconfig_item_t *cf_read_generic (const char *path, int depth); +static oconfig_item_t *cf_read_generic (const char *path, + const char *pattern, int depth); static int cf_include_all (oconfig_item_t *root, int depth) { @@ -545,9 +556,9 @@ static int cf_include_all (oconfig_item_t *root, int depth) oconfig_item_t *new; oconfig_item_t *old; - /* Ignore all blocks, including `Include' blocks. */ - if (root->children[i].children_num != 0) - continue; + char *pattern = NULL; + + int j; if (strcasecmp (root->children[i].key, "Include") != 0) continue; @@ -561,7 +572,20 @@ static int cf_include_all (oconfig_item_t *root, int depth) continue; } - new = cf_read_generic (old->values[0].value.string, depth + 1); + for (j = 0; j < old->children_num; ++j) + { + oconfig_item_t *child = old->children + j; + + if (strcasecmp (child->key, "Filter") == 0) + cf_util_get_string (child, &pattern); + else + ERROR ("configfile: Option `%s' not allowed in block.", + child->key); + } + + new = cf_read_generic (old->values[0].value.string, pattern, depth + 1); + sfree (pattern); + if (new == NULL) continue; @@ -578,12 +602,34 @@ static int cf_include_all (oconfig_item_t *root, int depth) return (0); } /* int cf_include_all */ -static oconfig_item_t *cf_read_file (const char *file, int depth) +static oconfig_item_t *cf_read_file (const char *file, + const char *pattern, int depth) { oconfig_item_t *root; assert (depth < CF_MAX_DEPTH); + if (pattern != NULL) { +#if HAVE_FNMATCH_H && HAVE_LIBGEN_H + char *tmp = sstrdup (file); + char *filename = basename (tmp); + + if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) { + DEBUG ("configfile: Not including `%s' because it " + "does not match pattern `%s'.", + filename, pattern); + free (tmp); + return (NULL); + } + + free (tmp); +#else + ERROR ("configfile: Cannot apply pattern filter '%s' " + "to file '%s': functions basename() and / or " + "fnmatch() not available.", pattern, file); +#endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */ + } + root = oconfig_parse_file (file); if (root == NULL) { @@ -601,7 +647,8 @@ static int cf_compare_string (const void *p1, const void *p2) return strcmp (*(const char **) p1, *(const char **) p2); } -static oconfig_item_t *cf_read_dir (const char *dir, int depth) +static oconfig_item_t *cf_read_dir (const char *dir, + const char *pattern, int depth) { oconfig_item_t *root = NULL; DIR *dh; @@ -676,7 +723,7 @@ static oconfig_item_t *cf_read_dir (const char *dir, int depth) oconfig_item_t *temp; char *name = filenames[i]; - temp = cf_read_generic (name, depth); + temp = cf_read_generic (name, pattern, depth); if (temp == NULL) { /* An error should already have been reported. */ @@ -707,7 +754,8 @@ static oconfig_item_t *cf_read_dir (const char *dir, int depth) * simpler function is used which does not do any such expansion. */ #if HAVE_WORDEXP_H -static oconfig_item_t *cf_read_generic (const char *path, int depth) +static oconfig_item_t *cf_read_generic (const char *path, + const char *pattern, int depth) { oconfig_item_t *root = NULL; int status; @@ -760,9 +808,9 @@ static oconfig_item_t *cf_read_generic (const char *path, int depth) } if (S_ISREG (statbuf.st_mode)) - temp = cf_read_file (path_ptr, depth); + temp = cf_read_file (path_ptr, pattern, depth); else if (S_ISDIR (statbuf.st_mode)) - temp = cf_read_dir (path_ptr, depth); + temp = cf_read_dir (path_ptr, pattern, depth); else { WARNING ("configfile: %s is neither a file nor a " @@ -793,7 +841,8 @@ static oconfig_item_t *cf_read_generic (const char *path, int depth) /* #endif HAVE_WORDEXP_H */ #else /* if !HAVE_WORDEXP_H */ -static oconfig_item_t *cf_read_generic (const char *path, int depth) +static oconfig_item_t *cf_read_generic (const char *path, + const char *pattern, int depth) { struct stat statbuf; int status; @@ -816,9 +865,9 @@ static oconfig_item_t *cf_read_generic (const char *path, int depth) } if (S_ISREG (statbuf.st_mode)) - return (cf_read_file (path, depth)); + return (cf_read_file (path, pattern, depth)); else if (S_ISDIR (statbuf.st_mode)) - return (cf_read_dir (path, depth)); + return (cf_read_dir (path, pattern, depth)); ERROR ("configfile: %s is neither a file nor a directory.", path); return (NULL); @@ -867,6 +916,29 @@ const char *global_option_get (const char *option) : cf_global_options[i].def); } /* char *global_option_get */ +cdtime_t cf_get_default_interval (void) +{ + char const *str = global_option_get ("Interval"); + double interval_double = COLLECTD_DEFAULT_INTERVAL; + + if (str != NULL) + { + char *endptr = NULL; + double tmp = strtod (str, &endptr); + + if ((endptr == NULL) || (endptr == str) || (*endptr != 0)) + ERROR ("cf_get_default_interval: Unable to parse string \"%s\" " + "as number.", str); + else if (tmp <= 0.0) + ERROR ("cf_get_default_interval: Interval must be a positive number. " + "The current number is %g.", tmp); + else + interval_double = tmp; + } + + return (DOUBLE_TO_CDTIME_T (interval_double)); +} /* }}} cdtime_t cf_get_default_interval */ + void cf_unregister (const char *type) { cf_callback_t *this, *prev; @@ -969,7 +1041,7 @@ int cf_read (char *filename) oconfig_item_t *conf; int i; - conf = cf_read_generic (filename, 0 /* depth */); + conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0); if (conf == NULL) { ERROR ("Unable to read config file %s.", filename); @@ -1153,6 +1225,54 @@ int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */ return (tmp); } /* }}} int cf_util_get_port_number */ +int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */ +{ + int port; + char *service; + int status; + + if (ci->values_num != 1) + { + ERROR ("cf_util_get_service: The %s option requires exactly " + "one argument.", ci->key); + return (-1); + } + + if (ci->values[0].type == OCONFIG_TYPE_STRING) + return (cf_util_get_string (ci, ret_string)); + if (ci->values[0].type != OCONFIG_TYPE_NUMBER) + { + ERROR ("cf_util_get_service: The %s option requires " + "exactly one string or numeric argument.", + ci->key); + } + + port = 0; + status = cf_util_get_int (ci, &port); + if (status != 0) + return (status); + else if ((port < 1) || (port > 65535)) + { + ERROR ("cf_util_get_service: The port number given " + "for the %s option is out of " + "range (%i).", ci->key, port); + return (-1); + } + + service = malloc (6); + if (service == NULL) + { + ERROR ("cf_util_get_service: Out of memory."); + return (-1); + } + ssnprintf (service, 6, "%i", port); + + sfree (*ret_string); + *ret_string = service; + + return (0); +} /* }}} int cf_util_get_service */ + int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */ { if ((ci == NULL) || (ret_value == NULL))