2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2014 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian Harl <sh at tokkee.org>
28 /* _GNU_SOURCE is needed in Linux to use pthread_setname_np */
35 #include "configfile.h"
36 #include "filter_chain.h"
37 #include "utils_avltree.h"
38 #include "utils_cache.h"
39 #include "utils_complain.h"
40 #include "utils_llist.h"
41 #include "utils_heap.h"
42 #include "utils_time.h"
43 #include "utils_random.h"
46 # include <pthread_np.h> /* for pthread_set_name_np(3) */
54 struct callback_func_s
60 typedef struct callback_func_s callback_func_t;
64 #define RF_REMOVE 65535
67 /* `read_func_t' "inherits" from `callback_func_t'.
68 * The `rf_super' member MUST be the first one in this structure! */
69 #define rf_callback rf_super.cf_callback
70 #define rf_udata rf_super.cf_udata
71 #define rf_ctx rf_super.cf_ctx
72 callback_func_t rf_super;
73 char rf_group[DATA_MAX_NAME_LEN];
77 cdtime_t rf_effective_interval;
78 cdtime_t rf_next_read;
80 typedef struct read_func_s read_func_t;
83 typedef struct write_queue_s write_queue_t;
91 struct flush_callback_s {
95 typedef struct flush_callback_s flush_callback_t;
100 static c_avl_tree_t *plugins_loaded = NULL;
102 static llist_t *list_init;
103 static llist_t *list_write;
104 static llist_t *list_flush;
105 static llist_t *list_missing;
106 static llist_t *list_shutdown;
107 static llist_t *list_log;
108 static llist_t *list_notification;
110 static fc_chain_t *pre_cache_chain = NULL;
111 static fc_chain_t *post_cache_chain = NULL;
113 static c_avl_tree_t *data_sets;
115 static char *plugindir = NULL;
117 #ifndef DEFAULT_MAX_READ_INTERVAL
118 # define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T_STATIC (86400)
120 static c_heap_t *read_heap = NULL;
121 static llist_t *read_list;
122 static int read_loop = 1;
123 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
124 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
125 static pthread_t *read_threads = NULL;
126 static int read_threads_num = 0;
127 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
129 static write_queue_t *write_queue_head;
130 static write_queue_t *write_queue_tail;
131 static long write_queue_length = 0;
132 static _Bool write_loop = 1;
133 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
134 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
135 static pthread_t *write_threads = NULL;
136 static size_t write_threads_num = 0;
138 static pthread_key_t plugin_ctx_key;
139 static _Bool plugin_ctx_key_initialized = 0;
141 static long write_limit_high = 0;
142 static long write_limit_low = 0;
144 static derive_t stats_values_dropped = 0;
145 static _Bool record_statistics = 0;
150 static int plugin_dispatch_values_internal (value_list_t *vl);
152 static const char *plugin_get_dir (void)
154 if (plugindir == NULL)
160 static void plugin_update_internal_statistics (void) { /* {{{ */
162 gauge_t copy_write_queue_length = (gauge_t) write_queue_length;
164 /* Initialize `vl' */
165 value_list_t vl = VALUE_LIST_INIT;
166 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
167 sstrncpy (vl.plugin, "collectd", sizeof (vl.plugin));
170 sstrncpy (vl.plugin_instance, "write_queue",
171 sizeof (vl.plugin_instance));
173 /* Write queue : queue length */
174 vl.values = &(value_t) { .gauge = copy_write_queue_length };
176 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
177 vl.type_instance[0] = 0;
178 plugin_dispatch_values (&vl);
180 /* Write queue : Values dropped (queue length > low limit) */
181 vl.values = &(value_t) { .gauge = (gauge_t) stats_values_dropped };
183 sstrncpy (vl.type, "derive", sizeof (vl.type));
184 sstrncpy (vl.type_instance, "dropped", sizeof (vl.type_instance));
185 plugin_dispatch_values (&vl);
188 sstrncpy (vl.plugin_instance, "cache",
189 sizeof (vl.plugin_instance));
191 /* Cache : Nb entry in cache tree */
192 vl.values = &(value_t) { .gauge = (gauge_t) uc_get_size() };
194 sstrncpy (vl.type, "cache_size", sizeof (vl.type));
195 vl.type_instance[0] = 0;
196 plugin_dispatch_values (&vl);
199 } /* }}} void plugin_update_internal_statistics */
201 static void destroy_callback (callback_func_t *cf) /* {{{ */
206 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
208 cf->cf_udata.free_func (cf->cf_udata.data);
209 cf->cf_udata.data = NULL;
210 cf->cf_udata.free_func = NULL;
213 } /* }}} void destroy_callback */
215 static void destroy_all_callbacks (llist_t **list) /* {{{ */
222 le = llist_head (*list);
230 destroy_callback (le->value);
236 llist_destroy (*list);
238 } /* }}} void destroy_all_callbacks */
240 static void destroy_read_heap (void) /* {{{ */
242 if (read_heap == NULL)
249 rf = c_heap_get_root (read_heap);
253 destroy_callback ((callback_func_t *) rf);
256 c_heap_destroy (read_heap);
258 } /* }}} void destroy_read_heap */
260 static int register_callback (llist_t **list, /* {{{ */
261 const char *name, callback_func_t *cf)
268 *list = llist_create ();
271 ERROR ("plugin: register_callback: "
272 "llist_create failed.");
273 destroy_callback (cf);
281 ERROR ("plugin: register_callback: strdup failed.");
282 destroy_callback (cf);
286 le = llist_search (*list, name);
289 le = llentry_create (key, cf);
292 ERROR ("plugin: register_callback: "
293 "llentry_create failed.");
295 destroy_callback (cf);
299 llist_append (*list, le);
303 callback_func_t *old_cf;
308 WARNING ("plugin: register_callback: "
309 "a callback named `%s' already exists - "
310 "overwriting the old entry!", name);
312 destroy_callback (old_cf);
317 } /* }}} int register_callback */
319 static void log_list_callbacks (llist_t **list, /* {{{ */
329 n = llist_size(*list);
332 INFO("%s [none]", comment);
336 keys = calloc(n, sizeof(char*));
340 ERROR("%s: failed to allocate memory for list of callbacks",
346 for (le = llist_head (*list), i = 0, len = 0;
351 len += strlen(le->key) + 6;
353 str = malloc(len + 10);
356 ERROR("%s: failed to allocate memory for list of callbacks",
362 strjoin(str, len, keys, n, "', '");
363 INFO("%s ['%s']", comment, str);
367 } /* }}} void log_list_callbacks */
369 static int create_register_callback (llist_t **list, /* {{{ */
370 const char *name, void *callback, user_data_t const *ud)
374 cf = calloc (1, sizeof (*cf));
377 ERROR ("plugin: create_register_callback: calloc failed.");
381 cf->cf_callback = callback;
384 cf->cf_udata.data = NULL;
385 cf->cf_udata.free_func = NULL;
392 cf->cf_ctx = plugin_get_ctx ();
394 return (register_callback (list, name, cf));
395 } /* }}} int create_register_callback */
397 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
404 e = llist_search (list, name);
408 llist_remove (list, e);
411 destroy_callback (e->value);
416 } /* }}} int plugin_unregister */
419 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
420 * object, but it will bitch about a shared object not having a
421 * ``module_register'' symbol..
423 static int plugin_load_file (char *file, uint32_t flags)
426 void (*reg_handle) (void);
429 lt_dlerror (); /* clear errors */
431 #if LIBTOOL_VERSION == 2
432 if (flags & PLUGIN_FLAGS_GLOBAL) {
434 lt_dladvise_init(&advise);
435 lt_dladvise_global(&advise);
436 dlh = lt_dlopenadvise(file, advise);
437 lt_dladvise_destroy(&advise);
439 dlh = lt_dlopen (file);
441 #else /* if LIBTOOL_VERSION == 1 */
442 if (flags & PLUGIN_FLAGS_GLOBAL)
443 WARNING ("plugin_load_file: The global flag is not supported, "
444 "libtool 2 is required for this.");
445 dlh = lt_dlopen (file);
450 char errbuf[1024] = "";
452 ssnprintf (errbuf, sizeof (errbuf),
453 "lt_dlopen (\"%s\") failed: %s. "
454 "The most common cause for this problem is "
455 "missing dependencies. Use ldd(1) to check "
456 "the dependencies of the plugin "
458 file, lt_dlerror ());
460 ERROR ("%s", errbuf);
461 /* Make sure this is printed to STDERR in any case, but also
462 * make sure it's printed only once. */
463 if (list_log != NULL)
464 fprintf (stderr, "ERROR: %s\n", errbuf);
469 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
471 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
472 file, lt_dlerror ());
482 static void *plugin_read_thread (void __attribute__((unused)) *args)
484 while (read_loop != 0)
487 plugin_ctx_t old_ctx;
495 /* Get the read function that needs to be read next.
496 * We don't need to hold "read_lock" for the heap, but we need
497 * to call c_heap_get_root() and pthread_cond_wait() in the
498 * same protected block. */
499 pthread_mutex_lock (&read_lock);
500 rf = c_heap_get_root (read_heap);
503 pthread_cond_wait (&read_cond, &read_lock);
504 pthread_mutex_unlock (&read_lock);
507 pthread_mutex_unlock (&read_lock);
509 if (rf->rf_interval == 0)
511 /* this should not happen, because the interval is set
512 * for each plugin when loading it
513 * XXX: issue a warning? */
514 rf->rf_interval = plugin_get_interval ();
515 rf->rf_effective_interval = rf->rf_interval;
517 rf->rf_next_read = cdtime ();
520 /* sleep until this entry is due,
521 * using pthread_cond_timedwait */
522 pthread_mutex_lock (&read_lock);
523 /* In pthread_cond_timedwait, spurious wakeups are possible
524 * (and really happen, at least on NetBSD with > 1 CPU), thus
525 * we need to re-evaluate the condition every time
526 * pthread_cond_timedwait returns. */
528 while ((read_loop != 0)
529 && (cdtime () < rf->rf_next_read)
532 rc = pthread_cond_timedwait (&read_cond, &read_lock,
533 &CDTIME_T_TO_TIMESPEC (rf->rf_next_read));
536 /* Must hold `read_lock' when accessing `rf->rf_type'. */
537 rf_type = rf->rf_type;
538 pthread_mutex_unlock (&read_lock);
540 /* Check if we're supposed to stop.. This may have interrupted
544 /* Insert `rf' again, so it can be free'd correctly */
545 c_heap_insert (read_heap, rf);
549 /* The entry has been marked for deletion. The linked list
550 * entry has already been removed by `plugin_unregister_read'.
551 * All we have to do here is free the `read_func_t' and
553 if (rf_type == RF_REMOVE)
555 DEBUG ("plugin_read_thread: Destroying the `%s' "
556 "callback.", rf->rf_name);
558 destroy_callback ((callback_func_t *) rf);
563 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
567 old_ctx = plugin_set_ctx (rf->rf_ctx);
569 if (rf_type == RF_SIMPLE)
571 int (*callback) (void);
573 callback = rf->rf_callback;
574 status = (*callback) ();
578 plugin_read_cb callback;
580 assert (rf_type == RF_COMPLEX);
582 callback = rf->rf_callback;
583 status = (*callback) (&rf->rf_udata);
586 plugin_set_ctx (old_ctx);
588 /* If the function signals failure, we will increase the
589 * intervals in which it will be called. */
592 rf->rf_effective_interval *= 2;
593 if (rf->rf_effective_interval > max_read_interval)
594 rf->rf_effective_interval = max_read_interval;
596 NOTICE ("read-function of plugin `%s' failed. "
597 "Will suspend it for %.3f seconds.",
599 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
603 /* Success: Restore the interval, if it was changed. */
604 rf->rf_effective_interval = rf->rf_interval;
607 /* update the ``next read due'' field */
610 /* calculate the time spent in the read function */
611 elapsed = (now - start);
613 if (elapsed > rf->rf_effective_interval)
614 WARNING ("plugin_read_thread: read-function of the `%s' plugin took %.3f "
615 "seconds, which is above its read interval (%.3f seconds). You might "
616 "want to adjust the `Interval' or `ReadThreads' settings.",
617 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed),
618 CDTIME_T_TO_DOUBLE(rf->rf_effective_interval));
620 DEBUG ("plugin_read_thread: read-function of the `%s' plugin took "
622 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed));
624 DEBUG ("plugin_read_thread: Effective interval of the "
625 "`%s' plugin is %.3f seconds.",
627 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
629 /* Calculate the next (absolute) time at which this function
630 * should be called. */
631 rf->rf_next_read += rf->rf_effective_interval;
633 /* Check, if `rf_next_read' is in the past. */
634 if (rf->rf_next_read < now)
636 /* `rf_next_read' is in the past. Insert `now'
637 * so this value doesn't trail off into the
639 rf->rf_next_read = now;
642 DEBUG ("plugin_read_thread: Next read of the `%s' plugin at %.3f.",
644 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
646 /* Re-insert this read function into the heap again. */
647 c_heap_insert (read_heap, rf);
648 } /* while (read_loop) */
652 } /* void *plugin_read_thread */
654 static void start_read_threads (int num)
656 if (read_threads != NULL)
659 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
660 if (read_threads == NULL)
662 ERROR ("plugin: start_read_threads: calloc failed.");
666 read_threads_num = 0;
667 for (int i = 0; i < num; i++)
669 if (pthread_create (read_threads + read_threads_num, NULL,
670 plugin_read_thread, NULL) == 0)
672 #if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP)
673 char thread_name[32];
674 ssnprintf(thread_name, sizeof (thread_name),
675 "plugin reader#%d", i);
676 # if defined(HAVE_PTHREAD_SETNAME_NP)
677 pthread_setname_np (*(read_threads + read_threads_num),
679 # elif defined(HAVE_PTHREAD_SET_NAME_NP)
680 pthread_set_name_np (*(read_threads + read_threads_num),
688 ERROR ("plugin: start_read_threads: pthread_create failed.");
692 } /* void start_read_threads */
694 static void stop_read_threads (void)
696 if (read_threads == NULL)
699 INFO ("collectd: Stopping %i read threads.", read_threads_num);
701 pthread_mutex_lock (&read_lock);
703 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
704 pthread_cond_broadcast (&read_cond);
705 pthread_mutex_unlock (&read_lock);
707 for (int i = 0; i < read_threads_num; i++)
709 if (pthread_join (read_threads[i], NULL) != 0)
711 ERROR ("plugin: stop_read_threads: pthread_join failed.");
713 read_threads[i] = (pthread_t) 0;
715 sfree (read_threads);
716 read_threads_num = 0;
717 } /* void stop_read_threads */
719 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
724 meta_data_destroy (vl->meta);
727 } /* }}} void plugin_value_list_free */
729 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
736 vl = malloc (sizeof (*vl));
739 memcpy (vl, vl_orig, sizeof (*vl));
741 if (vl->host[0] == 0)
742 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
744 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
745 if (vl->values == NULL)
747 plugin_value_list_free (vl);
750 memcpy (vl->values, vl_orig->values,
751 vl_orig->values_len * sizeof (*vl->values));
753 vl->meta = meta_data_clone (vl->meta);
754 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
756 plugin_value_list_free (vl);
761 vl->time = cdtime ();
763 /* Fill in the interval from the thread context, if it is zero. */
764 if (vl->interval == 0)
766 plugin_ctx_t ctx = plugin_get_ctx ();
768 if (ctx.interval != 0)
769 vl->interval = ctx.interval;
772 char name[6 * DATA_MAX_NAME_LEN];
773 FORMAT_VL (name, sizeof (name), vl);
774 ERROR ("plugin_value_list_clone: Unable to determine "
775 "interval from context for "
776 "value list \"%s\". "
777 "This indicates a broken plugin. "
778 "Please report this problem to the "
779 "collectd mailing list or at "
780 "<http://collectd.org/bugs/>.", name);
781 vl->interval = cf_get_default_interval ();
786 } /* }}} value_list_t *plugin_value_list_clone */
788 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
792 q = malloc (sizeof (*q));
797 q->vl = plugin_value_list_clone (vl);
804 /* Store context of caller (read plugin); otherwise, it would not be
805 * available to the write plugins when actually dispatching the
806 * value-list later on. */
807 q->ctx = plugin_get_ctx ();
809 pthread_mutex_lock (&write_lock);
811 if (write_queue_tail == NULL)
813 write_queue_head = q;
814 write_queue_tail = q;
815 write_queue_length = 1;
819 write_queue_tail->next = q;
820 write_queue_tail = q;
821 write_queue_length += 1;
824 pthread_cond_signal (&write_cond);
825 pthread_mutex_unlock (&write_lock);
828 } /* }}} int plugin_write_enqueue */
830 static value_list_t *plugin_write_dequeue (void) /* {{{ */
835 pthread_mutex_lock (&write_lock);
837 while (write_loop && (write_queue_head == NULL))
838 pthread_cond_wait (&write_cond, &write_lock);
840 if (write_queue_head == NULL)
842 pthread_mutex_unlock (&write_lock);
846 q = write_queue_head;
847 write_queue_head = q->next;
848 write_queue_length -= 1;
849 if (write_queue_head == NULL) {
850 write_queue_tail = NULL;
851 assert(0 == write_queue_length);
854 pthread_mutex_unlock (&write_lock);
856 (void) plugin_set_ctx (q->ctx);
861 } /* }}} value_list_t *plugin_write_dequeue */
863 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
867 value_list_t *vl = plugin_write_dequeue ();
871 plugin_dispatch_values_internal (vl);
873 plugin_value_list_free (vl);
878 } /* }}} void *plugin_write_thread */
880 static void start_write_threads (size_t num) /* {{{ */
882 if (write_threads != NULL)
885 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
886 if (write_threads == NULL)
888 ERROR ("plugin: start_write_threads: calloc failed.");
892 write_threads_num = 0;
893 for (size_t i = 0; i < num; i++)
897 status = pthread_create (write_threads + write_threads_num,
904 ERROR ("plugin: start_write_threads: pthread_create failed "
905 "with status %i (%s).", status,
906 sstrerror (status, errbuf, sizeof (errbuf)));
909 #if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP)
910 char thread_name[16];
911 sstrncpy (thread_name, "plugin writer", sizeof(thread_name));
912 # if defined(HAVE_PTHREAD_SETNAME_NP)
913 pthread_setname_np (*(write_threads + write_threads_num),
915 # elif defined(HAVE_PTHREAD_SET_NAME_NP)
916 pthread_set_name_np (*(write_threads + write_threads_num),
923 } /* }}} void start_write_threads */
925 static void stop_write_threads (void) /* {{{ */
930 if (write_threads == NULL)
933 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
935 pthread_mutex_lock (&write_lock);
937 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
938 pthread_cond_broadcast (&write_cond);
939 pthread_mutex_unlock (&write_lock);
941 for (i = 0; i < write_threads_num; i++)
943 if (pthread_join (write_threads[i], NULL) != 0)
945 ERROR ("plugin: stop_write_threads: pthread_join failed.");
947 write_threads[i] = (pthread_t) 0;
949 sfree (write_threads);
950 write_threads_num = 0;
952 pthread_mutex_lock (&write_lock);
954 for (q = write_queue_head; q != NULL; )
956 write_queue_t *q1 = q;
957 plugin_value_list_free (q->vl);
962 write_queue_head = NULL;
963 write_queue_tail = NULL;
964 write_queue_length = 0;
965 pthread_mutex_unlock (&write_lock);
969 WARNING ("plugin: %zu value list%s left after shutting down "
970 "the write threads.",
971 i, (i == 1) ? " was" : "s were");
973 } /* }}} void stop_write_threads */
978 void plugin_set_dir (const char *dir)
988 plugindir = strdup (dir);
989 if (plugindir == NULL)
990 ERROR ("plugin_set_dir: strdup(\"%s\") failed", dir);
993 static _Bool plugin_is_loaded (char const *name)
997 if (plugins_loaded == NULL)
998 plugins_loaded = c_avl_create ((int (*) (const void *, const void *)) strcasecmp);
999 assert (plugins_loaded != NULL);
1001 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
1002 return (status == 0);
1005 static int plugin_mark_loaded (char const *name)
1010 name_copy = strdup (name);
1011 if (name_copy == NULL)
1014 status = c_avl_insert (plugins_loaded,
1015 /* key = */ name_copy, /* value = */ NULL);
1019 static void plugin_free_loaded (void)
1024 if (plugins_loaded == NULL)
1027 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
1030 assert (value == NULL);
1033 c_avl_destroy (plugins_loaded);
1034 plugins_loaded = NULL;
1038 int plugin_load (char const *plugin_name, uint32_t flags)
1042 char filename[BUFSIZE] = "";
1043 char typename[BUFSIZE];
1045 struct stat statbuf;
1049 if (plugin_name == NULL)
1052 /* Check if plugin is already loaded and don't do anything in this
1054 if (plugin_is_loaded (plugin_name))
1057 dir = plugin_get_dir ();
1061 * XXX: Magic at work:
1063 * Some of the language bindings, for example the Python and Perl
1064 * plugins, need to be able to export symbols to the scripts they run.
1065 * For this to happen, the "Globals" flag needs to be set.
1066 * Unfortunately, this technical detail is hard to explain to the
1067 * average user and she shouldn't have to worry about this, ideally.
1068 * So in order to save everyone's sanity use a different default for a
1069 * handful of special plugins. --octo
1071 if ((strcasecmp ("perl", plugin_name) == 0)
1072 || (strcasecmp ("python", plugin_name) == 0))
1073 flags |= PLUGIN_FLAGS_GLOBAL;
1075 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
1076 * type when matching the filename */
1077 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
1078 if ((status < 0) || ((size_t) status >= sizeof (typename)))
1080 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
1084 if ((dh = opendir (dir)) == NULL)
1087 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
1088 sstrerror (errno, errbuf, sizeof (errbuf)));
1092 while ((de = readdir (dh)) != NULL)
1094 if (strcasecmp (de->d_name, typename))
1097 status = ssnprintf (filename, sizeof (filename),
1098 "%s/%s", dir, de->d_name);
1099 if ((status < 0) || ((size_t) status >= sizeof (filename)))
1101 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
1106 if (lstat (filename, &statbuf) == -1)
1109 WARNING ("plugin_load: stat (\"%s\") failed: %s",
1111 sstrerror (errno, errbuf, sizeof (errbuf)));
1114 else if (!S_ISREG (statbuf.st_mode))
1116 /* don't follow symlinks */
1117 WARNING ("plugin_load: %s is not a regular file.",
1122 status = plugin_load_file (filename, flags);
1126 plugin_mark_loaded (plugin_name);
1128 INFO ("plugin_load: plugin \"%s\" successfully loaded.", plugin_name);
1133 ERROR ("plugin_load: Load plugin \"%s\" failed with "
1134 "status %i.", plugin_name, status);
1140 if (filename[0] == 0)
1141 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1148 * The `register_*' functions follow
1150 int plugin_register_config (const char *name,
1151 int (*callback) (const char *key, const char *val),
1152 const char **keys, int keys_num)
1154 cf_register (name, callback, keys, keys_num);
1156 } /* int plugin_register_config */
1158 int plugin_register_complex_config (const char *type,
1159 int (*callback) (oconfig_item_t *))
1161 return (cf_register_complex (type, callback));
1162 } /* int plugin_register_complex_config */
1164 int plugin_register_init (const char *name,
1165 int (*callback) (void))
1167 return (create_register_callback (&list_init, name, (void *) callback,
1168 /* user_data = */ NULL));
1169 } /* plugin_register_init */
1171 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1173 const read_func_t *rf0;
1174 const read_func_t *rf1;
1179 if (rf0->rf_next_read < rf1->rf_next_read)
1181 else if (rf0->rf_next_read > rf1->rf_next_read)
1185 } /* int plugin_compare_read_func */
1187 /* Add a read function to both, the heap and a linked list. The linked list if
1188 * used to look-up read functions, especially for the remove function. The heap
1189 * is used to determine which plugin to read next. */
1190 static int plugin_insert_read (read_func_t *rf)
1195 rf->rf_next_read = cdtime ();
1196 rf->rf_effective_interval = rf->rf_interval;
1198 pthread_mutex_lock (&read_lock);
1200 if (read_list == NULL)
1202 read_list = llist_create ();
1203 if (read_list == NULL)
1205 pthread_mutex_unlock (&read_lock);
1206 ERROR ("plugin_insert_read: read_list failed.");
1211 if (read_heap == NULL)
1213 read_heap = c_heap_create (plugin_compare_read_func);
1214 if (read_heap == NULL)
1216 pthread_mutex_unlock (&read_lock);
1217 ERROR ("plugin_insert_read: c_heap_create failed.");
1222 le = llist_search (read_list, rf->rf_name);
1225 pthread_mutex_unlock (&read_lock);
1226 WARNING ("The read function \"%s\" is already registered. "
1227 "Check for duplicate \"LoadPlugin\" lines "
1228 "in your configuration!",
1233 le = llentry_create (rf->rf_name, rf);
1236 pthread_mutex_unlock (&read_lock);
1237 ERROR ("plugin_insert_read: llentry_create failed.");
1241 status = c_heap_insert (read_heap, rf);
1244 pthread_mutex_unlock (&read_lock);
1245 ERROR ("plugin_insert_read: c_heap_insert failed.");
1246 llentry_destroy (le);
1250 /* This does not fail. */
1251 llist_append (read_list, le);
1253 /* Wake up all the read threads. */
1254 pthread_cond_broadcast (&read_cond);
1255 pthread_mutex_unlock (&read_lock);
1257 } /* int plugin_insert_read */
1259 int plugin_register_read (const char *name,
1260 int (*callback) (void))
1265 rf = calloc (1, sizeof (*rf));
1268 ERROR ("plugin_register_read: calloc failed.");
1272 rf->rf_callback = (void *) callback;
1273 rf->rf_udata.data = NULL;
1274 rf->rf_udata.free_func = NULL;
1275 rf->rf_ctx = plugin_get_ctx ();
1276 rf->rf_group[0] = '\0';
1277 rf->rf_name = strdup (name);
1278 rf->rf_type = RF_SIMPLE;
1279 rf->rf_interval = plugin_get_interval ();
1281 status = plugin_insert_read (rf);
1283 sfree (rf->rf_name);
1288 } /* int plugin_register_read */
1290 int plugin_register_complex_read (const char *group, const char *name,
1291 plugin_read_cb callback,
1293 user_data_t const *user_data)
1298 rf = calloc (1,sizeof (*rf));
1301 ERROR ("plugin_register_complex_read: calloc failed.");
1305 rf->rf_callback = (void *) callback;
1307 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1309 rf->rf_group[0] = '\0';
1310 rf->rf_name = strdup (name);
1311 rf->rf_type = RF_COMPLEX;
1312 rf->rf_interval = (interval != 0) ? interval : plugin_get_interval ();
1315 if (user_data == NULL)
1317 rf->rf_udata.data = NULL;
1318 rf->rf_udata.free_func = NULL;
1322 rf->rf_udata = *user_data;
1325 rf->rf_ctx = plugin_get_ctx ();
1327 status = plugin_insert_read (rf);
1329 sfree (rf->rf_name);
1334 } /* int plugin_register_complex_read */
1336 int plugin_register_write (const char *name,
1337 plugin_write_cb callback, user_data_t const *ud)
1339 return (create_register_callback (&list_write, name,
1340 (void *) callback, ud));
1341 } /* int plugin_register_write */
1343 static int plugin_flush_timeout_callback (user_data_t *ud)
1345 flush_callback_t *cb = ud->data;
1347 return plugin_flush (cb->name, cb->timeout, /* identifier = */ NULL);
1348 } /* static int plugin_flush_callback */
1350 static void plugin_flush_timeout_callback_free (void *data)
1352 flush_callback_t *cb = data;
1354 if (cb == NULL) return;
1358 } /* static void plugin_flush_callback_free */
1360 static char *plugin_flush_callback_name (const char *name)
1362 const char *flush_prefix = "flush/";
1367 prefix_size = strlen(flush_prefix);
1368 name_size = strlen(name);
1370 flush_name = malloc (name_size + prefix_size + 1);
1371 if (flush_name == NULL)
1373 ERROR ("plugin_flush_callback_name: malloc failed.");
1377 sstrncpy (flush_name, flush_prefix, prefix_size + 1);
1378 sstrncpy (flush_name + prefix_size, name, name_size + 1);
1381 } /* static char *plugin_flush_callback_name */
1383 int plugin_register_flush (const char *name,
1384 plugin_flush_cb callback, user_data_t const *ud)
1387 plugin_ctx_t ctx = plugin_get_ctx ();
1389 status = create_register_callback (&list_flush, name,
1390 (void *) callback, ud);
1394 if (ctx.flush_interval != 0)
1397 flush_callback_t *cb;
1399 flush_name = plugin_flush_callback_name (name);
1400 if (flush_name == NULL)
1403 cb = malloc(sizeof (*cb));
1406 ERROR ("plugin_register_flush: malloc failed.");
1411 cb->name = strdup (name);
1412 if (cb->name == NULL)
1414 ERROR ("plugin_register_flush: strdup failed.");
1419 cb->timeout = ctx.flush_timeout;
1421 status = plugin_register_complex_read (
1422 /* group = */ "flush",
1423 /* name = */ flush_name,
1424 /* callback = */ plugin_flush_timeout_callback,
1425 /* interval = */ ctx.flush_interval,
1426 /* user data = */ &(user_data_t) {
1428 .free_func = plugin_flush_timeout_callback_free,
1441 } /* int plugin_register_flush */
1443 int plugin_register_missing (const char *name,
1444 plugin_missing_cb callback, user_data_t const *ud)
1446 return (create_register_callback (&list_missing, name,
1447 (void *) callback, ud));
1448 } /* int plugin_register_missing */
1450 int plugin_register_shutdown (const char *name,
1451 int (*callback) (void))
1453 return (create_register_callback (&list_shutdown, name,
1454 (void *) callback, /* user_data = */ NULL));
1455 } /* int plugin_register_shutdown */
1457 static void plugin_free_data_sets (void)
1462 if (data_sets == NULL)
1465 while (c_avl_pick (data_sets, &key, &value) == 0)
1467 data_set_t *ds = value;
1468 /* key is a pointer to ds->type */
1474 c_avl_destroy (data_sets);
1476 } /* void plugin_free_data_sets */
1478 int plugin_register_data_set (const data_set_t *ds)
1480 data_set_t *ds_copy;
1482 if ((data_sets != NULL)
1483 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1485 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1486 plugin_unregister_data_set (ds->type);
1488 else if (data_sets == NULL)
1490 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1491 if (data_sets == NULL)
1495 ds_copy = malloc (sizeof (*ds_copy));
1496 if (ds_copy == NULL)
1498 memcpy(ds_copy, ds, sizeof (data_set_t));
1500 ds_copy->ds = malloc (sizeof (*ds_copy->ds)
1502 if (ds_copy->ds == NULL)
1508 for (size_t i = 0; i < ds->ds_num; i++)
1509 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1511 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1512 } /* int plugin_register_data_set */
1514 int plugin_register_log (const char *name,
1515 plugin_log_cb callback, user_data_t const *ud)
1517 return (create_register_callback (&list_log, name,
1518 (void *) callback, ud));
1519 } /* int plugin_register_log */
1521 int plugin_register_notification (const char *name,
1522 plugin_notification_cb callback, user_data_t const *ud)
1524 return (create_register_callback (&list_notification, name,
1525 (void *) callback, ud));
1526 } /* int plugin_register_log */
1528 int plugin_unregister_config (const char *name)
1530 cf_unregister (name);
1532 } /* int plugin_unregister_config */
1534 int plugin_unregister_complex_config (const char *name)
1536 cf_unregister_complex (name);
1538 } /* int plugin_unregister_complex_config */
1540 int plugin_unregister_init (const char *name)
1542 return (plugin_unregister (list_init, name));
1545 int plugin_unregister_read (const char *name) /* {{{ */
1553 pthread_mutex_lock (&read_lock);
1555 if (read_list == NULL)
1557 pthread_mutex_unlock (&read_lock);
1561 le = llist_search (read_list, name);
1564 pthread_mutex_unlock (&read_lock);
1565 WARNING ("plugin_unregister_read: No such read function: %s",
1570 llist_remove (read_list, le);
1573 assert (rf != NULL);
1574 rf->rf_type = RF_REMOVE;
1576 pthread_mutex_unlock (&read_lock);
1578 llentry_destroy (le);
1580 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1583 } /* }}} int plugin_unregister_read */
1585 void plugin_log_available_writers (void)
1587 log_list_callbacks (&list_write, "Available write targets:");
1590 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1592 read_func_t *rf = e->value;
1595 return strcmp (rf->rf_group, (const char *)group);
1596 } /* }}} int compare_read_func_group */
1598 int plugin_unregister_read_group (const char *group) /* {{{ */
1608 pthread_mutex_lock (&read_lock);
1610 if (read_list == NULL)
1612 pthread_mutex_unlock (&read_lock);
1618 le = llist_search_custom (read_list,
1619 compare_read_func_group, (void *)group);
1626 llist_remove (read_list, le);
1629 assert (rf != NULL);
1630 rf->rf_type = RF_REMOVE;
1632 llentry_destroy (le);
1634 DEBUG ("plugin_unregister_read_group: "
1635 "Marked `%s' (group `%s') for removal.",
1636 rf->rf_name, group);
1639 pthread_mutex_unlock (&read_lock);
1643 WARNING ("plugin_unregister_read_group: No such "
1644 "group of read function: %s", group);
1649 } /* }}} int plugin_unregister_read_group */
1651 int plugin_unregister_write (const char *name)
1653 return (plugin_unregister (list_write, name));
1656 int plugin_unregister_flush (const char *name)
1658 plugin_ctx_t ctx = plugin_get_ctx ();
1660 if (ctx.flush_interval != 0)
1664 flush_name = plugin_flush_callback_name (name);
1665 if (flush_name != NULL)
1667 plugin_unregister_read(flush_name);
1672 return plugin_unregister (list_flush, name);
1675 int plugin_unregister_missing (const char *name)
1677 return (plugin_unregister (list_missing, name));
1680 int plugin_unregister_shutdown (const char *name)
1682 return (plugin_unregister (list_shutdown, name));
1685 int plugin_unregister_data_set (const char *name)
1689 if (data_sets == NULL)
1692 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1699 } /* int plugin_unregister_data_set */
1701 int plugin_unregister_log (const char *name)
1703 return (plugin_unregister (list_log, name));
1706 int plugin_unregister_notification (const char *name)
1708 return (plugin_unregister (list_notification, name));
1711 int plugin_init_all (void)
1713 char const *chain_name;
1718 /* Init the value cache */
1721 if (IS_TRUE (global_option_get ("CollectInternalStats")))
1722 record_statistics = 1;
1724 chain_name = global_option_get ("PreCacheChain");
1725 pre_cache_chain = fc_chain_get_by_name (chain_name);
1727 chain_name = global_option_get ("PostCacheChain");
1728 post_cache_chain = fc_chain_get_by_name (chain_name);
1730 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1732 if (write_limit_high < 0)
1734 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1735 write_limit_high = 0;
1738 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1739 /* default = */ write_limit_high / 2);
1740 if (write_limit_low < 0)
1742 ERROR ("WriteQueueLimitLow must be positive or zero.");
1743 write_limit_low = write_limit_high / 2;
1745 else if (write_limit_low > write_limit_high)
1747 ERROR ("WriteQueueLimitLow must not be larger than "
1748 "WriteQueueLimitHigh.");
1749 write_limit_low = write_limit_high;
1752 write_threads_num = global_option_get_long ("WriteThreads",
1754 if (write_threads_num < 1)
1756 ERROR ("WriteThreads must be positive.");
1757 write_threads_num = 5;
1760 if ((list_init == NULL) && (read_heap == NULL))
1763 /* Calling all init callbacks before checking if read callbacks
1764 * are available allows the init callbacks to register the read
1766 le = llist_head (list_init);
1769 callback_func_t *cf;
1770 plugin_init_cb callback;
1771 plugin_ctx_t old_ctx;
1774 old_ctx = plugin_set_ctx (cf->cf_ctx);
1775 callback = cf->cf_callback;
1776 status = (*callback) ();
1777 plugin_set_ctx (old_ctx);
1781 ERROR ("Initialization of plugin `%s' "
1782 "failed with status %i. "
1783 "Plugin will be unloaded.",
1785 /* Plugins that register read callbacks from the init
1786 * callback should take care of appropriate error
1787 * handling themselves. */
1788 /* FIXME: Unload _all_ functions */
1789 plugin_unregister_read (le->key);
1796 start_write_threads ((size_t) write_threads_num);
1798 max_read_interval = global_option_get_time ("MaxReadInterval",
1799 DEFAULT_MAX_READ_INTERVAL);
1801 /* Start read-threads */
1802 if (read_heap != NULL)
1807 rt = global_option_get ("ReadThreads");
1810 start_read_threads ((num > 0) ? num : 5);
1813 } /* void plugin_init_all */
1815 /* TODO: Rename this function. */
1816 void plugin_read_all (void)
1818 if(record_statistics) {
1819 plugin_update_internal_statistics ();
1821 uc_check_timeout ();
1824 } /* void plugin_read_all */
1826 /* Read function called when the `-T' command line argument is given. */
1827 int plugin_read_all_once (void)
1830 int return_status = 0;
1832 if (read_heap == NULL)
1834 NOTICE ("No read-functions are registered.");
1841 plugin_ctx_t old_ctx;
1843 rf = c_heap_get_root (read_heap);
1847 old_ctx = plugin_set_ctx (rf->rf_ctx);
1849 if (rf->rf_type == RF_SIMPLE)
1851 int (*callback) (void);
1853 callback = rf->rf_callback;
1854 status = (*callback) ();
1858 plugin_read_cb callback;
1860 callback = rf->rf_callback;
1861 status = (*callback) (&rf->rf_udata);
1864 plugin_set_ctx (old_ctx);
1868 NOTICE ("read-function of plugin `%s' failed.",
1873 sfree (rf->rf_name);
1874 destroy_callback ((void *) rf);
1877 return (return_status);
1878 } /* int plugin_read_all_once */
1880 int plugin_write (const char *plugin, /* {{{ */
1881 const data_set_t *ds, const value_list_t *vl)
1889 if (list_write == NULL)
1894 ds = plugin_get_ds (vl->type);
1897 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1907 le = llist_head (list_write);
1910 callback_func_t *cf = le->value;
1911 plugin_write_cb callback;
1913 /* do not switch plugin context; rather keep the context (interval)
1914 * information of the calling read plugin */
1916 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1917 callback = cf->cf_callback;
1918 status = (*callback) (ds, vl, &cf->cf_udata);
1927 if ((success == 0) && (failure != 0))
1932 else /* plugin != NULL */
1934 callback_func_t *cf;
1935 plugin_write_cb callback;
1937 le = llist_head (list_write);
1940 if (strcasecmp (plugin, le->key) == 0)
1951 /* do not switch plugin context; rather keep the context (interval)
1952 * information of the calling read plugin */
1954 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1955 callback = cf->cf_callback;
1956 status = (*callback) (ds, vl, &cf->cf_udata);
1960 } /* }}} int plugin_write */
1962 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1966 if (list_flush == NULL)
1969 le = llist_head (list_flush);
1972 callback_func_t *cf;
1973 plugin_flush_cb callback;
1974 plugin_ctx_t old_ctx;
1976 if ((plugin != NULL)
1977 && (strcmp (plugin, le->key) != 0))
1984 old_ctx = plugin_set_ctx (cf->cf_ctx);
1985 callback = cf->cf_callback;
1987 (*callback) (timeout, identifier, &cf->cf_udata);
1989 plugin_set_ctx (old_ctx);
1994 } /* int plugin_flush */
1996 int plugin_shutdown_all (void)
1999 int ret = 0; // Assume success.
2001 destroy_all_callbacks (&list_init);
2003 stop_read_threads ();
2005 pthread_mutex_lock (&read_lock);
2006 llist_destroy (read_list);
2008 pthread_mutex_unlock (&read_lock);
2010 destroy_read_heap ();
2012 /* blocks until all write threads have shut down. */
2013 stop_write_threads ();
2015 /* ask all plugins to write out the state they kept. */
2016 plugin_flush (/* plugin = */ NULL,
2018 /* identifier = */ NULL);
2021 if (list_shutdown != NULL)
2022 le = llist_head (list_shutdown);
2026 callback_func_t *cf;
2027 plugin_shutdown_cb callback;
2028 plugin_ctx_t old_ctx;
2031 old_ctx = plugin_set_ctx (cf->cf_ctx);
2032 callback = cf->cf_callback;
2034 /* Advance the pointer before calling the callback allows
2035 * shutdown functions to unregister themselves. If done the
2036 * other way around the memory `le' points to will be freed
2037 * after callback returns. */
2040 if ((*callback) () != 0)
2043 plugin_set_ctx (old_ctx);
2046 /* Write plugins which use the `user_data' pointer usually need the
2047 * same data available to the flush callback. If this is the case, set
2048 * the free_function to NULL when registering the flush callback and to
2049 * the real free function when registering the write callback. This way
2050 * the data isn't freed twice. */
2051 destroy_all_callbacks (&list_flush);
2052 destroy_all_callbacks (&list_missing);
2053 destroy_all_callbacks (&list_write);
2055 destroy_all_callbacks (&list_notification);
2056 destroy_all_callbacks (&list_shutdown);
2057 destroy_all_callbacks (&list_log);
2059 plugin_free_loaded ();
2060 plugin_free_data_sets ();
2062 } /* void plugin_shutdown_all */
2064 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
2068 if (list_missing == NULL)
2071 le = llist_head (list_missing);
2074 callback_func_t *cf;
2075 plugin_missing_cb callback;
2076 plugin_ctx_t old_ctx;
2080 old_ctx = plugin_set_ctx (cf->cf_ctx);
2081 callback = cf->cf_callback;
2083 status = (*callback) (vl, &cf->cf_udata);
2084 plugin_set_ctx (old_ctx);
2089 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
2090 "failed with status %i.",
2103 } /* int }}} plugin_dispatch_missing */
2105 static int plugin_dispatch_values_internal (value_list_t *vl)
2108 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
2112 _Bool free_meta_data = 0;
2114 assert (vl != NULL);
2116 /* These fields are initialized by plugin_value_list_clone() if needed: */
2117 assert (vl->host[0] != 0);
2118 assert (vl->time != 0); /* The time is determined at _enqueue_ time. */
2119 assert (vl->interval != 0);
2121 if (vl->type[0] == 0 || vl->values == NULL || vl->values_len < 1)
2123 ERROR ("plugin_dispatch_values: Invalid value list "
2124 "from plugin %s.", vl->plugin);
2128 /* Free meta data only if the calling function didn't specify any. In
2129 * this case matches and targets may add some and the calling function
2130 * may not expect (and therefore free) that data. */
2131 if (vl->meta == NULL)
2134 if (list_write == NULL)
2135 c_complain_once (LOG_WARNING, &no_write_complaint,
2136 "plugin_dispatch_values: No write callback has been "
2137 "registered. Please load at least one output plugin, "
2138 "if you want the collected data to be stored.");
2140 if (data_sets == NULL)
2142 ERROR ("plugin_dispatch_values: No data sets registered. "
2143 "Could the types database be read? Check "
2144 "your `TypesDB' setting!");
2148 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
2150 char ident[6 * DATA_MAX_NAME_LEN];
2152 FORMAT_VL (ident, sizeof (ident), vl);
2153 INFO ("plugin_dispatch_values: Dataset not found: %s "
2154 "(from \"%s\"), check your types.db!",
2159 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
2161 "plugin = %s; plugin_instance = %s; "
2162 "type = %s; type_instance = %s;",
2163 CDTIME_T_TO_DOUBLE (vl->time),
2164 CDTIME_T_TO_DOUBLE (vl->interval),
2166 vl->plugin, vl->plugin_instance,
2167 vl->type, vl->type_instance);
2170 assert (0 == strcmp (ds->type, vl->type));
2172 if (0 != strcmp (ds->type, vl->type))
2173 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
2174 ds->type, vl->type);
2178 assert (ds->ds_num == vl->values_len);
2180 if (ds->ds_num != vl->values_len)
2182 ERROR ("plugin_dispatch_values: ds->type = %s: "
2183 "(ds->ds_num = %zu) != "
2184 "(vl->values_len = %zu)",
2185 ds->type, ds->ds_num, vl->values_len);
2190 escape_slashes (vl->host, sizeof (vl->host));
2191 escape_slashes (vl->plugin, sizeof (vl->plugin));
2192 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
2193 escape_slashes (vl->type, sizeof (vl->type));
2194 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
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)
2210 /* Update the value cache */
2213 if (post_cache_chain != NULL)
2215 status = fc_process_chain (ds, vl, post_cache_chain);
2218 WARNING ("plugin_dispatch_values: Running the "
2219 "post-cache chain failed with "
2225 fc_default_action (ds, vl);
2227 if ((free_meta_data != 0) && (vl->meta != NULL))
2229 meta_data_destroy (vl->meta);
2234 } /* int plugin_dispatch_values_internal */
2236 static double get_drop_probability (void) /* {{{ */
2242 pthread_mutex_lock (&write_lock);
2243 wql = write_queue_length;
2244 pthread_mutex_unlock (&write_lock);
2246 if (wql < write_limit_low)
2248 if (wql >= write_limit_high)
2251 pos = 1 + wql - write_limit_low;
2252 size = 1 + write_limit_high - write_limit_low;
2254 return (((double) pos) / ((double) size));
2255 } /* }}} double get_drop_probability */
2257 static _Bool check_drop_value (void) /* {{{ */
2259 static cdtime_t last_message_time = 0;
2260 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2266 if (write_limit_high == 0)
2269 p = get_drop_probability ();
2273 status = pthread_mutex_trylock (&last_message_lock);
2279 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2281 last_message_time = now;
2282 ERROR ("plugin_dispatch_values: Low water mark "
2283 "reached. Dropping %.0f%% of metrics.",
2286 pthread_mutex_unlock (&last_message_lock);
2297 } /* }}} _Bool check_drop_value */
2299 int plugin_dispatch_values (value_list_t const *vl)
2302 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2304 if (check_drop_value ()) {
2305 if(record_statistics) {
2306 pthread_mutex_lock(&statistics_lock);
2307 stats_values_dropped++;
2308 pthread_mutex_unlock(&statistics_lock);
2313 status = plugin_write_enqueue (vl);
2317 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2318 "with status %i (%s).", status,
2319 sstrerror (status, errbuf, sizeof (errbuf)));
2326 __attribute__((sentinel))
2327 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2328 _Bool store_percentage, int store_type, ...)
2335 assert (template->values_len == 1);
2337 /* Calculate sum for Gauge to calculate percent if needed */
2338 if (DS_TYPE_GAUGE == store_type) {
2339 va_start (ap, store_type);
2345 name = va_arg (ap, char const *);
2349 value = va_arg (ap, gauge_t);
2357 vl = plugin_value_list_clone (template);
2358 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2359 if (store_percentage)
2360 sstrncpy (vl->type, "percent", sizeof (vl->type));
2362 va_start (ap, store_type);
2368 /* Set the type instance. */
2369 name = va_arg (ap, char const *);
2372 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2374 /* Set the value. */
2378 vl->values[0].gauge = va_arg (ap, gauge_t);
2379 if (store_percentage)
2380 vl->values[0].gauge *= sum ? (100.0 / sum) : NAN;
2382 case DS_TYPE_ABSOLUTE:
2383 vl->values[0].absolute = va_arg (ap, absolute_t);
2385 case DS_TYPE_COUNTER:
2386 vl->values[0].counter = va_arg (ap, counter_t);
2388 case DS_TYPE_DERIVE:
2389 vl->values[0].derive = va_arg (ap, derive_t);
2392 ERROR ("plugin_dispatch_multivalue: given store_type is incorrect.");
2397 status = plugin_write_enqueue (vl);
2403 plugin_value_list_free (vl);
2405 } /* }}} int plugin_dispatch_multivalue */
2407 int plugin_dispatch_notification (const notification_t *notif)
2410 /* Possible TODO: Add flap detection here */
2412 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2413 "time = %.3f; host = %s;",
2414 notif->severity, notif->message,
2415 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2417 /* Nobody cares for notifications */
2418 if (list_notification == NULL)
2421 le = llist_head (list_notification);
2424 callback_func_t *cf;
2425 plugin_notification_cb callback;
2428 /* do not switch plugin context; rather keep the context
2429 * (interval) information of the calling plugin */
2432 callback = cf->cf_callback;
2433 status = (*callback) (notif, &cf->cf_udata);
2436 WARNING ("plugin_dispatch_notification: Notification "
2437 "callback %s returned %i.",
2445 } /* int plugin_dispatch_notification */
2447 void plugin_log (int level, const char *format, ...)
2454 if (level >= LOG_DEBUG)
2458 va_start (ap, format);
2459 vsnprintf (msg, sizeof (msg), format, ap);
2460 msg[sizeof (msg) - 1] = '\0';
2463 if (list_log == NULL)
2465 fprintf (stderr, "%s\n", msg);
2469 le = llist_head (list_log);
2472 callback_func_t *cf;
2473 plugin_log_cb callback;
2476 callback = cf->cf_callback;
2478 /* do not switch plugin context; rather keep the context
2479 * (interval) information of the calling plugin */
2481 (*callback) (level, msg, &cf->cf_udata);
2485 } /* void plugin_log */
2487 int parse_log_severity (const char *severity)
2491 if ((0 == strcasecmp (severity, "emerg"))
2492 || (0 == strcasecmp (severity, "alert"))
2493 || (0 == strcasecmp (severity, "crit"))
2494 || (0 == strcasecmp (severity, "err")))
2495 log_level = LOG_ERR;
2496 else if (0 == strcasecmp (severity, "warning"))
2497 log_level = LOG_WARNING;
2498 else if (0 == strcasecmp (severity, "notice"))
2499 log_level = LOG_NOTICE;
2500 else if (0 == strcasecmp (severity, "info"))
2501 log_level = LOG_INFO;
2503 else if (0 == strcasecmp (severity, "debug"))
2504 log_level = LOG_DEBUG;
2505 #endif /* COLLECT_DEBUG */
2508 } /* int parse_log_severity */
2510 int parse_notif_severity (const char *severity)
2512 int notif_severity = -1;
2514 if (strcasecmp (severity, "FAILURE") == 0)
2515 notif_severity = NOTIF_FAILURE;
2516 else if (strcmp (severity, "OKAY") == 0)
2517 notif_severity = NOTIF_OKAY;
2518 else if ((strcmp (severity, "WARNING") == 0)
2519 || (strcmp (severity, "WARN") == 0))
2520 notif_severity = NOTIF_WARNING;
2522 return (notif_severity);
2523 } /* int parse_notif_severity */
2525 const data_set_t *plugin_get_ds (const char *name)
2529 if (data_sets == NULL)
2531 ERROR ("plugin_get_ds: No data sets are defined yet.");
2535 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2537 DEBUG ("No such dataset registered: %s", name);
2542 } /* data_set_t *plugin_get_ds */
2544 static int plugin_notification_meta_add (notification_t *n,
2546 enum notification_meta_type_e type,
2549 notification_meta_t *meta;
2550 notification_meta_t *tail;
2552 if ((n == NULL) || (name == NULL) || (value == NULL))
2554 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2558 meta = calloc (1, sizeof (*meta));
2561 ERROR ("plugin_notification_meta_add: calloc failed.");
2565 sstrncpy (meta->name, name, sizeof (meta->name));
2570 case NM_TYPE_STRING:
2572 meta->nm_value.nm_string = strdup ((const char *) value);
2573 if (meta->nm_value.nm_string == NULL)
2575 ERROR ("plugin_notification_meta_add: strdup failed.");
2581 case NM_TYPE_SIGNED_INT:
2583 meta->nm_value.nm_signed_int = *((int64_t *) value);
2586 case NM_TYPE_UNSIGNED_INT:
2588 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2591 case NM_TYPE_DOUBLE:
2593 meta->nm_value.nm_double = *((double *) value);
2596 case NM_TYPE_BOOLEAN:
2598 meta->nm_value.nm_boolean = *((_Bool *) value);
2603 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2607 } /* switch (type) */
2611 while ((tail != NULL) && (tail->next != NULL))
2620 } /* int plugin_notification_meta_add */
2622 int plugin_notification_meta_add_string (notification_t *n,
2626 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2629 int plugin_notification_meta_add_signed_int (notification_t *n,
2633 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2636 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2640 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2643 int plugin_notification_meta_add_double (notification_t *n,
2647 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2650 int plugin_notification_meta_add_boolean (notification_t *n,
2654 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2657 int plugin_notification_meta_copy (notification_t *dst,
2658 const notification_t *src)
2660 assert (dst != NULL);
2661 assert (src != NULL);
2662 assert (dst != src);
2663 assert ((src->meta == NULL) || (src->meta != dst->meta));
2665 for (notification_meta_t *meta = src->meta; meta != NULL; meta = meta->next)
2667 if (meta->type == NM_TYPE_STRING)
2668 plugin_notification_meta_add_string (dst, meta->name,
2669 meta->nm_value.nm_string);
2670 else if (meta->type == NM_TYPE_SIGNED_INT)
2671 plugin_notification_meta_add_signed_int (dst, meta->name,
2672 meta->nm_value.nm_signed_int);
2673 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2674 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2675 meta->nm_value.nm_unsigned_int);
2676 else if (meta->type == NM_TYPE_DOUBLE)
2677 plugin_notification_meta_add_double (dst, meta->name,
2678 meta->nm_value.nm_double);
2679 else if (meta->type == NM_TYPE_BOOLEAN)
2680 plugin_notification_meta_add_boolean (dst, meta->name,
2681 meta->nm_value.nm_boolean);
2685 } /* int plugin_notification_meta_copy */
2687 int plugin_notification_meta_free (notification_meta_t *n)
2689 notification_meta_t *this;
2690 notification_meta_t *next;
2694 ERROR ("plugin_notification_meta_free: n == NULL!");
2699 while (this != NULL)
2703 if (this->type == NM_TYPE_STRING)
2705 /* Assign to a temporary variable to work around nm_string's const
2707 void *tmp = (void *) this->nm_value.nm_string;
2710 this->nm_value.nm_string = NULL;
2718 } /* int plugin_notification_meta_free */
2720 static void plugin_ctx_destructor (void *ctx)
2723 } /* void plugin_ctx_destructor */
2725 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2727 static plugin_ctx_t *plugin_ctx_create (void)
2731 ctx = malloc (sizeof (*ctx));
2734 ERROR ("Failed to allocate plugin context: %s",
2735 sstrerror (errno, errbuf, sizeof (errbuf)));
2740 assert (plugin_ctx_key_initialized);
2741 pthread_setspecific (plugin_ctx_key, ctx);
2742 DEBUG("Created new plugin context.");
2744 } /* int plugin_ctx_create */
2746 void plugin_init_ctx (void)
2748 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2749 plugin_ctx_key_initialized = 1;
2750 } /* void plugin_init_ctx */
2752 plugin_ctx_t plugin_get_ctx (void)
2756 assert (plugin_ctx_key_initialized);
2757 ctx = pthread_getspecific (plugin_ctx_key);
2760 ctx = plugin_ctx_create ();
2761 /* this must no happen -- exit() instead? */
2767 } /* plugin_ctx_t plugin_get_ctx */
2769 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2774 assert (plugin_ctx_key_initialized);
2775 c = pthread_getspecific (plugin_ctx_key);
2778 c = plugin_ctx_create ();
2779 /* this must no happen -- exit() instead? */
2788 } /* void plugin_set_ctx */
2790 cdtime_t plugin_get_interval (void)
2794 interval = plugin_get_ctx().interval;
2798 return cf_get_default_interval ();
2799 } /* cdtime_t plugin_get_interval */
2803 void *(*start_routine) (void *);
2807 static void *plugin_thread_start (void *arg)
2809 plugin_thread_t *plugin_thread = arg;
2811 void *(*start_routine) (void *) = plugin_thread->start_routine;
2812 void *plugin_arg = plugin_thread->arg;
2814 plugin_set_ctx (plugin_thread->ctx);
2816 sfree (plugin_thread);
2818 return start_routine (plugin_arg);
2819 } /* void *plugin_thread_start */
2821 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2822 void *(*start_routine) (void *), void *arg, char *name)
2824 plugin_thread_t *plugin_thread;
2827 plugin_thread = malloc (sizeof (*plugin_thread));
2828 if (plugin_thread == NULL)
2831 plugin_thread->ctx = plugin_get_ctx ();
2832 plugin_thread->start_routine = start_routine;
2833 plugin_thread->arg = arg;
2835 ret = pthread_create (thread, attr,
2836 plugin_thread_start, plugin_thread);
2838 if (ret == 0 && name != NULL) {
2839 #if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP)
2840 char thread_name[16];
2841 sstrncpy (thread_name, name, sizeof(thread_name));
2842 # if defined(HAVE_PTHREAD_SETNAME_NP)
2843 pthread_setname_np (*thread, thread_name);
2844 # elif defined(HAVE_PTHREAD_SET_NAME_NP)
2845 pthread_set_name_np (*thread, thread_name);
2851 } /* int plugin_thread_create */
2853 /* vim: set sw=8 ts=8 noet fdm=marker : */