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