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
49 typedef struct callback_func_s callback_func_t;
53 #define RF_REMOVE 65535
56 /* `read_func_t' "inherits" from `callback_func_t'.
57 * The `rf_super' member MUST be the first one in this structure! */
58 #define rf_callback rf_super.cf_callback
59 #define rf_udata rf_super.cf_udata
60 callback_func_t rf_super;
61 char rf_group[DATA_MAX_NAME_LEN];
62 char rf_name[DATA_MAX_NAME_LEN];
64 struct timespec rf_interval;
65 struct timespec rf_effective_interval;
66 struct timespec rf_next_read;
68 typedef struct read_func_s read_func_t;
73 static llist_t *list_init;
74 static llist_t *list_write;
75 static llist_t *list_flush;
76 static llist_t *list_missing;
77 static llist_t *list_shutdown;
78 static llist_t *list_log;
79 static llist_t *list_notification;
81 static fc_chain_t *pre_cache_chain = NULL;
82 static fc_chain_t *post_cache_chain = NULL;
84 static c_avl_tree_t *data_sets;
86 static char *plugindir = NULL;
88 static c_heap_t *read_heap = NULL;
89 static llist_t *read_list;
90 static int read_loop = 1;
91 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
92 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
93 static pthread_t *read_threads = NULL;
94 static int read_threads_num = 0;
99 static const char *plugin_get_dir (void)
101 if (plugindir == NULL)
107 static void destroy_callback (callback_func_t *cf) /* {{{ */
112 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
114 cf->cf_udata.free_func (cf->cf_udata.data);
115 cf->cf_udata.data = NULL;
116 cf->cf_udata.free_func = NULL;
119 } /* }}} void destroy_callback */
121 static void destroy_all_callbacks (llist_t **list) /* {{{ */
128 le = llist_head (*list);
136 destroy_callback (le->value);
142 llist_destroy (*list);
144 } /* }}} void destroy_all_callbacks */
146 static void destroy_read_heap (void) /* {{{ */
148 if (read_heap == NULL)
155 cf = c_heap_get_root (read_heap);
159 destroy_callback (cf);
162 c_heap_destroy (read_heap);
164 } /* }}} void destroy_read_heap */
166 static int register_callback (llist_t **list, /* {{{ */
167 const char *name, callback_func_t *cf)
174 *list = llist_create ();
177 ERROR ("plugin: register_callback: "
178 "llist_create failed.");
179 destroy_callback (cf);
187 ERROR ("plugin: register_callback: strdup failed.");
188 destroy_callback (cf);
192 le = llist_search (*list, name);
195 le = llentry_create (key, cf);
198 ERROR ("plugin: register_callback: "
199 "llentry_create failed.");
201 destroy_callback (cf);
205 llist_append (*list, le);
209 callback_func_t *old_cf;
214 WARNING ("plugin: register_callback: "
215 "a callback named `%s' already exists - "
216 "overwriting the old entry!", name);
218 destroy_callback (old_cf);
223 } /* }}} int register_callback */
225 static int create_register_callback (llist_t **list, /* {{{ */
226 const char *name, void *callback, user_data_t *ud)
230 cf = (callback_func_t *) malloc (sizeof (*cf));
233 ERROR ("plugin: create_register_callback: malloc failed.");
236 memset (cf, 0, sizeof (*cf));
238 cf->cf_callback = callback;
241 cf->cf_udata.data = NULL;
242 cf->cf_udata.free_func = NULL;
249 return (register_callback (list, name, cf));
250 } /* }}} int create_register_callback */
252 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
259 e = llist_search (list, name);
263 llist_remove (list, e);
266 destroy_callback (e->value);
271 } /* }}} int plugin_unregister */
274 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
275 * object, but it will bitch about a shared object not having a
276 * ``module_register'' symbol..
278 static int plugin_load_file (char *file, uint32_t flags)
281 void (*reg_handle) (void);
283 DEBUG ("file = %s", file);
286 lt_dlerror (); /* clear errors */
288 #if LIBTOOL_VERSION == 2
289 if (flags & PLUGIN_FLAGS_GLOBAL) {
291 lt_dladvise_init(&advise);
292 lt_dladvise_global(&advise);
293 dlh = lt_dlopenadvise(file, advise);
294 lt_dladvise_destroy(&advise);
296 dlh = lt_dlopen (file);
298 #else /* if LIBTOOL_VERSION == 1 */
299 if (flags & PLUGIN_FLAGS_GLOBAL)
300 ERROR ("plugin_load_file: The global flag is not supported, "
301 "libtool 2 is required for this.");
302 dlh = lt_dlopen (file);
307 const char *error = lt_dlerror ();
309 ERROR ("lt_dlopen (%s) failed: %s", file, error);
310 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
314 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
316 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
317 file, lt_dlerror ());
327 static _Bool timeout_reached(struct timespec timeout)
330 gettimeofday(&now, NULL);
331 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
334 static void *plugin_read_thread (void __attribute__((unused)) *args)
336 while (read_loop != 0)
344 /* Get the read function that needs to be read next. */
345 rf = c_heap_get_root (read_heap);
348 struct timespec abstime;
352 CDTIME_T_TO_TIMESPEC (now + interval_g, &abstime);
354 pthread_mutex_lock (&read_lock);
355 pthread_cond_timedwait (&read_cond, &read_lock,
357 pthread_mutex_unlock (&read_lock);
361 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
365 CDTIME_T_TO_TIMESPEC (interval_g, &rf->rf_interval);
367 rf->rf_effective_interval = rf->rf_interval;
369 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
372 /* sleep until this entry is due,
373 * using pthread_cond_timedwait */
374 pthread_mutex_lock (&read_lock);
375 /* In pthread_cond_timedwait, spurious wakeups are possible
376 * (and really happen, at least on NetBSD with > 1 CPU), thus
377 * we need to re-evaluate the condition every time
378 * pthread_cond_timedwait returns. */
380 while ((read_loop != 0)
381 && !timeout_reached(rf->rf_next_read)
384 rc = pthread_cond_timedwait (&read_cond, &read_lock,
388 /* Must hold `read_lock' when accessing `rf->rf_type'. */
389 rf_type = rf->rf_type;
390 pthread_mutex_unlock (&read_lock);
392 /* Check if we're supposed to stop.. This may have interrupted
396 /* Insert `rf' again, so it can be free'd correctly */
397 c_heap_insert (read_heap, rf);
401 /* The entry has been marked for deletion. The linked list
402 * entry has already been removed by `plugin_unregister_read'.
403 * All we have to do here is free the `read_func_t' and
405 if (rf_type == RF_REMOVE)
407 DEBUG ("plugin_read_thread: Destroying the `%s' "
408 "callback.", rf->rf_name);
409 destroy_callback ((callback_func_t *) rf);
414 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
416 if (rf_type == RF_SIMPLE)
418 int (*callback) (void);
420 callback = rf->rf_callback;
421 status = (*callback) ();
425 plugin_read_cb callback;
427 assert (rf_type == RF_COMPLEX);
429 callback = rf->rf_callback;
430 status = (*callback) (&rf->rf_udata);
433 /* If the function signals failure, we will increase the
434 * intervals in which it will be called. */
437 rf->rf_effective_interval.tv_sec *= 2;
438 rf->rf_effective_interval.tv_nsec *= 2;
439 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
441 if (rf->rf_effective_interval.tv_sec >= 86400)
443 rf->rf_effective_interval.tv_sec = 86400;
444 rf->rf_effective_interval.tv_nsec = 0;
447 NOTICE ("read-function of plugin `%s' failed. "
448 "Will suspend it for %i seconds.",
450 (int) rf->rf_effective_interval.tv_sec);
454 /* Success: Restore the interval, if it was changed. */
455 rf->rf_effective_interval = rf->rf_interval;
458 /* update the ``next read due'' field */
461 DEBUG ("plugin_read_thread: Effective interval of the "
462 "%s plugin is %i.%09i.",
464 (int) rf->rf_effective_interval.tv_sec,
465 (int) rf->rf_effective_interval.tv_nsec);
467 /* Calculate the next (absolute) time at which this function
468 * should be called. */
469 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
470 + rf->rf_effective_interval.tv_sec;
471 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
472 + rf->rf_effective_interval.tv_nsec;
473 NORMALIZE_TIMESPEC (rf->rf_next_read);
475 /* Check, if `rf_next_read' is in the past. */
476 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
478 /* `rf_next_read' is in the past. Insert `now'
479 * so this value doesn't trail off into the
481 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
484 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
486 (int) rf->rf_next_read.tv_sec,
487 (int) rf->rf_next_read.tv_nsec);
489 /* Re-insert this read function into the heap again. */
490 c_heap_insert (read_heap, rf);
491 } /* while (read_loop) */
495 } /* void *plugin_read_thread */
497 static void start_read_threads (int num)
501 if (read_threads != NULL)
504 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
505 if (read_threads == NULL)
507 ERROR ("plugin: start_read_threads: calloc failed.");
511 read_threads_num = 0;
512 for (i = 0; i < num; i++)
514 if (pthread_create (read_threads + read_threads_num, NULL,
515 plugin_read_thread, NULL) == 0)
521 ERROR ("plugin: start_read_threads: pthread_create failed.");
525 } /* void start_read_threads */
527 static void stop_read_threads (void)
531 if (read_threads == NULL)
534 INFO ("collectd: Stopping %i read threads.", read_threads_num);
536 pthread_mutex_lock (&read_lock);
538 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
539 pthread_cond_broadcast (&read_cond);
540 pthread_mutex_unlock (&read_lock);
542 for (i = 0; i < read_threads_num; i++)
544 if (pthread_join (read_threads[i], NULL) != 0)
546 ERROR ("plugin: stop_read_threads: pthread_join failed.");
548 read_threads[i] = (pthread_t) 0;
550 sfree (read_threads);
551 read_threads_num = 0;
552 } /* void stop_read_threads */
557 void plugin_set_dir (const char *dir)
559 if (plugindir != NULL)
564 else if ((plugindir = strdup (dir)) == NULL)
567 ERROR ("strdup failed: %s",
568 sstrerror (errno, errbuf, sizeof (errbuf)));
573 int plugin_load (const char *type, uint32_t flags)
577 char filename[BUFSIZE] = "";
578 char typename[BUFSIZE];
585 DEBUG ("type = %s", type);
587 dir = plugin_get_dir ();
590 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
591 * type when matching the filename */
592 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
593 if ((status < 0) || ((size_t) status >= sizeof (typename)))
595 WARNING ("snprintf: truncated: `%s.so'", type);
598 typename_len = strlen (typename);
600 if ((dh = opendir (dir)) == NULL)
603 ERROR ("opendir (%s): %s", dir,
604 sstrerror (errno, errbuf, sizeof (errbuf)));
608 while ((de = readdir (dh)) != NULL)
610 if (strncasecmp (de->d_name, typename, typename_len))
613 status = ssnprintf (filename, sizeof (filename),
614 "%s/%s", dir, de->d_name);
615 if ((status < 0) || ((size_t) status >= sizeof (filename)))
617 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
621 if (lstat (filename, &statbuf) == -1)
624 WARNING ("stat %s: %s", filename,
625 sstrerror (errno, errbuf, sizeof (errbuf)));
628 else if (!S_ISREG (statbuf.st_mode))
630 /* don't follow symlinks */
631 WARNING ("stat %s: not a regular file", filename);
635 if (plugin_load_file (filename, flags) == 0)
643 fprintf (stderr, "Unable to load plugin %s.\n", type);
649 if (filename[0] == '\0')
650 fprintf (stderr, "Could not find plugin %s.\n", type);
656 * The `register_*' functions follow
658 int plugin_register_config (const char *name,
659 int (*callback) (const char *key, const char *val),
660 const char **keys, int keys_num)
662 cf_register (name, callback, keys, keys_num);
664 } /* int plugin_register_config */
666 int plugin_register_complex_config (const char *type,
667 int (*callback) (oconfig_item_t *))
669 return (cf_register_complex (type, callback));
670 } /* int plugin_register_complex_config */
672 int plugin_register_init (const char *name,
673 int (*callback) (void))
675 return (create_register_callback (&list_init, name, (void *) callback,
676 /* user_data = */ NULL));
677 } /* plugin_register_init */
679 static int plugin_compare_read_func (const void *arg0, const void *arg1)
681 const read_func_t *rf0;
682 const read_func_t *rf1;
687 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
689 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
691 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
693 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
697 } /* int plugin_compare_read_func */
699 /* Add a read function to both, the heap and a linked list. The linked list if
700 * used to look-up read functions, especially for the remove function. The heap
701 * is used to determine which plugin to read next. */
702 static int plugin_insert_read (read_func_t *rf)
707 pthread_mutex_lock (&read_lock);
709 if (read_list == NULL)
711 read_list = llist_create ();
712 if (read_list == NULL)
714 pthread_mutex_unlock (&read_lock);
715 ERROR ("plugin_insert_read: read_list failed.");
720 if (read_heap == NULL)
722 read_heap = c_heap_create (plugin_compare_read_func);
723 if (read_heap == NULL)
725 pthread_mutex_unlock (&read_lock);
726 ERROR ("plugin_insert_read: c_heap_create failed.");
731 le = llist_search (read_list, rf->rf_name);
734 pthread_mutex_unlock (&read_lock);
735 WARNING ("The read function \"%s\" is already registered. "
736 "Check for duplicate \"LoadPlugin\" lines "
737 "in your configuration!",
742 le = llentry_create (rf->rf_name, rf);
745 pthread_mutex_unlock (&read_lock);
746 ERROR ("plugin_insert_read: llentry_create failed.");
750 status = c_heap_insert (read_heap, rf);
753 pthread_mutex_unlock (&read_lock);
754 ERROR ("plugin_insert_read: c_heap_insert failed.");
755 llentry_destroy (le);
759 /* This does not fail. */
760 llist_append (read_list, le);
762 pthread_mutex_unlock (&read_lock);
764 } /* int plugin_insert_read */
766 int plugin_register_read (const char *name,
767 int (*callback) (void))
771 rf = (read_func_t *) malloc (sizeof (read_func_t));
775 ERROR ("plugin_register_read: malloc failed: %s",
776 sstrerror (errno, errbuf, sizeof (errbuf)));
780 memset (rf, 0, sizeof (read_func_t));
781 rf->rf_callback = (void *) callback;
782 rf->rf_udata.data = NULL;
783 rf->rf_udata.free_func = NULL;
784 rf->rf_group[0] = '\0';
785 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
786 rf->rf_type = RF_SIMPLE;
787 rf->rf_interval.tv_sec = 0;
788 rf->rf_interval.tv_nsec = 0;
789 rf->rf_effective_interval = rf->rf_interval;
791 return (plugin_insert_read (rf));
792 } /* int plugin_register_read */
794 int plugin_register_complex_read (const char *group, const char *name,
795 plugin_read_cb callback,
796 const struct timespec *interval,
797 user_data_t *user_data)
801 rf = (read_func_t *) malloc (sizeof (read_func_t));
804 ERROR ("plugin_register_complex_read: malloc failed.");
808 memset (rf, 0, sizeof (read_func_t));
809 rf->rf_callback = (void *) callback;
811 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
813 rf->rf_group[0] = '\0';
814 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
815 rf->rf_type = RF_COMPLEX;
816 if (interval != NULL)
818 rf->rf_interval = *interval;
820 rf->rf_effective_interval = rf->rf_interval;
823 if (user_data == NULL)
825 rf->rf_udata.data = NULL;
826 rf->rf_udata.free_func = NULL;
830 rf->rf_udata = *user_data;
833 return (plugin_insert_read (rf));
834 } /* int plugin_register_complex_read */
836 int plugin_register_write (const char *name,
837 plugin_write_cb callback, user_data_t *ud)
839 return (create_register_callback (&list_write, name,
840 (void *) callback, ud));
841 } /* int plugin_register_write */
843 int plugin_register_flush (const char *name,
844 plugin_flush_cb callback, user_data_t *ud)
846 return (create_register_callback (&list_flush, name,
847 (void *) callback, ud));
848 } /* int plugin_register_flush */
850 int plugin_register_missing (const char *name,
851 plugin_missing_cb callback, user_data_t *ud)
853 return (create_register_callback (&list_missing, name,
854 (void *) callback, ud));
855 } /* int plugin_register_missing */
857 int plugin_register_shutdown (char *name,
858 int (*callback) (void))
860 return (create_register_callback (&list_shutdown, name,
861 (void *) callback, /* user_data = */ NULL));
862 } /* int plugin_register_shutdown */
864 int plugin_register_data_set (const data_set_t *ds)
869 if ((data_sets != NULL)
870 && (c_avl_get (data_sets, ds->type, NULL) == 0))
872 NOTICE ("Replacing DS `%s' with another version.", ds->type);
873 plugin_unregister_data_set (ds->type);
875 else if (data_sets == NULL)
877 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
878 if (data_sets == NULL)
882 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
885 memcpy(ds_copy, ds, sizeof (data_set_t));
887 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
889 if (ds_copy->ds == NULL)
895 for (i = 0; i < ds->ds_num; i++)
896 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
898 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
899 } /* int plugin_register_data_set */
901 int plugin_register_log (const char *name,
902 plugin_log_cb callback, user_data_t *ud)
904 return (create_register_callback (&list_log, name,
905 (void *) callback, ud));
906 } /* int plugin_register_log */
908 int plugin_register_notification (const char *name,
909 plugin_notification_cb callback, user_data_t *ud)
911 return (create_register_callback (&list_notification, name,
912 (void *) callback, ud));
913 } /* int plugin_register_log */
915 int plugin_unregister_config (const char *name)
917 cf_unregister (name);
919 } /* int plugin_unregister_config */
921 int plugin_unregister_complex_config (const char *name)
923 cf_unregister_complex (name);
925 } /* int plugin_unregister_complex_config */
927 int plugin_unregister_init (const char *name)
929 return (plugin_unregister (list_init, name));
932 int plugin_unregister_read (const char *name) /* {{{ */
940 pthread_mutex_lock (&read_lock);
942 if (read_list == NULL)
944 pthread_mutex_unlock (&read_lock);
948 le = llist_search (read_list, name);
951 pthread_mutex_unlock (&read_lock);
952 WARNING ("plugin_unregister_read: No such read function: %s",
957 llist_remove (read_list, le);
961 rf->rf_type = RF_REMOVE;
963 pthread_mutex_unlock (&read_lock);
965 llentry_destroy (le);
967 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
970 } /* }}} int plugin_unregister_read */
972 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
974 read_func_t *rf = e->value;
977 return strcmp (rf->rf_group, (const char *)group);
978 } /* }}} int compare_read_func_group */
980 int plugin_unregister_read_group (const char *group) /* {{{ */
990 pthread_mutex_lock (&read_lock);
992 if (read_list == NULL)
994 pthread_mutex_unlock (&read_lock);
1000 le = llist_search_custom (read_list,
1001 compare_read_func_group, (void *)group);
1008 llist_remove (read_list, le);
1011 assert (rf != NULL);
1012 rf->rf_type = RF_REMOVE;
1014 llentry_destroy (le);
1016 DEBUG ("plugin_unregister_read_group: "
1017 "Marked `%s' (group `%s') for removal.",
1018 rf->rf_name, group);
1021 pthread_mutex_unlock (&read_lock);
1025 WARNING ("plugin_unregister_read_group: No such "
1026 "group of read function: %s", group);
1031 } /* }}} int plugin_unregister_read_group */
1033 int plugin_unregister_write (const char *name)
1035 return (plugin_unregister (list_write, name));
1038 int plugin_unregister_flush (const char *name)
1040 return (plugin_unregister (list_flush, name));
1043 int plugin_unregister_missing (const char *name)
1045 return (plugin_unregister (list_missing, name));
1048 int plugin_unregister_shutdown (const char *name)
1050 return (plugin_unregister (list_shutdown, name));
1053 int plugin_unregister_data_set (const char *name)
1057 if (data_sets == NULL)
1060 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1067 } /* int plugin_unregister_data_set */
1069 int plugin_unregister_log (const char *name)
1071 return (plugin_unregister (list_log, name));
1074 int plugin_unregister_notification (const char *name)
1076 return (plugin_unregister (list_notification, name));
1079 void plugin_init_all (void)
1081 const char *chain_name;
1085 /* Init the value cache */
1088 chain_name = global_option_get ("PreCacheChain");
1089 pre_cache_chain = fc_chain_get_by_name (chain_name);
1091 chain_name = global_option_get ("PostCacheChain");
1092 post_cache_chain = fc_chain_get_by_name (chain_name);
1095 if ((list_init == NULL) && (read_heap == NULL))
1098 /* Calling all init callbacks before checking if read callbacks
1099 * are available allows the init callbacks to register the read
1101 le = llist_head (list_init);
1104 callback_func_t *cf;
1105 plugin_init_cb callback;
1108 callback = cf->cf_callback;
1109 status = (*callback) ();
1113 ERROR ("Initialization of plugin `%s' "
1114 "failed with status %i. "
1115 "Plugin will be unloaded.",
1117 /* Plugins that register read callbacks from the init
1118 * callback should take care of appropriate error
1119 * handling themselves. */
1120 /* FIXME: Unload _all_ functions */
1121 plugin_unregister_read (le->key);
1127 /* Start read-threads */
1128 if (read_heap != NULL)
1132 rt = global_option_get ("ReadThreads");
1135 start_read_threads ((num > 0) ? num : 5);
1137 } /* void plugin_init_all */
1139 /* TODO: Rename this function. */
1140 void plugin_read_all (void)
1142 uc_check_timeout ();
1145 } /* void plugin_read_all */
1147 /* Read function called when the `-T' command line argument is given. */
1148 int plugin_read_all_once (void)
1151 int return_status = 0;
1153 if (read_heap == NULL)
1155 NOTICE ("No read-functions are registered.");
1163 rf = c_heap_get_root (read_heap);
1167 if (rf->rf_type == RF_SIMPLE)
1169 int (*callback) (void);
1171 callback = rf->rf_callback;
1172 status = (*callback) ();
1176 plugin_read_cb callback;
1178 callback = rf->rf_callback;
1179 status = (*callback) (&rf->rf_udata);
1184 NOTICE ("read-function of plugin `%s' failed.",
1189 destroy_callback ((void *) rf);
1192 return (return_status);
1193 } /* int plugin_read_all_once */
1195 int plugin_write (const char *plugin, /* {{{ */
1196 const data_set_t *ds, const value_list_t *vl)
1204 if (list_write == NULL)
1209 ds = plugin_get_ds (vl->type);
1212 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1222 le = llist_head (list_write);
1225 callback_func_t *cf = le->value;
1226 plugin_write_cb callback;
1228 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1229 callback = cf->cf_callback;
1230 status = (*callback) (ds, vl, &cf->cf_udata);
1239 if ((success == 0) && (failure != 0))
1244 else /* plugin != NULL */
1246 callback_func_t *cf;
1247 plugin_write_cb callback;
1249 le = llist_head (list_write);
1252 if (strcasecmp (plugin, le->key) == 0)
1263 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1264 callback = cf->cf_callback;
1265 status = (*callback) (ds, vl, &cf->cf_udata);
1269 } /* }}} int plugin_write */
1271 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1275 if (list_flush == NULL)
1278 le = llist_head (list_flush);
1281 callback_func_t *cf;
1282 plugin_flush_cb callback;
1284 if ((plugin != NULL)
1285 && (strcmp (plugin, le->key) != 0))
1292 callback = cf->cf_callback;
1294 (*callback) (timeout, identifier, &cf->cf_udata);
1299 } /* int plugin_flush */
1301 void plugin_shutdown_all (void)
1305 stop_read_threads ();
1307 destroy_all_callbacks (&list_init);
1309 pthread_mutex_lock (&read_lock);
1310 llist_destroy (read_list);
1312 pthread_mutex_unlock (&read_lock);
1314 destroy_read_heap ();
1316 plugin_flush (/* plugin = */ NULL,
1318 /* identifier = */ NULL);
1321 if (list_shutdown != NULL)
1322 le = llist_head (list_shutdown);
1326 callback_func_t *cf;
1327 plugin_shutdown_cb callback;
1330 callback = cf->cf_callback;
1332 /* Advance the pointer before calling the callback allows
1333 * shutdown functions to unregister themselves. If done the
1334 * other way around the memory `le' points to will be freed
1335 * after callback returns. */
1341 /* Write plugins which use the `user_data' pointer usually need the
1342 * same data available to the flush callback. If this is the case, set
1343 * the free_function to NULL when registering the flush callback and to
1344 * the real free function when registering the write callback. This way
1345 * the data isn't freed twice. */
1346 destroy_all_callbacks (&list_flush);
1347 destroy_all_callbacks (&list_missing);
1348 destroy_all_callbacks (&list_write);
1350 destroy_all_callbacks (&list_notification);
1351 destroy_all_callbacks (&list_shutdown);
1352 destroy_all_callbacks (&list_log);
1353 } /* void plugin_shutdown_all */
1355 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1359 if (list_missing == NULL)
1362 le = llist_head (list_missing);
1365 callback_func_t *cf;
1366 plugin_missing_cb callback;
1370 callback = cf->cf_callback;
1372 status = (*callback) (vl, &cf->cf_udata);
1377 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1378 "failed with status %i.",
1391 } /* int }}} plugin_dispatch_missing */
1393 int plugin_dispatch_values (value_list_t *vl)
1396 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1398 value_t *saved_values;
1399 int saved_values_len;
1403 int free_meta_data = 0;
1405 if ((vl == NULL) || (vl->type[0] == 0)
1406 || (vl->values == NULL) || (vl->values_len < 1))
1408 ERROR ("plugin_dispatch_values: Invalid value list.");
1412 /* Free meta data only if the calling function didn't specify any. In
1413 * this case matches and targets may add some and the calling function
1414 * may not expect (and therefore free) that data. */
1415 if (vl->meta == NULL)
1418 if (list_write == NULL)
1419 c_complain_once (LOG_WARNING, &no_write_complaint,
1420 "plugin_dispatch_values: No write callback has been "
1421 "registered. Please load at least one output plugin, "
1422 "if you want the collected data to be stored.");
1424 if (data_sets == NULL)
1426 ERROR ("plugin_dispatch_values: No data sets registered. "
1427 "Could the types database be read? Check "
1428 "your `TypesDB' setting!");
1432 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1434 char ident[6 * DATA_MAX_NAME_LEN];
1436 FORMAT_VL (ident, sizeof (ident), vl);
1437 INFO ("plugin_dispatch_values: Dataset not found: %s "
1438 "(from \"%s\"), check your types.db!",
1444 vl->time = cdtime ();
1446 if (vl->interval <= 0)
1447 vl->interval = interval_g;
1449 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1451 "plugin = %s; plugin_instance = %s; "
1452 "type = %s; type_instance = %s;",
1453 CDTIME_T_TO_DOUBLE (vl->time),
1454 CDTIME_T_TO_DOUBLE (vl->interval),
1456 vl->plugin, vl->plugin_instance,
1457 vl->type, vl->type_instance);
1460 assert (0 == strcmp (ds->type, vl->type));
1462 if (0 != strcmp (ds->type, vl->type))
1463 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1464 ds->type, vl->type);
1468 assert (ds->ds_num == vl->values_len);
1470 if (ds->ds_num != vl->values_len)
1472 ERROR ("plugin_dispatch_values: ds->type = %s: "
1473 "(ds->ds_num = %i) != "
1474 "(vl->values_len = %i)",
1475 ds->type, ds->ds_num, vl->values_len);
1480 escape_slashes (vl->host, sizeof (vl->host));
1481 escape_slashes (vl->plugin, sizeof (vl->plugin));
1482 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1483 escape_slashes (vl->type, sizeof (vl->type));
1484 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1486 /* Copy the values. This way, we can assure `targets' that they get
1487 * dynamically allocated values, which they can free and replace if
1489 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1491 saved_values = vl->values;
1492 saved_values_len = vl->values_len;
1494 vl->values = (value_t *) calloc (vl->values_len,
1495 sizeof (*vl->values));
1496 if (vl->values == NULL)
1498 ERROR ("plugin_dispatch_values: calloc failed.");
1499 vl->values = saved_values;
1502 memcpy (vl->values, saved_values,
1503 vl->values_len * sizeof (*vl->values));
1505 else /* if ((pre == NULL) && (post == NULL)) */
1507 saved_values = NULL;
1508 saved_values_len = 0;
1511 if (pre_cache_chain != NULL)
1513 status = fc_process_chain (ds, vl, pre_cache_chain);
1516 WARNING ("plugin_dispatch_values: Running the "
1517 "pre-cache chain failed with "
1521 else if (status == FC_TARGET_STOP)
1523 /* Restore the state of the value_list so that plugins
1524 * don't get confused.. */
1525 if (saved_values != NULL)
1528 vl->values = saved_values;
1529 vl->values_len = saved_values_len;
1535 /* Update the value cache */
1538 if (post_cache_chain != NULL)
1540 status = fc_process_chain (ds, vl, post_cache_chain);
1543 WARNING ("plugin_dispatch_values: Running the "
1544 "post-cache chain failed with "
1550 fc_default_action (ds, vl);
1552 /* Restore the state of the value_list so that plugins don't get
1554 if (saved_values != NULL)
1557 vl->values = saved_values;
1558 vl->values_len = saved_values_len;
1561 if ((free_meta_data != 0) && (vl->meta != NULL))
1563 meta_data_destroy (vl->meta);
1568 } /* int plugin_dispatch_values */
1570 int plugin_dispatch_notification (const notification_t *notif)
1573 /* Possible TODO: Add flap detection here */
1575 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1576 "time = %.3f; host = %s;",
1577 notif->severity, notif->message,
1578 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1580 /* Nobody cares for notifications */
1581 if (list_notification == NULL)
1584 le = llist_head (list_notification);
1587 callback_func_t *cf;
1588 plugin_notification_cb callback;
1592 callback = cf->cf_callback;
1593 status = (*callback) (notif, &cf->cf_udata);
1596 WARNING ("plugin_dispatch_notification: Notification "
1597 "callback %s returned %i.",
1605 } /* int plugin_dispatch_notification */
1607 void plugin_log (int level, const char *format, ...)
1614 if (level >= LOG_DEBUG)
1618 va_start (ap, format);
1619 vsnprintf (msg, sizeof (msg), format, ap);
1620 msg[sizeof (msg) - 1] = '\0';
1623 if (list_log == NULL)
1625 fprintf (stderr, "%s\n", msg);
1629 le = llist_head (list_log);
1632 callback_func_t *cf;
1633 plugin_log_cb callback;
1636 callback = cf->cf_callback;
1638 (*callback) (level, msg, &cf->cf_udata);
1642 } /* void plugin_log */
1644 const data_set_t *plugin_get_ds (const char *name)
1648 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1650 DEBUG ("No such dataset registered: %s", name);
1655 } /* data_set_t *plugin_get_ds */
1657 static int plugin_notification_meta_add (notification_t *n,
1659 enum notification_meta_type_e type,
1662 notification_meta_t *meta;
1663 notification_meta_t *tail;
1665 if ((n == NULL) || (name == NULL) || (value == NULL))
1667 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1671 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1674 ERROR ("plugin_notification_meta_add: malloc failed.");
1677 memset (meta, 0, sizeof (notification_meta_t));
1679 sstrncpy (meta->name, name, sizeof (meta->name));
1684 case NM_TYPE_STRING:
1686 meta->nm_value.nm_string = strdup ((const char *) value);
1687 if (meta->nm_value.nm_string == NULL)
1689 ERROR ("plugin_notification_meta_add: strdup failed.");
1695 case NM_TYPE_SIGNED_INT:
1697 meta->nm_value.nm_signed_int = *((int64_t *) value);
1700 case NM_TYPE_UNSIGNED_INT:
1702 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1705 case NM_TYPE_DOUBLE:
1707 meta->nm_value.nm_double = *((double *) value);
1710 case NM_TYPE_BOOLEAN:
1712 meta->nm_value.nm_boolean = *((_Bool *) value);
1717 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1721 } /* switch (type) */
1725 while ((tail != NULL) && (tail->next != NULL))
1734 } /* int plugin_notification_meta_add */
1736 int plugin_notification_meta_add_string (notification_t *n,
1740 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1743 int plugin_notification_meta_add_signed_int (notification_t *n,
1747 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1750 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1754 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1757 int plugin_notification_meta_add_double (notification_t *n,
1761 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1764 int plugin_notification_meta_add_boolean (notification_t *n,
1768 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1771 int plugin_notification_meta_copy (notification_t *dst,
1772 const notification_t *src)
1774 notification_meta_t *meta;
1776 assert (dst != NULL);
1777 assert (src != NULL);
1778 assert (dst != src);
1779 assert ((src->meta == NULL) || (src->meta != dst->meta));
1781 for (meta = src->meta; meta != NULL; meta = meta->next)
1783 if (meta->type == NM_TYPE_STRING)
1784 plugin_notification_meta_add_string (dst, meta->name,
1785 meta->nm_value.nm_string);
1786 else if (meta->type == NM_TYPE_SIGNED_INT)
1787 plugin_notification_meta_add_signed_int (dst, meta->name,
1788 meta->nm_value.nm_signed_int);
1789 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1790 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1791 meta->nm_value.nm_unsigned_int);
1792 else if (meta->type == NM_TYPE_DOUBLE)
1793 plugin_notification_meta_add_double (dst, meta->name,
1794 meta->nm_value.nm_double);
1795 else if (meta->type == NM_TYPE_BOOLEAN)
1796 plugin_notification_meta_add_boolean (dst, meta->name,
1797 meta->nm_value.nm_boolean);
1801 } /* int plugin_notification_meta_copy */
1803 int plugin_notification_meta_free (notification_meta_t *n)
1805 notification_meta_t *this;
1806 notification_meta_t *next;
1810 ERROR ("plugin_notification_meta_free: n == NULL!");
1815 while (this != NULL)
1819 if (this->type == NM_TYPE_STRING)
1821 free ((char *)this->nm_value.nm_string);
1822 this->nm_value.nm_string = NULL;
1830 } /* int plugin_notification_meta_free */
1832 /* vim: set sw=8 ts=8 noet fdm=marker : */