solaris-fixes branch: Fix the debugging output in `configfile.c'.
[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 #endif
262         else if (strcasecmp (value, "Log") == 0)
263                 operating_mode = MODE_LOG;
264         else
265         {
266                 syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
267                 return (LC_CBRET_ERROR);
268         }
269
270         return (LC_CBRET_OKAY);
271 }
272
273 /*
274  * `cf_callback_mode_plugindir'
275  *   Change the plugin directory
276  *
277  * <Mode xxx>
278  *   PluginDir `value'
279  * </Mode>
280  */
281 static int cf_callback_mode_plugindir (const char *shortvar, const char *var,
282                 const char *arguments, const char *value, lc_flags_t flags,
283                 void *extra)
284 {
285         DEBUG_CALLBACK (shortvar, var, arguments, value);
286
287         plugin_set_dir (value);
288
289         return (LC_CBRET_OKAY);
290 }
291
292 static int cf_callback_mode_option (const char *shortvar, const char *var,
293                 const char *arguments, const char *value, lc_flags_t flags,
294                 void *extra)
295 {
296         cf_mode_item_t *item;
297
298         DEBUG_CALLBACK (shortvar, var, arguments, value);
299
300         if (extra == NULL)
301         {
302                 fprintf (stderr, "No extra..?\n");
303                 return (LC_CBRET_ERROR);
304         }
305
306         item = (cf_mode_item_t *) extra;
307
308         if (strcasecmp (item->key, shortvar))
309         {
310                 fprintf (stderr, "Wrong extra..\n");
311                 return (LC_CBRET_ERROR);
312         }
313
314         if ((operating_mode & item->mode) == 0)
315         {
316                 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
317                 return (LC_CBRET_ERROR);
318         }
319
320         if (item->value != NULL)
321         {
322                 free (item->value);
323                 item->value = NULL;
324         }
325
326         if ((item->value = strdup (value)) == NULL)
327         {
328                 perror ("strdup");
329                 return (LC_CBRET_ERROR);
330         }
331
332         return (LC_CBRET_OKAY);
333 }
334
335 /*
336  * `cf_callback_mode_loadmodule':
337  *   Load a plugin.
338  *
339  * <Mode xxx>
340  *   LoadPlugin `value'
341  * </Mode>
342  */
343 static int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
344                 const char *arguments, const char *value, lc_flags_t flags,
345                 void *extra)
346 {
347         DEBUG_CALLBACK (shortvar, var, arguments, value);
348
349         if (plugin_load (value))
350                 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
351
352         /* Return `okay' even if there was an error, because it's not a syntax
353          * problem.. */
354         return (LC_CBRET_OKAY);
355 }
356
357 static int cf_callback_socket (const char *shortvar, const char *var,
358                 const char *arguments, const char *value, lc_flags_t flags,
359                 void *extra)
360 {
361         char *buffer;
362
363         char *fields[3];
364         int   numfields;
365
366         char *node;
367         char *service = NET_DEFAULT_PORT;
368
369         DEBUG_CALLBACK (shortvar, var, arguments, value);
370
371         buffer = strdup (value);
372         if (buffer == NULL)
373                 return (LC_CBRET_ERROR);
374
375         numfields = strsplit (buffer, fields, 3);
376
377         if ((numfields != 1) && (numfields != 2))
378         {
379                 syslog (LOG_ERR, "Invalid number of arguments to `%s'",
380                                 shortvar);
381                 free (buffer);
382                 return (LC_CBRET_ERROR);
383         }
384
385         node = fields[0];
386         if (numfields == 2)
387                 service = fields[1];
388
389         /* Still return `LC_CBRET_OKAY' because this is not an syntax error */
390         if (network_create_socket (node, service) < 1)
391                 syslog (LOG_ERR, "network_create_socket (%s, %s) failed",
392                                 node, service);
393
394         free (buffer);
395
396         return (LC_CBRET_OKAY);
397 }
398
399 /*
400  * `cf_callback_plugin'
401  *   Start/end section `plugin'
402  *
403  * <Plugin `arguments'>
404  *   ...
405  * </Plugin>
406  */
407 static int cf_callback_plugin (const char *shortvar, const char *var,
408                 const char *arguments, const char *value, lc_flags_t flags,
409                 void *extra)
410 {
411         DEBUG_CALLBACK (shortvar, var, arguments, value);
412
413         if (flags == LC_FLAGS_SECTIONSTART)
414         {
415                 if (nesting_depth != 0)
416                 {
417                         fprintf (stderr, ERR_NOT_NESTED);
418                         return (LC_CBRET_ERROR);
419                 }
420
421                 if (arguments == NULL)
422                 {
423                         fprintf (stderr, ERR_NEEDS_ARG, shortvar);
424                         return (LC_CBRET_ERROR);
425                 }
426
427                 if ((current_module = strdup (arguments)) == NULL)
428                 {
429                         perror ("strdup");
430                         return (LC_CBRET_ERROR);
431                 }
432
433                 nesting_depth++;
434
435                 if (cf_search (current_module) != NULL)
436                         return (LC_CBRET_OKAY);
437                 else
438                         return (LC_CBRET_IGNORESECTION);
439         }
440         else if (flags == LC_FLAGS_SECTIONEND)
441         {
442                 if (current_module != NULL)
443                 {
444                         free (current_module);
445                         current_module = NULL;
446                 }
447
448                 nesting_depth--;
449
450                 return (LC_CBRET_OKAY);
451         }
452         else
453         {
454                 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
455                 return (LC_CBRET_ERROR);
456         }
457 }
458
459 /*
460  * `cf_callback_plugin_dispatch'
461  *   Send options within `plugin' sections to the plugin that requests it.
462  *
463  * <Plugin `current_module'>
464  *   `var' `value'
465  * </Plugin>
466  */
467 static int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
468                 const char *arguments, const char *value, lc_flags_t flags,
469                 void *extra)
470 {
471         DEBUG_CALLBACK (shortvar, var, arguments, value);
472
473         if ((nesting_depth == 0) || (current_module == NULL))
474         {
475                 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
476                 return (LC_CBRET_ERROR);
477         }
478
479         /* Send the data to the plugin */
480         if (cf_dispatch (current_module, shortvar, value) < 0)
481                 return (LC_CBRET_ERROR);
482
483         return (LC_CBRET_OKAY);
484 }
485
486 static void cf_init (void)
487 {
488         static int run_once = 0;
489         int i;
490
491         if (run_once != 0)
492                 return;
493         run_once = 1;
494
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);
499
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);
504
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);
509
510         for (i = 0; i < cf_mode_num; i++)
511         {
512                 cf_mode_item_t *item;
513
514                 item = &cf_mode_list[i];
515
516                 lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
517                                 cf_callback_mode_option, (void *) item);
518         }
519 }
520
521 int cf_read (char *filename)
522 {
523         cf_init ();
524
525         if (filename == NULL)
526                 filename = CONFIGFILE;
527
528         DBG ("Starting to parse file `%s'", filename);
529
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         }
536
537         DBG ("Done parsing file `%s'", filename);
538
539         /* free memory and stuff */
540         lc_cleanup ();
541
542         return (0);
543 }