common.[ch]: Provide a function `format_name' to turn a value-list into its string...
[collectd.git] / src / plugin.c
1 /**
2  * collectd - src/plugin.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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23
24 #include <ltdl.h>
25
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_llist.h"
30
31 /*
32  * Private structures
33  */
34 struct read_func_s
35 {
36         int wait_time;
37         int wait_left;
38         int (*callback) (void);
39 };
40 typedef struct read_func_s read_func_t;
41
42 /*
43  * Private variables
44  */
45 static llist_t *list_init;
46 static llist_t *list_read;
47 static llist_t *list_write;
48 static llist_t *list_shutdown;
49 static llist_t *list_data_set;
50 static llist_t *list_log;
51
52 static char *plugindir = NULL;
53
54 /*
55  * Static functions
56  */
57 static const char *plugin_get_dir (void)
58 {
59         if (plugindir == NULL)
60                 return (PLUGINDIR);
61         else
62                 return (plugindir);
63 }
64
65 static int register_callback (llist_t **list, const char *name, void *callback)
66 {
67         llentry_t *le;
68
69         if ((*list == NULL)
70                         && ((*list = llist_create ()) == NULL))
71                 return (-1);
72
73         le = llist_search (*list, name);
74         if (le == NULL)
75         {
76                 le = llentry_create (name, callback);
77                 if (le == NULL)
78                         return (-1);
79
80                 llist_append (*list, le);
81         }
82         else
83         {
84                 le->value = callback;
85         }
86
87         return (0);
88 } /* int register_callback */
89
90 static int plugin_unregister (llist_t *list, const char *name)
91 {
92         llentry_t *e;
93
94         e = llist_search (list, name);
95
96         if (e == NULL)
97                 return (-1);
98
99         llist_remove (list, e);
100         llentry_destroy (e);
101
102         return (0);
103 } /* int plugin_unregister */
104
105 /*
106  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
107  * object, but it will bitch about a shared object not having a
108  * ``module_register'' symbol..
109  */
110 static int plugin_load_file (char *file)
111 {
112         lt_dlhandle dlh;
113         void (*reg_handle) (void);
114
115         DEBUG ("file = %s", file);
116
117         lt_dlinit ();
118         lt_dlerror (); /* clear errors */
119
120         if ((dlh = lt_dlopen (file)) == NULL)
121         {
122                 const char *error = lt_dlerror ();
123
124                 ERROR ("lt_dlopen failed: %s", error);
125                 DEBUG ("lt_dlopen failed: %s", error);
126                 return (1);
127         }
128
129         if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
130         {
131                 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
132                                 file, lt_dlerror ());
133                 lt_dlclose (dlh);
134                 return (-1);
135         }
136
137         (*reg_handle) ();
138
139         return (0);
140 }
141
142 /*
143  * Public functions
144  */
145 void plugin_set_dir (const char *dir)
146 {
147         if (plugindir != NULL)
148                 free (plugindir);
149
150         if (dir == NULL)
151                 plugindir = NULL;
152         else if ((plugindir = strdup (dir)) == NULL)
153         {
154                 char errbuf[1024];
155                 ERROR ("strdup failed: %s",
156                                 sstrerror (errno, errbuf, sizeof (errbuf)));
157         }
158 }
159
160 #define BUFSIZE 512
161 int plugin_load (const char *type)
162 {
163         DIR  *dh;
164         const char *dir;
165         char  filename[BUFSIZE];
166         char  typename[BUFSIZE];
167         int   typename_len;
168         int   ret;
169         struct stat    statbuf;
170         struct dirent *de;
171
172         DEBUG ("type = %s", type);
173
174         dir = plugin_get_dir ();
175         ret = 1;
176
177         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
178          * type when matching the filename */
179         if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
180         {
181                 WARNING ("snprintf: truncated: `%s.so'", type);
182                 return (-1);
183         }
184         typename_len = strlen (typename);
185
186         if ((dh = opendir (dir)) == NULL)
187         {
188                 char errbuf[1024];
189                 ERROR ("opendir (%s): %s", dir,
190                                 sstrerror (errno, errbuf, sizeof (errbuf)));
191                 return (-1);
192         }
193
194         while ((de = readdir (dh)) != NULL)
195         {
196                 if (strncasecmp (de->d_name, typename, typename_len))
197                         continue;
198
199                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
200                 {
201                         WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
202                         continue;
203                 }
204
205                 if (lstat (filename, &statbuf) == -1)
206                 {
207                         char errbuf[1024];
208                         WARNING ("stat %s: %s", filename,
209                                         sstrerror (errno, errbuf, sizeof (errbuf)));
210                         continue;
211                 }
212                 else if (!S_ISREG (statbuf.st_mode))
213                 {
214                         /* don't follow symlinks */
215                         continue;
216                 }
217
218                 if (plugin_load_file (filename) == 0)
219                 {
220                         /* success */
221                         ret = 0;
222                         break;
223                 }
224         }
225
226         closedir (dh);
227
228         return (ret);
229 }
230
231 /*
232  * The `register_*' functions follow
233  */
234 int plugin_register_config (const char *name,
235                 int (*callback) (const char *key, const char *val),
236                 const char **keys, int keys_num)
237 {
238         cf_register (name, callback, keys, keys_num);
239         return (0);
240 } /* int plugin_register_config */
241
242 int plugin_register_init (const char *name,
243                 int (*callback) (void))
244 {
245         return (register_callback (&list_init, name, (void *) callback));
246 } /* plugin_register_init */
247
248 int plugin_register_read (const char *name,
249                 int (*callback) (void))
250 {
251         read_func_t *rf;
252
253         rf = (read_func_t *) malloc (sizeof (read_func_t));
254         if (rf == NULL)
255         {
256                 char errbuf[1024];
257                 ERROR ("plugin_register_read: malloc failed: %s",
258                                 sstrerror (errno, errbuf, sizeof (errbuf)));
259                 return (-1);
260         }
261
262         memset (rf, '\0', sizeof (read_func_t));
263         rf->wait_time = interval_g;
264         rf->wait_left = 0;
265         rf->callback = callback;
266
267         return (register_callback (&list_read, name, (void *) rf));
268 } /* int plugin_register_read */
269
270 int plugin_register_write (const char *name,
271                 int (*callback) (const data_set_t *ds, const value_list_t *vl))
272 {
273         return (register_callback (&list_write, name, (void *) callback));
274 } /* int plugin_register_write */
275
276 int plugin_register_shutdown (char *name,
277                 int (*callback) (void))
278 {
279         return (register_callback (&list_shutdown, name, (void *) callback));
280 } /* int plugin_register_shutdown */
281
282 int plugin_register_data_set (const data_set_t *ds)
283 {
284         return (register_callback (&list_data_set, ds->type, (void *) ds));
285 } /* int plugin_register_data_set */
286
287 int plugin_register_log (char *name,
288                 void (*callback) (int priority, const char *msg))
289 {
290         return (register_callback (&list_log, name, (void *) callback));
291 } /* int plugin_register_log */
292
293 int plugin_unregister_config (const char *name)
294 {
295         cf_unregister (name);
296         return (0);
297 } /* int plugin_unregister_config */
298
299 int plugin_unregister_init (const char *name)
300 {
301         return (plugin_unregister (list_init, name));
302 }
303
304 int plugin_unregister_read (const char *name)
305 {
306         return (plugin_unregister (list_read, name));
307         llentry_t *e;
308
309         e = llist_search (list_read, name);
310
311         if (e == NULL)
312                 return (-1);
313
314         llist_remove (list_read, e);
315         free (e->value);
316         llentry_destroy (e);
317
318         return (0);
319 }
320
321 int plugin_unregister_write (const char *name)
322 {
323         return (plugin_unregister (list_write, name));
324 }
325
326 int plugin_unregister_shutdown (const char *name)
327 {
328         return (plugin_unregister (list_shutdown, name));
329 }
330
331 int plugin_unregister_data_set (const char *name)
332 {
333         return (plugin_unregister (list_data_set, name));
334 }
335
336 int plugin_unregister_log (const char *name)
337 {
338         return (plugin_unregister (list_log, name));
339 }
340
341 void plugin_init_all (void)
342 {
343         int (*callback) (void);
344         llentry_t *le;
345         int status;
346
347         if (list_init == NULL)
348                 return;
349
350         le = llist_head (list_init);
351         while (le != NULL)
352         {
353                 callback = le->value;
354                 status = (*callback) ();
355
356                 if (status != 0)
357                 {
358                         ERROR ("Initialization of plugin `%s' "
359                                         "failed with status %i. "
360                                         "Plugin will be unloaded. TODO!",
361                                         le->key, status);
362                         plugin_unregister_read (le->key);
363                 }
364
365                 le = le->next;
366         }
367 } /* void plugin_init_all */
368
369 void plugin_read_all (const int *loop)
370 {
371         llentry_t   *le;
372         read_func_t *rf;
373         int          status;
374
375         if (list_read == NULL)
376                 return;
377
378         le = llist_head (list_read);
379         while ((*loop == 0) && (le != NULL))
380         {
381                 rf = (read_func_t *) le->value;
382
383                 if (rf->wait_left > 0)
384                         rf->wait_left -= interval_g;
385                 if (rf->wait_left > 0)
386                 {
387                         le = le->next;
388                         continue;
389                 }
390
391                 status = rf->callback ();
392                 if (status != 0)
393                 {
394                         rf->wait_left = rf->wait_time;
395                         rf->wait_time = rf->wait_time * 2;
396                         if (rf->wait_time > 86400)
397                                 rf->wait_time = 86400;
398
399                         NOTICE ("read-function of plugin `%s' "
400                                         "failed. Will syspend it for %i "
401                                         "seconds.", le->key, rf->wait_left);
402                 }
403                 else
404                 {
405                         rf->wait_left = 0;
406                         rf->wait_time = interval_g;
407                 }
408
409                 le = le->next;
410         } /* while ((*loop == 0) && (le != NULL)) */
411 } /* void plugin_read_all */
412
413 void plugin_shutdown_all (void)
414 {
415         int (*callback) (void);
416         llentry_t *le;
417
418         if (list_shutdown == NULL)
419                 return;
420
421         le = llist_head (list_shutdown);
422         while (le != NULL)
423         {
424                 callback = le->value;
425                 (*callback) ();
426
427                 le = le->next;
428         }
429 } /* void plugin_shutdown_all */
430
431 int plugin_dispatch_values (const char *name, const value_list_t *vl)
432 {
433         int (*callback) (const data_set_t *, const value_list_t *);
434         data_set_t *ds;
435         llentry_t *le;
436
437         if (list_write == NULL)
438                 return (-1);
439
440         le = llist_search (list_data_set, name);
441         if (le == NULL)
442         {
443                 DEBUG ("No such dataset registered: %s", name);
444                 return (-1);
445         }
446
447         ds = (data_set_t *) le->value;
448
449         DEBUG ("time = %u; host = %s; "
450                         "plugin = %s; plugin_instance = %s; "
451                         "type = %s; type_instance = %s;",
452                         (unsigned int) vl->time, vl->host,
453                         vl->plugin, vl->plugin_instance,
454                         ds->type, vl->type_instance);
455
456         le = llist_head (list_write);
457         while (le != NULL)
458         {
459                 callback = le->value;
460                 (*callback) (ds, vl);
461
462                 le = le->next;
463         }
464
465         return (0);
466 }
467
468 void plugin_log (int level, const char *format, ...)
469 {
470         char msg[512];
471         va_list ap;
472
473         void (*callback) (int, const char *);
474         llentry_t *le;
475
476         if (list_log == NULL)
477                 return;
478
479 #if !COLLECT_DEBUG
480         if (level >= LOG_DEBUG)
481                 return;
482 #endif
483
484         va_start (ap, format);
485         vsnprintf (msg, 512, format, ap);
486         msg[511] = '\0';
487         va_end (ap);
488
489         le = llist_head (list_log);
490         while (le != NULL)
491         {
492                 callback = le->value;
493                 (*callback) (level, msg);
494
495                 le = le->next;
496         }
497 } /* void plugin_log */
498
499 void plugin_complain (int level, complain_t *c, const char *format, ...)
500 {
501         char message[512];
502         va_list ap;
503
504         if (c->delay > 0)
505         {
506                 c->delay--;
507                 return;
508         }
509
510         if (c->interval < interval_g)
511                 c->interval = interval_g;
512         else
513                 c->interval *= 2;
514
515         if (c->interval > 86400)
516                 c->interval = 86400;
517
518         c->delay = c->interval / interval_g;
519
520         va_start (ap, format);
521         vsnprintf (message, 512, format, ap);
522         message[511] = '\0';
523         va_end (ap);
524
525         plugin_log (level, message);
526 }
527
528 void plugin_relief (int level, complain_t *c, const char *format, ...)
529 {
530         char message[512];
531         va_list ap;
532
533         if (c->interval == 0)
534                 return;
535
536         c->interval = 0;
537
538         va_start (ap, format);
539         vsnprintf (message, 512, format, ap);
540         message[511] = '\0';
541         va_end (ap);
542
543         plugin_log (level, message);
544 }
545
546 const data_set_t *plugin_get_ds (const char *name)
547 {
548         data_set_t *ds;
549         llentry_t *le;
550
551         le = llist_search (list_data_set, name);
552         if (le == NULL)
553         {
554                 DEBUG ("No such dataset registered: %s", name);
555                 return (NULL);
556         }
557
558         ds = (data_set_t *) le->value;
559
560         return (ds);
561 } /* data_set_t *plugin_get_ds */