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