2 * collectd - src/configfile.c
3 * Copyright (C) 2005 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
24 * make internal-only functions `static' */
28 #include "libconfig/libconfig.h"
32 #include "configfile.h"
34 #include "utils_debug.h"
36 #define SHORTOPT_NONE 0
38 #define ERR_NOT_NESTED "Sections cannot be nested.\n"
39 #define ERR_SECTION_ONLY "`%s' can only be used as section.\n"
40 #define ERR_NEEDS_ARG "Section `%s' needs an argument.\n"
41 #define ERR_NEEDS_SECTION "`%s' can only be used within a section.\n"
44 extern int operating_mode;
46 static int operating_mode = MODE_CLIENT;
49 typedef struct cf_callback
52 int (*callback) (char *, char *);
55 struct cf_callback *next;
58 static cf_callback_t *first_callback = NULL;
60 typedef struct cf_mode_item
70 static cf_mode_item_t cf_mode_list[] =
72 {"MulticastTTL",NULL, MODE_CLIENT },
73 {"PIDFile", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
74 {"DataDir", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
75 {"LogFile", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL}
77 static int cf_mode_num = 4;
79 static int nesting_depth = 0;
80 static char *current_module = NULL;
82 /* `cf_register' needs this prototype */
83 int cf_callback_plugin_dispatch (const char *, const char *, const char *,
84 const char *, lc_flags_t, void *);
87 * Functions to handle register/unregister, search, and other plugin related
90 cf_callback_t *cf_search (char *type)
97 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
98 if (strcasecmp (cf_cb->type, type) == 0)
104 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
106 cf_callback_t *cf_cb;
112 DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
114 if ((cf_cb = cf_search (type)) == NULL)
116 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
120 if ((key = strdup (orig_key)) == NULL)
122 if ((value = strdup (orig_value)) == NULL)
130 for (i = 0; i < cf_cb->keys_num; i++)
132 if (strcasecmp (cf_cb->keys[i], key) == 0)
134 ret = (*cf_cb->callback) (key, value);
139 if (i >= cf_cb->keys_num)
140 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
148 void cf_unregister (char *type)
150 cf_callback_t *this, *prev;
152 for (prev = NULL, this = first_callback;
154 prev = this, this = this->next)
155 if (strcasecmp (this->type, type) == 0)
158 first_callback = this->next;
160 prev->next = this->next;
167 void cf_register (char *type,
168 int (*callback) (char *, char *),
169 char **keys, int keys_num)
171 cf_callback_t *cf_cb;
175 /* Remove this module from the list, if it already exists */
176 cf_unregister (type);
178 /* This pointer will be free'd in `cf_unregister' */
179 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
183 cf_cb->callback = callback;
185 cf_cb->keys_num = keys_num;
187 cf_cb->next = first_callback;
188 first_callback = cf_cb;
190 for (i = 0; i < keys_num; i++)
192 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
194 /* This may be called multiple times for the same
195 * `key', but apparently `lc_register_*' can handle
197 lc_register_callback (buf, SHORTOPT_NONE,
198 LC_VAR_STRING, cf_callback_plugin_dispatch,
203 DBG ("Key was truncated: `%s'", keys[i]);
209 * Other query functions
211 char *cf_get_option (const char *key, char *def)
215 for (i = 0; i < cf_mode_num; i++)
217 if ((cf_mode_list[i].mode & operating_mode) == 0)
220 if (strcasecmp (cf_mode_list[i].key, key) != 0)
223 if (cf_mode_list[i].value != NULL)
224 return (cf_mode_list[i].value);
231 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
232 * Functions for the actual parsing *
233 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
237 * Chose the `operating_mode'
241 int cf_callback_mode (const char *shortvar, const char *var,
242 const char *arguments, const char *value, lc_flags_t flags,
245 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
246 shortvar, var, arguments, value);
248 if (strcasecmp (value, "Client") == 0)
249 operating_mode = MODE_CLIENT;
250 else if (strcasecmp (value, "Server") == 0)
251 operating_mode = MODE_SERVER;
252 else if (strcasecmp (value, "Local") == 0)
253 operating_mode = MODE_LOCAL;
256 syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
257 return (LC_CBRET_ERROR);
260 return (LC_CBRET_OKAY);
264 * `cf_callback_mode_plugindir'
265 * Change the plugin directory
271 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
272 const char *arguments, const char *value, lc_flags_t flags,
275 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
276 shortvar, var, arguments, value);
278 plugin_set_dir (value);
280 return (LC_CBRET_OKAY);
283 int cf_callback_mode_option (const char *shortvar, const char *var,
284 const char *arguments, const char *value, lc_flags_t flags,
287 cf_mode_item_t *item;
289 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
290 shortvar, var, arguments, value);
294 fprintf (stderr, "No extra..?\n");
295 return (LC_CBRET_ERROR);
298 item = (cf_mode_item_t *) extra;
300 if (strcasecmp (item->key, shortvar))
302 fprintf (stderr, "Wrong extra..\n");
303 return (LC_CBRET_ERROR);
306 if ((operating_mode & item->mode) == 0)
308 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
309 return (LC_CBRET_ERROR);
312 if (item->value != NULL)
318 if ((item->value = strdup (value)) == NULL)
321 return (LC_CBRET_ERROR);
324 return (LC_CBRET_OKAY);
328 * `cf_callback_mode_loadmodule':
335 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
336 const char *arguments, const char *value, lc_flags_t flags,
339 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
340 shortvar, var, arguments, value);
342 if (plugin_load (value))
343 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
345 /* Return `okay' even if there was an error, because it's not a syntax
347 return (LC_CBRET_OKAY);
351 * `cf_callback_mode_switch'
352 * Change the contents of the global variable `operating_mode'
354 * This should be command line options. One *can* do this in the config
355 * files, but I will not document this. Don't whine abount it not working as
356 * you expect if you do it anyways.
358 int cf_callback_mode_switch (const char *shortvar, const char *var,
359 const char *arguments, const char *value, lc_flags_t flags,
362 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
363 shortvar, var, arguments, value);
365 if (strcasecmp (shortvar, "Client") == 0)
366 operating_mode = MODE_CLIENT;
367 else if (strcasecmp (shortvar, "Local") == 0)
368 operating_mode = MODE_LOCAL;
369 else if (strcasecmp (shortvar, "Server") == 0)
370 operating_mode = MODE_SERVER;
373 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
374 return (LC_CBRET_ERROR);
377 return (LC_CBRET_OKAY);
380 int cf_callback_socket (const char *shortvar, const char *var,
381 const char *arguments, const char *value, lc_flags_t flags,
390 char *service = NET_DEFAULT_PORT;
392 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
393 shortvar, var, arguments, value);
395 buffer = strdup (value);
397 return (LC_CBRET_ERROR);
399 numfields = strsplit (buffer, fields, 3);
401 if ((numfields != 1) && (numfields != 2))
403 syslog (LOG_ERR, "Invalid number of arguments to `%s'",
406 return (LC_CBRET_ERROR);
413 /* Still return `LC_CBRET_OKAY' because this is not an syntax error */
414 if (network_create_socket (node, service) < 1)
415 syslog (LOG_ERR, "network_create_socket (%s, %s) failed",
420 return (LC_CBRET_OKAY);
424 * `cf_callback_plugin'
425 * Start/end section `plugin'
427 * <Plugin `arguments'>
431 int cf_callback_plugin (const char *shortvar, const char *var,
432 const char *arguments, const char *value, lc_flags_t flags,
435 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
436 shortvar, var, arguments, value);
438 if (flags == LC_FLAGS_SECTIONSTART)
440 if (nesting_depth != 0)
442 fprintf (stderr, ERR_NOT_NESTED);
443 return (LC_CBRET_ERROR);
446 if (arguments == NULL)
448 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
449 return (LC_CBRET_ERROR);
452 if ((current_module = strdup (arguments)) == NULL)
455 return (LC_CBRET_ERROR);
460 if (cf_search (current_module) != NULL)
461 return (LC_CBRET_OKAY);
463 return (LC_CBRET_IGNORESECTION);
465 else if (flags == LC_FLAGS_SECTIONEND)
467 if (current_module != NULL)
469 free (current_module);
470 current_module = NULL;
475 return (LC_CBRET_OKAY);
479 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
480 return (LC_CBRET_ERROR);
485 * `cf_callback_plugin_dispatch'
486 * Send options within `plugin' sections to the plugin that requests it.
488 * <Plugin `current_module'>
492 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
493 const char *arguments, const char *value, lc_flags_t flags,
496 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
497 shortvar, var, arguments, value);
499 if ((nesting_depth == 0) || (current_module == NULL))
501 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
502 return (LC_CBRET_ERROR);
505 /* Send the data to the plugin */
506 if (cf_dispatch (current_module, shortvar, value) < 0)
507 return (LC_CBRET_ERROR);
509 return (LC_CBRET_OKAY);
514 static int run_once = 0;
521 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING,
522 cf_callback_mode, NULL);
523 lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
524 cf_callback_plugin, NULL);
526 lc_register_callback ("PluginDir", SHORTOPT_NONE,
527 LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
528 lc_register_callback ("LoadPlugin", SHORTOPT_NONE,
529 LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
531 lc_register_callback ("Listen", SHORTOPT_NONE,
532 LC_VAR_STRING, cf_callback_socket, NULL);
533 lc_register_callback ("Server", SHORTOPT_NONE,
534 LC_VAR_STRING, cf_callback_socket, NULL);
536 for (i = 0; i < cf_mode_num; i++)
538 cf_mode_item_t *item;
540 item = &cf_mode_list[i];
542 lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
543 cf_callback_mode_option, (void *) item);
547 int cf_read (char *filename)
551 if (filename == NULL)
552 filename = CONFIGFILE;
554 DBG ("Starting to parse file `%s'", filename);
556 /* int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type); */
557 if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
559 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
563 DBG ("Done parsing file `%s'", filename);
565 /* free memory and stuff */