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