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_head_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: create_register_callback: "
177 "llist_create failed.");
178 destroy_callback (cf);
186 ERROR ("plugin: create_register_callback: strdup failed.");
187 destroy_callback (cf);
191 le = llist_search (*list, name);
194 le = llentry_create (key, cf);
197 ERROR ("plugin: create_register_callback: "
198 "llentry_create failed.");
200 destroy_callback (cf);
204 llist_append (*list, le);
208 callback_func_t *old_cf;
213 destroy_callback (old_cf);
218 } /* }}} int register_callback */
220 static int create_register_callback (llist_t **list, /* {{{ */
221 const char *name, void *callback, user_data_t *ud)
225 cf = (callback_func_t *) malloc (sizeof (*cf));
228 ERROR ("plugin: create_register_callback: malloc failed.");
231 memset (cf, 0, sizeof (*cf));
233 cf->cf_callback = callback;
236 cf->cf_udata.data = NULL;
237 cf->cf_udata.free_func = NULL;
244 return (register_callback (list, name, cf));
245 } /* }}} int create_register_callback */
247 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
254 e = llist_search (list, name);
258 llist_remove (list, e);
261 destroy_callback (e->value);
266 } /* }}} int plugin_unregister */
269 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
270 * object, but it will bitch about a shared object not having a
271 * ``module_register'' symbol..
273 static int plugin_load_file (char *file, uint32_t flags)
276 void (*reg_handle) (void);
278 DEBUG ("file = %s", file);
281 lt_dlerror (); /* clear errors */
283 #if LIBTOOL_VERSION == 2
284 if (flags & PLUGIN_FLAGS_GLOBAL) {
286 lt_dladvise_init(&advise);
287 lt_dladvise_global(&advise);
288 dlh = lt_dlopenadvise(file, advise);
289 lt_dladvise_destroy(&advise);
291 dlh = lt_dlopen (file);
293 #else /* if LIBTOOL_VERSION == 1 */
294 if (flags & PLUGIN_FLAGS_GLOBAL)
295 ERROR ("plugin_load_file: The global flag is not supported, "
296 "libtool 2 is required for this.");
297 dlh = lt_dlopen (file);
302 const char *error = lt_dlerror ();
304 ERROR ("lt_dlopen (%s) failed: %s", file, error);
305 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
309 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
311 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
312 file, lt_dlerror ());
322 static void *plugin_read_thread (void __attribute__((unused)) *args)
324 while (read_loop != 0)
331 /* Get the read function that needs to be read next. */
332 rf = c_head_get_root (read_heap);
335 struct timespec abstime;
337 gettimeofday (&now, /* timezone = */ NULL);
339 abstime.tv_sec = now.tv_sec + interval_g;
340 abstime.tv_nsec = 1000 * now.tv_usec;
342 pthread_mutex_lock (&read_lock);
343 pthread_cond_timedwait (&read_cond, &read_lock,
345 pthread_mutex_unlock (&read_lock);
349 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
351 gettimeofday (&now, /* timezone = */ NULL);
353 rf->rf_interval.tv_sec = interval_g;
354 rf->rf_interval.tv_nsec = 0;
356 rf->rf_effective_interval = rf->rf_interval;
358 rf->rf_next_read.tv_sec = now.tv_sec;
359 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
362 /* sleep until this entry is due,
363 * using pthread_cond_timedwait */
364 pthread_mutex_lock (&read_lock);
365 pthread_cond_timedwait (&read_cond, &read_lock,
367 /* Must hold `real_lock' when accessing `rf->rf_type'. */
368 rf_type = rf->rf_type;
369 pthread_mutex_unlock (&read_lock);
371 /* Check if we're supposed to stop.. This may have interrupted
375 /* Insert `rf' again, so it can be free'd correctly */
376 c_heap_insert (read_heap, rf);
380 /* The entry has been marked for deletion. The linked list
381 * entry has already been removed by `plugin_unregister_read'.
382 * All we have to do here is free the `read_func_t' and
384 if (rf_type == RF_REMOVE)
386 DEBUG ("plugin_read_thread: Destroying the `%s' "
387 "callback.", rf->rf_name);
388 destroy_callback ((callback_func_t *) rf);
393 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
395 if (rf_type == RF_SIMPLE)
397 int (*callback) (void);
399 callback = rf->rf_callback;
400 status = (*callback) ();
404 plugin_read_cb callback;
406 assert (rf_type == RF_COMPLEX);
408 callback = rf->rf_callback;
409 status = (*callback) (&rf->rf_udata);
412 /* If the function signals failure, we will increase the
413 * intervals in which it will be called. */
416 rf->rf_effective_interval.tv_sec *= 2;
417 rf->rf_effective_interval.tv_nsec *= 2;
418 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
420 if (rf->rf_effective_interval.tv_sec >= 86400)
422 rf->rf_effective_interval.tv_sec = 86400;
423 rf->rf_effective_interval.tv_nsec = 0;
426 NOTICE ("read-function of plugin `%s' failed. "
427 "Will suspend it for %i seconds.",
429 (int) rf->rf_effective_interval.tv_sec);
433 /* Success: Restore the interval, if it was changed. */
434 rf->rf_effective_interval = rf->rf_interval;
437 /* update the ``next read due'' field */
438 gettimeofday (&now, /* timezone = */ NULL);
440 DEBUG ("plugin_read_thread: Effective interval of the "
441 "%s plugin is %i.%09i.",
443 (int) rf->rf_effective_interval.tv_sec,
444 (int) rf->rf_effective_interval.tv_nsec);
446 /* Calculate the next (absolute) time at which this function
447 * should be called. */
448 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
449 + rf->rf_effective_interval.tv_sec;
450 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
451 + rf->rf_effective_interval.tv_nsec;
452 NORMALIZE_TIMESPEC (rf->rf_next_read);
454 /* Check, if `rf_next_read' is in the past. */
455 if ((rf->rf_next_read.tv_sec < now.tv_sec)
456 || ((rf->rf_next_read.tv_sec == now.tv_sec)
457 && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
459 /* `rf_next_read' is in the past. Insert `now'
460 * so this value doesn't trail off into the
462 rf->rf_next_read.tv_sec = now.tv_sec;
463 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
466 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
468 (int) rf->rf_next_read.tv_sec,
469 (int) rf->rf_next_read.tv_nsec);
471 /* Re-insert this read function into the heap again. */
472 c_heap_insert (read_heap, rf);
473 } /* while (read_loop) */
477 } /* void *plugin_read_thread */
479 static void start_read_threads (int num)
483 if (read_threads != NULL)
486 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
487 if (read_threads == NULL)
489 ERROR ("plugin: start_read_threads: calloc failed.");
493 read_threads_num = 0;
494 for (i = 0; i < num; i++)
496 if (pthread_create (read_threads + read_threads_num, NULL,
497 plugin_read_thread, NULL) == 0)
503 ERROR ("plugin: start_read_threads: pthread_create failed.");
507 } /* void start_read_threads */
509 static void stop_read_threads (void)
513 if (read_threads == NULL)
516 INFO ("collectd: Stopping %i read threads.", read_threads_num);
518 pthread_mutex_lock (&read_lock);
520 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
521 pthread_cond_broadcast (&read_cond);
522 pthread_mutex_unlock (&read_lock);
524 for (i = 0; i < read_threads_num; i++)
526 if (pthread_join (read_threads[i], NULL) != 0)
528 ERROR ("plugin: stop_read_threads: pthread_join failed.");
530 read_threads[i] = (pthread_t) 0;
532 sfree (read_threads);
533 read_threads_num = 0;
534 } /* void stop_read_threads */
539 void plugin_set_dir (const char *dir)
541 if (plugindir != NULL)
546 else if ((plugindir = strdup (dir)) == NULL)
549 ERROR ("strdup failed: %s",
550 sstrerror (errno, errbuf, sizeof (errbuf)));
555 int plugin_load (const char *type, uint32_t flags)
559 char filename[BUFSIZE] = "";
560 char typename[BUFSIZE];
567 DEBUG ("type = %s", type);
569 dir = plugin_get_dir ();
572 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
573 * type when matching the filename */
574 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
575 if ((status < 0) || ((size_t) status >= sizeof (typename)))
577 WARNING ("snprintf: truncated: `%s.so'", type);
580 typename_len = strlen (typename);
582 if ((dh = opendir (dir)) == NULL)
585 ERROR ("opendir (%s): %s", dir,
586 sstrerror (errno, errbuf, sizeof (errbuf)));
590 while ((de = readdir (dh)) != NULL)
592 if (strncasecmp (de->d_name, typename, typename_len))
595 status = ssnprintf (filename, sizeof (filename),
596 "%s/%s", dir, de->d_name);
597 if ((status < 0) || ((size_t) status >= sizeof (filename)))
599 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
603 if (lstat (filename, &statbuf) == -1)
606 WARNING ("stat %s: %s", filename,
607 sstrerror (errno, errbuf, sizeof (errbuf)));
610 else if (!S_ISREG (statbuf.st_mode))
612 /* don't follow symlinks */
613 WARNING ("stat %s: not a regular file", filename);
617 if (plugin_load_file (filename, flags) == 0)
625 fprintf (stderr, "Unable to load plugin %s.\n", type);
631 if (filename[0] == '\0')
632 fprintf (stderr, "Could not find plugin %s.\n", type);
638 * The `register_*' functions follow
640 int plugin_register_config (const char *name,
641 int (*callback) (const char *key, const char *val),
642 const char **keys, int keys_num)
644 cf_register (name, callback, keys, keys_num);
646 } /* int plugin_register_config */
648 int plugin_register_complex_config (const char *type,
649 int (*callback) (oconfig_item_t *))
651 return (cf_register_complex (type, callback));
652 } /* int plugin_register_complex_config */
654 int plugin_register_init (const char *name,
655 int (*callback) (void))
657 return (create_register_callback (&list_init, name, (void *) callback,
658 /* user_data = */ NULL));
659 } /* plugin_register_init */
661 static int plugin_compare_read_func (const void *arg0, const void *arg1)
663 const read_func_t *rf0;
664 const read_func_t *rf1;
669 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
671 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
673 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
675 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
679 } /* int plugin_compare_read_func */
681 /* Add a read function to both, the heap and a linked list. The linked list if
682 * used to look-up read functions, especially for the remove function. The heap
683 * is used to determine which plugin to read next. */
684 static int plugin_insert_read (read_func_t *rf)
689 pthread_mutex_lock (&read_lock);
691 if (read_list == NULL)
693 read_list = llist_create ();
694 if (read_list == NULL)
696 pthread_mutex_unlock (&read_lock);
697 ERROR ("plugin_insert_read: read_list failed.");
702 if (read_heap == NULL)
704 read_heap = c_heap_create (plugin_compare_read_func);
705 if (read_heap == NULL)
707 pthread_mutex_unlock (&read_lock);
708 ERROR ("plugin_insert_read: c_heap_create failed.");
713 le = llentry_create (rf->rf_name, rf);
716 pthread_mutex_unlock (&read_lock);
717 ERROR ("plugin_insert_read: llentry_create failed.");
721 status = c_heap_insert (read_heap, rf);
724 pthread_mutex_unlock (&read_lock);
725 ERROR ("plugin_insert_read: c_heap_insert failed.");
726 llentry_destroy (le);
730 /* This does not fail. */
731 llist_append (read_list, le);
733 pthread_mutex_unlock (&read_lock);
735 } /* int plugin_insert_read */
737 int plugin_register_read (const char *name,
738 int (*callback) (void))
742 rf = (read_func_t *) malloc (sizeof (read_func_t));
746 ERROR ("plugin_register_read: malloc failed: %s",
747 sstrerror (errno, errbuf, sizeof (errbuf)));
751 memset (rf, 0, sizeof (read_func_t));
752 rf->rf_callback = (void *) callback;
753 rf->rf_udata.data = NULL;
754 rf->rf_udata.free_func = NULL;
755 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
756 rf->rf_type = RF_SIMPLE;
757 rf->rf_interval.tv_sec = 0;
758 rf->rf_interval.tv_nsec = 0;
759 rf->rf_effective_interval = rf->rf_interval;
761 return (plugin_insert_read (rf));
762 } /* int plugin_register_read */
764 int plugin_register_complex_read (const char *name,
765 plugin_read_cb callback,
766 const struct timespec *interval,
767 user_data_t *user_data)
771 rf = (read_func_t *) malloc (sizeof (read_func_t));
774 ERROR ("plugin_register_complex_read: malloc failed.");
778 memset (rf, 0, sizeof (read_func_t));
779 rf->rf_callback = (void *) callback;
780 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
781 rf->rf_type = RF_COMPLEX;
782 if (interval != NULL)
784 rf->rf_interval = *interval;
786 rf->rf_effective_interval = rf->rf_interval;
789 if (user_data == NULL)
791 rf->rf_udata.data = NULL;
792 rf->rf_udata.free_func = NULL;
796 rf->rf_udata = *user_data;
799 return (plugin_insert_read (rf));
800 } /* int plugin_register_complex_read */
802 int plugin_register_write (const char *name,
803 plugin_write_cb callback, user_data_t *ud)
805 return (create_register_callback (&list_write, name,
806 (void *) callback, ud));
807 } /* int plugin_register_write */
809 int plugin_register_flush (const char *name,
810 plugin_flush_cb callback, user_data_t *ud)
812 return (create_register_callback (&list_flush, name,
813 (void *) callback, ud));
814 } /* int plugin_register_flush */
816 int plugin_register_shutdown (char *name,
817 int (*callback) (void))
819 return (create_register_callback (&list_shutdown, name,
820 (void *) callback, /* user_data = */ NULL));
821 } /* int plugin_register_shutdown */
823 int plugin_register_data_set (const data_set_t *ds)
828 if ((data_sets != NULL)
829 && (c_avl_get (data_sets, ds->type, NULL) == 0))
831 NOTICE ("Replacing DS `%s' with another version.", ds->type);
832 plugin_unregister_data_set (ds->type);
834 else if (data_sets == NULL)
836 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
837 if (data_sets == NULL)
841 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
844 memcpy(ds_copy, ds, sizeof (data_set_t));
846 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
848 if (ds_copy->ds == NULL)
854 for (i = 0; i < ds->ds_num; i++)
855 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
857 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
858 } /* int plugin_register_data_set */
860 int plugin_register_log (const char *name,
861 plugin_log_cb callback, user_data_t *ud)
863 return (create_register_callback (&list_log, name,
864 (void *) callback, ud));
865 } /* int plugin_register_log */
867 int plugin_register_notification (const char *name,
868 plugin_notification_cb callback, user_data_t *ud)
870 return (create_register_callback (&list_notification, name,
871 (void *) callback, ud));
872 } /* int plugin_register_log */
874 int plugin_unregister_config (const char *name)
876 cf_unregister (name);
878 } /* int plugin_unregister_config */
880 int plugin_unregister_complex_config (const char *name)
882 cf_unregister_complex (name);
884 } /* int plugin_unregister_complex_config */
886 int plugin_unregister_init (const char *name)
888 return (plugin_unregister (list_init, name));
891 int plugin_unregister_read (const char *name) /* {{{ */
899 pthread_mutex_lock (&read_lock);
901 if (read_list == NULL)
903 pthread_mutex_unlock (&read_lock);
907 le = llist_search (read_list, name);
910 pthread_mutex_unlock (&read_lock);
911 WARNING ("plugin_unregister_read: No such read function: %s",
916 llist_remove (read_list, le);
920 rf->rf_type = RF_REMOVE;
922 pthread_mutex_unlock (&read_lock);
924 llentry_destroy (le);
926 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
929 } /* }}} int plugin_unregister_read */
931 int plugin_unregister_write (const char *name)
933 return (plugin_unregister (list_write, name));
936 int plugin_unregister_flush (const char *name)
938 return (plugin_unregister (list_flush, name));
941 int plugin_unregister_shutdown (const char *name)
943 return (plugin_unregister (list_shutdown, name));
946 int plugin_unregister_data_set (const char *name)
950 if (data_sets == NULL)
953 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
960 } /* int plugin_unregister_data_set */
962 int plugin_unregister_log (const char *name)
964 return (plugin_unregister (list_log, name));
967 int plugin_unregister_notification (const char *name)
969 return (plugin_unregister (list_notification, name));
972 void plugin_init_all (void)
974 const char *chain_name;
978 /* Init the value cache */
981 chain_name = global_option_get ("PreCacheChain");
982 pre_cache_chain = fc_chain_get_by_name (chain_name);
984 chain_name = global_option_get ("PostCacheChain");
985 post_cache_chain = fc_chain_get_by_name (chain_name);
988 if ((list_init == NULL) && (read_heap == NULL))
991 /* Calling all init callbacks before checking if read callbacks
992 * are available allows the init callbacks to register the read
994 le = llist_head (list_init);
998 plugin_init_cb callback;
1001 callback = cf->cf_callback;
1002 status = (*callback) ();
1006 ERROR ("Initialization of plugin `%s' "
1007 "failed with status %i. "
1008 "Plugin will be unloaded.",
1010 /* Plugins that register read callbacks from the init
1011 * callback should take care of appropriate error
1012 * handling themselves. */
1013 /* FIXME: Unload _all_ functions */
1014 plugin_unregister_read (le->key);
1020 /* Start read-threads */
1021 if (read_heap != NULL)
1025 rt = global_option_get ("ReadThreads");
1028 start_read_threads ((num > 0) ? num : 5);
1030 } /* void plugin_init_all */
1032 /* TODO: Rename this function. */
1033 void plugin_read_all (void)
1035 uc_check_timeout ();
1038 } /* void plugin_read_all */
1040 /* Read function called when the `-T' command line argument is given. */
1041 int plugin_read_all_once (void)
1044 int return_status = 0;
1046 if (read_heap == NULL)
1048 NOTICE ("No read-functions are registered.");
1056 rf = c_head_get_root (read_heap);
1060 if (rf->rf_type == RF_SIMPLE)
1062 int (*callback) (void);
1064 callback = rf->rf_callback;
1065 status = (*callback) ();
1069 plugin_read_cb callback;
1071 callback = rf->rf_callback;
1072 status = (*callback) (&rf->rf_udata);
1077 NOTICE ("read-function of plugin `%s' failed.",
1082 destroy_callback ((void *) rf);
1085 return (return_status);
1086 } /* int plugin_read_all_once */
1088 int plugin_write (const char *plugin, /* {{{ */
1089 const data_set_t *ds, const value_list_t *vl)
1097 if (list_write == NULL)
1102 ds = plugin_get_ds (vl->type);
1105 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1115 le = llist_head (list_write);
1118 callback_func_t *cf = le->value;
1119 plugin_write_cb callback;
1121 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1122 callback = cf->cf_callback;
1123 status = (*callback) (ds, vl, &cf->cf_udata);
1132 if ((success == 0) && (failure != 0))
1137 else /* plugin != NULL */
1139 callback_func_t *cf;
1140 plugin_write_cb callback;
1142 le = llist_head (list_write);
1145 if (strcasecmp (plugin, le->key) == 0)
1156 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1157 callback = cf->cf_callback;
1158 status = (*callback) (ds, vl, &cf->cf_udata);
1162 } /* }}} int plugin_write */
1164 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1168 if (list_flush == NULL)
1171 le = llist_head (list_flush);
1174 callback_func_t *cf;
1175 plugin_flush_cb callback;
1177 if ((plugin != NULL)
1178 && (strcmp (plugin, le->key) != 0))
1185 callback = cf->cf_callback;
1187 (*callback) (timeout, identifier, &cf->cf_udata);
1192 } /* int plugin_flush */
1194 void plugin_shutdown_all (void)
1198 stop_read_threads ();
1200 destroy_all_callbacks (&list_init);
1202 pthread_mutex_lock (&read_lock);
1203 llist_destroy (read_list);
1205 pthread_mutex_unlock (&read_lock);
1207 destroy_read_heap ();
1209 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1210 /* identifier = */ NULL);
1213 if (list_shutdown != NULL)
1214 le = llist_head (list_shutdown);
1218 callback_func_t *cf;
1219 plugin_shutdown_cb callback;
1222 callback = cf->cf_callback;
1224 /* Advance the pointer before calling the callback allows
1225 * shutdown functions to unregister themselves. If done the
1226 * other way around the memory `le' points to will be freed
1227 * after callback returns. */
1233 /* Write plugins which use the `user_data' pointer usually need the
1234 * same data available to the flush callback. If this is the case, set
1235 * the free_function to NULL when registering the flush callback and to
1236 * the real free function when registering the write callback. This way
1237 * the data isn't freed twice. */
1238 destroy_all_callbacks (&list_flush);
1239 destroy_all_callbacks (&list_write);
1241 destroy_all_callbacks (&list_notification);
1242 destroy_all_callbacks (&list_shutdown);
1243 destroy_all_callbacks (&list_log);
1244 } /* void plugin_shutdown_all */
1246 int plugin_dispatch_values (value_list_t *vl)
1249 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1251 value_t *saved_values;
1252 int saved_values_len;
1256 int free_meta_data = 0;
1258 if ((vl == NULL) || (vl->type[0] == 0)
1259 || (vl->values == NULL) || (vl->values_len < 1))
1261 ERROR ("plugin_dispatch_values: Invalid value list.");
1265 /* Free meta data only if the calling function didn't specify any. In
1266 * this case matches and targets may add some and the calling function
1267 * may not expect (and therefore free) that data. */
1268 if (vl->meta == NULL)
1271 if (list_write == NULL)
1272 c_complain_once (LOG_WARNING, &no_write_complaint,
1273 "plugin_dispatch_values: No write callback has been "
1274 "registered. Please load at least one output plugin, "
1275 "if you want the collected data to be stored.");
1277 if (data_sets == NULL)
1279 ERROR ("plugin_dispatch_values: No data sets registered. "
1280 "Could the types database be read? Check "
1281 "your `TypesDB' setting!");
1285 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1287 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1292 vl->time = time (NULL);
1294 if (vl->interval <= 0)
1295 vl->interval = interval_g;
1297 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1299 "plugin = %s; plugin_instance = %s; "
1300 "type = %s; type_instance = %s;",
1301 (unsigned int) vl->time, vl->interval,
1303 vl->plugin, vl->plugin_instance,
1304 vl->type, vl->type_instance);
1307 assert (0 == strcmp (ds->type, vl->type));
1309 if (0 != strcmp (ds->type, vl->type))
1310 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1311 ds->type, vl->type);
1315 assert (ds->ds_num == vl->values_len);
1317 if (ds->ds_num != vl->values_len)
1319 ERROR ("plugin_dispatch_values: ds->type = %s: "
1320 "(ds->ds_num = %i) != "
1321 "(vl->values_len = %i)",
1322 ds->type, ds->ds_num, vl->values_len);
1327 escape_slashes (vl->host, sizeof (vl->host));
1328 escape_slashes (vl->plugin, sizeof (vl->plugin));
1329 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1330 escape_slashes (vl->type, sizeof (vl->type));
1331 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1333 /* Copy the values. This way, we can assure `targets' that they get
1334 * dynamically allocated values, which they can free and replace if
1336 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1338 saved_values = vl->values;
1339 saved_values_len = vl->values_len;
1341 vl->values = (value_t *) calloc (vl->values_len,
1342 sizeof (*vl->values));
1343 if (vl->values == NULL)
1345 ERROR ("plugin_dispatch_values: calloc failed.");
1346 vl->values = saved_values;
1349 memcpy (vl->values, saved_values,
1350 vl->values_len * sizeof (*vl->values));
1352 else /* if ((pre == NULL) && (post == NULL)) */
1354 saved_values = NULL;
1355 saved_values_len = 0;
1358 if (pre_cache_chain != NULL)
1360 status = fc_process_chain (ds, vl, pre_cache_chain);
1363 WARNING ("plugin_dispatch_values: Running the "
1364 "pre-cache chain failed with "
1368 else if (status == FC_TARGET_STOP)
1370 /* Restore the state of the value_list so that plugins
1371 * don't get confused.. */
1372 if (saved_values != NULL)
1375 vl->values = saved_values;
1376 vl->values_len = saved_values_len;
1382 /* Update the value cache */
1385 /* Initiate threshold checking */
1386 ut_check_threshold (ds, vl);
1388 if (post_cache_chain != NULL)
1390 status = fc_process_chain (ds, vl, post_cache_chain);
1393 WARNING ("plugin_dispatch_values: Running the "
1394 "post-cache chain failed with "
1400 fc_default_action (ds, vl);
1402 /* Restore the state of the value_list so that plugins don't get
1404 if (saved_values != NULL)
1407 vl->values = saved_values;
1408 vl->values_len = saved_values_len;
1411 if ((free_meta_data != 0) && (vl->meta != NULL))
1413 meta_data_destroy (vl->meta);
1418 } /* int plugin_dispatch_values */
1420 int plugin_dispatch_notification (const notification_t *notif)
1423 /* Possible TODO: Add flap detection here */
1425 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1426 "time = %u; host = %s;",
1427 notif->severity, notif->message,
1428 (unsigned int) notif->time, notif->host);
1430 /* Nobody cares for notifications */
1431 if (list_notification == NULL)
1434 le = llist_head (list_notification);
1437 callback_func_t *cf;
1438 plugin_notification_cb callback;
1442 callback = cf->cf_callback;
1443 status = (*callback) (notif, &cf->cf_udata);
1446 WARNING ("plugin_dispatch_notification: Notification "
1447 "callback %s returned %i.",
1455 } /* int plugin_dispatch_notification */
1457 void plugin_log (int level, const char *format, ...)
1463 if (list_log == NULL)
1467 if (level >= LOG_DEBUG)
1471 va_start (ap, format);
1472 vsnprintf (msg, sizeof (msg), format, ap);
1473 msg[sizeof (msg) - 1] = '\0';
1476 le = llist_head (list_log);
1479 callback_func_t *cf;
1480 plugin_log_cb callback;
1483 callback = cf->cf_callback;
1485 (*callback) (level, msg, &cf->cf_udata);
1489 } /* void plugin_log */
1491 const data_set_t *plugin_get_ds (const char *name)
1495 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1497 DEBUG ("No such dataset registered: %s", name);
1502 } /* data_set_t *plugin_get_ds */
1504 static int plugin_notification_meta_add (notification_t *n,
1506 enum notification_meta_type_e type,
1509 notification_meta_t *meta;
1510 notification_meta_t *tail;
1512 if ((n == NULL) || (name == NULL) || (value == NULL))
1514 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1518 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1521 ERROR ("plugin_notification_meta_add: malloc failed.");
1524 memset (meta, 0, sizeof (notification_meta_t));
1526 sstrncpy (meta->name, name, sizeof (meta->name));
1531 case NM_TYPE_STRING:
1533 meta->nm_value.nm_string = strdup ((const char *) value);
1534 if (meta->nm_value.nm_string == NULL)
1536 ERROR ("plugin_notification_meta_add: strdup failed.");
1542 case NM_TYPE_SIGNED_INT:
1544 meta->nm_value.nm_signed_int = *((int64_t *) value);
1547 case NM_TYPE_UNSIGNED_INT:
1549 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1552 case NM_TYPE_DOUBLE:
1554 meta->nm_value.nm_double = *((double *) value);
1557 case NM_TYPE_BOOLEAN:
1559 meta->nm_value.nm_boolean = *((bool *) value);
1564 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1568 } /* switch (type) */
1572 while ((tail != NULL) && (tail->next != NULL))
1581 } /* int plugin_notification_meta_add */
1583 int plugin_notification_meta_add_string (notification_t *n,
1587 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1590 int plugin_notification_meta_add_signed_int (notification_t *n,
1594 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1597 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1601 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1604 int plugin_notification_meta_add_double (notification_t *n,
1608 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1611 int plugin_notification_meta_add_boolean (notification_t *n,
1615 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1618 int plugin_notification_meta_copy (notification_t *dst,
1619 const notification_t *src)
1621 notification_meta_t *meta;
1623 assert (dst != NULL);
1624 assert (src != NULL);
1625 assert (dst != src);
1626 assert ((src->meta == NULL) || (src->meta != dst->meta));
1628 for (meta = src->meta; meta != NULL; meta = meta->next)
1630 if (meta->type == NM_TYPE_STRING)
1631 plugin_notification_meta_add_string (dst, meta->name,
1632 meta->nm_value.nm_string);
1633 else if (meta->type == NM_TYPE_SIGNED_INT)
1634 plugin_notification_meta_add_signed_int (dst, meta->name,
1635 meta->nm_value.nm_signed_int);
1636 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1637 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1638 meta->nm_value.nm_unsigned_int);
1639 else if (meta->type == NM_TYPE_DOUBLE)
1640 plugin_notification_meta_add_double (dst, meta->name,
1641 meta->nm_value.nm_double);
1642 else if (meta->type == NM_TYPE_BOOLEAN)
1643 plugin_notification_meta_add_boolean (dst, meta->name,
1644 meta->nm_value.nm_boolean);
1648 } /* int plugin_notification_meta_copy */
1650 int plugin_notification_meta_free (notification_meta_t *n)
1652 notification_meta_t *this;
1653 notification_meta_t *next;
1657 ERROR ("plugin_notification_meta_free: n == NULL!");
1662 while (this != NULL)
1666 if (this->type == NM_TYPE_STRING)
1668 free ((char *)this->nm_value.nm_string);
1669 this->nm_value.nm_string = NULL;
1677 } /* int plugin_notification_meta_free */
1679 /* vim: set sw=8 ts=8 noet fdm=marker : */