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);
284 lt_dlerror (); /* clear errors */
286 #if LIBTOOL_VERSION == 2
287 if (flags & PLUGIN_FLAGS_GLOBAL) {
289 lt_dladvise_init(&advise);
290 lt_dladvise_global(&advise);
291 dlh = lt_dlopenadvise(file, advise);
292 lt_dladvise_destroy(&advise);
294 dlh = lt_dlopen (file);
296 #else /* if LIBTOOL_VERSION == 1 */
297 if (flags & PLUGIN_FLAGS_GLOBAL)
298 WARNING ("plugin_load_file: The global flag is not supported, "
299 "libtool 2 is required for this.");
300 dlh = lt_dlopen (file);
305 char errbuf[1024] = "";
307 ssnprintf (errbuf, sizeof (errbuf),
308 "lt_dlopen (\"%s\") failed: %s. "
309 "The most common cause for this problem are "
310 "missing dependencies. Use ldd(1) to check "
311 "the dependencies of the plugin "
313 file, lt_dlerror ());
315 ERROR ("%s", errbuf);
316 /* Make sure this is printed to STDERR in any case, but also
317 * make sure it's printed only once. */
318 if (list_log != NULL)
319 fprintf (stderr, "ERROR: %s\n", errbuf);
324 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
326 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
327 file, lt_dlerror ());
337 static _Bool timeout_reached(struct timespec timeout)
340 gettimeofday(&now, NULL);
341 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
344 static void *plugin_read_thread (void __attribute__((unused)) *args)
346 while (read_loop != 0)
354 /* Get the read function that needs to be read next. */
355 rf = c_heap_get_root (read_heap);
358 struct timespec abstime;
362 CDTIME_T_TO_TIMESPEC (now + interval_g, &abstime);
364 pthread_mutex_lock (&read_lock);
365 pthread_cond_timedwait (&read_cond, &read_lock,
367 pthread_mutex_unlock (&read_lock);
371 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
375 CDTIME_T_TO_TIMESPEC (interval_g, &rf->rf_interval);
377 rf->rf_effective_interval = rf->rf_interval;
379 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
382 /* sleep until this entry is due,
383 * using pthread_cond_timedwait */
384 pthread_mutex_lock (&read_lock);
385 /* In pthread_cond_timedwait, spurious wakeups are possible
386 * (and really happen, at least on NetBSD with > 1 CPU), thus
387 * we need to re-evaluate the condition every time
388 * pthread_cond_timedwait returns. */
390 while ((read_loop != 0)
391 && !timeout_reached(rf->rf_next_read)
394 rc = pthread_cond_timedwait (&read_cond, &read_lock,
398 /* Must hold `read_lock' when accessing `rf->rf_type'. */
399 rf_type = rf->rf_type;
400 pthread_mutex_unlock (&read_lock);
402 /* Check if we're supposed to stop.. This may have interrupted
406 /* Insert `rf' again, so it can be free'd correctly */
407 c_heap_insert (read_heap, rf);
411 /* The entry has been marked for deletion. The linked list
412 * entry has already been removed by `plugin_unregister_read'.
413 * All we have to do here is free the `read_func_t' and
415 if (rf_type == RF_REMOVE)
417 DEBUG ("plugin_read_thread: Destroying the `%s' "
418 "callback.", rf->rf_name);
419 destroy_callback ((callback_func_t *) rf);
424 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
426 if (rf_type == RF_SIMPLE)
428 int (*callback) (void);
430 callback = rf->rf_callback;
431 status = (*callback) ();
435 plugin_read_cb callback;
437 assert (rf_type == RF_COMPLEX);
439 callback = rf->rf_callback;
440 status = (*callback) (&rf->rf_udata);
443 /* If the function signals failure, we will increase the
444 * intervals in which it will be called. */
447 rf->rf_effective_interval.tv_sec *= 2;
448 rf->rf_effective_interval.tv_nsec *= 2;
449 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
451 if (rf->rf_effective_interval.tv_sec >= 86400)
453 rf->rf_effective_interval.tv_sec = 86400;
454 rf->rf_effective_interval.tv_nsec = 0;
457 NOTICE ("read-function of plugin `%s' failed. "
458 "Will suspend it for %i seconds.",
460 (int) rf->rf_effective_interval.tv_sec);
464 /* Success: Restore the interval, if it was changed. */
465 rf->rf_effective_interval = rf->rf_interval;
468 /* update the ``next read due'' field */
471 DEBUG ("plugin_read_thread: Effective interval of the "
472 "%s plugin is %i.%09i.",
474 (int) rf->rf_effective_interval.tv_sec,
475 (int) rf->rf_effective_interval.tv_nsec);
477 /* Calculate the next (absolute) time at which this function
478 * should be called. */
479 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
480 + rf->rf_effective_interval.tv_sec;
481 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
482 + rf->rf_effective_interval.tv_nsec;
483 NORMALIZE_TIMESPEC (rf->rf_next_read);
485 /* Check, if `rf_next_read' is in the past. */
486 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
488 /* `rf_next_read' is in the past. Insert `now'
489 * so this value doesn't trail off into the
491 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
494 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
496 (int) rf->rf_next_read.tv_sec,
497 (int) rf->rf_next_read.tv_nsec);
499 /* Re-insert this read function into the heap again. */
500 c_heap_insert (read_heap, rf);
501 } /* while (read_loop) */
505 } /* void *plugin_read_thread */
507 static void start_read_threads (int num)
511 if (read_threads != NULL)
514 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
515 if (read_threads == NULL)
517 ERROR ("plugin: start_read_threads: calloc failed.");
521 read_threads_num = 0;
522 for (i = 0; i < num; i++)
524 if (pthread_create (read_threads + read_threads_num, NULL,
525 plugin_read_thread, NULL) == 0)
531 ERROR ("plugin: start_read_threads: pthread_create failed.");
535 } /* void start_read_threads */
537 static void stop_read_threads (void)
541 if (read_threads == NULL)
544 INFO ("collectd: Stopping %i read threads.", read_threads_num);
546 pthread_mutex_lock (&read_lock);
548 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
549 pthread_cond_broadcast (&read_cond);
550 pthread_mutex_unlock (&read_lock);
552 for (i = 0; i < read_threads_num; i++)
554 if (pthread_join (read_threads[i], NULL) != 0)
556 ERROR ("plugin: stop_read_threads: pthread_join failed.");
558 read_threads[i] = (pthread_t) 0;
560 sfree (read_threads);
561 read_threads_num = 0;
562 } /* void stop_read_threads */
567 void plugin_set_dir (const char *dir)
569 if (plugindir != NULL)
574 else if ((plugindir = strdup (dir)) == NULL)
577 ERROR ("strdup failed: %s",
578 sstrerror (errno, errbuf, sizeof (errbuf)));
583 int plugin_load (const char *type, uint32_t flags)
587 char filename[BUFSIZE] = "";
588 char typename[BUFSIZE];
595 DEBUG ("type = %s", type);
597 dir = plugin_get_dir ();
600 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
601 * type when matching the filename */
602 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
603 if ((status < 0) || ((size_t) status >= sizeof (typename)))
605 WARNING ("snprintf: truncated: `%s.so'", type);
608 typename_len = strlen (typename);
610 if ((dh = opendir (dir)) == NULL)
613 ERROR ("opendir (%s): %s", dir,
614 sstrerror (errno, errbuf, sizeof (errbuf)));
618 while ((de = readdir (dh)) != NULL)
620 if (strncasecmp (de->d_name, typename, typename_len))
623 status = ssnprintf (filename, sizeof (filename),
624 "%s/%s", dir, de->d_name);
625 if ((status < 0) || ((size_t) status >= sizeof (filename)))
627 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
631 if (lstat (filename, &statbuf) == -1)
634 WARNING ("stat %s: %s", filename,
635 sstrerror (errno, errbuf, sizeof (errbuf)));
638 else if (!S_ISREG (statbuf.st_mode))
640 /* don't follow symlinks */
641 WARNING ("stat %s: not a regular file", filename);
645 if (plugin_load_file (filename, flags) == 0)
653 fprintf (stderr, "Unable to load plugin %s.\n", type);
659 if (filename[0] == '\0')
660 fprintf (stderr, "Could not find plugin %s.\n", type);
666 * The `register_*' functions follow
668 int plugin_register_config (const char *name,
669 int (*callback) (const char *key, const char *val),
670 const char **keys, int keys_num)
672 cf_register (name, callback, keys, keys_num);
674 } /* int plugin_register_config */
676 int plugin_register_complex_config (const char *type,
677 int (*callback) (oconfig_item_t *))
679 return (cf_register_complex (type, callback));
680 } /* int plugin_register_complex_config */
682 int plugin_register_init (const char *name,
683 int (*callback) (void))
685 return (create_register_callback (&list_init, name, (void *) callback,
686 /* user_data = */ NULL));
687 } /* plugin_register_init */
689 static int plugin_compare_read_func (const void *arg0, const void *arg1)
691 const read_func_t *rf0;
692 const read_func_t *rf1;
697 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
699 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
701 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
703 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
707 } /* int plugin_compare_read_func */
709 /* Add a read function to both, the heap and a linked list. The linked list if
710 * used to look-up read functions, especially for the remove function. The heap
711 * is used to determine which plugin to read next. */
712 static int plugin_insert_read (read_func_t *rf)
717 pthread_mutex_lock (&read_lock);
719 if (read_list == NULL)
721 read_list = llist_create ();
722 if (read_list == NULL)
724 pthread_mutex_unlock (&read_lock);
725 ERROR ("plugin_insert_read: read_list failed.");
730 if (read_heap == NULL)
732 read_heap = c_heap_create (plugin_compare_read_func);
733 if (read_heap == NULL)
735 pthread_mutex_unlock (&read_lock);
736 ERROR ("plugin_insert_read: c_heap_create failed.");
741 le = llist_search (read_list, rf->rf_name);
744 pthread_mutex_unlock (&read_lock);
745 WARNING ("The read function \"%s\" is already registered. "
746 "Check for duplicate \"LoadPlugin\" lines "
747 "in your configuration!",
752 le = llentry_create (rf->rf_name, rf);
755 pthread_mutex_unlock (&read_lock);
756 ERROR ("plugin_insert_read: llentry_create failed.");
760 status = c_heap_insert (read_heap, rf);
763 pthread_mutex_unlock (&read_lock);
764 ERROR ("plugin_insert_read: c_heap_insert failed.");
765 llentry_destroy (le);
769 /* This does not fail. */
770 llist_append (read_list, le);
772 pthread_mutex_unlock (&read_lock);
774 } /* int plugin_insert_read */
776 int plugin_register_read (const char *name,
777 int (*callback) (void))
782 rf = malloc (sizeof (*rf));
785 ERROR ("plugin_register_read: malloc failed.");
789 memset (rf, 0, sizeof (read_func_t));
790 rf->rf_callback = (void *) callback;
791 rf->rf_udata.data = NULL;
792 rf->rf_udata.free_func = NULL;
793 rf->rf_group[0] = '\0';
794 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
795 rf->rf_type = RF_SIMPLE;
796 rf->rf_interval.tv_sec = 0;
797 rf->rf_interval.tv_nsec = 0;
798 rf->rf_effective_interval = rf->rf_interval;
800 status = plugin_insert_read (rf);
805 } /* int plugin_register_read */
807 int plugin_register_complex_read (const char *group, const char *name,
808 plugin_read_cb callback,
809 const struct timespec *interval,
810 user_data_t *user_data)
815 rf = malloc (sizeof (*rf));
818 ERROR ("plugin_register_complex_read: malloc failed.");
822 memset (rf, 0, sizeof (read_func_t));
823 rf->rf_callback = (void *) callback;
825 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
827 rf->rf_group[0] = '\0';
828 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
829 rf->rf_type = RF_COMPLEX;
830 if (interval != NULL)
832 rf->rf_interval = *interval;
834 rf->rf_effective_interval = rf->rf_interval;
837 if (user_data == NULL)
839 rf->rf_udata.data = NULL;
840 rf->rf_udata.free_func = NULL;
844 rf->rf_udata = *user_data;
847 status = plugin_insert_read (rf);
852 } /* int plugin_register_complex_read */
854 int plugin_register_write (const char *name,
855 plugin_write_cb callback, user_data_t *ud)
857 return (create_register_callback (&list_write, name,
858 (void *) callback, ud));
859 } /* int plugin_register_write */
861 int plugin_register_flush (const char *name,
862 plugin_flush_cb callback, user_data_t *ud)
864 return (create_register_callback (&list_flush, name,
865 (void *) callback, ud));
866 } /* int plugin_register_flush */
868 int plugin_register_missing (const char *name,
869 plugin_missing_cb callback, user_data_t *ud)
871 return (create_register_callback (&list_missing, name,
872 (void *) callback, ud));
873 } /* int plugin_register_missing */
875 int plugin_register_shutdown (const char *name,
876 int (*callback) (void))
878 return (create_register_callback (&list_shutdown, name,
879 (void *) callback, /* user_data = */ NULL));
880 } /* int plugin_register_shutdown */
882 int plugin_register_data_set (const data_set_t *ds)
887 if ((data_sets != NULL)
888 && (c_avl_get (data_sets, ds->type, NULL) == 0))
890 NOTICE ("Replacing DS `%s' with another version.", ds->type);
891 plugin_unregister_data_set (ds->type);
893 else if (data_sets == NULL)
895 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
896 if (data_sets == NULL)
900 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
903 memcpy(ds_copy, ds, sizeof (data_set_t));
905 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
907 if (ds_copy->ds == NULL)
913 for (i = 0; i < ds->ds_num; i++)
914 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
916 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
917 } /* int plugin_register_data_set */
919 int plugin_register_log (const char *name,
920 plugin_log_cb callback, user_data_t *ud)
922 return (create_register_callback (&list_log, name,
923 (void *) callback, ud));
924 } /* int plugin_register_log */
926 int plugin_register_notification (const char *name,
927 plugin_notification_cb callback, user_data_t *ud)
929 return (create_register_callback (&list_notification, name,
930 (void *) callback, ud));
931 } /* int plugin_register_log */
933 int plugin_unregister_config (const char *name)
935 cf_unregister (name);
937 } /* int plugin_unregister_config */
939 int plugin_unregister_complex_config (const char *name)
941 cf_unregister_complex (name);
943 } /* int plugin_unregister_complex_config */
945 int plugin_unregister_init (const char *name)
947 return (plugin_unregister (list_init, name));
950 int plugin_unregister_read (const char *name) /* {{{ */
958 pthread_mutex_lock (&read_lock);
960 if (read_list == NULL)
962 pthread_mutex_unlock (&read_lock);
966 le = llist_search (read_list, name);
969 pthread_mutex_unlock (&read_lock);
970 WARNING ("plugin_unregister_read: No such read function: %s",
975 llist_remove (read_list, le);
979 rf->rf_type = RF_REMOVE;
981 pthread_mutex_unlock (&read_lock);
983 llentry_destroy (le);
985 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
988 } /* }}} int plugin_unregister_read */
990 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
992 read_func_t *rf = e->value;
995 return strcmp (rf->rf_group, (const char *)group);
996 } /* }}} int compare_read_func_group */
998 int plugin_unregister_read_group (const char *group) /* {{{ */
1008 pthread_mutex_lock (&read_lock);
1010 if (read_list == NULL)
1012 pthread_mutex_unlock (&read_lock);
1018 le = llist_search_custom (read_list,
1019 compare_read_func_group, (void *)group);
1026 llist_remove (read_list, le);
1029 assert (rf != NULL);
1030 rf->rf_type = RF_REMOVE;
1032 llentry_destroy (le);
1034 DEBUG ("plugin_unregister_read_group: "
1035 "Marked `%s' (group `%s') for removal.",
1036 rf->rf_name, group);
1039 pthread_mutex_unlock (&read_lock);
1043 WARNING ("plugin_unregister_read_group: No such "
1044 "group of read function: %s", group);
1049 } /* }}} int plugin_unregister_read_group */
1051 int plugin_unregister_write (const char *name)
1053 return (plugin_unregister (list_write, name));
1056 int plugin_unregister_flush (const char *name)
1058 return (plugin_unregister (list_flush, name));
1061 int plugin_unregister_missing (const char *name)
1063 return (plugin_unregister (list_missing, name));
1066 int plugin_unregister_shutdown (const char *name)
1068 return (plugin_unregister (list_shutdown, name));
1071 int plugin_unregister_data_set (const char *name)
1075 if (data_sets == NULL)
1078 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1085 } /* int plugin_unregister_data_set */
1087 int plugin_unregister_log (const char *name)
1089 return (plugin_unregister (list_log, name));
1092 int plugin_unregister_notification (const char *name)
1094 return (plugin_unregister (list_notification, name));
1097 void plugin_init_all (void)
1099 const char *chain_name;
1103 /* Init the value cache */
1106 chain_name = global_option_get ("PreCacheChain");
1107 pre_cache_chain = fc_chain_get_by_name (chain_name);
1109 chain_name = global_option_get ("PostCacheChain");
1110 post_cache_chain = fc_chain_get_by_name (chain_name);
1113 if ((list_init == NULL) && (read_heap == NULL))
1116 /* Calling all init callbacks before checking if read callbacks
1117 * are available allows the init callbacks to register the read
1119 le = llist_head (list_init);
1122 callback_func_t *cf;
1123 plugin_init_cb callback;
1126 callback = cf->cf_callback;
1127 status = (*callback) ();
1131 ERROR ("Initialization of plugin `%s' "
1132 "failed with status %i. "
1133 "Plugin will be unloaded.",
1135 /* Plugins that register read callbacks from the init
1136 * callback should take care of appropriate error
1137 * handling themselves. */
1138 /* FIXME: Unload _all_ functions */
1139 plugin_unregister_read (le->key);
1145 /* Start read-threads */
1146 if (read_heap != NULL)
1150 rt = global_option_get ("ReadThreads");
1153 start_read_threads ((num > 0) ? num : 5);
1155 } /* void plugin_init_all */
1157 /* TODO: Rename this function. */
1158 void plugin_read_all (void)
1160 uc_check_timeout ();
1163 } /* void plugin_read_all */
1165 /* Read function called when the `-T' command line argument is given. */
1166 int plugin_read_all_once (void)
1169 int return_status = 0;
1171 if (read_heap == NULL)
1173 NOTICE ("No read-functions are registered.");
1181 rf = c_heap_get_root (read_heap);
1185 if (rf->rf_type == RF_SIMPLE)
1187 int (*callback) (void);
1189 callback = rf->rf_callback;
1190 status = (*callback) ();
1194 plugin_read_cb callback;
1196 callback = rf->rf_callback;
1197 status = (*callback) (&rf->rf_udata);
1202 NOTICE ("read-function of plugin `%s' failed.",
1207 destroy_callback ((void *) rf);
1210 return (return_status);
1211 } /* int plugin_read_all_once */
1213 int plugin_write (const char *plugin, /* {{{ */
1214 const data_set_t *ds, const value_list_t *vl)
1222 if (list_write == NULL)
1227 ds = plugin_get_ds (vl->type);
1230 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1240 le = llist_head (list_write);
1243 callback_func_t *cf = le->value;
1244 plugin_write_cb callback;
1246 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1247 callback = cf->cf_callback;
1248 status = (*callback) (ds, vl, &cf->cf_udata);
1257 if ((success == 0) && (failure != 0))
1262 else /* plugin != NULL */
1264 callback_func_t *cf;
1265 plugin_write_cb callback;
1267 le = llist_head (list_write);
1270 if (strcasecmp (plugin, le->key) == 0)
1281 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1282 callback = cf->cf_callback;
1283 status = (*callback) (ds, vl, &cf->cf_udata);
1287 } /* }}} int plugin_write */
1289 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1293 if (list_flush == NULL)
1296 le = llist_head (list_flush);
1299 callback_func_t *cf;
1300 plugin_flush_cb callback;
1302 if ((plugin != NULL)
1303 && (strcmp (plugin, le->key) != 0))
1310 callback = cf->cf_callback;
1312 (*callback) (timeout, identifier, &cf->cf_udata);
1317 } /* int plugin_flush */
1319 void plugin_shutdown_all (void)
1323 stop_read_threads ();
1325 destroy_all_callbacks (&list_init);
1327 pthread_mutex_lock (&read_lock);
1328 llist_destroy (read_list);
1330 pthread_mutex_unlock (&read_lock);
1332 destroy_read_heap ();
1334 plugin_flush (/* plugin = */ NULL,
1336 /* identifier = */ NULL);
1339 if (list_shutdown != NULL)
1340 le = llist_head (list_shutdown);
1344 callback_func_t *cf;
1345 plugin_shutdown_cb callback;
1348 callback = cf->cf_callback;
1350 /* Advance the pointer before calling the callback allows
1351 * shutdown functions to unregister themselves. If done the
1352 * other way around the memory `le' points to will be freed
1353 * after callback returns. */
1359 /* Write plugins which use the `user_data' pointer usually need the
1360 * same data available to the flush callback. If this is the case, set
1361 * the free_function to NULL when registering the flush callback and to
1362 * the real free function when registering the write callback. This way
1363 * the data isn't freed twice. */
1364 destroy_all_callbacks (&list_flush);
1365 destroy_all_callbacks (&list_missing);
1366 destroy_all_callbacks (&list_write);
1368 destroy_all_callbacks (&list_notification);
1369 destroy_all_callbacks (&list_shutdown);
1370 destroy_all_callbacks (&list_log);
1371 } /* void plugin_shutdown_all */
1373 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1377 if (list_missing == NULL)
1380 le = llist_head (list_missing);
1383 callback_func_t *cf;
1384 plugin_missing_cb callback;
1388 callback = cf->cf_callback;
1390 status = (*callback) (vl, &cf->cf_udata);
1395 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1396 "failed with status %i.",
1409 } /* int }}} plugin_dispatch_missing */
1411 int plugin_dispatch_values (value_list_t *vl)
1414 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1416 value_t *saved_values;
1417 int saved_values_len;
1421 int free_meta_data = 0;
1423 if ((vl == NULL) || (vl->type[0] == 0)
1424 || (vl->values == NULL) || (vl->values_len < 1))
1426 ERROR ("plugin_dispatch_values: Invalid value list "
1427 "from plugin %s.", vl->plugin);
1431 /* Free meta data only if the calling function didn't specify any. In
1432 * this case matches and targets may add some and the calling function
1433 * may not expect (and therefore free) that data. */
1434 if (vl->meta == NULL)
1437 if (list_write == NULL)
1438 c_complain_once (LOG_WARNING, &no_write_complaint,
1439 "plugin_dispatch_values: No write callback has been "
1440 "registered. Please load at least one output plugin, "
1441 "if you want the collected data to be stored.");
1443 if (data_sets == NULL)
1445 ERROR ("plugin_dispatch_values: No data sets registered. "
1446 "Could the types database be read? Check "
1447 "your `TypesDB' setting!");
1451 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1453 char ident[6 * DATA_MAX_NAME_LEN];
1455 FORMAT_VL (ident, sizeof (ident), vl);
1456 INFO ("plugin_dispatch_values: Dataset not found: %s "
1457 "(from \"%s\"), check your types.db!",
1463 vl->time = cdtime ();
1465 if (vl->interval <= 0)
1466 vl->interval = interval_g;
1468 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1470 "plugin = %s; plugin_instance = %s; "
1471 "type = %s; type_instance = %s;",
1472 CDTIME_T_TO_DOUBLE (vl->time),
1473 CDTIME_T_TO_DOUBLE (vl->interval),
1475 vl->plugin, vl->plugin_instance,
1476 vl->type, vl->type_instance);
1479 assert (0 == strcmp (ds->type, vl->type));
1481 if (0 != strcmp (ds->type, vl->type))
1482 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1483 ds->type, vl->type);
1487 assert (ds->ds_num == vl->values_len);
1489 if (ds->ds_num != vl->values_len)
1491 ERROR ("plugin_dispatch_values: ds->type = %s: "
1492 "(ds->ds_num = %i) != "
1493 "(vl->values_len = %i)",
1494 ds->type, ds->ds_num, vl->values_len);
1499 escape_slashes (vl->host, sizeof (vl->host));
1500 escape_slashes (vl->plugin, sizeof (vl->plugin));
1501 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1502 escape_slashes (vl->type, sizeof (vl->type));
1503 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1505 /* Copy the values. This way, we can assure `targets' that they get
1506 * dynamically allocated values, which they can free and replace if
1508 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1510 saved_values = vl->values;
1511 saved_values_len = vl->values_len;
1513 vl->values = (value_t *) calloc (vl->values_len,
1514 sizeof (*vl->values));
1515 if (vl->values == NULL)
1517 ERROR ("plugin_dispatch_values: calloc failed.");
1518 vl->values = saved_values;
1521 memcpy (vl->values, saved_values,
1522 vl->values_len * sizeof (*vl->values));
1524 else /* if ((pre == NULL) && (post == NULL)) */
1526 saved_values = NULL;
1527 saved_values_len = 0;
1530 if (pre_cache_chain != NULL)
1532 status = fc_process_chain (ds, vl, pre_cache_chain);
1535 WARNING ("plugin_dispatch_values: Running the "
1536 "pre-cache chain failed with "
1540 else if (status == FC_TARGET_STOP)
1542 /* Restore the state of the value_list so that plugins
1543 * don't get confused.. */
1544 if (saved_values != NULL)
1547 vl->values = saved_values;
1548 vl->values_len = saved_values_len;
1554 /* Update the value cache */
1557 if (post_cache_chain != NULL)
1559 status = fc_process_chain (ds, vl, post_cache_chain);
1562 WARNING ("plugin_dispatch_values: Running the "
1563 "post-cache chain failed with "
1569 fc_default_action (ds, vl);
1571 /* Restore the state of the value_list so that plugins don't get
1573 if (saved_values != NULL)
1576 vl->values = saved_values;
1577 vl->values_len = saved_values_len;
1580 if ((free_meta_data != 0) && (vl->meta != NULL))
1582 meta_data_destroy (vl->meta);
1587 } /* int plugin_dispatch_values */
1589 int plugin_dispatch_values_secure (const value_list_t *vl)
1591 value_list_t vl_copy;
1597 memcpy (&vl_copy, vl, sizeof (vl_copy));
1599 /* Write callbacks must not change the values and meta pointers, so we can
1600 * savely skip copying those and make this more efficient. */
1601 if ((pre_cache_chain == NULL) && (post_cache_chain == NULL))
1602 return (plugin_dispatch_values (&vl_copy));
1604 /* Set pointers to NULL, just to be on the save side. */
1605 vl_copy.values = NULL;
1606 vl_copy.meta = NULL;
1608 vl_copy.values = malloc (sizeof (*vl_copy.values) * vl->values_len);
1609 if (vl_copy.values == NULL)
1611 ERROR ("plugin_dispatch_values_secure: malloc failed.");
1614 memcpy (vl_copy.values, vl->values, sizeof (*vl_copy.values) * vl->values_len);
1616 if (vl->meta != NULL)
1618 vl_copy.meta = meta_data_clone (vl->meta);
1619 if (vl_copy.meta == NULL)
1621 ERROR ("plugin_dispatch_values_secure: meta_data_clone failed.");
1622 free (vl_copy.values);
1625 } /* if (vl->meta) */
1627 status = plugin_dispatch_values (&vl_copy);
1629 meta_data_destroy (vl_copy.meta);
1630 free (vl_copy.values);
1633 } /* int plugin_dispatch_values_secure */
1635 int plugin_dispatch_notification (const notification_t *notif)
1638 /* Possible TODO: Add flap detection here */
1640 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1641 "time = %.3f; host = %s;",
1642 notif->severity, notif->message,
1643 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1645 /* Nobody cares for notifications */
1646 if (list_notification == NULL)
1649 le = llist_head (list_notification);
1652 callback_func_t *cf;
1653 plugin_notification_cb callback;
1657 callback = cf->cf_callback;
1658 status = (*callback) (notif, &cf->cf_udata);
1661 WARNING ("plugin_dispatch_notification: Notification "
1662 "callback %s returned %i.",
1670 } /* int plugin_dispatch_notification */
1672 void plugin_log (int level, const char *format, ...)
1679 if (level >= LOG_DEBUG)
1683 va_start (ap, format);
1684 vsnprintf (msg, sizeof (msg), format, ap);
1685 msg[sizeof (msg) - 1] = '\0';
1688 if (list_log == NULL)
1690 fprintf (stderr, "%s\n", msg);
1694 le = llist_head (list_log);
1697 callback_func_t *cf;
1698 plugin_log_cb callback;
1701 callback = cf->cf_callback;
1703 (*callback) (level, msg, &cf->cf_udata);
1707 } /* void plugin_log */
1709 int parse_log_severity (const char *severity)
1713 if ((0 == strcasecmp (severity, "emerg"))
1714 || (0 == strcasecmp (severity, "alert"))
1715 || (0 == strcasecmp (severity, "crit"))
1716 || (0 == strcasecmp (severity, "err")))
1717 log_level = LOG_ERR;
1718 else if (0 == strcasecmp (severity, "warning"))
1719 log_level = LOG_WARNING;
1720 else if (0 == strcasecmp (severity, "notice"))
1721 log_level = LOG_NOTICE;
1722 else if (0 == strcasecmp (severity, "info"))
1723 log_level = LOG_INFO;
1725 else if (0 == strcasecmp (severity, "debug"))
1726 log_level = LOG_DEBUG;
1727 #endif /* COLLECT_DEBUG */
1730 } /* int parse_log_severity */
1732 int parse_notif_severity (const char *severity)
1734 int notif_severity = -1;
1736 if (strcasecmp (severity, "FAILURE") == 0)
1737 notif_severity = NOTIF_FAILURE;
1738 else if (strcmp (severity, "OKAY") == 0)
1739 notif_severity = NOTIF_OKAY;
1740 else if ((strcmp (severity, "WARNING") == 0)
1741 || (strcmp (severity, "WARN") == 0))
1742 notif_severity = NOTIF_WARNING;
1744 return (notif_severity);
1745 } /* int parse_notif_severity */
1747 const data_set_t *plugin_get_ds (const char *name)
1751 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1753 DEBUG ("No such dataset registered: %s", name);
1758 } /* data_set_t *plugin_get_ds */
1760 static int plugin_notification_meta_add (notification_t *n,
1762 enum notification_meta_type_e type,
1765 notification_meta_t *meta;
1766 notification_meta_t *tail;
1768 if ((n == NULL) || (name == NULL) || (value == NULL))
1770 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1774 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1777 ERROR ("plugin_notification_meta_add: malloc failed.");
1780 memset (meta, 0, sizeof (notification_meta_t));
1782 sstrncpy (meta->name, name, sizeof (meta->name));
1787 case NM_TYPE_STRING:
1789 meta->nm_value.nm_string = strdup ((const char *) value);
1790 if (meta->nm_value.nm_string == NULL)
1792 ERROR ("plugin_notification_meta_add: strdup failed.");
1798 case NM_TYPE_SIGNED_INT:
1800 meta->nm_value.nm_signed_int = *((int64_t *) value);
1803 case NM_TYPE_UNSIGNED_INT:
1805 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1808 case NM_TYPE_DOUBLE:
1810 meta->nm_value.nm_double = *((double *) value);
1813 case NM_TYPE_BOOLEAN:
1815 meta->nm_value.nm_boolean = *((_Bool *) value);
1820 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1824 } /* switch (type) */
1828 while ((tail != NULL) && (tail->next != NULL))
1837 } /* int plugin_notification_meta_add */
1839 int plugin_notification_meta_add_string (notification_t *n,
1843 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1846 int plugin_notification_meta_add_signed_int (notification_t *n,
1850 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1853 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1857 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1860 int plugin_notification_meta_add_double (notification_t *n,
1864 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1867 int plugin_notification_meta_add_boolean (notification_t *n,
1871 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1874 int plugin_notification_meta_copy (notification_t *dst,
1875 const notification_t *src)
1877 notification_meta_t *meta;
1879 assert (dst != NULL);
1880 assert (src != NULL);
1881 assert (dst != src);
1882 assert ((src->meta == NULL) || (src->meta != dst->meta));
1884 for (meta = src->meta; meta != NULL; meta = meta->next)
1886 if (meta->type == NM_TYPE_STRING)
1887 plugin_notification_meta_add_string (dst, meta->name,
1888 meta->nm_value.nm_string);
1889 else if (meta->type == NM_TYPE_SIGNED_INT)
1890 plugin_notification_meta_add_signed_int (dst, meta->name,
1891 meta->nm_value.nm_signed_int);
1892 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1893 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1894 meta->nm_value.nm_unsigned_int);
1895 else if (meta->type == NM_TYPE_DOUBLE)
1896 plugin_notification_meta_add_double (dst, meta->name,
1897 meta->nm_value.nm_double);
1898 else if (meta->type == NM_TYPE_BOOLEAN)
1899 plugin_notification_meta_add_boolean (dst, meta->name,
1900 meta->nm_value.nm_boolean);
1904 } /* int plugin_notification_meta_copy */
1906 int plugin_notification_meta_free (notification_meta_t *n)
1908 notification_meta_t *this;
1909 notification_meta_t *next;
1913 ERROR ("plugin_notification_meta_free: n == NULL!");
1918 while (this != NULL)
1922 if (this->type == NM_TYPE_STRING)
1924 free ((char *)this->nm_value.nm_string);
1925 this->nm_value.nm_string = NULL;
1933 } /* int plugin_notification_meta_free */
1935 /* vim: set sw=8 ts=8 noet fdm=marker : */