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