2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2010 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 "utils_threshold.h"
40 #include "filter_chain.h"
45 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 callback_func_t rf_super;
62 char rf_group[DATA_MAX_NAME_LEN];
63 char rf_name[DATA_MAX_NAME_LEN];
65 struct timespec rf_interval;
66 struct timespec rf_effective_interval;
67 struct timespec rf_next_read;
69 typedef struct read_func_s read_func_t;
74 static llist_t *list_init;
75 static llist_t *list_write;
76 static llist_t *list_flush;
77 static llist_t *list_missing;
78 static llist_t *list_shutdown;
79 static llist_t *list_log;
80 static llist_t *list_notification;
82 static fc_chain_t *pre_cache_chain = NULL;
83 static fc_chain_t *post_cache_chain = NULL;
85 static c_avl_tree_t *data_sets;
87 static char *plugindir = NULL;
89 static c_heap_t *read_heap = NULL;
90 static llist_t *read_list;
91 static int read_loop = 1;
92 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
93 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
94 static pthread_t *read_threads = NULL;
95 static int read_threads_num = 0;
100 static const char *plugin_get_dir (void)
102 if (plugindir == NULL)
108 static void destroy_callback (callback_func_t *cf) /* {{{ */
113 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
115 cf->cf_udata.free_func (cf->cf_udata.data);
116 cf->cf_udata.data = NULL;
117 cf->cf_udata.free_func = NULL;
120 } /* }}} void destroy_callback */
122 static void destroy_all_callbacks (llist_t **list) /* {{{ */
129 le = llist_head (*list);
137 destroy_callback (le->value);
143 llist_destroy (*list);
145 } /* }}} void destroy_all_callbacks */
147 static void destroy_read_heap (void) /* {{{ */
149 if (read_heap == NULL)
156 cf = c_heap_get_root (read_heap);
160 destroy_callback (cf);
163 c_heap_destroy (read_heap);
165 } /* }}} void destroy_read_heap */
167 static int register_callback (llist_t **list, /* {{{ */
168 const char *name, callback_func_t *cf)
175 *list = llist_create ();
178 ERROR ("plugin: register_callback: "
179 "llist_create failed.");
180 destroy_callback (cf);
188 ERROR ("plugin: register_callback: strdup failed.");
189 destroy_callback (cf);
193 le = llist_search (*list, name);
196 le = llentry_create (key, cf);
199 ERROR ("plugin: register_callback: "
200 "llentry_create failed.");
202 destroy_callback (cf);
206 llist_append (*list, le);
210 callback_func_t *old_cf;
215 WARNING ("plugin: register_callback: "
216 "a callback named `%s' already exists - "
217 "overwriting the old entry!", name);
219 destroy_callback (old_cf);
224 } /* }}} int register_callback */
226 static int create_register_callback (llist_t **list, /* {{{ */
227 const char *name, void *callback, user_data_t *ud)
231 cf = (callback_func_t *) malloc (sizeof (*cf));
234 ERROR ("plugin: create_register_callback: malloc failed.");
237 memset (cf, 0, sizeof (*cf));
239 cf->cf_callback = callback;
242 cf->cf_udata.data = NULL;
243 cf->cf_udata.free_func = NULL;
250 return (register_callback (list, name, cf));
251 } /* }}} int create_register_callback */
253 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
260 e = llist_search (list, name);
264 llist_remove (list, e);
267 destroy_callback (e->value);
272 } /* }}} int plugin_unregister */
275 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
276 * object, but it will bitch about a shared object not having a
277 * ``module_register'' symbol..
279 static int plugin_load_file (char *file, uint32_t flags)
282 void (*reg_handle) (void);
284 DEBUG ("file = %s", file);
287 lt_dlerror (); /* clear errors */
289 #if LIBTOOL_VERSION == 2
290 if (flags & PLUGIN_FLAGS_GLOBAL) {
292 lt_dladvise_init(&advise);
293 lt_dladvise_global(&advise);
294 dlh = lt_dlopenadvise(file, advise);
295 lt_dladvise_destroy(&advise);
297 dlh = lt_dlopen (file);
299 #else /* if LIBTOOL_VERSION == 1 */
300 if (flags & PLUGIN_FLAGS_GLOBAL)
301 ERROR ("plugin_load_file: The global flag is not supported, "
302 "libtool 2 is required for this.");
303 dlh = lt_dlopen (file);
308 const char *error = lt_dlerror ();
310 ERROR ("lt_dlopen (%s) failed: %s", file, error);
311 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
315 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
317 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
318 file, lt_dlerror ());
328 static _Bool timeout_reached(struct timespec timeout)
331 gettimeofday(&now, NULL);
332 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
335 static void *plugin_read_thread (void __attribute__((unused)) *args)
337 while (read_loop != 0)
345 /* Get the read function that needs to be read next. */
346 rf = c_heap_get_root (read_heap);
349 struct timespec abstime;
353 CDTIME_T_TO_TIMESPEC (now + interval_g, &abstime);
355 pthread_mutex_lock (&read_lock);
356 pthread_cond_timedwait (&read_cond, &read_lock,
358 pthread_mutex_unlock (&read_lock);
362 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
366 CDTIME_T_TO_TIMESPEC (interval_g, &rf->rf_interval);
368 rf->rf_effective_interval = rf->rf_interval;
370 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
373 /* sleep until this entry is due,
374 * using pthread_cond_timedwait */
375 pthread_mutex_lock (&read_lock);
376 /* In pthread_cond_timedwait, spurious wakeups are possible
377 * (and really happen, at least on NetBSD with > 1 CPU), thus
378 * we need to re-evaluate the condition every time
379 * pthread_cond_timedwait returns. */
381 while ((read_loop != 0)
382 && !timeout_reached(rf->rf_next_read)
385 rc = pthread_cond_timedwait (&read_cond, &read_lock,
389 /* Must hold `read_lock' when accessing `rf->rf_type'. */
390 rf_type = rf->rf_type;
391 pthread_mutex_unlock (&read_lock);
393 /* Check if we're supposed to stop.. This may have interrupted
397 /* Insert `rf' again, so it can be free'd correctly */
398 c_heap_insert (read_heap, rf);
402 /* The entry has been marked for deletion. The linked list
403 * entry has already been removed by `plugin_unregister_read'.
404 * All we have to do here is free the `read_func_t' and
406 if (rf_type == RF_REMOVE)
408 DEBUG ("plugin_read_thread: Destroying the `%s' "
409 "callback.", rf->rf_name);
410 destroy_callback ((callback_func_t *) rf);
415 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
417 if (rf_type == RF_SIMPLE)
419 int (*callback) (void);
421 callback = rf->rf_callback;
422 status = (*callback) ();
426 plugin_read_cb callback;
428 assert (rf_type == RF_COMPLEX);
430 callback = rf->rf_callback;
431 status = (*callback) (&rf->rf_udata);
434 /* If the function signals failure, we will increase the
435 * intervals in which it will be called. */
438 rf->rf_effective_interval.tv_sec *= 2;
439 rf->rf_effective_interval.tv_nsec *= 2;
440 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
442 if (rf->rf_effective_interval.tv_sec >= 86400)
444 rf->rf_effective_interval.tv_sec = 86400;
445 rf->rf_effective_interval.tv_nsec = 0;
448 NOTICE ("read-function of plugin `%s' failed. "
449 "Will suspend it for %i seconds.",
451 (int) rf->rf_effective_interval.tv_sec);
455 /* Success: Restore the interval, if it was changed. */
456 rf->rf_effective_interval = rf->rf_interval;
459 /* update the ``next read due'' field */
462 DEBUG ("plugin_read_thread: Effective interval of the "
463 "%s plugin is %i.%09i.",
465 (int) rf->rf_effective_interval.tv_sec,
466 (int) rf->rf_effective_interval.tv_nsec);
468 /* Calculate the next (absolute) time at which this function
469 * should be called. */
470 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
471 + rf->rf_effective_interval.tv_sec;
472 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
473 + rf->rf_effective_interval.tv_nsec;
474 NORMALIZE_TIMESPEC (rf->rf_next_read);
476 /* Check, if `rf_next_read' is in the past. */
477 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
479 /* `rf_next_read' is in the past. Insert `now'
480 * so this value doesn't trail off into the
482 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
485 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
487 (int) rf->rf_next_read.tv_sec,
488 (int) rf->rf_next_read.tv_nsec);
490 /* Re-insert this read function into the heap again. */
491 c_heap_insert (read_heap, rf);
492 } /* while (read_loop) */
496 } /* void *plugin_read_thread */
498 static void start_read_threads (int num)
502 if (read_threads != NULL)
505 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
506 if (read_threads == NULL)
508 ERROR ("plugin: start_read_threads: calloc failed.");
512 read_threads_num = 0;
513 for (i = 0; i < num; i++)
515 if (pthread_create (read_threads + read_threads_num, NULL,
516 plugin_read_thread, NULL) == 0)
522 ERROR ("plugin: start_read_threads: pthread_create failed.");
526 } /* void start_read_threads */
528 static void stop_read_threads (void)
532 if (read_threads == NULL)
535 INFO ("collectd: Stopping %i read threads.", read_threads_num);
537 pthread_mutex_lock (&read_lock);
539 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
540 pthread_cond_broadcast (&read_cond);
541 pthread_mutex_unlock (&read_lock);
543 for (i = 0; i < read_threads_num; i++)
545 if (pthread_join (read_threads[i], NULL) != 0)
547 ERROR ("plugin: stop_read_threads: pthread_join failed.");
549 read_threads[i] = (pthread_t) 0;
551 sfree (read_threads);
552 read_threads_num = 0;
553 } /* void stop_read_threads */
558 void plugin_set_dir (const char *dir)
560 if (plugindir != NULL)
565 else if ((plugindir = strdup (dir)) == NULL)
568 ERROR ("strdup failed: %s",
569 sstrerror (errno, errbuf, sizeof (errbuf)));
574 int plugin_load (const char *type, uint32_t flags)
578 char filename[BUFSIZE] = "";
579 char typename[BUFSIZE];
586 DEBUG ("type = %s", type);
588 dir = plugin_get_dir ();
591 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
592 * type when matching the filename */
593 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
594 if ((status < 0) || ((size_t) status >= sizeof (typename)))
596 WARNING ("snprintf: truncated: `%s.so'", type);
599 typename_len = strlen (typename);
601 if ((dh = opendir (dir)) == NULL)
604 ERROR ("opendir (%s): %s", dir,
605 sstrerror (errno, errbuf, sizeof (errbuf)));
609 while ((de = readdir (dh)) != NULL)
611 if (strncasecmp (de->d_name, typename, typename_len))
614 status = ssnprintf (filename, sizeof (filename),
615 "%s/%s", dir, de->d_name);
616 if ((status < 0) || ((size_t) status >= sizeof (filename)))
618 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
622 if (lstat (filename, &statbuf) == -1)
625 WARNING ("stat %s: %s", filename,
626 sstrerror (errno, errbuf, sizeof (errbuf)));
629 else if (!S_ISREG (statbuf.st_mode))
631 /* don't follow symlinks */
632 WARNING ("stat %s: not a regular file", filename);
636 if (plugin_load_file (filename, flags) == 0)
644 fprintf (stderr, "Unable to load plugin %s.\n", type);
650 if (filename[0] == '\0')
651 fprintf (stderr, "Could not find plugin %s.\n", type);
657 * The `register_*' functions follow
659 int plugin_register_config (const char *name,
660 int (*callback) (const char *key, const char *val),
661 const char **keys, int keys_num)
663 cf_register (name, callback, keys, keys_num);
665 } /* int plugin_register_config */
667 int plugin_register_complex_config (const char *type,
668 int (*callback) (oconfig_item_t *))
670 return (cf_register_complex (type, callback));
671 } /* int plugin_register_complex_config */
673 int plugin_register_init (const char *name,
674 int (*callback) (void))
676 return (create_register_callback (&list_init, name, (void *) callback,
677 /* user_data = */ NULL));
678 } /* plugin_register_init */
680 static int plugin_compare_read_func (const void *arg0, const void *arg1)
682 const read_func_t *rf0;
683 const read_func_t *rf1;
688 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
690 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
692 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
694 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
698 } /* int plugin_compare_read_func */
700 /* Add a read function to both, the heap and a linked list. The linked list if
701 * used to look-up read functions, especially for the remove function. The heap
702 * is used to determine which plugin to read next. */
703 static int plugin_insert_read (read_func_t *rf)
708 pthread_mutex_lock (&read_lock);
710 if (read_list == NULL)
712 read_list = llist_create ();
713 if (read_list == NULL)
715 pthread_mutex_unlock (&read_lock);
716 ERROR ("plugin_insert_read: read_list failed.");
721 if (read_heap == NULL)
723 read_heap = c_heap_create (plugin_compare_read_func);
724 if (read_heap == NULL)
726 pthread_mutex_unlock (&read_lock);
727 ERROR ("plugin_insert_read: c_heap_create failed.");
732 le = llentry_create (rf->rf_name, rf);
735 pthread_mutex_unlock (&read_lock);
736 ERROR ("plugin_insert_read: llentry_create failed.");
740 status = c_heap_insert (read_heap, rf);
743 pthread_mutex_unlock (&read_lock);
744 ERROR ("plugin_insert_read: c_heap_insert failed.");
745 llentry_destroy (le);
749 /* This does not fail. */
750 llist_append (read_list, le);
752 pthread_mutex_unlock (&read_lock);
754 } /* int plugin_insert_read */
756 int plugin_register_read (const char *name,
757 int (*callback) (void))
761 rf = (read_func_t *) malloc (sizeof (read_func_t));
765 ERROR ("plugin_register_read: malloc failed: %s",
766 sstrerror (errno, errbuf, sizeof (errbuf)));
770 memset (rf, 0, sizeof (read_func_t));
771 rf->rf_callback = (void *) callback;
772 rf->rf_udata.data = NULL;
773 rf->rf_udata.free_func = NULL;
774 rf->rf_group[0] = '\0';
775 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
776 rf->rf_type = RF_SIMPLE;
777 rf->rf_interval.tv_sec = 0;
778 rf->rf_interval.tv_nsec = 0;
779 rf->rf_effective_interval = rf->rf_interval;
781 return (plugin_insert_read (rf));
782 } /* int plugin_register_read */
784 int plugin_register_complex_read (const char *group, const char *name,
785 plugin_read_cb callback,
786 const struct timespec *interval,
787 user_data_t *user_data)
791 rf = (read_func_t *) malloc (sizeof (read_func_t));
794 ERROR ("plugin_register_complex_read: malloc failed.");
798 memset (rf, 0, sizeof (read_func_t));
799 rf->rf_callback = (void *) callback;
801 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
803 rf->rf_group[0] = '\0';
804 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
805 rf->rf_type = RF_COMPLEX;
806 if (interval != NULL)
808 rf->rf_interval = *interval;
810 rf->rf_effective_interval = rf->rf_interval;
813 if (user_data == NULL)
815 rf->rf_udata.data = NULL;
816 rf->rf_udata.free_func = NULL;
820 rf->rf_udata = *user_data;
823 return (plugin_insert_read (rf));
824 } /* int plugin_register_complex_read */
826 int plugin_register_write (const char *name,
827 plugin_write_cb callback, user_data_t *ud)
829 return (create_register_callback (&list_write, name,
830 (void *) callback, ud));
831 } /* int plugin_register_write */
833 int plugin_register_flush (const char *name,
834 plugin_flush_cb callback, user_data_t *ud)
836 return (create_register_callback (&list_flush, name,
837 (void *) callback, ud));
838 } /* int plugin_register_flush */
840 int plugin_register_missing (const char *name,
841 plugin_missing_cb callback, user_data_t *ud)
843 return (create_register_callback (&list_missing, name,
844 (void *) callback, ud));
845 } /* int plugin_register_missing */
847 int plugin_register_shutdown (char *name,
848 int (*callback) (void))
850 return (create_register_callback (&list_shutdown, name,
851 (void *) callback, /* user_data = */ NULL));
852 } /* int plugin_register_shutdown */
854 int plugin_register_data_set (const data_set_t *ds)
859 if ((data_sets != NULL)
860 && (c_avl_get (data_sets, ds->type, NULL) == 0))
862 NOTICE ("Replacing DS `%s' with another version.", ds->type);
863 plugin_unregister_data_set (ds->type);
865 else if (data_sets == NULL)
867 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
868 if (data_sets == NULL)
872 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
875 memcpy(ds_copy, ds, sizeof (data_set_t));
877 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
879 if (ds_copy->ds == NULL)
885 for (i = 0; i < ds->ds_num; i++)
886 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
888 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
889 } /* int plugin_register_data_set */
891 int plugin_register_log (const char *name,
892 plugin_log_cb callback, user_data_t *ud)
894 return (create_register_callback (&list_log, name,
895 (void *) callback, ud));
896 } /* int plugin_register_log */
898 int plugin_register_notification (const char *name,
899 plugin_notification_cb callback, user_data_t *ud)
901 return (create_register_callback (&list_notification, name,
902 (void *) callback, ud));
903 } /* int plugin_register_log */
905 int plugin_unregister_config (const char *name)
907 cf_unregister (name);
909 } /* int plugin_unregister_config */
911 int plugin_unregister_complex_config (const char *name)
913 cf_unregister_complex (name);
915 } /* int plugin_unregister_complex_config */
917 int plugin_unregister_init (const char *name)
919 return (plugin_unregister (list_init, name));
922 int plugin_unregister_read (const char *name) /* {{{ */
930 pthread_mutex_lock (&read_lock);
932 if (read_list == NULL)
934 pthread_mutex_unlock (&read_lock);
938 le = llist_search (read_list, name);
941 pthread_mutex_unlock (&read_lock);
942 WARNING ("plugin_unregister_read: No such read function: %s",
947 llist_remove (read_list, le);
951 rf->rf_type = RF_REMOVE;
953 pthread_mutex_unlock (&read_lock);
955 llentry_destroy (le);
957 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
960 } /* }}} int plugin_unregister_read */
962 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
964 read_func_t *rf = e->value;
967 return strcmp (rf->rf_group, (const char *)group);
968 } /* }}} int compare_read_func_group */
970 int plugin_unregister_read_group (const char *group) /* {{{ */
980 pthread_mutex_lock (&read_lock);
982 if (read_list == NULL)
984 pthread_mutex_unlock (&read_lock);
990 le = llist_search_custom (read_list,
991 compare_read_func_group, (void *)group);
998 llist_remove (read_list, le);
1001 assert (rf != NULL);
1002 rf->rf_type = RF_REMOVE;
1004 llentry_destroy (le);
1006 DEBUG ("plugin_unregister_read_group: "
1007 "Marked `%s' (group `%s') for removal.",
1008 rf->rf_name, group);
1011 pthread_mutex_unlock (&read_lock);
1015 WARNING ("plugin_unregister_read_group: No such "
1016 "group of read function: %s", group);
1021 } /* }}} int plugin_unregister_read_group */
1023 int plugin_unregister_write (const char *name)
1025 return (plugin_unregister (list_write, name));
1028 int plugin_unregister_flush (const char *name)
1030 return (plugin_unregister (list_flush, name));
1033 int plugin_unregister_missing (const char *name)
1035 return (plugin_unregister (list_missing, name));
1038 int plugin_unregister_shutdown (const char *name)
1040 return (plugin_unregister (list_shutdown, name));
1043 int plugin_unregister_data_set (const char *name)
1047 if (data_sets == NULL)
1050 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1057 } /* int plugin_unregister_data_set */
1059 int plugin_unregister_log (const char *name)
1061 return (plugin_unregister (list_log, name));
1064 int plugin_unregister_notification (const char *name)
1066 return (plugin_unregister (list_notification, name));
1069 void plugin_init_all (void)
1071 const char *chain_name;
1075 /* Init the value cache */
1078 chain_name = global_option_get ("PreCacheChain");
1079 pre_cache_chain = fc_chain_get_by_name (chain_name);
1081 chain_name = global_option_get ("PostCacheChain");
1082 post_cache_chain = fc_chain_get_by_name (chain_name);
1085 if ((list_init == NULL) && (read_heap == NULL))
1088 /* Calling all init callbacks before checking if read callbacks
1089 * are available allows the init callbacks to register the read
1091 le = llist_head (list_init);
1094 callback_func_t *cf;
1095 plugin_init_cb callback;
1098 callback = cf->cf_callback;
1099 status = (*callback) ();
1103 ERROR ("Initialization of plugin `%s' "
1104 "failed with status %i. "
1105 "Plugin will be unloaded.",
1107 /* Plugins that register read callbacks from the init
1108 * callback should take care of appropriate error
1109 * handling themselves. */
1110 /* FIXME: Unload _all_ functions */
1111 plugin_unregister_read (le->key);
1117 /* Start read-threads */
1118 if (read_heap != NULL)
1122 rt = global_option_get ("ReadThreads");
1125 start_read_threads ((num > 0) ? num : 5);
1127 } /* void plugin_init_all */
1129 /* TODO: Rename this function. */
1130 void plugin_read_all (void)
1132 uc_check_timeout ();
1135 } /* void plugin_read_all */
1137 /* Read function called when the `-T' command line argument is given. */
1138 int plugin_read_all_once (void)
1141 int return_status = 0;
1143 if (read_heap == NULL)
1145 NOTICE ("No read-functions are registered.");
1153 rf = c_heap_get_root (read_heap);
1157 if (rf->rf_type == RF_SIMPLE)
1159 int (*callback) (void);
1161 callback = rf->rf_callback;
1162 status = (*callback) ();
1166 plugin_read_cb callback;
1168 callback = rf->rf_callback;
1169 status = (*callback) (&rf->rf_udata);
1174 NOTICE ("read-function of plugin `%s' failed.",
1179 destroy_callback ((void *) rf);
1182 return (return_status);
1183 } /* int plugin_read_all_once */
1185 int plugin_write (const char *plugin, /* {{{ */
1186 const data_set_t *ds, const value_list_t *vl)
1194 if (list_write == NULL)
1199 ds = plugin_get_ds (vl->type);
1202 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1212 le = llist_head (list_write);
1215 callback_func_t *cf = le->value;
1216 plugin_write_cb callback;
1218 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1219 callback = cf->cf_callback;
1220 status = (*callback) (ds, vl, &cf->cf_udata);
1229 if ((success == 0) && (failure != 0))
1234 else /* plugin != NULL */
1236 callback_func_t *cf;
1237 plugin_write_cb callback;
1239 le = llist_head (list_write);
1242 if (strcasecmp (plugin, le->key) == 0)
1253 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1254 callback = cf->cf_callback;
1255 status = (*callback) (ds, vl, &cf->cf_udata);
1259 } /* }}} int plugin_write */
1261 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1265 if (list_flush == NULL)
1268 le = llist_head (list_flush);
1271 callback_func_t *cf;
1272 plugin_flush_cb callback;
1274 if ((plugin != NULL)
1275 && (strcmp (plugin, le->key) != 0))
1282 callback = cf->cf_callback;
1284 (*callback) (timeout, identifier, &cf->cf_udata);
1289 } /* int plugin_flush */
1291 void plugin_shutdown_all (void)
1295 stop_read_threads ();
1297 destroy_all_callbacks (&list_init);
1299 pthread_mutex_lock (&read_lock);
1300 llist_destroy (read_list);
1302 pthread_mutex_unlock (&read_lock);
1304 destroy_read_heap ();
1306 plugin_flush (/* plugin = */ NULL,
1308 /* identifier = */ NULL);
1311 if (list_shutdown != NULL)
1312 le = llist_head (list_shutdown);
1316 callback_func_t *cf;
1317 plugin_shutdown_cb callback;
1320 callback = cf->cf_callback;
1322 /* Advance the pointer before calling the callback allows
1323 * shutdown functions to unregister themselves. If done the
1324 * other way around the memory `le' points to will be freed
1325 * after callback returns. */
1331 /* Write plugins which use the `user_data' pointer usually need the
1332 * same data available to the flush callback. If this is the case, set
1333 * the free_function to NULL when registering the flush callback and to
1334 * the real free function when registering the write callback. This way
1335 * the data isn't freed twice. */
1336 destroy_all_callbacks (&list_flush);
1337 destroy_all_callbacks (&list_missing);
1338 destroy_all_callbacks (&list_write);
1340 destroy_all_callbacks (&list_notification);
1341 destroy_all_callbacks (&list_shutdown);
1342 destroy_all_callbacks (&list_log);
1343 } /* void plugin_shutdown_all */
1345 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1349 if (list_missing == NULL)
1352 le = llist_head (list_missing);
1355 callback_func_t *cf;
1356 plugin_missing_cb callback;
1360 callback = cf->cf_callback;
1362 status = (*callback) (vl);
1367 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1368 "failed with status %i.",
1381 } /* int }}} plugin_dispatch_missing */
1383 int plugin_dispatch_values (value_list_t *vl)
1386 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1388 value_t *saved_values;
1389 int saved_values_len;
1393 int free_meta_data = 0;
1395 if ((vl == NULL) || (vl->type[0] == 0)
1396 || (vl->values == NULL) || (vl->values_len < 1))
1398 ERROR ("plugin_dispatch_values: Invalid value list.");
1402 /* Free meta data only if the calling function didn't specify any. In
1403 * this case matches and targets may add some and the calling function
1404 * may not expect (and therefore free) that data. */
1405 if (vl->meta == NULL)
1408 if (list_write == NULL)
1409 c_complain_once (LOG_WARNING, &no_write_complaint,
1410 "plugin_dispatch_values: No write callback has been "
1411 "registered. Please load at least one output plugin, "
1412 "if you want the collected data to be stored.");
1414 if (data_sets == NULL)
1416 ERROR ("plugin_dispatch_values: No data sets registered. "
1417 "Could the types database be read? Check "
1418 "your `TypesDB' setting!");
1422 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1424 char ident[6 * DATA_MAX_NAME_LEN];
1426 FORMAT_VL (ident, sizeof (ident), vl);
1427 INFO ("plugin_dispatch_values: Dataset not found: %s "
1428 "(from \"%s\"), check your types.db!",
1434 vl->time = cdtime ();
1436 if (vl->interval <= 0)
1437 vl->interval = interval_g;
1439 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1441 "plugin = %s; plugin_instance = %s; "
1442 "type = %s; type_instance = %s;",
1443 CDTIME_T_TO_DOUBLE (vl->time),
1444 CDTIME_T_TO_DOUBLE (vl->interval),
1446 vl->plugin, vl->plugin_instance,
1447 vl->type, vl->type_instance);
1450 assert (0 == strcmp (ds->type, vl->type));
1452 if (0 != strcmp (ds->type, vl->type))
1453 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1454 ds->type, vl->type);
1458 assert (ds->ds_num == vl->values_len);
1460 if (ds->ds_num != vl->values_len)
1462 ERROR ("plugin_dispatch_values: ds->type = %s: "
1463 "(ds->ds_num = %i) != "
1464 "(vl->values_len = %i)",
1465 ds->type, ds->ds_num, vl->values_len);
1470 escape_slashes (vl->host, sizeof (vl->host));
1471 escape_slashes (vl->plugin, sizeof (vl->plugin));
1472 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1473 escape_slashes (vl->type, sizeof (vl->type));
1474 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1476 /* Copy the values. This way, we can assure `targets' that they get
1477 * dynamically allocated values, which they can free and replace if
1479 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1481 saved_values = vl->values;
1482 saved_values_len = vl->values_len;
1484 vl->values = (value_t *) calloc (vl->values_len,
1485 sizeof (*vl->values));
1486 if (vl->values == NULL)
1488 ERROR ("plugin_dispatch_values: calloc failed.");
1489 vl->values = saved_values;
1492 memcpy (vl->values, saved_values,
1493 vl->values_len * sizeof (*vl->values));
1495 else /* if ((pre == NULL) && (post == NULL)) */
1497 saved_values = NULL;
1498 saved_values_len = 0;
1501 if (pre_cache_chain != NULL)
1503 status = fc_process_chain (ds, vl, pre_cache_chain);
1506 WARNING ("plugin_dispatch_values: Running the "
1507 "pre-cache chain failed with "
1511 else if (status == FC_TARGET_STOP)
1513 /* Restore the state of the value_list so that plugins
1514 * don't get confused.. */
1515 if (saved_values != NULL)
1518 vl->values = saved_values;
1519 vl->values_len = saved_values_len;
1525 /* Update the value cache */
1528 /* Initiate threshold checking */
1529 ut_check_threshold (ds, vl);
1531 if (post_cache_chain != NULL)
1533 status = fc_process_chain (ds, vl, post_cache_chain);
1536 WARNING ("plugin_dispatch_values: Running the "
1537 "post-cache chain failed with "
1543 fc_default_action (ds, vl);
1545 /* Restore the state of the value_list so that plugins don't get
1547 if (saved_values != NULL)
1550 vl->values = saved_values;
1551 vl->values_len = saved_values_len;
1554 if ((free_meta_data != 0) && (vl->meta != NULL))
1556 meta_data_destroy (vl->meta);
1561 } /* int plugin_dispatch_values */
1563 int plugin_dispatch_notification (const notification_t *notif)
1566 /* Possible TODO: Add flap detection here */
1568 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1569 "time = %.3f; host = %s;",
1570 notif->severity, notif->message,
1571 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1573 /* Nobody cares for notifications */
1574 if (list_notification == NULL)
1577 le = llist_head (list_notification);
1580 callback_func_t *cf;
1581 plugin_notification_cb callback;
1585 callback = cf->cf_callback;
1586 status = (*callback) (notif, &cf->cf_udata);
1589 WARNING ("plugin_dispatch_notification: Notification "
1590 "callback %s returned %i.",
1598 } /* int plugin_dispatch_notification */
1600 void plugin_log (int level, const char *format, ...)
1607 if (level >= LOG_DEBUG)
1611 va_start (ap, format);
1612 vsnprintf (msg, sizeof (msg), format, ap);
1613 msg[sizeof (msg) - 1] = '\0';
1616 if (list_log == NULL)
1618 fprintf (stderr, "%s\n", msg);
1622 le = llist_head (list_log);
1625 callback_func_t *cf;
1626 plugin_log_cb callback;
1629 callback = cf->cf_callback;
1631 (*callback) (level, msg, &cf->cf_udata);
1635 } /* void plugin_log */
1637 const data_set_t *plugin_get_ds (const char *name)
1641 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1643 DEBUG ("No such dataset registered: %s", name);
1648 } /* data_set_t *plugin_get_ds */
1650 static int plugin_notification_meta_add (notification_t *n,
1652 enum notification_meta_type_e type,
1655 notification_meta_t *meta;
1656 notification_meta_t *tail;
1658 if ((n == NULL) || (name == NULL) || (value == NULL))
1660 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1664 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1667 ERROR ("plugin_notification_meta_add: malloc failed.");
1670 memset (meta, 0, sizeof (notification_meta_t));
1672 sstrncpy (meta->name, name, sizeof (meta->name));
1677 case NM_TYPE_STRING:
1679 meta->nm_value.nm_string = strdup ((const char *) value);
1680 if (meta->nm_value.nm_string == NULL)
1682 ERROR ("plugin_notification_meta_add: strdup failed.");
1688 case NM_TYPE_SIGNED_INT:
1690 meta->nm_value.nm_signed_int = *((int64_t *) value);
1693 case NM_TYPE_UNSIGNED_INT:
1695 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1698 case NM_TYPE_DOUBLE:
1700 meta->nm_value.nm_double = *((double *) value);
1703 case NM_TYPE_BOOLEAN:
1705 meta->nm_value.nm_boolean = *((_Bool *) value);
1710 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1714 } /* switch (type) */
1718 while ((tail != NULL) && (tail->next != NULL))
1727 } /* int plugin_notification_meta_add */
1729 int plugin_notification_meta_add_string (notification_t *n,
1733 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1736 int plugin_notification_meta_add_signed_int (notification_t *n,
1740 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1743 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1747 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1750 int plugin_notification_meta_add_double (notification_t *n,
1754 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1757 int plugin_notification_meta_add_boolean (notification_t *n,
1761 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1764 int plugin_notification_meta_copy (notification_t *dst,
1765 const notification_t *src)
1767 notification_meta_t *meta;
1769 assert (dst != NULL);
1770 assert (src != NULL);
1771 assert (dst != src);
1772 assert ((src->meta == NULL) || (src->meta != dst->meta));
1774 for (meta = src->meta; meta != NULL; meta = meta->next)
1776 if (meta->type == NM_TYPE_STRING)
1777 plugin_notification_meta_add_string (dst, meta->name,
1778 meta->nm_value.nm_string);
1779 else if (meta->type == NM_TYPE_SIGNED_INT)
1780 plugin_notification_meta_add_signed_int (dst, meta->name,
1781 meta->nm_value.nm_signed_int);
1782 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1783 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1784 meta->nm_value.nm_unsigned_int);
1785 else if (meta->type == NM_TYPE_DOUBLE)
1786 plugin_notification_meta_add_double (dst, meta->name,
1787 meta->nm_value.nm_double);
1788 else if (meta->type == NM_TYPE_BOOLEAN)
1789 plugin_notification_meta_add_boolean (dst, meta->name,
1790 meta->nm_value.nm_boolean);
1794 } /* int plugin_notification_meta_copy */
1796 int plugin_notification_meta_free (notification_meta_t *n)
1798 notification_meta_t *this;
1799 notification_meta_t *next;
1803 ERROR ("plugin_notification_meta_free: n == NULL!");
1808 while (this != NULL)
1812 if (this->type == NM_TYPE_STRING)
1814 free ((char *)this->nm_value.nm_string);
1815 this->nm_value.nm_string = NULL;
1823 } /* int plugin_notification_meta_free */
1825 /* vim: set sw=8 ts=8 noet fdm=marker : */