Code

configfile: Be more verbose if the `Server' or `Local' modes are not available.
[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  **/
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 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
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))
49 extern int operating_mode;
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;
60 static cf_callback_t *first_callback = NULL;
62 typedef struct cf_mode_item
63 {
64         char *key;
65         char *value;
66         int   mode;
67 } cf_mode_item_t;
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;
81 static int nesting_depth = 0;
82 static char *current_module = NULL;
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 *);
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;
96         if (type == NULL)
97                 return (NULL);
99         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
100                 if (strcasecmp (cf_cb->type, type) == 0)
101                         break;
103         return (cf_cb);
106 static int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
108         cf_callback_t *cf_cb;
109         char *key;
110         char *value;
111         int ret;
112         int i;
114         DBG ("type = %s, key = %s, value = %s",
115                         ESCAPE_NULL(type),
116                         ESCAPE_NULL(orig_key),
117                         ESCAPE_NULL(orig_value));
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         }
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         }
133         ret = -1;
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         }
144         if (i >= cf_cb->keys_num)
145                 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.", type, key);
147         free (key);
148         free (value);
150         DBG ("return (%i)", ret);
152         return (ret);
155 void cf_unregister (char *type)
157         cf_callback_t *this, *prev;
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;
169                         free (this);
170                         break;
171                 }
174 void cf_register (char *type,
175                 int (*callback) (char *, char *),
176                 char **keys, int keys_num)
178         cf_callback_t *cf_cb;
179         char buf[64];
180         int i;
182         /* Remove this module from the list, if it already exists */
183         cf_unregister (type);
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;
189         cf_cb->type     = type;
190         cf_cb->callback = callback;
191         cf_cb->keys     = keys;
192         cf_cb->keys_num = keys_num;
194         cf_cb->next = first_callback;
195         first_callback = cf_cb;
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         }
215 /*
216  * Other query functions
217  */
218 char *cf_get_option (const char *key, char *def)
220         int i;
222         for (i = 0; i < cf_mode_num; i++)
223         {
224                 if ((cf_mode_list[i].mode & operating_mode) == 0)
225                         continue;
227                 if (strcasecmp (cf_mode_list[i].key, key) != 0)
228                         continue;
230                 if (cf_mode_list[i].value != NULL)
231                         return (cf_mode_list[i].value);
232                 return (def);
233         }
235         return (NULL);
238 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
239  * Functions for the actual parsing                                    *
240  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
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)
252         DEBUG_CALLBACK (shortvar, var, arguments, value);
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         }
291         return (LC_CBRET_OKAY);
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)
306         DEBUG_CALLBACK (shortvar, var, arguments, value);
308         plugin_set_dir (value);
310         return (LC_CBRET_OKAY);
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)
317         cf_mode_item_t *item;
319         DEBUG_CALLBACK (shortvar, var, arguments, value);
321         if (extra == NULL)
322         {
323                 fprintf (stderr, "No extra..?\n");
324                 return (LC_CBRET_ERROR);
325         }
327         item = (cf_mode_item_t *) extra;
329         if (strcasecmp (item->key, shortvar))
330         {
331                 fprintf (stderr, "Wrong extra..\n");
332                 return (LC_CBRET_ERROR);
333         }
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         }
341         if (item->value != NULL)
342         {
343                 free (item->value);
344                 item->value = NULL;
345         }
347         if ((item->value = strdup (value)) == NULL)
348         {
349                 perror ("strdup");
350                 return (LC_CBRET_ERROR);
351         }
353         return (LC_CBRET_OKAY);
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)
368         DEBUG_CALLBACK (shortvar, var, arguments, value);
370         if (plugin_load (value))
371                 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
373         /* Return `okay' even if there was an error, because it's not a syntax
374          * problem.. */
375         return (LC_CBRET_OKAY);
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)
382         char *buffer;
384         char *fields[3];
385         int   numfields;
387         char *node;
388         char *service = NET_DEFAULT_PORT;
390         DEBUG_CALLBACK (shortvar, var, arguments, value);
392         buffer = strdup (value);
393         if (buffer == NULL)
394                 return (LC_CBRET_ERROR);
396         numfields = strsplit (buffer, fields, 3);
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         }
406         node = fields[0];
407         if (numfields == 2)
408                 service = fields[1];
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);
415         free (buffer);
417         return (LC_CBRET_OKAY);
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)
432         DEBUG_CALLBACK (shortvar, var, arguments, value);
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                 }
442                 if (arguments == NULL)
443                 {
444                         fprintf (stderr, ERR_NEEDS_ARG, shortvar);
445                         return (LC_CBRET_ERROR);
446                 }
448                 if ((current_module = strdup (arguments)) == NULL)
449                 {
450                         perror ("strdup");
451                         return (LC_CBRET_ERROR);
452                 }
454                 nesting_depth++;
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                 }
469                 nesting_depth--;
471                 return (LC_CBRET_OKAY);
472         }
473         else
474         {
475                 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
476                 return (LC_CBRET_ERROR);
477         }
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)
492         DEBUG_CALLBACK (shortvar, var, arguments, value);
494         if ((nesting_depth == 0) || (current_module == NULL))
495         {
496                 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
497                 return (LC_CBRET_ERROR);
498         }
500         /* Send the data to the plugin */
501         if (cf_dispatch (current_module, shortvar, value) < 0)
502                 return (LC_CBRET_ERROR);
504         return (LC_CBRET_OKAY);
507 static void cf_init (void)
509         static int run_once = 0;
510         int i;
512         if (run_once != 0)
513                 return;
514         run_once = 1;
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);
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);
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);
531         for (i = 0; i < cf_mode_num; i++)
532         {
533                 cf_mode_item_t *item;
535                 item = &cf_mode_list[i];
537                 lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
538                                 cf_callback_mode_option, (void *) item);
539         }
542 int cf_read (char *filename)
544         cf_init ();
546         if (filename == NULL)
547                 filename = CONFIGFILE;
549         DBG ("Starting to parse file `%s'", filename);
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         }
558         DBG ("Done parsing file `%s'", filename);
560         /* free memory and stuff */
561         lc_cleanup ();
563         return (0);