2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2009 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 verplant.org>
20 * Sebastian Harl <sh at tokkee.org>
24 #include "utils_complain.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_heap.h"
38 #include "utils_cache.h"
39 #include "utils_threshold.h"
40 #include "filter_chain.h"
45 struct callback_func_s
50 typedef struct callback_func_s callback_func_t;
54 #define RF_REMOVE 65535
57 /* `read_func_t' "inherits" from `callback_func_t'.
58 * The `rf_super' member MUST be the first one in this structure! */
59 #define rf_callback rf_super.cf_callback
60 #define rf_udata rf_super.cf_udata
61 callback_func_t rf_super;
62 char rf_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_shutdown;
77 static llist_t *list_log;
78 static llist_t *list_notification;
80 static fc_chain_t *pre_cache_chain = NULL;
81 static fc_chain_t *post_cache_chain = NULL;
83 static c_avl_tree_t *data_sets;
85 static char *plugindir = NULL;
87 static c_heap_t *read_heap = NULL;
88 static llist_t *read_list;
89 static int read_loop = 1;
90 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
91 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
92 static pthread_t *read_threads = NULL;
93 static int read_threads_num = 0;
98 static const char *plugin_get_dir (void)
100 if (plugindir == NULL)
106 static void destroy_callback (callback_func_t *cf) /* {{{ */
111 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
113 cf->cf_udata.free_func (cf->cf_udata.data);
114 cf->cf_udata.data = NULL;
115 cf->cf_udata.free_func = NULL;
118 } /* }}} void destroy_callback */
120 static void destroy_all_callbacks (llist_t **list) /* {{{ */
127 le = llist_head (*list);
135 destroy_callback (le->value);
141 llist_destroy (*list);
143 } /* }}} void destroy_all_callbacks */
145 static void destroy_read_heap (void) /* {{{ */
147 if (read_heap == NULL)
154 cf = c_heap_get_root (read_heap);
158 destroy_callback (cf);
161 c_heap_destroy (read_heap);
163 } /* }}} void destroy_read_heap */
165 static int register_callback (llist_t **list, /* {{{ */
166 const char *name, callback_func_t *cf)
173 *list = llist_create ();
176 ERROR ("plugin: register_callback: "
177 "llist_create failed.");
178 destroy_callback (cf);
186 ERROR ("plugin: register_callback: strdup failed.");
187 destroy_callback (cf);
191 le = llist_search (*list, name);
194 le = llentry_create (key, cf);
197 ERROR ("plugin: register_callback: "
198 "llentry_create failed.");
200 destroy_callback (cf);
204 llist_append (*list, le);
208 callback_func_t *old_cf;
213 WARNING ("plugin: register_callback: "
214 "a callback named `%s' already exists - "
215 "overwriting the old entry!", name);
217 destroy_callback (old_cf);
222 } /* }}} int register_callback */
224 static int create_register_callback (llist_t **list, /* {{{ */
225 const char *name, void *callback, user_data_t *ud)
229 cf = (callback_func_t *) malloc (sizeof (*cf));
232 ERROR ("plugin: create_register_callback: malloc failed.");
235 memset (cf, 0, sizeof (*cf));
237 cf->cf_callback = callback;
240 cf->cf_udata.data = NULL;
241 cf->cf_udata.free_func = NULL;
248 return (register_callback (list, name, cf));
249 } /* }}} int create_register_callback */
251 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
258 e = llist_search (list, name);
262 llist_remove (list, e);
265 destroy_callback (e->value);
270 } /* }}} int plugin_unregister */
273 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
274 * object, but it will bitch about a shared object not having a
275 * ``module_register'' symbol..
277 static int plugin_load_file (char *file, uint32_t flags)
280 void (*reg_handle) (void);
283 lt_dlerror (); /* clear errors */
285 #if LIBTOOL_VERSION == 2
286 if (flags & PLUGIN_FLAGS_GLOBAL) {
288 lt_dladvise_init(&advise);
289 lt_dladvise_global(&advise);
290 dlh = lt_dlopenadvise(file, advise);
291 lt_dladvise_destroy(&advise);
293 dlh = lt_dlopen (file);
295 #else /* if LIBTOOL_VERSION == 1 */
296 if (flags & PLUGIN_FLAGS_GLOBAL)
297 WARNING ("plugin_load_file: The global flag is not supported, "
298 "libtool 2 is required for this.");
299 dlh = lt_dlopen (file);
304 char errbuf[1024] = "";
306 ssnprintf (errbuf, sizeof (errbuf),
307 "lt_dlopen (\"%s\") failed: %s. "
308 "The most common cause for this problem are "
309 "missing dependencies. Use ldd(1) to check "
310 "the dependencies of the plugin "
312 file, lt_dlerror ());
314 ERROR ("%s", errbuf);
315 /* Make sure this is printed to STDERR in any case, but also
316 * make sure it's printed only once. */
317 if (list_log != NULL)
318 fprintf (stderr, "ERROR: %s\n", errbuf);
323 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
325 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
326 file, lt_dlerror ());
336 static _Bool timeout_reached(struct timespec timeout)
339 gettimeofday(&now, NULL);
340 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
343 static void *plugin_read_thread (void __attribute__((unused)) *args)
345 while (read_loop != 0)
353 /* Get the read function that needs to be read next. */
354 rf = c_heap_get_root (read_heap);
357 struct timespec abstime;
359 gettimeofday (&now, /* timezone = */ NULL);
361 abstime.tv_sec = now.tv_sec + interval_g;
362 abstime.tv_nsec = 1000 * now.tv_usec;
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))
373 gettimeofday (&now, /* timezone = */ NULL);
375 rf->rf_interval.tv_sec = interval_g;
376 rf->rf_interval.tv_nsec = 0;
378 rf->rf_effective_interval = rf->rf_interval;
380 rf->rf_next_read.tv_sec = now.tv_sec;
381 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
384 /* sleep until this entry is due,
385 * using pthread_cond_timedwait */
386 pthread_mutex_lock (&read_lock);
387 /* In pthread_cond_timedwait, spurious wakeups are possible
388 * (and really happen, at least on NetBSD with > 1 CPU), thus
389 * we need to re-evaluate the condition every time
390 * pthread_cond_timedwait returns. */
392 while ((read_loop != 0)
393 && !timeout_reached(rf->rf_next_read)
396 rc = pthread_cond_timedwait (&read_cond, &read_lock,
400 /* Must hold `read_lock' when accessing `rf->rf_type'. */
401 rf_type = rf->rf_type;
402 pthread_mutex_unlock (&read_lock);
404 /* Check if we're supposed to stop.. This may have interrupted
408 /* Insert `rf' again, so it can be free'd correctly */
409 c_heap_insert (read_heap, rf);
413 /* The entry has been marked for deletion. The linked list
414 * entry has already been removed by `plugin_unregister_read'.
415 * All we have to do here is free the `read_func_t' and
417 if (rf_type == RF_REMOVE)
419 DEBUG ("plugin_read_thread: Destroying the `%s' "
420 "callback.", rf->rf_name);
421 destroy_callback ((callback_func_t *) rf);
426 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
428 if (rf_type == RF_SIMPLE)
430 int (*callback) (void);
432 callback = rf->rf_callback;
433 status = (*callback) ();
437 plugin_read_cb callback;
439 assert (rf_type == RF_COMPLEX);
441 callback = rf->rf_callback;
442 status = (*callback) (&rf->rf_udata);
445 /* If the function signals failure, we will increase the
446 * intervals in which it will be called. */
449 rf->rf_effective_interval.tv_sec *= 2;
450 rf->rf_effective_interval.tv_nsec *= 2;
451 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
453 if (rf->rf_effective_interval.tv_sec >= 86400)
455 rf->rf_effective_interval.tv_sec = 86400;
456 rf->rf_effective_interval.tv_nsec = 0;
459 NOTICE ("read-function of plugin `%s' failed. "
460 "Will suspend it for %i seconds.",
462 (int) rf->rf_effective_interval.tv_sec);
466 /* Success: Restore the interval, if it was changed. */
467 rf->rf_effective_interval = rf->rf_interval;
470 /* update the ``next read due'' field */
471 gettimeofday (&now, /* timezone = */ NULL);
473 DEBUG ("plugin_read_thread: Effective interval of the "
474 "%s plugin is %i.%09i.",
476 (int) rf->rf_effective_interval.tv_sec,
477 (int) rf->rf_effective_interval.tv_nsec);
479 /* Calculate the next (absolute) time at which this function
480 * should be called. */
481 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
482 + rf->rf_effective_interval.tv_sec;
483 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
484 + rf->rf_effective_interval.tv_nsec;
485 NORMALIZE_TIMESPEC (rf->rf_next_read);
487 /* Check, if `rf_next_read' is in the past. */
488 if ((rf->rf_next_read.tv_sec < now.tv_sec)
489 || ((rf->rf_next_read.tv_sec == now.tv_sec)
490 && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
492 /* `rf_next_read' is in the past. Insert `now'
493 * so this value doesn't trail off into the
495 rf->rf_next_read.tv_sec = now.tv_sec;
496 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
499 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
501 (int) rf->rf_next_read.tv_sec,
502 (int) rf->rf_next_read.tv_nsec);
504 /* Re-insert this read function into the heap again. */
505 c_heap_insert (read_heap, rf);
506 } /* while (read_loop) */
510 } /* void *plugin_read_thread */
512 static void start_read_threads (int num)
516 if (read_threads != NULL)
519 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
520 if (read_threads == NULL)
522 ERROR ("plugin: start_read_threads: calloc failed.");
526 read_threads_num = 0;
527 for (i = 0; i < num; i++)
529 if (pthread_create (read_threads + read_threads_num, NULL,
530 plugin_read_thread, NULL) == 0)
536 ERROR ("plugin: start_read_threads: pthread_create failed.");
540 } /* void start_read_threads */
542 static void stop_read_threads (void)
546 if (read_threads == NULL)
549 INFO ("collectd: Stopping %i read threads.", read_threads_num);
551 pthread_mutex_lock (&read_lock);
553 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
554 pthread_cond_broadcast (&read_cond);
555 pthread_mutex_unlock (&read_lock);
557 for (i = 0; i < read_threads_num; i++)
559 if (pthread_join (read_threads[i], NULL) != 0)
561 ERROR ("plugin: stop_read_threads: pthread_join failed.");
563 read_threads[i] = (pthread_t) 0;
565 sfree (read_threads);
566 read_threads_num = 0;
567 } /* void stop_read_threads */
572 void plugin_set_dir (const char *dir)
574 if (plugindir != NULL)
579 else if ((plugindir = strdup (dir)) == NULL)
582 ERROR ("strdup failed: %s",
583 sstrerror (errno, errbuf, sizeof (errbuf)));
588 int plugin_load (const char *type, uint32_t flags)
592 char filename[BUFSIZE] = "";
593 char typename[BUFSIZE];
600 DEBUG ("type = %s", type);
602 dir = plugin_get_dir ();
605 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
606 * type when matching the filename */
607 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
608 if ((status < 0) || ((size_t) status >= sizeof (typename)))
610 WARNING ("snprintf: truncated: `%s.so'", type);
613 typename_len = strlen (typename);
615 if ((dh = opendir (dir)) == NULL)
618 ERROR ("opendir (%s): %s", dir,
619 sstrerror (errno, errbuf, sizeof (errbuf)));
623 while ((de = readdir (dh)) != NULL)
625 if (strncasecmp (de->d_name, typename, typename_len))
628 status = ssnprintf (filename, sizeof (filename),
629 "%s/%s", dir, de->d_name);
630 if ((status < 0) || ((size_t) status >= sizeof (filename)))
632 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
636 if (lstat (filename, &statbuf) == -1)
639 WARNING ("stat %s: %s", filename,
640 sstrerror (errno, errbuf, sizeof (errbuf)));
643 else if (!S_ISREG (statbuf.st_mode))
645 /* don't follow symlinks */
646 WARNING ("stat %s: not a regular file", filename);
650 if (plugin_load_file (filename, flags) == 0)
658 fprintf (stderr, "Unable to load plugin %s.\n", type);
664 if (filename[0] == '\0')
665 fprintf (stderr, "Could not find plugin %s.\n", type);
671 * The `register_*' functions follow
673 int plugin_register_config (const char *name,
674 int (*callback) (const char *key, const char *val),
675 const char **keys, int keys_num)
677 cf_register (name, callback, keys, keys_num);
679 } /* int plugin_register_config */
681 int plugin_register_complex_config (const char *type,
682 int (*callback) (oconfig_item_t *))
684 return (cf_register_complex (type, callback));
685 } /* int plugin_register_complex_config */
687 int plugin_register_init (const char *name,
688 int (*callback) (void))
690 return (create_register_callback (&list_init, name, (void *) callback,
691 /* user_data = */ NULL));
692 } /* plugin_register_init */
694 static int plugin_compare_read_func (const void *arg0, const void *arg1)
696 const read_func_t *rf0;
697 const read_func_t *rf1;
702 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
704 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
706 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
708 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
712 } /* int plugin_compare_read_func */
714 /* Add a read function to both, the heap and a linked list. The linked list if
715 * used to look-up read functions, especially for the remove function. The heap
716 * is used to determine which plugin to read next. */
717 static int plugin_insert_read (read_func_t *rf)
722 pthread_mutex_lock (&read_lock);
724 if (read_list == NULL)
726 read_list = llist_create ();
727 if (read_list == NULL)
729 pthread_mutex_unlock (&read_lock);
730 ERROR ("plugin_insert_read: read_list failed.");
735 if (read_heap == NULL)
737 read_heap = c_heap_create (plugin_compare_read_func);
738 if (read_heap == NULL)
740 pthread_mutex_unlock (&read_lock);
741 ERROR ("plugin_insert_read: c_heap_create failed.");
746 le = llentry_create (rf->rf_name, rf);
749 pthread_mutex_unlock (&read_lock);
750 ERROR ("plugin_insert_read: llentry_create failed.");
754 status = c_heap_insert (read_heap, rf);
757 pthread_mutex_unlock (&read_lock);
758 ERROR ("plugin_insert_read: c_heap_insert failed.");
759 llentry_destroy (le);
763 /* This does not fail. */
764 llist_append (read_list, le);
766 pthread_mutex_unlock (&read_lock);
768 } /* int plugin_insert_read */
770 int plugin_register_read (const char *name,
771 int (*callback) (void))
775 rf = (read_func_t *) malloc (sizeof (read_func_t));
779 ERROR ("plugin_register_read: malloc failed: %s",
780 sstrerror (errno, errbuf, sizeof (errbuf)));
784 memset (rf, 0, sizeof (read_func_t));
785 rf->rf_callback = (void *) callback;
786 rf->rf_udata.data = NULL;
787 rf->rf_udata.free_func = NULL;
788 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
789 rf->rf_type = RF_SIMPLE;
790 rf->rf_interval.tv_sec = 0;
791 rf->rf_interval.tv_nsec = 0;
792 rf->rf_effective_interval = rf->rf_interval;
794 return (plugin_insert_read (rf));
795 } /* int plugin_register_read */
797 int plugin_register_complex_read (const char *name,
798 plugin_read_cb callback,
799 const struct timespec *interval,
800 user_data_t *user_data)
804 rf = (read_func_t *) malloc (sizeof (read_func_t));
807 ERROR ("plugin_register_complex_read: malloc failed.");
811 memset (rf, 0, sizeof (read_func_t));
812 rf->rf_callback = (void *) callback;
813 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
814 rf->rf_type = RF_COMPLEX;
815 if (interval != NULL)
817 rf->rf_interval = *interval;
819 rf->rf_effective_interval = rf->rf_interval;
822 if (user_data == NULL)
824 rf->rf_udata.data = NULL;
825 rf->rf_udata.free_func = NULL;
829 rf->rf_udata = *user_data;
832 return (plugin_insert_read (rf));
833 } /* int plugin_register_complex_read */
835 int plugin_register_write (const char *name,
836 plugin_write_cb callback, user_data_t *ud)
838 return (create_register_callback (&list_write, name,
839 (void *) callback, ud));
840 } /* int plugin_register_write */
842 int plugin_register_flush (const char *name,
843 plugin_flush_cb callback, user_data_t *ud)
845 return (create_register_callback (&list_flush, name,
846 (void *) callback, ud));
847 } /* int plugin_register_flush */
849 int plugin_register_shutdown (char *name,
850 int (*callback) (void))
852 return (create_register_callback (&list_shutdown, name,
853 (void *) callback, /* user_data = */ NULL));
854 } /* int plugin_register_shutdown */
856 int plugin_register_data_set (const data_set_t *ds)
861 if ((data_sets != NULL)
862 && (c_avl_get (data_sets, ds->type, NULL) == 0))
864 NOTICE ("Replacing DS `%s' with another version.", ds->type);
865 plugin_unregister_data_set (ds->type);
867 else if (data_sets == NULL)
869 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
870 if (data_sets == NULL)
874 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
877 memcpy(ds_copy, ds, sizeof (data_set_t));
879 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
881 if (ds_copy->ds == NULL)
887 for (i = 0; i < ds->ds_num; i++)
888 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
890 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
891 } /* int plugin_register_data_set */
893 int plugin_register_log (const char *name,
894 plugin_log_cb callback, user_data_t *ud)
896 return (create_register_callback (&list_log, name,
897 (void *) callback, ud));
898 } /* int plugin_register_log */
900 int plugin_register_notification (const char *name,
901 plugin_notification_cb callback, user_data_t *ud)
903 return (create_register_callback (&list_notification, name,
904 (void *) callback, ud));
905 } /* int plugin_register_log */
907 int plugin_unregister_config (const char *name)
909 cf_unregister (name);
911 } /* int plugin_unregister_config */
913 int plugin_unregister_complex_config (const char *name)
915 cf_unregister_complex (name);
917 } /* int plugin_unregister_complex_config */
919 int plugin_unregister_init (const char *name)
921 return (plugin_unregister (list_init, name));
924 int plugin_unregister_read (const char *name) /* {{{ */
932 pthread_mutex_lock (&read_lock);
934 if (read_list == NULL)
936 pthread_mutex_unlock (&read_lock);
940 le = llist_search (read_list, name);
943 pthread_mutex_unlock (&read_lock);
944 WARNING ("plugin_unregister_read: No such read function: %s",
949 llist_remove (read_list, le);
953 rf->rf_type = RF_REMOVE;
955 pthread_mutex_unlock (&read_lock);
957 llentry_destroy (le);
959 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
962 } /* }}} int plugin_unregister_read */
964 int plugin_unregister_write (const char *name)
966 return (plugin_unregister (list_write, name));
969 int plugin_unregister_flush (const char *name)
971 return (plugin_unregister (list_flush, name));
974 int plugin_unregister_shutdown (const char *name)
976 return (plugin_unregister (list_shutdown, name));
979 int plugin_unregister_data_set (const char *name)
983 if (data_sets == NULL)
986 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
993 } /* int plugin_unregister_data_set */
995 int plugin_unregister_log (const char *name)
997 return (plugin_unregister (list_log, name));
1000 int plugin_unregister_notification (const char *name)
1002 return (plugin_unregister (list_notification, name));
1005 void plugin_init_all (void)
1007 const char *chain_name;
1011 /* Init the value cache */
1014 chain_name = global_option_get ("PreCacheChain");
1015 pre_cache_chain = fc_chain_get_by_name (chain_name);
1017 chain_name = global_option_get ("PostCacheChain");
1018 post_cache_chain = fc_chain_get_by_name (chain_name);
1021 if ((list_init == NULL) && (read_heap == NULL))
1024 /* Calling all init callbacks before checking if read callbacks
1025 * are available allows the init callbacks to register the read
1027 le = llist_head (list_init);
1030 callback_func_t *cf;
1031 plugin_init_cb callback;
1034 callback = cf->cf_callback;
1035 status = (*callback) ();
1039 ERROR ("Initialization of plugin `%s' "
1040 "failed with status %i. "
1041 "Plugin will be unloaded.",
1043 /* Plugins that register read callbacks from the init
1044 * callback should take care of appropriate error
1045 * handling themselves. */
1046 /* FIXME: Unload _all_ functions */
1047 plugin_unregister_read (le->key);
1053 /* Start read-threads */
1054 if (read_heap != NULL)
1058 rt = global_option_get ("ReadThreads");
1061 start_read_threads ((num > 0) ? num : 5);
1063 } /* void plugin_init_all */
1065 /* TODO: Rename this function. */
1066 void plugin_read_all (void)
1068 uc_check_timeout ();
1071 } /* void plugin_read_all */
1073 /* Read function called when the `-T' command line argument is given. */
1074 int plugin_read_all_once (void)
1077 int return_status = 0;
1079 if (read_heap == NULL)
1081 NOTICE ("No read-functions are registered.");
1089 rf = c_heap_get_root (read_heap);
1093 if (rf->rf_type == RF_SIMPLE)
1095 int (*callback) (void);
1097 callback = rf->rf_callback;
1098 status = (*callback) ();
1102 plugin_read_cb callback;
1104 callback = rf->rf_callback;
1105 status = (*callback) (&rf->rf_udata);
1110 NOTICE ("read-function of plugin `%s' failed.",
1115 destroy_callback ((void *) rf);
1118 return (return_status);
1119 } /* int plugin_read_all_once */
1121 int plugin_write (const char *plugin, /* {{{ */
1122 const data_set_t *ds, const value_list_t *vl)
1130 if (list_write == NULL)
1135 ds = plugin_get_ds (vl->type);
1138 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1148 le = llist_head (list_write);
1151 callback_func_t *cf = le->value;
1152 plugin_write_cb callback;
1154 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1155 callback = cf->cf_callback;
1156 status = (*callback) (ds, vl, &cf->cf_udata);
1165 if ((success == 0) && (failure != 0))
1170 else /* plugin != NULL */
1172 callback_func_t *cf;
1173 plugin_write_cb callback;
1175 le = llist_head (list_write);
1178 if (strcasecmp (plugin, le->key) == 0)
1189 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1190 callback = cf->cf_callback;
1191 status = (*callback) (ds, vl, &cf->cf_udata);
1195 } /* }}} int plugin_write */
1197 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1201 if (list_flush == NULL)
1204 le = llist_head (list_flush);
1207 callback_func_t *cf;
1208 plugin_flush_cb callback;
1210 if ((plugin != NULL)
1211 && (strcmp (plugin, le->key) != 0))
1218 callback = cf->cf_callback;
1220 (*callback) (timeout, identifier, &cf->cf_udata);
1225 } /* int plugin_flush */
1227 void plugin_shutdown_all (void)
1231 stop_read_threads ();
1233 destroy_all_callbacks (&list_init);
1235 pthread_mutex_lock (&read_lock);
1236 llist_destroy (read_list);
1238 pthread_mutex_unlock (&read_lock);
1240 destroy_read_heap ();
1242 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1243 /* identifier = */ NULL);
1246 if (list_shutdown != NULL)
1247 le = llist_head (list_shutdown);
1251 callback_func_t *cf;
1252 plugin_shutdown_cb callback;
1255 callback = cf->cf_callback;
1257 /* Advance the pointer before calling the callback allows
1258 * shutdown functions to unregister themselves. If done the
1259 * other way around the memory `le' points to will be freed
1260 * after callback returns. */
1266 /* Write plugins which use the `user_data' pointer usually need the
1267 * same data available to the flush callback. If this is the case, set
1268 * the free_function to NULL when registering the flush callback and to
1269 * the real free function when registering the write callback. This way
1270 * the data isn't freed twice. */
1271 destroy_all_callbacks (&list_flush);
1272 destroy_all_callbacks (&list_write);
1274 destroy_all_callbacks (&list_notification);
1275 destroy_all_callbacks (&list_shutdown);
1276 destroy_all_callbacks (&list_log);
1277 } /* void plugin_shutdown_all */
1279 int plugin_dispatch_values (value_list_t *vl)
1282 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1284 value_t *saved_values;
1285 int saved_values_len;
1289 int free_meta_data = 0;
1291 if ((vl == NULL) || (vl->type[0] == 0)
1292 || (vl->values == NULL) || (vl->values_len < 1))
1294 ERROR ("plugin_dispatch_values: Invalid value list.");
1298 /* Free meta data only if the calling function didn't specify any. In
1299 * this case matches and targets may add some and the calling function
1300 * may not expect (and therefore free) that data. */
1301 if (vl->meta == NULL)
1304 if (list_write == NULL)
1305 c_complain_once (LOG_WARNING, &no_write_complaint,
1306 "plugin_dispatch_values: No write callback has been "
1307 "registered. Please load at least one output plugin, "
1308 "if you want the collected data to be stored.");
1310 if (data_sets == NULL)
1312 ERROR ("plugin_dispatch_values: No data sets registered. "
1313 "Could the types database be read? Check "
1314 "your `TypesDB' setting!");
1318 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1320 char ident[6 * DATA_MAX_NAME_LEN];
1322 FORMAT_VL (ident, sizeof (ident), vl);
1323 INFO ("plugin_dispatch_values: Dataset not found: %s "
1324 "(from \"%s\"), check your types.db!",
1330 vl->time = time (NULL);
1332 if (vl->interval <= 0)
1333 vl->interval = interval_g;
1335 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1337 "plugin = %s; plugin_instance = %s; "
1338 "type = %s; type_instance = %s;",
1339 (unsigned int) vl->time, vl->interval,
1341 vl->plugin, vl->plugin_instance,
1342 vl->type, vl->type_instance);
1345 assert (0 == strcmp (ds->type, vl->type));
1347 if (0 != strcmp (ds->type, vl->type))
1348 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1349 ds->type, vl->type);
1353 assert (ds->ds_num == vl->values_len);
1355 if (ds->ds_num != vl->values_len)
1357 ERROR ("plugin_dispatch_values: ds->type = %s: "
1358 "(ds->ds_num = %i) != "
1359 "(vl->values_len = %i)",
1360 ds->type, ds->ds_num, vl->values_len);
1365 escape_slashes (vl->host, sizeof (vl->host));
1366 escape_slashes (vl->plugin, sizeof (vl->plugin));
1367 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1368 escape_slashes (vl->type, sizeof (vl->type));
1369 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1371 /* Copy the values. This way, we can assure `targets' that they get
1372 * dynamically allocated values, which they can free and replace if
1374 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1376 saved_values = vl->values;
1377 saved_values_len = vl->values_len;
1379 vl->values = (value_t *) calloc (vl->values_len,
1380 sizeof (*vl->values));
1381 if (vl->values == NULL)
1383 ERROR ("plugin_dispatch_values: calloc failed.");
1384 vl->values = saved_values;
1387 memcpy (vl->values, saved_values,
1388 vl->values_len * sizeof (*vl->values));
1390 else /* if ((pre == NULL) && (post == NULL)) */
1392 saved_values = NULL;
1393 saved_values_len = 0;
1396 if (pre_cache_chain != NULL)
1398 status = fc_process_chain (ds, vl, pre_cache_chain);
1401 WARNING ("plugin_dispatch_values: Running the "
1402 "pre-cache chain failed with "
1406 else if (status == FC_TARGET_STOP)
1408 /* Restore the state of the value_list so that plugins
1409 * don't get confused.. */
1410 if (saved_values != NULL)
1413 vl->values = saved_values;
1414 vl->values_len = saved_values_len;
1420 /* Update the value cache */
1423 /* Initiate threshold checking */
1424 ut_check_threshold (ds, vl);
1426 if (post_cache_chain != NULL)
1428 status = fc_process_chain (ds, vl, post_cache_chain);
1431 WARNING ("plugin_dispatch_values: Running the "
1432 "post-cache chain failed with "
1438 fc_default_action (ds, vl);
1440 /* Restore the state of the value_list so that plugins don't get
1442 if (saved_values != NULL)
1445 vl->values = saved_values;
1446 vl->values_len = saved_values_len;
1449 if ((free_meta_data != 0) && (vl->meta != NULL))
1451 meta_data_destroy (vl->meta);
1456 } /* int plugin_dispatch_values */
1458 int plugin_dispatch_notification (const notification_t *notif)
1461 /* Possible TODO: Add flap detection here */
1463 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1464 "time = %u; host = %s;",
1465 notif->severity, notif->message,
1466 (unsigned int) notif->time, notif->host);
1468 /* Nobody cares for notifications */
1469 if (list_notification == NULL)
1472 le = llist_head (list_notification);
1475 callback_func_t *cf;
1476 plugin_notification_cb callback;
1480 callback = cf->cf_callback;
1481 status = (*callback) (notif, &cf->cf_udata);
1484 WARNING ("plugin_dispatch_notification: Notification "
1485 "callback %s returned %i.",
1493 } /* int plugin_dispatch_notification */
1495 void plugin_log (int level, const char *format, ...)
1502 if (level >= LOG_DEBUG)
1506 va_start (ap, format);
1507 vsnprintf (msg, sizeof (msg), format, ap);
1508 msg[sizeof (msg) - 1] = '\0';
1511 if (list_log == NULL)
1513 fprintf (stderr, "%s\n", msg);
1517 le = llist_head (list_log);
1520 callback_func_t *cf;
1521 plugin_log_cb callback;
1524 callback = cf->cf_callback;
1526 (*callback) (level, msg, &cf->cf_udata);
1530 } /* void plugin_log */
1532 const data_set_t *plugin_get_ds (const char *name)
1536 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1538 DEBUG ("No such dataset registered: %s", name);
1543 } /* data_set_t *plugin_get_ds */
1545 static int plugin_notification_meta_add (notification_t *n,
1547 enum notification_meta_type_e type,
1550 notification_meta_t *meta;
1551 notification_meta_t *tail;
1553 if ((n == NULL) || (name == NULL) || (value == NULL))
1555 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1559 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1562 ERROR ("plugin_notification_meta_add: malloc failed.");
1565 memset (meta, 0, sizeof (notification_meta_t));
1567 sstrncpy (meta->name, name, sizeof (meta->name));
1572 case NM_TYPE_STRING:
1574 meta->nm_value.nm_string = strdup ((const char *) value);
1575 if (meta->nm_value.nm_string == NULL)
1577 ERROR ("plugin_notification_meta_add: strdup failed.");
1583 case NM_TYPE_SIGNED_INT:
1585 meta->nm_value.nm_signed_int = *((int64_t *) value);
1588 case NM_TYPE_UNSIGNED_INT:
1590 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1593 case NM_TYPE_DOUBLE:
1595 meta->nm_value.nm_double = *((double *) value);
1598 case NM_TYPE_BOOLEAN:
1600 meta->nm_value.nm_boolean = *((bool *) value);
1605 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1609 } /* switch (type) */
1613 while ((tail != NULL) && (tail->next != NULL))
1622 } /* int plugin_notification_meta_add */
1624 int plugin_notification_meta_add_string (notification_t *n,
1628 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1631 int plugin_notification_meta_add_signed_int (notification_t *n,
1635 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1638 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1642 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1645 int plugin_notification_meta_add_double (notification_t *n,
1649 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1652 int plugin_notification_meta_add_boolean (notification_t *n,
1656 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1659 int plugin_notification_meta_copy (notification_t *dst,
1660 const notification_t *src)
1662 notification_meta_t *meta;
1664 assert (dst != NULL);
1665 assert (src != NULL);
1666 assert (dst != src);
1667 assert ((src->meta == NULL) || (src->meta != dst->meta));
1669 for (meta = src->meta; meta != NULL; meta = meta->next)
1671 if (meta->type == NM_TYPE_STRING)
1672 plugin_notification_meta_add_string (dst, meta->name,
1673 meta->nm_value.nm_string);
1674 else if (meta->type == NM_TYPE_SIGNED_INT)
1675 plugin_notification_meta_add_signed_int (dst, meta->name,
1676 meta->nm_value.nm_signed_int);
1677 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1678 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1679 meta->nm_value.nm_unsigned_int);
1680 else if (meta->type == NM_TYPE_DOUBLE)
1681 plugin_notification_meta_add_double (dst, meta->name,
1682 meta->nm_value.nm_double);
1683 else if (meta->type == NM_TYPE_BOOLEAN)
1684 plugin_notification_meta_add_boolean (dst, meta->name,
1685 meta->nm_value.nm_boolean);
1689 } /* int plugin_notification_meta_copy */
1691 int plugin_notification_meta_free (notification_meta_t *n)
1693 notification_meta_t *this;
1694 notification_meta_t *next;
1698 ERROR ("plugin_notification_meta_free: n == NULL!");
1703 while (this != NULL)
1707 if (this->type == NM_TYPE_STRING)
1709 free ((char *)this->nm_value.nm_string);
1710 this->nm_value.nm_string = NULL;
1718 } /* int plugin_notification_meta_free */
1720 /* vim: set sw=8 ts=8 noet fdm=marker : */