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