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[] =
73 {"Server", NULL, MODE_CLIENT },
74 {"Port", NULL, MODE_CLIENT | MODE_SERVER },
76 {"PIDFile", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
77 {"DataDir", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
78 {"LogFile", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL}
80 static int cf_mode_num = 3;
82 static int nesting_depth = 0;
83 static char *current_module = NULL;
85 /* `cf_register' needs this prototype */
86 int cf_callback_plugin_dispatch (const char *, const char *, const char *,
87 const char *, lc_flags_t, void *);
90 * Functions to handle register/unregister, search, and other plugin related
93 cf_callback_t *cf_search (char *type)
100 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
101 if (strcasecmp (cf_cb->type, type) == 0)
107 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
109 cf_callback_t *cf_cb;
115 DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
117 if ((cf_cb = cf_search (type)) == NULL)
119 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
123 if ((key = strdup (orig_key)) == NULL)
125 if ((value = strdup (orig_value)) == NULL)
133 for (i = 0; i < cf_cb->keys_num; i++)
135 if (strcasecmp (cf_cb->keys[i], key) == 0)
137 ret = (*cf_cb->callback) (key, value);
142 if (i >= cf_cb->keys_num)
143 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
151 void cf_unregister (char *type)
153 cf_callback_t *this, *prev;
155 for (prev = NULL, this = first_callback;
157 prev = this, this = this->next)
158 if (strcasecmp (this->type, type) == 0)
161 first_callback = this->next;
163 prev->next = this->next;
170 void cf_register (char *type,
171 int (*callback) (char *, char *),
172 char **keys, int keys_num)
174 cf_callback_t *cf_cb;
178 /* Remove this module from the list, if it already exists */
179 cf_unregister (type);
181 /* This pointer will be free'd in `cf_unregister' */
182 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
186 cf_cb->callback = callback;
188 cf_cb->keys_num = keys_num;
190 cf_cb->next = first_callback;
191 first_callback = cf_cb;
193 for (i = 0; i < keys_num; i++)
195 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
197 /* This may be called multiple times for the same
198 * `key', but apparently `lc_register_*' can handle
200 lc_register_callback (buf, SHORTOPT_NONE,
201 LC_VAR_STRING, cf_callback_plugin_dispatch,
206 DBG ("Key was truncated: `%s'", keys[i]);
212 * Other query functions
214 char *cf_get_option (const char *key, char *def)
218 for (i = 0; i < cf_mode_num; i++)
220 if ((cf_mode_list[i].mode & operating_mode) == 0)
223 if (strcasecmp (cf_mode_list[i].key, key) != 0)
226 if (cf_mode_list[i].value != NULL)
227 return (cf_mode_list[i].value);
234 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
235 * Functions for the actual parsing *
236 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
240 * Chose the `operating_mode'
244 int cf_callback_mode (const char *shortvar, const char *var,
245 const char *arguments, const char *value, lc_flags_t flags,
248 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
249 shortvar, var, arguments, value);
251 if (strcasecmp (value, "Client") == 0)
252 operating_mode = MODE_CLIENT;
253 else if (strcasecmp (value, "Server") == 0)
254 operating_mode = MODE_SERVER;
255 else if (strcasecmp (value, "Local") == 0)
256 operating_mode = MODE_LOCAL;
259 syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
260 return (LC_CBRET_ERROR);
263 return (LC_CBRET_OKAY);
267 * `cf_callback_mode_plugindir'
268 * Change the plugin directory
274 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
275 const char *arguments, const char *value, lc_flags_t flags,
278 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
279 shortvar, var, arguments, value);
281 plugin_set_dir (value);
283 return (LC_CBRET_OKAY);
286 int cf_callback_mode_option (const char *shortvar, const char *var,
287 const char *arguments, const char *value, lc_flags_t flags,
290 cf_mode_item_t *item;
292 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
293 shortvar, var, arguments, value);
297 fprintf (stderr, "No extra..?\n");
298 return (LC_CBRET_ERROR);
301 item = (cf_mode_item_t *) extra;
303 if (strcasecmp (item->key, shortvar))
305 fprintf (stderr, "Wrong extra..\n");
306 return (LC_CBRET_ERROR);
309 if ((operating_mode & item->mode) == 0)
311 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
312 return (LC_CBRET_ERROR);
315 if (item->value != NULL)
321 if ((item->value = strdup (value)) == NULL)
324 return (LC_CBRET_ERROR);
327 return (LC_CBRET_OKAY);
331 * `cf_callback_mode_loadmodule':
338 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
339 const char *arguments, const char *value, lc_flags_t flags,
342 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
343 shortvar, var, arguments, value);
345 if (plugin_load (value))
346 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
348 /* Return `okay' even if there was an error, because it's not a syntax
350 return (LC_CBRET_OKAY);
354 * `cf_callback_mode_switch'
355 * Change the contents of the global variable `operating_mode'
357 * This should be command line options. One *can* do this in the config
358 * files, but I will not document this. Don't whine abount it not working as
359 * you expect if you do it anyways.
361 int cf_callback_mode_switch (const char *shortvar, const char *var,
362 const char *arguments, const char *value, lc_flags_t flags,
365 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
366 shortvar, var, arguments, value);
368 if (strcasecmp (shortvar, "Client") == 0)
369 operating_mode = MODE_CLIENT;
370 else if (strcasecmp (shortvar, "Local") == 0)
371 operating_mode = MODE_LOCAL;
372 else if (strcasecmp (shortvar, "Server") == 0)
373 operating_mode = MODE_SERVER;
376 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
377 return (LC_CBRET_ERROR);
380 return (LC_CBRET_OKAY);
383 int cf_callback_socket (const char *shortvar, const char *var,
384 const char *arguments, const char *value, lc_flags_t flags,
393 char *service = NET_DEFAULT_PORT;
395 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
396 shortvar, var, arguments, value);
398 buffer = strdup (value);
400 return (LC_CBRET_ERROR);
402 numfields = strsplit (buffer, fields, 3);
404 if ((numfields != 1) && (numfields != 2))
406 syslog (LOG_ERR, "Invalid number of arguments to `%s'",
409 return (LC_CBRET_ERROR);
416 /* Still return `LC_CBRET_OKAY' because this is not an syntax error */
417 if (network_create_socket (node, service) < 1)
418 syslog (LOG_ERR, "network_create_socket (%s, %s) failed",
423 return (LC_CBRET_OKAY);
427 * `cf_callback_plugin'
428 * Start/end section `plugin'
430 * <Plugin `arguments'>
434 int cf_callback_plugin (const char *shortvar, const char *var,
435 const char *arguments, const char *value, lc_flags_t flags,
438 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
439 shortvar, var, arguments, value);
441 if (flags == LC_FLAGS_SECTIONSTART)
443 if (nesting_depth != 0)
445 fprintf (stderr, ERR_NOT_NESTED);
446 return (LC_CBRET_ERROR);
449 if (arguments == NULL)
451 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
452 return (LC_CBRET_ERROR);
455 if ((current_module = strdup (arguments)) == NULL)
458 return (LC_CBRET_ERROR);
463 if (cf_search (current_module) != NULL)
464 return (LC_CBRET_OKAY);
466 return (LC_CBRET_IGNORESECTION);
468 else if (flags == LC_FLAGS_SECTIONEND)
470 if (current_module != NULL)
472 free (current_module);
473 current_module = NULL;
478 return (LC_CBRET_OKAY);
482 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
483 return (LC_CBRET_ERROR);
488 * `cf_callback_plugin_dispatch'
489 * Send options within `plugin' sections to the plugin that requests it.
491 * <Plugin `current_module'>
495 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
496 const char *arguments, const char *value, lc_flags_t flags,
499 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
500 shortvar, var, arguments, value);
502 if ((nesting_depth == 0) || (current_module == NULL))
504 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
505 return (LC_CBRET_ERROR);
508 /* Send the data to the plugin */
509 if (cf_dispatch (current_module, shortvar, value) < 0)
510 return (LC_CBRET_ERROR);
512 return (LC_CBRET_OKAY);
517 static int run_once = 0;
524 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING,
525 cf_callback_mode, NULL);
526 lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
527 cf_callback_plugin, NULL);
529 lc_register_callback ("PluginDir", SHORTOPT_NONE,
530 LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
531 lc_register_callback ("LoadPlugin", SHORTOPT_NONE,
532 LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
534 lc_register_callback ("Listen", SHORTOPT_NONE,
535 LC_VAR_STRING, cf_callback_socket, NULL);
536 lc_register_callback ("Server", SHORTOPT_NONE,
537 LC_VAR_STRING, cf_callback_socket, NULL);
539 for (i = 0; i < cf_mode_num; i++)
541 cf_mode_item_t *item;
543 item = &cf_mode_list[i];
545 lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
546 cf_callback_mode_option, (void *) item);
550 int cf_read (char *filename)
554 if (filename == NULL)
555 filename = CONFIGFILE;
557 DBG ("Starting to parse file `%s'", filename);
559 /* int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type); */
560 if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
562 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
566 DBG ("Done parsing file `%s'", filename);
568 /* free memory and stuff */