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
66 static cf_mode_item_t cf_mode_list[] =
68 {"Server", NULL, MODE_CLIENT },
69 {"Port", NULL, MODE_CLIENT | MODE_SERVER },
70 {"PIDFile", PIDFILE, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
71 {"DataDir", PKGLOCALSTATEDIR, MODE_SERVER | MODE_LOCAL}
73 static int cf_mode_num = 4;
75 static int nesting_depth = 0;
76 static char *current_module = NULL;
78 /* `cf_register' needs this prototype */
79 int cf_callback_plugin_dispatch (const char *, const char *, const char *,
80 const char *, lc_flags_t, void *);
83 * Functions to handle register/unregister, search, and other plugin related
86 cf_callback_t *cf_search (char *type)
93 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
94 if (strcasecmp (cf_cb->type, type) == 0)
100 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
102 cf_callback_t *cf_cb;
108 DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
110 if ((cf_cb = cf_search (type)) == NULL)
112 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
116 if ((key = strdup (orig_key)) == NULL)
118 if ((value = strdup (orig_value)) == NULL)
126 for (i = 0; i < cf_cb->keys_num; i++)
128 if (strcasecmp (cf_cb->keys[i], key) == 0)
130 ret = (*cf_cb->callback) (key, value);
135 if (i >= cf_cb->keys_num)
136 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
144 void cf_unregister (char *type)
146 cf_callback_t *this, *prev;
148 for (prev = NULL, this = first_callback;
150 prev = this, this = this->next)
151 if (strcasecmp (this->type, type) == 0)
154 first_callback = this->next;
156 prev->next = this->next;
163 void cf_register (char *type,
164 int (*callback) (char *, char *),
165 char **keys, int keys_num)
167 cf_callback_t *cf_cb;
171 /* Remove this module from the list, if it already exists */
172 cf_unregister (type);
174 /* This pointer will be free'd in `cf_unregister' */
175 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
179 cf_cb->callback = callback;
181 cf_cb->keys_num = keys_num;
183 cf_cb->next = first_callback;
184 first_callback = cf_cb;
186 for (i = 0; i < keys_num; i++)
188 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
190 /* This may be called multiple times for the same
191 * `key', but apparently `lc_register_*' can handle
193 lc_register_callback (buf, SHORTOPT_NONE,
194 LC_VAR_STRING, cf_callback_plugin_dispatch,
199 DBG ("Key was truncated: `%s'", keys[i]);
205 * Other query functions
207 char *cf_get_mode_option (const char *key)
211 for (i = 0; i < cf_mode_num; i++)
213 if ((cf_mode_list[i].mode & operating_mode) == 0)
216 if (strcasecmp (cf_mode_list[i].key, key) == 0)
217 return (cf_mode_list[i].value);
223 int cf_callback_usage (const char *shortvar, const char *var,
224 const char *arguments, const char *value, lc_flags_t flags,
227 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
228 shortvar, var, arguments, value);
230 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
232 "Available options:\n"
234 " -P <file> PID file.\n"
235 " Default: "PIDFILE"\n"
237 " -M <dir> Module/Plugin directory.\n"
238 " Default: "PLUGINDIR"\n"
239 " -D <dir> Data storage directory.\n"
240 " Default: "PKGLOCALSTATEDIR"\n"
242 " -L <file> Log file.\n"
243 " Default: "LOGFILE"\n"
246 " -f Don't fork to the background.\n"
249 " -l Start in local mode (no network).\n"
250 " -c Start in client (sender) mode.\n"
251 " -s Start in server (listener) mode.\n"
252 #endif /* HAVE_LIBRRD */
255 " -p <host> Host to ping periodically, may be repeated to ping\n"
256 " more than one host.\n"
257 #endif /* COLLECT_PING */
258 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
259 "by Florian octo Forster <octo@verplant.org>\n"
260 "for contributions see `AUTHORS'\n");
264 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
265 * Functions for the actual parsing *
266 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
270 * Start/end the `mode' section
276 int cf_callback_mode (const char *shortvar, const char *var,
277 const char *arguments, const char *value, lc_flags_t flags,
280 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
281 shortvar, var, arguments, value);
283 if (flags == LC_FLAGS_SECTIONSTART)
285 if (nesting_depth != 0)
287 fprintf (stderr, ERR_NOT_NESTED);
288 return (LC_CBRET_ERROR);
291 if (arguments == NULL)
293 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
294 return (LC_CBRET_ERROR);
299 if (((operating_mode == MODE_CLIENT)
300 && (strcasecmp (arguments, "Client") == 0))
301 || ((operating_mode == MODE_SERVER)
302 && (strcasecmp (arguments, "Server") == 0))
303 || ((operating_mode == MODE_LOCAL)
304 && (strcasecmp (arguments, "Local") == 0)))
306 return (LC_CBRET_OKAY);
310 return (LC_CBRET_IGNORESECTION);
313 else if (flags == LC_FLAGS_SECTIONEND)
317 return (LC_CBRET_OKAY);
321 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
322 return (LC_CBRET_ERROR);
328 * `cf_callback_mode_plugindir'
329 * Change the plugin directory
335 int cf_callback_mode_plugindir (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 plugin_set_dir (value);
344 return (LC_CBRET_OKAY);
347 int cf_callback_mode_option (const char *shortvar, const char *var,
348 const char *arguments, const char *value, lc_flags_t flags,
351 cf_mode_item_t *item;
353 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
354 shortvar, var, arguments, value);
358 fprintf (stderr, "No extra..?\n");
359 return (LC_CBRET_ERROR);
362 item = (cf_mode_item_t *) extra;
364 if (strcasecmp (item->key, shortvar))
366 fprintf (stderr, "Wrong extra..\n");
367 return (LC_CBRET_ERROR);
370 if ((operating_mode & item->mode) == 0)
372 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
373 return (LC_CBRET_ERROR);
376 if (item->value != NULL)
382 if ((item->value = strdup (value)) == NULL)
385 return (LC_CBRET_ERROR);
388 return (LC_CBRET_OKAY);
392 * `cf_callback_mode_loadmodule':
399 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
400 const char *arguments, const char *value, lc_flags_t flags,
403 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
404 shortvar, var, arguments, value);
406 if (nesting_depth == 0)
408 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
409 return (LC_CBRET_ERROR);
412 if (plugin_load (value))
413 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
415 /* Return `okay' even if there was an error, because it's not a syntax
417 return (LC_CBRET_OKAY);
421 * `cf_callback_mode_switch'
422 * Change the contents of the global variable `operating_mode'
424 * This should be command line options. One *can* do this in the config
425 * files, but I will not document this. Don't whine abount it not working as
426 * you expect if you do it anyways.
428 int cf_callback_mode_switch (const char *shortvar, const char *var,
429 const char *arguments, const char *value, lc_flags_t flags,
432 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
433 shortvar, var, arguments, value);
435 if (strcasecmp (shortvar, "Client") == 0)
436 operating_mode = MODE_CLIENT;
437 else if (strcasecmp (shortvar, "Local") == 0)
438 operating_mode = MODE_LOCAL;
439 else if (strcasecmp (shortvar, "Server") == 0)
440 operating_mode = MODE_SERVER;
443 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
444 return (LC_CBRET_ERROR);
447 return (LC_CBRET_OKAY);
451 * `cf_callback_plugin'
452 * Start/end section `plugin'
454 * <Plugin `arguments'>
458 int cf_callback_plugin (const char *shortvar, const char *var,
459 const char *arguments, const char *value, lc_flags_t flags,
462 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
463 shortvar, var, arguments, value);
465 if (flags == LC_FLAGS_SECTIONSTART)
467 if (nesting_depth != 0)
469 fprintf (stderr, ERR_NOT_NESTED);
470 return (LC_CBRET_ERROR);
473 if (arguments == NULL)
475 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
476 return (LC_CBRET_ERROR);
479 if ((current_module = strdup (arguments)) == NULL)
482 return (LC_CBRET_ERROR);
487 if (cf_search (current_module) != NULL)
488 return (LC_CBRET_OKAY);
490 return (LC_CBRET_IGNORESECTION);
492 else if (flags == LC_FLAGS_SECTIONEND)
494 if (current_module != NULL)
496 free (current_module);
497 current_module = NULL;
502 return (LC_CBRET_OKAY);
506 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
507 return (LC_CBRET_ERROR);
512 * `cf_callback_plugin_dispatch'
513 * Send options within `plugin' sections to the plugin that requests it.
515 * <Plugin `current_module'>
519 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
520 const char *arguments, const char *value, lc_flags_t flags,
523 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
524 shortvar, var, arguments, value);
526 if ((nesting_depth == 0) || (current_module == NULL))
528 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
529 return (LC_CBRET_ERROR);
532 /* Send the data to the plugin */
533 if (cf_dispatch (current_module, shortvar, value) < 0)
534 return (LC_CBRET_ERROR);
536 return (LC_CBRET_OKAY);
541 static int run_once = 0;
548 lc_register_callback ("Help", 'h', LC_VAR_NONE,
549 cf_callback_usage, NULL);
551 lc_register_callback ("Client", 'c', LC_VAR_NONE,
552 cf_callback_mode_switch, NULL);
553 lc_register_callback ("Local", 'l', LC_VAR_NONE,
554 cf_callback_mode_switch, NULL);
555 lc_register_callback ("Server", 's', LC_VAR_NONE,
556 cf_callback_mode_switch, NULL);
558 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
559 cf_callback_mode, NULL);
560 lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
561 cf_callback_plugin, NULL);
563 lc_register_callback ("Mode.PluginDir", 'P',
564 LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
565 lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE,
566 LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
568 for (i = 0; i < cf_mode_num; i++)
571 cf_mode_item_t *item;
573 item = &cf_mode_list[i];
575 if (snprintf (longvar, 256, "Mode.%s", item->key) >= 256)
578 lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
579 cf_callback_mode_option, (void *) item);
583 int cf_read (int argc, char **argv, char *filename)
587 if (filename == NULL)
588 filename = CONFIGFILE;
590 if (lc_process (argc, argv, "collectd", LC_CONF_APACHE, filename))
592 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
596 /* free memory and stuff */