src/plugin.c: Add the `{Pre,Post}CacheChain' options and appropriate chains.
[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         static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
848
849         data_set_t *ds;
850
851         if ((vl == NULL) || (*vl->type == '\0')) {
852                 ERROR ("plugin_dispatch_values: Invalid value list.");
853                 return (-1);
854         }
855
856         if (list_write == NULL)
857                 c_complain_once (LOG_WARNING, &no_write_complaint,
858                                 "plugin_dispatch_values: No write callback has been "
859                                 "registered. Please load at least one output plugin, "
860                                 "if you want the collected data to be stored.");
861
862         if (data_sets == NULL)
863         {
864                 ERROR ("plugin_dispatch_values: No data sets registered. "
865                                 "Could the types database be read? Check "
866                                 "your `TypesDB' setting!");
867                 return (-1);
868         }
869
870         if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
871         {
872                 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
873                 return (-1);
874         }
875
876         DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
877                         "host = %s; "
878                         "plugin = %s; plugin_instance = %s; "
879                         "type = %s; type_instance = %s;",
880                         (unsigned int) vl->time, vl->interval,
881                         vl->host,
882                         vl->plugin, vl->plugin_instance,
883                         vl->type, vl->type_instance);
884
885 #if COLLECT_DEBUG
886         assert (0 == strcmp (ds->type, vl->type));
887 #else
888         if (0 != strcmp (ds->type, vl->type))
889                 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
890                                 ds->type, vl->type);
891 #endif
892
893 #if COLLECT_DEBUG
894         assert (ds->ds_num == vl->values_len);
895 #else
896         if (ds->ds_num != vl->values_len)
897         {
898                 ERROR ("plugin_dispatch_values: ds->type = %s: "
899                                 "(ds->ds_num = %i) != "
900                                 "(vl->values_len = %i)",
901                                 ds->type, ds->ds_num, vl->values_len);
902                 return (-1);
903         }
904 #endif
905
906         escape_slashes (vl->host, sizeof (vl->host));
907         escape_slashes (vl->plugin, sizeof (vl->plugin));
908         escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
909         escape_slashes (vl->type, sizeof (vl->type));
910         escape_slashes (vl->type_instance, sizeof (vl->type_instance));
911
912         if (pre_cache_chain != NULL)
913                 fc_process_chain (ds, vl, pre_cache_chain);
914
915         /* Update the value cache */
916         uc_update (ds, vl);
917
918         if (post_cache_chain != NULL)
919                 fc_process_chain (ds, vl, post_cache_chain);
920         else
921                 fc_default_action (ds, vl);
922
923         return (0);
924 } /* int plugin_dispatch_values */
925
926 int plugin_dispatch_notification (const notification_t *notif)
927 {
928         int (*callback) (const notification_t *);
929         llentry_t *le;
930         /* Possible TODO: Add flap detection here */
931
932         DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
933                         "time = %u; host = %s;",
934                         notif->severity, notif->message,
935                         (unsigned int) notif->time, notif->host);
936
937         /* Nobody cares for notifications */
938         if (list_notification == NULL)
939                 return (-1);
940
941         le = llist_head (list_notification);
942         while (le != NULL)
943         {
944                 callback = (int (*) (const notification_t *)) le->value;
945                 (*callback) (notif);
946
947                 le = le->next;
948         }
949
950         return (0);
951 } /* int plugin_dispatch_notification */
952
953 void plugin_log (int level, const char *format, ...)
954 {
955         char msg[1024];
956         va_list ap;
957
958         void (*callback) (int, const char *);
959         llentry_t *le;
960
961         if (list_log == NULL)
962                 return;
963
964 #if !COLLECT_DEBUG
965         if (level >= LOG_DEBUG)
966                 return;
967 #endif
968
969         va_start (ap, format);
970         vsnprintf (msg, sizeof (msg), format, ap);
971         msg[sizeof (msg) - 1] = '\0';
972         va_end (ap);
973
974         le = llist_head (list_log);
975         while (le != NULL)
976         {
977                 callback = (void (*) (int, const char *)) le->value;
978                 (*callback) (level, msg);
979
980                 le = le->next;
981         }
982 } /* void plugin_log */
983
984 const data_set_t *plugin_get_ds (const char *name)
985 {
986         data_set_t *ds;
987
988         if (c_avl_get (data_sets, name, (void *) &ds) != 0)
989         {
990                 DEBUG ("No such dataset registered: %s", name);
991                 return (NULL);
992         }
993
994         return (ds);
995 } /* data_set_t *plugin_get_ds */
996
997 static int plugin_notification_meta_add (notification_t *n,
998     const char *name,
999     enum notification_meta_type_e type,
1000     const void *value)
1001 {
1002   notification_meta_t *meta;
1003   notification_meta_t *tail;
1004
1005   if ((n == NULL) || (name == NULL) || (value == NULL))
1006   {
1007     ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1008     return (-1);
1009   }
1010
1011   meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1012   if (meta == NULL)
1013   {
1014     ERROR ("plugin_notification_meta_add: malloc failed.");
1015     return (-1);
1016   }
1017   memset (meta, 0, sizeof (notification_meta_t));
1018
1019   sstrncpy (meta->name, name, sizeof (meta->name));
1020   meta->type = type;
1021
1022   switch (type)
1023   {
1024     case NM_TYPE_STRING:
1025     {
1026       meta->nm_value.nm_string = strdup ((const char *) value);
1027       if (meta->nm_value.nm_string == NULL)
1028       {
1029         ERROR ("plugin_notification_meta_add: strdup failed.");
1030         sfree (meta);
1031         return (-1);
1032       }
1033       break;
1034     }
1035     case NM_TYPE_SIGNED_INT:
1036     {
1037       meta->nm_value.nm_signed_int = *((int64_t *) value);
1038       break;
1039     }
1040     case NM_TYPE_UNSIGNED_INT:
1041     {
1042       meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1043       break;
1044     }
1045     case NM_TYPE_DOUBLE:
1046     {
1047       meta->nm_value.nm_double = *((double *) value);
1048       break;
1049     }
1050     case NM_TYPE_BOOLEAN:
1051     {
1052       meta->nm_value.nm_boolean = *((bool *) value);
1053       break;
1054     }
1055     default:
1056     {
1057       ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1058       sfree (meta);
1059       return (-1);
1060     }
1061   } /* switch (type) */
1062
1063   meta->next = NULL;
1064   tail = n->meta;
1065   while ((tail != NULL) && (tail->next != NULL))
1066     tail = tail->next;
1067
1068   if (tail == NULL)
1069     n->meta = meta;
1070   else
1071     tail->next = meta;
1072
1073   return (0);
1074 } /* int plugin_notification_meta_add */
1075
1076 int plugin_notification_meta_add_string (notification_t *n,
1077     const char *name,
1078     const char *value)
1079 {
1080   return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1081 }
1082
1083 int plugin_notification_meta_add_signed_int (notification_t *n,
1084     const char *name,
1085     int64_t value)
1086 {
1087   return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1088 }
1089
1090 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1091     const char *name,
1092     uint64_t value)
1093 {
1094   return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1095 }
1096
1097 int plugin_notification_meta_add_double (notification_t *n,
1098     const char *name,
1099     double value)
1100 {
1101   return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1102 }
1103
1104 int plugin_notification_meta_add_boolean (notification_t *n,
1105     const char *name,
1106     bool value)
1107 {
1108   return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1109 }
1110
1111 int plugin_notification_meta_copy (notification_t *dst,
1112     const notification_t *src)
1113 {
1114   notification_meta_t *meta;
1115
1116   assert (dst != NULL);
1117   assert (src != NULL);
1118   assert (dst != src);
1119   assert ((src->meta == NULL) || (src->meta != dst->meta));
1120
1121   for (meta = src->meta; meta != NULL; meta = meta->next)
1122   {
1123     if (meta->type == NM_TYPE_STRING)
1124       plugin_notification_meta_add_string (dst, meta->name,
1125           meta->nm_value.nm_string);
1126     else if (meta->type == NM_TYPE_SIGNED_INT)
1127       plugin_notification_meta_add_signed_int (dst, meta->name,
1128           meta->nm_value.nm_signed_int);
1129     else if (meta->type == NM_TYPE_UNSIGNED_INT)
1130       plugin_notification_meta_add_unsigned_int (dst, meta->name,
1131           meta->nm_value.nm_unsigned_int);
1132     else if (meta->type == NM_TYPE_DOUBLE)
1133       plugin_notification_meta_add_double (dst, meta->name,
1134           meta->nm_value.nm_double);
1135     else if (meta->type == NM_TYPE_BOOLEAN)
1136       plugin_notification_meta_add_boolean (dst, meta->name,
1137           meta->nm_value.nm_boolean);
1138   }
1139
1140   return (0);
1141 } /* int plugin_notification_meta_copy */
1142
1143 int plugin_notification_meta_free (notification_t *n)
1144 {
1145   notification_meta_t *this;
1146   notification_meta_t *next;
1147
1148   if (n == NULL)
1149   {
1150     ERROR ("plugin_notification_meta_free: n == NULL!");
1151     return (-1);
1152   }
1153
1154   this = n->meta;
1155   n->meta = NULL;
1156   while (this != NULL)
1157   {
1158     next = this->next;
1159
1160     if (this->type == NM_TYPE_STRING)
1161     {
1162       free ((char *)this->nm_value.nm_string);
1163       this->nm_value.nm_string = NULL;
1164     }
1165     sfree (this);
1166
1167     this = next;
1168   }
1169
1170   return (0);
1171 } /* int plugin_notification_meta_free */