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) { /* {{{ */
155 gauge_t copy_write_queue_length = (gauge_t) write_queue_length;
157 /* Initialize `vl' */
158 value_list_t vl = VALUE_LIST_INIT;
159 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
160 sstrncpy (vl.plugin, "collectd", sizeof (vl.plugin));
163 sstrncpy (vl.plugin_instance, "write_queue",
164 sizeof (vl.plugin_instance));
166 /* Write queue : queue length */
167 vl.values = &(value_t) { .gauge = copy_write_queue_length };
169 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
170 vl.type_instance[0] = 0;
171 plugin_dispatch_values (&vl);
173 /* Write queue : Values dropped (queue length > low limit) */
174 vl.values = &(value_t) { .gauge = (gauge_t) stats_values_dropped };
176 sstrncpy (vl.type, "derive", sizeof (vl.type));
177 sstrncpy (vl.type_instance, "dropped", sizeof (vl.type_instance));
178 plugin_dispatch_values (&vl);
181 sstrncpy (vl.plugin_instance, "cache",
182 sizeof (vl.plugin_instance));
184 /* Cache : Nb entry in cache tree */
185 vl.values = &(value_t) { .gauge = (gauge_t) uc_get_size() };
187 sstrncpy (vl.type, "cache_size", sizeof (vl.type));
188 vl.type_instance[0] = 0;
189 plugin_dispatch_values (&vl);
192 } /* }}} void plugin_update_internal_statistics */
194 static void destroy_callback (callback_func_t *cf) /* {{{ */
199 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
201 cf->cf_udata.free_func (cf->cf_udata.data);
202 cf->cf_udata.data = NULL;
203 cf->cf_udata.free_func = NULL;
206 } /* }}} void destroy_callback */
208 static void destroy_all_callbacks (llist_t **list) /* {{{ */
215 le = llist_head (*list);
223 destroy_callback (le->value);
229 llist_destroy (*list);
231 } /* }}} void destroy_all_callbacks */
233 static void destroy_read_heap (void) /* {{{ */
235 if (read_heap == NULL)
242 rf = c_heap_get_root (read_heap);
246 destroy_callback ((callback_func_t *) rf);
249 c_heap_destroy (read_heap);
251 } /* }}} void destroy_read_heap */
253 static int register_callback (llist_t **list, /* {{{ */
254 const char *name, callback_func_t *cf)
261 *list = llist_create ();
264 ERROR ("plugin: register_callback: "
265 "llist_create failed.");
266 destroy_callback (cf);
274 ERROR ("plugin: register_callback: strdup failed.");
275 destroy_callback (cf);
279 le = llist_search (*list, name);
282 le = llentry_create (key, cf);
285 ERROR ("plugin: register_callback: "
286 "llentry_create failed.");
288 destroy_callback (cf);
292 llist_append (*list, le);
296 callback_func_t *old_cf;
301 WARNING ("plugin: register_callback: "
302 "a callback named `%s' already exists - "
303 "overwriting the old entry!", name);
305 destroy_callback (old_cf);
310 } /* }}} int register_callback */
312 static void log_list_callbacks (llist_t **list, /* {{{ */
322 n = llist_size(*list);
325 INFO("%s [none]", comment);
329 keys = calloc(n, sizeof(char*));
333 ERROR("%s: failed to allocate memory for list of callbacks",
339 for (le = llist_head (*list), i = 0, len = 0;
344 len += strlen(le->key) + 6;
346 str = malloc(len + 10);
349 ERROR("%s: failed to allocate memory for list of callbacks",
355 strjoin(str, len, keys, n, "', '");
356 INFO("%s ['%s']", comment, str);
360 } /* }}} void log_list_callbacks */
362 static int create_register_callback (llist_t **list, /* {{{ */
363 const char *name, void *callback, user_data_t *ud)
367 cf = calloc (1, sizeof (*cf));
370 ERROR ("plugin: create_register_callback: calloc failed.");
374 cf->cf_callback = callback;
377 cf->cf_udata.data = NULL;
378 cf->cf_udata.free_func = NULL;
385 cf->cf_ctx = plugin_get_ctx ();
387 return (register_callback (list, name, cf));
388 } /* }}} int create_register_callback */
390 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
397 e = llist_search (list, name);
401 llist_remove (list, e);
404 destroy_callback (e->value);
409 } /* }}} int plugin_unregister */
412 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
413 * object, but it will bitch about a shared object not having a
414 * ``module_register'' symbol..
416 static int plugin_load_file (char *file, uint32_t flags)
419 void (*reg_handle) (void);
422 lt_dlerror (); /* clear errors */
424 #if LIBTOOL_VERSION == 2
425 if (flags & PLUGIN_FLAGS_GLOBAL) {
427 lt_dladvise_init(&advise);
428 lt_dladvise_global(&advise);
429 dlh = lt_dlopenadvise(file, advise);
430 lt_dladvise_destroy(&advise);
432 dlh = lt_dlopen (file);
434 #else /* if LIBTOOL_VERSION == 1 */
435 if (flags & PLUGIN_FLAGS_GLOBAL)
436 WARNING ("plugin_load_file: The global flag is not supported, "
437 "libtool 2 is required for this.");
438 dlh = lt_dlopen (file);
443 char errbuf[1024] = "";
445 ssnprintf (errbuf, sizeof (errbuf),
446 "lt_dlopen (\"%s\") failed: %s. "
447 "The most common cause for this problem is "
448 "missing dependencies. Use ldd(1) to check "
449 "the dependencies of the plugin "
451 file, lt_dlerror ());
453 ERROR ("%s", errbuf);
454 /* Make sure this is printed to STDERR in any case, but also
455 * make sure it's printed only once. */
456 if (list_log != NULL)
457 fprintf (stderr, "ERROR: %s\n", errbuf);
462 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
464 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
465 file, lt_dlerror ());
475 static void *plugin_read_thread (void __attribute__((unused)) *args)
477 while (read_loop != 0)
480 plugin_ctx_t old_ctx;
488 /* Get the read function that needs to be read next.
489 * We don't need to hold "read_lock" for the heap, but we need
490 * to call c_heap_get_root() and pthread_cond_wait() in the
491 * same protected block. */
492 pthread_mutex_lock (&read_lock);
493 rf = c_heap_get_root (read_heap);
496 pthread_cond_wait (&read_cond, &read_lock);
497 pthread_mutex_unlock (&read_lock);
500 pthread_mutex_unlock (&read_lock);
502 if (rf->rf_interval == 0)
504 /* this should not happen, because the interval is set
505 * for each plugin when loading it
506 * XXX: issue a warning? */
507 rf->rf_interval = plugin_get_interval ();
508 rf->rf_effective_interval = rf->rf_interval;
510 rf->rf_next_read = cdtime ();
513 /* sleep until this entry is due,
514 * using pthread_cond_timedwait */
515 pthread_mutex_lock (&read_lock);
516 /* In pthread_cond_timedwait, spurious wakeups are possible
517 * (and really happen, at least on NetBSD with > 1 CPU), thus
518 * we need to re-evaluate the condition every time
519 * pthread_cond_timedwait returns. */
521 while ((read_loop != 0)
522 && (cdtime () < rf->rf_next_read)
525 struct timespec ts = { 0 };
527 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
529 rc = pthread_cond_timedwait (&read_cond, &read_lock,
533 /* Must hold `read_lock' when accessing `rf->rf_type'. */
534 rf_type = rf->rf_type;
535 pthread_mutex_unlock (&read_lock);
537 /* Check if we're supposed to stop.. This may have interrupted
541 /* Insert `rf' again, so it can be free'd correctly */
542 c_heap_insert (read_heap, rf);
546 /* The entry has been marked for deletion. The linked list
547 * entry has already been removed by `plugin_unregister_read'.
548 * All we have to do here is free the `read_func_t' and
550 if (rf_type == RF_REMOVE)
552 DEBUG ("plugin_read_thread: Destroying the `%s' "
553 "callback.", rf->rf_name);
555 destroy_callback ((callback_func_t *) rf);
560 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
564 old_ctx = plugin_set_ctx (rf->rf_ctx);
566 if (rf_type == RF_SIMPLE)
568 int (*callback) (void);
570 callback = rf->rf_callback;
571 status = (*callback) ();
575 plugin_read_cb callback;
577 assert (rf_type == RF_COMPLEX);
579 callback = rf->rf_callback;
580 status = (*callback) (&rf->rf_udata);
583 plugin_set_ctx (old_ctx);
585 /* If the function signals failure, we will increase the
586 * intervals in which it will be called. */
589 rf->rf_effective_interval *= 2;
590 if (rf->rf_effective_interval > max_read_interval)
591 rf->rf_effective_interval = max_read_interval;
593 NOTICE ("read-function of plugin `%s' failed. "
594 "Will suspend it for %.3f seconds.",
596 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
600 /* Success: Restore the interval, if it was changed. */
601 rf->rf_effective_interval = rf->rf_interval;
604 /* update the ``next read due'' field */
607 /* calculate the time spent in the read function */
608 elapsed = (now - start);
610 if (elapsed > rf->rf_effective_interval)
611 WARNING ("plugin_read_thread: read-function of the `%s' plugin took %.3f "
612 "seconds, which is above its read interval (%.3f seconds). You might "
613 "want to adjust the `Interval' or `ReadThreads' settings.",
614 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed),
615 CDTIME_T_TO_DOUBLE(rf->rf_effective_interval));
617 DEBUG ("plugin_read_thread: read-function of the `%s' plugin took "
619 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed));
621 DEBUG ("plugin_read_thread: Effective interval of the "
622 "`%s' plugin is %.3f seconds.",
624 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
626 /* Calculate the next (absolute) time at which this function
627 * should be called. */
628 rf->rf_next_read += rf->rf_effective_interval;
630 /* Check, if `rf_next_read' is in the past. */
631 if (rf->rf_next_read < now)
633 /* `rf_next_read' is in the past. Insert `now'
634 * so this value doesn't trail off into the
636 rf->rf_next_read = now;
639 DEBUG ("plugin_read_thread: Next read of the `%s' plugin at %.3f.",
641 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
643 /* Re-insert this read function into the heap again. */
644 c_heap_insert (read_heap, rf);
645 } /* while (read_loop) */
649 } /* void *plugin_read_thread */
651 static void start_read_threads (int num)
653 if (read_threads != NULL)
656 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
657 if (read_threads == NULL)
659 ERROR ("plugin: start_read_threads: calloc failed.");
663 read_threads_num = 0;
664 for (int i = 0; i < num; i++)
666 if (pthread_create (read_threads + read_threads_num, NULL,
667 plugin_read_thread, NULL) == 0)
673 ERROR ("plugin: start_read_threads: pthread_create failed.");
677 } /* void start_read_threads */
679 static void stop_read_threads (void)
681 if (read_threads == NULL)
684 INFO ("collectd: Stopping %i read threads.", read_threads_num);
686 pthread_mutex_lock (&read_lock);
688 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
689 pthread_cond_broadcast (&read_cond);
690 pthread_mutex_unlock (&read_lock);
692 for (int i = 0; i < read_threads_num; i++)
694 if (pthread_join (read_threads[i], NULL) != 0)
696 ERROR ("plugin: stop_read_threads: pthread_join failed.");
698 read_threads[i] = (pthread_t) 0;
700 sfree (read_threads);
701 read_threads_num = 0;
702 } /* void stop_read_threads */
704 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
709 meta_data_destroy (vl->meta);
712 } /* }}} void plugin_value_list_free */
714 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
721 vl = malloc (sizeof (*vl));
724 memcpy (vl, vl_orig, sizeof (*vl));
726 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
727 if (vl->values == NULL)
729 plugin_value_list_free (vl);
732 memcpy (vl->values, vl_orig->values,
733 vl_orig->values_len * sizeof (*vl->values));
735 vl->meta = meta_data_clone (vl->meta);
736 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
738 plugin_value_list_free (vl);
743 vl->time = cdtime ();
745 /* Fill in the interval from the thread context, if it is zero. */
746 if (vl->interval == 0)
748 plugin_ctx_t ctx = plugin_get_ctx ();
750 if (ctx.interval != 0)
751 vl->interval = ctx.interval;
754 char name[6 * DATA_MAX_NAME_LEN];
755 FORMAT_VL (name, sizeof (name), vl);
756 ERROR ("plugin_value_list_clone: Unable to determine "
757 "interval from context for "
758 "value list \"%s\". "
759 "This indicates a broken plugin. "
760 "Please report this problem to the "
761 "collectd mailing list or at "
762 "<http://collectd.org/bugs/>.", name);
763 vl->interval = cf_get_default_interval ();
768 } /* }}} value_list_t *plugin_value_list_clone */
770 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
774 q = malloc (sizeof (*q));
779 q->vl = plugin_value_list_clone (vl);
786 /* Store context of caller (read plugin); otherwise, it would not be
787 * available to the write plugins when actually dispatching the
788 * value-list later on. */
789 q->ctx = plugin_get_ctx ();
791 pthread_mutex_lock (&write_lock);
793 if (write_queue_tail == NULL)
795 write_queue_head = q;
796 write_queue_tail = q;
797 write_queue_length = 1;
801 write_queue_tail->next = q;
802 write_queue_tail = q;
803 write_queue_length += 1;
806 pthread_cond_signal (&write_cond);
807 pthread_mutex_unlock (&write_lock);
810 } /* }}} int plugin_write_enqueue */
812 static value_list_t *plugin_write_dequeue (void) /* {{{ */
817 pthread_mutex_lock (&write_lock);
819 while (write_loop && (write_queue_head == NULL))
820 pthread_cond_wait (&write_cond, &write_lock);
822 if (write_queue_head == NULL)
824 pthread_mutex_unlock (&write_lock);
828 q = write_queue_head;
829 write_queue_head = q->next;
830 write_queue_length -= 1;
831 if (write_queue_head == NULL) {
832 write_queue_tail = NULL;
833 assert(0 == write_queue_length);
836 pthread_mutex_unlock (&write_lock);
838 (void) plugin_set_ctx (q->ctx);
843 } /* }}} value_list_t *plugin_write_dequeue */
845 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
849 value_list_t *vl = plugin_write_dequeue ();
853 plugin_dispatch_values_internal (vl);
855 plugin_value_list_free (vl);
860 } /* }}} void *plugin_write_thread */
862 static void start_write_threads (size_t num) /* {{{ */
864 if (write_threads != NULL)
867 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
868 if (write_threads == NULL)
870 ERROR ("plugin: start_write_threads: calloc failed.");
874 write_threads_num = 0;
875 for (size_t i = 0; i < num; i++)
879 status = pthread_create (write_threads + write_threads_num,
886 ERROR ("plugin: start_write_threads: pthread_create failed "
887 "with status %i (%s).", status,
888 sstrerror (status, errbuf, sizeof (errbuf)));
894 } /* }}} void start_write_threads */
896 static void stop_write_threads (void) /* {{{ */
901 if (write_threads == NULL)
904 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
906 pthread_mutex_lock (&write_lock);
908 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
909 pthread_cond_broadcast (&write_cond);
910 pthread_mutex_unlock (&write_lock);
912 for (i = 0; i < write_threads_num; i++)
914 if (pthread_join (write_threads[i], NULL) != 0)
916 ERROR ("plugin: stop_write_threads: pthread_join failed.");
918 write_threads[i] = (pthread_t) 0;
920 sfree (write_threads);
921 write_threads_num = 0;
923 pthread_mutex_lock (&write_lock);
925 for (q = write_queue_head; q != NULL; )
927 write_queue_t *q1 = q;
928 plugin_value_list_free (q->vl);
933 write_queue_head = NULL;
934 write_queue_tail = NULL;
935 write_queue_length = 0;
936 pthread_mutex_unlock (&write_lock);
940 WARNING ("plugin: %zu value list%s left after shutting down "
941 "the write threads.",
942 i, (i == 1) ? " was" : "s were");
944 } /* }}} void stop_write_threads */
949 void plugin_set_dir (const char *dir)
959 plugindir = strdup (dir);
960 if (plugindir == NULL)
961 ERROR ("plugin_set_dir: strdup(\"%s\") failed", dir);
964 static _Bool plugin_is_loaded (char const *name)
968 if (plugins_loaded == NULL)
969 plugins_loaded = c_avl_create ((int (*) (const void *, const void *)) strcasecmp);
970 assert (plugins_loaded != NULL);
972 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
973 return (status == 0);
976 static int plugin_mark_loaded (char const *name)
981 name_copy = strdup (name);
982 if (name_copy == NULL)
985 status = c_avl_insert (plugins_loaded,
986 /* key = */ name_copy, /* value = */ NULL);
990 static void plugin_free_loaded (void)
995 if (plugins_loaded == NULL)
998 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
1001 assert (value == NULL);
1004 c_avl_destroy (plugins_loaded);
1005 plugins_loaded = NULL;
1009 int plugin_load (char const *plugin_name, uint32_t flags)
1013 char filename[BUFSIZE] = "";
1014 char typename[BUFSIZE];
1016 struct stat statbuf;
1020 if (plugin_name == NULL)
1023 /* Check if plugin is already loaded and don't do anything in this
1025 if (plugin_is_loaded (plugin_name))
1028 dir = plugin_get_dir ();
1032 * XXX: Magic at work:
1034 * Some of the language bindings, for example the Python and Perl
1035 * plugins, need to be able to export symbols to the scripts they run.
1036 * For this to happen, the "Globals" flag needs to be set.
1037 * Unfortunately, this technical detail is hard to explain to the
1038 * average user and she shouldn't have to worry about this, ideally.
1039 * So in order to save everyone's sanity use a different default for a
1040 * handful of special plugins. --octo
1042 if ((strcasecmp ("perl", plugin_name) == 0)
1043 || (strcasecmp ("python", plugin_name) == 0))
1044 flags |= PLUGIN_FLAGS_GLOBAL;
1046 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
1047 * type when matching the filename */
1048 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
1049 if ((status < 0) || ((size_t) status >= sizeof (typename)))
1051 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
1055 if ((dh = opendir (dir)) == NULL)
1058 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
1059 sstrerror (errno, errbuf, sizeof (errbuf)));
1063 while ((de = readdir (dh)) != NULL)
1065 if (strcasecmp (de->d_name, typename))
1068 status = ssnprintf (filename, sizeof (filename),
1069 "%s/%s", dir, de->d_name);
1070 if ((status < 0) || ((size_t) status >= sizeof (filename)))
1072 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
1077 if (lstat (filename, &statbuf) == -1)
1080 WARNING ("plugin_load: stat (\"%s\") failed: %s",
1082 sstrerror (errno, errbuf, sizeof (errbuf)));
1085 else if (!S_ISREG (statbuf.st_mode))
1087 /* don't follow symlinks */
1088 WARNING ("plugin_load: %s is not a regular file.",
1093 status = plugin_load_file (filename, flags);
1097 plugin_mark_loaded (plugin_name);
1099 INFO ("plugin_load: plugin \"%s\" successfully loaded.", plugin_name);
1104 ERROR ("plugin_load: Load plugin \"%s\" failed with "
1105 "status %i.", plugin_name, status);
1111 if (filename[0] == 0)
1112 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1119 * The `register_*' functions follow
1121 int plugin_register_config (const char *name,
1122 int (*callback) (const char *key, const char *val),
1123 const char **keys, int keys_num)
1125 cf_register (name, callback, keys, keys_num);
1127 } /* int plugin_register_config */
1129 int plugin_register_complex_config (const char *type,
1130 int (*callback) (oconfig_item_t *))
1132 return (cf_register_complex (type, callback));
1133 } /* int plugin_register_complex_config */
1135 int plugin_register_init (const char *name,
1136 int (*callback) (void))
1138 return (create_register_callback (&list_init, name, (void *) callback,
1139 /* user_data = */ NULL));
1140 } /* plugin_register_init */
1142 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1144 const read_func_t *rf0;
1145 const read_func_t *rf1;
1150 if (rf0->rf_next_read < rf1->rf_next_read)
1152 else if (rf0->rf_next_read > rf1->rf_next_read)
1156 } /* int plugin_compare_read_func */
1158 /* Add a read function to both, the heap and a linked list. The linked list if
1159 * used to look-up read functions, especially for the remove function. The heap
1160 * is used to determine which plugin to read next. */
1161 static int plugin_insert_read (read_func_t *rf)
1166 rf->rf_next_read = cdtime ();
1167 rf->rf_effective_interval = rf->rf_interval;
1169 pthread_mutex_lock (&read_lock);
1171 if (read_list == NULL)
1173 read_list = llist_create ();
1174 if (read_list == NULL)
1176 pthread_mutex_unlock (&read_lock);
1177 ERROR ("plugin_insert_read: read_list failed.");
1182 if (read_heap == NULL)
1184 read_heap = c_heap_create (plugin_compare_read_func);
1185 if (read_heap == NULL)
1187 pthread_mutex_unlock (&read_lock);
1188 ERROR ("plugin_insert_read: c_heap_create failed.");
1193 le = llist_search (read_list, rf->rf_name);
1196 pthread_mutex_unlock (&read_lock);
1197 WARNING ("The read function \"%s\" is already registered. "
1198 "Check for duplicate \"LoadPlugin\" lines "
1199 "in your configuration!",
1204 le = llentry_create (rf->rf_name, rf);
1207 pthread_mutex_unlock (&read_lock);
1208 ERROR ("plugin_insert_read: llentry_create failed.");
1212 status = c_heap_insert (read_heap, rf);
1215 pthread_mutex_unlock (&read_lock);
1216 ERROR ("plugin_insert_read: c_heap_insert failed.");
1217 llentry_destroy (le);
1221 /* This does not fail. */
1222 llist_append (read_list, le);
1224 /* Wake up all the read threads. */
1225 pthread_cond_broadcast (&read_cond);
1226 pthread_mutex_unlock (&read_lock);
1228 } /* int plugin_insert_read */
1230 int plugin_register_read (const char *name,
1231 int (*callback) (void))
1236 rf = calloc (1, sizeof (*rf));
1239 ERROR ("plugin_register_read: calloc failed.");
1243 rf->rf_callback = (void *) callback;
1244 rf->rf_udata.data = NULL;
1245 rf->rf_udata.free_func = NULL;
1246 rf->rf_ctx = plugin_get_ctx ();
1247 rf->rf_group[0] = '\0';
1248 rf->rf_name = strdup (name);
1249 rf->rf_type = RF_SIMPLE;
1250 rf->rf_interval = plugin_get_interval ();
1252 status = plugin_insert_read (rf);
1254 sfree (rf->rf_name);
1259 } /* int plugin_register_read */
1261 int plugin_register_complex_read (const char *group, const char *name,
1262 plugin_read_cb callback,
1264 user_data_t *user_data)
1269 rf = calloc (1,sizeof (*rf));
1272 ERROR ("plugin_register_complex_read: calloc failed.");
1276 rf->rf_callback = (void *) callback;
1278 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1280 rf->rf_group[0] = '\0';
1281 rf->rf_name = strdup (name);
1282 rf->rf_type = RF_COMPLEX;
1283 rf->rf_interval = (interval != 0) ? interval : plugin_get_interval ();
1286 if (user_data == NULL)
1288 rf->rf_udata.data = NULL;
1289 rf->rf_udata.free_func = NULL;
1293 rf->rf_udata = *user_data;
1296 rf->rf_ctx = plugin_get_ctx ();
1298 status = plugin_insert_read (rf);
1300 sfree (rf->rf_name);
1305 } /* int plugin_register_complex_read */
1307 int plugin_register_write (const char *name,
1308 plugin_write_cb callback, user_data_t *ud)
1310 return (create_register_callback (&list_write, name,
1311 (void *) callback, ud));
1312 } /* int plugin_register_write */
1314 static int plugin_flush_timeout_callback (user_data_t *ud)
1316 flush_callback_t *cb = ud->data;
1318 return plugin_flush (cb->name, cb->timeout, /* identifier = */ NULL);
1319 } /* static int plugin_flush_callback */
1321 static void plugin_flush_timeout_callback_free (void *data)
1323 flush_callback_t *cb = data;
1325 if (cb == NULL) return;
1329 } /* static void plugin_flush_callback_free */
1331 static char *plugin_flush_callback_name (const char *name)
1333 const char *flush_prefix = "flush/";
1338 prefix_size = strlen(flush_prefix);
1339 name_size = strlen(name);
1341 flush_name = malloc (name_size + prefix_size + 1);
1342 if (flush_name == NULL)
1344 ERROR ("plugin_flush_callback_name: malloc failed.");
1348 sstrncpy (flush_name, flush_prefix, prefix_size + 1);
1349 sstrncpy (flush_name + prefix_size, name, name_size + 1);
1352 } /* static char *plugin_flush_callback_name */
1354 int plugin_register_flush (const char *name,
1355 plugin_flush_cb callback, user_data_t *ud)
1358 plugin_ctx_t ctx = plugin_get_ctx ();
1360 status = create_register_callback (&list_flush, name,
1361 (void *) callback, ud);
1365 if (ctx.flush_interval != 0)
1368 flush_callback_t *cb;
1370 flush_name = plugin_flush_callback_name (name);
1371 if (flush_name == NULL)
1374 cb = malloc(sizeof (*cb));
1377 ERROR ("plugin_register_flush: malloc failed.");
1382 cb->name = strdup (name);
1383 if (cb->name == NULL)
1385 ERROR ("plugin_register_flush: strdup failed.");
1390 cb->timeout = ctx.flush_timeout;
1393 ud->free_func = plugin_flush_timeout_callback_free;
1395 status = plugin_register_complex_read (
1396 /* group = */ "flush",
1397 /* name = */ flush_name,
1398 /* callback = */ plugin_flush_timeout_callback,
1399 /* interval = */ ctx.flush_interval,
1400 /* user data = */ ud);
1412 } /* int plugin_register_flush */
1414 int plugin_register_missing (const char *name,
1415 plugin_missing_cb callback, user_data_t *ud)
1417 return (create_register_callback (&list_missing, name,
1418 (void *) callback, ud));
1419 } /* int plugin_register_missing */
1421 int plugin_register_shutdown (const char *name,
1422 int (*callback) (void))
1424 return (create_register_callback (&list_shutdown, name,
1425 (void *) callback, /* user_data = */ NULL));
1426 } /* int plugin_register_shutdown */
1428 static void plugin_free_data_sets (void)
1433 if (data_sets == NULL)
1436 while (c_avl_pick (data_sets, &key, &value) == 0)
1438 data_set_t *ds = value;
1439 /* key is a pointer to ds->type */
1445 c_avl_destroy (data_sets);
1447 } /* void plugin_free_data_sets */
1449 int plugin_register_data_set (const data_set_t *ds)
1451 data_set_t *ds_copy;
1453 if ((data_sets != NULL)
1454 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1456 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1457 plugin_unregister_data_set (ds->type);
1459 else if (data_sets == NULL)
1461 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1462 if (data_sets == NULL)
1466 ds_copy = malloc (sizeof (*ds_copy));
1467 if (ds_copy == NULL)
1469 memcpy(ds_copy, ds, sizeof (data_set_t));
1471 ds_copy->ds = malloc (sizeof (*ds_copy->ds)
1473 if (ds_copy->ds == NULL)
1479 for (size_t i = 0; i < ds->ds_num; i++)
1480 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1482 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1483 } /* int plugin_register_data_set */
1485 int plugin_register_log (const char *name,
1486 plugin_log_cb callback, user_data_t *ud)
1488 return (create_register_callback (&list_log, name,
1489 (void *) callback, ud));
1490 } /* int plugin_register_log */
1492 int plugin_register_notification (const char *name,
1493 plugin_notification_cb callback, user_data_t *ud)
1495 return (create_register_callback (&list_notification, name,
1496 (void *) callback, ud));
1497 } /* int plugin_register_log */
1499 int plugin_unregister_config (const char *name)
1501 cf_unregister (name);
1503 } /* int plugin_unregister_config */
1505 int plugin_unregister_complex_config (const char *name)
1507 cf_unregister_complex (name);
1509 } /* int plugin_unregister_complex_config */
1511 int plugin_unregister_init (const char *name)
1513 return (plugin_unregister (list_init, name));
1516 int plugin_unregister_read (const char *name) /* {{{ */
1524 pthread_mutex_lock (&read_lock);
1526 if (read_list == NULL)
1528 pthread_mutex_unlock (&read_lock);
1532 le = llist_search (read_list, name);
1535 pthread_mutex_unlock (&read_lock);
1536 WARNING ("plugin_unregister_read: No such read function: %s",
1541 llist_remove (read_list, le);
1544 assert (rf != NULL);
1545 rf->rf_type = RF_REMOVE;
1547 pthread_mutex_unlock (&read_lock);
1549 llentry_destroy (le);
1551 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1554 } /* }}} int plugin_unregister_read */
1556 void plugin_log_available_writers (void)
1558 log_list_callbacks (&list_write, "Available write targets:");
1561 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1563 read_func_t *rf = e->value;
1566 return strcmp (rf->rf_group, (const char *)group);
1567 } /* }}} int compare_read_func_group */
1569 int plugin_unregister_read_group (const char *group) /* {{{ */
1579 pthread_mutex_lock (&read_lock);
1581 if (read_list == NULL)
1583 pthread_mutex_unlock (&read_lock);
1589 le = llist_search_custom (read_list,
1590 compare_read_func_group, (void *)group);
1597 llist_remove (read_list, le);
1600 assert (rf != NULL);
1601 rf->rf_type = RF_REMOVE;
1603 llentry_destroy (le);
1605 DEBUG ("plugin_unregister_read_group: "
1606 "Marked `%s' (group `%s') for removal.",
1607 rf->rf_name, group);
1610 pthread_mutex_unlock (&read_lock);
1614 WARNING ("plugin_unregister_read_group: No such "
1615 "group of read function: %s", group);
1620 } /* }}} int plugin_unregister_read_group */
1622 int plugin_unregister_write (const char *name)
1624 return (plugin_unregister (list_write, name));
1627 int plugin_unregister_flush (const char *name)
1629 plugin_ctx_t ctx = plugin_get_ctx ();
1631 if (ctx.flush_interval != 0)
1635 flush_name = plugin_flush_callback_name (name);
1636 if (flush_name != NULL)
1638 plugin_unregister_read(flush_name);
1643 return plugin_unregister (list_flush, name);
1646 int plugin_unregister_missing (const char *name)
1648 return (plugin_unregister (list_missing, name));
1651 int plugin_unregister_shutdown (const char *name)
1653 return (plugin_unregister (list_shutdown, name));
1656 int plugin_unregister_data_set (const char *name)
1660 if (data_sets == NULL)
1663 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1670 } /* int plugin_unregister_data_set */
1672 int plugin_unregister_log (const char *name)
1674 return (plugin_unregister (list_log, name));
1677 int plugin_unregister_notification (const char *name)
1679 return (plugin_unregister (list_notification, name));
1682 int plugin_init_all (void)
1684 char const *chain_name;
1689 /* Init the value cache */
1692 if (IS_TRUE (global_option_get ("CollectInternalStats")))
1693 record_statistics = 1;
1695 chain_name = global_option_get ("PreCacheChain");
1696 pre_cache_chain = fc_chain_get_by_name (chain_name);
1698 chain_name = global_option_get ("PostCacheChain");
1699 post_cache_chain = fc_chain_get_by_name (chain_name);
1701 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1703 if (write_limit_high < 0)
1705 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1706 write_limit_high = 0;
1709 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1710 /* default = */ write_limit_high / 2);
1711 if (write_limit_low < 0)
1713 ERROR ("WriteQueueLimitLow must be positive or zero.");
1714 write_limit_low = write_limit_high / 2;
1716 else if (write_limit_low > write_limit_high)
1718 ERROR ("WriteQueueLimitLow must not be larger than "
1719 "WriteQueueLimitHigh.");
1720 write_limit_low = write_limit_high;
1723 write_threads_num = global_option_get_long ("WriteThreads",
1725 if (write_threads_num < 1)
1727 ERROR ("WriteThreads must be positive.");
1728 write_threads_num = 5;
1731 if ((list_init == NULL) && (read_heap == NULL))
1734 /* Calling all init callbacks before checking if read callbacks
1735 * are available allows the init callbacks to register the read
1737 le = llist_head (list_init);
1740 callback_func_t *cf;
1741 plugin_init_cb callback;
1742 plugin_ctx_t old_ctx;
1745 old_ctx = plugin_set_ctx (cf->cf_ctx);
1746 callback = cf->cf_callback;
1747 status = (*callback) ();
1748 plugin_set_ctx (old_ctx);
1752 ERROR ("Initialization of plugin `%s' "
1753 "failed with status %i. "
1754 "Plugin will be unloaded.",
1756 /* Plugins that register read callbacks from the init
1757 * callback should take care of appropriate error
1758 * handling themselves. */
1759 /* FIXME: Unload _all_ functions */
1760 plugin_unregister_read (le->key);
1767 start_write_threads ((size_t) write_threads_num);
1769 max_read_interval = global_option_get_time ("MaxReadInterval",
1770 DEFAULT_MAX_READ_INTERVAL);
1772 /* Start read-threads */
1773 if (read_heap != NULL)
1778 rt = global_option_get ("ReadThreads");
1781 start_read_threads ((num > 0) ? num : 5);
1784 } /* void plugin_init_all */
1786 /* TODO: Rename this function. */
1787 void plugin_read_all (void)
1789 if(record_statistics) {
1790 plugin_update_internal_statistics ();
1792 uc_check_timeout ();
1795 } /* void plugin_read_all */
1797 /* Read function called when the `-T' command line argument is given. */
1798 int plugin_read_all_once (void)
1801 int return_status = 0;
1803 if (read_heap == NULL)
1805 NOTICE ("No read-functions are registered.");
1812 plugin_ctx_t old_ctx;
1814 rf = c_heap_get_root (read_heap);
1818 old_ctx = plugin_set_ctx (rf->rf_ctx);
1820 if (rf->rf_type == RF_SIMPLE)
1822 int (*callback) (void);
1824 callback = rf->rf_callback;
1825 status = (*callback) ();
1829 plugin_read_cb callback;
1831 callback = rf->rf_callback;
1832 status = (*callback) (&rf->rf_udata);
1835 plugin_set_ctx (old_ctx);
1839 NOTICE ("read-function of plugin `%s' failed.",
1844 sfree (rf->rf_name);
1845 destroy_callback ((void *) rf);
1848 return (return_status);
1849 } /* int plugin_read_all_once */
1851 int plugin_write (const char *plugin, /* {{{ */
1852 const data_set_t *ds, const value_list_t *vl)
1860 if (list_write == NULL)
1865 ds = plugin_get_ds (vl->type);
1868 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1878 le = llist_head (list_write);
1881 callback_func_t *cf = le->value;
1882 plugin_write_cb callback;
1884 /* do not switch plugin context; rather keep the context (interval)
1885 * information of the calling read plugin */
1887 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1888 callback = cf->cf_callback;
1889 status = (*callback) (ds, vl, &cf->cf_udata);
1898 if ((success == 0) && (failure != 0))
1903 else /* plugin != NULL */
1905 callback_func_t *cf;
1906 plugin_write_cb callback;
1908 le = llist_head (list_write);
1911 if (strcasecmp (plugin, le->key) == 0)
1922 /* do not switch plugin context; rather keep the context (interval)
1923 * information of the calling read plugin */
1925 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1926 callback = cf->cf_callback;
1927 status = (*callback) (ds, vl, &cf->cf_udata);
1931 } /* }}} int plugin_write */
1933 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1937 if (list_flush == NULL)
1940 le = llist_head (list_flush);
1943 callback_func_t *cf;
1944 plugin_flush_cb callback;
1945 plugin_ctx_t old_ctx;
1947 if ((plugin != NULL)
1948 && (strcmp (plugin, le->key) != 0))
1955 old_ctx = plugin_set_ctx (cf->cf_ctx);
1956 callback = cf->cf_callback;
1958 (*callback) (timeout, identifier, &cf->cf_udata);
1960 plugin_set_ctx (old_ctx);
1965 } /* int plugin_flush */
1967 int plugin_shutdown_all (void)
1970 int ret = 0; // Assume success.
1972 destroy_all_callbacks (&list_init);
1974 stop_read_threads ();
1976 pthread_mutex_lock (&read_lock);
1977 llist_destroy (read_list);
1979 pthread_mutex_unlock (&read_lock);
1981 destroy_read_heap ();
1983 /* blocks until all write threads have shut down. */
1984 stop_write_threads ();
1986 /* ask all plugins to write out the state they kept. */
1987 plugin_flush (/* plugin = */ NULL,
1989 /* identifier = */ NULL);
1992 if (list_shutdown != NULL)
1993 le = llist_head (list_shutdown);
1997 callback_func_t *cf;
1998 plugin_shutdown_cb callback;
1999 plugin_ctx_t old_ctx;
2002 old_ctx = plugin_set_ctx (cf->cf_ctx);
2003 callback = cf->cf_callback;
2005 /* Advance the pointer before calling the callback allows
2006 * shutdown functions to unregister themselves. If done the
2007 * other way around the memory `le' points to will be freed
2008 * after callback returns. */
2011 if ((*callback) () != 0)
2014 plugin_set_ctx (old_ctx);
2017 /* Write plugins which use the `user_data' pointer usually need the
2018 * same data available to the flush callback. If this is the case, set
2019 * the free_function to NULL when registering the flush callback and to
2020 * the real free function when registering the write callback. This way
2021 * the data isn't freed twice. */
2022 destroy_all_callbacks (&list_flush);
2023 destroy_all_callbacks (&list_missing);
2024 destroy_all_callbacks (&list_write);
2026 destroy_all_callbacks (&list_notification);
2027 destroy_all_callbacks (&list_shutdown);
2028 destroy_all_callbacks (&list_log);
2030 plugin_free_loaded ();
2031 plugin_free_data_sets ();
2033 } /* void plugin_shutdown_all */
2035 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
2039 if (list_missing == NULL)
2042 le = llist_head (list_missing);
2045 callback_func_t *cf;
2046 plugin_missing_cb callback;
2047 plugin_ctx_t old_ctx;
2051 old_ctx = plugin_set_ctx (cf->cf_ctx);
2052 callback = cf->cf_callback;
2054 status = (*callback) (vl, &cf->cf_udata);
2055 plugin_set_ctx (old_ctx);
2060 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
2061 "failed with status %i.",
2074 } /* int }}} plugin_dispatch_missing */
2076 static int plugin_dispatch_values_internal (value_list_t *vl)
2079 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
2081 value_t *saved_values;
2082 int saved_values_len;
2086 int free_meta_data = 0;
2091 if (vl->type[0] == 0 || vl->values == NULL || vl->values_len < 1)
2093 ERROR ("plugin_dispatch_values: Invalid value list "
2094 "from plugin %s.", vl->plugin);
2098 /* Free meta data only if the calling function didn't specify any. In
2099 * this case matches and targets may add some and the calling function
2100 * may not expect (and therefore free) that data. */
2101 if (vl->meta == NULL)
2104 if (list_write == NULL)
2105 c_complain_once (LOG_WARNING, &no_write_complaint,
2106 "plugin_dispatch_values: No write callback has been "
2107 "registered. Please load at least one output plugin, "
2108 "if you want the collected data to be stored.");
2110 if (data_sets == NULL)
2112 ERROR ("plugin_dispatch_values: No data sets registered. "
2113 "Could the types database be read? Check "
2114 "your `TypesDB' setting!");
2118 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
2120 char ident[6 * DATA_MAX_NAME_LEN];
2122 FORMAT_VL (ident, sizeof (ident), vl);
2123 INFO ("plugin_dispatch_values: Dataset not found: %s "
2124 "(from \"%s\"), check your types.db!",
2129 /* Assured by plugin_value_list_clone(). The time is determined at
2130 * _enqueue_ time. */
2131 assert (vl->time != 0);
2132 assert (vl->interval != 0);
2134 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
2136 "plugin = %s; plugin_instance = %s; "
2137 "type = %s; type_instance = %s;",
2138 CDTIME_T_TO_DOUBLE (vl->time),
2139 CDTIME_T_TO_DOUBLE (vl->interval),
2141 vl->plugin, vl->plugin_instance,
2142 vl->type, vl->type_instance);
2145 assert (0 == strcmp (ds->type, vl->type));
2147 if (0 != strcmp (ds->type, vl->type))
2148 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
2149 ds->type, vl->type);
2153 assert (ds->ds_num == vl->values_len);
2155 if (ds->ds_num != vl->values_len)
2157 ERROR ("plugin_dispatch_values: ds->type = %s: "
2158 "(ds->ds_num = %zu) != "
2159 "(vl->values_len = %zu)",
2160 ds->type, ds->ds_num, vl->values_len);
2165 escape_slashes (vl->host, sizeof (vl->host));
2166 escape_slashes (vl->plugin, sizeof (vl->plugin));
2167 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
2168 escape_slashes (vl->type, sizeof (vl->type));
2169 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
2171 /* Copy the values. This way, we can assure `targets' that they get
2172 * dynamically allocated values, which they can free and replace if
2174 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
2176 saved_values = vl->values;
2177 saved_values_len = vl->values_len;
2179 vl->values = (value_t *) calloc (vl->values_len,
2180 sizeof (*vl->values));
2181 if (vl->values == NULL)
2183 ERROR ("plugin_dispatch_values: calloc failed.");
2184 vl->values = saved_values;
2187 memcpy (vl->values, saved_values,
2188 vl->values_len * sizeof (*vl->values));
2190 else /* if ((pre == NULL) && (post == NULL)) */
2192 saved_values = NULL;
2193 saved_values_len = 0;
2196 if (pre_cache_chain != NULL)
2198 status = fc_process_chain (ds, vl, pre_cache_chain);
2201 WARNING ("plugin_dispatch_values: Running the "
2202 "pre-cache chain failed with "
2206 else if (status == FC_TARGET_STOP)
2208 /* Restore the state of the value_list so that plugins
2209 * don't get confused.. */
2210 if (saved_values != NULL)
2213 vl->values = saved_values;
2214 vl->values_len = saved_values_len;
2220 /* Update the value cache */
2223 if (post_cache_chain != NULL)
2225 status = fc_process_chain (ds, vl, post_cache_chain);
2228 WARNING ("plugin_dispatch_values: Running the "
2229 "post-cache chain failed with "
2235 fc_default_action (ds, vl);
2237 /* Restore the state of the value_list so that plugins don't get
2239 if (saved_values != NULL)
2242 vl->values = saved_values;
2243 vl->values_len = saved_values_len;
2246 if ((free_meta_data != 0) && (vl->meta != NULL))
2248 meta_data_destroy (vl->meta);
2253 } /* int plugin_dispatch_values_internal */
2255 static double get_drop_probability (void) /* {{{ */
2261 pthread_mutex_lock (&write_lock);
2262 wql = write_queue_length;
2263 pthread_mutex_unlock (&write_lock);
2265 if (wql < write_limit_low)
2267 if (wql >= write_limit_high)
2270 pos = 1 + wql - write_limit_low;
2271 size = 1 + write_limit_high - write_limit_low;
2273 return (((double) pos) / ((double) size));
2274 } /* }}} double get_drop_probability */
2276 static _Bool check_drop_value (void) /* {{{ */
2278 static cdtime_t last_message_time = 0;
2279 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2285 if (write_limit_high == 0)
2288 p = get_drop_probability ();
2292 status = pthread_mutex_trylock (&last_message_lock);
2298 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2300 last_message_time = now;
2301 ERROR ("plugin_dispatch_values: Low water mark "
2302 "reached. Dropping %.0f%% of metrics.",
2305 pthread_mutex_unlock (&last_message_lock);
2316 } /* }}} _Bool check_drop_value */
2318 int plugin_dispatch_values (value_list_t const *vl)
2321 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2323 if (check_drop_value ()) {
2324 if(record_statistics) {
2325 pthread_mutex_lock(&statistics_lock);
2326 stats_values_dropped++;
2327 pthread_mutex_unlock(&statistics_lock);
2332 status = plugin_write_enqueue (vl);
2336 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2337 "with status %i (%s).", status,
2338 sstrerror (status, errbuf, sizeof (errbuf)));
2345 __attribute__((sentinel))
2346 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2347 _Bool store_percentage, int store_type, ...)
2354 assert (template->values_len == 1);
2356 /* Calculate sum for Gauge to calculate percent if needed */
2357 if (DS_TYPE_GAUGE == store_type) {
2358 va_start (ap, store_type);
2364 name = va_arg (ap, char const *);
2368 value = va_arg (ap, gauge_t);
2376 vl = plugin_value_list_clone (template);
2377 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2378 if (store_percentage)
2379 sstrncpy (vl->type, "percent", sizeof (vl->type));
2381 va_start (ap, store_type);
2387 /* Set the type instance. */
2388 name = va_arg (ap, char const *);
2391 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2393 /* Set the value. */
2397 vl->values[0].gauge = va_arg (ap, gauge_t);
2398 if (store_percentage)
2399 vl->values[0].gauge *= sum ? (100.0 / sum) : NAN;
2401 case DS_TYPE_ABSOLUTE:
2402 vl->values[0].absolute = va_arg (ap, absolute_t);
2404 case DS_TYPE_COUNTER:
2405 vl->values[0].counter = va_arg (ap, counter_t);
2407 case DS_TYPE_DERIVE:
2408 vl->values[0].derive = va_arg (ap, derive_t);
2411 ERROR ("plugin_dispatch_multivalue: given store_type is incorrect.");
2416 status = plugin_write_enqueue (vl);
2422 plugin_value_list_free (vl);
2424 } /* }}} int plugin_dispatch_multivalue */
2426 int plugin_dispatch_notification (const notification_t *notif)
2429 /* Possible TODO: Add flap detection here */
2431 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2432 "time = %.3f; host = %s;",
2433 notif->severity, notif->message,
2434 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2436 /* Nobody cares for notifications */
2437 if (list_notification == NULL)
2440 le = llist_head (list_notification);
2443 callback_func_t *cf;
2444 plugin_notification_cb callback;
2447 /* do not switch plugin context; rather keep the context
2448 * (interval) information of the calling plugin */
2451 callback = cf->cf_callback;
2452 status = (*callback) (notif, &cf->cf_udata);
2455 WARNING ("plugin_dispatch_notification: Notification "
2456 "callback %s returned %i.",
2464 } /* int plugin_dispatch_notification */
2466 void plugin_log (int level, const char *format, ...)
2473 if (level >= LOG_DEBUG)
2477 va_start (ap, format);
2478 vsnprintf (msg, sizeof (msg), format, ap);
2479 msg[sizeof (msg) - 1] = '\0';
2482 if (list_log == NULL)
2484 fprintf (stderr, "%s\n", msg);
2488 le = llist_head (list_log);
2491 callback_func_t *cf;
2492 plugin_log_cb callback;
2495 callback = cf->cf_callback;
2497 /* do not switch plugin context; rather keep the context
2498 * (interval) information of the calling plugin */
2500 (*callback) (level, msg, &cf->cf_udata);
2504 } /* void plugin_log */
2506 int parse_log_severity (const char *severity)
2510 if ((0 == strcasecmp (severity, "emerg"))
2511 || (0 == strcasecmp (severity, "alert"))
2512 || (0 == strcasecmp (severity, "crit"))
2513 || (0 == strcasecmp (severity, "err")))
2514 log_level = LOG_ERR;
2515 else if (0 == strcasecmp (severity, "warning"))
2516 log_level = LOG_WARNING;
2517 else if (0 == strcasecmp (severity, "notice"))
2518 log_level = LOG_NOTICE;
2519 else if (0 == strcasecmp (severity, "info"))
2520 log_level = LOG_INFO;
2522 else if (0 == strcasecmp (severity, "debug"))
2523 log_level = LOG_DEBUG;
2524 #endif /* COLLECT_DEBUG */
2527 } /* int parse_log_severity */
2529 int parse_notif_severity (const char *severity)
2531 int notif_severity = -1;
2533 if (strcasecmp (severity, "FAILURE") == 0)
2534 notif_severity = NOTIF_FAILURE;
2535 else if (strcmp (severity, "OKAY") == 0)
2536 notif_severity = NOTIF_OKAY;
2537 else if ((strcmp (severity, "WARNING") == 0)
2538 || (strcmp (severity, "WARN") == 0))
2539 notif_severity = NOTIF_WARNING;
2541 return (notif_severity);
2542 } /* int parse_notif_severity */
2544 const data_set_t *plugin_get_ds (const char *name)
2548 if (data_sets == NULL)
2550 ERROR ("plugin_get_ds: No data sets are defined yet.");
2554 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2556 DEBUG ("No such dataset registered: %s", name);
2561 } /* data_set_t *plugin_get_ds */
2563 static int plugin_notification_meta_add (notification_t *n,
2565 enum notification_meta_type_e type,
2568 notification_meta_t *meta;
2569 notification_meta_t *tail;
2571 if ((n == NULL) || (name == NULL) || (value == NULL))
2573 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2577 meta = calloc (1, sizeof (*meta));
2580 ERROR ("plugin_notification_meta_add: calloc failed.");
2584 sstrncpy (meta->name, name, sizeof (meta->name));
2589 case NM_TYPE_STRING:
2591 meta->nm_value.nm_string = strdup ((const char *) value);
2592 if (meta->nm_value.nm_string == NULL)
2594 ERROR ("plugin_notification_meta_add: strdup failed.");
2600 case NM_TYPE_SIGNED_INT:
2602 meta->nm_value.nm_signed_int = *((int64_t *) value);
2605 case NM_TYPE_UNSIGNED_INT:
2607 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2610 case NM_TYPE_DOUBLE:
2612 meta->nm_value.nm_double = *((double *) value);
2615 case NM_TYPE_BOOLEAN:
2617 meta->nm_value.nm_boolean = *((_Bool *) value);
2622 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2626 } /* switch (type) */
2630 while ((tail != NULL) && (tail->next != NULL))
2639 } /* int plugin_notification_meta_add */
2641 int plugin_notification_meta_add_string (notification_t *n,
2645 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2648 int plugin_notification_meta_add_signed_int (notification_t *n,
2652 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2655 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2659 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2662 int plugin_notification_meta_add_double (notification_t *n,
2666 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2669 int plugin_notification_meta_add_boolean (notification_t *n,
2673 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2676 int plugin_notification_meta_copy (notification_t *dst,
2677 const notification_t *src)
2679 assert (dst != NULL);
2680 assert (src != NULL);
2681 assert (dst != src);
2682 assert ((src->meta == NULL) || (src->meta != dst->meta));
2684 for (notification_meta_t *meta = src->meta; meta != NULL; meta = meta->next)
2686 if (meta->type == NM_TYPE_STRING)
2687 plugin_notification_meta_add_string (dst, meta->name,
2688 meta->nm_value.nm_string);
2689 else if (meta->type == NM_TYPE_SIGNED_INT)
2690 plugin_notification_meta_add_signed_int (dst, meta->name,
2691 meta->nm_value.nm_signed_int);
2692 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2693 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2694 meta->nm_value.nm_unsigned_int);
2695 else if (meta->type == NM_TYPE_DOUBLE)
2696 plugin_notification_meta_add_double (dst, meta->name,
2697 meta->nm_value.nm_double);
2698 else if (meta->type == NM_TYPE_BOOLEAN)
2699 plugin_notification_meta_add_boolean (dst, meta->name,
2700 meta->nm_value.nm_boolean);
2704 } /* int plugin_notification_meta_copy */
2706 int plugin_notification_meta_free (notification_meta_t *n)
2708 notification_meta_t *this;
2709 notification_meta_t *next;
2713 ERROR ("plugin_notification_meta_free: n == NULL!");
2718 while (this != NULL)
2722 if (this->type == NM_TYPE_STRING)
2724 /* Assign to a temporary variable to work around nm_string's const
2726 void *tmp = (void *) this->nm_value.nm_string;
2729 this->nm_value.nm_string = NULL;
2737 } /* int plugin_notification_meta_free */
2739 static void plugin_ctx_destructor (void *ctx)
2742 } /* void plugin_ctx_destructor */
2744 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2746 static plugin_ctx_t *plugin_ctx_create (void)
2750 ctx = malloc (sizeof (*ctx));
2753 ERROR ("Failed to allocate plugin context: %s",
2754 sstrerror (errno, errbuf, sizeof (errbuf)));
2759 assert (plugin_ctx_key_initialized);
2760 pthread_setspecific (plugin_ctx_key, ctx);
2761 DEBUG("Created new plugin context.");
2763 } /* int plugin_ctx_create */
2765 void plugin_init_ctx (void)
2767 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2768 plugin_ctx_key_initialized = 1;
2769 } /* void plugin_init_ctx */
2771 plugin_ctx_t plugin_get_ctx (void)
2775 assert (plugin_ctx_key_initialized);
2776 ctx = pthread_getspecific (plugin_ctx_key);
2779 ctx = plugin_ctx_create ();
2780 /* this must no happen -- exit() instead? */
2786 } /* plugin_ctx_t plugin_get_ctx */
2788 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2793 assert (plugin_ctx_key_initialized);
2794 c = pthread_getspecific (plugin_ctx_key);
2797 c = plugin_ctx_create ();
2798 /* this must no happen -- exit() instead? */
2807 } /* void plugin_set_ctx */
2809 cdtime_t plugin_get_interval (void)
2813 interval = plugin_get_ctx().interval;
2817 return cf_get_default_interval ();
2818 } /* cdtime_t plugin_get_interval */
2822 void *(*start_routine) (void *);
2826 static void *plugin_thread_start (void *arg)
2828 plugin_thread_t *plugin_thread = arg;
2830 void *(*start_routine) (void *) = plugin_thread->start_routine;
2831 void *plugin_arg = plugin_thread->arg;
2833 plugin_set_ctx (plugin_thread->ctx);
2835 sfree (plugin_thread);
2837 return start_routine (plugin_arg);
2838 } /* void *plugin_thread_start */
2840 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2841 void *(*start_routine) (void *), void *arg)
2843 plugin_thread_t *plugin_thread;
2845 plugin_thread = malloc (sizeof (*plugin_thread));
2846 if (plugin_thread == NULL)
2849 plugin_thread->ctx = plugin_get_ctx ();
2850 plugin_thread->start_routine = start_routine;
2851 plugin_thread->arg = arg;
2853 return pthread_create (thread, attr,
2854 plugin_thread_start, plugin_thread);
2855 } /* int plugin_thread_create */
2857 /* vim: set sw=8 ts=8 noet fdm=marker : */