e3341dd4fed39a68c886b76fca981a0ce5de1d20
1 /**
2 * collectd - src/configfile.c
3 * Copyright (C) 2005 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
25 #include "libconfig/libconfig.h"
27 #include "plugin.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"
38 #ifdef HAVE_LIBRRD
39 extern int operating_mode;
40 #else
41 static int operating_mode = MODE_CLIENT;
42 #endif
44 typedef struct cf_callback
45 {
46 char *type;
47 int (*callback) (char *, char *);
48 char **keys;
49 int keys_num;
50 struct cf_callback *next;
51 } cf_callback_t;
53 static cf_callback_t *first_callback = NULL;
55 typedef struct cf_mode_item
56 {
57 char *key;
58 char *value;
59 int mode;
60 } cf_mode_item_t;
62 /* TODO
63 * - LogFile
64 * - DontFork
65 */
66 static cf_mode_item_t cf_mode_list[] =
67 {
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}
72 };
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 *);
82 /*
83 * Functions to handle register/unregister, search, and other plugin related
84 * stuff
85 */
86 cf_callback_t *cf_search (char *type)
87 {
88 cf_callback_t *cf_cb;
90 if (type == NULL)
91 return (NULL);
93 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
94 if (strcasecmp (cf_cb->type, type) == 0)
95 break;
97 return (cf_cb);
98 }
100 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
101 {
102 cf_callback_t *cf_cb;
103 char *key;
104 char *value;
105 int ret;
106 int i;
108 DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
110 if ((cf_cb = cf_search (type)) == NULL)
111 {
112 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
113 return (-1);
114 }
116 if ((key = strdup (orig_key)) == NULL)
117 return (1);
118 if ((value = strdup (orig_value)) == NULL)
119 {
120 free (key);
121 return (2);
122 }
124 ret = -1;
126 for (i = 0; i < cf_cb->keys_num; i++)
127 {
128 if (strcasecmp (cf_cb->keys[i], key) == 0)
129 {
130 ret = (*cf_cb->callback) (key, value);
131 break;
132 }
133 }
135 if (i >= cf_cb->keys_num)
136 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
138 free (key);
139 free (value);
141 return (ret);
142 }
144 void cf_unregister (char *type)
145 {
146 cf_callback_t *this, *prev;
148 for (prev = NULL, this = first_callback;
149 this != NULL;
150 prev = this, this = this->next)
151 if (strcasecmp (this->type, type) == 0)
152 {
153 if (prev == NULL)
154 first_callback = this->next;
155 else
156 prev->next = this->next;
158 free (this);
159 break;
160 }
161 }
163 void cf_register (char *type,
164 int (*callback) (char *, char *),
165 char **keys, int keys_num)
166 {
167 cf_callback_t *cf_cb;
168 char buf[64];
169 int i;
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)
176 return;
178 cf_cb->type = type;
179 cf_cb->callback = callback;
180 cf_cb->keys = keys;
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++)
187 {
188 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
189 {
190 /* This may be called multiple times for the same
191 * `key', but apparently `lc_register_*' can handle
192 * it.. */
193 lc_register_callback (buf, SHORTOPT_NONE,
194 LC_VAR_STRING, cf_callback_plugin_dispatch,
195 NULL);
196 }
197 else
198 {
199 DBG ("Key was truncated: `%s'", keys[i]);
200 }
201 }
202 }
204 /*
205 * Other query functions
206 */
207 char *cf_get_mode_option (const char *key)
208 {
209 int i;
211 for (i = 0; i < cf_mode_num; i++)
212 {
213 if ((cf_mode_list[i].mode & operating_mode) == 0)
214 continue;
216 if (strcasecmp (cf_mode_list[i].key, key) == 0)
217 return (cf_mode_list[i].value);
218 }
220 return (NULL);
221 }
223 int cf_callback_usage (const char *shortvar, const char *var,
224 const char *arguments, const char *value, lc_flags_t flags,
225 void *extra)
226 {
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"
233 #if COLLECT_DAEMON
234 " -P <file> PID file.\n"
235 " Default: "PIDFILE"\n"
236 #endif
237 " -M <dir> Module/Plugin directory.\n"
238 " Default: "PLUGINDIR"\n"
239 " -D <dir> Data storage directory.\n"
240 " Default: "PKGLOCALSTATEDIR"\n"
241 #if COLLECT_DEBUG
242 " -L <file> Log file.\n"
243 " Default: "LOGFILE"\n"
244 #endif
245 #if COLLECT_DAEMON
246 " -f Don't fork to the background.\n"
247 #endif
248 #if HAVE_LIBRRD
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 */
253 #if COLLECT_PING
254 " Ping:\n"
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");
261 exit (0);
262 } /* exit_usage */
264 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
265 * Functions for the actual parsing *
266 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
268 /*
269 * `cf_callback_mode'
270 * Start/end the `mode' section
271 *
272 * <Mode `arguments'>
273 * ...
274 * </Mode>
275 */
276 int cf_callback_mode (const char *shortvar, const char *var,
277 const char *arguments, const char *value, lc_flags_t flags,
278 void *extra)
279 {
280 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
281 shortvar, var, arguments, value);
283 if (flags == LC_FLAGS_SECTIONSTART)
284 {
285 if (nesting_depth != 0)
286 {
287 fprintf (stderr, ERR_NOT_NESTED);
288 return (LC_CBRET_ERROR);
289 }
291 if (arguments == NULL)
292 {
293 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
294 return (LC_CBRET_ERROR);
295 }
297 nesting_depth++;
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)))
305 {
306 return (LC_CBRET_OKAY);
307 }
308 else
309 {
310 return (LC_CBRET_IGNORESECTION);
311 }
312 }
313 else if (flags == LC_FLAGS_SECTIONEND)
314 {
315 nesting_depth--;
317 return (LC_CBRET_OKAY);
318 }
319 else
320 {
321 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
322 return (LC_CBRET_ERROR);
323 }
325 }
327 /*
328 * `cf_callback_mode_plugindir'
329 * Change the plugin directory
330 *
331 * <Mode xxx>
332 * PluginDir `value'
333 * </Mode>
334 */
335 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
336 const char *arguments, const char *value, lc_flags_t flags,
337 void *extra)
338 {
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);
345 }
347 int cf_callback_mode_option (const char *shortvar, const char *var,
348 const char *arguments, const char *value, lc_flags_t flags,
349 void *extra)
350 {
351 cf_mode_item_t *item;
353 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
354 shortvar, var, arguments, value);
356 if (extra == NULL)
357 {
358 fprintf (stderr, "No extra..?\n");
359 return (LC_CBRET_ERROR);
360 }
362 item = (cf_mode_item_t *) extra;
364 if (strcasecmp (item->key, shortvar))
365 {
366 fprintf (stderr, "Wrong extra..\n");
367 return (LC_CBRET_ERROR);
368 }
370 if ((operating_mode & item->mode) == 0)
371 {
372 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
373 return (LC_CBRET_ERROR);
374 }
376 if (item->value != NULL)
377 {
378 free (item->value);
379 item->value = NULL;
380 }
382 if ((item->value = strdup (value)) == NULL)
383 {
384 perror ("strdup");
385 return (LC_CBRET_ERROR);
386 }
388 return (LC_CBRET_OKAY);
389 }
391 /*
392 * `cf_callback_mode_loadmodule':
393 * Load a plugin.
394 *
395 * <Mode xxx>
396 * LoadPlugin `value'
397 * </Mode>
398 */
399 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
400 const char *arguments, const char *value, lc_flags_t flags,
401 void *extra)
402 {
403 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
404 shortvar, var, arguments, value);
406 if (nesting_depth == 0)
407 {
408 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
409 return (LC_CBRET_ERROR);
410 }
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
416 * problem.. */
417 return (LC_CBRET_OKAY);
418 }
420 /*
421 * `cf_callback_mode_switch'
422 * Change the contents of the global variable `operating_mode'
423 *
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.
427 */
428 int cf_callback_mode_switch (const char *shortvar, const char *var,
429 const char *arguments, const char *value, lc_flags_t flags,
430 void *extra)
431 {
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;
441 else
442 {
443 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
444 return (LC_CBRET_ERROR);
445 }
447 return (LC_CBRET_OKAY);
448 }
450 /*
451 * `cf_callback_plugin'
452 * Start/end section `plugin'
453 *
454 * <Plugin `arguments'>
455 * ...
456 * </Plugin>
457 */
458 int cf_callback_plugin (const char *shortvar, const char *var,
459 const char *arguments, const char *value, lc_flags_t flags,
460 void *extra)
461 {
462 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
463 shortvar, var, arguments, value);
465 if (flags == LC_FLAGS_SECTIONSTART)
466 {
467 if (nesting_depth != 0)
468 {
469 fprintf (stderr, ERR_NOT_NESTED);
470 return (LC_CBRET_ERROR);
471 }
473 if (arguments == NULL)
474 {
475 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
476 return (LC_CBRET_ERROR);
477 }
479 if ((current_module = strdup (arguments)) == NULL)
480 {
481 perror ("strdup");
482 return (LC_CBRET_ERROR);
483 }
485 nesting_depth++;
487 if (cf_search (current_module) != NULL)
488 return (LC_CBRET_OKAY);
489 else
490 return (LC_CBRET_IGNORESECTION);
491 }
492 else if (flags == LC_FLAGS_SECTIONEND)
493 {
494 if (current_module != NULL)
495 {
496 free (current_module);
497 current_module = NULL;
498 }
500 nesting_depth--;
502 return (LC_CBRET_OKAY);
503 }
504 else
505 {
506 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
507 return (LC_CBRET_ERROR);
508 }
509 }
511 /*
512 * `cf_callback_plugin_dispatch'
513 * Send options within `plugin' sections to the plugin that requests it.
514 *
515 * <Plugin `current_module'>
516 * `var' `value'
517 * </Plugin>
518 */
519 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
520 const char *arguments, const char *value, lc_flags_t flags,
521 void *extra)
522 {
523 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
524 shortvar, var, arguments, value);
526 if ((nesting_depth == 0) || (current_module == NULL))
527 {
528 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
529 return (LC_CBRET_ERROR);
530 }
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);
537 }
539 void cf_init (void)
540 {
541 static int run_once = 0;
542 int i;
544 if (run_once != 0)
545 return;
546 run_once = 1;
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++)
569 {
570 char longvar[256];
571 cf_mode_item_t *item;
573 item = &cf_mode_list[i];
575 if (snprintf (longvar, 256, "Mode.%s", item->key) >= 256)
576 continue;
578 lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
579 cf_callback_mode_option, (void *) item);
580 }
581 }
583 int cf_read (int argc, char **argv, char *filename)
584 {
585 cf_init ();
587 if (filename == NULL)
588 filename = CONFIGFILE;
590 if (lc_process (argc, argv, "collectd", LC_CONF_APACHE, filename))
591 {
592 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
593 return (-1);
594 }
596 /* free memory and stuff */
597 lc_cleanup ();
599 return (0);
600 }