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>
25 #include "libconfig/libconfig.h"
28 #include "configfile.h"
29 #include "utils_debug.h"
31 #define SHORTOPT_NONE 0
33 #define ERR_NOT_NESTED "Sections cannot be nested.\n"
34 #define ERR_SECTION_ONLY "`%s' can only be used as section.\n"
35 #define ERR_NEEDS_ARG "Section `%s' needs an argument.\n"
36 #define ERR_NEEDS_SECTION "`%s' can only be used within a section.\n"
39 extern int operating_mode;
41 static int operating_mode = MODE_CLIENT;
44 typedef struct cf_callback
47 int (*callback) (char *, char *);
50 struct cf_callback *next;
53 static cf_callback_t *first_callback = NULL;
55 typedef struct cf_mode_item
62 static cf_mode_item_t cf_mode_list[] =
64 {"Server", NULL, MODE_CLIENT },
65 {"Port", NULL, MODE_CLIENT | MODE_SERVER },
66 {"PIDFile", PIDFILE, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
67 {"DataDir", PKGLOCALSTATEDIR, MODE_SERVER | MODE_LOCAL}
69 static int cf_mode_num = 4;
71 static int nesting_depth = 0;
72 static char *current_module = NULL;
74 /* `cf_register' needs this prototype */
75 int cf_callback_plugin_dispatch (const char *, const char *, const char *,
76 const char *, lc_flags_t, void *);
79 * Functions to handle register/unregister, search, and other plugin related
82 cf_callback_t *cf_search (char *type)
89 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
90 if (strcasecmp (cf_cb->type, type) == 0)
96 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
104 DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
106 if ((cf_cb = cf_search (type)) == NULL)
108 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
112 if ((key = strdup (orig_key)) == NULL)
114 if ((value = strdup (orig_value)) == NULL)
122 for (i = 0; i < cf_cb->keys_num; i++)
124 if (strcasecmp (cf_cb->keys[i], key) == 0)
126 ret = (*cf_cb->callback) (key, value);
131 if (i >= cf_cb->keys_num)
132 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
140 void cf_unregister (char *type)
142 cf_callback_t *this, *prev;
144 for (prev = NULL, this = first_callback;
146 prev = this, this = this->next)
147 if (strcasecmp (this->type, type) == 0)
150 first_callback = this->next;
152 prev->next = this->next;
159 void cf_register (char *type,
160 int (*callback) (char *, char *),
161 char **keys, int keys_num)
163 cf_callback_t *cf_cb;
167 /* Remove this module from the list, if it already exists */
168 cf_unregister (type);
170 /* This pointer will be free'd in `cf_unregister' */
171 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
175 cf_cb->callback = callback;
177 cf_cb->keys_num = keys_num;
179 cf_cb->next = first_callback;
180 first_callback = cf_cb;
182 for (i = 0; i < keys_num; i++)
184 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
186 /* This may be called multiple times for the same
187 * `key', but apparently `lc_register_*' can handle
189 lc_register_callback (buf, SHORTOPT_NONE,
190 LC_VAR_STRING, cf_callback_plugin_dispatch,
195 DBG ("Key was truncated: `%s'", keys[i]);
201 * Other query functions
203 char *cf_get_mode_option (const char *key)
207 for (i = 0; i < cf_mode_num; i++)
209 if ((cf_mode_list[i].mode & operating_mode) == 0)
212 if (strcasecmp (cf_mode_list[i].key, key) == 0)
213 return (cf_mode_list[i].value);
219 int cf_callback_usage (const char *shortvar, const char *var,
220 const char *arguments, const char *value, lc_flags_t flags,
223 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
224 shortvar, var, arguments, value);
226 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
228 "Available options:\n"
230 " -P <file> PID file.\n"
231 " Default: "PIDFILE"\n"
233 " -M <dir> Module/Plugin directory.\n"
234 " Default: "PLUGINDIR"\n"
235 " -D <dir> Data storage directory.\n"
236 " Default: "PKGLOCALSTATEDIR"\n"
238 " -L <file> Log file.\n"
239 " Default: "LOGFILE"\n"
242 " -f Don't fork to the background.\n"
245 " -l Start in local mode (no network).\n"
246 " -c Start in client (sender) mode.\n"
247 " -s Start in server (listener) mode.\n"
248 #endif /* HAVE_LIBRRD */
251 " -p <host> Host to ping periodically, may be repeated to ping\n"
252 " more than one host.\n"
253 #endif /* COLLECT_PING */
254 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
255 "by Florian octo Forster <octo@verplant.org>\n"
256 "for contributions see `AUTHORS'\n");
260 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
261 * Functions for the actual parsing *
262 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
266 * Start/end the `mode' section
272 int cf_callback_mode (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 if (flags == LC_FLAGS_SECTIONSTART)
281 if (nesting_depth != 0)
283 fprintf (stderr, ERR_NOT_NESTED);
284 return (LC_CBRET_ERROR);
287 if (arguments == NULL)
289 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
290 return (LC_CBRET_ERROR);
295 if (((operating_mode == MODE_CLIENT)
296 && (strcasecmp (arguments, "Client") == 0))
297 || ((operating_mode == MODE_SERVER)
298 && (strcasecmp (arguments, "Server") == 0))
299 || ((operating_mode == MODE_LOCAL)
300 && (strcasecmp (arguments, "Local") == 0)))
302 return (LC_CBRET_OKAY);
306 return (LC_CBRET_IGNORESECTION);
309 else if (flags == LC_FLAGS_SECTIONEND)
313 return (LC_CBRET_OKAY);
317 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
318 return (LC_CBRET_ERROR);
324 * `cf_callback_mode_plugindir'
325 * Change the plugin directory
331 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
332 const char *arguments, const char *value, lc_flags_t flags,
335 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
336 shortvar, var, arguments, value);
338 plugin_set_dir (value);
340 return (LC_CBRET_OKAY);
343 int cf_callback_mode_option (const char *shortvar, const char *var,
344 const char *arguments, const char *value, lc_flags_t flags,
347 cf_mode_item_t *item;
349 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
350 shortvar, var, arguments, value);
354 fprintf (stderr, "No extra..?\n");
355 return (LC_CBRET_ERROR);
358 item = (cf_mode_item_t *) extra;
360 if (strcasecmp (item->key, shortvar))
362 fprintf (stderr, "Wrong extra..\n");
363 return (LC_CBRET_ERROR);
366 if ((operating_mode & item->mode) == 0)
368 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
369 return (LC_CBRET_ERROR);
372 if (item->value != NULL)
378 if ((item->value = strdup (value)) == NULL)
381 return (LC_CBRET_ERROR);
384 return (LC_CBRET_OKAY);
388 * `cf_callback_mode_loadmodule':
395 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
396 const char *arguments, const char *value, lc_flags_t flags,
399 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
400 shortvar, var, arguments, value);
402 if (nesting_depth == 0)
404 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
405 return (LC_CBRET_ERROR);
408 if (plugin_load (value))
409 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
411 /* Return `okay' even if there was an error, because it's not a syntax
413 return (LC_CBRET_OKAY);
416 /* XXX think about how to do the command line stuff */
417 int cf_callback_mode_switch (const char *shortvar, const char *var,
418 const char *arguments, const char *value, lc_flags_t flags,
421 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
422 shortvar, var, arguments, value);
424 if (strcasecmp (shortvar, "Client") == 0)
425 operating_mode = MODE_CLIENT;
426 else if (strcasecmp (shortvar, "Local") == 0)
427 operating_mode = MODE_LOCAL;
428 else if (strcasecmp (shortvar, "Server") == 0)
429 operating_mode = MODE_SERVER;
432 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
433 return (LC_CBRET_ERROR);
436 return (LC_CBRET_OKAY);
440 * `cf_callback_plugin'
441 * Start/end section `plugin'
443 * <Plugin `arguments'>
447 int cf_callback_plugin (const char *shortvar, const char *var,
448 const char *arguments, const char *value, lc_flags_t flags,
451 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
452 shortvar, var, arguments, value);
454 if (flags == LC_FLAGS_SECTIONSTART)
456 if (nesting_depth != 0)
458 fprintf (stderr, ERR_NOT_NESTED);
459 return (LC_CBRET_ERROR);
462 if (arguments == NULL)
464 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
465 return (LC_CBRET_ERROR);
468 if ((current_module = strdup (arguments)) == NULL)
471 return (LC_CBRET_ERROR);
476 if (cf_search (current_module) != NULL)
477 return (LC_CBRET_OKAY);
479 return (LC_CBRET_IGNORESECTION);
481 else if (flags == LC_FLAGS_SECTIONEND)
483 if (current_module != NULL)
485 free (current_module);
486 current_module = NULL;
491 return (LC_CBRET_OKAY);
495 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
496 return (LC_CBRET_ERROR);
501 * `cf_callback_plugin_dispatch'
502 * Send options within `plugin' sections to the plugin that requests it.
504 * <Plugin `current_module'>
508 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
509 const char *arguments, const char *value, lc_flags_t flags,
512 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
513 shortvar, var, arguments, value);
515 if ((nesting_depth == 0) || (current_module == NULL))
517 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
518 return (LC_CBRET_ERROR);
521 /* Send the data to the plugin */
522 if (cf_dispatch (current_module, shortvar, value) < 0)
523 return (LC_CBRET_ERROR);
525 return (LC_CBRET_OKAY);
530 static int run_once = 0;
537 lc_register_callback ("Help", 'h', LC_VAR_NONE,
538 cf_callback_usage, NULL);
540 lc_register_callback ("Client", 'c', LC_VAR_NONE,
541 cf_callback_mode_switch, NULL);
542 lc_register_callback ("Local", 'l', LC_VAR_NONE,
543 cf_callback_mode_switch, NULL);
544 lc_register_callback ("Server", 's', LC_VAR_NONE,
545 cf_callback_mode_switch, NULL);
547 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
548 cf_callback_mode, NULL);
549 lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
550 cf_callback_plugin, NULL);
552 lc_register_callback ("Mode.PluginDir", 'P',
553 LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
554 lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE,
555 LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
557 for (i = 0; i < cf_mode_num; i++)
560 cf_mode_item_t *item;
562 item = &cf_mode_list[i];
564 if (snprintf (longvar, 256, "Mode.%s", item->key) >= 256)
567 lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
568 cf_callback_mode_option, (void *) item);
572 int cf_read (int argc, char **argv, char *filename)
576 if (filename == NULL)
577 filename = CONFIGFILE;
579 if (lc_process (argc, argv, "collectd", LC_CONF_APACHE, filename))
581 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
585 /* free memory and stuff */