Use -Wall -Werror (AM_CFLAGS) when building any module.
[collectd.git] / src / plugin.c
1 /**
2  * collectd - src/plugin.c
3  * Copyright (C) 2005-2008  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  *   Sebastian Harl <sh at tokkee.org>
21  **/
22
23 #include "collectd.h"
24 #include "utils_complain.h"
25
26 #include <ltdl.h>
27
28 #if HAVE_PTHREAD_H
29 # include <pthread.h>
30 #endif
31
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_cache.h"
38 #include "utils_threshold.h"
39
40 /*
41  * Private structures
42  */
43 struct read_func_s
44 {
45         int wait_time;
46         int wait_left;
47         int (*callback) (void);
48         enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
49 };
50 typedef struct read_func_s read_func_t;
51
52 /*
53  * Private variables
54  */
55 static llist_t *list_init;
56 static llist_t *list_read;
57 static llist_t *list_write;
58 static llist_t *list_flush;
59 static llist_t *list_shutdown;
60 static llist_t *list_log;
61 static llist_t *list_notification;
62
63 static c_avl_tree_t *data_sets;
64
65 static char *plugindir = NULL;
66
67 static int             read_loop = 1;
68 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
69 static pthread_cond_t  read_cond = PTHREAD_COND_INITIALIZER;
70 static pthread_t      *read_threads = NULL;
71 static int             read_threads_num = 0;
72
73 /*
74  * Static functions
75  */
76 static const char *plugin_get_dir (void)
77 {
78         if (plugindir == NULL)
79                 return (PLUGINDIR);
80         else
81                 return (plugindir);
82 }
83
84 static int register_callback (llist_t **list, const char *name, void *callback)
85 {
86         llentry_t *le;
87         char *key;
88
89         if ((*list == NULL)
90                         && ((*list = llist_create ()) == NULL))
91                 return (-1);
92
93         le = llist_search (*list, name);
94         if (le == NULL)
95         {
96                 key = strdup (name);
97                 if (key == NULL)
98                         return (-1);
99
100                 le = llentry_create (key, callback);
101                 if (le == NULL)
102                 {
103                         free (key);
104                         return (-1);
105                 }
106
107                 llist_append (*list, le);
108         }
109         else
110         {
111                 le->value = callback;
112         }
113
114         return (0);
115 } /* int register_callback */
116
117 static int plugin_unregister (llist_t *list, const char *name)
118 {
119         llentry_t *e;
120
121         e = llist_search (list, name);
122
123         if (e == NULL)
124                 return (-1);
125
126         llist_remove (list, e);
127         free (e->key);
128         llentry_destroy (e);
129
130         return (0);
131 } /* int plugin_unregister */
132
133 /*
134  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
135  * object, but it will bitch about a shared object not having a
136  * ``module_register'' symbol..
137  */
138 static int plugin_load_file (char *file)
139 {
140         lt_dlhandle dlh;
141         void (*reg_handle) (void);
142
143         DEBUG ("file = %s", file);
144
145         lt_dlinit ();
146         lt_dlerror (); /* clear errors */
147
148         if ((dlh = lt_dlopen (file)) == NULL)
149         {
150                 const char *error = lt_dlerror ();
151
152                 ERROR ("lt_dlopen failed: %s", error);
153                 fprintf (stderr, "lt_dlopen failed: %s\n", error);
154                 return (1);
155         }
156
157         if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
158         {
159                 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
160                                 file, lt_dlerror ());
161                 lt_dlclose (dlh);
162                 return (-1);
163         }
164
165         (*reg_handle) ();
166
167         return (0);
168 }
169
170 static void *plugin_read_thread (void *args)
171 {
172         llentry_t   *le;
173         read_func_t *rf;
174         int          status;
175         int          done;
176
177         pthread_mutex_lock (&read_lock);
178
179         while (read_loop != 0)
180         {
181                 le = llist_head (list_read);
182                 done = 0;
183
184                 while ((read_loop != 0) && (le != NULL))
185                 {
186                         rf = (read_func_t *) le->value;
187
188                         if (rf->needs_read != TODO)
189                         {
190                                 le = le->next;
191                                 continue;
192                         }
193
194                         /* We will do this read function */
195                         rf->needs_read = ACTIVE;
196
197                         DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
198                                         (unsigned long int) pthread_self (), le->key);
199                         pthread_mutex_unlock (&read_lock);
200
201                         status = rf->callback ();
202                         done++;
203
204                         if (status != 0)
205                         {
206                                 if (rf->wait_time < interval_g)
207                                         rf->wait_time = interval_g;
208                                 rf->wait_left = rf->wait_time;
209                                 rf->wait_time = rf->wait_time * 2;
210                                 if (rf->wait_time > 86400)
211                                         rf->wait_time = 86400;
212
213                                 NOTICE ("read-function of plugin `%s' "
214                                                 "failed. Will suspend it for %i "
215                                                 "seconds.", le->key, rf->wait_left);
216                         }
217                         else
218                         {
219                                 rf->wait_left = 0;
220                                 rf->wait_time = interval_g;
221                         }
222
223                         pthread_mutex_lock (&read_lock);
224
225                         rf->needs_read = DONE;
226                         le = le->next;
227                 } /* while (le != NULL) */
228
229                 if ((read_loop != 0) && (done == 0))
230                 {
231                         DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
232                                         (unsigned long int) pthread_self ());
233                         pthread_cond_wait (&read_cond, &read_lock);
234                 }
235         } /* while (read_loop) */
236
237         pthread_mutex_unlock (&read_lock);
238
239         pthread_exit (NULL);
240         return ((void *) 0);
241 } /* void *plugin_read_thread */
242
243 static void start_threads (int num)
244 {
245         int i;
246
247         if (read_threads != NULL)
248                 return;
249
250         read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
251         if (read_threads == NULL)
252         {
253                 ERROR ("plugin: start_threads: calloc failed.");
254                 return;
255         }
256
257         read_threads_num = 0;
258         for (i = 0; i < num; i++)
259         {
260                 if (pthread_create (read_threads + read_threads_num, NULL,
261                                         plugin_read_thread, NULL) == 0)
262                 {
263                         read_threads_num++;
264                 }
265                 else
266                 {
267                         ERROR ("plugin: start_threads: pthread_create failed.");
268                         return;
269                 }
270         } /* for (i) */
271 } /* void start_threads */
272
273 static void stop_threads (void)
274 {
275         int i;
276
277         pthread_mutex_lock (&read_lock);
278         read_loop = 0;
279         DEBUG ("plugin: stop_threads: Signalling `read_cond'");
280         pthread_cond_broadcast (&read_cond);
281         pthread_mutex_unlock (&read_lock);
282
283         for (i = 0; i < read_threads_num; i++)
284         {
285                 if (pthread_join (read_threads[i], NULL) != 0)
286                 {
287                         ERROR ("plugin: stop_threads: pthread_join failed.");
288                 }
289                 read_threads[i] = (pthread_t) 0;
290         }
291         sfree (read_threads);
292         read_threads_num = 0;
293 } /* void stop_threads */
294
295 /*
296  * Public functions
297  */
298 void plugin_set_dir (const char *dir)
299 {
300         if (plugindir != NULL)
301                 free (plugindir);
302
303         if (dir == NULL)
304                 plugindir = NULL;
305         else if ((plugindir = strdup (dir)) == NULL)
306         {
307                 char errbuf[1024];
308                 ERROR ("strdup failed: %s",
309                                 sstrerror (errno, errbuf, sizeof (errbuf)));
310         }
311 }
312
313 #define BUFSIZE 512
314 int plugin_load (const char *type)
315 {
316         DIR  *dh;
317         const char *dir;
318         char  filename[BUFSIZE] = "";
319         char  typename[BUFSIZE];
320         int   typename_len;
321         int   ret;
322         struct stat    statbuf;
323         struct dirent *de;
324
325         DEBUG ("type = %s", type);
326
327         dir = plugin_get_dir ();
328         ret = 1;
329
330         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
331          * type when matching the filename */
332         if (ssnprintf (typename, sizeof (typename),
333                         "%s.so", type) >= sizeof (typename))
334         {
335                 WARNING ("snprintf: truncated: `%s.so'", type);
336                 return (-1);
337         }
338         typename_len = strlen (typename);
339
340         if ((dh = opendir (dir)) == NULL)
341         {
342                 char errbuf[1024];
343                 ERROR ("opendir (%s): %s", dir,
344                                 sstrerror (errno, errbuf, sizeof (errbuf)));
345                 return (-1);
346         }
347
348         while ((de = readdir (dh)) != NULL)
349         {
350                 if (strncasecmp (de->d_name, typename, typename_len))
351                         continue;
352
353                 if (ssnprintf (filename, sizeof (filename),
354                                 "%s/%s", dir, de->d_name) >= sizeof (filename))
355                 {
356                         WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
357                         continue;
358                 }
359
360                 if (lstat (filename, &statbuf) == -1)
361                 {
362                         char errbuf[1024];
363                         WARNING ("stat %s: %s", filename,
364                                         sstrerror (errno, errbuf, sizeof (errbuf)));
365                         continue;
366                 }
367                 else if (!S_ISREG (statbuf.st_mode))
368                 {
369                         /* don't follow symlinks */
370                         continue;
371                 }
372
373                 if (plugin_load_file (filename) == 0)
374                 {
375                         /* success */
376                         ret = 0;
377                         break;
378                 }
379                 else
380                 {
381                         fprintf (stderr, "Unable to load plugin %s.\n", type);
382                 }
383         }
384
385         closedir (dh);
386
387         if (filename[0] == '\0')
388                 fprintf (stderr, "Could not find plugin %s.\n", type);
389
390         return (ret);
391 }
392
393 /*
394  * The `register_*' functions follow
395  */
396 int plugin_register_config (const char *name,
397                 int (*callback) (const char *key, const char *val),
398                 const char **keys, int keys_num)
399 {
400         cf_register (name, callback, keys, keys_num);
401         return (0);
402 } /* int plugin_register_config */
403
404 int plugin_register_complex_config (const char *type,
405                 int (*callback) (oconfig_item_t *))
406 {
407         return (cf_register_complex (type, callback));
408 } /* int plugin_register_complex_config */
409
410 int plugin_register_init (const char *name,
411                 int (*callback) (void))
412 {
413         return (register_callback (&list_init, name, (void *) callback));
414 } /* plugin_register_init */
415
416 int plugin_register_read (const char *name,
417                 int (*callback) (void))
418 {
419         read_func_t *rf;
420
421         rf = (read_func_t *) malloc (sizeof (read_func_t));
422         if (rf == NULL)
423         {
424                 char errbuf[1024];
425                 ERROR ("plugin_register_read: malloc failed: %s",
426                                 sstrerror (errno, errbuf, sizeof (errbuf)));
427                 return (-1);
428         }
429
430         memset (rf, '\0', sizeof (read_func_t));
431         rf->wait_time = interval_g;
432         rf->wait_left = 0;
433         rf->callback = callback;
434         rf->needs_read = DONE;
435
436         return (register_callback (&list_read, name, (void *) rf));
437 } /* int plugin_register_read */
438
439 int plugin_register_write (const char *name,
440                 int (*callback) (const data_set_t *ds, const value_list_t *vl))
441 {
442         return (register_callback (&list_write, name, (void *) callback));
443 } /* int plugin_register_write */
444
445 int plugin_register_flush (const char *name,
446                 int (*callback) (const int timeout, const char *identifier))
447 {
448         return (register_callback (&list_flush, name, (void *) callback));
449 } /* int plugin_register_flush */
450
451 int plugin_register_shutdown (char *name,
452                 int (*callback) (void))
453 {
454         return (register_callback (&list_shutdown, name, (void *) callback));
455 } /* int plugin_register_shutdown */
456
457 int plugin_register_data_set (const data_set_t *ds)
458 {
459         data_set_t *ds_copy;
460         int i;
461
462         if ((data_sets != NULL)
463                         && (c_avl_get (data_sets, ds->type, NULL) == 0))
464         {
465                 NOTICE ("Replacing DS `%s' with another version.", ds->type);
466                 plugin_unregister_data_set (ds->type);
467         }
468         else if (data_sets == NULL)
469         {
470                 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
471                 if (data_sets == NULL)
472                         return (-1);
473         }
474
475         ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
476         if (ds_copy == NULL)
477                 return (-1);
478         memcpy(ds_copy, ds, sizeof (data_set_t));
479
480         ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
481                         * ds->ds_num);
482         if (ds_copy->ds == NULL)
483         {
484                 free (ds_copy);
485                 return (-1);
486         }
487
488         for (i = 0; i < ds->ds_num; i++)
489                 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
490
491         return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
492 } /* int plugin_register_data_set */
493
494 int plugin_register_log (char *name,
495                 void (*callback) (int priority, const char *msg))
496 {
497         return (register_callback (&list_log, name, (void *) callback));
498 } /* int plugin_register_log */
499
500 int plugin_register_notification (const char *name,
501                 int (*callback) (const notification_t *notif))
502 {
503         return (register_callback (&list_notification, name, (void *) callback));
504 } /* int plugin_register_log */
505
506 int plugin_unregister_config (const char *name)
507 {
508         cf_unregister (name);
509         return (0);
510 } /* int plugin_unregister_config */
511
512 int plugin_unregister_complex_config (const char *name)
513 {
514         cf_unregister_complex (name);
515         return (0);
516 } /* int plugin_unregister_complex_config */
517
518 int plugin_unregister_init (const char *name)
519 {
520         return (plugin_unregister (list_init, name));
521 }
522
523 int plugin_unregister_read (const char *name)
524 {
525         llentry_t *e;
526
527         e = llist_search (list_read, name);
528
529         if (e == NULL)
530                 return (-1);
531
532         llist_remove (list_read, e);
533         free (e->value);
534         free (e->key);
535         llentry_destroy (e);
536
537         return (0);
538 }
539
540 int plugin_unregister_write (const char *name)
541 {
542         return (plugin_unregister (list_write, name));
543 }
544
545 int plugin_unregister_flush (const char *name)
546 {
547         return (plugin_unregister (list_flush, name));
548 }
549
550 int plugin_unregister_shutdown (const char *name)
551 {
552         return (plugin_unregister (list_shutdown, name));
553 }
554
555 int plugin_unregister_data_set (const char *name)
556 {
557         data_set_t *ds;
558
559         if (data_sets == NULL)
560                 return (-1);
561
562         if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
563                 return (-1);
564
565         sfree (ds->ds);
566         sfree (ds);
567
568         return (0);
569 } /* int plugin_unregister_data_set */
570
571 int plugin_unregister_log (const char *name)
572 {
573         return (plugin_unregister (list_log, name));
574 }
575
576 int plugin_unregister_notification (const char *name)
577 {
578         return (plugin_unregister (list_notification, name));
579 }
580
581 void plugin_init_all (void)
582 {
583         int (*callback) (void);
584         llentry_t *le;
585         int status;
586
587         /* Start read-threads */
588         if (list_read != NULL)
589         {
590                 const char *rt;
591                 int num;
592                 rt = global_option_get ("ReadThreads");
593                 num = atoi (rt);
594                 start_threads ((num > 0) ? num : 5);
595         }
596
597         /* Init the value cache */
598         uc_init ();
599
600         if (list_init == NULL)
601                 return;
602
603         le = llist_head (list_init);
604         while (le != NULL)
605         {
606                 callback = (int (*) (void)) le->value;
607                 status = (*callback) ();
608
609                 if (status != 0)
610                 {
611                         ERROR ("Initialization of plugin `%s' "
612                                         "failed with status %i. "
613                                         "Plugin will be unloaded.",
614                                         le->key, status);
615                         /* FIXME: Unload _all_ functions */
616                         plugin_unregister_read (le->key);
617                 }
618
619                 le = le->next;
620         }
621 } /* void plugin_init_all */
622
623 void plugin_read_all (void)
624 {
625         llentry_t   *le;
626         read_func_t *rf;
627
628         uc_check_timeout ();
629
630         if (list_read == NULL)
631                 return;
632
633         pthread_mutex_lock (&read_lock);
634
635         le = llist_head (list_read);
636         while (le != NULL)
637         {
638                 rf = (read_func_t *) le->value;
639
640                 if (rf->needs_read != DONE)
641                 {
642                         le = le->next;
643                         continue;
644                 }
645
646                 if (rf->wait_left > 0)
647                         rf->wait_left -= interval_g;
648
649                 if (rf->wait_left <= 0)
650                 {
651                         rf->needs_read = TODO;
652                 }
653
654                 le = le->next;
655         }
656
657         DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
658         pthread_cond_broadcast (&read_cond);
659         pthread_mutex_unlock (&read_lock);
660 } /* void plugin_read_all */
661
662 int plugin_flush_one (int timeout, const char *name)
663 {
664         int (*callback) (int);
665         llentry_t *le;
666         int status;
667
668         if (list_flush == NULL)
669                 return (-1);
670
671         le = llist_search (list_flush, name);
672         if (le == NULL)
673                 return (-1);
674         callback = (int (*) (int)) le->value;
675
676         status = (*callback) (timeout);
677
678         return (status);
679 } /* int plugin_flush_ont */
680
681 void plugin_flush_all (int timeout)
682 {
683         int (*callback) (int);
684         llentry_t *le;
685
686         if (list_flush == NULL)
687                 return;
688
689         le = llist_head (list_flush);
690         while (le != NULL)
691         {
692                 callback = (int (*) (int)) le->value;
693                 le = le->next;
694
695                 (*callback) (timeout);
696         }
697 } /* void plugin_flush_all */
698
699 int plugin_flush (const char *plugin, int timeout, const char *identifier)
700 {
701   int (*callback) (int timeout, const char *identifier);
702   llentry_t *le;
703
704   if (list_flush == NULL)
705     return (0);
706
707   le = llist_head (list_flush);
708   while (le != NULL)
709   {
710     if ((plugin != NULL)
711         && (strcmp (plugin, le->key) != 0))
712       continue;
713
714     callback = (int (*) (int, const char *)) le->value;
715     le = le->next;
716
717     (*callback) (timeout, identifier);
718   }
719   return (0);
720 } /* int plugin_flush */
721
722 void plugin_shutdown_all (void)
723 {
724         int (*callback) (void);
725         llentry_t *le;
726
727         stop_threads ();
728
729         if (list_shutdown == NULL)
730                 return;
731
732         le = llist_head (list_shutdown);
733         while (le != NULL)
734         {
735                 callback = (int (*) (void)) le->value;
736
737                 /* Advance the pointer before calling the callback allows
738                  * shutdown functions to unregister themselves. If done the
739                  * other way around the memory `le' points to will be freed
740                  * after callback returns. */
741                 le = le->next;
742
743                 (*callback) ();
744         }
745 } /* void plugin_shutdown_all */
746
747 int plugin_dispatch_values (value_list_t *vl)
748 {
749         static c_complain_t no_write_complaint = C_COMPLAIN_INIT;
750
751         int (*callback) (const data_set_t *, const value_list_t *);
752         data_set_t *ds;
753         llentry_t *le;
754
755         if ((vl == NULL) || (*vl->type == '\0')) {
756                 ERROR ("plugin_dispatch_values: Invalid value list.");
757                 return (-1);
758         }
759
760         if (list_write == NULL)
761                 c_complain_once (LOG_WARNING, &no_write_complaint,
762                                 "plugin_dispatch_values: No write callback has been "
763                                 "registered. Please load at least one output plugin, "
764                                 "if you want the collected data to be stored.");
765
766         if (data_sets == NULL)
767         {
768                 ERROR ("plugin_dispatch_values: No data sets registered. "
769                                 "Could the types database be read? Check "
770                                 "your `TypesDB' setting!");
771                 return (-1);
772         }
773
774         if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
775         {
776                 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
777                 return (-1);
778         }
779
780         DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
781                         "host = %s; "
782                         "plugin = %s; plugin_instance = %s; "
783                         "type = %s; type_instance = %s;",
784                         (unsigned int) vl->time, vl->interval,
785                         vl->host,
786                         vl->plugin, vl->plugin_instance,
787                         vl->type, vl->type_instance);
788
789 #if COLLECT_DEBUG
790         assert (0 == strcmp (ds->type, vl->type));
791 #else
792         if (0 != strcmp (ds->type, vl->type))
793                 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
794                                 ds->type, vl->type);
795 #endif
796
797 #if COLLECT_DEBUG
798         assert (ds->ds_num == vl->values_len);
799 #else
800         if (ds->ds_num != vl->values_len)
801         {
802                 ERROR ("plugin_dispatch_values: ds->type = %s: "
803                                 "(ds->ds_num = %i) != "
804                                 "(vl->values_len = %i)",
805                                 ds->type, ds->ds_num, vl->values_len);
806                 return (-1);
807         }
808 #endif
809
810         escape_slashes (vl->host, sizeof (vl->host));
811         escape_slashes (vl->plugin, sizeof (vl->plugin));
812         escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
813         escape_slashes (vl->type, sizeof (vl->type));
814         escape_slashes (vl->type_instance, sizeof (vl->type_instance));
815
816         /* Update the value cache */
817         uc_update (ds, vl);
818         ut_check_threshold (ds, vl);
819
820         le = llist_head (list_write);
821         while (le != NULL)
822         {
823                 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
824                 (*callback) (ds, vl);
825
826                 le = le->next;
827         }
828
829         return (0);
830 } /* int plugin_dispatch_values */
831
832 int plugin_dispatch_notification (const notification_t *notif)
833 {
834         int (*callback) (const notification_t *);
835         llentry_t *le;
836         /* Possible TODO: Add flap detection here */
837
838         DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
839                         "time = %u; host = %s;",
840                         notif->severity, notif->message,
841                         (unsigned int) notif->time, notif->host);
842
843         /* Nobody cares for notifications */
844         if (list_notification == NULL)
845                 return (-1);
846
847         le = llist_head (list_notification);
848         while (le != NULL)
849         {
850                 callback = (int (*) (const notification_t *)) le->value;
851                 (*callback) (notif);
852
853                 le = le->next;
854         }
855
856         return (0);
857 } /* int plugin_dispatch_notification */
858
859 void plugin_log (int level, const char *format, ...)
860 {
861         char msg[512];
862         va_list ap;
863
864         void (*callback) (int, const char *);
865         llentry_t *le;
866
867         if (list_log == NULL)
868                 return;
869
870 #if !COLLECT_DEBUG
871         if (level >= LOG_DEBUG)
872                 return;
873 #endif
874
875         va_start (ap, format);
876         vsnprintf (msg, 512, format, ap);
877         msg[511] = '\0';
878         va_end (ap);
879
880         le = llist_head (list_log);
881         while (le != NULL)
882         {
883                 callback = (void (*) (int, const char *)) le->value;
884                 (*callback) (level, msg);
885
886                 le = le->next;
887         }
888 } /* void plugin_log */
889
890 const data_set_t *plugin_get_ds (const char *name)
891 {
892         data_set_t *ds;
893
894         if (c_avl_get (data_sets, name, (void *) &ds) != 0)
895         {
896                 DEBUG ("No such dataset registered: %s", name);
897                 return (NULL);
898         }
899
900         return (ds);
901 } /* data_set_t *plugin_get_ds */
902
903 static int plugin_notification_meta_add (notification_t *n,
904                 const char *name,
905                 enum notification_meta_type_e type,
906                 const void *value)
907 {
908   notification_meta_t *meta;
909   notification_meta_t *tail;
910
911   if ((n == NULL) || (name == NULL) || (value == NULL))
912   {
913     ERROR ("plugin_notification_meta_add: A pointer is NULL!");
914     return (-1);
915   }
916
917   meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
918   if (meta == NULL)
919   {
920     ERROR ("plugin_notification_meta_add: malloc failed.");
921     return (-1);
922   }
923   memset (meta, 0, sizeof (notification_meta_t));
924
925   sstrncpy (meta->name, name, sizeof (meta->name));
926   meta->type = type;
927
928   switch (type)
929   {
930     case NM_TYPE_STRING:
931     {
932       meta->value_string = strdup ((const char *) value);
933       if (meta->value_string == NULL)
934       {
935         ERROR ("plugin_notification_meta_add: strdup failed.");
936         sfree (meta);
937         return (-1);
938       }
939       break;
940     }
941     case NM_TYPE_SIGNED_INT:
942     {
943       meta->value_signed_int = *((int64_t *) value);
944       break;
945     }
946     case NM_TYPE_UNSIGNED_INT:
947     {
948       meta->value_unsigned_int = *((uint64_t *) value);
949       break;
950     }
951     case NM_TYPE_DOUBLE:
952     {
953       meta->value_double = *((double *) value);
954       break;
955     }
956     case NM_TYPE_BOOLEAN:
957     {
958       meta->value_boolean = *((bool *) value);
959       break;
960     }
961     default:
962     {
963       ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
964       sfree (meta);
965       return (-1);
966     }
967   } /* switch (type) */
968
969   meta->next = NULL;
970   tail = n->meta;
971   while ((tail != NULL) && (tail->next != NULL))
972     tail = tail->next;
973
974   if (tail == NULL)
975     n->meta = meta;
976   else
977     tail->next = meta;
978
979   return (0);
980 } /* int plugin_notification_meta_add */
981
982 int plugin_notification_meta_add_string (notification_t *n,
983     const char *name,
984     const char *value)
985 {
986   return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
987 }
988
989 int plugin_notification_meta_add_signed_int (notification_t *n,
990     const char *name,
991     int64_t value)
992 {
993   return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
994 }
995
996 int plugin_notification_meta_add_unsigned_int (notification_t *n,
997     const char *name,
998     uint64_t value)
999 {
1000   return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1001 }
1002
1003 int plugin_notification_meta_add_double (notification_t *n,
1004     const char *name,
1005     double value)
1006 {
1007   return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1008 }
1009
1010 int plugin_notification_meta_add_boolean (notification_t *n,
1011     const char *name,
1012     bool value)
1013 {
1014   return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1015 }
1016
1017 int plugin_notification_meta_copy (notification_t *dst,
1018     const notification_t *src)
1019 {
1020   notification_meta_t *meta;
1021
1022   assert (dst != NULL);
1023   assert (src != NULL);
1024   assert (dst != src);
1025   assert ((src->meta == NULL) || (src->meta != dst->meta));
1026
1027   for (meta = src->meta; meta != NULL; meta = meta->next)
1028   {
1029     if (meta->type == NM_TYPE_STRING)
1030       plugin_notification_meta_add_string (dst, meta->name,
1031           meta->value_string);
1032     else if (meta->type == NM_TYPE_SIGNED_INT)
1033       plugin_notification_meta_add_signed_int (dst, meta->name,
1034           meta->value_signed_int);
1035     else if (meta->type == NM_TYPE_UNSIGNED_INT)
1036       plugin_notification_meta_add_unsigned_int (dst, meta->name,
1037           meta->value_unsigned_int);
1038     else if (meta->type == NM_TYPE_DOUBLE)
1039       plugin_notification_meta_add_double (dst, meta->name,
1040           meta->value_double);
1041     else if (meta->type == NM_TYPE_BOOLEAN)
1042       plugin_notification_meta_add_boolean (dst, meta->name,
1043           meta->value_boolean);
1044   }
1045
1046   return (0);
1047 } /* int plugin_notification_meta_copy */
1048
1049 int plugin_notification_meta_free (notification_t *n)
1050 {
1051   notification_meta_t *this;
1052   notification_meta_t *next;
1053
1054   if (n == NULL)
1055   {
1056     ERROR ("plugin_notification_meta_free: n == NULL!");
1057     return (-1);
1058   }
1059
1060   this = n->meta;
1061   n->meta = NULL;
1062   while (this != NULL)
1063   {
1064     next = this->next;
1065
1066     if (this->type == NM_TYPE_STRING)
1067     {
1068       free ((char *)this->value_string);
1069       this->value_string = NULL;
1070     }
1071     sfree (this);
1072
1073     this = next;
1074   }
1075
1076   return (0);
1077 } /* int plugin_notification_meta_free */