src/plugin.c, network, rrdtool: Improved thread shutdown.
[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         INFO ("collectd: Stopping %i read threads.", read_threads_num);
408
409         pthread_mutex_lock (&read_lock);
410         read_loop = 0;
411         DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
412         pthread_cond_broadcast (&read_cond);
413         pthread_mutex_unlock (&read_lock);
414
415         for (i = 0; i < read_threads_num; i++)
416         {
417                 if (pthread_join (read_threads[i], NULL) != 0)
418                 {
419                         ERROR ("plugin: stop_read_threads: pthread_join failed.");
420                 }
421                 read_threads[i] = (pthread_t) 0;
422         }
423         sfree (read_threads);
424         read_threads_num = 0;
425 } /* void stop_read_threads */
426
427 /*
428  * Public functions
429  */
430 void plugin_set_dir (const char *dir)
431 {
432         if (plugindir != NULL)
433                 free (plugindir);
434
435         if (dir == NULL)
436                 plugindir = NULL;
437         else if ((plugindir = strdup (dir)) == NULL)
438         {
439                 char errbuf[1024];
440                 ERROR ("strdup failed: %s",
441                                 sstrerror (errno, errbuf, sizeof (errbuf)));
442         }
443 }
444
445 #define BUFSIZE 512
446 int plugin_load (const char *type)
447 {
448         DIR  *dh;
449         const char *dir;
450         char  filename[BUFSIZE] = "";
451         char  typename[BUFSIZE];
452         int   typename_len;
453         int   ret;
454         struct stat    statbuf;
455         struct dirent *de;
456         int status;
457
458         DEBUG ("type = %s", type);
459
460         dir = plugin_get_dir ();
461         ret = 1;
462
463         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
464          * type when matching the filename */
465         status = ssnprintf (typename, sizeof (typename), "%s.so", type);
466         if ((status < 0) || ((size_t) status >= sizeof (typename)))
467         {
468                 WARNING ("snprintf: truncated: `%s.so'", type);
469                 return (-1);
470         }
471         typename_len = strlen (typename);
472
473         if ((dh = opendir (dir)) == NULL)
474         {
475                 char errbuf[1024];
476                 ERROR ("opendir (%s): %s", dir,
477                                 sstrerror (errno, errbuf, sizeof (errbuf)));
478                 return (-1);
479         }
480
481         while ((de = readdir (dh)) != NULL)
482         {
483                 if (strncasecmp (de->d_name, typename, typename_len))
484                         continue;
485
486                 status = ssnprintf (filename, sizeof (filename),
487                                 "%s/%s", dir, de->d_name);
488                 if ((status < 0) || ((size_t) status >= sizeof (filename)))
489                 {
490                         WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
491                         continue;
492                 }
493
494                 if (lstat (filename, &statbuf) == -1)
495                 {
496                         char errbuf[1024];
497                         WARNING ("stat %s: %s", filename,
498                                         sstrerror (errno, errbuf, sizeof (errbuf)));
499                         continue;
500                 }
501                 else if (!S_ISREG (statbuf.st_mode))
502                 {
503                         /* don't follow symlinks */
504                         continue;
505                 }
506
507                 if (plugin_load_file (filename) == 0)
508                 {
509                         /* success */
510                         ret = 0;
511                         break;
512                 }
513                 else
514                 {
515                         fprintf (stderr, "Unable to load plugin %s.\n", type);
516                 }
517         }
518
519         closedir (dh);
520
521         if (filename[0] == '\0')
522                 fprintf (stderr, "Could not find plugin %s.\n", type);
523
524         return (ret);
525 }
526
527 /*
528  * The `register_*' functions follow
529  */
530 int plugin_register_config (const char *name,
531                 int (*callback) (const char *key, const char *val),
532                 const char **keys, int keys_num)
533 {
534         cf_register (name, callback, keys, keys_num);
535         return (0);
536 } /* int plugin_register_config */
537
538 int plugin_register_complex_config (const char *type,
539                 int (*callback) (oconfig_item_t *))
540 {
541         return (cf_register_complex (type, callback));
542 } /* int plugin_register_complex_config */
543
544 int plugin_register_init (const char *name,
545                 int (*callback) (void))
546 {
547         return (create_register_callback (&list_init, name, (void *) callback,
548                                 /* user_data = */ NULL));
549 } /* plugin_register_init */
550
551 int plugin_register_read (const char *name,
552                 int (*callback) (void))
553 {
554         read_func_t *rf;
555
556         rf = (read_func_t *) malloc (sizeof (read_func_t));
557         if (rf == NULL)
558         {
559                 char errbuf[1024];
560                 ERROR ("plugin_register_read: malloc failed: %s",
561                                 sstrerror (errno, errbuf, sizeof (errbuf)));
562                 return (-1);
563         }
564
565         memset (rf, 0, sizeof (read_func_t));
566         rf->rf_callback = (void *) callback;
567         rf->rf_udata.data = NULL;
568         rf->rf_udata.free_func = NULL;
569         rf->rf_wait_time = interval_g;
570         rf->rf_wait_left = 0;
571         rf->rf_type = RF_SIMPLE;
572         rf->rf_needs_read = DONE;
573
574         return (register_callback (&list_read, name, (callback_func_t *) rf));
575 } /* int plugin_register_read */
576
577 int plugin_register_complex_read (const char *name,
578                 plugin_read_cb callback, user_data_t *user_data)
579 {
580         read_func_t *rf;
581
582         rf = (read_func_t *) malloc (sizeof (read_func_t));
583         if (rf == NULL)
584         {
585                 ERROR ("plugin_register_complex_read: malloc failed.");
586                 return (-1);
587         }
588
589         memset (rf, 0, sizeof (read_func_t));
590         rf->rf_callback = (void *) callback;
591         rf->rf_wait_time = interval_g;
592         rf->rf_wait_left = 0;
593         rf->rf_type = RF_COMPLEX;
594         rf->rf_needs_read = DONE;
595
596         /* Set user data */
597         if (user_data == NULL)
598         {
599                 rf->rf_udata.data = NULL;
600                 rf->rf_udata.free_func = NULL;
601         }
602         else
603         {
604                 rf->rf_udata = *user_data;
605         }
606
607         return (register_callback (&list_read, name, (callback_func_t *) rf));
608 } /* int plugin_register_complex_read */
609
610 int plugin_register_write (const char *name,
611                 plugin_write_cb callback, user_data_t *ud)
612 {
613         return (create_register_callback (&list_write, name,
614                                 (void *) callback, ud));
615 } /* int plugin_register_write */
616
617 int plugin_register_flush (const char *name,
618                 plugin_flush_cb callback, user_data_t *ud)
619 {
620         return (create_register_callback (&list_flush, name,
621                                 (void *) callback, ud));
622 } /* int plugin_register_flush */
623
624 int plugin_register_shutdown (char *name,
625                 int (*callback) (void))
626 {
627         return (create_register_callback (&list_shutdown, name,
628                                 (void *) callback, /* user_data = */ NULL));
629 } /* int plugin_register_shutdown */
630
631 int plugin_register_data_set (const data_set_t *ds)
632 {
633         data_set_t *ds_copy;
634         int i;
635
636         if ((data_sets != NULL)
637                         && (c_avl_get (data_sets, ds->type, NULL) == 0))
638         {
639                 NOTICE ("Replacing DS `%s' with another version.", ds->type);
640                 plugin_unregister_data_set (ds->type);
641         }
642         else if (data_sets == NULL)
643         {
644                 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
645                 if (data_sets == NULL)
646                         return (-1);
647         }
648
649         ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
650         if (ds_copy == NULL)
651                 return (-1);
652         memcpy(ds_copy, ds, sizeof (data_set_t));
653
654         ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
655                         * ds->ds_num);
656         if (ds_copy->ds == NULL)
657         {
658                 free (ds_copy);
659                 return (-1);
660         }
661
662         for (i = 0; i < ds->ds_num; i++)
663                 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
664
665         return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
666 } /* int plugin_register_data_set */
667
668 int plugin_register_log (const char *name,
669                 plugin_log_cb callback, user_data_t *ud)
670 {
671         return (create_register_callback (&list_log, name,
672                                 (void *) callback, ud));
673 } /* int plugin_register_log */
674
675 int plugin_register_notification (const char *name,
676                 plugin_notification_cb callback, user_data_t *ud)
677 {
678         return (create_register_callback (&list_notification, name,
679                                 (void *) callback, ud));
680 } /* int plugin_register_log */
681
682 int plugin_unregister_config (const char *name)
683 {
684         cf_unregister (name);
685         return (0);
686 } /* int plugin_unregister_config */
687
688 int plugin_unregister_complex_config (const char *name)
689 {
690         cf_unregister_complex (name);
691         return (0);
692 } /* int plugin_unregister_complex_config */
693
694 int plugin_unregister_init (const char *name)
695 {
696         return (plugin_unregister (list_init, name));
697 }
698
699 int plugin_unregister_read (const char *name)
700 {
701         return (plugin_unregister (list_read, name));
702 }
703
704 int plugin_unregister_write (const char *name)
705 {
706         return (plugin_unregister (list_write, name));
707 }
708
709 int plugin_unregister_flush (const char *name)
710 {
711         return (plugin_unregister (list_flush, name));
712 }
713
714 int plugin_unregister_shutdown (const char *name)
715 {
716         return (plugin_unregister (list_shutdown, name));
717 }
718
719 int plugin_unregister_data_set (const char *name)
720 {
721         data_set_t *ds;
722
723         if (data_sets == NULL)
724                 return (-1);
725
726         if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
727                 return (-1);
728
729         sfree (ds->ds);
730         sfree (ds);
731
732         return (0);
733 } /* int plugin_unregister_data_set */
734
735 int plugin_unregister_log (const char *name)
736 {
737         return (plugin_unregister (list_log, name));
738 }
739
740 int plugin_unregister_notification (const char *name)
741 {
742         return (plugin_unregister (list_notification, name));
743 }
744
745 void plugin_init_all (void)
746 {
747         const char *chain_name;
748         llentry_t *le;
749         int status;
750
751         /* Init the value cache */
752         uc_init ();
753
754         chain_name = global_option_get ("PreCacheChain");
755         pre_cache_chain = fc_chain_get_by_name (chain_name);
756
757         chain_name = global_option_get ("PostCacheChain");
758         post_cache_chain = fc_chain_get_by_name (chain_name);
759
760
761         if ((list_init == NULL) && (list_read == NULL))
762                 return;
763
764         /* Calling all init callbacks before checking if read callbacks
765          * are available allows the init callbacks to register the read
766          * callback. */
767         le = llist_head (list_init);
768         while (le != NULL)
769         {
770                 callback_func_t *cf;
771                 plugin_init_cb callback;
772
773                 cf = le->value;
774                 callback = cf->cf_callback;
775                 status = (*callback) ();
776
777                 if (status != 0)
778                 {
779                         ERROR ("Initialization of plugin `%s' "
780                                         "failed with status %i. "
781                                         "Plugin will be unloaded.",
782                                         le->key, status);
783                         /* Plugins that register read callbacks from the init
784                          * callback should take care of appropriate error
785                          * handling themselves. */
786                         /* FIXME: Unload _all_ functions */
787                         plugin_unregister_read (le->key);
788                 }
789
790                 le = le->next;
791         }
792
793         /* Start read-threads */
794         if (list_read != NULL)
795         {
796                 const char *rt;
797                 int num;
798                 rt = global_option_get ("ReadThreads");
799                 num = atoi (rt);
800                 if (num != -1)
801                         start_read_threads ((num > 0) ? num : 5);
802         }
803 } /* void plugin_init_all */
804
805 void plugin_read_all (void)
806 {
807         llentry_t   *le;
808         read_func_t *rf;
809
810         uc_check_timeout ();
811
812         if (list_read == NULL)
813                 return;
814
815         pthread_mutex_lock (&read_lock);
816
817         le = llist_head (list_read);
818         while (le != NULL)
819         {
820                 rf = (read_func_t *) le->value;
821
822                 if (rf->rf_needs_read != DONE)
823                 {
824                         le = le->next;
825                         continue;
826                 }
827
828                 if (rf->rf_wait_left > 0)
829                         rf->rf_wait_left -= interval_g;
830
831                 if (rf->rf_wait_left <= 0)
832                 {
833                         rf->rf_needs_read = TODO;
834                 }
835
836                 le = le->next;
837         }
838
839         DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
840         pthread_cond_broadcast (&read_cond);
841         pthread_mutex_unlock (&read_lock);
842 } /* void plugin_read_all */
843
844 /* Read function called when the `-T' command line argument is given. */
845 int plugin_read_all_once (void)
846 {
847         llentry_t   *le;
848         read_func_t *rf;
849         int status;
850         int return_status = 0;
851
852         if (list_read == NULL)
853         {
854                 NOTICE ("No read-functions are registered.");
855                 return (0);
856         }
857
858         for (le = llist_head (list_read);
859              le != NULL;
860              le = le->next)
861         {
862                 rf = (read_func_t *) le->value;
863
864                 if (rf->rf_type == RF_SIMPLE)
865                 {
866                         int (*callback) (void);
867
868                         callback = rf->rf_callback;
869                         status = (*callback) ();
870                 }
871                 else
872                 {
873                         plugin_read_cb callback;
874
875                         callback = rf->rf_callback;
876                         status = (*callback) (&rf->rf_udata);
877                 }
878
879                 if (status != 0)
880                 {
881                         NOTICE ("read-function of plugin `%s' failed.",
882                                 le->key);
883                         return_status = -1;
884                 }
885         }
886
887         return (return_status);
888 } /* int plugin_read_all_once */
889
890 int plugin_write (const char *plugin, /* {{{ */
891                 const data_set_t *ds, const value_list_t *vl)
892 {
893   llentry_t *le;
894   int status;
895
896   if (vl == NULL)
897     return (EINVAL);
898
899   if (list_write == NULL)
900     return (ENOENT);
901
902   if (ds == NULL)
903   {
904     ds = plugin_get_ds (vl->type);
905     if (ds == NULL)
906     {
907       ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
908       return (ENOENT);
909     }
910   }
911
912   if (plugin == NULL)
913   {
914     int success = 0;
915     int failure = 0;
916
917     le = llist_head (list_write);
918     while (le != NULL)
919     {
920       callback_func_t *cf = le->value;
921       plugin_write_cb callback;
922
923       DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
924       callback = cf->cf_callback;
925       status = (*callback) (ds, vl, &cf->cf_udata);
926       if (status != 0)
927         failure++;
928       else
929         success++;
930
931       le = le->next;
932     }
933
934     if ((success == 0) && (failure != 0))
935       status = -1;
936     else
937       status = 0;
938   }
939   else /* plugin != NULL */
940   {
941     callback_func_t *cf;
942     plugin_write_cb callback;
943
944     le = llist_head (list_write);
945     while (le != NULL)
946     {
947       if (strcasecmp (plugin, le->key) == 0)
948         break;
949
950       le = le->next;
951     }
952
953     if (le == NULL)
954       return (ENOENT);
955
956     cf = le->value;
957
958     DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
959     callback = cf->cf_callback;
960     status = (*callback) (ds, vl, &cf->cf_udata);
961   }
962
963   return (status);
964 } /* }}} int plugin_write */
965
966 int plugin_flush (const char *plugin, int timeout, const char *identifier)
967 {
968   llentry_t *le;
969
970   if (list_flush == NULL)
971     return (0);
972
973   le = llist_head (list_flush);
974   while (le != NULL)
975   {
976     callback_func_t *cf;
977     plugin_flush_cb callback;
978
979     if ((plugin != NULL)
980         && (strcmp (plugin, le->key) != 0))
981     {
982       le = le->next;
983       continue;
984     }
985
986     cf = le->value;
987     callback = cf->cf_callback;
988
989     (*callback) (timeout, identifier, &cf->cf_udata);
990
991     le = le->next;
992   }
993   return (0);
994 } /* int plugin_flush */
995
996 void plugin_shutdown_all (void)
997 {
998         llentry_t *le;
999
1000         stop_read_threads ();
1001
1002         destroy_all_callbacks (&list_init);
1003         destroy_all_callbacks (&list_read);
1004
1005         plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1006                         /* identifier = */ NULL);
1007
1008         le = NULL;
1009         if (list_shutdown != NULL)
1010                 le = llist_head (list_shutdown);
1011
1012         while (le != NULL)
1013         {
1014                 callback_func_t *cf;
1015                 plugin_shutdown_cb callback;
1016
1017                 cf = le->value;
1018                 callback = cf->cf_callback;
1019
1020                 /* Advance the pointer before calling the callback allows
1021                  * shutdown functions to unregister themselves. If done the
1022                  * other way around the memory `le' points to will be freed
1023                  * after callback returns. */
1024                 le = le->next;
1025
1026                 (*callback) ();
1027         }
1028
1029         destroy_all_callbacks (&list_write);
1030         destroy_all_callbacks (&list_flush);
1031         destroy_all_callbacks (&list_notification);
1032         destroy_all_callbacks (&list_shutdown);
1033         destroy_all_callbacks (&list_log);
1034 } /* void plugin_shutdown_all */
1035
1036 int plugin_dispatch_values (value_list_t *vl)
1037 {
1038         int status;
1039         static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1040
1041         value_t *saved_values;
1042         int      saved_values_len;
1043
1044         data_set_t *ds;
1045
1046         if ((vl == NULL) || (vl->type[0] == 0)
1047                         || (vl->values == NULL) || (vl->values_len < 1))
1048         {
1049                 ERROR ("plugin_dispatch_values: Invalid value list.");
1050                 return (-1);
1051         }
1052
1053         if (list_write == NULL)
1054                 c_complain_once (LOG_WARNING, &no_write_complaint,
1055                                 "plugin_dispatch_values: No write callback has been "
1056                                 "registered. Please load at least one output plugin, "
1057                                 "if you want the collected data to be stored.");
1058
1059         if (data_sets == NULL)
1060         {
1061                 ERROR ("plugin_dispatch_values: No data sets registered. "
1062                                 "Could the types database be read? Check "
1063                                 "your `TypesDB' setting!");
1064                 return (-1);
1065         }
1066
1067         if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1068         {
1069                 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1070                 return (-1);
1071         }
1072
1073         if (vl->time == 0)
1074                 vl->time = time (NULL);
1075
1076         DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1077                         "host = %s; "
1078                         "plugin = %s; plugin_instance = %s; "
1079                         "type = %s; type_instance = %s;",
1080                         (unsigned int) vl->time, vl->interval,
1081                         vl->host,
1082                         vl->plugin, vl->plugin_instance,
1083                         vl->type, vl->type_instance);
1084
1085 #if COLLECT_DEBUG
1086         assert (0 == strcmp (ds->type, vl->type));
1087 #else
1088         if (0 != strcmp (ds->type, vl->type))
1089                 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1090                                 ds->type, vl->type);
1091 #endif
1092
1093 #if COLLECT_DEBUG
1094         assert (ds->ds_num == vl->values_len);
1095 #else
1096         if (ds->ds_num != vl->values_len)
1097         {
1098                 ERROR ("plugin_dispatch_values: ds->type = %s: "
1099                                 "(ds->ds_num = %i) != "
1100                                 "(vl->values_len = %i)",
1101                                 ds->type, ds->ds_num, vl->values_len);
1102                 return (-1);
1103         }
1104 #endif
1105
1106         escape_slashes (vl->host, sizeof (vl->host));
1107         escape_slashes (vl->plugin, sizeof (vl->plugin));
1108         escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1109         escape_slashes (vl->type, sizeof (vl->type));
1110         escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1111
1112         /* Copy the values. This way, we can assure `targets' that they get
1113          * dynamically allocated values, which they can free and replace if
1114          * they like. */
1115         if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1116         {
1117                 saved_values     = vl->values;
1118                 saved_values_len = vl->values_len;
1119
1120                 vl->values = (value_t *) calloc (vl->values_len,
1121                                 sizeof (*vl->values));
1122                 if (vl->values == NULL)
1123                 {
1124                         ERROR ("plugin_dispatch_values: calloc failed.");
1125                         vl->values = saved_values;
1126                         return (-1);
1127                 }
1128                 memcpy (vl->values, saved_values,
1129                                 vl->values_len * sizeof (*vl->values));
1130         }
1131         else /* if ((pre == NULL) && (post == NULL)) */
1132         {
1133                 saved_values     = NULL;
1134                 saved_values_len = 0;
1135         }
1136
1137         if (pre_cache_chain != NULL)
1138         {
1139                 status = fc_process_chain (ds, vl, pre_cache_chain);
1140                 if (status < 0)
1141                 {
1142                         WARNING ("plugin_dispatch_values: Running the "
1143                                         "pre-cache chain failed with "
1144                                         "status %i (%#x).",
1145                                         status, status);
1146                 }
1147                 else if (status == FC_TARGET_STOP)
1148                 {
1149                         /* Restore the state of the value_list so that plugins
1150                          * don't get confused.. */
1151                         if (saved_values != NULL)
1152                         {
1153                                 free (vl->values);
1154                                 vl->values     = saved_values;
1155                                 vl->values_len = saved_values_len;
1156                         }
1157                         return (0);
1158                 }
1159         }
1160
1161         /* Update the value cache */
1162         uc_update (ds, vl);
1163
1164         if (post_cache_chain != NULL)
1165         {
1166                 status = fc_process_chain (ds, vl, post_cache_chain);
1167                 if (status < 0)
1168                 {
1169                         WARNING ("plugin_dispatch_values: Running the "
1170                                         "post-cache chain failed with "
1171                                         "status %i (%#x).",
1172                                         status, status);
1173                 }
1174         }
1175         else
1176                 fc_default_action (ds, vl);
1177
1178         /* Restore the state of the value_list so that plugins don't get
1179          * confused.. */
1180         if (saved_values != NULL)
1181         {
1182                 free (vl->values);
1183                 vl->values     = saved_values;
1184                 vl->values_len = saved_values_len;
1185         }
1186
1187         return (0);
1188 } /* int plugin_dispatch_values */
1189
1190 int plugin_dispatch_notification (const notification_t *notif)
1191 {
1192         llentry_t *le;
1193         /* Possible TODO: Add flap detection here */
1194
1195         DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1196                         "time = %u; host = %s;",
1197                         notif->severity, notif->message,
1198                         (unsigned int) notif->time, notif->host);
1199
1200         /* Nobody cares for notifications */
1201         if (list_notification == NULL)
1202                 return (-1);
1203
1204         le = llist_head (list_notification);
1205         while (le != NULL)
1206         {
1207                 callback_func_t *cf;
1208                 plugin_notification_cb callback;
1209                 int status;
1210
1211                 cf = le->value;
1212                 callback = cf->cf_callback;
1213                 status = (*callback) (notif, &cf->cf_udata);
1214                 if (status != 0)
1215                 {
1216                         WARNING ("plugin_dispatch_notification: Notification "
1217                                         "callback %s returned %i.",
1218                                         le->key, status);
1219                 }
1220
1221                 le = le->next;
1222         }
1223
1224         return (0);
1225 } /* int plugin_dispatch_notification */
1226
1227 void plugin_log (int level, const char *format, ...)
1228 {
1229         char msg[1024];
1230         va_list ap;
1231         llentry_t *le;
1232
1233         if (list_log == NULL)
1234                 return;
1235
1236 #if !COLLECT_DEBUG
1237         if (level >= LOG_DEBUG)
1238                 return;
1239 #endif
1240
1241         va_start (ap, format);
1242         vsnprintf (msg, sizeof (msg), format, ap);
1243         msg[sizeof (msg) - 1] = '\0';
1244         va_end (ap);
1245
1246         le = llist_head (list_log);
1247         while (le != NULL)
1248         {
1249                 callback_func_t *cf;
1250                 plugin_log_cb callback;
1251
1252                 cf = le->value;
1253                 callback = cf->cf_callback;
1254
1255                 (*callback) (level, msg, &cf->cf_udata);
1256
1257                 le = le->next;
1258         }
1259 } /* void plugin_log */
1260
1261 const data_set_t *plugin_get_ds (const char *name)
1262 {
1263         data_set_t *ds;
1264
1265         if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1266         {
1267                 DEBUG ("No such dataset registered: %s", name);
1268                 return (NULL);
1269         }
1270
1271         return (ds);
1272 } /* data_set_t *plugin_get_ds */
1273
1274 static int plugin_notification_meta_add (notification_t *n,
1275     const char *name,
1276     enum notification_meta_type_e type,
1277     const void *value)
1278 {
1279   notification_meta_t *meta;
1280   notification_meta_t *tail;
1281
1282   if ((n == NULL) || (name == NULL) || (value == NULL))
1283   {
1284     ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1285     return (-1);
1286   }
1287
1288   meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1289   if (meta == NULL)
1290   {
1291     ERROR ("plugin_notification_meta_add: malloc failed.");
1292     return (-1);
1293   }
1294   memset (meta, 0, sizeof (notification_meta_t));
1295
1296   sstrncpy (meta->name, name, sizeof (meta->name));
1297   meta->type = type;
1298
1299   switch (type)
1300   {
1301     case NM_TYPE_STRING:
1302     {
1303       meta->nm_value.nm_string = strdup ((const char *) value);
1304       if (meta->nm_value.nm_string == NULL)
1305       {
1306         ERROR ("plugin_notification_meta_add: strdup failed.");
1307         sfree (meta);
1308         return (-1);
1309       }
1310       break;
1311     }
1312     case NM_TYPE_SIGNED_INT:
1313     {
1314       meta->nm_value.nm_signed_int = *((int64_t *) value);
1315       break;
1316     }
1317     case NM_TYPE_UNSIGNED_INT:
1318     {
1319       meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1320       break;
1321     }
1322     case NM_TYPE_DOUBLE:
1323     {
1324       meta->nm_value.nm_double = *((double *) value);
1325       break;
1326     }
1327     case NM_TYPE_BOOLEAN:
1328     {
1329       meta->nm_value.nm_boolean = *((bool *) value);
1330       break;
1331     }
1332     default:
1333     {
1334       ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1335       sfree (meta);
1336       return (-1);
1337     }
1338   } /* switch (type) */
1339
1340   meta->next = NULL;
1341   tail = n->meta;
1342   while ((tail != NULL) && (tail->next != NULL))
1343     tail = tail->next;
1344
1345   if (tail == NULL)
1346     n->meta = meta;
1347   else
1348     tail->next = meta;
1349
1350   return (0);
1351 } /* int plugin_notification_meta_add */
1352
1353 int plugin_notification_meta_add_string (notification_t *n,
1354     const char *name,
1355     const char *value)
1356 {
1357   return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1358 }
1359
1360 int plugin_notification_meta_add_signed_int (notification_t *n,
1361     const char *name,
1362     int64_t value)
1363 {
1364   return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1365 }
1366
1367 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1368     const char *name,
1369     uint64_t value)
1370 {
1371   return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1372 }
1373
1374 int plugin_notification_meta_add_double (notification_t *n,
1375     const char *name,
1376     double value)
1377 {
1378   return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1379 }
1380
1381 int plugin_notification_meta_add_boolean (notification_t *n,
1382     const char *name,
1383     bool value)
1384 {
1385   return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1386 }
1387
1388 int plugin_notification_meta_copy (notification_t *dst,
1389     const notification_t *src)
1390 {
1391   notification_meta_t *meta;
1392
1393   assert (dst != NULL);
1394   assert (src != NULL);
1395   assert (dst != src);
1396   assert ((src->meta == NULL) || (src->meta != dst->meta));
1397
1398   for (meta = src->meta; meta != NULL; meta = meta->next)
1399   {
1400     if (meta->type == NM_TYPE_STRING)
1401       plugin_notification_meta_add_string (dst, meta->name,
1402           meta->nm_value.nm_string);
1403     else if (meta->type == NM_TYPE_SIGNED_INT)
1404       plugin_notification_meta_add_signed_int (dst, meta->name,
1405           meta->nm_value.nm_signed_int);
1406     else if (meta->type == NM_TYPE_UNSIGNED_INT)
1407       plugin_notification_meta_add_unsigned_int (dst, meta->name,
1408           meta->nm_value.nm_unsigned_int);
1409     else if (meta->type == NM_TYPE_DOUBLE)
1410       plugin_notification_meta_add_double (dst, meta->name,
1411           meta->nm_value.nm_double);
1412     else if (meta->type == NM_TYPE_BOOLEAN)
1413       plugin_notification_meta_add_boolean (dst, meta->name,
1414           meta->nm_value.nm_boolean);
1415   }
1416
1417   return (0);
1418 } /* int plugin_notification_meta_copy */
1419
1420 int plugin_notification_meta_free (notification_meta_t *n)
1421 {
1422   notification_meta_t *this;
1423   notification_meta_t *next;
1424
1425   if (n == NULL)
1426   {
1427     ERROR ("plugin_notification_meta_free: n == NULL!");
1428     return (-1);
1429   }
1430
1431   this = n;
1432   while (this != NULL)
1433   {
1434     next = this->next;
1435
1436     if (this->type == NM_TYPE_STRING)
1437     {
1438       free ((char *)this->nm_value.nm_string);
1439       this->nm_value.nm_string = NULL;
1440     }
1441     sfree (this);
1442
1443     this = next;
1444   }
1445
1446   return (0);
1447 } /* int plugin_notification_meta_free */
1448
1449 /* vim: set sw=8 ts=8 noet fdm=marker : */