2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2014 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian Harl <sh at tokkee.org>
28 /* _GNU_SOURCE is needed in Linux to use pthread_setname_np */
35 #include "configfile.h"
36 #include "filter_chain.h"
37 #include "utils_avltree.h"
38 #include "utils_cache.h"
39 #include "utils_complain.h"
40 #include "utils_llist.h"
41 #include "utils_heap.h"
42 #include "utils_time.h"
43 #include "utils_random.h"
46 # include <pthread_np.h> /* for pthread_set_name_np(3) */
54 struct callback_func_s
60 typedef struct callback_func_s callback_func_t;
64 #define RF_REMOVE 65535
67 /* `read_func_t' "inherits" from `callback_func_t'.
68 * The `rf_super' member MUST be the first one in this structure! */
69 #define rf_callback rf_super.cf_callback
70 #define rf_udata rf_super.cf_udata
71 #define rf_ctx rf_super.cf_ctx
72 callback_func_t rf_super;
73 char rf_group[DATA_MAX_NAME_LEN];
77 cdtime_t rf_effective_interval;
78 cdtime_t rf_next_read;
80 typedef struct read_func_s read_func_t;
83 typedef struct write_queue_s write_queue_t;
91 struct flush_callback_s {
95 typedef struct flush_callback_s flush_callback_t;
100 static c_avl_tree_t *plugins_loaded = NULL;
102 static llist_t *list_init;
103 static llist_t *list_write;
104 static llist_t *list_flush;
105 static llist_t *list_missing;
106 static llist_t *list_shutdown;
107 static llist_t *list_log;
108 static llist_t *list_notification;
110 static fc_chain_t *pre_cache_chain = NULL;
111 static fc_chain_t *post_cache_chain = NULL;
113 static c_avl_tree_t *data_sets;
115 static char *plugindir = NULL;
117 #ifndef DEFAULT_MAX_READ_INTERVAL
118 # define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T_STATIC (86400)
120 static c_heap_t *read_heap = NULL;
121 static llist_t *read_list;
122 static int read_loop = 1;
123 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
124 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
125 static pthread_t *read_threads = NULL;
126 static size_t read_threads_num = 0;
127 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
129 static write_queue_t *write_queue_head;
130 static write_queue_t *write_queue_tail;
131 static long write_queue_length = 0;
132 static _Bool write_loop = 1;
133 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
134 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
135 static pthread_t *write_threads = NULL;
136 static size_t write_threads_num = 0;
138 static pthread_key_t plugin_ctx_key;
139 static _Bool plugin_ctx_key_initialized = 0;
141 static long write_limit_high = 0;
142 static long write_limit_low = 0;
144 static derive_t stats_values_dropped = 0;
145 static _Bool record_statistics = 0;
150 static int plugin_dispatch_values_internal (value_list_t *vl);
152 static const char *plugin_get_dir (void)
154 if (plugindir == NULL)
160 static void plugin_update_internal_statistics (void) { /* {{{ */
162 gauge_t copy_write_queue_length = (gauge_t) write_queue_length;
164 /* Initialize `vl' */
165 value_list_t vl = VALUE_LIST_INIT;
166 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
167 sstrncpy (vl.plugin, "collectd", sizeof (vl.plugin));
170 sstrncpy (vl.plugin_instance, "write_queue",
171 sizeof (vl.plugin_instance));
173 /* Write queue : queue length */
174 vl.values = &(value_t) { .gauge = copy_write_queue_length };
176 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
177 vl.type_instance[0] = 0;
178 plugin_dispatch_values (&vl);
180 /* Write queue : Values dropped (queue length > low limit) */
181 vl.values = &(value_t) { .gauge = (gauge_t) stats_values_dropped };
183 sstrncpy (vl.type, "derive", sizeof (vl.type));
184 sstrncpy (vl.type_instance, "dropped", sizeof (vl.type_instance));
185 plugin_dispatch_values (&vl);
188 sstrncpy (vl.plugin_instance, "cache",
189 sizeof (vl.plugin_instance));
191 /* Cache : Nb entry in cache tree */
192 vl.values = &(value_t) { .gauge = (gauge_t) uc_get_size() };
194 sstrncpy (vl.type, "cache_size", sizeof (vl.type));
195 vl.type_instance[0] = 0;
196 plugin_dispatch_values (&vl);
199 } /* }}} void plugin_update_internal_statistics */
201 static void destroy_callback (callback_func_t *cf) /* {{{ */
206 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
208 cf->cf_udata.free_func (cf->cf_udata.data);
209 cf->cf_udata.data = NULL;
210 cf->cf_udata.free_func = NULL;
213 } /* }}} void destroy_callback */
215 static void destroy_all_callbacks (llist_t **list) /* {{{ */
222 le = llist_head (*list);
230 destroy_callback (le->value);
236 llist_destroy (*list);
238 } /* }}} void destroy_all_callbacks */
240 static void destroy_read_heap (void) /* {{{ */
242 if (read_heap == NULL)
249 rf = c_heap_get_root (read_heap);
253 destroy_callback ((callback_func_t *) rf);
256 c_heap_destroy (read_heap);
258 } /* }}} void destroy_read_heap */
260 static int register_callback (llist_t **list, /* {{{ */
261 const char *name, callback_func_t *cf)
268 *list = llist_create ();
271 ERROR ("plugin: register_callback: "
272 "llist_create failed.");
273 destroy_callback (cf);
281 ERROR ("plugin: register_callback: strdup failed.");
282 destroy_callback (cf);
286 le = llist_search (*list, name);
289 le = llentry_create (key, cf);
292 ERROR ("plugin: register_callback: "
293 "llentry_create failed.");
295 destroy_callback (cf);
299 llist_append (*list, le);
303 callback_func_t *old_cf;
308 WARNING ("plugin: register_callback: "
309 "a callback named `%s' already exists - "
310 "overwriting the old entry!", name);
312 destroy_callback (old_cf);
317 } /* }}} int register_callback */
319 static void log_list_callbacks (llist_t **list, /* {{{ */
329 n = llist_size(*list);
332 INFO("%s [none]", comment);
336 keys = calloc(n, sizeof(char*));
340 ERROR("%s: failed to allocate memory for list of callbacks",
346 for (le = llist_head (*list), i = 0, len = 0;
351 len += strlen(le->key) + 6;
353 str = malloc(len + 10);
356 ERROR("%s: failed to allocate memory for list of callbacks",
362 strjoin(str, len, keys, n, "', '");
363 INFO("%s ['%s']", comment, str);
367 } /* }}} void log_list_callbacks */
369 static int create_register_callback (llist_t **list, /* {{{ */
370 const char *name, void *callback, user_data_t const *ud)
374 cf = calloc (1, sizeof (*cf));
377 ERROR ("plugin: create_register_callback: calloc failed.");
381 cf->cf_callback = callback;
384 cf->cf_udata.data = NULL;
385 cf->cf_udata.free_func = NULL;
392 cf->cf_ctx = plugin_get_ctx ();
394 return (register_callback (list, name, cf));
395 } /* }}} int create_register_callback */
397 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
404 e = llist_search (list, name);
408 llist_remove (list, e);
411 destroy_callback (e->value);
416 } /* }}} int plugin_unregister */
419 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
420 * object, but it will bitch about a shared object not having a
421 * ``module_register'' symbol..
423 static int plugin_load_file (char *file, uint32_t flags)
426 void (*reg_handle) (void);
429 lt_dlerror (); /* clear errors */
431 #if LIBTOOL_VERSION == 2
432 if (flags & PLUGIN_FLAGS_GLOBAL) {
434 lt_dladvise_init(&advise);
435 lt_dladvise_global(&advise);
436 dlh = lt_dlopenadvise(file, advise);
437 lt_dladvise_destroy(&advise);
439 dlh = lt_dlopen (file);
441 #else /* if LIBTOOL_VERSION == 1 */
442 if (flags & PLUGIN_FLAGS_GLOBAL)
443 WARNING ("plugin_load_file: The global flag is not supported, "
444 "libtool 2 is required for this.");
445 dlh = lt_dlopen (file);
450 char errbuf[1024] = "";
452 ssnprintf (errbuf, sizeof (errbuf),
453 "lt_dlopen (\"%s\") failed: %s. "
454 "The most common cause for this problem is "
455 "missing dependencies. Use ldd(1) to check "
456 "the dependencies of the plugin "
458 file, lt_dlerror ());
460 ERROR ("%s", errbuf);
461 /* Make sure this is printed to STDERR in any case, but also
462 * make sure it's printed only once. */
463 if (list_log != NULL)
464 fprintf (stderr, "ERROR: %s\n", errbuf);
469 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
471 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
472 file, lt_dlerror ());
482 static void *plugin_read_thread (void __attribute__((unused)) *args)
484 while (read_loop != 0)
487 plugin_ctx_t old_ctx;
495 /* Get the read function that needs to be read next.
496 * We don't need to hold "read_lock" for the heap, but we need
497 * to call c_heap_get_root() and pthread_cond_wait() in the
498 * same protected block. */
499 pthread_mutex_lock (&read_lock);
500 rf = c_heap_get_root (read_heap);
503 pthread_cond_wait (&read_cond, &read_lock);
504 pthread_mutex_unlock (&read_lock);
507 pthread_mutex_unlock (&read_lock);
509 if (rf->rf_interval == 0)
511 /* this should not happen, because the interval is set
512 * for each plugin when loading it
513 * XXX: issue a warning? */
514 rf->rf_interval = plugin_get_interval ();
515 rf->rf_effective_interval = rf->rf_interval;
517 rf->rf_next_read = cdtime ();
520 /* sleep until this entry is due,
521 * using pthread_cond_timedwait */
522 pthread_mutex_lock (&read_lock);
523 /* In pthread_cond_timedwait, spurious wakeups are possible
524 * (and really happen, at least on NetBSD with > 1 CPU), thus
525 * we need to re-evaluate the condition every time
526 * pthread_cond_timedwait returns. */
528 while ((read_loop != 0)
529 && (cdtime () < rf->rf_next_read)
532 rc = pthread_cond_timedwait (&read_cond, &read_lock,
533 &CDTIME_T_TO_TIMESPEC (rf->rf_next_read));
536 /* Must hold `read_lock' when accessing `rf->rf_type'. */
537 rf_type = rf->rf_type;
538 pthread_mutex_unlock (&read_lock);
540 /* Check if we're supposed to stop.. This may have interrupted
544 /* Insert `rf' again, so it can be free'd correctly */
545 c_heap_insert (read_heap, rf);
549 /* The entry has been marked for deletion. The linked list
550 * entry has already been removed by `plugin_unregister_read'.
551 * All we have to do here is free the `read_func_t' and
553 if (rf_type == RF_REMOVE)
555 DEBUG ("plugin_read_thread: Destroying the `%s' "
556 "callback.", rf->rf_name);
558 destroy_callback ((callback_func_t *) rf);
563 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
567 old_ctx = plugin_set_ctx (rf->rf_ctx);
569 if (rf_type == RF_SIMPLE)
571 int (*callback) (void);
573 callback = rf->rf_callback;
574 status = (*callback) ();
578 plugin_read_cb callback;
580 assert (rf_type == RF_COMPLEX);
582 callback = rf->rf_callback;
583 status = (*callback) (&rf->rf_udata);
586 plugin_set_ctx (old_ctx);
588 /* If the function signals failure, we will increase the
589 * intervals in which it will be called. */
592 rf->rf_effective_interval *= 2;
593 if (rf->rf_effective_interval > max_read_interval)
594 rf->rf_effective_interval = max_read_interval;
596 NOTICE ("read-function of plugin `%s' failed. "
597 "Will suspend it for %.3f seconds.",
599 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
603 /* Success: Restore the interval, if it was changed. */
604 rf->rf_effective_interval = rf->rf_interval;
607 /* update the ``next read due'' field */
610 /* calculate the time spent in the read function */
611 elapsed = (now - start);
613 if (elapsed > rf->rf_effective_interval)
614 WARNING ("plugin_read_thread: read-function of the `%s' plugin took %.3f "
615 "seconds, which is above its read interval (%.3f seconds). You might "
616 "want to adjust the `Interval' or `ReadThreads' settings.",
617 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed),
618 CDTIME_T_TO_DOUBLE(rf->rf_effective_interval));
620 DEBUG ("plugin_read_thread: read-function of the `%s' plugin took "
622 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed));
624 DEBUG ("plugin_read_thread: Effective interval of the "
625 "`%s' plugin is %.3f seconds.",
627 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
629 /* Calculate the next (absolute) time at which this function
630 * should be called. */
631 rf->rf_next_read += rf->rf_effective_interval;
633 /* Check, if `rf_next_read' is in the past. */
634 if (rf->rf_next_read < now)
636 /* `rf_next_read' is in the past. Insert `now'
637 * so this value doesn't trail off into the
639 rf->rf_next_read = now;
642 DEBUG ("plugin_read_thread: Next read of the `%s' plugin at %.3f.",
644 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
646 /* Re-insert this read function into the heap again. */
647 c_heap_insert (read_heap, rf);
648 } /* while (read_loop) */
652 } /* void *plugin_read_thread */
654 #ifdef PTHREAD_MAX_NAMELEN_NP
655 # define THREAD_NAME_MAX PTHREAD_MAX_NAMELEN_NP
657 # define THREAD_NAME_MAX 16
660 static void set_thread_name(pthread_t tid, char const *name) {
661 #if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP)
663 /* glibc limits the length of the name and fails if the passed string
664 * is too long, so we truncate it here. */
665 char n[THREAD_NAME_MAX];
666 if (strlen (name) >= THREAD_NAME_MAX)
667 WARNING("set_thread_name(\"%s\"): name too long", name);
668 sstrncpy (n, name, sizeof(n));
670 #if defined(HAVE_PTHREAD_SETNAME_NP)
671 int status = pthread_setname_np (tid, n);
675 ERROR ("set_thread_name(\"%s\"): %s", n,
676 sstrerror (status, errbuf, sizeof(errbuf)));
678 #else /* if defined(HAVE_PTHREAD_SET_NAME_NP) */
679 pthread_set_name_np (tid, n);
685 static void start_read_threads (size_t num) /* {{{ */
687 if (read_threads != NULL)
690 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
691 if (read_threads == NULL)
693 ERROR ("plugin: start_read_threads: calloc failed.");
697 read_threads_num = 0;
698 for (size_t i = 0; i < num; i++)
700 int status = pthread_create (read_threads + read_threads_num,
707 ERROR ("plugin: start_read_threads: pthread_create failed "
708 "with status %i (%s).", status,
709 sstrerror (status, errbuf, sizeof (errbuf)));
713 char name[THREAD_NAME_MAX];
714 ssnprintf (name, sizeof (name), "reader#%zu", read_threads_num);
715 set_thread_name (read_threads[read_threads_num], name);
719 } /* }}} void start_read_threads */
721 static void stop_read_threads (void)
723 if (read_threads == NULL)
726 INFO ("collectd: Stopping %zu read threads.", read_threads_num);
728 pthread_mutex_lock (&read_lock);
730 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
731 pthread_cond_broadcast (&read_cond);
732 pthread_mutex_unlock (&read_lock);
734 for (size_t i = 0; i < read_threads_num; i++)
736 if (pthread_join (read_threads[i], NULL) != 0)
738 ERROR ("plugin: stop_read_threads: pthread_join failed.");
740 read_threads[i] = (pthread_t) 0;
742 sfree (read_threads);
743 read_threads_num = 0;
744 } /* void stop_read_threads */
746 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
751 meta_data_destroy (vl->meta);
754 } /* }}} void plugin_value_list_free */
756 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
763 vl = malloc (sizeof (*vl));
766 memcpy (vl, vl_orig, sizeof (*vl));
768 if (vl->host[0] == 0)
769 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
771 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
772 if (vl->values == NULL)
774 plugin_value_list_free (vl);
777 memcpy (vl->values, vl_orig->values,
778 vl_orig->values_len * sizeof (*vl->values));
780 vl->meta = meta_data_clone (vl->meta);
781 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
783 plugin_value_list_free (vl);
788 vl->time = cdtime ();
790 /* Fill in the interval from the thread context, if it is zero. */
791 if (vl->interval == 0)
793 plugin_ctx_t ctx = plugin_get_ctx ();
795 if (ctx.interval != 0)
796 vl->interval = ctx.interval;
799 char name[6 * DATA_MAX_NAME_LEN];
800 FORMAT_VL (name, sizeof (name), vl);
801 ERROR ("plugin_value_list_clone: Unable to determine "
802 "interval from context for "
803 "value list \"%s\". "
804 "This indicates a broken plugin. "
805 "Please report this problem to the "
806 "collectd mailing list or at "
807 "<http://collectd.org/bugs/>.", name);
808 vl->interval = cf_get_default_interval ();
813 } /* }}} value_list_t *plugin_value_list_clone */
815 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
819 q = malloc (sizeof (*q));
824 q->vl = plugin_value_list_clone (vl);
831 /* Store context of caller (read plugin); otherwise, it would not be
832 * available to the write plugins when actually dispatching the
833 * value-list later on. */
834 q->ctx = plugin_get_ctx ();
836 pthread_mutex_lock (&write_lock);
838 if (write_queue_tail == NULL)
840 write_queue_head = q;
841 write_queue_tail = q;
842 write_queue_length = 1;
846 write_queue_tail->next = q;
847 write_queue_tail = q;
848 write_queue_length += 1;
851 pthread_cond_signal (&write_cond);
852 pthread_mutex_unlock (&write_lock);
855 } /* }}} int plugin_write_enqueue */
857 static value_list_t *plugin_write_dequeue (void) /* {{{ */
862 pthread_mutex_lock (&write_lock);
864 while (write_loop && (write_queue_head == NULL))
865 pthread_cond_wait (&write_cond, &write_lock);
867 if (write_queue_head == NULL)
869 pthread_mutex_unlock (&write_lock);
873 q = write_queue_head;
874 write_queue_head = q->next;
875 write_queue_length -= 1;
876 if (write_queue_head == NULL) {
877 write_queue_tail = NULL;
878 assert(0 == write_queue_length);
881 pthread_mutex_unlock (&write_lock);
883 (void) plugin_set_ctx (q->ctx);
888 } /* }}} value_list_t *plugin_write_dequeue */
890 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
894 value_list_t *vl = plugin_write_dequeue ();
898 plugin_dispatch_values_internal (vl);
900 plugin_value_list_free (vl);
905 } /* }}} void *plugin_write_thread */
907 static void start_write_threads (size_t num) /* {{{ */
909 if (write_threads != NULL)
912 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
913 if (write_threads == NULL)
915 ERROR ("plugin: start_write_threads: calloc failed.");
919 write_threads_num = 0;
920 for (size_t i = 0; i < num; i++)
922 int status = pthread_create (write_threads + write_threads_num,
929 ERROR ("plugin: start_write_threads: pthread_create failed "
930 "with status %i (%s).", status,
931 sstrerror (status, errbuf, sizeof (errbuf)));
935 char name[THREAD_NAME_MAX];
936 ssnprintf (name, sizeof (name), "writer#%zu", write_threads_num);
937 set_thread_name (write_threads[write_threads_num], name);
941 } /* }}} void start_write_threads */
943 static void stop_write_threads (void) /* {{{ */
948 if (write_threads == NULL)
951 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
953 pthread_mutex_lock (&write_lock);
955 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
956 pthread_cond_broadcast (&write_cond);
957 pthread_mutex_unlock (&write_lock);
959 for (i = 0; i < write_threads_num; i++)
961 if (pthread_join (write_threads[i], NULL) != 0)
963 ERROR ("plugin: stop_write_threads: pthread_join failed.");
965 write_threads[i] = (pthread_t) 0;
967 sfree (write_threads);
968 write_threads_num = 0;
970 pthread_mutex_lock (&write_lock);
972 for (q = write_queue_head; q != NULL; )
974 write_queue_t *q1 = q;
975 plugin_value_list_free (q->vl);
980 write_queue_head = NULL;
981 write_queue_tail = NULL;
982 write_queue_length = 0;
983 pthread_mutex_unlock (&write_lock);
987 WARNING ("plugin: %zu value list%s left after shutting down "
988 "the write threads.",
989 i, (i == 1) ? " was" : "s were");
991 } /* }}} void stop_write_threads */
996 void plugin_set_dir (const char *dir)
1006 plugindir = strdup (dir);
1007 if (plugindir == NULL)
1008 ERROR ("plugin_set_dir: strdup(\"%s\") failed", dir);
1011 static _Bool plugin_is_loaded (char const *name)
1015 if (plugins_loaded == NULL)
1016 plugins_loaded = c_avl_create ((int (*) (const void *, const void *)) strcasecmp);
1017 assert (plugins_loaded != NULL);
1019 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
1020 return (status == 0);
1023 static int plugin_mark_loaded (char const *name)
1028 name_copy = strdup (name);
1029 if (name_copy == NULL)
1032 status = c_avl_insert (plugins_loaded,
1033 /* key = */ name_copy, /* value = */ NULL);
1037 static void plugin_free_loaded (void)
1042 if (plugins_loaded == NULL)
1045 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
1048 assert (value == NULL);
1051 c_avl_destroy (plugins_loaded);
1052 plugins_loaded = NULL;
1056 int plugin_load (char const *plugin_name, uint32_t flags)
1060 char filename[BUFSIZE] = "";
1061 char typename[BUFSIZE];
1063 struct stat statbuf;
1067 if (plugin_name == NULL)
1070 /* Check if plugin is already loaded and don't do anything in this
1072 if (plugin_is_loaded (plugin_name))
1075 dir = plugin_get_dir ();
1079 * XXX: Magic at work:
1081 * Some of the language bindings, for example the Python and Perl
1082 * plugins, need to be able to export symbols to the scripts they run.
1083 * For this to happen, the "Globals" flag needs to be set.
1084 * Unfortunately, this technical detail is hard to explain to the
1085 * average user and she shouldn't have to worry about this, ideally.
1086 * So in order to save everyone's sanity use a different default for a
1087 * handful of special plugins. --octo
1089 if ((strcasecmp ("perl", plugin_name) == 0)
1090 || (strcasecmp ("python", plugin_name) == 0))
1091 flags |= PLUGIN_FLAGS_GLOBAL;
1093 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
1094 * type when matching the filename */
1095 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
1096 if ((status < 0) || ((size_t) status >= sizeof (typename)))
1098 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
1102 if ((dh = opendir (dir)) == NULL)
1105 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
1106 sstrerror (errno, errbuf, sizeof (errbuf)));
1110 while ((de = readdir (dh)) != NULL)
1112 if (strcasecmp (de->d_name, typename))
1115 status = ssnprintf (filename, sizeof (filename),
1116 "%s/%s", dir, de->d_name);
1117 if ((status < 0) || ((size_t) status >= sizeof (filename)))
1119 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
1124 if (lstat (filename, &statbuf) == -1)
1127 WARNING ("plugin_load: stat (\"%s\") failed: %s",
1129 sstrerror (errno, errbuf, sizeof (errbuf)));
1132 else if (!S_ISREG (statbuf.st_mode))
1134 /* don't follow symlinks */
1135 WARNING ("plugin_load: %s is not a regular file.",
1140 status = plugin_load_file (filename, flags);
1144 plugin_mark_loaded (plugin_name);
1146 INFO ("plugin_load: plugin \"%s\" successfully loaded.", plugin_name);
1151 ERROR ("plugin_load: Load plugin \"%s\" failed with "
1152 "status %i.", plugin_name, status);
1158 if (filename[0] == 0)
1159 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1166 * The `register_*' functions follow
1168 int plugin_register_config (const char *name,
1169 int (*callback) (const char *key, const char *val),
1170 const char **keys, int keys_num)
1172 cf_register (name, callback, keys, keys_num);
1174 } /* int plugin_register_config */
1176 int plugin_register_complex_config (const char *type,
1177 int (*callback) (oconfig_item_t *))
1179 return (cf_register_complex (type, callback));
1180 } /* int plugin_register_complex_config */
1182 int plugin_register_init (const char *name,
1183 int (*callback) (void))
1185 return (create_register_callback (&list_init, name, (void *) callback,
1186 /* user_data = */ NULL));
1187 } /* plugin_register_init */
1189 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1191 const read_func_t *rf0;
1192 const read_func_t *rf1;
1197 if (rf0->rf_next_read < rf1->rf_next_read)
1199 else if (rf0->rf_next_read > rf1->rf_next_read)
1203 } /* int plugin_compare_read_func */
1205 /* Add a read function to both, the heap and a linked list. The linked list if
1206 * used to look-up read functions, especially for the remove function. The heap
1207 * is used to determine which plugin to read next. */
1208 static int plugin_insert_read (read_func_t *rf)
1213 rf->rf_next_read = cdtime ();
1214 rf->rf_effective_interval = rf->rf_interval;
1216 pthread_mutex_lock (&read_lock);
1218 if (read_list == NULL)
1220 read_list = llist_create ();
1221 if (read_list == NULL)
1223 pthread_mutex_unlock (&read_lock);
1224 ERROR ("plugin_insert_read: read_list failed.");
1229 if (read_heap == NULL)
1231 read_heap = c_heap_create (plugin_compare_read_func);
1232 if (read_heap == NULL)
1234 pthread_mutex_unlock (&read_lock);
1235 ERROR ("plugin_insert_read: c_heap_create failed.");
1240 le = llist_search (read_list, rf->rf_name);
1243 pthread_mutex_unlock (&read_lock);
1244 WARNING ("The read function \"%s\" is already registered. "
1245 "Check for duplicate \"LoadPlugin\" lines "
1246 "in your configuration!",
1251 le = llentry_create (rf->rf_name, rf);
1254 pthread_mutex_unlock (&read_lock);
1255 ERROR ("plugin_insert_read: llentry_create failed.");
1259 status = c_heap_insert (read_heap, rf);
1262 pthread_mutex_unlock (&read_lock);
1263 ERROR ("plugin_insert_read: c_heap_insert failed.");
1264 llentry_destroy (le);
1268 /* This does not fail. */
1269 llist_append (read_list, le);
1271 /* Wake up all the read threads. */
1272 pthread_cond_broadcast (&read_cond);
1273 pthread_mutex_unlock (&read_lock);
1275 } /* int plugin_insert_read */
1277 int plugin_register_read (const char *name,
1278 int (*callback) (void))
1283 rf = calloc (1, sizeof (*rf));
1286 ERROR ("plugin_register_read: calloc failed.");
1290 rf->rf_callback = (void *) callback;
1291 rf->rf_udata.data = NULL;
1292 rf->rf_udata.free_func = NULL;
1293 rf->rf_ctx = plugin_get_ctx ();
1294 rf->rf_group[0] = '\0';
1295 rf->rf_name = strdup (name);
1296 rf->rf_type = RF_SIMPLE;
1297 rf->rf_interval = plugin_get_interval ();
1299 status = plugin_insert_read (rf);
1301 sfree (rf->rf_name);
1306 } /* int plugin_register_read */
1308 int plugin_register_complex_read (const char *group, const char *name,
1309 plugin_read_cb callback,
1311 user_data_t const *user_data)
1316 rf = calloc (1,sizeof (*rf));
1319 ERROR ("plugin_register_complex_read: calloc failed.");
1323 rf->rf_callback = (void *) callback;
1325 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1327 rf->rf_group[0] = '\0';
1328 rf->rf_name = strdup (name);
1329 rf->rf_type = RF_COMPLEX;
1330 rf->rf_interval = (interval != 0) ? interval : plugin_get_interval ();
1333 if (user_data == NULL)
1335 rf->rf_udata.data = NULL;
1336 rf->rf_udata.free_func = NULL;
1340 rf->rf_udata = *user_data;
1343 rf->rf_ctx = plugin_get_ctx ();
1345 status = plugin_insert_read (rf);
1347 sfree (rf->rf_name);
1352 } /* int plugin_register_complex_read */
1354 int plugin_register_write (const char *name,
1355 plugin_write_cb callback, user_data_t const *ud)
1357 return (create_register_callback (&list_write, name,
1358 (void *) callback, ud));
1359 } /* int plugin_register_write */
1361 static int plugin_flush_timeout_callback (user_data_t *ud)
1363 flush_callback_t *cb = ud->data;
1365 return plugin_flush (cb->name, cb->timeout, /* identifier = */ NULL);
1366 } /* static int plugin_flush_callback */
1368 static void plugin_flush_timeout_callback_free (void *data)
1370 flush_callback_t *cb = data;
1372 if (cb == NULL) return;
1376 } /* static void plugin_flush_callback_free */
1378 static char *plugin_flush_callback_name (const char *name)
1380 const char *flush_prefix = "flush/";
1385 prefix_size = strlen(flush_prefix);
1386 name_size = strlen(name);
1388 flush_name = malloc (name_size + prefix_size + 1);
1389 if (flush_name == NULL)
1391 ERROR ("plugin_flush_callback_name: malloc failed.");
1395 sstrncpy (flush_name, flush_prefix, prefix_size + 1);
1396 sstrncpy (flush_name + prefix_size, name, name_size + 1);
1399 } /* static char *plugin_flush_callback_name */
1401 int plugin_register_flush (const char *name,
1402 plugin_flush_cb callback, user_data_t const *ud)
1405 plugin_ctx_t ctx = plugin_get_ctx ();
1407 status = create_register_callback (&list_flush, name,
1408 (void *) callback, ud);
1412 if (ctx.flush_interval != 0)
1415 flush_callback_t *cb;
1417 flush_name = plugin_flush_callback_name (name);
1418 if (flush_name == NULL)
1421 cb = malloc(sizeof (*cb));
1424 ERROR ("plugin_register_flush: malloc failed.");
1429 cb->name = strdup (name);
1430 if (cb->name == NULL)
1432 ERROR ("plugin_register_flush: strdup failed.");
1437 cb->timeout = ctx.flush_timeout;
1439 status = plugin_register_complex_read (
1440 /* group = */ "flush",
1441 /* name = */ flush_name,
1442 /* callback = */ plugin_flush_timeout_callback,
1443 /* interval = */ ctx.flush_interval,
1444 /* user data = */ &(user_data_t) {
1446 .free_func = plugin_flush_timeout_callback_free,
1459 } /* int plugin_register_flush */
1461 int plugin_register_missing (const char *name,
1462 plugin_missing_cb callback, user_data_t const *ud)
1464 return (create_register_callback (&list_missing, name,
1465 (void *) callback, ud));
1466 } /* int plugin_register_missing */
1468 int plugin_register_shutdown (const char *name,
1469 int (*callback) (void))
1471 return (create_register_callback (&list_shutdown, name,
1472 (void *) callback, /* user_data = */ NULL));
1473 } /* int plugin_register_shutdown */
1475 static void plugin_free_data_sets (void)
1480 if (data_sets == NULL)
1483 while (c_avl_pick (data_sets, &key, &value) == 0)
1485 data_set_t *ds = value;
1486 /* key is a pointer to ds->type */
1492 c_avl_destroy (data_sets);
1494 } /* void plugin_free_data_sets */
1496 int plugin_register_data_set (const data_set_t *ds)
1498 data_set_t *ds_copy;
1500 if ((data_sets != NULL)
1501 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1503 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1504 plugin_unregister_data_set (ds->type);
1506 else if (data_sets == NULL)
1508 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1509 if (data_sets == NULL)
1513 ds_copy = malloc (sizeof (*ds_copy));
1514 if (ds_copy == NULL)
1516 memcpy(ds_copy, ds, sizeof (data_set_t));
1518 ds_copy->ds = malloc (sizeof (*ds_copy->ds)
1520 if (ds_copy->ds == NULL)
1526 for (size_t i = 0; i < ds->ds_num; i++)
1527 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1529 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1530 } /* int plugin_register_data_set */
1532 int plugin_register_log (const char *name,
1533 plugin_log_cb callback, user_data_t const *ud)
1535 return (create_register_callback (&list_log, name,
1536 (void *) callback, ud));
1537 } /* int plugin_register_log */
1539 int plugin_register_notification (const char *name,
1540 plugin_notification_cb callback, user_data_t const *ud)
1542 return (create_register_callback (&list_notification, name,
1543 (void *) callback, ud));
1544 } /* int plugin_register_log */
1546 int plugin_unregister_config (const char *name)
1548 cf_unregister (name);
1550 } /* int plugin_unregister_config */
1552 int plugin_unregister_complex_config (const char *name)
1554 cf_unregister_complex (name);
1556 } /* int plugin_unregister_complex_config */
1558 int plugin_unregister_init (const char *name)
1560 return (plugin_unregister (list_init, name));
1563 int plugin_unregister_read (const char *name) /* {{{ */
1571 pthread_mutex_lock (&read_lock);
1573 if (read_list == NULL)
1575 pthread_mutex_unlock (&read_lock);
1579 le = llist_search (read_list, name);
1582 pthread_mutex_unlock (&read_lock);
1583 WARNING ("plugin_unregister_read: No such read function: %s",
1588 llist_remove (read_list, le);
1591 assert (rf != NULL);
1592 rf->rf_type = RF_REMOVE;
1594 pthread_mutex_unlock (&read_lock);
1596 llentry_destroy (le);
1598 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1601 } /* }}} int plugin_unregister_read */
1603 void plugin_log_available_writers (void)
1605 log_list_callbacks (&list_write, "Available write targets:");
1608 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1610 read_func_t *rf = e->value;
1613 return strcmp (rf->rf_group, (const char *)group);
1614 } /* }}} int compare_read_func_group */
1616 int plugin_unregister_read_group (const char *group) /* {{{ */
1626 pthread_mutex_lock (&read_lock);
1628 if (read_list == NULL)
1630 pthread_mutex_unlock (&read_lock);
1636 le = llist_search_custom (read_list,
1637 compare_read_func_group, (void *)group);
1644 llist_remove (read_list, le);
1647 assert (rf != NULL);
1648 rf->rf_type = RF_REMOVE;
1650 llentry_destroy (le);
1652 DEBUG ("plugin_unregister_read_group: "
1653 "Marked `%s' (group `%s') for removal.",
1654 rf->rf_name, group);
1657 pthread_mutex_unlock (&read_lock);
1661 WARNING ("plugin_unregister_read_group: No such "
1662 "group of read function: %s", group);
1667 } /* }}} int plugin_unregister_read_group */
1669 int plugin_unregister_write (const char *name)
1671 return (plugin_unregister (list_write, name));
1674 int plugin_unregister_flush (const char *name)
1676 plugin_ctx_t ctx = plugin_get_ctx ();
1678 if (ctx.flush_interval != 0)
1682 flush_name = plugin_flush_callback_name (name);
1683 if (flush_name != NULL)
1685 plugin_unregister_read(flush_name);
1690 return plugin_unregister (list_flush, name);
1693 int plugin_unregister_missing (const char *name)
1695 return (plugin_unregister (list_missing, name));
1698 int plugin_unregister_shutdown (const char *name)
1700 return (plugin_unregister (list_shutdown, name));
1703 int plugin_unregister_data_set (const char *name)
1707 if (data_sets == NULL)
1710 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1717 } /* int plugin_unregister_data_set */
1719 int plugin_unregister_log (const char *name)
1721 return (plugin_unregister (list_log, name));
1724 int plugin_unregister_notification (const char *name)
1726 return (plugin_unregister (list_notification, name));
1729 int plugin_init_all (void)
1731 char const *chain_name;
1736 /* Init the value cache */
1739 if (IS_TRUE (global_option_get ("CollectInternalStats")))
1740 record_statistics = 1;
1742 chain_name = global_option_get ("PreCacheChain");
1743 pre_cache_chain = fc_chain_get_by_name (chain_name);
1745 chain_name = global_option_get ("PostCacheChain");
1746 post_cache_chain = fc_chain_get_by_name (chain_name);
1748 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1750 if (write_limit_high < 0)
1752 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1753 write_limit_high = 0;
1756 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1757 /* default = */ write_limit_high / 2);
1758 if (write_limit_low < 0)
1760 ERROR ("WriteQueueLimitLow must be positive or zero.");
1761 write_limit_low = write_limit_high / 2;
1763 else if (write_limit_low > write_limit_high)
1765 ERROR ("WriteQueueLimitLow must not be larger than "
1766 "WriteQueueLimitHigh.");
1767 write_limit_low = write_limit_high;
1770 write_threads_num = global_option_get_long ("WriteThreads",
1772 if (write_threads_num < 1)
1774 ERROR ("WriteThreads must be positive.");
1775 write_threads_num = 5;
1778 if ((list_init == NULL) && (read_heap == NULL))
1781 /* Calling all init callbacks before checking if read callbacks
1782 * are available allows the init callbacks to register the read
1784 le = llist_head (list_init);
1787 callback_func_t *cf;
1788 plugin_init_cb callback;
1789 plugin_ctx_t old_ctx;
1792 old_ctx = plugin_set_ctx (cf->cf_ctx);
1793 callback = cf->cf_callback;
1794 status = (*callback) ();
1795 plugin_set_ctx (old_ctx);
1799 ERROR ("Initialization of plugin `%s' "
1800 "failed with status %i. "
1801 "Plugin will be unloaded.",
1803 /* Plugins that register read callbacks from the init
1804 * callback should take care of appropriate error
1805 * handling themselves. */
1806 /* FIXME: Unload _all_ functions */
1807 plugin_unregister_read (le->key);
1814 start_write_threads ((size_t) write_threads_num);
1816 max_read_interval = global_option_get_time ("MaxReadInterval",
1817 DEFAULT_MAX_READ_INTERVAL);
1819 /* Start read-threads */
1820 if (read_heap != NULL)
1825 rt = global_option_get ("ReadThreads");
1828 start_read_threads ((num > 0) ? ((size_t) num) : 5);
1831 } /* void plugin_init_all */
1833 /* TODO: Rename this function. */
1834 void plugin_read_all (void)
1836 if(record_statistics) {
1837 plugin_update_internal_statistics ();
1839 uc_check_timeout ();
1842 } /* void plugin_read_all */
1844 /* Read function called when the `-T' command line argument is given. */
1845 int plugin_read_all_once (void)
1848 int return_status = 0;
1850 if (read_heap == NULL)
1852 NOTICE ("No read-functions are registered.");
1859 plugin_ctx_t old_ctx;
1861 rf = c_heap_get_root (read_heap);
1865 old_ctx = plugin_set_ctx (rf->rf_ctx);
1867 if (rf->rf_type == RF_SIMPLE)
1869 int (*callback) (void);
1871 callback = rf->rf_callback;
1872 status = (*callback) ();
1876 plugin_read_cb callback;
1878 callback = rf->rf_callback;
1879 status = (*callback) (&rf->rf_udata);
1882 plugin_set_ctx (old_ctx);
1886 NOTICE ("read-function of plugin `%s' failed.",
1891 sfree (rf->rf_name);
1892 destroy_callback ((void *) rf);
1895 return (return_status);
1896 } /* int plugin_read_all_once */
1898 int plugin_write (const char *plugin, /* {{{ */
1899 const data_set_t *ds, const value_list_t *vl)
1907 if (list_write == NULL)
1912 ds = plugin_get_ds (vl->type);
1915 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1925 le = llist_head (list_write);
1928 callback_func_t *cf = le->value;
1929 plugin_write_cb callback;
1931 /* do not switch plugin context; rather keep the context (interval)
1932 * information of the calling read plugin */
1934 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1935 callback = cf->cf_callback;
1936 status = (*callback) (ds, vl, &cf->cf_udata);
1945 if ((success == 0) && (failure != 0))
1950 else /* plugin != NULL */
1952 callback_func_t *cf;
1953 plugin_write_cb callback;
1955 le = llist_head (list_write);
1958 if (strcasecmp (plugin, le->key) == 0)
1969 /* do not switch plugin context; rather keep the context (interval)
1970 * information of the calling read plugin */
1972 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1973 callback = cf->cf_callback;
1974 status = (*callback) (ds, vl, &cf->cf_udata);
1978 } /* }}} int plugin_write */
1980 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1984 if (list_flush == NULL)
1987 le = llist_head (list_flush);
1990 callback_func_t *cf;
1991 plugin_flush_cb callback;
1992 plugin_ctx_t old_ctx;
1994 if ((plugin != NULL)
1995 && (strcmp (plugin, le->key) != 0))
2002 old_ctx = plugin_set_ctx (cf->cf_ctx);
2003 callback = cf->cf_callback;
2005 (*callback) (timeout, identifier, &cf->cf_udata);
2007 plugin_set_ctx (old_ctx);
2012 } /* int plugin_flush */
2014 int plugin_shutdown_all (void)
2017 int ret = 0; // Assume success.
2019 destroy_all_callbacks (&list_init);
2021 stop_read_threads ();
2023 pthread_mutex_lock (&read_lock);
2024 llist_destroy (read_list);
2026 pthread_mutex_unlock (&read_lock);
2028 destroy_read_heap ();
2030 /* blocks until all write threads have shut down. */
2031 stop_write_threads ();
2033 /* ask all plugins to write out the state they kept. */
2034 plugin_flush (/* plugin = */ NULL,
2036 /* identifier = */ NULL);
2039 if (list_shutdown != NULL)
2040 le = llist_head (list_shutdown);
2044 callback_func_t *cf;
2045 plugin_shutdown_cb callback;
2046 plugin_ctx_t old_ctx;
2049 old_ctx = plugin_set_ctx (cf->cf_ctx);
2050 callback = cf->cf_callback;
2052 /* Advance the pointer before calling the callback allows
2053 * shutdown functions to unregister themselves. If done the
2054 * other way around the memory `le' points to will be freed
2055 * after callback returns. */
2058 if ((*callback) () != 0)
2061 plugin_set_ctx (old_ctx);
2064 /* Write plugins which use the `user_data' pointer usually need the
2065 * same data available to the flush callback. If this is the case, set
2066 * the free_function to NULL when registering the flush callback and to
2067 * the real free function when registering the write callback. This way
2068 * the data isn't freed twice. */
2069 destroy_all_callbacks (&list_flush);
2070 destroy_all_callbacks (&list_missing);
2071 destroy_all_callbacks (&list_write);
2073 destroy_all_callbacks (&list_notification);
2074 destroy_all_callbacks (&list_shutdown);
2075 destroy_all_callbacks (&list_log);
2077 plugin_free_loaded ();
2078 plugin_free_data_sets ();
2080 } /* void plugin_shutdown_all */
2082 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
2086 if (list_missing == NULL)
2089 le = llist_head (list_missing);
2092 callback_func_t *cf;
2093 plugin_missing_cb callback;
2094 plugin_ctx_t old_ctx;
2098 old_ctx = plugin_set_ctx (cf->cf_ctx);
2099 callback = cf->cf_callback;
2101 status = (*callback) (vl, &cf->cf_udata);
2102 plugin_set_ctx (old_ctx);
2107 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
2108 "failed with status %i.",
2121 } /* int }}} plugin_dispatch_missing */
2123 static int plugin_dispatch_values_internal (value_list_t *vl)
2126 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
2130 _Bool free_meta_data = 0;
2132 assert (vl != NULL);
2134 /* These fields are initialized by plugin_value_list_clone() if needed: */
2135 assert (vl->host[0] != 0);
2136 assert (vl->time != 0); /* The time is determined at _enqueue_ time. */
2137 assert (vl->interval != 0);
2139 if (vl->type[0] == 0 || vl->values == NULL || vl->values_len < 1)
2141 ERROR ("plugin_dispatch_values: Invalid value list "
2142 "from plugin %s.", vl->plugin);
2146 /* Free meta data only if the calling function didn't specify any. In
2147 * this case matches and targets may add some and the calling function
2148 * may not expect (and therefore free) that data. */
2149 if (vl->meta == NULL)
2152 if (list_write == NULL)
2153 c_complain_once (LOG_WARNING, &no_write_complaint,
2154 "plugin_dispatch_values: No write callback has been "
2155 "registered. Please load at least one output plugin, "
2156 "if you want the collected data to be stored.");
2158 if (data_sets == NULL)
2160 ERROR ("plugin_dispatch_values: No data sets registered. "
2161 "Could the types database be read? Check "
2162 "your `TypesDB' setting!");
2166 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
2168 char ident[6 * DATA_MAX_NAME_LEN];
2170 FORMAT_VL (ident, sizeof (ident), vl);
2171 INFO ("plugin_dispatch_values: Dataset not found: %s "
2172 "(from \"%s\"), check your types.db!",
2177 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
2179 "plugin = %s; plugin_instance = %s; "
2180 "type = %s; type_instance = %s;",
2181 CDTIME_T_TO_DOUBLE (vl->time),
2182 CDTIME_T_TO_DOUBLE (vl->interval),
2184 vl->plugin, vl->plugin_instance,
2185 vl->type, vl->type_instance);
2188 assert (0 == strcmp (ds->type, vl->type));
2190 if (0 != strcmp (ds->type, vl->type))
2191 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
2192 ds->type, vl->type);
2196 assert (ds->ds_num == vl->values_len);
2198 if (ds->ds_num != vl->values_len)
2200 ERROR ("plugin_dispatch_values: ds->type = %s: "
2201 "(ds->ds_num = %zu) != "
2202 "(vl->values_len = %zu)",
2203 ds->type, ds->ds_num, vl->values_len);
2208 escape_slashes (vl->host, sizeof (vl->host));
2209 escape_slashes (vl->plugin, sizeof (vl->plugin));
2210 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
2211 escape_slashes (vl->type, sizeof (vl->type));
2212 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
2214 if (pre_cache_chain != NULL)
2216 status = fc_process_chain (ds, vl, pre_cache_chain);
2219 WARNING ("plugin_dispatch_values: Running the "
2220 "pre-cache chain failed with "
2224 else if (status == FC_TARGET_STOP)
2228 /* Update the value cache */
2231 if (post_cache_chain != NULL)
2233 status = fc_process_chain (ds, vl, post_cache_chain);
2236 WARNING ("plugin_dispatch_values: Running the "
2237 "post-cache chain failed with "
2243 fc_default_action (ds, vl);
2245 if ((free_meta_data != 0) && (vl->meta != NULL))
2247 meta_data_destroy (vl->meta);
2252 } /* int plugin_dispatch_values_internal */
2254 static double get_drop_probability (void) /* {{{ */
2260 pthread_mutex_lock (&write_lock);
2261 wql = write_queue_length;
2262 pthread_mutex_unlock (&write_lock);
2264 if (wql < write_limit_low)
2266 if (wql >= write_limit_high)
2269 pos = 1 + wql - write_limit_low;
2270 size = 1 + write_limit_high - write_limit_low;
2272 return (((double) pos) / ((double) size));
2273 } /* }}} double get_drop_probability */
2275 static _Bool check_drop_value (void) /* {{{ */
2277 static cdtime_t last_message_time = 0;
2278 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2284 if (write_limit_high == 0)
2287 p = get_drop_probability ();
2291 status = pthread_mutex_trylock (&last_message_lock);
2297 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2299 last_message_time = now;
2300 ERROR ("plugin_dispatch_values: Low water mark "
2301 "reached. Dropping %.0f%% of metrics.",
2304 pthread_mutex_unlock (&last_message_lock);
2315 } /* }}} _Bool check_drop_value */
2317 int plugin_dispatch_values (value_list_t const *vl)
2320 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2322 if (check_drop_value ()) {
2323 if(record_statistics) {
2324 pthread_mutex_lock(&statistics_lock);
2325 stats_values_dropped++;
2326 pthread_mutex_unlock(&statistics_lock);
2331 status = plugin_write_enqueue (vl);
2335 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2336 "with status %i (%s).", status,
2337 sstrerror (status, errbuf, sizeof (errbuf)));
2344 __attribute__((sentinel))
2345 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2346 _Bool store_percentage, int store_type, ...)
2353 assert (template->values_len == 1);
2355 /* Calculate sum for Gauge to calculate percent if needed */
2356 if (DS_TYPE_GAUGE == store_type) {
2357 va_start (ap, store_type);
2363 name = va_arg (ap, char const *);
2367 value = va_arg (ap, gauge_t);
2375 vl = plugin_value_list_clone (template);
2376 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2377 if (store_percentage)
2378 sstrncpy (vl->type, "percent", sizeof (vl->type));
2380 va_start (ap, store_type);
2386 /* Set the type instance. */
2387 name = va_arg (ap, char const *);
2390 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2392 /* Set the value. */
2396 vl->values[0].gauge = va_arg (ap, gauge_t);
2397 if (store_percentage)
2398 vl->values[0].gauge *= sum ? (100.0 / sum) : NAN;
2400 case DS_TYPE_ABSOLUTE:
2401 vl->values[0].absolute = va_arg (ap, absolute_t);
2403 case DS_TYPE_COUNTER:
2404 vl->values[0].counter = va_arg (ap, counter_t);
2406 case DS_TYPE_DERIVE:
2407 vl->values[0].derive = va_arg (ap, derive_t);
2410 ERROR ("plugin_dispatch_multivalue: given store_type is incorrect.");
2415 status = plugin_write_enqueue (vl);
2421 plugin_value_list_free (vl);
2423 } /* }}} int plugin_dispatch_multivalue */
2425 int plugin_dispatch_notification (const notification_t *notif)
2428 /* Possible TODO: Add flap detection here */
2430 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2431 "time = %.3f; host = %s;",
2432 notif->severity, notif->message,
2433 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2435 /* Nobody cares for notifications */
2436 if (list_notification == NULL)
2439 le = llist_head (list_notification);
2442 callback_func_t *cf;
2443 plugin_notification_cb callback;
2446 /* do not switch plugin context; rather keep the context
2447 * (interval) information of the calling plugin */
2450 callback = cf->cf_callback;
2451 status = (*callback) (notif, &cf->cf_udata);
2454 WARNING ("plugin_dispatch_notification: Notification "
2455 "callback %s returned %i.",
2463 } /* int plugin_dispatch_notification */
2465 void plugin_log (int level, const char *format, ...)
2472 if (level >= LOG_DEBUG)
2476 va_start (ap, format);
2477 vsnprintf (msg, sizeof (msg), format, ap);
2478 msg[sizeof (msg) - 1] = '\0';
2481 if (list_log == NULL)
2483 fprintf (stderr, "%s\n", msg);
2487 le = llist_head (list_log);
2490 callback_func_t *cf;
2491 plugin_log_cb callback;
2494 callback = cf->cf_callback;
2496 /* do not switch plugin context; rather keep the context
2497 * (interval) information of the calling plugin */
2499 (*callback) (level, msg, &cf->cf_udata);
2503 } /* void plugin_log */
2505 int parse_log_severity (const char *severity)
2509 if ((0 == strcasecmp (severity, "emerg"))
2510 || (0 == strcasecmp (severity, "alert"))
2511 || (0 == strcasecmp (severity, "crit"))
2512 || (0 == strcasecmp (severity, "err")))
2513 log_level = LOG_ERR;
2514 else if (0 == strcasecmp (severity, "warning"))
2515 log_level = LOG_WARNING;
2516 else if (0 == strcasecmp (severity, "notice"))
2517 log_level = LOG_NOTICE;
2518 else if (0 == strcasecmp (severity, "info"))
2519 log_level = LOG_INFO;
2521 else if (0 == strcasecmp (severity, "debug"))
2522 log_level = LOG_DEBUG;
2523 #endif /* COLLECT_DEBUG */
2526 } /* int parse_log_severity */
2528 int parse_notif_severity (const char *severity)
2530 int notif_severity = -1;
2532 if (strcasecmp (severity, "FAILURE") == 0)
2533 notif_severity = NOTIF_FAILURE;
2534 else if (strcmp (severity, "OKAY") == 0)
2535 notif_severity = NOTIF_OKAY;
2536 else if ((strcmp (severity, "WARNING") == 0)
2537 || (strcmp (severity, "WARN") == 0))
2538 notif_severity = NOTIF_WARNING;
2540 return (notif_severity);
2541 } /* int parse_notif_severity */
2543 const data_set_t *plugin_get_ds (const char *name)
2547 if (data_sets == NULL)
2549 ERROR ("plugin_get_ds: No data sets are defined yet.");
2553 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2555 DEBUG ("No such dataset registered: %s", name);
2560 } /* data_set_t *plugin_get_ds */
2562 static int plugin_notification_meta_add (notification_t *n,
2564 enum notification_meta_type_e type,
2567 notification_meta_t *meta;
2568 notification_meta_t *tail;
2570 if ((n == NULL) || (name == NULL) || (value == NULL))
2572 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2576 meta = calloc (1, sizeof (*meta));
2579 ERROR ("plugin_notification_meta_add: calloc failed.");
2583 sstrncpy (meta->name, name, sizeof (meta->name));
2588 case NM_TYPE_STRING:
2590 meta->nm_value.nm_string = strdup ((const char *) value);
2591 if (meta->nm_value.nm_string == NULL)
2593 ERROR ("plugin_notification_meta_add: strdup failed.");
2599 case NM_TYPE_SIGNED_INT:
2601 meta->nm_value.nm_signed_int = *((int64_t *) value);
2604 case NM_TYPE_UNSIGNED_INT:
2606 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2609 case NM_TYPE_DOUBLE:
2611 meta->nm_value.nm_double = *((double *) value);
2614 case NM_TYPE_BOOLEAN:
2616 meta->nm_value.nm_boolean = *((_Bool *) value);
2621 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2625 } /* switch (type) */
2629 while ((tail != NULL) && (tail->next != NULL))
2638 } /* int plugin_notification_meta_add */
2640 int plugin_notification_meta_add_string (notification_t *n,
2644 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2647 int plugin_notification_meta_add_signed_int (notification_t *n,
2651 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2654 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2658 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2661 int plugin_notification_meta_add_double (notification_t *n,
2665 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2668 int plugin_notification_meta_add_boolean (notification_t *n,
2672 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2675 int plugin_notification_meta_copy (notification_t *dst,
2676 const notification_t *src)
2678 assert (dst != NULL);
2679 assert (src != NULL);
2680 assert (dst != src);
2681 assert ((src->meta == NULL) || (src->meta != dst->meta));
2683 for (notification_meta_t *meta = src->meta; meta != NULL; meta = meta->next)
2685 if (meta->type == NM_TYPE_STRING)
2686 plugin_notification_meta_add_string (dst, meta->name,
2687 meta->nm_value.nm_string);
2688 else if (meta->type == NM_TYPE_SIGNED_INT)
2689 plugin_notification_meta_add_signed_int (dst, meta->name,
2690 meta->nm_value.nm_signed_int);
2691 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2692 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2693 meta->nm_value.nm_unsigned_int);
2694 else if (meta->type == NM_TYPE_DOUBLE)
2695 plugin_notification_meta_add_double (dst, meta->name,
2696 meta->nm_value.nm_double);
2697 else if (meta->type == NM_TYPE_BOOLEAN)
2698 plugin_notification_meta_add_boolean (dst, meta->name,
2699 meta->nm_value.nm_boolean);
2703 } /* int plugin_notification_meta_copy */
2705 int plugin_notification_meta_free (notification_meta_t *n)
2707 notification_meta_t *this;
2708 notification_meta_t *next;
2712 ERROR ("plugin_notification_meta_free: n == NULL!");
2717 while (this != NULL)
2721 if (this->type == NM_TYPE_STRING)
2723 /* Assign to a temporary variable to work around nm_string's const
2725 void *tmp = (void *) this->nm_value.nm_string;
2728 this->nm_value.nm_string = NULL;
2736 } /* int plugin_notification_meta_free */
2738 static void plugin_ctx_destructor (void *ctx)
2741 } /* void plugin_ctx_destructor */
2743 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2745 static plugin_ctx_t *plugin_ctx_create (void)
2749 ctx = malloc (sizeof (*ctx));
2752 ERROR ("Failed to allocate plugin context: %s",
2753 sstrerror (errno, errbuf, sizeof (errbuf)));
2758 assert (plugin_ctx_key_initialized);
2759 pthread_setspecific (plugin_ctx_key, ctx);
2760 DEBUG("Created new plugin context.");
2762 } /* int plugin_ctx_create */
2764 void plugin_init_ctx (void)
2766 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2767 plugin_ctx_key_initialized = 1;
2768 } /* void plugin_init_ctx */
2770 plugin_ctx_t plugin_get_ctx (void)
2774 assert (plugin_ctx_key_initialized);
2775 ctx = pthread_getspecific (plugin_ctx_key);
2778 ctx = plugin_ctx_create ();
2779 /* this must no happen -- exit() instead? */
2785 } /* plugin_ctx_t plugin_get_ctx */
2787 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2792 assert (plugin_ctx_key_initialized);
2793 c = pthread_getspecific (plugin_ctx_key);
2796 c = plugin_ctx_create ();
2797 /* this must no happen -- exit() instead? */
2806 } /* void plugin_set_ctx */
2808 cdtime_t plugin_get_interval (void)
2812 interval = plugin_get_ctx().interval;
2816 return cf_get_default_interval ();
2817 } /* cdtime_t plugin_get_interval */
2821 void *(*start_routine) (void *);
2825 static void *plugin_thread_start (void *arg)
2827 plugin_thread_t *plugin_thread = arg;
2829 void *(*start_routine) (void *) = plugin_thread->start_routine;
2830 void *plugin_arg = plugin_thread->arg;
2832 plugin_set_ctx (plugin_thread->ctx);
2834 sfree (plugin_thread);
2836 return start_routine (plugin_arg);
2837 } /* void *plugin_thread_start */
2839 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2840 void *(*start_routine) (void *), void *arg, char const *name)
2842 plugin_thread_t *plugin_thread;
2844 plugin_thread = malloc (sizeof (*plugin_thread));
2845 if (plugin_thread == NULL)
2848 plugin_thread->ctx = plugin_get_ctx ();
2849 plugin_thread->start_routine = start_routine;
2850 plugin_thread->arg = arg;
2852 int ret = pthread_create (thread, attr,
2853 plugin_thread_start, plugin_thread);
2856 sfree (plugin_thread);
2861 set_thread_name (*thread, name);
2864 } /* int plugin_thread_create */
2866 /* vim: set sw=8 ts=8 noet fdm=marker : */