70ee6d601911109905ffb59e0e7d0167320bcbb6
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 "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
30 #include "network.h"
31 #include "utils_debug.h"
33 #define SHORTOPT_NONE 0
35 #define ERR_NOT_NESTED "Sections cannot be nested.\n"
36 #define ERR_SECTION_ONLY "`%s' can only be used as section.\n"
37 #define ERR_NEEDS_ARG "Section `%s' needs an argument.\n"
38 #define ERR_NEEDS_SECTION "`%s' can only be used within a section.\n"
40 extern int operating_mode;
42 typedef struct cf_callback
43 {
44 char *type;
45 int (*callback) (char *, char *);
46 char **keys;
47 int keys_num;
48 struct cf_callback *next;
49 } cf_callback_t;
51 static cf_callback_t *first_callback = NULL;
53 typedef struct cf_mode_item
54 {
55 char *key;
56 char *value;
57 int mode;
58 } cf_mode_item_t;
60 /* TODO
61 * - LogFile
62 */
63 static cf_mode_item_t cf_mode_list[] =
64 {
65 {"TimeToLive", NULL, MODE_CLIENT },
66 {"PIDFile", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL | MODE_LOG },
67 {"DataDir", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL | MODE_LOG },
68 {"LogFile", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL | MODE_LOG }
69 };
70 static int cf_mode_num = 4;
72 static int nesting_depth = 0;
73 static char *current_module = NULL;
75 /* `cf_register' needs this prototype */
76 static int cf_callback_plugin_dispatch (const char *, const char *,
77 const char *, const char *, lc_flags_t, void *);
79 /*
80 * Functions to handle register/unregister, search, and other plugin related
81 * stuff
82 */
83 static cf_callback_t *cf_search (char *type)
84 {
85 cf_callback_t *cf_cb;
87 if (type == NULL)
88 return (NULL);
90 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
91 if (strcasecmp (cf_cb->type, type) == 0)
92 break;
94 return (cf_cb);
95 }
97 static int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
98 {
99 cf_callback_t *cf_cb;
100 char *key;
101 char *value;
102 int ret;
103 int i;
105 DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
107 if ((cf_cb = cf_search (type)) == NULL)
108 {
109 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
110 return (-1);
111 }
113 if ((key = strdup (orig_key)) == NULL)
114 return (1);
115 if ((value = strdup (orig_value)) == NULL)
116 {
117 free (key);
118 return (2);
119 }
121 ret = -1;
123 for (i = 0; i < cf_cb->keys_num; i++)
124 {
125 if (strcasecmp (cf_cb->keys[i], key) == 0)
126 {
127 ret = (*cf_cb->callback) (key, value);
128 break;
129 }
130 }
132 if (i >= cf_cb->keys_num)
133 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
135 free (key);
136 free (value);
138 return (ret);
139 }
141 void cf_unregister (char *type)
142 {
143 cf_callback_t *this, *prev;
145 for (prev = NULL, this = first_callback;
146 this != NULL;
147 prev = this, this = this->next)
148 if (strcasecmp (this->type, type) == 0)
149 {
150 if (prev == NULL)
151 first_callback = this->next;
152 else
153 prev->next = this->next;
155 free (this);
156 break;
157 }
158 }
160 void cf_register (char *type,
161 int (*callback) (char *, char *),
162 char **keys, int keys_num)
163 {
164 cf_callback_t *cf_cb;
165 char buf[64];
166 int i;
168 /* Remove this module from the list, if it already exists */
169 cf_unregister (type);
171 /* This pointer will be free'd in `cf_unregister' */
172 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
173 return;
175 cf_cb->type = type;
176 cf_cb->callback = callback;
177 cf_cb->keys = keys;
178 cf_cb->keys_num = keys_num;
180 cf_cb->next = first_callback;
181 first_callback = cf_cb;
183 for (i = 0; i < keys_num; i++)
184 {
185 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
186 {
187 /* This may be called multiple times for the same
188 * `key', but apparently `lc_register_*' can handle
189 * it.. */
190 lc_register_callback (buf, SHORTOPT_NONE,
191 LC_VAR_STRING, cf_callback_plugin_dispatch,
192 NULL);
193 }
194 else
195 {
196 DBG ("Key was truncated: `%s'", keys[i]);
197 }
198 }
199 }
201 /*
202 * Other query functions
203 */
204 char *cf_get_option (const char *key, char *def)
205 {
206 int i;
208 for (i = 0; i < cf_mode_num; i++)
209 {
210 if ((cf_mode_list[i].mode & operating_mode) == 0)
211 continue;
213 if (strcasecmp (cf_mode_list[i].key, key) != 0)
214 continue;
216 if (cf_mode_list[i].value != NULL)
217 return (cf_mode_list[i].value);
218 return (def);
219 }
221 return (NULL);
222 }
224 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
225 * Functions for the actual parsing *
226 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
228 /*
229 * `cf_callback_mode'
230 * Chose the `operating_mode'
231 *
232 * Mode `value'
233 */
234 static int cf_callback_mode (const char *shortvar, const char *var,
235 const char *arguments, const char *value, lc_flags_t flags,
236 void *extra)
237 {
238 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
239 shortvar, var, (arguments) ? arguments : "<NULL>",
240 (value) ? value : "?");
242 if (strcasecmp (value, "Client") == 0)
243 operating_mode = MODE_CLIENT;
244 #if HAVE_LIBRRD
245 else if (strcasecmp (value, "Server") == 0)
246 operating_mode = MODE_SERVER;
247 else if (strcasecmp (value, "Local") == 0)
248 operating_mode = MODE_LOCAL;
249 #endif
250 else if (strcasecmp (value, "Log") == 0)
251 operating_mode = MODE_LOG;
252 else
253 {
254 syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
255 return (LC_CBRET_ERROR);
256 }
258 return (LC_CBRET_OKAY);
259 }
261 /*
262 * `cf_callback_mode_plugindir'
263 * Change the plugin directory
264 *
265 * <Mode xxx>
266 * PluginDir `value'
267 * </Mode>
268 */
269 static int cf_callback_mode_plugindir (const char *shortvar, const char *var,
270 const char *arguments, const char *value, lc_flags_t flags,
271 void *extra)
272 {
273 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
274 shortvar, var, (arguments) ? arguments : "<NULL>",
275 (value) ? value : "?");
277 plugin_set_dir (value);
279 return (LC_CBRET_OKAY);
280 }
282 static int cf_callback_mode_option (const char *shortvar, const char *var,
283 const char *arguments, const char *value, lc_flags_t flags,
284 void *extra)
285 {
286 cf_mode_item_t *item;
288 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
289 shortvar, var, (arguments) ? arguments : "<NULL>",
290 (value) ? value : "?");
292 if (extra == NULL)
293 {
294 fprintf (stderr, "No extra..?\n");
295 return (LC_CBRET_ERROR);
296 }
298 item = (cf_mode_item_t *) extra;
300 if (strcasecmp (item->key, shortvar))
301 {
302 fprintf (stderr, "Wrong extra..\n");
303 return (LC_CBRET_ERROR);
304 }
306 if ((operating_mode & item->mode) == 0)
307 {
308 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
309 return (LC_CBRET_ERROR);
310 }
312 if (item->value != NULL)
313 {
314 free (item->value);
315 item->value = NULL;
316 }
318 if ((item->value = strdup (value)) == NULL)
319 {
320 perror ("strdup");
321 return (LC_CBRET_ERROR);
322 }
324 return (LC_CBRET_OKAY);
325 }
327 /*
328 * `cf_callback_mode_loadmodule':
329 * Load a plugin.
330 *
331 * <Mode xxx>
332 * LoadPlugin `value'
333 * </Mode>
334 */
335 static int cf_callback_mode_loadmodule (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) ? arguments : "<NULL>",
341 (value) ? 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
347 * problem.. */
348 return (LC_CBRET_OKAY);
349 }
351 static int cf_callback_socket (const char *shortvar, const char *var,
352 const char *arguments, const char *value, lc_flags_t flags,
353 void *extra)
354 {
355 char *buffer;
357 char *fields[3];
358 int numfields;
360 char *node;
361 char *service = NET_DEFAULT_PORT;
363 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
364 shortvar, var, (arguments) ? arguments : "<NULL>",
365 (value) ? value : "?");
367 buffer = strdup (value);
368 if (buffer == NULL)
369 return (LC_CBRET_ERROR);
371 numfields = strsplit (buffer, fields, 3);
373 if ((numfields != 1) && (numfields != 2))
374 {
375 syslog (LOG_ERR, "Invalid number of arguments to `%s'",
376 shortvar);
377 free (buffer);
378 return (LC_CBRET_ERROR);
379 }
381 node = fields[0];
382 if (numfields == 2)
383 service = fields[1];
385 /* Still return `LC_CBRET_OKAY' because this is not an syntax error */
386 if (network_create_socket (node, service) < 1)
387 syslog (LOG_ERR, "network_create_socket (%s, %s) failed",
388 node, service);
390 free (buffer);
392 return (LC_CBRET_OKAY);
393 }
395 /*
396 * `cf_callback_plugin'
397 * Start/end section `plugin'
398 *
399 * <Plugin `arguments'>
400 * ...
401 * </Plugin>
402 */
403 static int cf_callback_plugin (const char *shortvar, const char *var,
404 const char *arguments, const char *value, lc_flags_t flags,
405 void *extra)
406 {
407 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
408 shortvar, var, (arguments) ? arguments : "<NULL>",
409 (value) ? value : "?");
411 if (flags == LC_FLAGS_SECTIONSTART)
412 {
413 if (nesting_depth != 0)
414 {
415 fprintf (stderr, ERR_NOT_NESTED);
416 return (LC_CBRET_ERROR);
417 }
419 if (arguments == NULL)
420 {
421 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
422 return (LC_CBRET_ERROR);
423 }
425 if ((current_module = strdup (arguments)) == NULL)
426 {
427 perror ("strdup");
428 return (LC_CBRET_ERROR);
429 }
431 nesting_depth++;
433 if (cf_search (current_module) != NULL)
434 return (LC_CBRET_OKAY);
435 else
436 return (LC_CBRET_IGNORESECTION);
437 }
438 else if (flags == LC_FLAGS_SECTIONEND)
439 {
440 if (current_module != NULL)
441 {
442 free (current_module);
443 current_module = NULL;
444 }
446 nesting_depth--;
448 return (LC_CBRET_OKAY);
449 }
450 else
451 {
452 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
453 return (LC_CBRET_ERROR);
454 }
455 }
457 /*
458 * `cf_callback_plugin_dispatch'
459 * Send options within `plugin' sections to the plugin that requests it.
460 *
461 * <Plugin `current_module'>
462 * `var' `value'
463 * </Plugin>
464 */
465 static int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
466 const char *arguments, const char *value, lc_flags_t flags,
467 void *extra)
468 {
469 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
470 shortvar, var, (arguments) ? arguments : "<NULL>",
471 (value) ? value : "?");
473 if ((nesting_depth == 0) || (current_module == NULL))
474 {
475 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
476 return (LC_CBRET_ERROR);
477 }
479 /* Send the data to the plugin */
480 if (cf_dispatch (current_module, shortvar, value) < 0)
481 return (LC_CBRET_ERROR);
483 return (LC_CBRET_OKAY);
484 }
486 static void cf_init (void)
487 {
488 static int run_once = 0;
489 int i;
491 if (run_once != 0)
492 return;
493 run_once = 1;
495 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING,
496 cf_callback_mode, NULL);
497 lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
498 cf_callback_plugin, NULL);
500 lc_register_callback ("PluginDir", SHORTOPT_NONE,
501 LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
502 lc_register_callback ("LoadPlugin", SHORTOPT_NONE,
503 LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
505 lc_register_callback ("Listen", SHORTOPT_NONE,
506 LC_VAR_STRING, cf_callback_socket, NULL);
507 lc_register_callback ("Server", SHORTOPT_NONE,
508 LC_VAR_STRING, cf_callback_socket, NULL);
510 for (i = 0; i < cf_mode_num; i++)
511 {
512 cf_mode_item_t *item;
514 item = &cf_mode_list[i];
516 lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
517 cf_callback_mode_option, (void *) item);
518 }
519 }
521 int cf_read (char *filename)
522 {
523 cf_init ();
525 if (filename == NULL)
526 filename = CONFIGFILE;
528 DBG ("Starting to parse file `%s'", filename);
530 /* int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type); */
531 if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
532 {
533 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
534 return (-1);
535 }
537 DBG ("Done parsing file `%s'", filename);
539 /* free memory and stuff */
540 lc_cleanup ();
542 return (0);
543 }