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