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 {"Server", NULL, MODE_CLIENT },
73 {"Port", NULL, MODE_CLIENT | MODE_SERVER },
74 {"PIDFile", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
75 {"DataDir", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
76 {"LogFile", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL}
78 static int cf_mode_num = 5;
80 static int nesting_depth = 0;
81 static char *current_module = NULL;
83 /* `cf_register' needs this prototype */
84 int cf_callback_plugin_dispatch (const char *, const char *, const char *,
85 const char *, lc_flags_t, void *);
88 * Functions to handle register/unregister, search, and other plugin related
91 cf_callback_t *cf_search (char *type)
98 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
99 if (strcasecmp (cf_cb->type, type) == 0)
105 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
107 cf_callback_t *cf_cb;
113 DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
115 if ((cf_cb = cf_search (type)) == NULL)
117 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
121 if ((key = strdup (orig_key)) == NULL)
123 if ((value = strdup (orig_value)) == NULL)
131 for (i = 0; i < cf_cb->keys_num; i++)
133 if (strcasecmp (cf_cb->keys[i], key) == 0)
135 ret = (*cf_cb->callback) (key, value);
140 if (i >= cf_cb->keys_num)
141 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
149 void cf_unregister (char *type)
151 cf_callback_t *this, *prev;
153 for (prev = NULL, this = first_callback;
155 prev = this, this = this->next)
156 if (strcasecmp (this->type, type) == 0)
159 first_callback = this->next;
161 prev->next = this->next;
168 void cf_register (char *type,
169 int (*callback) (char *, char *),
170 char **keys, int keys_num)
172 cf_callback_t *cf_cb;
176 /* Remove this module from the list, if it already exists */
177 cf_unregister (type);
179 /* This pointer will be free'd in `cf_unregister' */
180 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
184 cf_cb->callback = callback;
186 cf_cb->keys_num = keys_num;
188 cf_cb->next = first_callback;
189 first_callback = cf_cb;
191 for (i = 0; i < keys_num; i++)
193 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
195 /* This may be called multiple times for the same
196 * `key', but apparently `lc_register_*' can handle
198 lc_register_callback (buf, SHORTOPT_NONE,
199 LC_VAR_STRING, cf_callback_plugin_dispatch,
204 DBG ("Key was truncated: `%s'", keys[i]);
210 * Other query functions
212 char *cf_get_option (const char *key, char *def)
216 for (i = 0; i < cf_mode_num; i++)
218 if ((cf_mode_list[i].mode & operating_mode) == 0)
221 if (strcasecmp (cf_mode_list[i].key, key) != 0)
224 if (cf_mode_list[i].value != NULL)
225 return (cf_mode_list[i].value);
232 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
233 * Functions for the actual parsing *
234 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
238 * Chose the `operating_mode'
242 int cf_callback_mode (const char *shortvar, const char *var,
243 const char *arguments, const char *value, lc_flags_t flags,
246 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
247 shortvar, var, arguments, value);
249 if (strcasecmp (value, "Client") == 0)
250 operating_mode = MODE_CLIENT;
251 else if (strcasecmp (value, "Server") == 0)
252 operating_mode = MODE_SERVER;
253 else if (strcasecmp (value, "Local") == 0)
254 operating_mode = MODE_LOCAL;
257 syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
258 return (LC_CBRET_ERROR);
261 return (LC_CBRET_OKAY);
265 * `cf_callback_mode_plugindir'
266 * Change the plugin directory
272 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
273 const char *arguments, const char *value, lc_flags_t flags,
276 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
277 shortvar, var, arguments, value);
279 plugin_set_dir (value);
281 return (LC_CBRET_OKAY);
284 int cf_callback_mode_option (const char *shortvar, const char *var,
285 const char *arguments, const char *value, lc_flags_t flags,
288 cf_mode_item_t *item;
290 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
291 shortvar, var, arguments, value);
295 fprintf (stderr, "No extra..?\n");
296 return (LC_CBRET_ERROR);
299 item = (cf_mode_item_t *) extra;
301 if (strcasecmp (item->key, shortvar))
303 fprintf (stderr, "Wrong extra..\n");
304 return (LC_CBRET_ERROR);
307 if ((operating_mode & item->mode) == 0)
309 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
310 return (LC_CBRET_ERROR);
313 if (item->value != NULL)
319 if ((item->value = strdup (value)) == NULL)
322 return (LC_CBRET_ERROR);
325 return (LC_CBRET_OKAY);
329 * `cf_callback_mode_loadmodule':
336 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
337 const char *arguments, const char *value, lc_flags_t flags,
340 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
341 shortvar, var, arguments, value);
343 if (plugin_load (value))
344 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
346 /* Return `okay' even if there was an error, because it's not a syntax
348 return (LC_CBRET_OKAY);
352 * `cf_callback_mode_switch'
353 * Change the contents of the global variable `operating_mode'
355 * This should be command line options. One *can* do this in the config
356 * files, but I will not document this. Don't whine abount it not working as
357 * you expect if you do it anyways.
359 int cf_callback_mode_switch (const char *shortvar, const char *var,
360 const char *arguments, const char *value, lc_flags_t flags,
363 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
364 shortvar, var, arguments, value);
366 if (strcasecmp (shortvar, "Client") == 0)
367 operating_mode = MODE_CLIENT;
368 else if (strcasecmp (shortvar, "Local") == 0)
369 operating_mode = MODE_LOCAL;
370 else if (strcasecmp (shortvar, "Server") == 0)
371 operating_mode = MODE_SERVER;
374 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
375 return (LC_CBRET_ERROR);
378 return (LC_CBRET_OKAY);
381 int cf_callback_socket (const char *shortvar, const char *var,
382 const char *arguments, const char *value, lc_flags_t flags,
391 char *service = NET_DEFAULT_PORT;
393 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
394 shortvar, var, arguments, value);
396 buffer = strdup (value);
398 return (LC_CBRET_ERROR);
400 numfields = strsplit (buffer, fields, 3);
402 if ((numfields != 1) && (numfields != 2))
404 syslog (LOG_ERR, "Invalid number of arguments to `%s'",
407 return (LC_CBRET_ERROR);
414 /* Still return `LC_CBRET_OKAY' because this is not an syntax error */
415 if (network_create_socket (node, service) < 1)
416 syslog (LOG_ERR, "network_create_socket (%s, %s) failed",
421 return (LC_CBRET_OKAY);
425 * `cf_callback_plugin'
426 * Start/end section `plugin'
428 * <Plugin `arguments'>
432 int cf_callback_plugin (const char *shortvar, const char *var,
433 const char *arguments, const char *value, lc_flags_t flags,
436 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
437 shortvar, var, arguments, value);
439 if (flags == LC_FLAGS_SECTIONSTART)
441 if (nesting_depth != 0)
443 fprintf (stderr, ERR_NOT_NESTED);
444 return (LC_CBRET_ERROR);
447 if (arguments == NULL)
449 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
450 return (LC_CBRET_ERROR);
453 if ((current_module = strdup (arguments)) == NULL)
456 return (LC_CBRET_ERROR);
461 if (cf_search (current_module) != NULL)
462 return (LC_CBRET_OKAY);
464 return (LC_CBRET_IGNORESECTION);
466 else if (flags == LC_FLAGS_SECTIONEND)
468 if (current_module != NULL)
470 free (current_module);
471 current_module = NULL;
476 return (LC_CBRET_OKAY);
480 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
481 return (LC_CBRET_ERROR);
486 * `cf_callback_plugin_dispatch'
487 * Send options within `plugin' sections to the plugin that requests it.
489 * <Plugin `current_module'>
493 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
494 const char *arguments, const char *value, lc_flags_t flags,
497 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
498 shortvar, var, arguments, value);
500 if ((nesting_depth == 0) || (current_module == NULL))
502 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
503 return (LC_CBRET_ERROR);
506 /* Send the data to the plugin */
507 if (cf_dispatch (current_module, shortvar, value) < 0)
508 return (LC_CBRET_ERROR);
510 return (LC_CBRET_OKAY);
515 static int run_once = 0;
522 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING,
523 cf_callback_mode, NULL);
524 lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
525 cf_callback_plugin, NULL);
527 lc_register_callback ("PluginDir", SHORTOPT_NONE,
528 LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
529 lc_register_callback ("LoadPlugin", SHORTOPT_NONE,
530 LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
532 lc_register_callback ("Listen", SHORTOPT_NONE,
533 LC_VAR_STRING, cf_callback_socket, NULL);
534 lc_register_callback ("Server", SHORTOPT_NONE,
535 LC_VAR_STRING, cf_callback_socket, NULL);
537 for (i = 0; i < cf_mode_num; i++)
539 cf_mode_item_t *item;
541 item = &cf_mode_list[i];
543 lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
544 cf_callback_mode_option, (void *) item);
548 int cf_read (char *filename)
552 if (filename == NULL)
553 filename = CONFIGFILE;
555 DBG ("Starting to parse file `%s'", filename);
557 /* int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type); */
558 if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
560 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
564 DBG ("Done parsing file `%s'", filename);
566 /* free memory and stuff */