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