interface, memory, ping plugins: Update copyright information.
[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 (%s) failed: %s", file, error);
153                 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, 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                         WARNING ("stat %s: not a regular file", filename);
371                         continue;
372                 }
373
374                 if (plugin_load_file (filename) == 0)
375                 {
376                         /* success */
377                         ret = 0;
378                         break;
379                 }
380                 else
381                 {
382                         fprintf (stderr, "Unable to load plugin %s.\n", type);
383                 }
384         }
385
386         closedir (dh);
387
388         if (filename[0] == '\0')
389                 fprintf (stderr, "Could not find plugin %s.\n", type);
390
391         return (ret);
392 }
393
394 /*
395  * The `register_*' functions follow
396  */
397 int plugin_register_config (const char *name,
398                 int (*callback) (const char *key, const char *val),
399                 const char **keys, int keys_num)
400 {
401         cf_register (name, callback, keys, keys_num);
402         return (0);
403 } /* int plugin_register_config */
404
405 int plugin_register_complex_config (const char *type,
406                 int (*callback) (oconfig_item_t *))
407 {
408         return (cf_register_complex (type, callback));
409 } /* int plugin_register_complex_config */
410
411 int plugin_register_init (const char *name,
412                 int (*callback) (void))
413 {
414         return (register_callback (&list_init, name, (void *) callback));
415 } /* plugin_register_init */
416
417 int plugin_register_read (const char *name,
418                 int (*callback) (void))
419 {
420         read_func_t *rf;
421
422         rf = (read_func_t *) malloc (sizeof (read_func_t));
423         if (rf == NULL)
424         {
425                 char errbuf[1024];
426                 ERROR ("plugin_register_read: malloc failed: %s",
427                                 sstrerror (errno, errbuf, sizeof (errbuf)));
428                 return (-1);
429         }
430
431         memset (rf, '\0', sizeof (read_func_t));
432         rf->wait_time = interval_g;
433         rf->wait_left = 0;
434         rf->callback = callback;
435         rf->needs_read = DONE;
436
437         return (register_callback (&list_read, name, (void *) rf));
438 } /* int plugin_register_read */
439
440 int plugin_register_write (const char *name,
441                 int (*callback) (const data_set_t *ds, const value_list_t *vl))
442 {
443         return (register_callback (&list_write, name, (void *) callback));
444 } /* int plugin_register_write */
445
446 int plugin_register_flush (const char *name,
447                 int (*callback) (const int timeout, const char *identifier))
448 {
449         return (register_callback (&list_flush, name, (void *) callback));
450 } /* int plugin_register_flush */
451
452 int plugin_register_shutdown (char *name,
453                 int (*callback) (void))
454 {
455         return (register_callback (&list_shutdown, name, (void *) callback));
456 } /* int plugin_register_shutdown */
457
458 int plugin_register_data_set (const data_set_t *ds)
459 {
460         data_set_t *ds_copy;
461         int i;
462
463         if ((data_sets != NULL)
464                         && (c_avl_get (data_sets, ds->type, NULL) == 0))
465         {
466                 NOTICE ("Replacing DS `%s' with another version.", ds->type);
467                 plugin_unregister_data_set (ds->type);
468         }
469         else if (data_sets == NULL)
470         {
471                 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
472                 if (data_sets == NULL)
473                         return (-1);
474         }
475
476         ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
477         if (ds_copy == NULL)
478                 return (-1);
479         memcpy(ds_copy, ds, sizeof (data_set_t));
480
481         ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
482                         * ds->ds_num);
483         if (ds_copy->ds == NULL)
484         {
485                 free (ds_copy);
486                 return (-1);
487         }
488
489         for (i = 0; i < ds->ds_num; i++)
490                 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
491
492         return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
493 } /* int plugin_register_data_set */
494
495 int plugin_register_log (char *name,
496                 void (*callback) (int priority, const char *msg))
497 {
498         return (register_callback (&list_log, name, (void *) callback));
499 } /* int plugin_register_log */
500
501 int plugin_register_notification (const char *name,
502                 int (*callback) (const notification_t *notif))
503 {
504         return (register_callback (&list_notification, name, (void *) callback));
505 } /* int plugin_register_log */
506
507 int plugin_unregister_config (const char *name)
508 {
509         cf_unregister (name);
510         return (0);
511 } /* int plugin_unregister_config */
512
513 int plugin_unregister_complex_config (const char *name)
514 {
515         cf_unregister_complex (name);
516         return (0);
517 } /* int plugin_unregister_complex_config */
518
519 int plugin_unregister_init (const char *name)
520 {
521         return (plugin_unregister (list_init, name));
522 }
523
524 int plugin_unregister_read (const char *name)
525 {
526         llentry_t *e;
527
528         e = llist_search (list_read, name);
529
530         if (e == NULL)
531                 return (-1);
532
533         llist_remove (list_read, e);
534         free (e->value);
535         free (e->key);
536         llentry_destroy (e);
537
538         return (0);
539 }
540
541 int plugin_unregister_write (const char *name)
542 {
543         return (plugin_unregister (list_write, name));
544 }
545
546 int plugin_unregister_flush (const char *name)
547 {
548         return (plugin_unregister (list_flush, name));
549 }
550
551 int plugin_unregister_shutdown (const char *name)
552 {
553         return (plugin_unregister (list_shutdown, name));
554 }
555
556 int plugin_unregister_data_set (const char *name)
557 {
558         data_set_t *ds;
559
560         if (data_sets == NULL)
561                 return (-1);
562
563         if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
564                 return (-1);
565
566         sfree (ds->ds);
567         sfree (ds);
568
569         return (0);
570 } /* int plugin_unregister_data_set */
571
572 int plugin_unregister_log (const char *name)
573 {
574         return (plugin_unregister (list_log, name));
575 }
576
577 int plugin_unregister_notification (const char *name)
578 {
579         return (plugin_unregister (list_notification, name));
580 }
581
582 void plugin_init_all (void)
583 {
584         int (*callback) (void);
585         llentry_t *le;
586         int status;
587
588         /* Init the value cache */
589         uc_init ();
590
591         if ((list_init == NULL) && (list_read == NULL))
592                 return;
593
594         /* Calling all init callbacks before checking if read callbacks
595          * are available allows the init callbacks to register the read
596          * callback. */
597         le = llist_head (list_init);
598         while (le != NULL)
599         {
600                 callback = (int (*) (void)) le->value;
601                 status = (*callback) ();
602
603                 if (status != 0)
604                 {
605                         ERROR ("Initialization of plugin `%s' "
606                                         "failed with status %i. "
607                                         "Plugin will be unloaded.",
608                                         le->key, status);
609                         /* Plugins that register read callbacks from the init
610                          * callback should take care of appropriate error
611                          * handling themselves. */
612                         /* FIXME: Unload _all_ functions */
613                         plugin_unregister_read (le->key);
614                 }
615
616                 le = le->next;
617         }
618
619         /* Start read-threads */
620         if (list_read != NULL)
621         {
622                 const char *rt;
623                 int num;
624                 rt = global_option_get ("ReadThreads");
625                 num = atoi (rt);
626                 start_threads ((num > 0) ? num : 5);
627         }
628 } /* void plugin_init_all */
629
630 void plugin_read_all (void)
631 {
632         llentry_t   *le;
633         read_func_t *rf;
634
635         uc_check_timeout ();
636
637         if (list_read == NULL)
638                 return;
639
640         pthread_mutex_lock (&read_lock);
641
642         le = llist_head (list_read);
643         while (le != NULL)
644         {
645                 rf = (read_func_t *) le->value;
646
647                 if (rf->needs_read != DONE)
648                 {
649                         le = le->next;
650                         continue;
651                 }
652
653                 if (rf->wait_left > 0)
654                         rf->wait_left -= interval_g;
655
656                 if (rf->wait_left <= 0)
657                 {
658                         rf->needs_read = TODO;
659                 }
660
661                 le = le->next;
662         }
663
664         DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
665         pthread_cond_broadcast (&read_cond);
666         pthread_mutex_unlock (&read_lock);
667 } /* void plugin_read_all */
668
669 int plugin_flush (const char *plugin, int timeout, const char *identifier)
670 {
671   int (*callback) (int timeout, const char *identifier);
672   llentry_t *le;
673
674   if (list_flush == NULL)
675     return (0);
676
677   le = llist_head (list_flush);
678   while (le != NULL)
679   {
680     if ((plugin != NULL)
681         && (strcmp (plugin, le->key) != 0))
682     {
683       le = le->next;
684       continue;
685     }
686
687     callback = (int (*) (int, const char *)) le->value;
688     (*callback) (timeout, identifier);
689
690     le = le->next;
691   }
692   return (0);
693 } /* int plugin_flush */
694
695 void plugin_shutdown_all (void)
696 {
697         int (*callback) (void);
698         llentry_t *le;
699
700         stop_threads ();
701
702         if (list_shutdown == NULL)
703                 return;
704
705         le = llist_head (list_shutdown);
706         while (le != NULL)
707         {
708                 callback = (int (*) (void)) le->value;
709
710                 /* Advance the pointer before calling the callback allows
711                  * shutdown functions to unregister themselves. If done the
712                  * other way around the memory `le' points to will be freed
713                  * after callback returns. */
714                 le = le->next;
715
716                 (*callback) ();
717         }
718 } /* void plugin_shutdown_all */
719
720 int plugin_dispatch_values (value_list_t *vl)
721 {
722         static c_complain_t no_write_complaint = C_COMPLAIN_INIT;
723
724         int (*callback) (const data_set_t *, const value_list_t *);
725         data_set_t *ds;
726         llentry_t *le;
727
728         if ((vl == NULL) || (*vl->type == '\0')) {
729                 ERROR ("plugin_dispatch_values: Invalid value list.");
730                 return (-1);
731         }
732
733         if (list_write == NULL)
734                 c_complain_once (LOG_WARNING, &no_write_complaint,
735                                 "plugin_dispatch_values: No write callback has been "
736                                 "registered. Please load at least one output plugin, "
737                                 "if you want the collected data to be stored.");
738
739         if (data_sets == NULL)
740         {
741                 ERROR ("plugin_dispatch_values: No data sets registered. "
742                                 "Could the types database be read? Check "
743                                 "your `TypesDB' setting!");
744                 return (-1);
745         }
746
747         if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
748         {
749                 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
750                 return (-1);
751         }
752
753         DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
754                         "host = %s; "
755                         "plugin = %s; plugin_instance = %s; "
756                         "type = %s; type_instance = %s;",
757                         (unsigned int) vl->time, vl->interval,
758                         vl->host,
759                         vl->plugin, vl->plugin_instance,
760                         vl->type, vl->type_instance);
761
762 #if COLLECT_DEBUG
763         assert (0 == strcmp (ds->type, vl->type));
764 #else
765         if (0 != strcmp (ds->type, vl->type))
766                 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
767                                 ds->type, vl->type);
768 #endif
769
770 #if COLLECT_DEBUG
771         assert (ds->ds_num == vl->values_len);
772 #else
773         if (ds->ds_num != vl->values_len)
774         {
775                 ERROR ("plugin_dispatch_values: ds->type = %s: "
776                                 "(ds->ds_num = %i) != "
777                                 "(vl->values_len = %i)",
778                                 ds->type, ds->ds_num, vl->values_len);
779                 return (-1);
780         }
781 #endif
782
783         escape_slashes (vl->host, sizeof (vl->host));
784         escape_slashes (vl->plugin, sizeof (vl->plugin));
785         escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
786         escape_slashes (vl->type, sizeof (vl->type));
787         escape_slashes (vl->type_instance, sizeof (vl->type_instance));
788
789         /* Update the value cache */
790         uc_update (ds, vl);
791         ut_check_threshold (ds, vl);
792
793         le = llist_head (list_write);
794         while (le != NULL)
795         {
796                 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
797                 (*callback) (ds, vl);
798
799                 le = le->next;
800         }
801
802         return (0);
803 } /* int plugin_dispatch_values */
804
805 int plugin_dispatch_notification (const notification_t *notif)
806 {
807         int (*callback) (const notification_t *);
808         llentry_t *le;
809         /* Possible TODO: Add flap detection here */
810
811         DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
812                         "time = %u; host = %s;",
813                         notif->severity, notif->message,
814                         (unsigned int) notif->time, notif->host);
815
816         /* Nobody cares for notifications */
817         if (list_notification == NULL)
818                 return (-1);
819
820         le = llist_head (list_notification);
821         while (le != NULL)
822         {
823                 callback = (int (*) (const notification_t *)) le->value;
824                 (*callback) (notif);
825
826                 le = le->next;
827         }
828
829         return (0);
830 } /* int plugin_dispatch_notification */
831
832 void plugin_log (int level, const char *format, ...)
833 {
834         char msg[1024];
835         va_list ap;
836
837         void (*callback) (int, const char *);
838         llentry_t *le;
839
840         if (list_log == NULL)
841                 return;
842
843 #if !COLLECT_DEBUG
844         if (level >= LOG_DEBUG)
845                 return;
846 #endif
847
848         va_start (ap, format);
849         vsnprintf (msg, sizeof (msg), format, ap);
850         msg[sizeof (msg) - 1] = '\0';
851         va_end (ap);
852
853         le = llist_head (list_log);
854         while (le != NULL)
855         {
856                 callback = (void (*) (int, const char *)) le->value;
857                 (*callback) (level, msg);
858
859                 le = le->next;
860         }
861 } /* void plugin_log */
862
863 const data_set_t *plugin_get_ds (const char *name)
864 {
865         data_set_t *ds;
866
867         if (c_avl_get (data_sets, name, (void *) &ds) != 0)
868         {
869                 DEBUG ("No such dataset registered: %s", name);
870                 return (NULL);
871         }
872
873         return (ds);
874 } /* data_set_t *plugin_get_ds */
875
876 static int plugin_notification_meta_add (notification_t *n,
877     const char *name,
878     enum notification_meta_type_e type,
879     const void *value)
880 {
881   notification_meta_t *meta;
882   notification_meta_t *tail;
883
884   if ((n == NULL) || (name == NULL) || (value == NULL))
885   {
886     ERROR ("plugin_notification_meta_add: A pointer is NULL!");
887     return (-1);
888   }
889
890   meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
891   if (meta == NULL)
892   {
893     ERROR ("plugin_notification_meta_add: malloc failed.");
894     return (-1);
895   }
896   memset (meta, 0, sizeof (notification_meta_t));
897
898   sstrncpy (meta->name, name, sizeof (meta->name));
899   meta->type = type;
900
901   switch (type)
902   {
903     case NM_TYPE_STRING:
904     {
905       meta->nm_value.nm_string = strdup ((const char *) value);
906       if (meta->nm_value.nm_string == NULL)
907       {
908         ERROR ("plugin_notification_meta_add: strdup failed.");
909         sfree (meta);
910         return (-1);
911       }
912       break;
913     }
914     case NM_TYPE_SIGNED_INT:
915     {
916       meta->nm_value.nm_signed_int = *((int64_t *) value);
917       break;
918     }
919     case NM_TYPE_UNSIGNED_INT:
920     {
921       meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
922       break;
923     }
924     case NM_TYPE_DOUBLE:
925     {
926       meta->nm_value.nm_double = *((double *) value);
927       break;
928     }
929     case NM_TYPE_BOOLEAN:
930     {
931       meta->nm_value.nm_boolean = *((bool *) value);
932       break;
933     }
934     default:
935     {
936       ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
937       sfree (meta);
938       return (-1);
939     }
940   } /* switch (type) */
941
942   meta->next = NULL;
943   tail = n->meta;
944   while ((tail != NULL) && (tail->next != NULL))
945     tail = tail->next;
946
947   if (tail == NULL)
948     n->meta = meta;
949   else
950     tail->next = meta;
951
952   return (0);
953 } /* int plugin_notification_meta_add */
954
955 int plugin_notification_meta_add_string (notification_t *n,
956     const char *name,
957     const char *value)
958 {
959   return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
960 }
961
962 int plugin_notification_meta_add_signed_int (notification_t *n,
963     const char *name,
964     int64_t value)
965 {
966   return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
967 }
968
969 int plugin_notification_meta_add_unsigned_int (notification_t *n,
970     const char *name,
971     uint64_t value)
972 {
973   return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
974 }
975
976 int plugin_notification_meta_add_double (notification_t *n,
977     const char *name,
978     double value)
979 {
980   return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
981 }
982
983 int plugin_notification_meta_add_boolean (notification_t *n,
984     const char *name,
985     bool value)
986 {
987   return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
988 }
989
990 int plugin_notification_meta_copy (notification_t *dst,
991     const notification_t *src)
992 {
993   notification_meta_t *meta;
994
995   assert (dst != NULL);
996   assert (src != NULL);
997   assert (dst != src);
998   assert ((src->meta == NULL) || (src->meta != dst->meta));
999
1000   for (meta = src->meta; meta != NULL; meta = meta->next)
1001   {
1002     if (meta->type == NM_TYPE_STRING)
1003       plugin_notification_meta_add_string (dst, meta->name,
1004           meta->nm_value.nm_string);
1005     else if (meta->type == NM_TYPE_SIGNED_INT)
1006       plugin_notification_meta_add_signed_int (dst, meta->name,
1007           meta->nm_value.nm_signed_int);
1008     else if (meta->type == NM_TYPE_UNSIGNED_INT)
1009       plugin_notification_meta_add_unsigned_int (dst, meta->name,
1010           meta->nm_value.nm_unsigned_int);
1011     else if (meta->type == NM_TYPE_DOUBLE)
1012       plugin_notification_meta_add_double (dst, meta->name,
1013           meta->nm_value.nm_double);
1014     else if (meta->type == NM_TYPE_BOOLEAN)
1015       plugin_notification_meta_add_boolean (dst, meta->name,
1016           meta->nm_value.nm_boolean);
1017   }
1018
1019   return (0);
1020 } /* int plugin_notification_meta_copy */
1021
1022 int plugin_notification_meta_free (notification_t *n)
1023 {
1024   notification_meta_t *this;
1025   notification_meta_t *next;
1026
1027   if (n == NULL)
1028   {
1029     ERROR ("plugin_notification_meta_free: n == NULL!");
1030     return (-1);
1031   }
1032
1033   this = n->meta;
1034   n->meta = NULL;
1035   while (this != NULL)
1036   {
1037     next = this->next;
1038
1039     if (this->type == NM_TYPE_STRING)
1040     {
1041       free ((char *)this->nm_value.nm_string);
1042       this->nm_value.nm_string = NULL;
1043     }
1044     sfree (this);
1045
1046     this = next;
1047   }
1048
1049   return (0);
1050 } /* int plugin_notification_meta_free */