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 cdtime_t now = cdtime ();
718 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
720 pthread_mutex_lock (&read_lock);
722 if (read_list == NULL)
724 read_list = llist_create ();
725 if (read_list == NULL)
727 pthread_mutex_unlock (&read_lock);
728 ERROR ("plugin_insert_read: read_list failed.");
733 if (read_heap == NULL)
735 read_heap = c_heap_create (plugin_compare_read_func);
736 if (read_heap == NULL)
738 pthread_mutex_unlock (&read_lock);
739 ERROR ("plugin_insert_read: c_heap_create failed.");
744 le = llist_search (read_list, rf->rf_name);
747 pthread_mutex_unlock (&read_lock);
748 WARNING ("The read function \"%s\" is already registered. "
749 "Check for duplicate \"LoadPlugin\" lines "
750 "in your configuration!",
755 le = llentry_create (rf->rf_name, rf);
758 pthread_mutex_unlock (&read_lock);
759 ERROR ("plugin_insert_read: llentry_create failed.");
763 status = c_heap_insert (read_heap, rf);
766 pthread_mutex_unlock (&read_lock);
767 ERROR ("plugin_insert_read: c_heap_insert failed.");
768 llentry_destroy (le);
772 /* This does not fail. */
773 llist_append (read_list, le);
775 pthread_mutex_unlock (&read_lock);
777 } /* int plugin_insert_read */
779 int plugin_register_read (const char *name,
780 int (*callback) (void))
785 rf = malloc (sizeof (*rf));
788 ERROR ("plugin_register_read: malloc failed.");
792 memset (rf, 0, sizeof (read_func_t));
793 rf->rf_callback = (void *) callback;
794 rf->rf_udata.data = NULL;
795 rf->rf_udata.free_func = NULL;
796 rf->rf_group[0] = '\0';
797 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
798 rf->rf_type = RF_SIMPLE;
799 rf->rf_interval.tv_sec = 0;
800 rf->rf_interval.tv_nsec = 0;
801 rf->rf_effective_interval = rf->rf_interval;
803 status = plugin_insert_read (rf);
808 } /* int plugin_register_read */
810 int plugin_register_complex_read (const char *group, const char *name,
811 plugin_read_cb callback,
812 const struct timespec *interval,
813 user_data_t *user_data)
818 rf = malloc (sizeof (*rf));
821 ERROR ("plugin_register_complex_read: malloc failed.");
825 memset (rf, 0, sizeof (read_func_t));
826 rf->rf_callback = (void *) callback;
828 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
830 rf->rf_group[0] = '\0';
831 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
832 rf->rf_type = RF_COMPLEX;
833 if (interval != NULL)
835 rf->rf_interval = *interval;
837 rf->rf_effective_interval = rf->rf_interval;
840 if (user_data == NULL)
842 rf->rf_udata.data = NULL;
843 rf->rf_udata.free_func = NULL;
847 rf->rf_udata = *user_data;
850 status = plugin_insert_read (rf);
855 } /* int plugin_register_complex_read */
857 int plugin_register_write (const char *name,
858 plugin_write_cb callback, user_data_t *ud)
860 return (create_register_callback (&list_write, name,
861 (void *) callback, ud));
862 } /* int plugin_register_write */
864 int plugin_register_flush (const char *name,
865 plugin_flush_cb callback, user_data_t *ud)
867 return (create_register_callback (&list_flush, name,
868 (void *) callback, ud));
869 } /* int plugin_register_flush */
871 int plugin_register_missing (const char *name,
872 plugin_missing_cb callback, user_data_t *ud)
874 return (create_register_callback (&list_missing, name,
875 (void *) callback, ud));
876 } /* int plugin_register_missing */
878 int plugin_register_shutdown (const char *name,
879 int (*callback) (void))
881 return (create_register_callback (&list_shutdown, name,
882 (void *) callback, /* user_data = */ NULL));
883 } /* int plugin_register_shutdown */
885 int plugin_register_data_set (const data_set_t *ds)
890 if ((data_sets != NULL)
891 && (c_avl_get (data_sets, ds->type, NULL) == 0))
893 NOTICE ("Replacing DS `%s' with another version.", ds->type);
894 plugin_unregister_data_set (ds->type);
896 else if (data_sets == NULL)
898 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
899 if (data_sets == NULL)
903 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
906 memcpy(ds_copy, ds, sizeof (data_set_t));
908 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
910 if (ds_copy->ds == NULL)
916 for (i = 0; i < ds->ds_num; i++)
917 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
919 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
920 } /* int plugin_register_data_set */
922 int plugin_register_log (const char *name,
923 plugin_log_cb callback, user_data_t *ud)
925 return (create_register_callback (&list_log, name,
926 (void *) callback, ud));
927 } /* int plugin_register_log */
929 int plugin_register_notification (const char *name,
930 plugin_notification_cb callback, user_data_t *ud)
932 return (create_register_callback (&list_notification, name,
933 (void *) callback, ud));
934 } /* int plugin_register_log */
936 int plugin_unregister_config (const char *name)
938 cf_unregister (name);
940 } /* int plugin_unregister_config */
942 int plugin_unregister_complex_config (const char *name)
944 cf_unregister_complex (name);
946 } /* int plugin_unregister_complex_config */
948 int plugin_unregister_init (const char *name)
950 return (plugin_unregister (list_init, name));
953 int plugin_unregister_read (const char *name) /* {{{ */
961 pthread_mutex_lock (&read_lock);
963 if (read_list == NULL)
965 pthread_mutex_unlock (&read_lock);
969 le = llist_search (read_list, name);
972 pthread_mutex_unlock (&read_lock);
973 WARNING ("plugin_unregister_read: No such read function: %s",
978 llist_remove (read_list, le);
982 rf->rf_type = RF_REMOVE;
984 pthread_mutex_unlock (&read_lock);
986 llentry_destroy (le);
988 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
991 } /* }}} int plugin_unregister_read */
993 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
995 read_func_t *rf = e->value;
998 return strcmp (rf->rf_group, (const char *)group);
999 } /* }}} int compare_read_func_group */
1001 int plugin_unregister_read_group (const char *group) /* {{{ */
1011 pthread_mutex_lock (&read_lock);
1013 if (read_list == NULL)
1015 pthread_mutex_unlock (&read_lock);
1021 le = llist_search_custom (read_list,
1022 compare_read_func_group, (void *)group);
1029 llist_remove (read_list, le);
1032 assert (rf != NULL);
1033 rf->rf_type = RF_REMOVE;
1035 llentry_destroy (le);
1037 DEBUG ("plugin_unregister_read_group: "
1038 "Marked `%s' (group `%s') for removal.",
1039 rf->rf_name, group);
1042 pthread_mutex_unlock (&read_lock);
1046 WARNING ("plugin_unregister_read_group: No such "
1047 "group of read function: %s", group);
1052 } /* }}} int plugin_unregister_read_group */
1054 int plugin_unregister_write (const char *name)
1056 return (plugin_unregister (list_write, name));
1059 int plugin_unregister_flush (const char *name)
1061 return (plugin_unregister (list_flush, name));
1064 int plugin_unregister_missing (const char *name)
1066 return (plugin_unregister (list_missing, name));
1069 int plugin_unregister_shutdown (const char *name)
1071 return (plugin_unregister (list_shutdown, name));
1074 int plugin_unregister_data_set (const char *name)
1078 if (data_sets == NULL)
1081 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1088 } /* int plugin_unregister_data_set */
1090 int plugin_unregister_log (const char *name)
1092 return (plugin_unregister (list_log, name));
1095 int plugin_unregister_notification (const char *name)
1097 return (plugin_unregister (list_notification, name));
1100 void plugin_init_all (void)
1102 const char *chain_name;
1106 /* Init the value cache */
1109 chain_name = global_option_get ("PreCacheChain");
1110 pre_cache_chain = fc_chain_get_by_name (chain_name);
1112 chain_name = global_option_get ("PostCacheChain");
1113 post_cache_chain = fc_chain_get_by_name (chain_name);
1116 if ((list_init == NULL) && (read_heap == NULL))
1119 /* Calling all init callbacks before checking if read callbacks
1120 * are available allows the init callbacks to register the read
1122 le = llist_head (list_init);
1125 callback_func_t *cf;
1126 plugin_init_cb callback;
1129 callback = cf->cf_callback;
1130 status = (*callback) ();
1134 ERROR ("Initialization of plugin `%s' "
1135 "failed with status %i. "
1136 "Plugin will be unloaded.",
1138 /* Plugins that register read callbacks from the init
1139 * callback should take care of appropriate error
1140 * handling themselves. */
1141 /* FIXME: Unload _all_ functions */
1142 plugin_unregister_read (le->key);
1148 /* Start read-threads */
1149 if (read_heap != NULL)
1153 rt = global_option_get ("ReadThreads");
1156 start_read_threads ((num > 0) ? num : 5);
1158 } /* void plugin_init_all */
1160 /* TODO: Rename this function. */
1161 void plugin_read_all (void)
1163 uc_check_timeout ();
1166 } /* void plugin_read_all */
1168 /* Read function called when the `-T' command line argument is given. */
1169 int plugin_read_all_once (void)
1172 int return_status = 0;
1174 if (read_heap == NULL)
1176 NOTICE ("No read-functions are registered.");
1184 rf = c_heap_get_root (read_heap);
1188 if (rf->rf_type == RF_SIMPLE)
1190 int (*callback) (void);
1192 callback = rf->rf_callback;
1193 status = (*callback) ();
1197 plugin_read_cb callback;
1199 callback = rf->rf_callback;
1200 status = (*callback) (&rf->rf_udata);
1205 NOTICE ("read-function of plugin `%s' failed.",
1210 destroy_callback ((void *) rf);
1213 return (return_status);
1214 } /* int plugin_read_all_once */
1216 int plugin_write (const char *plugin, /* {{{ */
1217 const data_set_t *ds, const value_list_t *vl)
1225 if (list_write == NULL)
1230 ds = plugin_get_ds (vl->type);
1233 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1243 le = llist_head (list_write);
1246 callback_func_t *cf = le->value;
1247 plugin_write_cb callback;
1249 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1250 callback = cf->cf_callback;
1251 status = (*callback) (ds, vl, &cf->cf_udata);
1260 if ((success == 0) && (failure != 0))
1265 else /* plugin != NULL */
1267 callback_func_t *cf;
1268 plugin_write_cb callback;
1270 le = llist_head (list_write);
1273 if (strcasecmp (plugin, le->key) == 0)
1284 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1285 callback = cf->cf_callback;
1286 status = (*callback) (ds, vl, &cf->cf_udata);
1290 } /* }}} int plugin_write */
1292 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1296 if (list_flush == NULL)
1299 le = llist_head (list_flush);
1302 callback_func_t *cf;
1303 plugin_flush_cb callback;
1305 if ((plugin != NULL)
1306 && (strcmp (plugin, le->key) != 0))
1313 callback = cf->cf_callback;
1315 (*callback) (timeout, identifier, &cf->cf_udata);
1320 } /* int plugin_flush */
1322 void plugin_shutdown_all (void)
1326 stop_read_threads ();
1328 destroy_all_callbacks (&list_init);
1330 pthread_mutex_lock (&read_lock);
1331 llist_destroy (read_list);
1333 pthread_mutex_unlock (&read_lock);
1335 destroy_read_heap ();
1337 plugin_flush (/* plugin = */ NULL,
1339 /* identifier = */ NULL);
1342 if (list_shutdown != NULL)
1343 le = llist_head (list_shutdown);
1347 callback_func_t *cf;
1348 plugin_shutdown_cb callback;
1351 callback = cf->cf_callback;
1353 /* Advance the pointer before calling the callback allows
1354 * shutdown functions to unregister themselves. If done the
1355 * other way around the memory `le' points to will be freed
1356 * after callback returns. */
1362 /* Write plugins which use the `user_data' pointer usually need the
1363 * same data available to the flush callback. If this is the case, set
1364 * the free_function to NULL when registering the flush callback and to
1365 * the real free function when registering the write callback. This way
1366 * the data isn't freed twice. */
1367 destroy_all_callbacks (&list_flush);
1368 destroy_all_callbacks (&list_missing);
1369 destroy_all_callbacks (&list_write);
1371 destroy_all_callbacks (&list_notification);
1372 destroy_all_callbacks (&list_shutdown);
1373 destroy_all_callbacks (&list_log);
1374 } /* void plugin_shutdown_all */
1376 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1380 if (list_missing == NULL)
1383 le = llist_head (list_missing);
1386 callback_func_t *cf;
1387 plugin_missing_cb callback;
1391 callback = cf->cf_callback;
1393 status = (*callback) (vl, &cf->cf_udata);
1398 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1399 "failed with status %i.",
1412 } /* int }}} plugin_dispatch_missing */
1414 int plugin_dispatch_values (value_list_t *vl)
1417 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1419 value_t *saved_values;
1420 int saved_values_len;
1424 int free_meta_data = 0;
1426 if ((vl == NULL) || (vl->type[0] == 0)
1427 || (vl->values == NULL) || (vl->values_len < 1))
1429 ERROR ("plugin_dispatch_values: Invalid value list "
1430 "from plugin %s.", vl->plugin);
1434 /* Free meta data only if the calling function didn't specify any. In
1435 * this case matches and targets may add some and the calling function
1436 * may not expect (and therefore free) that data. */
1437 if (vl->meta == NULL)
1440 if (list_write == NULL)
1441 c_complain_once (LOG_WARNING, &no_write_complaint,
1442 "plugin_dispatch_values: No write callback has been "
1443 "registered. Please load at least one output plugin, "
1444 "if you want the collected data to be stored.");
1446 if (data_sets == NULL)
1448 ERROR ("plugin_dispatch_values: No data sets registered. "
1449 "Could the types database be read? Check "
1450 "your `TypesDB' setting!");
1454 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1456 char ident[6 * DATA_MAX_NAME_LEN];
1458 FORMAT_VL (ident, sizeof (ident), vl);
1459 INFO ("plugin_dispatch_values: Dataset not found: %s "
1460 "(from \"%s\"), check your types.db!",
1466 vl->time = cdtime ();
1468 if (vl->interval <= 0)
1469 vl->interval = interval_g;
1471 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1473 "plugin = %s; plugin_instance = %s; "
1474 "type = %s; type_instance = %s;",
1475 CDTIME_T_TO_DOUBLE (vl->time),
1476 CDTIME_T_TO_DOUBLE (vl->interval),
1478 vl->plugin, vl->plugin_instance,
1479 vl->type, vl->type_instance);
1482 assert (0 == strcmp (ds->type, vl->type));
1484 if (0 != strcmp (ds->type, vl->type))
1485 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1486 ds->type, vl->type);
1490 assert (ds->ds_num == vl->values_len);
1492 if (ds->ds_num != vl->values_len)
1494 ERROR ("plugin_dispatch_values: ds->type = %s: "
1495 "(ds->ds_num = %i) != "
1496 "(vl->values_len = %i)",
1497 ds->type, ds->ds_num, vl->values_len);
1502 escape_slashes (vl->host, sizeof (vl->host));
1503 escape_slashes (vl->plugin, sizeof (vl->plugin));
1504 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1505 escape_slashes (vl->type, sizeof (vl->type));
1506 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1508 /* Copy the values. This way, we can assure `targets' that they get
1509 * dynamically allocated values, which they can free and replace if
1511 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1513 saved_values = vl->values;
1514 saved_values_len = vl->values_len;
1516 vl->values = (value_t *) calloc (vl->values_len,
1517 sizeof (*vl->values));
1518 if (vl->values == NULL)
1520 ERROR ("plugin_dispatch_values: calloc failed.");
1521 vl->values = saved_values;
1524 memcpy (vl->values, saved_values,
1525 vl->values_len * sizeof (*vl->values));
1527 else /* if ((pre == NULL) && (post == NULL)) */
1529 saved_values = NULL;
1530 saved_values_len = 0;
1533 if (pre_cache_chain != NULL)
1535 status = fc_process_chain (ds, vl, pre_cache_chain);
1538 WARNING ("plugin_dispatch_values: Running the "
1539 "pre-cache chain failed with "
1543 else if (status == FC_TARGET_STOP)
1545 /* Restore the state of the value_list so that plugins
1546 * don't get confused.. */
1547 if (saved_values != NULL)
1550 vl->values = saved_values;
1551 vl->values_len = saved_values_len;
1557 /* Update the value cache */
1560 if (post_cache_chain != NULL)
1562 status = fc_process_chain (ds, vl, post_cache_chain);
1565 WARNING ("plugin_dispatch_values: Running the "
1566 "post-cache chain failed with "
1572 fc_default_action (ds, vl);
1574 /* Restore the state of the value_list so that plugins don't get
1576 if (saved_values != NULL)
1579 vl->values = saved_values;
1580 vl->values_len = saved_values_len;
1583 if ((free_meta_data != 0) && (vl->meta != NULL))
1585 meta_data_destroy (vl->meta);
1590 } /* int plugin_dispatch_values */
1592 int plugin_dispatch_values_secure (const value_list_t *vl)
1594 value_list_t vl_copy;
1600 memcpy (&vl_copy, vl, sizeof (vl_copy));
1602 /* Write callbacks must not change the values and meta pointers, so we can
1603 * savely skip copying those and make this more efficient. */
1604 if ((pre_cache_chain == NULL) && (post_cache_chain == NULL))
1605 return (plugin_dispatch_values (&vl_copy));
1607 /* Set pointers to NULL, just to be on the save side. */
1608 vl_copy.values = NULL;
1609 vl_copy.meta = NULL;
1611 vl_copy.values = malloc (sizeof (*vl_copy.values) * vl->values_len);
1612 if (vl_copy.values == NULL)
1614 ERROR ("plugin_dispatch_values_secure: malloc failed.");
1617 memcpy (vl_copy.values, vl->values, sizeof (*vl_copy.values) * vl->values_len);
1619 if (vl->meta != NULL)
1621 vl_copy.meta = meta_data_clone (vl->meta);
1622 if (vl_copy.meta == NULL)
1624 ERROR ("plugin_dispatch_values_secure: meta_data_clone failed.");
1625 free (vl_copy.values);
1628 } /* if (vl->meta) */
1630 status = plugin_dispatch_values (&vl_copy);
1632 meta_data_destroy (vl_copy.meta);
1633 free (vl_copy.values);
1636 } /* int plugin_dispatch_values_secure */
1638 int plugin_dispatch_notification (const notification_t *notif)
1641 /* Possible TODO: Add flap detection here */
1643 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1644 "time = %.3f; host = %s;",
1645 notif->severity, notif->message,
1646 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1648 /* Nobody cares for notifications */
1649 if (list_notification == NULL)
1652 le = llist_head (list_notification);
1655 callback_func_t *cf;
1656 plugin_notification_cb callback;
1660 callback = cf->cf_callback;
1661 status = (*callback) (notif, &cf->cf_udata);
1664 WARNING ("plugin_dispatch_notification: Notification "
1665 "callback %s returned %i.",
1673 } /* int plugin_dispatch_notification */
1675 void plugin_log (int level, const char *format, ...)
1682 if (level >= LOG_DEBUG)
1686 va_start (ap, format);
1687 vsnprintf (msg, sizeof (msg), format, ap);
1688 msg[sizeof (msg) - 1] = '\0';
1691 if (list_log == NULL)
1693 fprintf (stderr, "%s\n", msg);
1697 le = llist_head (list_log);
1700 callback_func_t *cf;
1701 plugin_log_cb callback;
1704 callback = cf->cf_callback;
1706 (*callback) (level, msg, &cf->cf_udata);
1710 } /* void plugin_log */
1712 int parse_log_severity (const char *severity)
1716 if ((0 == strcasecmp (severity, "emerg"))
1717 || (0 == strcasecmp (severity, "alert"))
1718 || (0 == strcasecmp (severity, "crit"))
1719 || (0 == strcasecmp (severity, "err")))
1720 log_level = LOG_ERR;
1721 else if (0 == strcasecmp (severity, "warning"))
1722 log_level = LOG_WARNING;
1723 else if (0 == strcasecmp (severity, "notice"))
1724 log_level = LOG_NOTICE;
1725 else if (0 == strcasecmp (severity, "info"))
1726 log_level = LOG_INFO;
1728 else if (0 == strcasecmp (severity, "debug"))
1729 log_level = LOG_DEBUG;
1730 #endif /* COLLECT_DEBUG */
1733 } /* int parse_log_severity */
1735 int parse_notif_severity (const char *severity)
1737 int notif_severity = -1;
1739 if (strcasecmp (severity, "FAILURE") == 0)
1740 notif_severity = NOTIF_FAILURE;
1741 else if (strcmp (severity, "OKAY") == 0)
1742 notif_severity = NOTIF_OKAY;
1743 else if ((strcmp (severity, "WARNING") == 0)
1744 || (strcmp (severity, "WARN") == 0))
1745 notif_severity = NOTIF_WARNING;
1747 return (notif_severity);
1748 } /* int parse_notif_severity */
1750 const data_set_t *plugin_get_ds (const char *name)
1754 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1756 DEBUG ("No such dataset registered: %s", name);
1761 } /* data_set_t *plugin_get_ds */
1763 static int plugin_notification_meta_add (notification_t *n,
1765 enum notification_meta_type_e type,
1768 notification_meta_t *meta;
1769 notification_meta_t *tail;
1771 if ((n == NULL) || (name == NULL) || (value == NULL))
1773 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1777 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1780 ERROR ("plugin_notification_meta_add: malloc failed.");
1783 memset (meta, 0, sizeof (notification_meta_t));
1785 sstrncpy (meta->name, name, sizeof (meta->name));
1790 case NM_TYPE_STRING:
1792 meta->nm_value.nm_string = strdup ((const char *) value);
1793 if (meta->nm_value.nm_string == NULL)
1795 ERROR ("plugin_notification_meta_add: strdup failed.");
1801 case NM_TYPE_SIGNED_INT:
1803 meta->nm_value.nm_signed_int = *((int64_t *) value);
1806 case NM_TYPE_UNSIGNED_INT:
1808 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1811 case NM_TYPE_DOUBLE:
1813 meta->nm_value.nm_double = *((double *) value);
1816 case NM_TYPE_BOOLEAN:
1818 meta->nm_value.nm_boolean = *((_Bool *) value);
1823 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1827 } /* switch (type) */
1831 while ((tail != NULL) && (tail->next != NULL))
1840 } /* int plugin_notification_meta_add */
1842 int plugin_notification_meta_add_string (notification_t *n,
1846 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1849 int plugin_notification_meta_add_signed_int (notification_t *n,
1853 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1856 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1860 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1863 int plugin_notification_meta_add_double (notification_t *n,
1867 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1870 int plugin_notification_meta_add_boolean (notification_t *n,
1874 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1877 int plugin_notification_meta_copy (notification_t *dst,
1878 const notification_t *src)
1880 notification_meta_t *meta;
1882 assert (dst != NULL);
1883 assert (src != NULL);
1884 assert (dst != src);
1885 assert ((src->meta == NULL) || (src->meta != dst->meta));
1887 for (meta = src->meta; meta != NULL; meta = meta->next)
1889 if (meta->type == NM_TYPE_STRING)
1890 plugin_notification_meta_add_string (dst, meta->name,
1891 meta->nm_value.nm_string);
1892 else if (meta->type == NM_TYPE_SIGNED_INT)
1893 plugin_notification_meta_add_signed_int (dst, meta->name,
1894 meta->nm_value.nm_signed_int);
1895 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1896 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1897 meta->nm_value.nm_unsigned_int);
1898 else if (meta->type == NM_TYPE_DOUBLE)
1899 plugin_notification_meta_add_double (dst, meta->name,
1900 meta->nm_value.nm_double);
1901 else if (meta->type == NM_TYPE_BOOLEAN)
1902 plugin_notification_meta_add_boolean (dst, meta->name,
1903 meta->nm_value.nm_boolean);
1907 } /* int plugin_notification_meta_copy */
1909 int plugin_notification_meta_free (notification_meta_t *n)
1911 notification_meta_t *this;
1912 notification_meta_t *next;
1916 ERROR ("plugin_notification_meta_free: n == NULL!");
1921 while (this != NULL)
1925 if (this->type == NM_TYPE_STRING)
1927 free ((char *)this->nm_value.nm_string);
1928 this->nm_value.nm_string = NULL;
1936 } /* int plugin_notification_meta_free */
1938 /* vim: set sw=8 ts=8 noet fdm=marker : */