2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2011 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at collectd.org>
20 * Sebastian Harl <sh at tokkee.org>
24 #include "utils_complain.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_heap.h"
38 #include "utils_cache.h"
39 #include "filter_chain.h"
44 struct callback_func_s
50 typedef struct callback_func_s callback_func_t;
54 #define RF_REMOVE 65535
57 /* `read_func_t' "inherits" from `callback_func_t'.
58 * The `rf_super' member MUST be the first one in this structure! */
59 #define rf_callback rf_super.cf_callback
60 #define rf_udata rf_super.cf_udata
61 #define rf_ctx rf_super.cf_ctx
62 callback_func_t rf_super;
63 char rf_group[DATA_MAX_NAME_LEN];
64 char rf_name[DATA_MAX_NAME_LEN];
66 struct timespec rf_interval;
67 struct timespec rf_effective_interval;
68 struct timespec rf_next_read;
70 typedef struct read_func_s read_func_t;
75 static llist_t *list_init;
76 static llist_t *list_write;
77 static llist_t *list_flush;
78 static llist_t *list_missing;
79 static llist_t *list_shutdown;
80 static llist_t *list_log;
81 static llist_t *list_notification;
83 static fc_chain_t *pre_cache_chain = NULL;
84 static fc_chain_t *post_cache_chain = NULL;
86 static c_avl_tree_t *data_sets;
88 static char *plugindir = NULL;
90 static c_heap_t *read_heap = NULL;
91 static llist_t *read_list;
92 static int read_loop = 1;
93 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
94 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
95 static pthread_t *read_threads = NULL;
96 static int read_threads_num = 0;
98 static pthread_key_t plugin_ctx_key;
103 static const char *plugin_get_dir (void)
105 if (plugindir == NULL)
111 static void destroy_callback (callback_func_t *cf) /* {{{ */
116 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
118 cf->cf_udata.free_func (cf->cf_udata.data);
119 cf->cf_udata.data = NULL;
120 cf->cf_udata.free_func = NULL;
123 } /* }}} void destroy_callback */
125 static void destroy_all_callbacks (llist_t **list) /* {{{ */
132 le = llist_head (*list);
140 destroy_callback (le->value);
146 llist_destroy (*list);
148 } /* }}} void destroy_all_callbacks */
150 static void destroy_read_heap (void) /* {{{ */
152 if (read_heap == NULL)
159 cf = c_heap_get_root (read_heap);
163 destroy_callback (cf);
166 c_heap_destroy (read_heap);
168 } /* }}} void destroy_read_heap */
170 static int register_callback (llist_t **list, /* {{{ */
171 const char *name, callback_func_t *cf)
178 *list = llist_create ();
181 ERROR ("plugin: register_callback: "
182 "llist_create failed.");
183 destroy_callback (cf);
191 ERROR ("plugin: register_callback: strdup failed.");
192 destroy_callback (cf);
196 le = llist_search (*list, name);
199 le = llentry_create (key, cf);
202 ERROR ("plugin: register_callback: "
203 "llentry_create failed.");
205 destroy_callback (cf);
209 llist_append (*list, le);
213 callback_func_t *old_cf;
218 WARNING ("plugin: register_callback: "
219 "a callback named `%s' already exists - "
220 "overwriting the old entry!", name);
222 destroy_callback (old_cf);
227 } /* }}} int register_callback */
229 static int create_register_callback (llist_t **list, /* {{{ */
230 const char *name, void *callback, user_data_t *ud)
234 cf = (callback_func_t *) malloc (sizeof (*cf));
237 ERROR ("plugin: create_register_callback: malloc failed.");
240 memset (cf, 0, sizeof (*cf));
242 cf->cf_callback = callback;
245 cf->cf_udata.data = NULL;
246 cf->cf_udata.free_func = NULL;
253 cf->cf_ctx = plugin_get_ctx ();
255 return (register_callback (list, name, cf));
256 } /* }}} int create_register_callback */
258 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
265 e = llist_search (list, name);
269 llist_remove (list, e);
272 destroy_callback (e->value);
277 } /* }}} int plugin_unregister */
280 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
281 * object, but it will bitch about a shared object not having a
282 * ``module_register'' symbol..
284 static int plugin_load_file (char *file, uint32_t flags)
287 void (*reg_handle) (void);
290 lt_dlerror (); /* clear errors */
292 #if LIBTOOL_VERSION == 2
293 if (flags & PLUGIN_FLAGS_GLOBAL) {
295 lt_dladvise_init(&advise);
296 lt_dladvise_global(&advise);
297 dlh = lt_dlopenadvise(file, advise);
298 lt_dladvise_destroy(&advise);
300 dlh = lt_dlopen (file);
302 #else /* if LIBTOOL_VERSION == 1 */
303 if (flags & PLUGIN_FLAGS_GLOBAL)
304 WARNING ("plugin_load_file: The global flag is not supported, "
305 "libtool 2 is required for this.");
306 dlh = lt_dlopen (file);
311 char errbuf[1024] = "";
313 ssnprintf (errbuf, sizeof (errbuf),
314 "lt_dlopen (\"%s\") failed: %s. "
315 "The most common cause for this problem are "
316 "missing dependencies. Use ldd(1) to check "
317 "the dependencies of the plugin "
319 file, lt_dlerror ());
321 ERROR ("%s", errbuf);
322 /* Make sure this is printed to STDERR in any case, but also
323 * make sure it's printed only once. */
324 if (list_log != NULL)
325 fprintf (stderr, "ERROR: %s\n", errbuf);
330 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
332 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
333 file, lt_dlerror ());
343 static _Bool timeout_reached(struct timespec timeout)
346 gettimeofday(&now, NULL);
347 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
350 static void *plugin_read_thread (void __attribute__((unused)) *args)
352 while (read_loop != 0)
355 plugin_ctx_t old_ctx;
361 /* Get the read function that needs to be read next. */
362 rf = c_heap_get_root (read_heap);
365 struct timespec abstime;
369 CDTIME_T_TO_TIMESPEC (now + interval_g, &abstime);
371 pthread_mutex_lock (&read_lock);
372 pthread_cond_timedwait (&read_cond, &read_lock,
374 pthread_mutex_unlock (&read_lock);
378 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
382 CDTIME_T_TO_TIMESPEC (interval_g, &rf->rf_interval);
384 rf->rf_effective_interval = rf->rf_interval;
386 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
389 /* sleep until this entry is due,
390 * using pthread_cond_timedwait */
391 pthread_mutex_lock (&read_lock);
392 /* In pthread_cond_timedwait, spurious wakeups are possible
393 * (and really happen, at least on NetBSD with > 1 CPU), thus
394 * we need to re-evaluate the condition every time
395 * pthread_cond_timedwait returns. */
397 while ((read_loop != 0)
398 && !timeout_reached(rf->rf_next_read)
401 rc = pthread_cond_timedwait (&read_cond, &read_lock,
405 /* Must hold `read_lock' when accessing `rf->rf_type'. */
406 rf_type = rf->rf_type;
407 pthread_mutex_unlock (&read_lock);
409 /* Check if we're supposed to stop.. This may have interrupted
413 /* Insert `rf' again, so it can be free'd correctly */
414 c_heap_insert (read_heap, rf);
418 /* The entry has been marked for deletion. The linked list
419 * entry has already been removed by `plugin_unregister_read'.
420 * All we have to do here is free the `read_func_t' and
422 if (rf_type == RF_REMOVE)
424 DEBUG ("plugin_read_thread: Destroying the `%s' "
425 "callback.", rf->rf_name);
426 destroy_callback ((callback_func_t *) rf);
431 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
433 old_ctx = plugin_set_ctx (rf->rf_ctx);
435 if (rf_type == RF_SIMPLE)
437 int (*callback) (void);
439 callback = rf->rf_callback;
440 status = (*callback) ();
444 plugin_read_cb callback;
446 assert (rf_type == RF_COMPLEX);
448 callback = rf->rf_callback;
449 status = (*callback) (&rf->rf_udata);
452 plugin_set_ctx (old_ctx);
454 /* If the function signals failure, we will increase the
455 * intervals in which it will be called. */
458 rf->rf_effective_interval.tv_sec *= 2;
459 rf->rf_effective_interval.tv_nsec *= 2;
460 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
462 if (rf->rf_effective_interval.tv_sec >= 86400)
464 rf->rf_effective_interval.tv_sec = 86400;
465 rf->rf_effective_interval.tv_nsec = 0;
468 NOTICE ("read-function of plugin `%s' failed. "
469 "Will suspend it for %i seconds.",
471 (int) rf->rf_effective_interval.tv_sec);
475 /* Success: Restore the interval, if it was changed. */
476 rf->rf_effective_interval = rf->rf_interval;
479 /* update the ``next read due'' field */
482 DEBUG ("plugin_read_thread: Effective interval of the "
483 "%s plugin is %i.%09i.",
485 (int) rf->rf_effective_interval.tv_sec,
486 (int) rf->rf_effective_interval.tv_nsec);
488 /* Calculate the next (absolute) time at which this function
489 * should be called. */
490 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
491 + rf->rf_effective_interval.tv_sec;
492 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
493 + rf->rf_effective_interval.tv_nsec;
494 NORMALIZE_TIMESPEC (rf->rf_next_read);
496 /* Check, if `rf_next_read' is in the past. */
497 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
499 /* `rf_next_read' is in the past. Insert `now'
500 * so this value doesn't trail off into the
502 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
505 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
507 (int) rf->rf_next_read.tv_sec,
508 (int) rf->rf_next_read.tv_nsec);
510 /* Re-insert this read function into the heap again. */
511 c_heap_insert (read_heap, rf);
512 } /* while (read_loop) */
516 } /* void *plugin_read_thread */
518 static void start_read_threads (int num)
522 if (read_threads != NULL)
525 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
526 if (read_threads == NULL)
528 ERROR ("plugin: start_read_threads: calloc failed.");
532 read_threads_num = 0;
533 for (i = 0; i < num; i++)
535 if (pthread_create (read_threads + read_threads_num, NULL,
536 plugin_read_thread, NULL) == 0)
542 ERROR ("plugin: start_read_threads: pthread_create failed.");
546 } /* void start_read_threads */
548 static void stop_read_threads (void)
552 if (read_threads == NULL)
555 INFO ("collectd: Stopping %i read threads.", read_threads_num);
557 pthread_mutex_lock (&read_lock);
559 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
560 pthread_cond_broadcast (&read_cond);
561 pthread_mutex_unlock (&read_lock);
563 for (i = 0; i < read_threads_num; i++)
565 if (pthread_join (read_threads[i], NULL) != 0)
567 ERROR ("plugin: stop_read_threads: pthread_join failed.");
569 read_threads[i] = (pthread_t) 0;
571 sfree (read_threads);
572 read_threads_num = 0;
573 } /* void stop_read_threads */
578 void plugin_set_dir (const char *dir)
580 if (plugindir != NULL)
585 else if ((plugindir = strdup (dir)) == NULL)
588 ERROR ("strdup failed: %s",
589 sstrerror (errno, errbuf, sizeof (errbuf)));
594 int plugin_load (const char *type, uint32_t flags)
598 char filename[BUFSIZE] = "";
599 char typename[BUFSIZE];
606 DEBUG ("type = %s", type);
608 dir = plugin_get_dir ();
611 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
612 * type when matching the filename */
613 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
614 if ((status < 0) || ((size_t) status >= sizeof (typename)))
616 WARNING ("snprintf: truncated: `%s.so'", type);
619 typename_len = strlen (typename);
621 if ((dh = opendir (dir)) == NULL)
624 ERROR ("opendir (%s): %s", dir,
625 sstrerror (errno, errbuf, sizeof (errbuf)));
629 while ((de = readdir (dh)) != NULL)
631 if (strncasecmp (de->d_name, typename, typename_len))
634 status = ssnprintf (filename, sizeof (filename),
635 "%s/%s", dir, de->d_name);
636 if ((status < 0) || ((size_t) status >= sizeof (filename)))
638 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
642 if (lstat (filename, &statbuf) == -1)
645 WARNING ("stat %s: %s", filename,
646 sstrerror (errno, errbuf, sizeof (errbuf)));
649 else if (!S_ISREG (statbuf.st_mode))
651 /* don't follow symlinks */
652 WARNING ("stat %s: not a regular file", filename);
656 if (plugin_load_file (filename, flags) == 0)
664 fprintf (stderr, "Unable to load plugin %s.\n", type);
670 if (filename[0] == '\0')
671 fprintf (stderr, "Could not find plugin %s.\n", type);
677 * The `register_*' functions follow
679 int plugin_register_config (const char *name,
680 int (*callback) (const char *key, const char *val),
681 const char **keys, int keys_num)
683 cf_register (name, callback, keys, keys_num);
685 } /* int plugin_register_config */
687 int plugin_register_complex_config (const char *type,
688 int (*callback) (oconfig_item_t *))
690 return (cf_register_complex (type, callback));
691 } /* int plugin_register_complex_config */
693 int plugin_register_init (const char *name,
694 int (*callback) (void))
696 return (create_register_callback (&list_init, name, (void *) callback,
697 /* user_data = */ NULL));
698 } /* plugin_register_init */
700 static int plugin_compare_read_func (const void *arg0, const void *arg1)
702 const read_func_t *rf0;
703 const read_func_t *rf1;
708 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
710 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
712 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
714 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
718 } /* int plugin_compare_read_func */
720 /* Add a read function to both, the heap and a linked list. The linked list if
721 * used to look-up read functions, especially for the remove function. The heap
722 * is used to determine which plugin to read next. */
723 static int plugin_insert_read (read_func_t *rf)
728 pthread_mutex_lock (&read_lock);
730 if (read_list == NULL)
732 read_list = llist_create ();
733 if (read_list == NULL)
735 pthread_mutex_unlock (&read_lock);
736 ERROR ("plugin_insert_read: read_list failed.");
741 if (read_heap == NULL)
743 read_heap = c_heap_create (plugin_compare_read_func);
744 if (read_heap == NULL)
746 pthread_mutex_unlock (&read_lock);
747 ERROR ("plugin_insert_read: c_heap_create failed.");
752 le = llist_search (read_list, rf->rf_name);
755 pthread_mutex_unlock (&read_lock);
756 WARNING ("The read function \"%s\" is already registered. "
757 "Check for duplicate \"LoadPlugin\" lines "
758 "in your configuration!",
763 le = llentry_create (rf->rf_name, rf);
766 pthread_mutex_unlock (&read_lock);
767 ERROR ("plugin_insert_read: llentry_create failed.");
771 status = c_heap_insert (read_heap, rf);
774 pthread_mutex_unlock (&read_lock);
775 ERROR ("plugin_insert_read: c_heap_insert failed.");
776 llentry_destroy (le);
780 /* This does not fail. */
781 llist_append (read_list, le);
783 pthread_mutex_unlock (&read_lock);
785 } /* int plugin_insert_read */
787 static int read_cb_wrapper (user_data_t *ud)
789 int (*callback) (void);
796 } /* int read_cb_wrapper */
798 int plugin_register_read (const char *name,
799 int (*callback) (void))
802 plugin_ctx_t ctx = plugin_get_ctx ();
805 if (ctx.interval != 0) {
806 struct timespec interval;
807 user_data_t user_data;
809 DEBUG ("plugin_register_read: plugin_interval = %.3f",
810 CDTIME_T_TO_DOUBLE(plugin_interval));
812 user_data.data = callback;
813 user_data.free_func = NULL;
815 CDTIME_T_TO_TIMESPEC (ctx.interval, &interval);
816 return plugin_register_complex_read (/* group = */ NULL,
817 name, read_cb_wrapper, &interval, &user_data);
820 rf = malloc (sizeof (*rf));
823 ERROR ("plugin_register_read: malloc failed.");
827 memset (rf, 0, sizeof (read_func_t));
828 rf->rf_callback = (void *) callback;
829 rf->rf_udata.data = NULL;
830 rf->rf_udata.free_func = NULL;
832 rf->rf_group[0] = '\0';
833 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
834 rf->rf_type = RF_SIMPLE;
835 rf->rf_interval.tv_sec = 0;
836 rf->rf_interval.tv_nsec = 0;
837 rf->rf_effective_interval = rf->rf_interval;
839 status = plugin_insert_read (rf);
844 } /* int plugin_register_read */
846 int plugin_register_complex_read (const char *group, const char *name,
847 plugin_read_cb callback,
848 const struct timespec *interval,
849 user_data_t *user_data)
852 plugin_ctx_t ctx = plugin_get_ctx ();
855 rf = malloc (sizeof (*rf));
858 ERROR ("plugin_register_complex_read: malloc failed.");
862 memset (rf, 0, sizeof (read_func_t));
863 rf->rf_callback = (void *) callback;
865 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
867 rf->rf_group[0] = '\0';
868 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
869 rf->rf_type = RF_COMPLEX;
870 if (interval != NULL)
872 rf->rf_interval = *interval;
874 else if (ctx.interval != 0)
876 CDTIME_T_TO_TIMESPEC (ctx.interval, &rf->rf_interval);
878 rf->rf_effective_interval = rf->rf_interval;
881 if (user_data == NULL)
883 rf->rf_udata.data = NULL;
884 rf->rf_udata.free_func = NULL;
888 rf->rf_udata = *user_data;
893 status = plugin_insert_read (rf);
898 } /* int plugin_register_complex_read */
900 int plugin_register_write (const char *name,
901 plugin_write_cb callback, user_data_t *ud)
903 return (create_register_callback (&list_write, name,
904 (void *) callback, ud));
905 } /* int plugin_register_write */
907 int plugin_register_flush (const char *name,
908 plugin_flush_cb callback, user_data_t *ud)
910 return (create_register_callback (&list_flush, name,
911 (void *) callback, ud));
912 } /* int plugin_register_flush */
914 int plugin_register_missing (const char *name,
915 plugin_missing_cb callback, user_data_t *ud)
917 return (create_register_callback (&list_missing, name,
918 (void *) callback, ud));
919 } /* int plugin_register_missing */
921 int plugin_register_shutdown (const char *name,
922 int (*callback) (void))
924 return (create_register_callback (&list_shutdown, name,
925 (void *) callback, /* user_data = */ NULL));
926 } /* int plugin_register_shutdown */
928 int plugin_register_data_set (const data_set_t *ds)
933 if ((data_sets != NULL)
934 && (c_avl_get (data_sets, ds->type, NULL) == 0))
936 NOTICE ("Replacing DS `%s' with another version.", ds->type);
937 plugin_unregister_data_set (ds->type);
939 else if (data_sets == NULL)
941 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
942 if (data_sets == NULL)
946 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
949 memcpy(ds_copy, ds, sizeof (data_set_t));
951 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
953 if (ds_copy->ds == NULL)
959 for (i = 0; i < ds->ds_num; i++)
960 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
962 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
963 } /* int plugin_register_data_set */
965 int plugin_register_log (const char *name,
966 plugin_log_cb callback, user_data_t *ud)
968 return (create_register_callback (&list_log, name,
969 (void *) callback, ud));
970 } /* int plugin_register_log */
972 int plugin_register_notification (const char *name,
973 plugin_notification_cb callback, user_data_t *ud)
975 return (create_register_callback (&list_notification, name,
976 (void *) callback, ud));
977 } /* int plugin_register_log */
979 int plugin_unregister_config (const char *name)
981 cf_unregister (name);
983 } /* int plugin_unregister_config */
985 int plugin_unregister_complex_config (const char *name)
987 cf_unregister_complex (name);
989 } /* int plugin_unregister_complex_config */
991 int plugin_unregister_init (const char *name)
993 return (plugin_unregister (list_init, name));
996 int plugin_unregister_read (const char *name) /* {{{ */
1004 pthread_mutex_lock (&read_lock);
1006 if (read_list == NULL)
1008 pthread_mutex_unlock (&read_lock);
1012 le = llist_search (read_list, name);
1015 pthread_mutex_unlock (&read_lock);
1016 WARNING ("plugin_unregister_read: No such read function: %s",
1021 llist_remove (read_list, le);
1024 assert (rf != NULL);
1025 rf->rf_type = RF_REMOVE;
1027 pthread_mutex_unlock (&read_lock);
1029 llentry_destroy (le);
1031 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1034 } /* }}} int plugin_unregister_read */
1036 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1038 read_func_t *rf = e->value;
1041 return strcmp (rf->rf_group, (const char *)group);
1042 } /* }}} int compare_read_func_group */
1044 int plugin_unregister_read_group (const char *group) /* {{{ */
1054 pthread_mutex_lock (&read_lock);
1056 if (read_list == NULL)
1058 pthread_mutex_unlock (&read_lock);
1064 le = llist_search_custom (read_list,
1065 compare_read_func_group, (void *)group);
1072 llist_remove (read_list, le);
1075 assert (rf != NULL);
1076 rf->rf_type = RF_REMOVE;
1078 llentry_destroy (le);
1080 DEBUG ("plugin_unregister_read_group: "
1081 "Marked `%s' (group `%s') for removal.",
1082 rf->rf_name, group);
1085 pthread_mutex_unlock (&read_lock);
1089 WARNING ("plugin_unregister_read_group: No such "
1090 "group of read function: %s", group);
1095 } /* }}} int plugin_unregister_read_group */
1097 int plugin_unregister_write (const char *name)
1099 return (plugin_unregister (list_write, name));
1102 int plugin_unregister_flush (const char *name)
1104 return (plugin_unregister (list_flush, name));
1107 int plugin_unregister_missing (const char *name)
1109 return (plugin_unregister (list_missing, name));
1112 int plugin_unregister_shutdown (const char *name)
1114 return (plugin_unregister (list_shutdown, name));
1117 int plugin_unregister_data_set (const char *name)
1121 if (data_sets == NULL)
1124 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1131 } /* int plugin_unregister_data_set */
1133 int plugin_unregister_log (const char *name)
1135 return (plugin_unregister (list_log, name));
1138 int plugin_unregister_notification (const char *name)
1140 return (plugin_unregister (list_notification, name));
1143 void plugin_init_all (void)
1145 const char *chain_name;
1149 /* Init the value cache */
1152 chain_name = global_option_get ("PreCacheChain");
1153 pre_cache_chain = fc_chain_get_by_name (chain_name);
1155 chain_name = global_option_get ("PostCacheChain");
1156 post_cache_chain = fc_chain_get_by_name (chain_name);
1159 if ((list_init == NULL) && (read_heap == NULL))
1162 /* Calling all init callbacks before checking if read callbacks
1163 * are available allows the init callbacks to register the read
1165 le = llist_head (list_init);
1168 callback_func_t *cf;
1169 plugin_init_cb callback;
1170 plugin_ctx_t old_ctx;
1173 old_ctx = plugin_set_ctx (cf->cf_ctx);
1174 callback = cf->cf_callback;
1175 status = (*callback) ();
1176 plugin_set_ctx (old_ctx);
1180 ERROR ("Initialization of plugin `%s' "
1181 "failed with status %i. "
1182 "Plugin will be unloaded.",
1184 /* Plugins that register read callbacks from the init
1185 * callback should take care of appropriate error
1186 * handling themselves. */
1187 /* FIXME: Unload _all_ functions */
1188 plugin_unregister_read (le->key);
1194 /* Start read-threads */
1195 if (read_heap != NULL)
1199 rt = global_option_get ("ReadThreads");
1202 start_read_threads ((num > 0) ? num : 5);
1204 } /* void plugin_init_all */
1206 /* TODO: Rename this function. */
1207 void plugin_read_all (void)
1209 uc_check_timeout ();
1212 } /* void plugin_read_all */
1214 /* Read function called when the `-T' command line argument is given. */
1215 int plugin_read_all_once (void)
1218 int return_status = 0;
1220 if (read_heap == NULL)
1222 NOTICE ("No read-functions are registered.");
1229 plugin_ctx_t old_ctx;
1231 rf = c_heap_get_root (read_heap);
1235 old_ctx = plugin_set_ctx (rf->rf_ctx);
1237 if (rf->rf_type == RF_SIMPLE)
1239 int (*callback) (void);
1241 callback = rf->rf_callback;
1242 status = (*callback) ();
1246 plugin_read_cb callback;
1248 callback = rf->rf_callback;
1249 status = (*callback) (&rf->rf_udata);
1252 plugin_set_ctx (old_ctx);
1256 NOTICE ("read-function of plugin `%s' failed.",
1261 destroy_callback ((void *) rf);
1264 return (return_status);
1265 } /* int plugin_read_all_once */
1267 int plugin_write (const char *plugin, /* {{{ */
1268 const data_set_t *ds, const value_list_t *vl)
1276 if (list_write == NULL)
1281 ds = plugin_get_ds (vl->type);
1284 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1294 le = llist_head (list_write);
1297 callback_func_t *cf = le->value;
1298 plugin_write_cb callback;
1299 plugin_ctx_t old_ctx = plugin_set_ctx (cf->cf_ctx);
1301 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1302 callback = cf->cf_callback;
1303 status = (*callback) (ds, vl, &cf->cf_udata);
1309 plugin_set_ctx (old_ctx);
1314 if ((success == 0) && (failure != 0))
1319 else /* plugin != NULL */
1321 callback_func_t *cf;
1322 plugin_write_cb callback;
1323 plugin_ctx_t old_ctx;
1325 le = llist_head (list_write);
1328 if (strcasecmp (plugin, le->key) == 0)
1339 old_ctx = plugin_set_ctx (cf->cf_ctx);
1341 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1342 callback = cf->cf_callback;
1343 status = (*callback) (ds, vl, &cf->cf_udata);
1345 plugin_set_ctx (old_ctx);
1349 } /* }}} int plugin_write */
1351 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1355 if (list_flush == NULL)
1358 le = llist_head (list_flush);
1361 callback_func_t *cf;
1362 plugin_flush_cb callback;
1363 plugin_ctx_t old_ctx;
1365 if ((plugin != NULL)
1366 && (strcmp (plugin, le->key) != 0))
1373 old_ctx = plugin_set_ctx (cf->cf_ctx);
1374 callback = cf->cf_callback;
1376 (*callback) (timeout, identifier, &cf->cf_udata);
1378 plugin_set_ctx (old_ctx);
1383 } /* int plugin_flush */
1385 void plugin_shutdown_all (void)
1389 stop_read_threads ();
1391 destroy_all_callbacks (&list_init);
1393 pthread_mutex_lock (&read_lock);
1394 llist_destroy (read_list);
1396 pthread_mutex_unlock (&read_lock);
1398 destroy_read_heap ();
1400 plugin_flush (/* plugin = */ NULL,
1402 /* identifier = */ NULL);
1405 if (list_shutdown != NULL)
1406 le = llist_head (list_shutdown);
1410 callback_func_t *cf;
1411 plugin_shutdown_cb callback;
1412 plugin_ctx_t old_ctx;
1415 old_ctx = plugin_set_ctx (cf->cf_ctx);
1416 callback = cf->cf_callback;
1418 /* Advance the pointer before calling the callback allows
1419 * shutdown functions to unregister themselves. If done the
1420 * other way around the memory `le' points to will be freed
1421 * after callback returns. */
1426 plugin_set_ctx (old_ctx);
1429 /* Write plugins which use the `user_data' pointer usually need the
1430 * same data available to the flush callback. If this is the case, set
1431 * the free_function to NULL when registering the flush callback and to
1432 * the real free function when registering the write callback. This way
1433 * the data isn't freed twice. */
1434 destroy_all_callbacks (&list_flush);
1435 destroy_all_callbacks (&list_missing);
1436 destroy_all_callbacks (&list_write);
1438 destroy_all_callbacks (&list_notification);
1439 destroy_all_callbacks (&list_shutdown);
1440 destroy_all_callbacks (&list_log);
1441 } /* void plugin_shutdown_all */
1443 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1447 if (list_missing == NULL)
1450 le = llist_head (list_missing);
1453 callback_func_t *cf;
1454 plugin_missing_cb callback;
1455 plugin_ctx_t old_ctx;
1459 old_ctx = plugin_set_ctx (cf->cf_ctx);
1460 callback = cf->cf_callback;
1462 status = (*callback) (vl, &cf->cf_udata);
1463 plugin_set_ctx (old_ctx);
1468 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1469 "failed with status %i.",
1482 } /* int }}} plugin_dispatch_missing */
1484 int plugin_dispatch_values (value_list_t *vl)
1487 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1489 value_t *saved_values;
1490 int saved_values_len;
1494 int free_meta_data = 0;
1496 if ((vl == NULL) || (vl->type[0] == 0)
1497 || (vl->values == NULL) || (vl->values_len < 1))
1499 ERROR ("plugin_dispatch_values: Invalid value list "
1500 "from plugin %s.", vl->plugin);
1504 /* Free meta data only if the calling function didn't specify any. In
1505 * this case matches and targets may add some and the calling function
1506 * may not expect (and therefore free) that data. */
1507 if (vl->meta == NULL)
1510 if (list_write == NULL)
1511 c_complain_once (LOG_WARNING, &no_write_complaint,
1512 "plugin_dispatch_values: No write callback has been "
1513 "registered. Please load at least one output plugin, "
1514 "if you want the collected data to be stored.");
1516 if (data_sets == NULL)
1518 ERROR ("plugin_dispatch_values: No data sets registered. "
1519 "Could the types database be read? Check "
1520 "your `TypesDB' setting!");
1524 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1526 char ident[6 * DATA_MAX_NAME_LEN];
1528 FORMAT_VL (ident, sizeof (ident), vl);
1529 INFO ("plugin_dispatch_values: Dataset not found: %s "
1530 "(from \"%s\"), check your types.db!",
1536 vl->time = cdtime ();
1538 if (vl->interval <= 0) {
1539 plugin_ctx_t ctx = plugin_get_ctx ();
1541 if (ctx.interval != 0)
1542 vl->interval = ctx.interval;
1544 vl->interval = interval_g;
1547 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1549 "plugin = %s; plugin_instance = %s; "
1550 "type = %s; type_instance = %s;",
1551 CDTIME_T_TO_DOUBLE (vl->time),
1552 CDTIME_T_TO_DOUBLE (vl->interval),
1554 vl->plugin, vl->plugin_instance,
1555 vl->type, vl->type_instance);
1558 assert (0 == strcmp (ds->type, vl->type));
1560 if (0 != strcmp (ds->type, vl->type))
1561 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1562 ds->type, vl->type);
1566 assert (ds->ds_num == vl->values_len);
1568 if (ds->ds_num != vl->values_len)
1570 ERROR ("plugin_dispatch_values: ds->type = %s: "
1571 "(ds->ds_num = %i) != "
1572 "(vl->values_len = %i)",
1573 ds->type, ds->ds_num, vl->values_len);
1578 escape_slashes (vl->host, sizeof (vl->host));
1579 escape_slashes (vl->plugin, sizeof (vl->plugin));
1580 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1581 escape_slashes (vl->type, sizeof (vl->type));
1582 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1584 /* Copy the values. This way, we can assure `targets' that they get
1585 * dynamically allocated values, which they can free and replace if
1587 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1589 saved_values = vl->values;
1590 saved_values_len = vl->values_len;
1592 vl->values = (value_t *) calloc (vl->values_len,
1593 sizeof (*vl->values));
1594 if (vl->values == NULL)
1596 ERROR ("plugin_dispatch_values: calloc failed.");
1597 vl->values = saved_values;
1600 memcpy (vl->values, saved_values,
1601 vl->values_len * sizeof (*vl->values));
1603 else /* if ((pre == NULL) && (post == NULL)) */
1605 saved_values = NULL;
1606 saved_values_len = 0;
1609 if (pre_cache_chain != NULL)
1611 status = fc_process_chain (ds, vl, pre_cache_chain);
1614 WARNING ("plugin_dispatch_values: Running the "
1615 "pre-cache chain failed with "
1619 else if (status == FC_TARGET_STOP)
1621 /* Restore the state of the value_list so that plugins
1622 * don't get confused.. */
1623 if (saved_values != NULL)
1626 vl->values = saved_values;
1627 vl->values_len = saved_values_len;
1633 /* Update the value cache */
1636 if (post_cache_chain != NULL)
1638 status = fc_process_chain (ds, vl, post_cache_chain);
1641 WARNING ("plugin_dispatch_values: Running the "
1642 "post-cache chain failed with "
1648 fc_default_action (ds, vl);
1650 /* Restore the state of the value_list so that plugins don't get
1652 if (saved_values != NULL)
1655 vl->values = saved_values;
1656 vl->values_len = saved_values_len;
1659 if ((free_meta_data != 0) && (vl->meta != NULL))
1661 meta_data_destroy (vl->meta);
1666 } /* int plugin_dispatch_values */
1668 int plugin_dispatch_values_secure (const value_list_t *vl)
1670 value_list_t vl_copy;
1676 memcpy (&vl_copy, vl, sizeof (vl_copy));
1678 /* Write callbacks must not change the values and meta pointers, so we can
1679 * savely skip copying those and make this more efficient. */
1680 if ((pre_cache_chain == NULL) && (post_cache_chain == NULL))
1681 return (plugin_dispatch_values (&vl_copy));
1683 /* Set pointers to NULL, just to be on the save side. */
1684 vl_copy.values = NULL;
1685 vl_copy.meta = NULL;
1687 vl_copy.values = malloc (sizeof (*vl_copy.values) * vl->values_len);
1688 if (vl_copy.values == NULL)
1690 ERROR ("plugin_dispatch_values_secure: malloc failed.");
1693 memcpy (vl_copy.values, vl->values, sizeof (*vl_copy.values) * vl->values_len);
1695 if (vl->meta != NULL)
1697 vl_copy.meta = meta_data_clone (vl->meta);
1698 if (vl_copy.meta == NULL)
1700 ERROR ("plugin_dispatch_values_secure: meta_data_clone failed.");
1701 free (vl_copy.values);
1704 } /* if (vl->meta) */
1706 status = plugin_dispatch_values (&vl_copy);
1708 meta_data_destroy (vl_copy.meta);
1709 free (vl_copy.values);
1712 } /* int plugin_dispatch_values_secure */
1714 int plugin_dispatch_notification (const notification_t *notif)
1717 /* Possible TODO: Add flap detection here */
1719 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1720 "time = %.3f; host = %s;",
1721 notif->severity, notif->message,
1722 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1724 /* Nobody cares for notifications */
1725 if (list_notification == NULL)
1728 le = llist_head (list_notification);
1731 callback_func_t *cf;
1732 plugin_notification_cb callback;
1733 plugin_ctx_t old_ctx;
1737 old_ctx = plugin_set_ctx (cf->cf_ctx);
1738 callback = cf->cf_callback;
1739 status = (*callback) (notif, &cf->cf_udata);
1740 plugin_set_ctx (old_ctx);
1743 WARNING ("plugin_dispatch_notification: Notification "
1744 "callback %s returned %i.",
1752 } /* int plugin_dispatch_notification */
1754 void plugin_log (int level, const char *format, ...)
1761 if (level >= LOG_DEBUG)
1765 va_start (ap, format);
1766 vsnprintf (msg, sizeof (msg), format, ap);
1767 msg[sizeof (msg) - 1] = '\0';
1770 if (list_log == NULL)
1772 fprintf (stderr, "%s\n", msg);
1776 le = llist_head (list_log);
1779 callback_func_t *cf;
1780 plugin_log_cb callback;
1781 plugin_ctx_t old_ctx;
1784 old_ctx = plugin_set_ctx (cf->cf_ctx);
1785 callback = cf->cf_callback;
1787 (*callback) (level, msg, &cf->cf_udata);
1789 plugin_set_ctx (old_ctx);
1792 } /* void plugin_log */
1794 const data_set_t *plugin_get_ds (const char *name)
1798 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1800 DEBUG ("No such dataset registered: %s", name);
1805 } /* data_set_t *plugin_get_ds */
1807 static int plugin_notification_meta_add (notification_t *n,
1809 enum notification_meta_type_e type,
1812 notification_meta_t *meta;
1813 notification_meta_t *tail;
1815 if ((n == NULL) || (name == NULL) || (value == NULL))
1817 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1821 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1824 ERROR ("plugin_notification_meta_add: malloc failed.");
1827 memset (meta, 0, sizeof (notification_meta_t));
1829 sstrncpy (meta->name, name, sizeof (meta->name));
1834 case NM_TYPE_STRING:
1836 meta->nm_value.nm_string = strdup ((const char *) value);
1837 if (meta->nm_value.nm_string == NULL)
1839 ERROR ("plugin_notification_meta_add: strdup failed.");
1845 case NM_TYPE_SIGNED_INT:
1847 meta->nm_value.nm_signed_int = *((int64_t *) value);
1850 case NM_TYPE_UNSIGNED_INT:
1852 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1855 case NM_TYPE_DOUBLE:
1857 meta->nm_value.nm_double = *((double *) value);
1860 case NM_TYPE_BOOLEAN:
1862 meta->nm_value.nm_boolean = *((_Bool *) value);
1867 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1871 } /* switch (type) */
1875 while ((tail != NULL) && (tail->next != NULL))
1884 } /* int plugin_notification_meta_add */
1886 int plugin_notification_meta_add_string (notification_t *n,
1890 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1893 int plugin_notification_meta_add_signed_int (notification_t *n,
1897 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1900 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1904 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1907 int plugin_notification_meta_add_double (notification_t *n,
1911 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1914 int plugin_notification_meta_add_boolean (notification_t *n,
1918 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1921 int plugin_notification_meta_copy (notification_t *dst,
1922 const notification_t *src)
1924 notification_meta_t *meta;
1926 assert (dst != NULL);
1927 assert (src != NULL);
1928 assert (dst != src);
1929 assert ((src->meta == NULL) || (src->meta != dst->meta));
1931 for (meta = src->meta; meta != NULL; meta = meta->next)
1933 if (meta->type == NM_TYPE_STRING)
1934 plugin_notification_meta_add_string (dst, meta->name,
1935 meta->nm_value.nm_string);
1936 else if (meta->type == NM_TYPE_SIGNED_INT)
1937 plugin_notification_meta_add_signed_int (dst, meta->name,
1938 meta->nm_value.nm_signed_int);
1939 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1940 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1941 meta->nm_value.nm_unsigned_int);
1942 else if (meta->type == NM_TYPE_DOUBLE)
1943 plugin_notification_meta_add_double (dst, meta->name,
1944 meta->nm_value.nm_double);
1945 else if (meta->type == NM_TYPE_BOOLEAN)
1946 plugin_notification_meta_add_boolean (dst, meta->name,
1947 meta->nm_value.nm_boolean);
1951 } /* int plugin_notification_meta_copy */
1953 int plugin_notification_meta_free (notification_meta_t *n)
1955 notification_meta_t *this;
1956 notification_meta_t *next;
1960 ERROR ("plugin_notification_meta_free: n == NULL!");
1965 while (this != NULL)
1969 if (this->type == NM_TYPE_STRING)
1971 free ((char *)this->nm_value.nm_string);
1972 this->nm_value.nm_string = NULL;
1980 } /* int plugin_notification_meta_free */
1982 static void plugin_ctx_destructor (void *ctx)
1985 } /* void plugin_ctx_destructor */
1987 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
1989 static plugin_ctx_t *plugin_ctx_create (void)
1993 ctx = malloc (sizeof (*ctx));
1996 ERROR ("Failed to allocate plugin context: %s",
1997 sstrerror (errno, errbuf, sizeof (errbuf)));
2002 pthread_setspecific (plugin_ctx_key, ctx);
2003 DEBUG("Created new plugin context.");
2005 } /* int plugin_ctx_create */
2007 void plugin_init_ctx (void)
2009 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2010 } /* void plugin_init_ctx */
2012 plugin_ctx_t plugin_get_ctx (void)
2016 ctx = pthread_getspecific (plugin_ctx_key);
2019 ctx = plugin_ctx_create ();
2020 /* this must no happen -- exit() instead? */
2026 } /* plugin_ctx_t plugin_get_ctx */
2028 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2033 c = pthread_getspecific (plugin_ctx_key);
2036 c = plugin_ctx_create ();
2037 /* this must no happen -- exit() instead? */
2046 } /* void plugin_set_ctx */
2048 /* vim: set sw=8 ts=8 noet fdm=marker : */