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