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>
31 #include "configfile.h"
32 #include "filter_chain.h"
34 #include "utils_avltree.h"
35 #include "utils_cache.h"
36 #include "utils_complain.h"
37 #include "utils_heap.h"
38 #include "utils_llist.h"
39 #include "utils_random.h"
40 #include "utils_time.h"
47 struct callback_func_s {
52 typedef struct callback_func_s callback_func_t;
56 #define RF_REMOVE 65535
58 /* `read_func_t' "inherits" from `callback_func_t'.
59 * The `rf_super' member MUST be the first one in this structure! */
60 #define rf_callback rf_super.cf_callback
61 #define rf_udata rf_super.cf_udata
62 #define rf_ctx rf_super.cf_ctx
63 callback_func_t rf_super;
64 char rf_group[DATA_MAX_NAME_LEN];
68 cdtime_t rf_effective_interval;
69 cdtime_t rf_next_read;
71 typedef struct read_func_s read_func_t;
74 typedef struct write_queue_s write_queue_t;
75 struct write_queue_s {
81 struct flush_callback_s {
85 typedef struct flush_callback_s flush_callback_t;
90 static c_avl_tree_t *plugins_loaded = NULL;
92 static llist_t *list_init;
93 static llist_t *list_write;
94 static llist_t *list_flush;
95 static llist_t *list_missing;
96 static llist_t *list_shutdown;
97 static llist_t *list_log;
98 static llist_t *list_notification;
100 static fc_chain_t *pre_cache_chain = NULL;
101 static fc_chain_t *post_cache_chain = NULL;
103 static c_avl_tree_t *data_sets;
105 static char *plugindir = NULL;
107 #ifndef DEFAULT_MAX_READ_INTERVAL
108 #define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T(86400)
110 static c_heap_t *read_heap = NULL;
111 static llist_t *read_list;
112 static int read_loop = 1;
113 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
114 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
115 static pthread_t *read_threads = NULL;
116 static int read_threads_num = 0;
117 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
119 static write_queue_t *write_queue_head;
120 static write_queue_t *write_queue_tail;
121 static long write_queue_length = 0;
122 static _Bool write_loop = 1;
123 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
124 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
125 static pthread_t *write_threads = NULL;
126 static size_t write_threads_num = 0;
128 static pthread_key_t plugin_ctx_key;
129 static _Bool plugin_ctx_key_initialized = 0;
131 static long write_limit_high = 0;
132 static long write_limit_low = 0;
134 static derive_t stats_values_dropped = 0;
135 static _Bool record_statistics = 0;
140 static int plugin_dispatch_values_internal(value_list_t *vl);
142 static const char *plugin_get_dir(void) {
143 if (plugindir == NULL)
149 static void plugin_update_internal_statistics(void) { /* {{{ */
150 derive_t copy_write_queue_length;
151 value_list_t vl = VALUE_LIST_INIT;
154 copy_write_queue_length = write_queue_length;
156 /* Initialize `vl' */
160 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
161 sstrncpy(vl.plugin, "collectd", sizeof(vl.plugin));
163 vl.type_instance[0] = 0;
167 sstrncpy(vl.plugin_instance, "write_queue", sizeof(vl.plugin_instance));
169 /* Write queue : queue length */
170 vl.values[0].gauge = (gauge_t)copy_write_queue_length;
171 sstrncpy(vl.type, "queue_length", sizeof(vl.type));
172 vl.type_instance[0] = 0;
173 plugin_dispatch_values(&vl);
175 /* Write queue : Values dropped (queue length > low limit) */
176 vl.values[0].derive = (derive_t)stats_values_dropped;
177 sstrncpy(vl.type, "derive", sizeof(vl.type));
178 sstrncpy(vl.type_instance, "dropped", sizeof(vl.type_instance));
179 plugin_dispatch_values(&vl);
182 sstrncpy(vl.plugin_instance, "cache", sizeof(vl.plugin_instance));
184 /* Cache : Nb entry in cache tree */
185 vl.values[0].gauge = (gauge_t)uc_get_size();
186 sstrncpy(vl.type, "cache_size", sizeof(vl.type));
187 vl.type_instance[0] = 0;
188 plugin_dispatch_values(&vl);
191 } /* }}} void plugin_update_internal_statistics */
193 static void destroy_callback(callback_func_t *cf) /* {{{ */
198 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL)) {
199 cf->cf_udata.free_func(cf->cf_udata.data);
200 cf->cf_udata.data = NULL;
201 cf->cf_udata.free_func = NULL;
204 } /* }}} void destroy_callback */
206 static void destroy_all_callbacks(llist_t **list) /* {{{ */
213 le = llist_head(*list);
220 destroy_callback(le->value);
226 llist_destroy(*list);
228 } /* }}} void destroy_all_callbacks */
230 static void destroy_read_heap(void) /* {{{ */
232 if (read_heap == NULL)
238 rf = c_heap_get_root(read_heap);
242 destroy_callback((callback_func_t *)rf);
245 c_heap_destroy(read_heap);
247 } /* }}} void destroy_read_heap */
249 static int register_callback(llist_t **list, /* {{{ */
250 const char *name, callback_func_t *cf) {
255 *list = llist_create();
257 ERROR("plugin: register_callback: "
258 "llist_create failed.");
259 destroy_callback(cf);
266 ERROR("plugin: register_callback: strdup failed.");
267 destroy_callback(cf);
271 le = llist_search(*list, name);
273 le = llentry_create(key, cf);
275 ERROR("plugin: register_callback: "
276 "llentry_create failed.");
278 destroy_callback(cf);
282 llist_append(*list, le);
284 callback_func_t *old_cf;
289 WARNING("plugin: register_callback: "
290 "a callback named `%s' already exists - "
291 "overwriting the old entry!",
294 destroy_callback(old_cf);
299 } /* }}} int register_callback */
301 static void log_list_callbacks(llist_t **list, /* {{{ */
302 const char *comment) {
310 n = llist_size(*list);
312 INFO("%s [none]", comment);
316 keys = calloc(n, sizeof(char *));
319 ERROR("%s: failed to allocate memory for list of callbacks", comment);
324 for (le = llist_head(*list), i = 0, len = 0; le != NULL; le = le->next, i++) {
326 len += strlen(le->key) + 6;
328 str = malloc(len + 10);
330 ERROR("%s: failed to allocate memory for list of callbacks", comment);
333 strjoin(str, len, keys, n, "', '");
334 INFO("%s ['%s']", comment, str);
338 } /* }}} void log_list_callbacks */
340 static int create_register_callback(llist_t **list, /* {{{ */
341 const char *name, void *callback,
342 user_data_t const *ud) {
345 cf = calloc(1, sizeof(*cf));
347 ERROR("plugin: create_register_callback: calloc failed.");
351 cf->cf_callback = callback;
353 cf->cf_udata.data = NULL;
354 cf->cf_udata.free_func = NULL;
359 cf->cf_ctx = plugin_get_ctx();
361 return (register_callback(list, name, cf));
362 } /* }}} int create_register_callback */
364 static int plugin_unregister(llist_t *list, const char *name) /* {{{ */
371 e = llist_search(list, name);
375 llist_remove(list, e);
378 destroy_callback(e->value);
383 } /* }}} int plugin_unregister */
386 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
387 * object, but it will bitch about a shared object not having a
388 * ``module_register'' symbol..
390 static int plugin_load_file(char *file, uint32_t flags) {
392 void (*reg_handle)(void);
395 lt_dlerror(); /* clear errors */
397 #if LIBTOOL_VERSION == 2
398 if (flags & PLUGIN_FLAGS_GLOBAL) {
400 lt_dladvise_init(&advise);
401 lt_dladvise_global(&advise);
402 dlh = lt_dlopenadvise(file, advise);
403 lt_dladvise_destroy(&advise);
405 dlh = lt_dlopen(file);
407 #else /* if LIBTOOL_VERSION == 1 */
408 if (flags & PLUGIN_FLAGS_GLOBAL)
409 WARNING("plugin_load_file: The global flag is not supported, "
410 "libtool 2 is required for this.");
411 dlh = lt_dlopen(file);
415 char errbuf[1024] = "";
417 ssnprintf(errbuf, sizeof(errbuf),
418 "lt_dlopen (\"%s\") failed: %s. "
419 "The most common cause for this problem is "
420 "missing dependencies. Use ldd(1) to check "
421 "the dependencies of the plugin "
426 /* Make sure this is printed to STDERR in any case, but also
427 * make sure it's printed only once. */
428 if (list_log != NULL)
429 fprintf(stderr, "ERROR: %s\n", errbuf);
434 if ((reg_handle = (void (*)(void))lt_dlsym(dlh, "module_register")) == NULL) {
435 WARNING("Couldn't find symbol \"module_register\" in \"%s\": %s\n", file,
446 static void *plugin_read_thread(void __attribute__((unused)) * args) {
447 while (read_loop != 0) {
449 plugin_ctx_t old_ctx;
457 /* Get the read function that needs to be read next.
458 * We don't need to hold "read_lock" for the heap, but we need
459 * to call c_heap_get_root() and pthread_cond_wait() in the
460 * same protected block. */
461 pthread_mutex_lock(&read_lock);
462 rf = c_heap_get_root(read_heap);
464 pthread_cond_wait(&read_cond, &read_lock);
465 pthread_mutex_unlock(&read_lock);
468 pthread_mutex_unlock(&read_lock);
470 if (rf->rf_interval == 0) {
471 /* this should not happen, because the interval is set
472 * for each plugin when loading it
473 * XXX: issue a warning? */
474 rf->rf_interval = plugin_get_interval();
475 rf->rf_effective_interval = rf->rf_interval;
477 rf->rf_next_read = cdtime();
480 /* sleep until this entry is due,
481 * using pthread_cond_timedwait */
482 pthread_mutex_lock(&read_lock);
483 /* In pthread_cond_timedwait, spurious wakeups are possible
484 * (and really happen, at least on NetBSD with > 1 CPU), thus
485 * we need to re-evaluate the condition every time
486 * pthread_cond_timedwait returns. */
488 while ((read_loop != 0) && (cdtime() < rf->rf_next_read) && rc == 0) {
489 struct timespec ts = {0};
491 CDTIME_T_TO_TIMESPEC(rf->rf_next_read, &ts);
493 rc = pthread_cond_timedwait(&read_cond, &read_lock, &ts);
496 /* Must hold `read_lock' when accessing `rf->rf_type'. */
497 rf_type = rf->rf_type;
498 pthread_mutex_unlock(&read_lock);
500 /* Check if we're supposed to stop.. This may have interrupted
502 if (read_loop == 0) {
503 /* Insert `rf' again, so it can be free'd correctly */
504 c_heap_insert(read_heap, rf);
508 /* The entry has been marked for deletion. The linked list
509 * entry has already been removed by `plugin_unregister_read'.
510 * All we have to do here is free the `read_func_t' and
512 if (rf_type == RF_REMOVE) {
513 DEBUG("plugin_read_thread: Destroying the `%s' "
517 destroy_callback((callback_func_t *)rf);
522 DEBUG("plugin_read_thread: Handling `%s'.", rf->rf_name);
526 old_ctx = plugin_set_ctx(rf->rf_ctx);
528 if (rf_type == RF_SIMPLE) {
529 int (*callback)(void);
531 callback = rf->rf_callback;
532 status = (*callback)();
534 plugin_read_cb callback;
536 assert(rf_type == RF_COMPLEX);
538 callback = rf->rf_callback;
539 status = (*callback)(&rf->rf_udata);
542 plugin_set_ctx(old_ctx);
544 /* If the function signals failure, we will increase the
545 * intervals in which it will be called. */
547 rf->rf_effective_interval *= 2;
548 if (rf->rf_effective_interval > max_read_interval)
549 rf->rf_effective_interval = max_read_interval;
551 NOTICE("read-function of plugin `%s' failed. "
552 "Will suspend it for %.3f seconds.",
553 rf->rf_name, CDTIME_T_TO_DOUBLE(rf->rf_effective_interval));
555 /* Success: Restore the interval, if it was changed. */
556 rf->rf_effective_interval = rf->rf_interval;
559 /* update the ``next read due'' field */
562 /* calculate the time spent in the read function */
563 elapsed = (now - start);
565 if (elapsed > rf->rf_effective_interval)
567 "plugin_read_thread: read-function of the `%s' plugin took %.3f "
568 "seconds, which is above its read interval (%.3f seconds). You might "
569 "want to adjust the `Interval' or `ReadThreads' settings.",
570 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed),
571 CDTIME_T_TO_DOUBLE(rf->rf_effective_interval));
573 DEBUG("plugin_read_thread: read-function of the `%s' plugin took "
575 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed));
577 DEBUG("plugin_read_thread: Effective interval of the "
578 "`%s' plugin is %.3f seconds.",
579 rf->rf_name, CDTIME_T_TO_DOUBLE(rf->rf_effective_interval));
581 /* Calculate the next (absolute) time at which this function
582 * should be called. */
583 rf->rf_next_read += rf->rf_effective_interval;
585 /* Check, if `rf_next_read' is in the past. */
586 if (rf->rf_next_read < now) {
587 /* `rf_next_read' is in the past. Insert `now'
588 * so this value doesn't trail off into the
590 rf->rf_next_read = now;
593 DEBUG("plugin_read_thread: Next read of the `%s' plugin at %.3f.",
594 rf->rf_name, CDTIME_T_TO_DOUBLE(rf->rf_next_read));
596 /* Re-insert this read function into the heap again. */
597 c_heap_insert(read_heap, rf);
598 } /* while (read_loop) */
602 } /* void *plugin_read_thread */
604 static void start_read_threads(int num) {
605 if (read_threads != NULL)
608 read_threads = (pthread_t *)calloc(num, sizeof(pthread_t));
609 if (read_threads == NULL) {
610 ERROR("plugin: start_read_threads: calloc failed.");
614 read_threads_num = 0;
615 for (int i = 0; i < num; i++) {
616 if (pthread_create(read_threads + read_threads_num, NULL,
617 plugin_read_thread, NULL) == 0) {
620 ERROR("plugin: start_read_threads: pthread_create failed.");
624 } /* void start_read_threads */
626 static void stop_read_threads(void) {
627 if (read_threads == NULL)
630 INFO("collectd: Stopping %i read threads.", read_threads_num);
632 pthread_mutex_lock(&read_lock);
634 DEBUG("plugin: stop_read_threads: Signalling `read_cond'");
635 pthread_cond_broadcast(&read_cond);
636 pthread_mutex_unlock(&read_lock);
638 for (int i = 0; i < read_threads_num; i++) {
639 if (pthread_join(read_threads[i], NULL) != 0) {
640 ERROR("plugin: stop_read_threads: pthread_join failed.");
642 read_threads[i] = (pthread_t)0;
645 read_threads_num = 0;
646 } /* void stop_read_threads */
648 static void plugin_value_list_free(value_list_t *vl) /* {{{ */
653 meta_data_destroy(vl->meta);
656 } /* }}} void plugin_value_list_free */
658 static value_list_t *
659 plugin_value_list_clone(value_list_t const *vl_orig) /* {{{ */
666 vl = malloc(sizeof(*vl));
669 memcpy(vl, vl_orig, sizeof(*vl));
671 vl->values = calloc(vl_orig->values_len, sizeof(*vl->values));
672 if (vl->values == NULL) {
673 plugin_value_list_free(vl);
676 memcpy(vl->values, vl_orig->values,
677 vl_orig->values_len * sizeof(*vl->values));
679 vl->meta = meta_data_clone(vl->meta);
680 if ((vl_orig->meta != NULL) && (vl->meta == NULL)) {
681 plugin_value_list_free(vl);
688 /* Fill in the interval from the thread context, if it is zero. */
689 if (vl->interval == 0) {
690 plugin_ctx_t ctx = plugin_get_ctx();
692 if (ctx.interval != 0)
693 vl->interval = ctx.interval;
695 char name[6 * DATA_MAX_NAME_LEN];
696 FORMAT_VL(name, sizeof(name), vl);
697 ERROR("plugin_value_list_clone: Unable to determine "
698 "interval from context for "
699 "value list \"%s\". "
700 "This indicates a broken plugin. "
701 "Please report this problem to the "
702 "collectd mailing list or at "
703 "<http://collectd.org/bugs/>.",
705 vl->interval = cf_get_default_interval();
710 } /* }}} value_list_t *plugin_value_list_clone */
712 static int plugin_write_enqueue(value_list_t const *vl) /* {{{ */
716 q = malloc(sizeof(*q));
721 q->vl = plugin_value_list_clone(vl);
727 /* Store context of caller (read plugin); otherwise, it would not be
728 * available to the write plugins when actually dispatching the
729 * value-list later on. */
730 q->ctx = plugin_get_ctx();
732 pthread_mutex_lock(&write_lock);
734 if (write_queue_tail == NULL) {
735 write_queue_head = q;
736 write_queue_tail = q;
737 write_queue_length = 1;
739 write_queue_tail->next = q;
740 write_queue_tail = q;
741 write_queue_length += 1;
744 pthread_cond_signal(&write_cond);
745 pthread_mutex_unlock(&write_lock);
748 } /* }}} int plugin_write_enqueue */
750 static value_list_t *plugin_write_dequeue(void) /* {{{ */
755 pthread_mutex_lock(&write_lock);
757 while (write_loop && (write_queue_head == NULL))
758 pthread_cond_wait(&write_cond, &write_lock);
760 if (write_queue_head == NULL) {
761 pthread_mutex_unlock(&write_lock);
765 q = write_queue_head;
766 write_queue_head = q->next;
767 write_queue_length -= 1;
768 if (write_queue_head == NULL) {
769 write_queue_tail = NULL;
770 assert(0 == write_queue_length);
773 pthread_mutex_unlock(&write_lock);
775 (void)plugin_set_ctx(q->ctx);
780 } /* }}} value_list_t *plugin_write_dequeue */
782 static void *plugin_write_thread(void __attribute__((unused)) * args) /* {{{ */
785 value_list_t *vl = plugin_write_dequeue();
789 plugin_dispatch_values_internal(vl);
791 plugin_value_list_free(vl);
796 } /* }}} void *plugin_write_thread */
798 static void start_write_threads(size_t num) /* {{{ */
800 if (write_threads != NULL)
803 write_threads = (pthread_t *)calloc(num, sizeof(pthread_t));
804 if (write_threads == NULL) {
805 ERROR("plugin: start_write_threads: calloc failed.");
809 write_threads_num = 0;
810 for (size_t i = 0; i < num; i++) {
813 status = pthread_create(write_threads + write_threads_num,
814 /* attr = */ NULL, plugin_write_thread,
818 ERROR("plugin: start_write_threads: pthread_create failed "
819 "with status %i (%s).",
820 status, sstrerror(status, errbuf, sizeof(errbuf)));
826 } /* }}} void start_write_threads */
828 static void stop_write_threads(void) /* {{{ */
833 if (write_threads == NULL)
836 INFO("collectd: Stopping %zu write threads.", write_threads_num);
838 pthread_mutex_lock(&write_lock);
840 DEBUG("plugin: stop_write_threads: Signalling `write_cond'");
841 pthread_cond_broadcast(&write_cond);
842 pthread_mutex_unlock(&write_lock);
844 for (i = 0; i < write_threads_num; i++) {
845 if (pthread_join(write_threads[i], NULL) != 0) {
846 ERROR("plugin: stop_write_threads: pthread_join failed.");
848 write_threads[i] = (pthread_t)0;
850 sfree(write_threads);
851 write_threads_num = 0;
853 pthread_mutex_lock(&write_lock);
855 for (q = write_queue_head; q != NULL;) {
856 write_queue_t *q1 = q;
857 plugin_value_list_free(q->vl);
862 write_queue_head = NULL;
863 write_queue_tail = NULL;
864 write_queue_length = 0;
865 pthread_mutex_unlock(&write_lock);
868 WARNING("plugin: %zu value list%s left after shutting down "
869 "the write threads.",
870 i, (i == 1) ? " was" : "s were");
872 } /* }}} void stop_write_threads */
877 void plugin_set_dir(const char *dir) {
885 plugindir = strdup(dir);
886 if (plugindir == NULL)
887 ERROR("plugin_set_dir: strdup(\"%s\") failed", dir);
890 static _Bool plugin_is_loaded(char const *name) {
893 if (plugins_loaded == NULL)
895 c_avl_create((int (*)(const void *, const void *))strcasecmp);
896 assert(plugins_loaded != NULL);
898 status = c_avl_get(plugins_loaded, name, /* ret_value = */ NULL);
899 return (status == 0);
902 static int plugin_mark_loaded(char const *name) {
906 name_copy = strdup(name);
907 if (name_copy == NULL)
910 status = c_avl_insert(plugins_loaded,
911 /* key = */ name_copy, /* value = */ NULL);
915 static void plugin_free_loaded(void) {
919 if (plugins_loaded == NULL)
922 while (c_avl_pick(plugins_loaded, &key, &value) == 0) {
924 assert(value == NULL);
927 c_avl_destroy(plugins_loaded);
928 plugins_loaded = NULL;
932 int plugin_load(char const *plugin_name, uint32_t flags) {
935 char filename[BUFSIZE] = "";
936 char typename[BUFSIZE];
942 if (plugin_name == NULL)
945 /* Check if plugin is already loaded and don't do anything in this
947 if (plugin_is_loaded(plugin_name))
950 dir = plugin_get_dir();
954 * XXX: Magic at work:
956 * Some of the language bindings, for example the Python and Perl
957 * plugins, need to be able to export symbols to the scripts they run.
958 * For this to happen, the "Globals" flag needs to be set.
959 * Unfortunately, this technical detail is hard to explain to the
960 * average user and she shouldn't have to worry about this, ideally.
961 * So in order to save everyone's sanity use a different default for a
962 * handful of special plugins. --octo
964 if ((strcasecmp("perl", plugin_name) == 0) ||
965 (strcasecmp("python", plugin_name) == 0))
966 flags |= PLUGIN_FLAGS_GLOBAL;
968 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
969 * type when matching the filename */
970 status = ssnprintf(typename, sizeof(typename), "%s.so", plugin_name);
971 if ((status < 0) || ((size_t)status >= sizeof(typename))) {
972 WARNING("plugin_load: Filename too long: \"%s.so\"", plugin_name);
976 if ((dh = opendir(dir)) == NULL) {
978 ERROR("plugin_load: opendir (%s) failed: %s", dir,
979 sstrerror(errno, errbuf, sizeof(errbuf)));
983 while ((de = readdir(dh)) != NULL) {
984 if (strcasecmp(de->d_name, typename))
987 status = ssnprintf(filename, sizeof(filename), "%s/%s", dir, de->d_name);
988 if ((status < 0) || ((size_t)status >= sizeof(filename))) {
989 WARNING("plugin_load: Filename too long: \"%s/%s\"", dir, de->d_name);
993 if (lstat(filename, &statbuf) == -1) {
995 WARNING("plugin_load: stat (\"%s\") failed: %s", filename,
996 sstrerror(errno, errbuf, sizeof(errbuf)));
998 } else if (!S_ISREG(statbuf.st_mode)) {
999 /* don't follow symlinks */
1000 WARNING("plugin_load: %s is not a regular file.", filename);
1004 status = plugin_load_file(filename, flags);
1007 plugin_mark_loaded(plugin_name);
1009 INFO("plugin_load: plugin \"%s\" successfully loaded.", plugin_name);
1012 ERROR("plugin_load: Load plugin \"%s\" failed with "
1014 plugin_name, status);
1020 if (filename[0] == 0)
1021 ERROR("plugin_load: Could not find plugin \"%s\" in %s", plugin_name, dir);
1027 * The `register_*' functions follow
1029 int plugin_register_config(const char *name,
1030 int (*callback)(const char *key, const char *val),
1031 const char **keys, int keys_num) {
1032 cf_register(name, callback, keys, keys_num);
1034 } /* int plugin_register_config */
1036 int plugin_register_complex_config(const char *type,
1037 int (*callback)(oconfig_item_t *)) {
1038 return (cf_register_complex(type, callback));
1039 } /* int plugin_register_complex_config */
1041 int plugin_register_init(const char *name, int (*callback)(void)) {
1042 return (create_register_callback(&list_init, name, (void *)callback,
1043 /* user_data = */ NULL));
1044 } /* plugin_register_init */
1046 static int plugin_compare_read_func(const void *arg0, const void *arg1) {
1047 const read_func_t *rf0;
1048 const read_func_t *rf1;
1053 if (rf0->rf_next_read < rf1->rf_next_read)
1055 else if (rf0->rf_next_read > rf1->rf_next_read)
1059 } /* int plugin_compare_read_func */
1061 /* Add a read function to both, the heap and a linked list. The linked list if
1062 * used to look-up read functions, especially for the remove function. The heap
1063 * is used to determine which plugin to read next. */
1064 static int plugin_insert_read(read_func_t *rf) {
1068 rf->rf_next_read = cdtime();
1069 rf->rf_effective_interval = rf->rf_interval;
1071 pthread_mutex_lock(&read_lock);
1073 if (read_list == NULL) {
1074 read_list = llist_create();
1075 if (read_list == NULL) {
1076 pthread_mutex_unlock(&read_lock);
1077 ERROR("plugin_insert_read: read_list failed.");
1082 if (read_heap == NULL) {
1083 read_heap = c_heap_create(plugin_compare_read_func);
1084 if (read_heap == NULL) {
1085 pthread_mutex_unlock(&read_lock);
1086 ERROR("plugin_insert_read: c_heap_create failed.");
1091 le = llist_search(read_list, rf->rf_name);
1093 pthread_mutex_unlock(&read_lock);
1094 WARNING("The read function \"%s\" is already registered. "
1095 "Check for duplicate \"LoadPlugin\" lines "
1096 "in your configuration!",
1101 le = llentry_create(rf->rf_name, rf);
1103 pthread_mutex_unlock(&read_lock);
1104 ERROR("plugin_insert_read: llentry_create failed.");
1108 status = c_heap_insert(read_heap, rf);
1110 pthread_mutex_unlock(&read_lock);
1111 ERROR("plugin_insert_read: c_heap_insert failed.");
1112 llentry_destroy(le);
1116 /* This does not fail. */
1117 llist_append(read_list, le);
1119 /* Wake up all the read threads. */
1120 pthread_cond_broadcast(&read_cond);
1121 pthread_mutex_unlock(&read_lock);
1123 } /* int plugin_insert_read */
1125 int plugin_register_read(const char *name, int (*callback)(void)) {
1129 rf = calloc(1, sizeof(*rf));
1131 ERROR("plugin_register_read: calloc failed.");
1135 rf->rf_callback = (void *)callback;
1136 rf->rf_udata.data = NULL;
1137 rf->rf_udata.free_func = NULL;
1138 rf->rf_ctx = plugin_get_ctx();
1139 rf->rf_group[0] = '\0';
1140 rf->rf_name = strdup(name);
1141 rf->rf_type = RF_SIMPLE;
1142 rf->rf_interval = plugin_get_interval();
1144 status = plugin_insert_read(rf);
1151 } /* int plugin_register_read */
1153 int plugin_register_complex_read(const char *group, const char *name,
1154 plugin_read_cb callback, cdtime_t interval,
1155 user_data_t const *user_data) {
1159 rf = calloc(1, sizeof(*rf));
1161 ERROR("plugin_register_complex_read: calloc failed.");
1165 rf->rf_callback = (void *)callback;
1167 sstrncpy(rf->rf_group, group, sizeof(rf->rf_group));
1169 rf->rf_group[0] = '\0';
1170 rf->rf_name = strdup(name);
1171 rf->rf_type = RF_COMPLEX;
1172 rf->rf_interval = (interval != 0) ? interval : plugin_get_interval();
1175 if (user_data == NULL) {
1176 rf->rf_udata.data = NULL;
1177 rf->rf_udata.free_func = NULL;
1179 rf->rf_udata = *user_data;
1182 rf->rf_ctx = plugin_get_ctx();
1184 status = plugin_insert_read(rf);
1191 } /* int plugin_register_complex_read */
1193 int plugin_register_write(const char *name, plugin_write_cb callback,
1194 user_data_t const *ud) {
1195 return (create_register_callback(&list_write, name, (void *)callback, ud));
1196 } /* int plugin_register_write */
1198 static int plugin_flush_timeout_callback(user_data_t *ud) {
1199 flush_callback_t *cb = ud->data;
1201 return plugin_flush(cb->name, cb->timeout, /* identifier = */ NULL);
1202 } /* static int plugin_flush_callback */
1204 static void plugin_flush_timeout_callback_free(void *data) {
1205 flush_callback_t *cb = data;
1212 } /* static void plugin_flush_callback_free */
1214 static char *plugin_flush_callback_name(const char *name) {
1215 const char *flush_prefix = "flush/";
1220 prefix_size = strlen(flush_prefix);
1221 name_size = strlen(name);
1223 flush_name = malloc(name_size + prefix_size + 1);
1224 if (flush_name == NULL) {
1225 ERROR("plugin_flush_callback_name: malloc failed.");
1229 sstrncpy(flush_name, flush_prefix, prefix_size + 1);
1230 sstrncpy(flush_name + prefix_size, name, name_size + 1);
1233 } /* static char *plugin_flush_callback_name */
1235 int plugin_register_flush(const char *name, plugin_flush_cb callback,
1236 user_data_t const *ud) {
1238 plugin_ctx_t ctx = plugin_get_ctx();
1240 status = create_register_callback(&list_flush, name, (void *)callback, ud);
1244 if (ctx.flush_interval != 0) {
1246 flush_callback_t *cb;
1248 flush_name = plugin_flush_callback_name(name);
1249 if (flush_name == NULL)
1252 cb = malloc(sizeof(*cb));
1254 ERROR("plugin_register_flush: malloc failed.");
1259 cb->name = strdup(name);
1260 if (cb->name == NULL) {
1261 ERROR("plugin_register_flush: strdup failed.");
1266 cb->timeout = ctx.flush_timeout;
1268 status = plugin_register_complex_read(
1269 /* group = */ "flush",
1270 /* name = */ flush_name,
1271 /* callback = */ plugin_flush_timeout_callback,
1272 /* interval = */ ctx.flush_interval,
1273 /* user data = */ &(user_data_t){
1274 .data = cb, .free_func = plugin_flush_timeout_callback_free,
1286 } /* int plugin_register_flush */
1288 int plugin_register_missing(const char *name, plugin_missing_cb callback,
1289 user_data_t const *ud) {
1290 return (create_register_callback(&list_missing, name, (void *)callback, ud));
1291 } /* int plugin_register_missing */
1293 int plugin_register_shutdown(const char *name, int (*callback)(void)) {
1294 return (create_register_callback(&list_shutdown, name, (void *)callback,
1295 /* user_data = */ NULL));
1296 } /* int plugin_register_shutdown */
1298 static void plugin_free_data_sets(void) {
1302 if (data_sets == NULL)
1305 while (c_avl_pick(data_sets, &key, &value) == 0) {
1306 data_set_t *ds = value;
1307 /* key is a pointer to ds->type */
1313 c_avl_destroy(data_sets);
1315 } /* void plugin_free_data_sets */
1317 int plugin_register_data_set(const data_set_t *ds) {
1318 data_set_t *ds_copy;
1320 if ((data_sets != NULL) && (c_avl_get(data_sets, ds->type, NULL) == 0)) {
1321 NOTICE("Replacing DS `%s' with another version.", ds->type);
1322 plugin_unregister_data_set(ds->type);
1323 } else if (data_sets == NULL) {
1324 data_sets = c_avl_create((int (*)(const void *, const void *))strcmp);
1325 if (data_sets == NULL)
1329 ds_copy = malloc(sizeof(*ds_copy));
1330 if (ds_copy == NULL)
1332 memcpy(ds_copy, ds, sizeof(data_set_t));
1334 ds_copy->ds = malloc(sizeof(*ds_copy->ds) * ds->ds_num);
1335 if (ds_copy->ds == NULL) {
1340 for (size_t i = 0; i < ds->ds_num; i++)
1341 memcpy(ds_copy->ds + i, ds->ds + i, sizeof(data_source_t));
1343 return (c_avl_insert(data_sets, (void *)ds_copy->type, (void *)ds_copy));
1344 } /* int plugin_register_data_set */
1346 int plugin_register_log(const char *name, plugin_log_cb callback,
1347 user_data_t const *ud) {
1348 return (create_register_callback(&list_log, name, (void *)callback, ud));
1349 } /* int plugin_register_log */
1351 int plugin_register_notification(const char *name,
1352 plugin_notification_cb callback,
1353 user_data_t const *ud) {
1355 create_register_callback(&list_notification, name, (void *)callback, ud));
1356 } /* int plugin_register_log */
1358 int plugin_unregister_config(const char *name) {
1359 cf_unregister(name);
1361 } /* int plugin_unregister_config */
1363 int plugin_unregister_complex_config(const char *name) {
1364 cf_unregister_complex(name);
1366 } /* int plugin_unregister_complex_config */
1368 int plugin_unregister_init(const char *name) {
1369 return (plugin_unregister(list_init, name));
1372 int plugin_unregister_read(const char *name) /* {{{ */
1380 pthread_mutex_lock(&read_lock);
1382 if (read_list == NULL) {
1383 pthread_mutex_unlock(&read_lock);
1387 le = llist_search(read_list, name);
1389 pthread_mutex_unlock(&read_lock);
1390 WARNING("plugin_unregister_read: No such read function: %s", name);
1394 llist_remove(read_list, le);
1398 rf->rf_type = RF_REMOVE;
1400 pthread_mutex_unlock(&read_lock);
1402 llentry_destroy(le);
1404 DEBUG("plugin_unregister_read: Marked `%s' for removal.", name);
1407 } /* }}} int plugin_unregister_read */
1409 void plugin_log_available_writers(void) {
1410 log_list_callbacks(&list_write, "Available write targets:");
1413 static int compare_read_func_group(llentry_t *e, void *ud) /* {{{ */
1415 read_func_t *rf = e->value;
1418 return strcmp(rf->rf_group, (const char *)group);
1419 } /* }}} int compare_read_func_group */
1421 int plugin_unregister_read_group(const char *group) /* {{{ */
1431 pthread_mutex_lock(&read_lock);
1433 if (read_list == NULL) {
1434 pthread_mutex_unlock(&read_lock);
1439 le = llist_search_custom(read_list, compare_read_func_group, (void *)group);
1446 llist_remove(read_list, le);
1450 rf->rf_type = RF_REMOVE;
1452 llentry_destroy(le);
1454 DEBUG("plugin_unregister_read_group: "
1455 "Marked `%s' (group `%s') for removal.",
1456 rf->rf_name, group);
1459 pthread_mutex_unlock(&read_lock);
1462 WARNING("plugin_unregister_read_group: No such "
1463 "group of read function: %s",
1469 } /* }}} int plugin_unregister_read_group */
1471 int plugin_unregister_write(const char *name) {
1472 return (plugin_unregister(list_write, name));
1475 int plugin_unregister_flush(const char *name) {
1476 plugin_ctx_t ctx = plugin_get_ctx();
1478 if (ctx.flush_interval != 0) {
1481 flush_name = plugin_flush_callback_name(name);
1482 if (flush_name != NULL) {
1483 plugin_unregister_read(flush_name);
1488 return plugin_unregister(list_flush, name);
1491 int plugin_unregister_missing(const char *name) {
1492 return (plugin_unregister(list_missing, name));
1495 int plugin_unregister_shutdown(const char *name) {
1496 return (plugin_unregister(list_shutdown, name));
1499 int plugin_unregister_data_set(const char *name) {
1502 if (data_sets == NULL)
1505 if (c_avl_remove(data_sets, name, NULL, (void *)&ds) != 0)
1512 } /* int plugin_unregister_data_set */
1514 int plugin_unregister_log(const char *name) {
1515 return (plugin_unregister(list_log, name));
1518 int plugin_unregister_notification(const char *name) {
1519 return (plugin_unregister(list_notification, name));
1522 int plugin_init_all(void) {
1523 char const *chain_name;
1528 /* Init the value cache */
1531 if (IS_TRUE(global_option_get("CollectInternalStats")))
1532 record_statistics = 1;
1534 chain_name = global_option_get("PreCacheChain");
1535 pre_cache_chain = fc_chain_get_by_name(chain_name);
1537 chain_name = global_option_get("PostCacheChain");
1538 post_cache_chain = fc_chain_get_by_name(chain_name);
1540 write_limit_high = global_option_get_long("WriteQueueLimitHigh",
1542 if (write_limit_high < 0) {
1543 ERROR("WriteQueueLimitHigh must be positive or zero.");
1544 write_limit_high = 0;
1548 global_option_get_long("WriteQueueLimitLow",
1549 /* default = */ write_limit_high / 2);
1550 if (write_limit_low < 0) {
1551 ERROR("WriteQueueLimitLow must be positive or zero.");
1552 write_limit_low = write_limit_high / 2;
1553 } else if (write_limit_low > write_limit_high) {
1554 ERROR("WriteQueueLimitLow must not be larger than "
1555 "WriteQueueLimitHigh.");
1556 write_limit_low = write_limit_high;
1559 write_threads_num = global_option_get_long("WriteThreads",
1561 if (write_threads_num < 1) {
1562 ERROR("WriteThreads must be positive.");
1563 write_threads_num = 5;
1566 if ((list_init == NULL) && (read_heap == NULL))
1569 /* Calling all init callbacks before checking if read callbacks
1570 * are available allows the init callbacks to register the read
1572 le = llist_head(list_init);
1573 while (le != NULL) {
1574 callback_func_t *cf;
1575 plugin_init_cb callback;
1576 plugin_ctx_t old_ctx;
1579 old_ctx = plugin_set_ctx(cf->cf_ctx);
1580 callback = cf->cf_callback;
1581 status = (*callback)();
1582 plugin_set_ctx(old_ctx);
1585 ERROR("Initialization of plugin `%s' "
1586 "failed with status %i. "
1587 "Plugin will be unloaded.",
1589 /* Plugins that register read callbacks from the init
1590 * callback should take care of appropriate error
1591 * handling themselves. */
1592 /* FIXME: Unload _all_ functions */
1593 plugin_unregister_read(le->key);
1600 start_write_threads((size_t)write_threads_num);
1603 global_option_get_time("MaxReadInterval", DEFAULT_MAX_READ_INTERVAL);
1605 /* Start read-threads */
1606 if (read_heap != NULL) {
1610 rt = global_option_get("ReadThreads");
1613 start_read_threads((num > 0) ? num : 5);
1616 } /* void plugin_init_all */
1618 /* TODO: Rename this function. */
1619 void plugin_read_all(void) {
1620 if (record_statistics) {
1621 plugin_update_internal_statistics();
1626 } /* void plugin_read_all */
1628 /* Read function called when the `-T' command line argument is given. */
1629 int plugin_read_all_once(void) {
1631 int return_status = 0;
1633 if (read_heap == NULL) {
1634 NOTICE("No read-functions are registered.");
1640 plugin_ctx_t old_ctx;
1642 rf = c_heap_get_root(read_heap);
1646 old_ctx = plugin_set_ctx(rf->rf_ctx);
1648 if (rf->rf_type == RF_SIMPLE) {
1649 int (*callback)(void);
1651 callback = rf->rf_callback;
1652 status = (*callback)();
1654 plugin_read_cb callback;
1656 callback = rf->rf_callback;
1657 status = (*callback)(&rf->rf_udata);
1660 plugin_set_ctx(old_ctx);
1663 NOTICE("read-function of plugin `%s' failed.", rf->rf_name);
1668 destroy_callback((void *)rf);
1671 return (return_status);
1672 } /* int plugin_read_all_once */
1674 int plugin_write(const char *plugin, /* {{{ */
1675 const data_set_t *ds, const value_list_t *vl) {
1682 if (list_write == NULL)
1686 ds = plugin_get_ds(vl->type);
1688 ERROR("plugin_write: Unable to lookup type `%s'.", vl->type);
1693 if (plugin == NULL) {
1697 le = llist_head(list_write);
1698 while (le != NULL) {
1699 callback_func_t *cf = le->value;
1700 plugin_write_cb callback;
1702 /* do not switch plugin context; rather keep the context (interval)
1703 * information of the calling read plugin */
1705 DEBUG("plugin: plugin_write: Writing values via %s.", le->key);
1706 callback = cf->cf_callback;
1707 status = (*callback)(ds, vl, &cf->cf_udata);
1716 if ((success == 0) && (failure != 0))
1720 } else /* plugin != NULL */
1722 callback_func_t *cf;
1723 plugin_write_cb callback;
1725 le = llist_head(list_write);
1726 while (le != NULL) {
1727 if (strcasecmp(plugin, le->key) == 0)
1738 /* do not switch plugin context; rather keep the context (interval)
1739 * information of the calling read plugin */
1741 DEBUG("plugin: plugin_write: Writing values via %s.", le->key);
1742 callback = cf->cf_callback;
1743 status = (*callback)(ds, vl, &cf->cf_udata);
1747 } /* }}} int plugin_write */
1749 int plugin_flush(const char *plugin, cdtime_t timeout, const char *identifier) {
1752 if (list_flush == NULL)
1755 le = llist_head(list_flush);
1756 while (le != NULL) {
1757 callback_func_t *cf;
1758 plugin_flush_cb callback;
1759 plugin_ctx_t old_ctx;
1761 if ((plugin != NULL) && (strcmp(plugin, le->key) != 0)) {
1767 old_ctx = plugin_set_ctx(cf->cf_ctx);
1768 callback = cf->cf_callback;
1770 (*callback)(timeout, identifier, &cf->cf_udata);
1772 plugin_set_ctx(old_ctx);
1777 } /* int plugin_flush */
1779 int plugin_shutdown_all(void) {
1781 int ret = 0; // Assume success.
1783 destroy_all_callbacks(&list_init);
1785 stop_read_threads();
1787 pthread_mutex_lock(&read_lock);
1788 llist_destroy(read_list);
1790 pthread_mutex_unlock(&read_lock);
1792 destroy_read_heap();
1794 /* blocks until all write threads have shut down. */
1795 stop_write_threads();
1797 /* ask all plugins to write out the state they kept. */
1798 plugin_flush(/* plugin = */ NULL,
1800 /* identifier = */ NULL);
1803 if (list_shutdown != NULL)
1804 le = llist_head(list_shutdown);
1806 while (le != NULL) {
1807 callback_func_t *cf;
1808 plugin_shutdown_cb callback;
1809 plugin_ctx_t old_ctx;
1812 old_ctx = plugin_set_ctx(cf->cf_ctx);
1813 callback = cf->cf_callback;
1815 /* Advance the pointer before calling the callback allows
1816 * shutdown functions to unregister themselves. If done the
1817 * other way around the memory `le' points to will be freed
1818 * after callback returns. */
1821 if ((*callback)() != 0)
1824 plugin_set_ctx(old_ctx);
1827 /* Write plugins which use the `user_data' pointer usually need the
1828 * same data available to the flush callback. If this is the case, set
1829 * the free_function to NULL when registering the flush callback and to
1830 * the real free function when registering the write callback. This way
1831 * the data isn't freed twice. */
1832 destroy_all_callbacks(&list_flush);
1833 destroy_all_callbacks(&list_missing);
1834 destroy_all_callbacks(&list_write);
1836 destroy_all_callbacks(&list_notification);
1837 destroy_all_callbacks(&list_shutdown);
1838 destroy_all_callbacks(&list_log);
1840 plugin_free_loaded();
1841 plugin_free_data_sets();
1843 } /* void plugin_shutdown_all */
1845 int plugin_dispatch_missing(const value_list_t *vl) /* {{{ */
1849 if (list_missing == NULL)
1852 le = llist_head(list_missing);
1853 while (le != NULL) {
1854 callback_func_t *cf;
1855 plugin_missing_cb callback;
1856 plugin_ctx_t old_ctx;
1860 old_ctx = plugin_set_ctx(cf->cf_ctx);
1861 callback = cf->cf_callback;
1863 status = (*callback)(vl, &cf->cf_udata);
1864 plugin_set_ctx(old_ctx);
1867 ERROR("plugin_dispatch_missing: Callback function \"%s\" "
1868 "failed with status %i.",
1879 } /* int }}} plugin_dispatch_missing */
1881 static int plugin_dispatch_values_internal(value_list_t *vl) {
1883 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1885 value_t *saved_values;
1886 int saved_values_len;
1890 int free_meta_data = 0;
1895 if (vl->type[0] == 0 || vl->values == NULL || vl->values_len < 1) {
1896 ERROR("plugin_dispatch_values: Invalid value list "
1902 /* Free meta data only if the calling function didn't specify any. In
1903 * this case matches and targets may add some and the calling function
1904 * may not expect (and therefore free) that data. */
1905 if (vl->meta == NULL)
1908 if (list_write == NULL)
1909 c_complain_once(LOG_WARNING, &no_write_complaint,
1910 "plugin_dispatch_values: No write callback has been "
1911 "registered. Please load at least one output plugin, "
1912 "if you want the collected data to be stored.");
1914 if (data_sets == NULL) {
1915 ERROR("plugin_dispatch_values: No data sets registered. "
1916 "Could the types database be read? Check "
1917 "your `TypesDB' setting!");
1921 if (c_avl_get(data_sets, vl->type, (void *)&ds) != 0) {
1922 char ident[6 * DATA_MAX_NAME_LEN];
1924 FORMAT_VL(ident, sizeof(ident), vl);
1925 INFO("plugin_dispatch_values: Dataset not found: %s "
1926 "(from \"%s\"), check your types.db!",
1931 /* Assured by plugin_value_list_clone(). The time is determined at
1932 * _enqueue_ time. */
1933 assert(vl->time != 0);
1934 assert(vl->interval != 0);
1936 DEBUG("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1938 "plugin = %s; plugin_instance = %s; "
1939 "type = %s; type_instance = %s;",
1940 CDTIME_T_TO_DOUBLE(vl->time), CDTIME_T_TO_DOUBLE(vl->interval),
1941 vl->host, vl->plugin, vl->plugin_instance, vl->type, vl->type_instance);
1944 assert(0 == strcmp(ds->type, vl->type));
1946 if (0 != strcmp(ds->type, vl->type))
1947 WARNING("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1948 ds->type, vl->type);
1952 assert(ds->ds_num == vl->values_len);
1954 if (ds->ds_num != vl->values_len) {
1955 ERROR("plugin_dispatch_values: ds->type = %s: "
1956 "(ds->ds_num = %zu) != "
1957 "(vl->values_len = %zu)",
1958 ds->type, ds->ds_num, vl->values_len);
1963 escape_slashes(vl->host, sizeof(vl->host));
1964 escape_slashes(vl->plugin, sizeof(vl->plugin));
1965 escape_slashes(vl->plugin_instance, sizeof(vl->plugin_instance));
1966 escape_slashes(vl->type, sizeof(vl->type));
1967 escape_slashes(vl->type_instance, sizeof(vl->type_instance));
1969 /* Copy the values. This way, we can assure `targets' that they get
1970 * dynamically allocated values, which they can free and replace if
1972 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL)) {
1973 saved_values = vl->values;
1974 saved_values_len = vl->values_len;
1976 vl->values = (value_t *)calloc(vl->values_len, sizeof(*vl->values));
1977 if (vl->values == NULL) {
1978 ERROR("plugin_dispatch_values: calloc failed.");
1979 vl->values = saved_values;
1982 memcpy(vl->values, saved_values, vl->values_len * sizeof(*vl->values));
1983 } else /* if ((pre == NULL) && (post == NULL)) */
1985 saved_values = NULL;
1986 saved_values_len = 0;
1989 if (pre_cache_chain != NULL) {
1990 status = fc_process_chain(ds, vl, pre_cache_chain);
1992 WARNING("plugin_dispatch_values: Running the "
1993 "pre-cache chain failed with "
1996 } else if (status == FC_TARGET_STOP) {
1997 /* Restore the state of the value_list so that plugins
1998 * don't get confused.. */
1999 if (saved_values != NULL) {
2001 vl->values = saved_values;
2002 vl->values_len = saved_values_len;
2008 /* Update the value cache */
2011 if (post_cache_chain != NULL) {
2012 status = fc_process_chain(ds, vl, post_cache_chain);
2014 WARNING("plugin_dispatch_values: Running the "
2015 "post-cache chain failed with "
2020 fc_default_action(ds, vl);
2022 /* Restore the state of the value_list so that plugins don't get
2024 if (saved_values != NULL) {
2026 vl->values = saved_values;
2027 vl->values_len = saved_values_len;
2030 if ((free_meta_data != 0) && (vl->meta != NULL)) {
2031 meta_data_destroy(vl->meta);
2036 } /* int plugin_dispatch_values_internal */
2038 static double get_drop_probability(void) /* {{{ */
2044 pthread_mutex_lock(&write_lock);
2045 wql = write_queue_length;
2046 pthread_mutex_unlock(&write_lock);
2048 if (wql < write_limit_low)
2050 if (wql >= write_limit_high)
2053 pos = 1 + wql - write_limit_low;
2054 size = 1 + write_limit_high - write_limit_low;
2056 return (((double)pos) / ((double)size));
2057 } /* }}} double get_drop_probability */
2059 static _Bool check_drop_value(void) /* {{{ */
2061 static cdtime_t last_message_time = 0;
2062 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2068 if (write_limit_high == 0)
2071 p = get_drop_probability();
2075 status = pthread_mutex_trylock(&last_message_lock);
2080 if ((now - last_message_time) > TIME_T_TO_CDTIME_T(1)) {
2081 last_message_time = now;
2082 ERROR("plugin_dispatch_values: Low water mark "
2083 "reached. Dropping %.0f%% of metrics.",
2086 pthread_mutex_unlock(&last_message_lock);
2097 } /* }}} _Bool check_drop_value */
2099 int plugin_dispatch_values(value_list_t const *vl) {
2101 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2103 if (check_drop_value()) {
2104 if (record_statistics) {
2105 pthread_mutex_lock(&statistics_lock);
2106 stats_values_dropped++;
2107 pthread_mutex_unlock(&statistics_lock);
2112 status = plugin_write_enqueue(vl);
2115 ERROR("plugin_dispatch_values: plugin_write_enqueue failed "
2116 "with status %i (%s).",
2117 status, sstrerror(status, errbuf, sizeof(errbuf)));
2124 __attribute__((sentinel)) int
2125 plugin_dispatch_multivalue(value_list_t const *template, /* {{{ */
2126 _Bool store_percentage, int store_type, ...) {
2132 assert(template->values_len == 1);
2134 /* Calculate sum for Gauge to calculate percent if needed */
2135 if (DS_TYPE_GAUGE == store_type) {
2136 va_start(ap, store_type);
2141 name = va_arg(ap, char const *);
2145 value = va_arg(ap, gauge_t);
2152 vl = plugin_value_list_clone(template);
2153 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2154 if (store_percentage)
2155 sstrncpy(vl->type, "percent", sizeof(vl->type));
2157 va_start(ap, store_type);
2162 /* Set the type instance. */
2163 name = va_arg(ap, char const *);
2166 sstrncpy(vl->type_instance, name, sizeof(vl->type_instance));
2168 /* Set the value. */
2169 switch (store_type) {
2171 vl->values[0].gauge = va_arg(ap, gauge_t);
2172 if (store_percentage)
2173 vl->values[0].gauge *= sum ? (100.0 / sum) : NAN;
2175 case DS_TYPE_ABSOLUTE:
2176 vl->values[0].absolute = va_arg(ap, absolute_t);
2178 case DS_TYPE_COUNTER:
2179 vl->values[0].counter = va_arg(ap, counter_t);
2181 case DS_TYPE_DERIVE:
2182 vl->values[0].derive = va_arg(ap, derive_t);
2185 ERROR("plugin_dispatch_multivalue: given store_type is incorrect.");
2189 status = plugin_write_enqueue(vl);
2195 plugin_value_list_free(vl);
2197 } /* }}} int plugin_dispatch_multivalue */
2199 int plugin_dispatch_notification(const notification_t *notif) {
2201 /* Possible TODO: Add flap detection here */
2203 DEBUG("plugin_dispatch_notification: severity = %i; message = %s; "
2204 "time = %.3f; host = %s;",
2205 notif->severity, notif->message, CDTIME_T_TO_DOUBLE(notif->time),
2208 /* Nobody cares for notifications */
2209 if (list_notification == NULL)
2212 le = llist_head(list_notification);
2213 while (le != NULL) {
2214 callback_func_t *cf;
2215 plugin_notification_cb callback;
2218 /* do not switch plugin context; rather keep the context
2219 * (interval) information of the calling plugin */
2222 callback = cf->cf_callback;
2223 status = (*callback)(notif, &cf->cf_udata);
2225 WARNING("plugin_dispatch_notification: Notification "
2226 "callback %s returned %i.",
2234 } /* int plugin_dispatch_notification */
2236 void plugin_log(int level, const char *format, ...) {
2242 if (level >= LOG_DEBUG)
2246 va_start(ap, format);
2247 vsnprintf(msg, sizeof(msg), format, ap);
2248 msg[sizeof(msg) - 1] = '\0';
2251 if (list_log == NULL) {
2252 fprintf(stderr, "%s\n", msg);
2256 le = llist_head(list_log);
2257 while (le != NULL) {
2258 callback_func_t *cf;
2259 plugin_log_cb callback;
2262 callback = cf->cf_callback;
2264 /* do not switch plugin context; rather keep the context
2265 * (interval) information of the calling plugin */
2267 (*callback)(level, msg, &cf->cf_udata);
2271 } /* void plugin_log */
2273 int parse_log_severity(const char *severity) {
2276 if ((0 == strcasecmp(severity, "emerg")) ||
2277 (0 == strcasecmp(severity, "alert")) ||
2278 (0 == strcasecmp(severity, "crit")) || (0 == strcasecmp(severity, "err")))
2279 log_level = LOG_ERR;
2280 else if (0 == strcasecmp(severity, "warning"))
2281 log_level = LOG_WARNING;
2282 else if (0 == strcasecmp(severity, "notice"))
2283 log_level = LOG_NOTICE;
2284 else if (0 == strcasecmp(severity, "info"))
2285 log_level = LOG_INFO;
2287 else if (0 == strcasecmp(severity, "debug"))
2288 log_level = LOG_DEBUG;
2289 #endif /* COLLECT_DEBUG */
2292 } /* int parse_log_severity */
2294 int parse_notif_severity(const char *severity) {
2295 int notif_severity = -1;
2297 if (strcasecmp(severity, "FAILURE") == 0)
2298 notif_severity = NOTIF_FAILURE;
2299 else if (strcmp(severity, "OKAY") == 0)
2300 notif_severity = NOTIF_OKAY;
2301 else if ((strcmp(severity, "WARNING") == 0) ||
2302 (strcmp(severity, "WARN") == 0))
2303 notif_severity = NOTIF_WARNING;
2305 return (notif_severity);
2306 } /* int parse_notif_severity */
2308 const data_set_t *plugin_get_ds(const char *name) {
2311 if (data_sets == NULL) {
2312 ERROR("plugin_get_ds: No data sets are defined yet.");
2316 if (c_avl_get(data_sets, name, (void *)&ds) != 0) {
2317 DEBUG("No such dataset registered: %s", name);
2322 } /* data_set_t *plugin_get_ds */
2324 static int plugin_notification_meta_add(notification_t *n, const char *name,
2325 enum notification_meta_type_e type,
2326 const void *value) {
2327 notification_meta_t *meta;
2328 notification_meta_t *tail;
2330 if ((n == NULL) || (name == NULL) || (value == NULL)) {
2331 ERROR("plugin_notification_meta_add: A pointer is NULL!");
2335 meta = calloc(1, sizeof(*meta));
2337 ERROR("plugin_notification_meta_add: calloc failed.");
2341 sstrncpy(meta->name, name, sizeof(meta->name));
2345 case NM_TYPE_STRING: {
2346 meta->nm_value.nm_string = strdup((const char *)value);
2347 if (meta->nm_value.nm_string == NULL) {
2348 ERROR("plugin_notification_meta_add: strdup failed.");
2354 case NM_TYPE_SIGNED_INT: {
2355 meta->nm_value.nm_signed_int = *((int64_t *)value);
2358 case NM_TYPE_UNSIGNED_INT: {
2359 meta->nm_value.nm_unsigned_int = *((uint64_t *)value);
2362 case NM_TYPE_DOUBLE: {
2363 meta->nm_value.nm_double = *((double *)value);
2366 case NM_TYPE_BOOLEAN: {
2367 meta->nm_value.nm_boolean = *((_Bool *)value);
2371 ERROR("plugin_notification_meta_add: Unknown type: %i", type);
2375 } /* switch (type) */
2379 while ((tail != NULL) && (tail->next != NULL))
2388 } /* int plugin_notification_meta_add */
2390 int plugin_notification_meta_add_string(notification_t *n, const char *name,
2391 const char *value) {
2392 return (plugin_notification_meta_add(n, name, NM_TYPE_STRING, value));
2395 int plugin_notification_meta_add_signed_int(notification_t *n, const char *name,
2397 return (plugin_notification_meta_add(n, name, NM_TYPE_SIGNED_INT, &value));
2400 int plugin_notification_meta_add_unsigned_int(notification_t *n,
2403 return (plugin_notification_meta_add(n, name, NM_TYPE_UNSIGNED_INT, &value));
2406 int plugin_notification_meta_add_double(notification_t *n, const char *name,
2408 return (plugin_notification_meta_add(n, name, NM_TYPE_DOUBLE, &value));
2411 int plugin_notification_meta_add_boolean(notification_t *n, const char *name,
2413 return (plugin_notification_meta_add(n, name, NM_TYPE_BOOLEAN, &value));
2416 int plugin_notification_meta_copy(notification_t *dst,
2417 const notification_t *src) {
2418 assert(dst != NULL);
2419 assert(src != NULL);
2421 assert((src->meta == NULL) || (src->meta != dst->meta));
2423 for (notification_meta_t *meta = src->meta; meta != NULL; meta = meta->next) {
2424 if (meta->type == NM_TYPE_STRING)
2425 plugin_notification_meta_add_string(dst, meta->name,
2426 meta->nm_value.nm_string);
2427 else if (meta->type == NM_TYPE_SIGNED_INT)
2428 plugin_notification_meta_add_signed_int(dst, meta->name,
2429 meta->nm_value.nm_signed_int);
2430 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2431 plugin_notification_meta_add_unsigned_int(dst, meta->name,
2432 meta->nm_value.nm_unsigned_int);
2433 else if (meta->type == NM_TYPE_DOUBLE)
2434 plugin_notification_meta_add_double(dst, meta->name,
2435 meta->nm_value.nm_double);
2436 else if (meta->type == NM_TYPE_BOOLEAN)
2437 plugin_notification_meta_add_boolean(dst, meta->name,
2438 meta->nm_value.nm_boolean);
2442 } /* int plugin_notification_meta_copy */
2444 int plugin_notification_meta_free(notification_meta_t *n) {
2445 notification_meta_t *this;
2446 notification_meta_t *next;
2449 ERROR("plugin_notification_meta_free: n == NULL!");
2454 while (this != NULL) {
2457 if (this->type == NM_TYPE_STRING) {
2458 /* Assign to a temporary variable to work around nm_string's const
2460 void *tmp = (void *)this->nm_value.nm_string;
2463 this->nm_value.nm_string = NULL;
2471 } /* int plugin_notification_meta_free */
2473 static void plugin_ctx_destructor(void *ctx) {
2475 } /* void plugin_ctx_destructor */
2477 static plugin_ctx_t ctx_init = {/* interval = */ 0};
2479 static plugin_ctx_t *plugin_ctx_create(void) {
2482 ctx = malloc(sizeof(*ctx));
2485 ERROR("Failed to allocate plugin context: %s",
2486 sstrerror(errno, errbuf, sizeof(errbuf)));
2491 assert(plugin_ctx_key_initialized);
2492 pthread_setspecific(plugin_ctx_key, ctx);
2493 DEBUG("Created new plugin context.");
2495 } /* int plugin_ctx_create */
2497 void plugin_init_ctx(void) {
2498 pthread_key_create(&plugin_ctx_key, plugin_ctx_destructor);
2499 plugin_ctx_key_initialized = 1;
2500 } /* void plugin_init_ctx */
2502 plugin_ctx_t plugin_get_ctx(void) {
2505 assert(plugin_ctx_key_initialized);
2506 ctx = pthread_getspecific(plugin_ctx_key);
2509 ctx = plugin_ctx_create();
2510 /* this must no happen -- exit() instead? */
2516 } /* plugin_ctx_t plugin_get_ctx */
2518 plugin_ctx_t plugin_set_ctx(plugin_ctx_t ctx) {
2522 assert(plugin_ctx_key_initialized);
2523 c = pthread_getspecific(plugin_ctx_key);
2526 c = plugin_ctx_create();
2527 /* this must no happen -- exit() instead? */
2536 } /* void plugin_set_ctx */
2538 cdtime_t plugin_get_interval(void) {
2541 interval = plugin_get_ctx().interval;
2545 return cf_get_default_interval();
2546 } /* cdtime_t plugin_get_interval */
2550 void *(*start_routine)(void *);
2554 static void *plugin_thread_start(void *arg) {
2555 plugin_thread_t *plugin_thread = arg;
2557 void *(*start_routine)(void *) = plugin_thread->start_routine;
2558 void *plugin_arg = plugin_thread->arg;
2560 plugin_set_ctx(plugin_thread->ctx);
2562 sfree(plugin_thread);
2564 return start_routine(plugin_arg);
2565 } /* void *plugin_thread_start */
2567 int plugin_thread_create(pthread_t *thread, const pthread_attr_t *attr,
2568 void *(*start_routine)(void *), void *arg) {
2569 plugin_thread_t *plugin_thread;
2571 plugin_thread = malloc(sizeof(*plugin_thread));
2572 if (plugin_thread == NULL)
2575 plugin_thread->ctx = plugin_get_ctx();
2576 plugin_thread->start_routine = start_routine;
2577 plugin_thread->arg = arg;
2579 return pthread_create(thread, attr, plugin_thread_start, plugin_thread);
2580 } /* int plugin_thread_create */
2582 /* vim: set sw=8 ts=8 noet fdm=marker : */