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>
32 #include "configfile.h"
33 #include "filter_chain.h"
34 #include "utils_avltree.h"
35 #include "utils_cache.h"
36 #include "utils_complain.h"
37 #include "utils_llist.h"
38 #include "utils_heap.h"
39 #include "utils_time.h"
40 #include "utils_random.h"
47 struct callback_func_s
53 typedef struct callback_func_s callback_func_t;
57 #define RF_REMOVE 65535
60 /* `read_func_t' "inherits" from `callback_func_t'.
61 * The `rf_super' member MUST be the first one in this structure! */
62 #define rf_callback rf_super.cf_callback
63 #define rf_udata rf_super.cf_udata
64 #define rf_ctx rf_super.cf_ctx
65 callback_func_t rf_super;
66 char rf_group[DATA_MAX_NAME_LEN];
70 cdtime_t rf_effective_interval;
71 cdtime_t rf_next_read;
73 typedef struct read_func_s read_func_t;
76 typedef struct write_queue_s write_queue_t;
84 struct flush_callback_s {
88 typedef struct flush_callback_s flush_callback_t;
93 static c_avl_tree_t *plugins_loaded = NULL;
95 static llist_t *list_init;
96 static llist_t *list_write;
97 static llist_t *list_flush;
98 static llist_t *list_missing;
99 static llist_t *list_shutdown;
100 static llist_t *list_log;
101 static llist_t *list_notification;
103 static fc_chain_t *pre_cache_chain = NULL;
104 static fc_chain_t *post_cache_chain = NULL;
106 static c_avl_tree_t *data_sets;
108 static char *plugindir = NULL;
110 #ifndef DEFAULT_MAX_READ_INTERVAL
111 # define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T (86400)
113 static c_heap_t *read_heap = NULL;
114 static llist_t *read_list;
115 static int read_loop = 1;
116 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
117 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
118 static pthread_t *read_threads = NULL;
119 static int read_threads_num = 0;
120 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
122 static write_queue_t *write_queue_head;
123 static write_queue_t *write_queue_tail;
124 static long write_queue_length = 0;
125 static _Bool write_loop = 1;
126 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
127 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
128 static pthread_t *write_threads = NULL;
129 static size_t write_threads_num = 0;
131 static pthread_key_t plugin_ctx_key;
132 static _Bool plugin_ctx_key_initialized = 0;
134 static long write_limit_high = 0;
135 static long write_limit_low = 0;
137 static derive_t stats_values_dropped = 0;
138 static _Bool record_statistics = 0;
143 static int plugin_dispatch_values_internal (value_list_t *vl);
145 static const char *plugin_get_dir (void)
147 if (plugindir == NULL)
153 static void plugin_update_internal_statistics (void) { /* {{{ */
154 derive_t copy_write_queue_length;
155 value_list_t vl = VALUE_LIST_INIT;
158 copy_write_queue_length = write_queue_length;
160 /* Initialize `vl' */
164 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
165 sstrncpy (vl.plugin, "collectd", sizeof (vl.plugin));
167 vl.type_instance[0] = 0;
171 sstrncpy (vl.plugin_instance, "write_queue",
172 sizeof (vl.plugin_instance));
174 /* Write queue : queue length */
175 vl.values[0].gauge = (gauge_t) 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[0].derive = (derive_t) stats_values_dropped;
182 sstrncpy (vl.type, "derive", sizeof (vl.type));
183 sstrncpy (vl.type_instance, "dropped", sizeof (vl.type_instance));
184 plugin_dispatch_values (&vl);
187 sstrncpy (vl.plugin_instance, "cache",
188 sizeof (vl.plugin_instance));
190 /* Cache : Nb entry in cache tree */
191 vl.values[0].gauge = (gauge_t) uc_get_size();
192 sstrncpy (vl.type, "cache_size", sizeof (vl.type));
193 vl.type_instance[0] = 0;
194 plugin_dispatch_values (&vl);
197 } /* }}} void plugin_update_internal_statistics */
199 static void destroy_callback (callback_func_t *cf) /* {{{ */
204 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
206 cf->cf_udata.free_func (cf->cf_udata.data);
207 cf->cf_udata.data = NULL;
208 cf->cf_udata.free_func = NULL;
211 } /* }}} void destroy_callback */
213 static void destroy_all_callbacks (llist_t **list) /* {{{ */
220 le = llist_head (*list);
228 destroy_callback (le->value);
234 llist_destroy (*list);
236 } /* }}} void destroy_all_callbacks */
238 static void destroy_read_heap (void) /* {{{ */
240 if (read_heap == NULL)
247 rf = c_heap_get_root (read_heap);
251 destroy_callback ((callback_func_t *) rf);
254 c_heap_destroy (read_heap);
256 } /* }}} void destroy_read_heap */
258 static int register_callback (llist_t **list, /* {{{ */
259 const char *name, callback_func_t *cf)
266 *list = llist_create ();
269 ERROR ("plugin: register_callback: "
270 "llist_create failed.");
271 destroy_callback (cf);
279 ERROR ("plugin: register_callback: strdup failed.");
280 destroy_callback (cf);
284 le = llist_search (*list, name);
287 le = llentry_create (key, cf);
290 ERROR ("plugin: register_callback: "
291 "llentry_create failed.");
293 destroy_callback (cf);
297 llist_append (*list, le);
301 callback_func_t *old_cf;
306 WARNING ("plugin: register_callback: "
307 "a callback named `%s' already exists - "
308 "overwriting the old entry!", name);
310 destroy_callback (old_cf);
315 } /* }}} int register_callback */
317 static void log_list_callbacks (llist_t **list, /* {{{ */
327 n = llist_size(*list);
330 INFO("%s [none]", comment);
334 keys = calloc(n, sizeof(char*));
338 ERROR("%s: failed to allocate memory for list of callbacks",
344 for (le = llist_head (*list), i = 0, len = 0;
349 len += strlen(le->key) + 6;
351 str = malloc(len + 10);
354 ERROR("%s: failed to allocate memory for list of callbacks",
360 strjoin(str, len, keys, n, "', '");
361 INFO("%s ['%s']", comment, str);
365 } /* }}} void log_list_callbacks */
367 static int create_register_callback (llist_t **list, /* {{{ */
368 const char *name, void *callback, user_data_t *ud)
372 cf = calloc (1, sizeof (*cf));
375 ERROR ("plugin: create_register_callback: calloc failed.");
379 cf->cf_callback = callback;
382 cf->cf_udata.data = NULL;
383 cf->cf_udata.free_func = NULL;
390 cf->cf_ctx = plugin_get_ctx ();
392 return (register_callback (list, name, cf));
393 } /* }}} int create_register_callback */
395 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
402 e = llist_search (list, name);
406 llist_remove (list, e);
409 destroy_callback (e->value);
414 } /* }}} int plugin_unregister */
417 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
418 * object, but it will bitch about a shared object not having a
419 * ``module_register'' symbol..
421 static int plugin_load_file (char *file, uint32_t flags)
424 void (*reg_handle) (void);
427 lt_dlerror (); /* clear errors */
429 #if LIBTOOL_VERSION == 2
430 if (flags & PLUGIN_FLAGS_GLOBAL) {
432 lt_dladvise_init(&advise);
433 lt_dladvise_global(&advise);
434 dlh = lt_dlopenadvise(file, advise);
435 lt_dladvise_destroy(&advise);
437 dlh = lt_dlopen (file);
439 #else /* if LIBTOOL_VERSION == 1 */
440 if (flags & PLUGIN_FLAGS_GLOBAL)
441 WARNING ("plugin_load_file: The global flag is not supported, "
442 "libtool 2 is required for this.");
443 dlh = lt_dlopen (file);
448 char errbuf[1024] = "";
450 ssnprintf (errbuf, sizeof (errbuf),
451 "lt_dlopen (\"%s\") failed: %s. "
452 "The most common cause for this problem is "
453 "missing dependencies. Use ldd(1) to check "
454 "the dependencies of the plugin "
456 file, lt_dlerror ());
458 ERROR ("%s", errbuf);
459 /* Make sure this is printed to STDERR in any case, but also
460 * make sure it's printed only once. */
461 if (list_log != NULL)
462 fprintf (stderr, "ERROR: %s\n", errbuf);
467 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
469 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
470 file, lt_dlerror ());
480 static void *plugin_read_thread (void __attribute__((unused)) *args)
482 while (read_loop != 0)
485 plugin_ctx_t old_ctx;
493 /* Get the read function that needs to be read next.
494 * We don't need to hold "read_lock" for the heap, but we need
495 * to call c_heap_get_root() and pthread_cond_wait() in the
496 * same protected block. */
497 pthread_mutex_lock (&read_lock);
498 rf = c_heap_get_root (read_heap);
501 pthread_cond_wait (&read_cond, &read_lock);
502 pthread_mutex_unlock (&read_lock);
505 pthread_mutex_unlock (&read_lock);
507 if (rf->rf_interval == 0)
509 /* this should not happen, because the interval is set
510 * for each plugin when loading it
511 * XXX: issue a warning? */
512 rf->rf_interval = plugin_get_interval ();
513 rf->rf_effective_interval = rf->rf_interval;
515 rf->rf_next_read = cdtime ();
518 /* sleep until this entry is due,
519 * using pthread_cond_timedwait */
520 pthread_mutex_lock (&read_lock);
521 /* In pthread_cond_timedwait, spurious wakeups are possible
522 * (and really happen, at least on NetBSD with > 1 CPU), thus
523 * we need to re-evaluate the condition every time
524 * pthread_cond_timedwait returns. */
526 while ((read_loop != 0)
527 && (cdtime () < rf->rf_next_read)
530 struct timespec ts = { 0 };
532 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
534 rc = pthread_cond_timedwait (&read_cond, &read_lock,
538 /* Must hold `read_lock' when accessing `rf->rf_type'. */
539 rf_type = rf->rf_type;
540 pthread_mutex_unlock (&read_lock);
542 /* Check if we're supposed to stop.. This may have interrupted
546 /* Insert `rf' again, so it can be free'd correctly */
547 c_heap_insert (read_heap, rf);
551 /* The entry has been marked for deletion. The linked list
552 * entry has already been removed by `plugin_unregister_read'.
553 * All we have to do here is free the `read_func_t' and
555 if (rf_type == RF_REMOVE)
557 DEBUG ("plugin_read_thread: Destroying the `%s' "
558 "callback.", rf->rf_name);
560 destroy_callback ((callback_func_t *) rf);
565 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
569 old_ctx = plugin_set_ctx (rf->rf_ctx);
571 if (rf_type == RF_SIMPLE)
573 int (*callback) (void);
575 callback = rf->rf_callback;
576 status = (*callback) ();
580 plugin_read_cb callback;
582 assert (rf_type == RF_COMPLEX);
584 callback = rf->rf_callback;
585 status = (*callback) (&rf->rf_udata);
588 plugin_set_ctx (old_ctx);
590 /* If the function signals failure, we will increase the
591 * intervals in which it will be called. */
594 rf->rf_effective_interval *= 2;
595 if (rf->rf_effective_interval > max_read_interval)
596 rf->rf_effective_interval = max_read_interval;
598 NOTICE ("read-function of plugin `%s' failed. "
599 "Will suspend it for %.3f seconds.",
601 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
605 /* Success: Restore the interval, if it was changed. */
606 rf->rf_effective_interval = rf->rf_interval;
609 /* update the ``next read due'' field */
612 /* calculate the time spent in the read function */
613 elapsed = (now - start);
615 if (elapsed > rf->rf_effective_interval)
616 WARNING ("plugin_read_thread: read-function of the `%s' plugin took %.3f "
617 "seconds, which is above its read interval (%.3f seconds). You might "
618 "want to adjust the `Interval' or `ReadThreads' settings.",
619 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed),
620 CDTIME_T_TO_DOUBLE(rf->rf_effective_interval));
622 DEBUG ("plugin_read_thread: read-function of the `%s' plugin took "
624 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed));
626 DEBUG ("plugin_read_thread: Effective interval of the "
627 "`%s' plugin is %.3f seconds.",
629 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
631 /* Calculate the next (absolute) time at which this function
632 * should be called. */
633 rf->rf_next_read += rf->rf_effective_interval;
635 /* Check, if `rf_next_read' is in the past. */
636 if (rf->rf_next_read < now)
638 /* `rf_next_read' is in the past. Insert `now'
639 * so this value doesn't trail off into the
641 rf->rf_next_read = now;
644 DEBUG ("plugin_read_thread: Next read of the `%s' plugin at %.3f.",
646 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
648 /* Re-insert this read function into the heap again. */
649 c_heap_insert (read_heap, rf);
650 } /* while (read_loop) */
654 } /* void *plugin_read_thread */
656 static void start_read_threads (int num)
660 if (read_threads != NULL)
663 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
664 if (read_threads == NULL)
666 ERROR ("plugin: start_read_threads: calloc failed.");
670 read_threads_num = 0;
671 for (i = 0; i < num; i++)
673 if (pthread_create (read_threads + read_threads_num, NULL,
674 plugin_read_thread, NULL) == 0)
680 ERROR ("plugin: start_read_threads: pthread_create failed.");
684 } /* void start_read_threads */
686 static void stop_read_threads (void)
690 if (read_threads == NULL)
693 INFO ("collectd: Stopping %i read threads.", read_threads_num);
695 pthread_mutex_lock (&read_lock);
697 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
698 pthread_cond_broadcast (&read_cond);
699 pthread_mutex_unlock (&read_lock);
701 for (i = 0; i < read_threads_num; i++)
703 if (pthread_join (read_threads[i], NULL) != 0)
705 ERROR ("plugin: stop_read_threads: pthread_join failed.");
707 read_threads[i] = (pthread_t) 0;
709 sfree (read_threads);
710 read_threads_num = 0;
711 } /* void stop_read_threads */
713 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
718 meta_data_destroy (vl->meta);
721 } /* }}} void plugin_value_list_free */
723 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
730 vl = malloc (sizeof (*vl));
733 memcpy (vl, vl_orig, sizeof (*vl));
735 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
736 if (vl->values == NULL)
738 plugin_value_list_free (vl);
741 memcpy (vl->values, vl_orig->values,
742 vl_orig->values_len * sizeof (*vl->values));
744 vl->meta = meta_data_clone (vl->meta);
745 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
747 plugin_value_list_free (vl);
752 vl->time = cdtime ();
754 /* Fill in the interval from the thread context, if it is zero. */
755 if (vl->interval == 0)
757 plugin_ctx_t ctx = plugin_get_ctx ();
759 if (ctx.interval != 0)
760 vl->interval = ctx.interval;
763 char name[6 * DATA_MAX_NAME_LEN];
764 FORMAT_VL (name, sizeof (name), vl);
765 ERROR ("plugin_value_list_clone: Unable to determine "
766 "interval from context for "
767 "value list \"%s\". "
768 "This indicates a broken plugin. "
769 "Please report this problem to the "
770 "collectd mailing list or at "
771 "<http://collectd.org/bugs/>.", name);
772 vl->interval = cf_get_default_interval ();
777 } /* }}} value_list_t *plugin_value_list_clone */
779 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
783 q = malloc (sizeof (*q));
788 q->vl = plugin_value_list_clone (vl);
795 /* Store context of caller (read plugin); otherwise, it would not be
796 * available to the write plugins when actually dispatching the
797 * value-list later on. */
798 q->ctx = plugin_get_ctx ();
800 pthread_mutex_lock (&write_lock);
802 if (write_queue_tail == NULL)
804 write_queue_head = q;
805 write_queue_tail = q;
806 write_queue_length = 1;
810 write_queue_tail->next = q;
811 write_queue_tail = q;
812 write_queue_length += 1;
815 pthread_cond_signal (&write_cond);
816 pthread_mutex_unlock (&write_lock);
819 } /* }}} int plugin_write_enqueue */
821 static value_list_t *plugin_write_dequeue (void) /* {{{ */
826 pthread_mutex_lock (&write_lock);
828 while (write_loop && (write_queue_head == NULL))
829 pthread_cond_wait (&write_cond, &write_lock);
831 if (write_queue_head == NULL)
833 pthread_mutex_unlock (&write_lock);
837 q = write_queue_head;
838 write_queue_head = q->next;
839 write_queue_length -= 1;
840 if (write_queue_head == NULL) {
841 write_queue_tail = NULL;
842 assert(0 == write_queue_length);
845 pthread_mutex_unlock (&write_lock);
847 (void) plugin_set_ctx (q->ctx);
852 } /* }}} value_list_t *plugin_write_dequeue */
854 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
858 value_list_t *vl = plugin_write_dequeue ();
862 plugin_dispatch_values_internal (vl);
864 plugin_value_list_free (vl);
869 } /* }}} void *plugin_write_thread */
871 static void start_write_threads (size_t num) /* {{{ */
875 if (write_threads != NULL)
878 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
879 if (write_threads == NULL)
881 ERROR ("plugin: start_write_threads: calloc failed.");
885 write_threads_num = 0;
886 for (i = 0; i < num; i++)
890 status = pthread_create (write_threads + write_threads_num,
897 ERROR ("plugin: start_write_threads: pthread_create failed "
898 "with status %i (%s).", status,
899 sstrerror (status, errbuf, sizeof (errbuf)));
905 } /* }}} void start_write_threads */
907 static void stop_write_threads (void) /* {{{ */
912 if (write_threads == NULL)
915 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
917 pthread_mutex_lock (&write_lock);
919 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
920 pthread_cond_broadcast (&write_cond);
921 pthread_mutex_unlock (&write_lock);
923 for (i = 0; i < write_threads_num; i++)
925 if (pthread_join (write_threads[i], NULL) != 0)
927 ERROR ("plugin: stop_write_threads: pthread_join failed.");
929 write_threads[i] = (pthread_t) 0;
931 sfree (write_threads);
932 write_threads_num = 0;
934 pthread_mutex_lock (&write_lock);
936 for (q = write_queue_head; q != NULL; )
938 write_queue_t *q1 = q;
939 plugin_value_list_free (q->vl);
944 write_queue_head = NULL;
945 write_queue_tail = NULL;
946 write_queue_length = 0;
947 pthread_mutex_unlock (&write_lock);
951 WARNING ("plugin: %zu value list%s left after shutting down "
952 "the write threads.",
953 i, (i == 1) ? " was" : "s were");
955 } /* }}} void stop_write_threads */
960 void plugin_set_dir (const char *dir)
970 plugindir = strdup (dir);
971 if (plugindir == NULL)
972 ERROR ("plugin_set_dir: strdup(\"%s\") failed", dir);
975 static _Bool plugin_is_loaded (char const *name)
979 if (plugins_loaded == NULL)
980 plugins_loaded = c_avl_create ((int (*) (const void *, const void *)) strcasecmp);
981 assert (plugins_loaded != NULL);
983 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
984 return (status == 0);
987 static int plugin_mark_loaded (char const *name)
992 name_copy = strdup (name);
993 if (name_copy == NULL)
996 status = c_avl_insert (plugins_loaded,
997 /* key = */ name_copy, /* value = */ NULL);
1001 static void plugin_free_loaded (void)
1006 if (plugins_loaded == NULL)
1009 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
1012 assert (value == NULL);
1015 c_avl_destroy (plugins_loaded);
1016 plugins_loaded = NULL;
1020 int plugin_load (char const *plugin_name, uint32_t flags)
1024 char filename[BUFSIZE] = "";
1025 char typename[BUFSIZE];
1027 struct stat statbuf;
1031 if (plugin_name == NULL)
1034 /* Check if plugin is already loaded and don't do anything in this
1036 if (plugin_is_loaded (plugin_name))
1039 dir = plugin_get_dir ();
1043 * XXX: Magic at work:
1045 * Some of the language bindings, for example the Python and Perl
1046 * plugins, need to be able to export symbols to the scripts they run.
1047 * For this to happen, the "Globals" flag needs to be set.
1048 * Unfortunately, this technical detail is hard to explain to the
1049 * average user and she shouldn't have to worry about this, ideally.
1050 * So in order to save everyone's sanity use a different default for a
1051 * handful of special plugins. --octo
1053 if ((strcasecmp ("perl", plugin_name) == 0)
1054 || (strcasecmp ("python", plugin_name) == 0))
1055 flags |= PLUGIN_FLAGS_GLOBAL;
1057 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
1058 * type when matching the filename */
1059 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
1060 if ((status < 0) || ((size_t) status >= sizeof (typename)))
1062 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
1066 if ((dh = opendir (dir)) == NULL)
1069 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
1070 sstrerror (errno, errbuf, sizeof (errbuf)));
1074 while ((de = readdir (dh)) != NULL)
1076 if (strcasecmp (de->d_name, typename))
1079 status = ssnprintf (filename, sizeof (filename),
1080 "%s/%s", dir, de->d_name);
1081 if ((status < 0) || ((size_t) status >= sizeof (filename)))
1083 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
1088 if (lstat (filename, &statbuf) == -1)
1091 WARNING ("plugin_load: stat (\"%s\") failed: %s",
1093 sstrerror (errno, errbuf, sizeof (errbuf)));
1096 else if (!S_ISREG (statbuf.st_mode))
1098 /* don't follow symlinks */
1099 WARNING ("plugin_load: %s is not a regular file.",
1104 status = plugin_load_file (filename, flags);
1108 plugin_mark_loaded (plugin_name);
1110 INFO ("plugin_load: plugin \"%s\" successfully loaded.", plugin_name);
1115 ERROR ("plugin_load: Load plugin \"%s\" failed with "
1116 "status %i.", plugin_name, status);
1122 if (filename[0] == 0)
1123 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1130 * The `register_*' functions follow
1132 int plugin_register_config (const char *name,
1133 int (*callback) (const char *key, const char *val),
1134 const char **keys, int keys_num)
1136 cf_register (name, callback, keys, keys_num);
1138 } /* int plugin_register_config */
1140 int plugin_register_complex_config (const char *type,
1141 int (*callback) (oconfig_item_t *))
1143 return (cf_register_complex (type, callback));
1144 } /* int plugin_register_complex_config */
1146 int plugin_register_init (const char *name,
1147 int (*callback) (void))
1149 return (create_register_callback (&list_init, name, (void *) callback,
1150 /* user_data = */ NULL));
1151 } /* plugin_register_init */
1153 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1155 const read_func_t *rf0;
1156 const read_func_t *rf1;
1161 if (rf0->rf_next_read < rf1->rf_next_read)
1163 else if (rf0->rf_next_read > rf1->rf_next_read)
1167 } /* int plugin_compare_read_func */
1169 /* Add a read function to both, the heap and a linked list. The linked list if
1170 * used to look-up read functions, especially for the remove function. The heap
1171 * is used to determine which plugin to read next. */
1172 static int plugin_insert_read (read_func_t *rf)
1177 rf->rf_next_read = cdtime ();
1178 rf->rf_effective_interval = rf->rf_interval;
1180 pthread_mutex_lock (&read_lock);
1182 if (read_list == NULL)
1184 read_list = llist_create ();
1185 if (read_list == NULL)
1187 pthread_mutex_unlock (&read_lock);
1188 ERROR ("plugin_insert_read: read_list failed.");
1193 if (read_heap == NULL)
1195 read_heap = c_heap_create (plugin_compare_read_func);
1196 if (read_heap == NULL)
1198 pthread_mutex_unlock (&read_lock);
1199 ERROR ("plugin_insert_read: c_heap_create failed.");
1204 le = llist_search (read_list, rf->rf_name);
1207 pthread_mutex_unlock (&read_lock);
1208 WARNING ("The read function \"%s\" is already registered. "
1209 "Check for duplicate \"LoadPlugin\" lines "
1210 "in your configuration!",
1215 le = llentry_create (rf->rf_name, rf);
1218 pthread_mutex_unlock (&read_lock);
1219 ERROR ("plugin_insert_read: llentry_create failed.");
1223 status = c_heap_insert (read_heap, rf);
1226 pthread_mutex_unlock (&read_lock);
1227 ERROR ("plugin_insert_read: c_heap_insert failed.");
1228 llentry_destroy (le);
1232 /* This does not fail. */
1233 llist_append (read_list, le);
1235 /* Wake up all the read threads. */
1236 pthread_cond_broadcast (&read_cond);
1237 pthread_mutex_unlock (&read_lock);
1239 } /* int plugin_insert_read */
1241 int plugin_register_read (const char *name,
1242 int (*callback) (void))
1247 rf = calloc (1, sizeof (*rf));
1250 ERROR ("plugin_register_read: calloc failed.");
1254 rf->rf_callback = (void *) callback;
1255 rf->rf_udata.data = NULL;
1256 rf->rf_udata.free_func = NULL;
1257 rf->rf_ctx = plugin_get_ctx ();
1258 rf->rf_group[0] = '\0';
1259 rf->rf_name = strdup (name);
1260 rf->rf_type = RF_SIMPLE;
1261 rf->rf_interval = plugin_get_interval ();
1263 status = plugin_insert_read (rf);
1265 sfree (rf->rf_name);
1270 } /* int plugin_register_read */
1272 int plugin_register_complex_read (const char *group, const char *name,
1273 plugin_read_cb callback,
1275 user_data_t *user_data)
1280 rf = calloc (1,sizeof (*rf));
1283 ERROR ("plugin_register_complex_read: calloc failed.");
1287 rf->rf_callback = (void *) callback;
1289 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1291 rf->rf_group[0] = '\0';
1292 rf->rf_name = strdup (name);
1293 rf->rf_type = RF_COMPLEX;
1294 rf->rf_interval = (interval != 0) ? interval : plugin_get_interval ();
1297 if (user_data == NULL)
1299 rf->rf_udata.data = NULL;
1300 rf->rf_udata.free_func = NULL;
1304 rf->rf_udata = *user_data;
1307 rf->rf_ctx = plugin_get_ctx ();
1309 status = plugin_insert_read (rf);
1311 sfree (rf->rf_name);
1316 } /* int plugin_register_complex_read */
1318 int plugin_register_write (const char *name,
1319 plugin_write_cb callback, user_data_t *ud)
1321 return (create_register_callback (&list_write, name,
1322 (void *) callback, ud));
1323 } /* int plugin_register_write */
1325 static int plugin_flush_timeout_callback (user_data_t *ud)
1327 flush_callback_t *cb = ud->data;
1329 return plugin_flush (cb->name, cb->timeout, /* identifier = */ NULL);
1330 } /* static int plugin_flush_callback */
1332 static void plugin_flush_timeout_callback_free (void *data)
1334 flush_callback_t *cb = data;
1336 if (cb == NULL) return;
1340 } /* static void plugin_flush_callback_free */
1342 static char *plugin_flush_callback_name (const char *name)
1344 const char *flush_prefix = "flush/";
1349 prefix_size = strlen(flush_prefix);
1350 name_size = strlen(name);
1352 flush_name = malloc (name_size + prefix_size + 1);
1353 if (flush_name == NULL)
1355 ERROR ("plugin_flush_callback_name: malloc failed.");
1359 sstrncpy (flush_name, flush_prefix, prefix_size + 1);
1360 sstrncpy (flush_name + prefix_size, name, name_size + 1);
1363 } /* static char *plugin_flush_callback_name */
1365 int plugin_register_flush (const char *name,
1366 plugin_flush_cb callback, user_data_t *ud)
1369 plugin_ctx_t ctx = plugin_get_ctx ();
1371 status = create_register_callback (&list_flush, name,
1372 (void *) callback, ud);
1376 if (ctx.flush_interval != 0)
1379 flush_callback_t *cb;
1381 flush_name = plugin_flush_callback_name (name);
1382 if (flush_name == NULL)
1385 cb = malloc(sizeof (*cb));
1388 ERROR ("plugin_register_flush: malloc failed.");
1393 cb->name = strdup (name);
1394 if (cb->name == NULL)
1396 ERROR ("plugin_register_flush: strdup failed.");
1401 cb->timeout = ctx.flush_timeout;
1404 ud->free_func = plugin_flush_timeout_callback_free;
1406 status = plugin_register_complex_read (
1407 /* group = */ "flush",
1408 /* name = */ flush_name,
1409 /* callback = */ plugin_flush_timeout_callback,
1410 /* interval = */ ctx.flush_interval,
1411 /* user data = */ ud);
1423 } /* int plugin_register_flush */
1425 int plugin_register_missing (const char *name,
1426 plugin_missing_cb callback, user_data_t *ud)
1428 return (create_register_callback (&list_missing, name,
1429 (void *) callback, ud));
1430 } /* int plugin_register_missing */
1432 int plugin_register_shutdown (const char *name,
1433 int (*callback) (void))
1435 return (create_register_callback (&list_shutdown, name,
1436 (void *) callback, /* user_data = */ NULL));
1437 } /* int plugin_register_shutdown */
1439 static void plugin_free_data_sets (void)
1444 if (data_sets == NULL)
1447 while (c_avl_pick (data_sets, &key, &value) == 0)
1449 data_set_t *ds = value;
1450 /* key is a pointer to ds->type */
1456 c_avl_destroy (data_sets);
1458 } /* void plugin_free_data_sets */
1460 int plugin_register_data_set (const data_set_t *ds)
1462 data_set_t *ds_copy;
1465 if ((data_sets != NULL)
1466 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1468 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1469 plugin_unregister_data_set (ds->type);
1471 else if (data_sets == NULL)
1473 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1474 if (data_sets == NULL)
1478 ds_copy = malloc (sizeof (*ds_copy));
1479 if (ds_copy == NULL)
1481 memcpy(ds_copy, ds, sizeof (data_set_t));
1483 ds_copy->ds = malloc (sizeof (*ds_copy->ds)
1485 if (ds_copy->ds == NULL)
1491 for (i = 0; i < ds->ds_num; i++)
1492 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1494 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1495 } /* int plugin_register_data_set */
1497 int plugin_register_log (const char *name,
1498 plugin_log_cb callback, user_data_t *ud)
1500 return (create_register_callback (&list_log, name,
1501 (void *) callback, ud));
1502 } /* int plugin_register_log */
1504 int plugin_register_notification (const char *name,
1505 plugin_notification_cb callback, user_data_t *ud)
1507 return (create_register_callback (&list_notification, name,
1508 (void *) callback, ud));
1509 } /* int plugin_register_log */
1511 int plugin_unregister_config (const char *name)
1513 cf_unregister (name);
1515 } /* int plugin_unregister_config */
1517 int plugin_unregister_complex_config (const char *name)
1519 cf_unregister_complex (name);
1521 } /* int plugin_unregister_complex_config */
1523 int plugin_unregister_init (const char *name)
1525 return (plugin_unregister (list_init, name));
1528 int plugin_unregister_read (const char *name) /* {{{ */
1536 pthread_mutex_lock (&read_lock);
1538 if (read_list == NULL)
1540 pthread_mutex_unlock (&read_lock);
1544 le = llist_search (read_list, name);
1547 pthread_mutex_unlock (&read_lock);
1548 WARNING ("plugin_unregister_read: No such read function: %s",
1553 llist_remove (read_list, le);
1556 assert (rf != NULL);
1557 rf->rf_type = RF_REMOVE;
1559 pthread_mutex_unlock (&read_lock);
1561 llentry_destroy (le);
1563 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1566 } /* }}} int plugin_unregister_read */
1568 void plugin_log_available_writers (void)
1570 log_list_callbacks (&list_write, "Available write targets:");
1573 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1575 read_func_t *rf = e->value;
1578 return strcmp (rf->rf_group, (const char *)group);
1579 } /* }}} int compare_read_func_group */
1581 int plugin_unregister_read_group (const char *group) /* {{{ */
1591 pthread_mutex_lock (&read_lock);
1593 if (read_list == NULL)
1595 pthread_mutex_unlock (&read_lock);
1601 le = llist_search_custom (read_list,
1602 compare_read_func_group, (void *)group);
1609 llist_remove (read_list, le);
1612 assert (rf != NULL);
1613 rf->rf_type = RF_REMOVE;
1615 llentry_destroy (le);
1617 DEBUG ("plugin_unregister_read_group: "
1618 "Marked `%s' (group `%s') for removal.",
1619 rf->rf_name, group);
1622 pthread_mutex_unlock (&read_lock);
1626 WARNING ("plugin_unregister_read_group: No such "
1627 "group of read function: %s", group);
1632 } /* }}} int plugin_unregister_read_group */
1634 int plugin_unregister_write (const char *name)
1636 return (plugin_unregister (list_write, name));
1639 int plugin_unregister_flush (const char *name)
1641 plugin_ctx_t ctx = plugin_get_ctx ();
1643 if (ctx.flush_interval != 0)
1647 flush_name = plugin_flush_callback_name (name);
1648 if (flush_name != NULL)
1650 plugin_unregister_read(flush_name);
1655 return plugin_unregister (list_flush, name);
1658 int plugin_unregister_missing (const char *name)
1660 return (plugin_unregister (list_missing, name));
1663 int plugin_unregister_shutdown (const char *name)
1665 return (plugin_unregister (list_shutdown, name));
1668 int plugin_unregister_data_set (const char *name)
1672 if (data_sets == NULL)
1675 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1682 } /* int plugin_unregister_data_set */
1684 int plugin_unregister_log (const char *name)
1686 return (plugin_unregister (list_log, name));
1689 int plugin_unregister_notification (const char *name)
1691 return (plugin_unregister (list_notification, name));
1694 int plugin_init_all (void)
1696 char const *chain_name;
1701 /* Init the value cache */
1704 if (IS_TRUE (global_option_get ("CollectInternalStats")))
1705 record_statistics = 1;
1707 chain_name = global_option_get ("PreCacheChain");
1708 pre_cache_chain = fc_chain_get_by_name (chain_name);
1710 chain_name = global_option_get ("PostCacheChain");
1711 post_cache_chain = fc_chain_get_by_name (chain_name);
1713 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1715 if (write_limit_high < 0)
1717 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1718 write_limit_high = 0;
1721 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1722 /* default = */ write_limit_high / 2);
1723 if (write_limit_low < 0)
1725 ERROR ("WriteQueueLimitLow must be positive or zero.");
1726 write_limit_low = write_limit_high / 2;
1728 else if (write_limit_low > write_limit_high)
1730 ERROR ("WriteQueueLimitLow must not be larger than "
1731 "WriteQueueLimitHigh.");
1732 write_limit_low = write_limit_high;
1735 write_threads_num = global_option_get_long ("WriteThreads",
1737 if (write_threads_num < 1)
1739 ERROR ("WriteThreads must be positive.");
1740 write_threads_num = 5;
1743 if ((list_init == NULL) && (read_heap == NULL))
1746 /* Calling all init callbacks before checking if read callbacks
1747 * are available allows the init callbacks to register the read
1749 le = llist_head (list_init);
1752 callback_func_t *cf;
1753 plugin_init_cb callback;
1754 plugin_ctx_t old_ctx;
1757 old_ctx = plugin_set_ctx (cf->cf_ctx);
1758 callback = cf->cf_callback;
1759 status = (*callback) ();
1760 plugin_set_ctx (old_ctx);
1764 ERROR ("Initialization of plugin `%s' "
1765 "failed with status %i. "
1766 "Plugin will be unloaded.",
1768 /* Plugins that register read callbacks from the init
1769 * callback should take care of appropriate error
1770 * handling themselves. */
1771 /* FIXME: Unload _all_ functions */
1772 plugin_unregister_read (le->key);
1779 start_write_threads ((size_t) write_threads_num);
1781 max_read_interval = global_option_get_time ("MaxReadInterval",
1782 DEFAULT_MAX_READ_INTERVAL);
1784 /* Start read-threads */
1785 if (read_heap != NULL)
1790 rt = global_option_get ("ReadThreads");
1793 start_read_threads ((num > 0) ? num : 5);
1796 } /* void plugin_init_all */
1798 /* TODO: Rename this function. */
1799 void plugin_read_all (void)
1801 if(record_statistics) {
1802 plugin_update_internal_statistics ();
1804 uc_check_timeout ();
1807 } /* void plugin_read_all */
1809 /* Read function called when the `-T' command line argument is given. */
1810 int plugin_read_all_once (void)
1813 int return_status = 0;
1815 if (read_heap == NULL)
1817 NOTICE ("No read-functions are registered.");
1824 plugin_ctx_t old_ctx;
1826 rf = c_heap_get_root (read_heap);
1830 old_ctx = plugin_set_ctx (rf->rf_ctx);
1832 if (rf->rf_type == RF_SIMPLE)
1834 int (*callback) (void);
1836 callback = rf->rf_callback;
1837 status = (*callback) ();
1841 plugin_read_cb callback;
1843 callback = rf->rf_callback;
1844 status = (*callback) (&rf->rf_udata);
1847 plugin_set_ctx (old_ctx);
1851 NOTICE ("read-function of plugin `%s' failed.",
1856 sfree (rf->rf_name);
1857 destroy_callback ((void *) rf);
1860 return (return_status);
1861 } /* int plugin_read_all_once */
1863 int plugin_write (const char *plugin, /* {{{ */
1864 const data_set_t *ds, const value_list_t *vl)
1872 if (list_write == NULL)
1877 ds = plugin_get_ds (vl->type);
1880 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1890 le = llist_head (list_write);
1893 callback_func_t *cf = le->value;
1894 plugin_write_cb callback;
1896 /* do not switch plugin context; rather keep the context (interval)
1897 * information of the calling read plugin */
1899 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1900 callback = cf->cf_callback;
1901 status = (*callback) (ds, vl, &cf->cf_udata);
1910 if ((success == 0) && (failure != 0))
1915 else /* plugin != NULL */
1917 callback_func_t *cf;
1918 plugin_write_cb callback;
1920 le = llist_head (list_write);
1923 if (strcasecmp (plugin, le->key) == 0)
1934 /* do not switch plugin context; rather keep the context (interval)
1935 * information of the calling read plugin */
1937 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1938 callback = cf->cf_callback;
1939 status = (*callback) (ds, vl, &cf->cf_udata);
1943 } /* }}} int plugin_write */
1945 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1949 if (list_flush == NULL)
1952 le = llist_head (list_flush);
1955 callback_func_t *cf;
1956 plugin_flush_cb callback;
1957 plugin_ctx_t old_ctx;
1959 if ((plugin != NULL)
1960 && (strcmp (plugin, le->key) != 0))
1967 old_ctx = plugin_set_ctx (cf->cf_ctx);
1968 callback = cf->cf_callback;
1970 (*callback) (timeout, identifier, &cf->cf_udata);
1972 plugin_set_ctx (old_ctx);
1977 } /* int plugin_flush */
1979 int plugin_shutdown_all (void)
1982 int ret = 0; // Assume success.
1984 stop_read_threads ();
1986 destroy_all_callbacks (&list_init);
1988 pthread_mutex_lock (&read_lock);
1989 llist_destroy (read_list);
1991 pthread_mutex_unlock (&read_lock);
1993 destroy_read_heap ();
1995 plugin_flush (/* plugin = */ NULL,
1997 /* identifier = */ NULL);
2000 if (list_shutdown != NULL)
2001 le = llist_head (list_shutdown);
2005 callback_func_t *cf;
2006 plugin_shutdown_cb callback;
2007 plugin_ctx_t old_ctx;
2010 old_ctx = plugin_set_ctx (cf->cf_ctx);
2011 callback = cf->cf_callback;
2013 /* Advance the pointer before calling the callback allows
2014 * shutdown functions to unregister themselves. If done the
2015 * other way around the memory `le' points to will be freed
2016 * after callback returns. */
2019 if ((*callback) () != 0)
2022 plugin_set_ctx (old_ctx);
2025 stop_write_threads ();
2027 /* Write plugins which use the `user_data' pointer usually need the
2028 * same data available to the flush callback. If this is the case, set
2029 * the free_function to NULL when registering the flush callback and to
2030 * the real free function when registering the write callback. This way
2031 * the data isn't freed twice. */
2032 destroy_all_callbacks (&list_flush);
2033 destroy_all_callbacks (&list_missing);
2034 destroy_all_callbacks (&list_write);
2036 destroy_all_callbacks (&list_notification);
2037 destroy_all_callbacks (&list_shutdown);
2038 destroy_all_callbacks (&list_log);
2040 plugin_free_loaded ();
2041 plugin_free_data_sets ();
2043 } /* void plugin_shutdown_all */
2045 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
2049 if (list_missing == NULL)
2052 le = llist_head (list_missing);
2055 callback_func_t *cf;
2056 plugin_missing_cb callback;
2057 plugin_ctx_t old_ctx;
2061 old_ctx = plugin_set_ctx (cf->cf_ctx);
2062 callback = cf->cf_callback;
2064 status = (*callback) (vl, &cf->cf_udata);
2065 plugin_set_ctx (old_ctx);
2070 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
2071 "failed with status %i.",
2084 } /* int }}} plugin_dispatch_missing */
2086 static int plugin_dispatch_values_internal (value_list_t *vl)
2089 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
2091 value_t *saved_values;
2092 int saved_values_len;
2096 int free_meta_data = 0;
2101 if (vl->type[0] == 0 || vl->values == NULL || vl->values_len < 1)
2103 ERROR ("plugin_dispatch_values: Invalid value list "
2104 "from plugin %s.", vl->plugin);
2108 /* Free meta data only if the calling function didn't specify any. In
2109 * this case matches and targets may add some and the calling function
2110 * may not expect (and therefore free) that data. */
2111 if (vl->meta == NULL)
2114 if (list_write == NULL)
2115 c_complain_once (LOG_WARNING, &no_write_complaint,
2116 "plugin_dispatch_values: No write callback has been "
2117 "registered. Please load at least one output plugin, "
2118 "if you want the collected data to be stored.");
2120 if (data_sets == NULL)
2122 ERROR ("plugin_dispatch_values: No data sets registered. "
2123 "Could the types database be read? Check "
2124 "your `TypesDB' setting!");
2128 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
2130 char ident[6 * DATA_MAX_NAME_LEN];
2132 FORMAT_VL (ident, sizeof (ident), vl);
2133 INFO ("plugin_dispatch_values: Dataset not found: %s "
2134 "(from \"%s\"), check your types.db!",
2139 /* Assured by plugin_value_list_clone(). The time is determined at
2140 * _enqueue_ time. */
2141 assert (vl->time != 0);
2142 assert (vl->interval != 0);
2144 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
2146 "plugin = %s; plugin_instance = %s; "
2147 "type = %s; type_instance = %s;",
2148 CDTIME_T_TO_DOUBLE (vl->time),
2149 CDTIME_T_TO_DOUBLE (vl->interval),
2151 vl->plugin, vl->plugin_instance,
2152 vl->type, vl->type_instance);
2155 assert (0 == strcmp (ds->type, vl->type));
2157 if (0 != strcmp (ds->type, vl->type))
2158 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
2159 ds->type, vl->type);
2163 assert (ds->ds_num == vl->values_len);
2165 if (ds->ds_num != vl->values_len)
2167 ERROR ("plugin_dispatch_values: ds->type = %s: "
2168 "(ds->ds_num = %zu) != "
2169 "(vl->values_len = %zu)",
2170 ds->type, ds->ds_num, vl->values_len);
2175 escape_slashes (vl->host, sizeof (vl->host));
2176 escape_slashes (vl->plugin, sizeof (vl->plugin));
2177 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
2178 escape_slashes (vl->type, sizeof (vl->type));
2179 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
2181 /* Copy the values. This way, we can assure `targets' that they get
2182 * dynamically allocated values, which they can free and replace if
2184 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
2186 saved_values = vl->values;
2187 saved_values_len = vl->values_len;
2189 vl->values = (value_t *) calloc (vl->values_len,
2190 sizeof (*vl->values));
2191 if (vl->values == NULL)
2193 ERROR ("plugin_dispatch_values: calloc failed.");
2194 vl->values = saved_values;
2197 memcpy (vl->values, saved_values,
2198 vl->values_len * sizeof (*vl->values));
2200 else /* if ((pre == NULL) && (post == NULL)) */
2202 saved_values = NULL;
2203 saved_values_len = 0;
2206 if (pre_cache_chain != NULL)
2208 status = fc_process_chain (ds, vl, pre_cache_chain);
2211 WARNING ("plugin_dispatch_values: Running the "
2212 "pre-cache chain failed with "
2216 else if (status == FC_TARGET_STOP)
2218 /* Restore the state of the value_list so that plugins
2219 * don't get confused.. */
2220 if (saved_values != NULL)
2223 vl->values = saved_values;
2224 vl->values_len = saved_values_len;
2230 /* Update the value cache */
2233 if (post_cache_chain != NULL)
2235 status = fc_process_chain (ds, vl, post_cache_chain);
2238 WARNING ("plugin_dispatch_values: Running the "
2239 "post-cache chain failed with "
2245 fc_default_action (ds, vl);
2247 /* Restore the state of the value_list so that plugins don't get
2249 if (saved_values != NULL)
2252 vl->values = saved_values;
2253 vl->values_len = saved_values_len;
2256 if ((free_meta_data != 0) && (vl->meta != NULL))
2258 meta_data_destroy (vl->meta);
2263 } /* int plugin_dispatch_values_internal */
2265 static double get_drop_probability (void) /* {{{ */
2271 pthread_mutex_lock (&write_lock);
2272 wql = write_queue_length;
2273 pthread_mutex_unlock (&write_lock);
2275 if (wql < write_limit_low)
2277 if (wql >= write_limit_high)
2280 pos = 1 + wql - write_limit_low;
2281 size = 1 + write_limit_high - write_limit_low;
2283 return (((double) pos) / ((double) size));
2284 } /* }}} double get_drop_probability */
2286 static _Bool check_drop_value (void) /* {{{ */
2288 static cdtime_t last_message_time = 0;
2289 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2295 if (write_limit_high == 0)
2298 p = get_drop_probability ();
2302 status = pthread_mutex_trylock (&last_message_lock);
2308 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2310 last_message_time = now;
2311 ERROR ("plugin_dispatch_values: Low water mark "
2312 "reached. Dropping %.0f%% of metrics.",
2315 pthread_mutex_unlock (&last_message_lock);
2326 } /* }}} _Bool check_drop_value */
2328 int plugin_dispatch_values (value_list_t const *vl)
2331 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2333 if (check_drop_value ()) {
2334 if(record_statistics) {
2335 pthread_mutex_lock(&statistics_lock);
2336 stats_values_dropped++;
2337 pthread_mutex_unlock(&statistics_lock);
2342 status = plugin_write_enqueue (vl);
2346 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2347 "with status %i (%s).", status,
2348 sstrerror (status, errbuf, sizeof (errbuf)));
2355 __attribute__((sentinel))
2356 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2357 _Bool store_percentage, int store_type, ...)
2364 assert (template->values_len == 1);
2366 /* Calculate sum for Gauge to calculate percent if needed */
2367 if (DS_TYPE_GAUGE == store_type) {
2368 va_start (ap, store_type);
2374 name = va_arg (ap, char const *);
2378 value = va_arg (ap, gauge_t);
2386 vl = plugin_value_list_clone (template);
2387 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2388 if (store_percentage)
2389 sstrncpy (vl->type, "percent", sizeof (vl->type));
2391 va_start (ap, store_type);
2397 /* Set the type instance. */
2398 name = va_arg (ap, char const *);
2401 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2403 /* Set the value. */
2407 vl->values[0].gauge = va_arg (ap, gauge_t);
2408 if (store_percentage)
2409 vl->values[0].gauge *= sum ? (100.0 / sum) : 0;
2411 case DS_TYPE_ABSOLUTE:
2412 vl->values[0].absolute = va_arg (ap, absolute_t);
2414 case DS_TYPE_COUNTER:
2415 vl->values[0].counter = va_arg (ap, counter_t);
2417 case DS_TYPE_DERIVE:
2418 vl->values[0].derive = va_arg (ap, derive_t);
2421 ERROR ("plugin_dispatch_multivalue: given store_type is incorrect.");
2426 status = plugin_write_enqueue (vl);
2432 plugin_value_list_free (vl);
2434 } /* }}} int plugin_dispatch_multivalue */
2436 int plugin_dispatch_notification (const notification_t *notif)
2439 /* Possible TODO: Add flap detection here */
2441 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2442 "time = %.3f; host = %s;",
2443 notif->severity, notif->message,
2444 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2446 /* Nobody cares for notifications */
2447 if (list_notification == NULL)
2450 le = llist_head (list_notification);
2453 callback_func_t *cf;
2454 plugin_notification_cb callback;
2457 /* do not switch plugin context; rather keep the context
2458 * (interval) information of the calling plugin */
2461 callback = cf->cf_callback;
2462 status = (*callback) (notif, &cf->cf_udata);
2465 WARNING ("plugin_dispatch_notification: Notification "
2466 "callback %s returned %i.",
2474 } /* int plugin_dispatch_notification */
2476 void plugin_log (int level, const char *format, ...)
2483 if (level >= LOG_DEBUG)
2487 va_start (ap, format);
2488 vsnprintf (msg, sizeof (msg), format, ap);
2489 msg[sizeof (msg) - 1] = '\0';
2492 if (list_log == NULL)
2494 fprintf (stderr, "%s\n", msg);
2498 le = llist_head (list_log);
2501 callback_func_t *cf;
2502 plugin_log_cb callback;
2505 callback = cf->cf_callback;
2507 /* do not switch plugin context; rather keep the context
2508 * (interval) information of the calling plugin */
2510 (*callback) (level, msg, &cf->cf_udata);
2514 } /* void plugin_log */
2516 int parse_log_severity (const char *severity)
2520 if ((0 == strcasecmp (severity, "emerg"))
2521 || (0 == strcasecmp (severity, "alert"))
2522 || (0 == strcasecmp (severity, "crit"))
2523 || (0 == strcasecmp (severity, "err")))
2524 log_level = LOG_ERR;
2525 else if (0 == strcasecmp (severity, "warning"))
2526 log_level = LOG_WARNING;
2527 else if (0 == strcasecmp (severity, "notice"))
2528 log_level = LOG_NOTICE;
2529 else if (0 == strcasecmp (severity, "info"))
2530 log_level = LOG_INFO;
2532 else if (0 == strcasecmp (severity, "debug"))
2533 log_level = LOG_DEBUG;
2534 #endif /* COLLECT_DEBUG */
2537 } /* int parse_log_severity */
2539 int parse_notif_severity (const char *severity)
2541 int notif_severity = -1;
2543 if (strcasecmp (severity, "FAILURE") == 0)
2544 notif_severity = NOTIF_FAILURE;
2545 else if (strcmp (severity, "OKAY") == 0)
2546 notif_severity = NOTIF_OKAY;
2547 else if ((strcmp (severity, "WARNING") == 0)
2548 || (strcmp (severity, "WARN") == 0))
2549 notif_severity = NOTIF_WARNING;
2551 return (notif_severity);
2552 } /* int parse_notif_severity */
2554 const data_set_t *plugin_get_ds (const char *name)
2558 if (data_sets == NULL)
2560 ERROR ("plugin_get_ds: No data sets are defined yet.");
2564 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2566 DEBUG ("No such dataset registered: %s", name);
2571 } /* data_set_t *plugin_get_ds */
2573 static int plugin_notification_meta_add (notification_t *n,
2575 enum notification_meta_type_e type,
2578 notification_meta_t *meta;
2579 notification_meta_t *tail;
2581 if ((n == NULL) || (name == NULL) || (value == NULL))
2583 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2587 meta = calloc (1, sizeof (*meta));
2590 ERROR ("plugin_notification_meta_add: calloc failed.");
2594 sstrncpy (meta->name, name, sizeof (meta->name));
2599 case NM_TYPE_STRING:
2601 meta->nm_value.nm_string = strdup ((const char *) value);
2602 if (meta->nm_value.nm_string == NULL)
2604 ERROR ("plugin_notification_meta_add: strdup failed.");
2610 case NM_TYPE_SIGNED_INT:
2612 meta->nm_value.nm_signed_int = *((int64_t *) value);
2615 case NM_TYPE_UNSIGNED_INT:
2617 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2620 case NM_TYPE_DOUBLE:
2622 meta->nm_value.nm_double = *((double *) value);
2625 case NM_TYPE_BOOLEAN:
2627 meta->nm_value.nm_boolean = *((_Bool *) value);
2632 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2636 } /* switch (type) */
2640 while ((tail != NULL) && (tail->next != NULL))
2649 } /* int plugin_notification_meta_add */
2651 int plugin_notification_meta_add_string (notification_t *n,
2655 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2658 int plugin_notification_meta_add_signed_int (notification_t *n,
2662 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2665 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2669 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2672 int plugin_notification_meta_add_double (notification_t *n,
2676 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2679 int plugin_notification_meta_add_boolean (notification_t *n,
2683 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2686 int plugin_notification_meta_copy (notification_t *dst,
2687 const notification_t *src)
2689 notification_meta_t *meta;
2691 assert (dst != NULL);
2692 assert (src != NULL);
2693 assert (dst != src);
2694 assert ((src->meta == NULL) || (src->meta != dst->meta));
2696 for (meta = src->meta; meta != NULL; meta = meta->next)
2698 if (meta->type == NM_TYPE_STRING)
2699 plugin_notification_meta_add_string (dst, meta->name,
2700 meta->nm_value.nm_string);
2701 else if (meta->type == NM_TYPE_SIGNED_INT)
2702 plugin_notification_meta_add_signed_int (dst, meta->name,
2703 meta->nm_value.nm_signed_int);
2704 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2705 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2706 meta->nm_value.nm_unsigned_int);
2707 else if (meta->type == NM_TYPE_DOUBLE)
2708 plugin_notification_meta_add_double (dst, meta->name,
2709 meta->nm_value.nm_double);
2710 else if (meta->type == NM_TYPE_BOOLEAN)
2711 plugin_notification_meta_add_boolean (dst, meta->name,
2712 meta->nm_value.nm_boolean);
2716 } /* int plugin_notification_meta_copy */
2718 int plugin_notification_meta_free (notification_meta_t *n)
2720 notification_meta_t *this;
2721 notification_meta_t *next;
2725 ERROR ("plugin_notification_meta_free: n == NULL!");
2730 while (this != NULL)
2734 if (this->type == NM_TYPE_STRING)
2736 /* Assign to a temporary variable to work around nm_string's const
2738 void *tmp = (void *) this->nm_value.nm_string;
2741 this->nm_value.nm_string = NULL;
2749 } /* int plugin_notification_meta_free */
2751 static void plugin_ctx_destructor (void *ctx)
2754 } /* void plugin_ctx_destructor */
2756 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2758 static plugin_ctx_t *plugin_ctx_create (void)
2762 ctx = malloc (sizeof (*ctx));
2765 ERROR ("Failed to allocate plugin context: %s",
2766 sstrerror (errno, errbuf, sizeof (errbuf)));
2771 assert (plugin_ctx_key_initialized);
2772 pthread_setspecific (plugin_ctx_key, ctx);
2773 DEBUG("Created new plugin context.");
2775 } /* int plugin_ctx_create */
2777 void plugin_init_ctx (void)
2779 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2780 plugin_ctx_key_initialized = 1;
2781 } /* void plugin_init_ctx */
2783 plugin_ctx_t plugin_get_ctx (void)
2787 assert (plugin_ctx_key_initialized);
2788 ctx = pthread_getspecific (plugin_ctx_key);
2791 ctx = plugin_ctx_create ();
2792 /* this must no happen -- exit() instead? */
2798 } /* plugin_ctx_t plugin_get_ctx */
2800 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2805 assert (plugin_ctx_key_initialized);
2806 c = pthread_getspecific (plugin_ctx_key);
2809 c = plugin_ctx_create ();
2810 /* this must no happen -- exit() instead? */
2819 } /* void plugin_set_ctx */
2821 cdtime_t plugin_get_interval (void)
2825 interval = plugin_get_ctx().interval;
2829 return cf_get_default_interval ();
2830 } /* cdtime_t plugin_get_interval */
2834 void *(*start_routine) (void *);
2838 static void *plugin_thread_start (void *arg)
2840 plugin_thread_t *plugin_thread = arg;
2842 void *(*start_routine) (void *) = plugin_thread->start_routine;
2843 void *plugin_arg = plugin_thread->arg;
2845 plugin_set_ctx (plugin_thread->ctx);
2847 sfree (plugin_thread);
2849 return start_routine (plugin_arg);
2850 } /* void *plugin_thread_start */
2852 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2853 void *(*start_routine) (void *), void *arg)
2855 plugin_thread_t *plugin_thread;
2857 plugin_thread = malloc (sizeof (*plugin_thread));
2858 if (plugin_thread == NULL)
2861 plugin_thread->ctx = plugin_get_ctx ();
2862 plugin_thread->start_routine = start_routine;
2863 plugin_thread->arg = arg;
2865 return pthread_create (thread, attr,
2866 plugin_thread_start, plugin_thread);
2867 } /* int plugin_thread_create */
2869 /* vim: set sw=8 ts=8 noet fdm=marker : */