Bumped version to 3.11.6; Updated ChangeLog.
[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         char  *type;
54         int  (*callback) (char *, char *);
55         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 (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 (char *type,
175                 int (*callback) (char *, char *),
176                 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 static int cf_callback_socket (const char *shortvar, const char *var,
379                 const char *arguments, const char *value, lc_flags_t flags,
380                 void *extra)
381 {
382         char *buffer;
383
384         char *fields[3];
385         int   numfields;
386
387         char *node;
388         char *service = NET_DEFAULT_PORT;
389
390         DEBUG_CALLBACK (shortvar, var, arguments, value);
391
392         buffer = strdup (value);
393         if (buffer == NULL)
394                 return (LC_CBRET_ERROR);
395
396         numfields = strsplit (buffer, fields, 3);
397
398         if ((numfields != 1) && (numfields != 2))
399         {
400                 syslog (LOG_ERR, "Invalid number of arguments to `%s'",
401                                 shortvar);
402                 free (buffer);
403                 return (LC_CBRET_ERROR);
404         }
405
406         node = fields[0];
407         if (numfields == 2)
408                 service = fields[1];
409
410         /* Still return `LC_CBRET_OKAY' because this is not an syntax error */
411         if (network_create_socket (node, service) < 1)
412                 syslog (LOG_ERR, "network_create_socket (%s, %s) failed",
413                                 node, service);
414
415         free (buffer);
416
417         return (LC_CBRET_OKAY);
418 }
419
420 /*
421  * `cf_callback_plugin'
422  *   Start/end section `plugin'
423  *
424  * <Plugin `arguments'>
425  *   ...
426  * </Plugin>
427  */
428 static int cf_callback_plugin (const char *shortvar, const char *var,
429                 const char *arguments, const char *value, lc_flags_t flags,
430                 void *extra)
431 {
432         DEBUG_CALLBACK (shortvar, var, arguments, value);
433
434         if (flags == LC_FLAGS_SECTIONSTART)
435         {
436                 if (nesting_depth != 0)
437                 {
438                         fprintf (stderr, ERR_NOT_NESTED);
439                         return (LC_CBRET_ERROR);
440                 }
441
442                 if (arguments == NULL)
443                 {
444                         fprintf (stderr, ERR_NEEDS_ARG, shortvar);
445                         return (LC_CBRET_ERROR);
446                 }
447
448                 if ((current_module = strdup (arguments)) == NULL)
449                 {
450                         perror ("strdup");
451                         return (LC_CBRET_ERROR);
452                 }
453
454                 nesting_depth++;
455
456                 if (cf_search (current_module) != NULL)
457                         return (LC_CBRET_OKAY);
458                 else
459                         return (LC_CBRET_IGNORESECTION);
460         }
461         else if (flags == LC_FLAGS_SECTIONEND)
462         {
463                 if (current_module != NULL)
464                 {
465                         free (current_module);
466                         current_module = NULL;
467                 }
468
469                 nesting_depth--;
470
471                 return (LC_CBRET_OKAY);
472         }
473         else
474         {
475                 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
476                 return (LC_CBRET_ERROR);
477         }
478 }
479
480 /*
481  * `cf_callback_plugin_dispatch'
482  *   Send options within `plugin' sections to the plugin that requests it.
483  *
484  * <Plugin `current_module'>
485  *   `var' `value'
486  * </Plugin>
487  */
488 static int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
489                 const char *arguments, const char *value, lc_flags_t flags,
490                 void *extra)
491 {
492         DEBUG_CALLBACK (shortvar, var, arguments, value);
493
494         if ((nesting_depth == 0) || (current_module == NULL))
495         {
496                 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
497                 return (LC_CBRET_ERROR);
498         }
499
500         /* Send the data to the plugin */
501         if (cf_dispatch (current_module, shortvar, value) < 0)
502                 return (LC_CBRET_ERROR);
503
504         return (LC_CBRET_OKAY);
505 }
506
507 static void cf_init (void)
508 {
509         static int run_once = 0;
510         int i;
511
512         if (run_once != 0)
513                 return;
514         run_once = 1;
515
516         lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING,
517                         cf_callback_mode, NULL);
518         lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
519                         cf_callback_plugin, NULL);
520
521         lc_register_callback ("PluginDir", SHORTOPT_NONE,
522                         LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
523         lc_register_callback ("LoadPlugin", SHORTOPT_NONE,
524                         LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
525
526         lc_register_callback ("Listen", SHORTOPT_NONE,
527                         LC_VAR_STRING, cf_callback_socket, NULL);
528         lc_register_callback ("Server", SHORTOPT_NONE,
529                         LC_VAR_STRING, cf_callback_socket, NULL);
530
531         for (i = 0; i < cf_mode_num; i++)
532         {
533                 cf_mode_item_t *item;
534
535                 item = &cf_mode_list[i];
536
537                 lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
538                                 cf_callback_mode_option, (void *) item);
539         }
540 }
541
542 int cf_read (char *filename)
543 {
544         cf_init ();
545
546         if (filename == NULL)
547                 filename = CONFIGFILE;
548
549         DBG ("Starting to parse file `%s'", filename);
550
551         /* int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type); */
552         if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
553         {
554                 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
555                 return (-1);
556         }
557
558         DBG ("Done parsing file `%s'", filename);
559
560         /* free memory and stuff */
561         lc_cleanup ();
562
563         return (0);
564 }