2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2014 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian Harl <sh at tokkee.org>
31 #include "configfile.h"
32 #include "filter_chain.h"
33 #include "utils_avltree.h"
34 #include "utils_cache.h"
35 #include "utils_complain.h"
36 #include "utils_llist.h"
37 #include "utils_heap.h"
38 #include "utils_time.h"
39 #include "utils_random.h"
46 struct callback_func_s
52 typedef struct callback_func_s callback_func_t;
56 #define RF_REMOVE 65535
59 /* `read_func_t' "inherits" from `callback_func_t'.
60 * The `rf_super' member MUST be the first one in this structure! */
61 #define rf_callback rf_super.cf_callback
62 #define rf_udata rf_super.cf_udata
63 #define rf_ctx rf_super.cf_ctx
64 callback_func_t rf_super;
65 char rf_group[DATA_MAX_NAME_LEN];
69 cdtime_t rf_effective_interval;
70 cdtime_t rf_next_read;
72 typedef struct read_func_s read_func_t;
75 typedef struct write_queue_s write_queue_t;
83 struct flush_callback_s {
87 typedef struct flush_callback_s flush_callback_t;
92 static c_avl_tree_t *plugins_loaded = NULL;
94 static llist_t *list_init;
95 static llist_t *list_write;
96 static llist_t *list_flush;
97 static llist_t *list_missing;
98 static llist_t *list_shutdown;
99 static llist_t *list_log;
100 static llist_t *list_notification;
102 static fc_chain_t *pre_cache_chain = NULL;
103 static fc_chain_t *post_cache_chain = NULL;
105 static c_avl_tree_t *data_sets;
107 static char *plugindir = NULL;
109 #ifndef DEFAULT_MAX_READ_INTERVAL
110 # define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T (86400)
112 static c_heap_t *read_heap = NULL;
113 static llist_t *read_list;
114 static int read_loop = 1;
115 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
116 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
117 static pthread_t *read_threads = NULL;
118 static int read_threads_num = 0;
119 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
121 static write_queue_t *write_queue_head;
122 static write_queue_t *write_queue_tail;
123 static long write_queue_length = 0;
124 static _Bool write_loop = 1;
125 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
126 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
127 static pthread_t *write_threads = NULL;
128 static size_t write_threads_num = 0;
130 static pthread_key_t plugin_ctx_key;
131 static _Bool plugin_ctx_key_initialized = 0;
133 static long write_limit_high = 0;
134 static long write_limit_low = 0;
136 static derive_t stats_values_dropped = 0;
137 static _Bool record_statistics = 0;
142 static int plugin_dispatch_values_internal (value_list_t *vl);
144 static const char *plugin_get_dir (void)
146 if (plugindir == NULL)
152 static void plugin_update_internal_statistics (void) { /* {{{ */
153 derive_t copy_write_queue_length;
154 value_list_t vl = VALUE_LIST_INIT;
157 copy_write_queue_length = write_queue_length;
159 /* Initialize `vl' */
163 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
164 sstrncpy (vl.plugin, "collectd", sizeof (vl.plugin));
166 vl.type_instance[0] = 0;
170 sstrncpy (vl.plugin_instance, "write_queue",
171 sizeof (vl.plugin_instance));
173 /* Write queue : queue length */
174 vl.values[0].gauge = (gauge_t) copy_write_queue_length;
175 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
176 vl.type_instance[0] = 0;
177 plugin_dispatch_values (&vl);
179 /* Write queue : Values dropped (queue length > low limit) */
180 vl.values[0].derive = (derive_t) stats_values_dropped;
181 sstrncpy (vl.type, "derive", sizeof (vl.type));
182 sstrncpy (vl.type_instance, "dropped", sizeof (vl.type_instance));
183 plugin_dispatch_values (&vl);
186 sstrncpy (vl.plugin_instance, "cache",
187 sizeof (vl.plugin_instance));
189 /* Cache : Nb entry in cache tree */
190 vl.values[0].gauge = (gauge_t) uc_get_size();
191 sstrncpy (vl.type, "cache_size", sizeof (vl.type));
192 vl.type_instance[0] = 0;
193 plugin_dispatch_values (&vl);
196 } /* }}} void plugin_update_internal_statistics */
198 static void destroy_callback (callback_func_t *cf) /* {{{ */
203 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
205 cf->cf_udata.free_func (cf->cf_udata.data);
206 cf->cf_udata.data = NULL;
207 cf->cf_udata.free_func = NULL;
210 } /* }}} void destroy_callback */
212 static void destroy_all_callbacks (llist_t **list) /* {{{ */
219 le = llist_head (*list);
227 destroy_callback (le->value);
233 llist_destroy (*list);
235 } /* }}} void destroy_all_callbacks */
237 static void destroy_read_heap (void) /* {{{ */
239 if (read_heap == NULL)
246 rf = c_heap_get_root (read_heap);
250 destroy_callback ((callback_func_t *) rf);
253 c_heap_destroy (read_heap);
255 } /* }}} void destroy_read_heap */
257 static int register_callback (llist_t **list, /* {{{ */
258 const char *name, callback_func_t *cf)
265 *list = llist_create ();
268 ERROR ("plugin: register_callback: "
269 "llist_create failed.");
270 destroy_callback (cf);
278 ERROR ("plugin: register_callback: strdup failed.");
279 destroy_callback (cf);
283 le = llist_search (*list, name);
286 le = llentry_create (key, cf);
289 ERROR ("plugin: register_callback: "
290 "llentry_create failed.");
292 destroy_callback (cf);
296 llist_append (*list, le);
300 callback_func_t *old_cf;
305 WARNING ("plugin: register_callback: "
306 "a callback named `%s' already exists - "
307 "overwriting the old entry!", name);
309 destroy_callback (old_cf);
314 } /* }}} int register_callback */
316 static void log_list_callbacks (llist_t **list, /* {{{ */
326 n = llist_size(*list);
329 INFO("%s [none]", comment);
333 keys = calloc(n, sizeof(char*));
337 ERROR("%s: failed to allocate memory for list of callbacks",
343 for (le = llist_head (*list), i = 0, len = 0;
348 len += strlen(le->key) + 6;
350 str = malloc(len + 10);
353 ERROR("%s: failed to allocate memory for list of callbacks",
359 strjoin(str, len, keys, n, "', '");
360 INFO("%s ['%s']", comment, str);
364 } /* }}} void log_list_callbacks */
366 static int create_register_callback (llist_t **list, /* {{{ */
367 const char *name, void *callback, user_data_t *ud)
371 cf = calloc (1, sizeof (*cf));
374 ERROR ("plugin: create_register_callback: calloc failed.");
378 cf->cf_callback = callback;
381 cf->cf_udata.data = NULL;
382 cf->cf_udata.free_func = NULL;
389 cf->cf_ctx = plugin_get_ctx ();
391 return (register_callback (list, name, cf));
392 } /* }}} int create_register_callback */
394 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
401 e = llist_search (list, name);
405 llist_remove (list, e);
408 destroy_callback (e->value);
413 } /* }}} int plugin_unregister */
416 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
417 * object, but it will bitch about a shared object not having a
418 * ``module_register'' symbol..
420 static int plugin_load_file (char *file, uint32_t flags)
423 void (*reg_handle) (void);
426 lt_dlerror (); /* clear errors */
428 #if LIBTOOL_VERSION == 2
429 if (flags & PLUGIN_FLAGS_GLOBAL) {
431 lt_dladvise_init(&advise);
432 lt_dladvise_global(&advise);
433 dlh = lt_dlopenadvise(file, advise);
434 lt_dladvise_destroy(&advise);
436 dlh = lt_dlopen (file);
438 #else /* if LIBTOOL_VERSION == 1 */
439 if (flags & PLUGIN_FLAGS_GLOBAL)
440 WARNING ("plugin_load_file: The global flag is not supported, "
441 "libtool 2 is required for this.");
442 dlh = lt_dlopen (file);
447 char errbuf[1024] = "";
449 ssnprintf (errbuf, sizeof (errbuf),
450 "lt_dlopen (\"%s\") failed: %s. "
451 "The most common cause for this problem is "
452 "missing dependencies. Use ldd(1) to check "
453 "the dependencies of the plugin "
455 file, lt_dlerror ());
457 ERROR ("%s", errbuf);
458 /* Make sure this is printed to STDERR in any case, but also
459 * make sure it's printed only once. */
460 if (list_log != NULL)
461 fprintf (stderr, "ERROR: %s\n", errbuf);
466 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
468 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
469 file, lt_dlerror ());
479 static void *plugin_read_thread (void __attribute__((unused)) *args)
481 while (read_loop != 0)
484 plugin_ctx_t old_ctx;
492 /* Get the read function that needs to be read next.
493 * We don't need to hold "read_lock" for the heap, but we need
494 * to call c_heap_get_root() and pthread_cond_wait() in the
495 * same protected block. */
496 pthread_mutex_lock (&read_lock);
497 rf = c_heap_get_root (read_heap);
500 pthread_cond_wait (&read_cond, &read_lock);
501 pthread_mutex_unlock (&read_lock);
504 pthread_mutex_unlock (&read_lock);
506 if (rf->rf_interval == 0)
508 /* this should not happen, because the interval is set
509 * for each plugin when loading it
510 * XXX: issue a warning? */
511 rf->rf_interval = plugin_get_interval ();
512 rf->rf_effective_interval = rf->rf_interval;
514 rf->rf_next_read = cdtime ();
517 /* sleep until this entry is due,
518 * using pthread_cond_timedwait */
519 pthread_mutex_lock (&read_lock);
520 /* In pthread_cond_timedwait, spurious wakeups are possible
521 * (and really happen, at least on NetBSD with > 1 CPU), thus
522 * we need to re-evaluate the condition every time
523 * pthread_cond_timedwait returns. */
525 while ((read_loop != 0)
526 && (cdtime () < rf->rf_next_read)
529 struct timespec ts = { 0 };
531 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
533 rc = pthread_cond_timedwait (&read_cond, &read_lock,
537 /* Must hold `read_lock' when accessing `rf->rf_type'. */
538 rf_type = rf->rf_type;
539 pthread_mutex_unlock (&read_lock);
541 /* Check if we're supposed to stop.. This may have interrupted
545 /* Insert `rf' again, so it can be free'd correctly */
546 c_heap_insert (read_heap, rf);
550 /* The entry has been marked for deletion. The linked list
551 * entry has already been removed by `plugin_unregister_read'.
552 * All we have to do here is free the `read_func_t' and
554 if (rf_type == RF_REMOVE)
556 DEBUG ("plugin_read_thread: Destroying the `%s' "
557 "callback.", rf->rf_name);
559 destroy_callback ((callback_func_t *) rf);
564 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
568 old_ctx = plugin_set_ctx (rf->rf_ctx);
570 if (rf_type == RF_SIMPLE)
572 int (*callback) (void);
574 callback = rf->rf_callback;
575 status = (*callback) ();
579 plugin_read_cb callback;
581 assert (rf_type == RF_COMPLEX);
583 callback = rf->rf_callback;
584 status = (*callback) (&rf->rf_udata);
587 plugin_set_ctx (old_ctx);
589 /* If the function signals failure, we will increase the
590 * intervals in which it will be called. */
593 rf->rf_effective_interval *= 2;
594 if (rf->rf_effective_interval > max_read_interval)
595 rf->rf_effective_interval = max_read_interval;
597 NOTICE ("read-function of plugin `%s' failed. "
598 "Will suspend it for %.3f seconds.",
600 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
604 /* Success: Restore the interval, if it was changed. */
605 rf->rf_effective_interval = rf->rf_interval;
608 /* update the ``next read due'' field */
611 /* calculate the time spent in the read function */
612 elapsed = (now - start);
614 if (elapsed > rf->rf_effective_interval)
615 WARNING ("plugin_read_thread: read-function of the `%s' plugin took %.3f "
616 "seconds, which is above its read interval (%.3f seconds). You might "
617 "want to adjust the `Interval' or `ReadThreads' settings.",
618 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed),
619 CDTIME_T_TO_DOUBLE(rf->rf_effective_interval));
621 DEBUG ("plugin_read_thread: read-function of the `%s' plugin took "
623 rf->rf_name, CDTIME_T_TO_DOUBLE(elapsed));
625 DEBUG ("plugin_read_thread: Effective interval of the "
626 "`%s' plugin is %.3f seconds.",
628 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
630 /* Calculate the next (absolute) time at which this function
631 * should be called. */
632 rf->rf_next_read += rf->rf_effective_interval;
634 /* Check, if `rf_next_read' is in the past. */
635 if (rf->rf_next_read < now)
637 /* `rf_next_read' is in the past. Insert `now'
638 * so this value doesn't trail off into the
640 rf->rf_next_read = now;
643 DEBUG ("plugin_read_thread: Next read of the `%s' plugin at %.3f.",
645 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
647 /* Re-insert this read function into the heap again. */
648 c_heap_insert (read_heap, rf);
649 } /* while (read_loop) */
653 } /* void *plugin_read_thread */
655 static void start_read_threads (int num)
659 if (read_threads != NULL)
662 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
663 if (read_threads == NULL)
665 ERROR ("plugin: start_read_threads: calloc failed.");
669 read_threads_num = 0;
670 for (i = 0; i < num; i++)
672 if (pthread_create (read_threads + read_threads_num, NULL,
673 plugin_read_thread, NULL) == 0)
679 ERROR ("plugin: start_read_threads: pthread_create failed.");
683 } /* void start_read_threads */
685 static void stop_read_threads (void)
689 if (read_threads == NULL)
692 INFO ("collectd: Stopping %i read threads.", read_threads_num);
694 pthread_mutex_lock (&read_lock);
696 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
697 pthread_cond_broadcast (&read_cond);
698 pthread_mutex_unlock (&read_lock);
700 for (i = 0; i < read_threads_num; i++)
702 if (pthread_join (read_threads[i], NULL) != 0)
704 ERROR ("plugin: stop_read_threads: pthread_join failed.");
706 read_threads[i] = (pthread_t) 0;
708 sfree (read_threads);
709 read_threads_num = 0;
710 } /* void stop_read_threads */
712 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
717 meta_data_destroy (vl->meta);
720 } /* }}} void plugin_value_list_free */
722 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
729 vl = malloc (sizeof (*vl));
732 memcpy (vl, vl_orig, sizeof (*vl));
734 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
735 if (vl->values == NULL)
737 plugin_value_list_free (vl);
740 memcpy (vl->values, vl_orig->values,
741 vl_orig->values_len * sizeof (*vl->values));
743 vl->meta = meta_data_clone (vl->meta);
744 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
746 plugin_value_list_free (vl);
751 vl->time = cdtime ();
753 /* Fill in the interval from the thread context, if it is zero. */
754 if (vl->interval == 0)
756 plugin_ctx_t ctx = plugin_get_ctx ();
758 if (ctx.interval != 0)
759 vl->interval = ctx.interval;
762 char name[6 * DATA_MAX_NAME_LEN];
763 FORMAT_VL (name, sizeof (name), vl);
764 ERROR ("plugin_value_list_clone: Unable to determine "
765 "interval from context for "
766 "value list \"%s\". "
767 "This indicates a broken plugin. "
768 "Please report this problem to the "
769 "collectd mailing list or at "
770 "<http://collectd.org/bugs/>.", name);
771 vl->interval = cf_get_default_interval ();
776 } /* }}} value_list_t *plugin_value_list_clone */
778 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
782 q = malloc (sizeof (*q));
787 q->vl = plugin_value_list_clone (vl);
794 /* Store context of caller (read plugin); otherwise, it would not be
795 * available to the write plugins when actually dispatching the
796 * value-list later on. */
797 q->ctx = plugin_get_ctx ();
799 pthread_mutex_lock (&write_lock);
801 if (write_queue_tail == NULL)
803 write_queue_head = q;
804 write_queue_tail = q;
805 write_queue_length = 1;
809 write_queue_tail->next = q;
810 write_queue_tail = q;
811 write_queue_length += 1;
814 pthread_cond_signal (&write_cond);
815 pthread_mutex_unlock (&write_lock);
818 } /* }}} int plugin_write_enqueue */
820 static value_list_t *plugin_write_dequeue (void) /* {{{ */
825 pthread_mutex_lock (&write_lock);
827 while (write_loop && (write_queue_head == NULL))
828 pthread_cond_wait (&write_cond, &write_lock);
830 if (write_queue_head == NULL)
832 pthread_mutex_unlock (&write_lock);
836 q = write_queue_head;
837 write_queue_head = q->next;
838 write_queue_length -= 1;
839 if (write_queue_head == NULL) {
840 write_queue_tail = NULL;
841 assert(0 == write_queue_length);
844 pthread_mutex_unlock (&write_lock);
846 (void) plugin_set_ctx (q->ctx);
851 } /* }}} value_list_t *plugin_write_dequeue */
853 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
857 value_list_t *vl = plugin_write_dequeue ();
861 plugin_dispatch_values_internal (vl);
863 plugin_value_list_free (vl);
868 } /* }}} void *plugin_write_thread */
870 static void start_write_threads (size_t num) /* {{{ */
874 if (write_threads != NULL)
877 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
878 if (write_threads == NULL)
880 ERROR ("plugin: start_write_threads: calloc failed.");
884 write_threads_num = 0;
885 for (i = 0; i < num; i++)
889 status = pthread_create (write_threads + write_threads_num,
896 ERROR ("plugin: start_write_threads: pthread_create failed "
897 "with status %i (%s).", status,
898 sstrerror (status, errbuf, sizeof (errbuf)));
904 } /* }}} void start_write_threads */
906 static void stop_write_threads (void) /* {{{ */
911 if (write_threads == NULL)
914 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
916 pthread_mutex_lock (&write_lock);
918 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
919 pthread_cond_broadcast (&write_cond);
920 pthread_mutex_unlock (&write_lock);
922 for (i = 0; i < write_threads_num; i++)
924 if (pthread_join (write_threads[i], NULL) != 0)
926 ERROR ("plugin: stop_write_threads: pthread_join failed.");
928 write_threads[i] = (pthread_t) 0;
930 sfree (write_threads);
931 write_threads_num = 0;
933 pthread_mutex_lock (&write_lock);
935 for (q = write_queue_head; q != NULL; )
937 write_queue_t *q1 = q;
938 plugin_value_list_free (q->vl);
943 write_queue_head = NULL;
944 write_queue_tail = NULL;
945 write_queue_length = 0;
946 pthread_mutex_unlock (&write_lock);
950 WARNING ("plugin: %zu value list%s left after shutting down "
951 "the write threads.",
952 i, (i == 1) ? " was" : "s were");
954 } /* }}} void stop_write_threads */
959 void plugin_set_dir (const char *dir)
969 plugindir = strdup (dir);
970 if (plugindir == NULL)
971 ERROR ("plugin_set_dir: strdup(\"%s\") failed", dir);
974 static _Bool plugin_is_loaded (char const *name)
978 if (plugins_loaded == NULL)
979 plugins_loaded = c_avl_create ((void *) strcasecmp);
980 assert (plugins_loaded != NULL);
982 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
983 return (status == 0);
986 static int plugin_mark_loaded (char const *name)
991 name_copy = strdup (name);
992 if (name_copy == NULL)
995 status = c_avl_insert (plugins_loaded,
996 /* key = */ name_copy, /* value = */ NULL);
1000 static void plugin_free_loaded (void)
1005 if (plugins_loaded == NULL)
1008 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
1011 assert (value == NULL);
1014 c_avl_destroy (plugins_loaded);
1015 plugins_loaded = NULL;
1019 int plugin_load (char const *plugin_name, uint32_t flags)
1023 char filename[BUFSIZE] = "";
1024 char typename[BUFSIZE];
1026 struct stat statbuf;
1030 if (plugin_name == NULL)
1033 /* Check if plugin is already loaded and don't do anything in this
1035 if (plugin_is_loaded (plugin_name))
1038 dir = plugin_get_dir ();
1042 * XXX: Magic at work:
1044 * Some of the language bindings, for example the Python and Perl
1045 * plugins, need to be able to export symbols to the scripts they run.
1046 * For this to happen, the "Globals" flag needs to be set.
1047 * Unfortunately, this technical detail is hard to explain to the
1048 * average user and she shouldn't have to worry about this, ideally.
1049 * So in order to save everyone's sanity use a different default for a
1050 * handful of special plugins. --octo
1052 if ((strcasecmp ("perl", plugin_name) == 0)
1053 || (strcasecmp ("python", plugin_name) == 0))
1054 flags |= PLUGIN_FLAGS_GLOBAL;
1056 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
1057 * type when matching the filename */
1058 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
1059 if ((status < 0) || ((size_t) status >= sizeof (typename)))
1061 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
1065 if ((dh = opendir (dir)) == NULL)
1068 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
1069 sstrerror (errno, errbuf, sizeof (errbuf)));
1073 while ((de = readdir (dh)) != NULL)
1075 if (strcasecmp (de->d_name, typename))
1078 status = ssnprintf (filename, sizeof (filename),
1079 "%s/%s", dir, de->d_name);
1080 if ((status < 0) || ((size_t) status >= sizeof (filename)))
1082 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
1087 if (lstat (filename, &statbuf) == -1)
1090 WARNING ("plugin_load: stat (\"%s\") failed: %s",
1092 sstrerror (errno, errbuf, sizeof (errbuf)));
1095 else if (!S_ISREG (statbuf.st_mode))
1097 /* don't follow symlinks */
1098 WARNING ("plugin_load: %s is not a regular file.",
1103 status = plugin_load_file (filename, flags);
1107 plugin_mark_loaded (plugin_name);
1109 INFO ("plugin_load: plugin \"%s\" successfully loaded.", plugin_name);
1114 ERROR ("plugin_load: Load plugin \"%s\" failed with "
1115 "status %i.", plugin_name, status);
1121 if (filename[0] == 0)
1122 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1129 * The `register_*' functions follow
1131 int plugin_register_config (const char *name,
1132 int (*callback) (const char *key, const char *val),
1133 const char **keys, int keys_num)
1135 cf_register (name, callback, keys, keys_num);
1137 } /* int plugin_register_config */
1139 int plugin_register_complex_config (const char *type,
1140 int (*callback) (oconfig_item_t *))
1142 return (cf_register_complex (type, callback));
1143 } /* int plugin_register_complex_config */
1145 int plugin_register_init (const char *name,
1146 int (*callback) (void))
1148 return (create_register_callback (&list_init, name, (void *) callback,
1149 /* user_data = */ NULL));
1150 } /* plugin_register_init */
1152 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1154 const read_func_t *rf0;
1155 const read_func_t *rf1;
1160 if (rf0->rf_next_read < rf1->rf_next_read)
1162 else if (rf0->rf_next_read > rf1->rf_next_read)
1166 } /* int plugin_compare_read_func */
1168 /* Add a read function to both, the heap and a linked list. The linked list if
1169 * used to look-up read functions, especially for the remove function. The heap
1170 * is used to determine which plugin to read next. */
1171 static int plugin_insert_read (read_func_t *rf)
1176 rf->rf_next_read = cdtime ();
1177 rf->rf_effective_interval = rf->rf_interval;
1179 pthread_mutex_lock (&read_lock);
1181 if (read_list == NULL)
1183 read_list = llist_create ();
1184 if (read_list == NULL)
1186 pthread_mutex_unlock (&read_lock);
1187 ERROR ("plugin_insert_read: read_list failed.");
1192 if (read_heap == NULL)
1194 read_heap = c_heap_create (plugin_compare_read_func);
1195 if (read_heap == NULL)
1197 pthread_mutex_unlock (&read_lock);
1198 ERROR ("plugin_insert_read: c_heap_create failed.");
1203 le = llist_search (read_list, rf->rf_name);
1206 pthread_mutex_unlock (&read_lock);
1207 WARNING ("The read function \"%s\" is already registered. "
1208 "Check for duplicate \"LoadPlugin\" lines "
1209 "in your configuration!",
1214 le = llentry_create (rf->rf_name, rf);
1217 pthread_mutex_unlock (&read_lock);
1218 ERROR ("plugin_insert_read: llentry_create failed.");
1222 status = c_heap_insert (read_heap, rf);
1225 pthread_mutex_unlock (&read_lock);
1226 ERROR ("plugin_insert_read: c_heap_insert failed.");
1227 llentry_destroy (le);
1231 /* This does not fail. */
1232 llist_append (read_list, le);
1234 /* Wake up all the read threads. */
1235 pthread_cond_broadcast (&read_cond);
1236 pthread_mutex_unlock (&read_lock);
1238 } /* int plugin_insert_read */
1240 int plugin_register_read (const char *name,
1241 int (*callback) (void))
1246 rf = calloc (1, sizeof (*rf));
1249 ERROR ("plugin_register_read: calloc failed.");
1253 rf->rf_callback = (void *) callback;
1254 rf->rf_udata.data = NULL;
1255 rf->rf_udata.free_func = NULL;
1256 rf->rf_ctx = plugin_get_ctx ();
1257 rf->rf_group[0] = '\0';
1258 rf->rf_name = strdup (name);
1259 rf->rf_type = RF_SIMPLE;
1260 rf->rf_interval = plugin_get_interval ();
1262 status = plugin_insert_read (rf);
1264 sfree (rf->rf_name);
1269 } /* int plugin_register_read */
1271 int plugin_register_complex_read (const char *group, const char *name,
1272 plugin_read_cb callback,
1274 user_data_t *user_data)
1279 rf = calloc (1,sizeof (*rf));
1282 ERROR ("plugin_register_complex_read: calloc failed.");
1286 rf->rf_callback = (void *) callback;
1288 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1290 rf->rf_group[0] = '\0';
1291 rf->rf_name = strdup (name);
1292 rf->rf_type = RF_COMPLEX;
1293 rf->rf_interval = (interval != 0) ? interval : plugin_get_interval ();
1296 if (user_data == NULL)
1298 rf->rf_udata.data = NULL;
1299 rf->rf_udata.free_func = NULL;
1303 rf->rf_udata = *user_data;
1306 rf->rf_ctx = plugin_get_ctx ();
1308 status = plugin_insert_read (rf);
1310 sfree (rf->rf_name);
1315 } /* int plugin_register_complex_read */
1317 int plugin_register_write (const char *name,
1318 plugin_write_cb callback, user_data_t *ud)
1320 return (create_register_callback (&list_write, name,
1321 (void *) callback, ud));
1322 } /* int plugin_register_write */
1324 static int plugin_flush_timeout_callback (user_data_t *ud)
1326 flush_callback_t *cb = ud->data;
1328 return plugin_flush (cb->name, cb->timeout, /* identifier = */ NULL);
1329 } /* static int plugin_flush_callback */
1331 static void plugin_flush_timeout_callback_free (void *data)
1333 flush_callback_t *cb = data;
1335 if (cb == NULL) return;
1339 } /* static void plugin_flush_callback_free */
1341 static char *plugin_flush_callback_name (const char *name)
1343 const char *flush_prefix = "flush/";
1348 prefix_size = strlen(flush_prefix);
1349 name_size = strlen(name);
1351 flush_name = malloc (name_size + prefix_size + 1);
1352 if (flush_name == NULL)
1354 ERROR ("plugin_flush_callback_name: malloc failed.");
1358 sstrncpy (flush_name, flush_prefix, prefix_size + 1);
1359 sstrncpy (flush_name + prefix_size, name, name_size + 1);
1362 } /* static char *plugin_flush_callback_name */
1364 int plugin_register_flush (const char *name,
1365 plugin_flush_cb callback, user_data_t *ud)
1368 plugin_ctx_t ctx = plugin_get_ctx ();
1370 status = create_register_callback (&list_flush, name,
1371 (void *) callback, ud);
1375 if (ctx.flush_interval != 0)
1378 flush_callback_t *cb;
1380 flush_name = plugin_flush_callback_name (name);
1381 if (flush_name == NULL)
1384 cb = malloc(sizeof (*cb));
1387 ERROR ("plugin_register_flush: malloc failed.");
1392 cb->name = strdup (name);
1393 if (cb->name == NULL)
1395 ERROR ("plugin_register_flush: strdup failed.");
1400 cb->timeout = ctx.flush_timeout;
1403 ud->free_func = plugin_flush_timeout_callback_free;
1405 status = plugin_register_complex_read (
1406 /* group = */ "flush",
1407 /* name = */ flush_name,
1408 /* callback = */ plugin_flush_timeout_callback,
1409 /* interval = */ ctx.flush_interval,
1410 /* user data = */ ud);
1422 } /* int plugin_register_flush */
1424 int plugin_register_missing (const char *name,
1425 plugin_missing_cb callback, user_data_t *ud)
1427 return (create_register_callback (&list_missing, name,
1428 (void *) callback, ud));
1429 } /* int plugin_register_missing */
1431 int plugin_register_shutdown (const char *name,
1432 int (*callback) (void))
1434 return (create_register_callback (&list_shutdown, name,
1435 (void *) callback, /* user_data = */ NULL));
1436 } /* int plugin_register_shutdown */
1438 static void plugin_free_data_sets (void)
1443 if (data_sets == NULL)
1446 while (c_avl_pick (data_sets, &key, &value) == 0)
1448 data_set_t *ds = value;
1449 /* key is a pointer to ds->type */
1455 c_avl_destroy (data_sets);
1457 } /* void plugin_free_data_sets */
1459 int plugin_register_data_set (const data_set_t *ds)
1461 data_set_t *ds_copy;
1464 if ((data_sets != NULL)
1465 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1467 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1468 plugin_unregister_data_set (ds->type);
1470 else if (data_sets == NULL)
1472 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1473 if (data_sets == NULL)
1477 ds_copy = malloc (sizeof (*ds_copy));
1478 if (ds_copy == NULL)
1480 memcpy(ds_copy, ds, sizeof (data_set_t));
1482 ds_copy->ds = malloc (sizeof (*ds_copy->ds)
1484 if (ds_copy->ds == NULL)
1490 for (i = 0; i < ds->ds_num; i++)
1491 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1493 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1494 } /* int plugin_register_data_set */
1496 int plugin_register_log (const char *name,
1497 plugin_log_cb callback, user_data_t *ud)
1499 return (create_register_callback (&list_log, name,
1500 (void *) callback, ud));
1501 } /* int plugin_register_log */
1503 int plugin_register_notification (const char *name,
1504 plugin_notification_cb callback, user_data_t *ud)
1506 return (create_register_callback (&list_notification, name,
1507 (void *) callback, ud));
1508 } /* int plugin_register_log */
1510 int plugin_unregister_config (const char *name)
1512 cf_unregister (name);
1514 } /* int plugin_unregister_config */
1516 int plugin_unregister_complex_config (const char *name)
1518 cf_unregister_complex (name);
1520 } /* int plugin_unregister_complex_config */
1522 int plugin_unregister_init (const char *name)
1524 return (plugin_unregister (list_init, name));
1527 int plugin_unregister_read (const char *name) /* {{{ */
1535 pthread_mutex_lock (&read_lock);
1537 if (read_list == NULL)
1539 pthread_mutex_unlock (&read_lock);
1543 le = llist_search (read_list, name);
1546 pthread_mutex_unlock (&read_lock);
1547 WARNING ("plugin_unregister_read: No such read function: %s",
1552 llist_remove (read_list, le);
1555 assert (rf != NULL);
1556 rf->rf_type = RF_REMOVE;
1558 pthread_mutex_unlock (&read_lock);
1560 llentry_destroy (le);
1562 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1565 } /* }}} int plugin_unregister_read */
1567 void plugin_log_available_writers (void)
1569 log_list_callbacks (&list_write, "Available write targets:");
1572 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1574 read_func_t *rf = e->value;
1577 return strcmp (rf->rf_group, (const char *)group);
1578 } /* }}} int compare_read_func_group */
1580 int plugin_unregister_read_group (const char *group) /* {{{ */
1590 pthread_mutex_lock (&read_lock);
1592 if (read_list == NULL)
1594 pthread_mutex_unlock (&read_lock);
1600 le = llist_search_custom (read_list,
1601 compare_read_func_group, (void *)group);
1608 llist_remove (read_list, le);
1611 assert (rf != NULL);
1612 rf->rf_type = RF_REMOVE;
1614 llentry_destroy (le);
1616 DEBUG ("plugin_unregister_read_group: "
1617 "Marked `%s' (group `%s') for removal.",
1618 rf->rf_name, group);
1621 pthread_mutex_unlock (&read_lock);
1625 WARNING ("plugin_unregister_read_group: No such "
1626 "group of read function: %s", group);
1631 } /* }}} int plugin_unregister_read_group */
1633 int plugin_unregister_write (const char *name)
1635 return (plugin_unregister (list_write, name));
1638 int plugin_unregister_flush (const char *name)
1640 plugin_ctx_t ctx = plugin_get_ctx ();
1642 if (ctx.flush_interval != 0)
1646 flush_name = plugin_flush_callback_name (name);
1647 if (flush_name != NULL)
1649 plugin_unregister_read(flush_name);
1654 return plugin_unregister (list_flush, name);
1657 int plugin_unregister_missing (const char *name)
1659 return (plugin_unregister (list_missing, name));
1662 int plugin_unregister_shutdown (const char *name)
1664 return (plugin_unregister (list_shutdown, name));
1667 int plugin_unregister_data_set (const char *name)
1671 if (data_sets == NULL)
1674 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1681 } /* int plugin_unregister_data_set */
1683 int plugin_unregister_log (const char *name)
1685 return (plugin_unregister (list_log, name));
1688 int plugin_unregister_notification (const char *name)
1690 return (plugin_unregister (list_notification, name));
1693 void plugin_init_all (void)
1695 char const *chain_name;
1699 /* Init the value cache */
1702 if (IS_TRUE (global_option_get ("CollectInternalStats")))
1703 record_statistics = 1;
1705 chain_name = global_option_get ("PreCacheChain");
1706 pre_cache_chain = fc_chain_get_by_name (chain_name);
1708 chain_name = global_option_get ("PostCacheChain");
1709 post_cache_chain = fc_chain_get_by_name (chain_name);
1711 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1713 if (write_limit_high < 0)
1715 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1716 write_limit_high = 0;
1719 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1720 /* default = */ write_limit_high / 2);
1721 if (write_limit_low < 0)
1723 ERROR ("WriteQueueLimitLow must be positive or zero.");
1724 write_limit_low = write_limit_high / 2;
1726 else if (write_limit_low > write_limit_high)
1728 ERROR ("WriteQueueLimitLow must not be larger than "
1729 "WriteQueueLimitHigh.");
1730 write_limit_low = write_limit_high;
1733 write_threads_num = global_option_get_long ("WriteThreads",
1735 if (write_threads_num < 1)
1737 ERROR ("WriteThreads must be positive.");
1738 write_threads_num = 5;
1741 if ((list_init == NULL) && (read_heap == NULL))
1744 /* Calling all init callbacks before checking if read callbacks
1745 * are available allows the init callbacks to register the read
1747 le = llist_head (list_init);
1750 callback_func_t *cf;
1751 plugin_init_cb callback;
1752 plugin_ctx_t old_ctx;
1755 old_ctx = plugin_set_ctx (cf->cf_ctx);
1756 callback = cf->cf_callback;
1757 status = (*callback) ();
1758 plugin_set_ctx (old_ctx);
1762 ERROR ("Initialization of plugin `%s' "
1763 "failed with status %i. "
1764 "Plugin will be unloaded.",
1766 /* Plugins that register read callbacks from the init
1767 * callback should take care of appropriate error
1768 * handling themselves. */
1769 /* FIXME: Unload _all_ functions */
1770 plugin_unregister_read (le->key);
1776 start_write_threads ((size_t) write_threads_num);
1778 max_read_interval = global_option_get_time ("MaxReadInterval",
1779 DEFAULT_MAX_READ_INTERVAL);
1781 /* Start read-threads */
1782 if (read_heap != NULL)
1787 rt = global_option_get ("ReadThreads");
1790 start_read_threads ((num > 0) ? num : 5);
1792 } /* void plugin_init_all */
1794 /* TODO: Rename this function. */
1795 void plugin_read_all (void)
1797 if(record_statistics) {
1798 plugin_update_internal_statistics ();
1800 uc_check_timeout ();
1803 } /* void plugin_read_all */
1805 /* Read function called when the `-T' command line argument is given. */
1806 int plugin_read_all_once (void)
1809 int return_status = 0;
1811 if (read_heap == NULL)
1813 NOTICE ("No read-functions are registered.");
1820 plugin_ctx_t old_ctx;
1822 rf = c_heap_get_root (read_heap);
1826 old_ctx = plugin_set_ctx (rf->rf_ctx);
1828 if (rf->rf_type == RF_SIMPLE)
1830 int (*callback) (void);
1832 callback = rf->rf_callback;
1833 status = (*callback) ();
1837 plugin_read_cb callback;
1839 callback = rf->rf_callback;
1840 status = (*callback) (&rf->rf_udata);
1843 plugin_set_ctx (old_ctx);
1847 NOTICE ("read-function of plugin `%s' failed.",
1852 sfree (rf->rf_name);
1853 destroy_callback ((void *) rf);
1856 return (return_status);
1857 } /* int plugin_read_all_once */
1859 int plugin_write (const char *plugin, /* {{{ */
1860 const data_set_t *ds, const value_list_t *vl)
1868 if (list_write == NULL)
1873 ds = plugin_get_ds (vl->type);
1876 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1886 le = llist_head (list_write);
1889 callback_func_t *cf = le->value;
1890 plugin_write_cb callback;
1892 /* do not switch plugin context; rather keep the context (interval)
1893 * information of the calling read plugin */
1895 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1896 callback = cf->cf_callback;
1897 status = (*callback) (ds, vl, &cf->cf_udata);
1906 if ((success == 0) && (failure != 0))
1911 else /* plugin != NULL */
1913 callback_func_t *cf;
1914 plugin_write_cb callback;
1916 le = llist_head (list_write);
1919 if (strcasecmp (plugin, le->key) == 0)
1930 /* do not switch plugin context; rather keep the context (interval)
1931 * information of the calling read plugin */
1933 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1934 callback = cf->cf_callback;
1935 status = (*callback) (ds, vl, &cf->cf_udata);
1939 } /* }}} int plugin_write */
1941 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1945 if (list_flush == NULL)
1948 le = llist_head (list_flush);
1951 callback_func_t *cf;
1952 plugin_flush_cb callback;
1953 plugin_ctx_t old_ctx;
1955 if ((plugin != NULL)
1956 && (strcmp (plugin, le->key) != 0))
1963 old_ctx = plugin_set_ctx (cf->cf_ctx);
1964 callback = cf->cf_callback;
1966 (*callback) (timeout, identifier, &cf->cf_udata);
1968 plugin_set_ctx (old_ctx);
1973 } /* int plugin_flush */
1975 void plugin_shutdown_all (void)
1979 stop_read_threads ();
1981 destroy_all_callbacks (&list_init);
1983 pthread_mutex_lock (&read_lock);
1984 llist_destroy (read_list);
1986 pthread_mutex_unlock (&read_lock);
1988 destroy_read_heap ();
1990 plugin_flush (/* plugin = */ NULL,
1992 /* identifier = */ NULL);
1995 if (list_shutdown != NULL)
1996 le = llist_head (list_shutdown);
2000 callback_func_t *cf;
2001 plugin_shutdown_cb callback;
2002 plugin_ctx_t old_ctx;
2005 old_ctx = plugin_set_ctx (cf->cf_ctx);
2006 callback = cf->cf_callback;
2008 /* Advance the pointer before calling the callback allows
2009 * shutdown functions to unregister themselves. If done the
2010 * other way around the memory `le' points to will be freed
2011 * after callback returns. */
2016 plugin_set_ctx (old_ctx);
2019 stop_write_threads ();
2021 /* Write plugins which use the `user_data' pointer usually need the
2022 * same data available to the flush callback. If this is the case, set
2023 * the free_function to NULL when registering the flush callback and to
2024 * the real free function when registering the write callback. This way
2025 * the data isn't freed twice. */
2026 destroy_all_callbacks (&list_flush);
2027 destroy_all_callbacks (&list_missing);
2028 destroy_all_callbacks (&list_write);
2030 destroy_all_callbacks (&list_notification);
2031 destroy_all_callbacks (&list_shutdown);
2032 destroy_all_callbacks (&list_log);
2034 plugin_free_loaded ();
2035 plugin_free_data_sets ();
2036 } /* void plugin_shutdown_all */
2038 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
2042 if (list_missing == NULL)
2045 le = llist_head (list_missing);
2048 callback_func_t *cf;
2049 plugin_missing_cb callback;
2050 plugin_ctx_t old_ctx;
2054 old_ctx = plugin_set_ctx (cf->cf_ctx);
2055 callback = cf->cf_callback;
2057 status = (*callback) (vl, &cf->cf_udata);
2058 plugin_set_ctx (old_ctx);
2063 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
2064 "failed with status %i.",
2077 } /* int }}} plugin_dispatch_missing */
2079 static int plugin_dispatch_values_internal (value_list_t *vl)
2082 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
2084 value_t *saved_values;
2085 int saved_values_len;
2089 int free_meta_data = 0;
2091 if ((vl == NULL) || (vl->type[0] == 0)
2092 || (vl->values == NULL) || (vl->values_len < 1))
2094 ERROR ("plugin_dispatch_values: Invalid value list "
2095 "from plugin %s.", vl->plugin);
2099 /* Free meta data only if the calling function didn't specify any. In
2100 * this case matches and targets may add some and the calling function
2101 * may not expect (and therefore free) that data. */
2102 if (vl->meta == NULL)
2105 if (list_write == NULL)
2106 c_complain_once (LOG_WARNING, &no_write_complaint,
2107 "plugin_dispatch_values: No write callback has been "
2108 "registered. Please load at least one output plugin, "
2109 "if you want the collected data to be stored.");
2111 if (data_sets == NULL)
2113 ERROR ("plugin_dispatch_values: No data sets registered. "
2114 "Could the types database be read? Check "
2115 "your `TypesDB' setting!");
2119 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
2121 char ident[6 * DATA_MAX_NAME_LEN];
2123 FORMAT_VL (ident, sizeof (ident), vl);
2124 INFO ("plugin_dispatch_values: Dataset not found: %s "
2125 "(from \"%s\"), check your types.db!",
2130 /* Assured by plugin_value_list_clone(). The time is determined at
2131 * _enqueue_ time. */
2132 assert (vl->time != 0);
2133 assert (vl->interval != 0);
2135 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
2137 "plugin = %s; plugin_instance = %s; "
2138 "type = %s; type_instance = %s;",
2139 CDTIME_T_TO_DOUBLE (vl->time),
2140 CDTIME_T_TO_DOUBLE (vl->interval),
2142 vl->plugin, vl->plugin_instance,
2143 vl->type, vl->type_instance);
2146 assert (0 == strcmp (ds->type, vl->type));
2148 if (0 != strcmp (ds->type, vl->type))
2149 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
2150 ds->type, vl->type);
2154 assert (ds->ds_num == vl->values_len);
2156 if (ds->ds_num != vl->values_len)
2158 ERROR ("plugin_dispatch_values: ds->type = %s: "
2159 "(ds->ds_num = %zu) != "
2160 "(vl->values_len = %zu)",
2161 ds->type, ds->ds_num, vl->values_len);
2166 escape_slashes (vl->host, sizeof (vl->host));
2167 escape_slashes (vl->plugin, sizeof (vl->plugin));
2168 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
2169 escape_slashes (vl->type, sizeof (vl->type));
2170 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
2172 /* Copy the values. This way, we can assure `targets' that they get
2173 * dynamically allocated values, which they can free and replace if
2175 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
2177 saved_values = vl->values;
2178 saved_values_len = vl->values_len;
2180 vl->values = (value_t *) calloc (vl->values_len,
2181 sizeof (*vl->values));
2182 if (vl->values == NULL)
2184 ERROR ("plugin_dispatch_values: calloc failed.");
2185 vl->values = saved_values;
2188 memcpy (vl->values, saved_values,
2189 vl->values_len * sizeof (*vl->values));
2191 else /* if ((pre == NULL) && (post == NULL)) */
2193 saved_values = NULL;
2194 saved_values_len = 0;
2197 if (pre_cache_chain != NULL)
2199 status = fc_process_chain (ds, vl, pre_cache_chain);
2202 WARNING ("plugin_dispatch_values: Running the "
2203 "pre-cache chain failed with "
2207 else if (status == FC_TARGET_STOP)
2209 /* Restore the state of the value_list so that plugins
2210 * don't get confused.. */
2211 if (saved_values != NULL)
2214 vl->values = saved_values;
2215 vl->values_len = saved_values_len;
2221 /* Update the value cache */
2224 if (post_cache_chain != NULL)
2226 status = fc_process_chain (ds, vl, post_cache_chain);
2229 WARNING ("plugin_dispatch_values: Running the "
2230 "post-cache chain failed with "
2236 fc_default_action (ds, vl);
2238 /* Restore the state of the value_list so that plugins don't get
2240 if (saved_values != NULL)
2243 vl->values = saved_values;
2244 vl->values_len = saved_values_len;
2247 if ((free_meta_data != 0) && (vl->meta != NULL))
2249 meta_data_destroy (vl->meta);
2254 } /* int plugin_dispatch_values_internal */
2256 static double get_drop_probability (void) /* {{{ */
2262 pthread_mutex_lock (&write_lock);
2263 wql = write_queue_length;
2264 pthread_mutex_unlock (&write_lock);
2266 if (wql < write_limit_low)
2268 if (wql >= write_limit_high)
2271 pos = 1 + wql - write_limit_low;
2272 size = 1 + write_limit_high - write_limit_low;
2274 return (((double) pos) / ((double) size));
2275 } /* }}} double get_drop_probability */
2277 static _Bool check_drop_value (void) /* {{{ */
2279 static cdtime_t last_message_time = 0;
2280 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2286 if (write_limit_high == 0)
2289 p = get_drop_probability ();
2293 status = pthread_mutex_trylock (&last_message_lock);
2299 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2301 last_message_time = now;
2302 ERROR ("plugin_dispatch_values: Low water mark "
2303 "reached. Dropping %.0f%% of metrics.",
2306 pthread_mutex_unlock (&last_message_lock);
2317 } /* }}} _Bool check_drop_value */
2319 int plugin_dispatch_values (value_list_t const *vl)
2322 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2324 if (check_drop_value ()) {
2325 if(record_statistics) {
2326 pthread_mutex_lock(&statistics_lock);
2327 stats_values_dropped++;
2328 pthread_mutex_unlock(&statistics_lock);
2333 status = plugin_write_enqueue (vl);
2337 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2338 "with status %i (%s).", status,
2339 sstrerror (status, errbuf, sizeof (errbuf)));
2346 __attribute__((sentinel))
2347 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2348 _Bool store_percentage, int store_type, ...)
2355 assert (template->values_len == 1);
2357 /* Calculate sum for Gauge to calculate percent if needed */
2358 if (DS_TYPE_GAUGE == store_type) {
2359 va_start (ap, store_type);
2365 name = va_arg (ap, char const *);
2369 value = va_arg (ap, gauge_t);
2377 vl = plugin_value_list_clone (template);
2378 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2379 if (store_percentage)
2380 sstrncpy (vl->type, "percent", sizeof (vl->type));
2382 va_start (ap, store_type);
2388 /* Set the type instance. */
2389 name = va_arg (ap, char const *);
2392 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2394 /* Set the value. */
2398 vl->values[0].gauge = va_arg (ap, gauge_t);
2399 if (store_percentage)
2400 vl->values[0].gauge *= sum ? (100.0 / sum) : 0;
2402 case DS_TYPE_ABSOLUTE:
2403 vl->values[0].absolute = va_arg (ap, absolute_t);
2405 case DS_TYPE_COUNTER:
2406 vl->values[0].counter = va_arg (ap, counter_t);
2408 case DS_TYPE_DERIVE:
2409 vl->values[0].derive = va_arg (ap, derive_t);
2412 ERROR ("plugin_dispatch_multivalue: given store_type is incorrect.");
2417 status = plugin_write_enqueue (vl);
2423 plugin_value_list_free (vl);
2425 } /* }}} int plugin_dispatch_multivalue */
2427 int plugin_dispatch_notification (const notification_t *notif)
2430 /* Possible TODO: Add flap detection here */
2432 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2433 "time = %.3f; host = %s;",
2434 notif->severity, notif->message,
2435 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2437 /* Nobody cares for notifications */
2438 if (list_notification == NULL)
2441 le = llist_head (list_notification);
2444 callback_func_t *cf;
2445 plugin_notification_cb callback;
2448 /* do not switch plugin context; rather keep the context
2449 * (interval) information of the calling plugin */
2452 callback = cf->cf_callback;
2453 status = (*callback) (notif, &cf->cf_udata);
2456 WARNING ("plugin_dispatch_notification: Notification "
2457 "callback %s returned %i.",
2465 } /* int plugin_dispatch_notification */
2467 void plugin_log (int level, const char *format, ...)
2474 if (level >= LOG_DEBUG)
2478 va_start (ap, format);
2479 vsnprintf (msg, sizeof (msg), format, ap);
2480 msg[sizeof (msg) - 1] = '\0';
2483 if (list_log == NULL)
2485 fprintf (stderr, "%s\n", msg);
2489 le = llist_head (list_log);
2492 callback_func_t *cf;
2493 plugin_log_cb callback;
2496 callback = cf->cf_callback;
2498 /* do not switch plugin context; rather keep the context
2499 * (interval) information of the calling plugin */
2501 (*callback) (level, msg, &cf->cf_udata);
2505 } /* void plugin_log */
2507 int parse_log_severity (const char *severity)
2511 if ((0 == strcasecmp (severity, "emerg"))
2512 || (0 == strcasecmp (severity, "alert"))
2513 || (0 == strcasecmp (severity, "crit"))
2514 || (0 == strcasecmp (severity, "err")))
2515 log_level = LOG_ERR;
2516 else if (0 == strcasecmp (severity, "warning"))
2517 log_level = LOG_WARNING;
2518 else if (0 == strcasecmp (severity, "notice"))
2519 log_level = LOG_NOTICE;
2520 else if (0 == strcasecmp (severity, "info"))
2521 log_level = LOG_INFO;
2523 else if (0 == strcasecmp (severity, "debug"))
2524 log_level = LOG_DEBUG;
2525 #endif /* COLLECT_DEBUG */
2528 } /* int parse_log_severity */
2530 int parse_notif_severity (const char *severity)
2532 int notif_severity = -1;
2534 if (strcasecmp (severity, "FAILURE") == 0)
2535 notif_severity = NOTIF_FAILURE;
2536 else if (strcmp (severity, "OKAY") == 0)
2537 notif_severity = NOTIF_OKAY;
2538 else if ((strcmp (severity, "WARNING") == 0)
2539 || (strcmp (severity, "WARN") == 0))
2540 notif_severity = NOTIF_WARNING;
2542 return (notif_severity);
2543 } /* int parse_notif_severity */
2545 const data_set_t *plugin_get_ds (const char *name)
2549 if (data_sets == NULL)
2551 ERROR ("plugin_get_ds: No data sets are defined yet.");
2555 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2557 DEBUG ("No such dataset registered: %s", name);
2562 } /* data_set_t *plugin_get_ds */
2564 static int plugin_notification_meta_add (notification_t *n,
2566 enum notification_meta_type_e type,
2569 notification_meta_t *meta;
2570 notification_meta_t *tail;
2572 if ((n == NULL) || (name == NULL) || (value == NULL))
2574 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2578 meta = calloc (1, sizeof (*meta));
2581 ERROR ("plugin_notification_meta_add: calloc failed.");
2585 sstrncpy (meta->name, name, sizeof (meta->name));
2590 case NM_TYPE_STRING:
2592 meta->nm_value.nm_string = strdup ((const char *) value);
2593 if (meta->nm_value.nm_string == NULL)
2595 ERROR ("plugin_notification_meta_add: strdup failed.");
2601 case NM_TYPE_SIGNED_INT:
2603 meta->nm_value.nm_signed_int = *((int64_t *) value);
2606 case NM_TYPE_UNSIGNED_INT:
2608 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2611 case NM_TYPE_DOUBLE:
2613 meta->nm_value.nm_double = *((double *) value);
2616 case NM_TYPE_BOOLEAN:
2618 meta->nm_value.nm_boolean = *((_Bool *) value);
2623 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2627 } /* switch (type) */
2631 while ((tail != NULL) && (tail->next != NULL))
2640 } /* int plugin_notification_meta_add */
2642 int plugin_notification_meta_add_string (notification_t *n,
2646 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2649 int plugin_notification_meta_add_signed_int (notification_t *n,
2653 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2656 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2660 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2663 int plugin_notification_meta_add_double (notification_t *n,
2667 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2670 int plugin_notification_meta_add_boolean (notification_t *n,
2674 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2677 int plugin_notification_meta_copy (notification_t *dst,
2678 const notification_t *src)
2680 notification_meta_t *meta;
2682 assert (dst != NULL);
2683 assert (src != NULL);
2684 assert (dst != src);
2685 assert ((src->meta == NULL) || (src->meta != dst->meta));
2687 for (meta = src->meta; meta != NULL; meta = meta->next)
2689 if (meta->type == NM_TYPE_STRING)
2690 plugin_notification_meta_add_string (dst, meta->name,
2691 meta->nm_value.nm_string);
2692 else if (meta->type == NM_TYPE_SIGNED_INT)
2693 plugin_notification_meta_add_signed_int (dst, meta->name,
2694 meta->nm_value.nm_signed_int);
2695 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2696 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2697 meta->nm_value.nm_unsigned_int);
2698 else if (meta->type == NM_TYPE_DOUBLE)
2699 plugin_notification_meta_add_double (dst, meta->name,
2700 meta->nm_value.nm_double);
2701 else if (meta->type == NM_TYPE_BOOLEAN)
2702 plugin_notification_meta_add_boolean (dst, meta->name,
2703 meta->nm_value.nm_boolean);
2707 } /* int plugin_notification_meta_copy */
2709 int plugin_notification_meta_free (notification_meta_t *n)
2711 notification_meta_t *this;
2712 notification_meta_t *next;
2716 ERROR ("plugin_notification_meta_free: n == NULL!");
2721 while (this != NULL)
2725 if (this->type == NM_TYPE_STRING)
2727 /* Assign to a temporary variable to work around nm_string's const
2729 void *tmp = (void *) this->nm_value.nm_string;
2732 this->nm_value.nm_string = NULL;
2740 } /* int plugin_notification_meta_free */
2742 static void plugin_ctx_destructor (void *ctx)
2745 } /* void plugin_ctx_destructor */
2747 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2749 static plugin_ctx_t *plugin_ctx_create (void)
2753 ctx = malloc (sizeof (*ctx));
2756 ERROR ("Failed to allocate plugin context: %s",
2757 sstrerror (errno, errbuf, sizeof (errbuf)));
2762 assert (plugin_ctx_key_initialized);
2763 pthread_setspecific (plugin_ctx_key, ctx);
2764 DEBUG("Created new plugin context.");
2766 } /* int plugin_ctx_create */
2768 void plugin_init_ctx (void)
2770 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2771 plugin_ctx_key_initialized = 1;
2772 } /* void plugin_init_ctx */
2774 plugin_ctx_t plugin_get_ctx (void)
2778 assert (plugin_ctx_key_initialized);
2779 ctx = pthread_getspecific (plugin_ctx_key);
2782 ctx = plugin_ctx_create ();
2783 /* this must no happen -- exit() instead? */
2789 } /* plugin_ctx_t plugin_get_ctx */
2791 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2796 assert (plugin_ctx_key_initialized);
2797 c = pthread_getspecific (plugin_ctx_key);
2800 c = plugin_ctx_create ();
2801 /* this must no happen -- exit() instead? */
2810 } /* void plugin_set_ctx */
2812 cdtime_t plugin_get_interval (void)
2816 interval = plugin_get_ctx().interval;
2820 return cf_get_default_interval ();
2821 } /* cdtime_t plugin_get_interval */
2825 void *(*start_routine) (void *);
2829 static void *plugin_thread_start (void *arg)
2831 plugin_thread_t *plugin_thread = arg;
2833 void *(*start_routine) (void *) = plugin_thread->start_routine;
2834 void *plugin_arg = plugin_thread->arg;
2836 plugin_set_ctx (plugin_thread->ctx);
2838 sfree (plugin_thread);
2840 return start_routine (plugin_arg);
2841 } /* void *plugin_thread_start */
2843 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2844 void *(*start_routine) (void *), void *arg)
2846 plugin_thread_t *plugin_thread;
2848 plugin_thread = malloc (sizeof (*plugin_thread));
2849 if (plugin_thread == NULL)
2852 plugin_thread->ctx = plugin_get_ctx ();
2853 plugin_thread->start_routine = start_routine;
2854 plugin_thread->arg = arg;
2856 return pthread_create (thread, attr,
2857 plugin_thread_start, plugin_thread);
2858 } /* int plugin_thread_create */
2860 /* vim: set sw=8 ts=8 noet fdm=marker : */