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;
86 static c_avl_tree_t *plugins_loaded = NULL;
88 static llist_t *list_init;
89 static llist_t *list_write;
90 static llist_t *list_flush;
91 static llist_t *list_missing;
92 static llist_t *list_shutdown;
93 static llist_t *list_log;
94 static llist_t *list_notification;
96 static fc_chain_t *pre_cache_chain = NULL;
97 static fc_chain_t *post_cache_chain = NULL;
99 static c_avl_tree_t *data_sets;
101 static char *plugindir = NULL;
103 #ifndef DEFAULT_MAX_READ_INTERVAL
104 # define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T (86400)
106 static c_heap_t *read_heap = NULL;
107 static llist_t *read_list;
108 static int read_loop = 1;
109 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
110 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
111 static pthread_t *read_threads = NULL;
112 static int read_threads_num = 0;
113 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
115 static write_queue_t *write_queue_head;
116 static write_queue_t *write_queue_tail;
117 static long write_queue_length = 0;
118 static _Bool write_loop = 1;
119 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
120 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
121 static pthread_t *write_threads = NULL;
122 static size_t write_threads_num = 0;
124 static pthread_key_t plugin_ctx_key;
125 static _Bool plugin_ctx_key_initialized = 0;
127 static long write_limit_high = 0;
128 static long write_limit_low = 0;
130 static derive_t stats_values_dropped = 0;
131 static _Bool record_statistics = 0;
136 static int plugin_dispatch_values_internal (value_list_t *vl);
138 static const char *plugin_get_dir (void)
140 if (plugindir == NULL)
146 static void plugin_update_internal_statistics (void) { /* {{{ */
147 derive_t copy_write_queue_length;
148 value_list_t vl = VALUE_LIST_INIT;
151 copy_write_queue_length = write_queue_length;
153 /* Initialize `vl' */
157 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
158 sstrncpy (vl.plugin, "collectd", sizeof (vl.plugin));
160 vl.type_instance[0] = 0;
164 sstrncpy (vl.plugin_instance, "write_queue",
165 sizeof (vl.plugin_instance));
167 /* Write queue : queue length */
168 vl.values[0].gauge = (gauge_t) copy_write_queue_length;
169 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
170 vl.type_instance[0] = 0;
171 plugin_dispatch_values (&vl);
173 /* Write queue : Values dropped (queue length > low limit) */
174 vl.values[0].derive = (derive_t) stats_values_dropped;
175 sstrncpy (vl.type, "derive", sizeof (vl.type));
176 sstrncpy (vl.type_instance, "dropped", sizeof (vl.type_instance));
177 plugin_dispatch_values (&vl);
180 sstrncpy (vl.plugin_instance, "cache",
181 sizeof (vl.plugin_instance));
183 /* Cache : Nb entry in cache tree */
184 vl.values[0].gauge = (gauge_t) uc_get_size();
185 sstrncpy (vl.type, "cache_size", sizeof (vl.type));
186 vl.type_instance[0] = 0;
187 plugin_dispatch_values (&vl);
190 } /* }}} void plugin_update_internal_statistics */
192 static void destroy_callback (callback_func_t *cf) /* {{{ */
197 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
199 cf->cf_udata.free_func (cf->cf_udata.data);
200 cf->cf_udata.data = NULL;
201 cf->cf_udata.free_func = NULL;
204 } /* }}} void destroy_callback */
206 static void destroy_all_callbacks (llist_t **list) /* {{{ */
213 le = llist_head (*list);
221 destroy_callback (le->value);
227 llist_destroy (*list);
229 } /* }}} void destroy_all_callbacks */
231 static void destroy_read_heap (void) /* {{{ */
233 if (read_heap == NULL)
240 cf = c_heap_get_root (read_heap);
244 destroy_callback (cf);
247 c_heap_destroy (read_heap);
249 } /* }}} void destroy_read_heap */
251 static int register_callback (llist_t **list, /* {{{ */
252 const char *name, callback_func_t *cf)
259 *list = llist_create ();
262 ERROR ("plugin: register_callback: "
263 "llist_create failed.");
264 destroy_callback (cf);
272 ERROR ("plugin: register_callback: strdup failed.");
273 destroy_callback (cf);
277 le = llist_search (*list, name);
280 le = llentry_create (key, cf);
283 ERROR ("plugin: register_callback: "
284 "llentry_create failed.");
286 destroy_callback (cf);
290 llist_append (*list, le);
294 callback_func_t *old_cf;
299 WARNING ("plugin: register_callback: "
300 "a callback named `%s' already exists - "
301 "overwriting the old entry!", name);
303 destroy_callback (old_cf);
308 } /* }}} int register_callback */
310 static void log_list_callbacks (llist_t **list, /* {{{ */
320 n = llist_size(*list);
323 INFO("%s [none]", comment);
327 keys = calloc(n, sizeof(char*));
331 ERROR("%s: failed to allocate memory for list of callbacks",
337 for (le = llist_head (*list), i = 0, len = 0;
342 len += strlen(le->key) + 6;
344 str = malloc(len + 10);
347 ERROR("%s: failed to allocate memory for list of callbacks",
353 strjoin(str, len, keys, n, "', '");
354 INFO("%s ['%s']", comment, str);
358 } /* }}} void log_list_callbacks */
360 static int create_register_callback (llist_t **list, /* {{{ */
361 const char *name, void *callback, user_data_t *ud)
365 cf = (callback_func_t *) malloc (sizeof (*cf));
368 ERROR ("plugin: create_register_callback: malloc failed.");
371 memset (cf, 0, sizeof (*cf));
373 cf->cf_callback = callback;
376 cf->cf_udata.data = NULL;
377 cf->cf_udata.free_func = NULL;
384 cf->cf_ctx = plugin_get_ctx ();
386 return (register_callback (list, name, cf));
387 } /* }}} int create_register_callback */
389 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
396 e = llist_search (list, name);
400 llist_remove (list, e);
403 destroy_callback (e->value);
408 } /* }}} int plugin_unregister */
411 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
412 * object, but it will bitch about a shared object not having a
413 * ``module_register'' symbol..
415 static int plugin_load_file (char *file, uint32_t flags)
418 void (*reg_handle) (void);
421 lt_dlerror (); /* clear errors */
423 #if LIBTOOL_VERSION == 2
424 if (flags & PLUGIN_FLAGS_GLOBAL) {
426 lt_dladvise_init(&advise);
427 lt_dladvise_global(&advise);
428 dlh = lt_dlopenadvise(file, advise);
429 lt_dladvise_destroy(&advise);
431 dlh = lt_dlopen (file);
433 #else /* if LIBTOOL_VERSION == 1 */
434 if (flags & PLUGIN_FLAGS_GLOBAL)
435 WARNING ("plugin_load_file: The global flag is not supported, "
436 "libtool 2 is required for this.");
437 dlh = lt_dlopen (file);
442 char errbuf[1024] = "";
444 ssnprintf (errbuf, sizeof (errbuf),
445 "lt_dlopen (\"%s\") failed: %s. "
446 "The most common cause for this problem is "
447 "missing dependencies. Use ldd(1) to check "
448 "the dependencies of the plugin "
450 file, lt_dlerror ());
452 ERROR ("%s", errbuf);
453 /* Make sure this is printed to STDERR in any case, but also
454 * make sure it's printed only once. */
455 if (list_log != NULL)
456 fprintf (stderr, "ERROR: %s\n", errbuf);
461 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
463 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
464 file, lt_dlerror ());
474 static void *plugin_read_thread (void __attribute__((unused)) *args)
476 while (read_loop != 0)
479 plugin_ctx_t old_ctx;
485 /* Get the read function that needs to be read next.
486 * We don't need to hold "read_lock" for the heap, but we need
487 * to call c_heap_get_root() and pthread_cond_wait() in the
488 * same protected block. */
489 pthread_mutex_lock (&read_lock);
490 rf = c_heap_get_root (read_heap);
493 pthread_cond_wait (&read_cond, &read_lock);
494 pthread_mutex_unlock (&read_lock);
497 pthread_mutex_unlock (&read_lock);
499 if (rf->rf_interval == 0)
501 /* this should not happen, because the interval is set
502 * for each plugin when loading it
503 * XXX: issue a warning? */
504 rf->rf_interval = plugin_get_interval ();
505 rf->rf_effective_interval = rf->rf_interval;
507 rf->rf_next_read = cdtime ();
510 /* sleep until this entry is due,
511 * using pthread_cond_timedwait */
512 pthread_mutex_lock (&read_lock);
513 /* In pthread_cond_timedwait, spurious wakeups are possible
514 * (and really happen, at least on NetBSD with > 1 CPU), thus
515 * we need to re-evaluate the condition every time
516 * pthread_cond_timedwait returns. */
518 while ((read_loop != 0)
519 && (cdtime () < rf->rf_next_read)
522 struct timespec ts = { 0 };
524 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
526 rc = pthread_cond_timedwait (&read_cond, &read_lock,
530 /* Must hold `read_lock' when accessing `rf->rf_type'. */
531 rf_type = rf->rf_type;
532 pthread_mutex_unlock (&read_lock);
534 /* Check if we're supposed to stop.. This may have interrupted
538 /* Insert `rf' again, so it can be free'd correctly */
539 c_heap_insert (read_heap, rf);
543 /* The entry has been marked for deletion. The linked list
544 * entry has already been removed by `plugin_unregister_read'.
545 * All we have to do here is free the `read_func_t' and
547 if (rf_type == RF_REMOVE)
549 DEBUG ("plugin_read_thread: Destroying the `%s' "
550 "callback.", rf->rf_name);
552 destroy_callback ((callback_func_t *) rf);
557 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
559 old_ctx = plugin_set_ctx (rf->rf_ctx);
561 if (rf_type == RF_SIMPLE)
563 int (*callback) (void);
565 callback = rf->rf_callback;
566 status = (*callback) ();
570 plugin_read_cb callback;
572 assert (rf_type == RF_COMPLEX);
574 callback = rf->rf_callback;
575 status = (*callback) (&rf->rf_udata);
578 plugin_set_ctx (old_ctx);
580 /* If the function signals failure, we will increase the
581 * intervals in which it will be called. */
584 rf->rf_effective_interval *= 2;
585 if (rf->rf_effective_interval > max_read_interval)
586 rf->rf_effective_interval = max_read_interval;
588 NOTICE ("read-function of plugin `%s' failed. "
589 "Will suspend it for %.3f seconds.",
591 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
595 /* Success: Restore the interval, if it was changed. */
596 rf->rf_effective_interval = rf->rf_interval;
599 /* update the ``next read due'' field */
602 DEBUG ("plugin_read_thread: Effective interval of the "
603 "%s plugin is %.3f seconds.",
605 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
607 /* Calculate the next (absolute) time at which this function
608 * should be called. */
609 rf->rf_next_read += rf->rf_effective_interval;
611 /* Check, if `rf_next_read' is in the past. */
612 if (rf->rf_next_read < now)
614 /* `rf_next_read' is in the past. Insert `now'
615 * so this value doesn't trail off into the
617 rf->rf_next_read = now;
620 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
622 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
624 /* Re-insert this read function into the heap again. */
625 c_heap_insert (read_heap, rf);
626 } /* while (read_loop) */
630 } /* void *plugin_read_thread */
632 static void start_read_threads (int num)
636 if (read_threads != NULL)
639 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
640 if (read_threads == NULL)
642 ERROR ("plugin: start_read_threads: calloc failed.");
646 read_threads_num = 0;
647 for (i = 0; i < num; i++)
649 if (pthread_create (read_threads + read_threads_num, NULL,
650 plugin_read_thread, NULL) == 0)
656 ERROR ("plugin: start_read_threads: pthread_create failed.");
660 } /* void start_read_threads */
662 static void stop_read_threads (void)
666 if (read_threads == NULL)
669 INFO ("collectd: Stopping %i read threads.", read_threads_num);
671 pthread_mutex_lock (&read_lock);
673 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
674 pthread_cond_broadcast (&read_cond);
675 pthread_mutex_unlock (&read_lock);
677 for (i = 0; i < read_threads_num; i++)
679 if (pthread_join (read_threads[i], NULL) != 0)
681 ERROR ("plugin: stop_read_threads: pthread_join failed.");
683 read_threads[i] = (pthread_t) 0;
685 sfree (read_threads);
686 read_threads_num = 0;
687 } /* void stop_read_threads */
689 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
694 meta_data_destroy (vl->meta);
697 } /* }}} void plugin_value_list_free */
699 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
706 vl = malloc (sizeof (*vl));
709 memcpy (vl, vl_orig, sizeof (*vl));
711 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
712 if (vl->values == NULL)
714 plugin_value_list_free (vl);
717 memcpy (vl->values, vl_orig->values,
718 vl_orig->values_len * sizeof (*vl->values));
720 vl->meta = meta_data_clone (vl->meta);
721 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
723 plugin_value_list_free (vl);
728 vl->time = cdtime ();
730 /* Fill in the interval from the thread context, if it is zero. */
731 if (vl->interval == 0)
733 plugin_ctx_t ctx = plugin_get_ctx ();
735 if (ctx.interval != 0)
736 vl->interval = ctx.interval;
739 char name[6 * DATA_MAX_NAME_LEN];
740 FORMAT_VL (name, sizeof (name), vl);
741 ERROR ("plugin_value_list_clone: Unable to determine "
742 "interval from context for "
743 "value list \"%s\". "
744 "This indicates a broken plugin. "
745 "Please report this problem to the "
746 "collectd mailing list or at "
747 "<http://collectd.org/bugs/>.", name);
748 vl->interval = cf_get_default_interval ();
753 } /* }}} value_list_t *plugin_value_list_clone */
755 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
759 q = malloc (sizeof (*q));
764 q->vl = plugin_value_list_clone (vl);
771 /* Store context of caller (read plugin); otherwise, it would not be
772 * available to the write plugins when actually dispatching the
773 * value-list later on. */
774 q->ctx = plugin_get_ctx ();
776 pthread_mutex_lock (&write_lock);
778 if (write_queue_tail == NULL)
780 write_queue_head = q;
781 write_queue_tail = q;
782 write_queue_length = 1;
786 write_queue_tail->next = q;
787 write_queue_tail = q;
788 write_queue_length += 1;
791 pthread_cond_signal (&write_cond);
792 pthread_mutex_unlock (&write_lock);
795 } /* }}} int plugin_write_enqueue */
797 static value_list_t *plugin_write_dequeue (void) /* {{{ */
802 pthread_mutex_lock (&write_lock);
804 while (write_loop && (write_queue_head == NULL))
805 pthread_cond_wait (&write_cond, &write_lock);
807 if (write_queue_head == NULL)
809 pthread_mutex_unlock (&write_lock);
813 q = write_queue_head;
814 write_queue_head = q->next;
815 write_queue_length -= 1;
816 if (write_queue_head == NULL) {
817 write_queue_tail = NULL;
818 assert(0 == write_queue_length);
821 pthread_mutex_unlock (&write_lock);
823 (void) plugin_set_ctx (q->ctx);
828 } /* }}} value_list_t *plugin_write_dequeue */
830 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
834 value_list_t *vl = plugin_write_dequeue ();
838 plugin_dispatch_values_internal (vl);
840 plugin_value_list_free (vl);
845 } /* }}} void *plugin_write_thread */
847 static void start_write_threads (size_t num) /* {{{ */
851 if (write_threads != NULL)
854 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
855 if (write_threads == NULL)
857 ERROR ("plugin: start_write_threads: calloc failed.");
861 write_threads_num = 0;
862 for (i = 0; i < num; i++)
866 status = pthread_create (write_threads + write_threads_num,
873 ERROR ("plugin: start_write_threads: pthread_create failed "
874 "with status %i (%s).", status,
875 sstrerror (status, errbuf, sizeof (errbuf)));
881 } /* }}} void start_write_threads */
883 static void stop_write_threads (void) /* {{{ */
888 if (write_threads == NULL)
891 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
893 pthread_mutex_lock (&write_lock);
895 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
896 pthread_cond_broadcast (&write_cond);
897 pthread_mutex_unlock (&write_lock);
899 for (i = 0; i < write_threads_num; i++)
901 if (pthread_join (write_threads[i], NULL) != 0)
903 ERROR ("plugin: stop_write_threads: pthread_join failed.");
905 write_threads[i] = (pthread_t) 0;
907 sfree (write_threads);
908 write_threads_num = 0;
910 pthread_mutex_lock (&write_lock);
912 for (q = write_queue_head; q != NULL; )
914 write_queue_t *q1 = q;
915 plugin_value_list_free (q->vl);
920 write_queue_head = NULL;
921 write_queue_tail = NULL;
922 write_queue_length = 0;
923 pthread_mutex_unlock (&write_lock);
927 WARNING ("plugin: %i value list%s left after shutting down "
928 "the write threads.",
929 i, (i == 1) ? " was" : "s were");
931 } /* }}} void stop_write_threads */
936 void plugin_set_dir (const char *dir)
938 if (plugindir != NULL)
943 else if ((plugindir = strdup (dir)) == NULL)
946 ERROR ("strdup failed: %s",
947 sstrerror (errno, errbuf, sizeof (errbuf)));
951 static _Bool plugin_is_loaded (char const *name)
955 if (plugins_loaded == NULL)
956 plugins_loaded = c_avl_create ((void *) strcasecmp);
957 assert (plugins_loaded != NULL);
959 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
960 return (status == 0);
963 static int plugin_mark_loaded (char const *name)
968 name_copy = strdup (name);
969 if (name_copy == NULL)
972 status = c_avl_insert (plugins_loaded,
973 /* key = */ name_copy, /* value = */ NULL);
977 static void plugin_free_loaded ()
982 if (plugins_loaded == NULL)
985 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
988 assert (value == NULL);
991 c_avl_destroy (plugins_loaded);
992 plugins_loaded = NULL;
996 int plugin_load (char const *plugin_name, uint32_t flags)
1000 char filename[BUFSIZE] = "";
1001 char typename[BUFSIZE];
1003 struct stat statbuf;
1007 if (plugin_name == NULL)
1010 /* Check if plugin is already loaded and don't do anything in this
1012 if (plugin_is_loaded (plugin_name))
1015 dir = plugin_get_dir ();
1019 * XXX: Magic at work:
1021 * Some of the language bindings, for example the Python and Perl
1022 * plugins, need to be able to export symbols to the scripts they run.
1023 * For this to happen, the "Globals" flag needs to be set.
1024 * Unfortunately, this technical detail is hard to explain to the
1025 * average user and she shouldn't have to worry about this, ideally.
1026 * So in order to save everyone's sanity use a different default for a
1027 * handful of special plugins. --octo
1029 if ((strcasecmp ("perl", plugin_name) == 0)
1030 || (strcasecmp ("python", plugin_name) == 0))
1031 flags |= PLUGIN_FLAGS_GLOBAL;
1033 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
1034 * type when matching the filename */
1035 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
1036 if ((status < 0) || ((size_t) status >= sizeof (typename)))
1038 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
1042 if ((dh = opendir (dir)) == NULL)
1045 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
1046 sstrerror (errno, errbuf, sizeof (errbuf)));
1050 while ((de = readdir (dh)) != NULL)
1052 if (strcasecmp (de->d_name, typename))
1055 status = ssnprintf (filename, sizeof (filename),
1056 "%s/%s", dir, de->d_name);
1057 if ((status < 0) || ((size_t) status >= sizeof (filename)))
1059 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
1064 if (lstat (filename, &statbuf) == -1)
1067 WARNING ("plugin_load: stat (\"%s\") failed: %s",
1069 sstrerror (errno, errbuf, sizeof (errbuf)));
1072 else if (!S_ISREG (statbuf.st_mode))
1074 /* don't follow symlinks */
1075 WARNING ("plugin_load: %s is not a regular file.",
1080 status = plugin_load_file (filename, flags);
1084 plugin_mark_loaded (plugin_name);
1090 ERROR ("plugin_load: Load plugin \"%s\" failed with "
1091 "status %i.", plugin_name, status);
1097 if (filename[0] == 0)
1098 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1105 * The `register_*' functions follow
1107 int plugin_register_config (const char *name,
1108 int (*callback) (const char *key, const char *val),
1109 const char **keys, int keys_num)
1111 cf_register (name, callback, keys, keys_num);
1113 } /* int plugin_register_config */
1115 int plugin_register_complex_config (const char *type,
1116 int (*callback) (oconfig_item_t *))
1118 return (cf_register_complex (type, callback));
1119 } /* int plugin_register_complex_config */
1121 int plugin_register_init (const char *name,
1122 int (*callback) (void))
1124 return (create_register_callback (&list_init, name, (void *) callback,
1125 /* user_data = */ NULL));
1126 } /* plugin_register_init */
1128 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1130 const read_func_t *rf0;
1131 const read_func_t *rf1;
1136 if (rf0->rf_next_read < rf1->rf_next_read)
1138 else if (rf0->rf_next_read > rf1->rf_next_read)
1142 } /* int plugin_compare_read_func */
1144 /* Add a read function to both, the heap and a linked list. The linked list if
1145 * used to look-up read functions, especially for the remove function. The heap
1146 * is used to determine which plugin to read next. */
1147 static int plugin_insert_read (read_func_t *rf)
1152 rf->rf_next_read = cdtime ();
1153 rf->rf_effective_interval = rf->rf_interval;
1155 pthread_mutex_lock (&read_lock);
1157 if (read_list == NULL)
1159 read_list = llist_create ();
1160 if (read_list == NULL)
1162 pthread_mutex_unlock (&read_lock);
1163 ERROR ("plugin_insert_read: read_list failed.");
1168 if (read_heap == NULL)
1170 read_heap = c_heap_create (plugin_compare_read_func);
1171 if (read_heap == NULL)
1173 pthread_mutex_unlock (&read_lock);
1174 ERROR ("plugin_insert_read: c_heap_create failed.");
1179 le = llist_search (read_list, rf->rf_name);
1182 pthread_mutex_unlock (&read_lock);
1183 WARNING ("The read function \"%s\" is already registered. "
1184 "Check for duplicate \"LoadPlugin\" lines "
1185 "in your configuration!",
1190 le = llentry_create (rf->rf_name, rf);
1193 pthread_mutex_unlock (&read_lock);
1194 ERROR ("plugin_insert_read: llentry_create failed.");
1198 status = c_heap_insert (read_heap, rf);
1201 pthread_mutex_unlock (&read_lock);
1202 ERROR ("plugin_insert_read: c_heap_insert failed.");
1203 llentry_destroy (le);
1207 /* This does not fail. */
1208 llist_append (read_list, le);
1210 /* Wake up all the read threads. */
1211 pthread_cond_broadcast (&read_cond);
1212 pthread_mutex_unlock (&read_lock);
1214 } /* int plugin_insert_read */
1216 int plugin_register_read (const char *name,
1217 int (*callback) (void))
1222 rf = malloc (sizeof (*rf));
1225 ERROR ("plugin_register_read: malloc failed.");
1229 memset (rf, 0, sizeof (read_func_t));
1230 rf->rf_callback = (void *) callback;
1231 rf->rf_udata.data = NULL;
1232 rf->rf_udata.free_func = NULL;
1233 rf->rf_ctx = plugin_get_ctx ();
1234 rf->rf_group[0] = '\0';
1235 rf->rf_name = strdup (name);
1236 rf->rf_type = RF_SIMPLE;
1237 rf->rf_interval = plugin_get_interval ();
1239 status = plugin_insert_read (rf);
1244 } /* int plugin_register_read */
1246 int plugin_register_complex_read (const char *group, const char *name,
1247 plugin_read_cb callback,
1249 user_data_t *user_data)
1254 rf = malloc (sizeof (*rf));
1257 ERROR ("plugin_register_complex_read: malloc failed.");
1261 memset (rf, 0, sizeof (read_func_t));
1262 rf->rf_callback = (void *) callback;
1264 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1266 rf->rf_group[0] = '\0';
1267 rf->rf_name = strdup (name);
1268 rf->rf_type = RF_COMPLEX;
1269 rf->rf_interval = (interval != 0) ? interval : plugin_get_interval ();
1272 if (user_data == NULL)
1274 rf->rf_udata.data = NULL;
1275 rf->rf_udata.free_func = NULL;
1279 rf->rf_udata = *user_data;
1282 rf->rf_ctx = plugin_get_ctx ();
1284 status = plugin_insert_read (rf);
1289 } /* int plugin_register_complex_read */
1291 int plugin_register_write (const char *name,
1292 plugin_write_cb callback, user_data_t *ud)
1294 return (create_register_callback (&list_write, name,
1295 (void *) callback, ud));
1296 } /* int plugin_register_write */
1298 int plugin_register_flush (const char *name,
1299 plugin_flush_cb callback, user_data_t *ud)
1301 return (create_register_callback (&list_flush, name,
1302 (void *) callback, ud));
1303 } /* int plugin_register_flush */
1305 int plugin_register_missing (const char *name,
1306 plugin_missing_cb callback, user_data_t *ud)
1308 return (create_register_callback (&list_missing, name,
1309 (void *) callback, ud));
1310 } /* int plugin_register_missing */
1312 int plugin_register_shutdown (const char *name,
1313 int (*callback) (void))
1315 return (create_register_callback (&list_shutdown, name,
1316 (void *) callback, /* user_data = */ NULL));
1317 } /* int plugin_register_shutdown */
1319 static void plugin_free_data_sets (void)
1324 if (data_sets == NULL)
1327 while (c_avl_pick (data_sets, &key, &value) == 0)
1329 data_set_t *ds = value;
1330 /* key is a pointer to ds->type */
1336 c_avl_destroy (data_sets);
1338 } /* void plugin_free_data_sets */
1340 int plugin_register_data_set (const data_set_t *ds)
1342 data_set_t *ds_copy;
1345 if ((data_sets != NULL)
1346 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1348 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1349 plugin_unregister_data_set (ds->type);
1351 else if (data_sets == NULL)
1353 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1354 if (data_sets == NULL)
1358 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1359 if (ds_copy == NULL)
1361 memcpy(ds_copy, ds, sizeof (data_set_t));
1363 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1365 if (ds_copy->ds == NULL)
1371 for (i = 0; i < ds->ds_num; i++)
1372 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1374 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1375 } /* int plugin_register_data_set */
1377 int plugin_register_log (const char *name,
1378 plugin_log_cb callback, user_data_t *ud)
1380 return (create_register_callback (&list_log, name,
1381 (void *) callback, ud));
1382 } /* int plugin_register_log */
1384 int plugin_register_notification (const char *name,
1385 plugin_notification_cb callback, user_data_t *ud)
1387 return (create_register_callback (&list_notification, name,
1388 (void *) callback, ud));
1389 } /* int plugin_register_log */
1391 int plugin_unregister_config (const char *name)
1393 cf_unregister (name);
1395 } /* int plugin_unregister_config */
1397 int plugin_unregister_complex_config (const char *name)
1399 cf_unregister_complex (name);
1401 } /* int plugin_unregister_complex_config */
1403 int plugin_unregister_init (const char *name)
1405 return (plugin_unregister (list_init, name));
1408 int plugin_unregister_read (const char *name) /* {{{ */
1416 pthread_mutex_lock (&read_lock);
1418 if (read_list == NULL)
1420 pthread_mutex_unlock (&read_lock);
1424 le = llist_search (read_list, name);
1427 pthread_mutex_unlock (&read_lock);
1428 WARNING ("plugin_unregister_read: No such read function: %s",
1433 llist_remove (read_list, le);
1436 assert (rf != NULL);
1437 rf->rf_type = RF_REMOVE;
1439 pthread_mutex_unlock (&read_lock);
1441 llentry_destroy (le);
1443 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1446 } /* }}} int plugin_unregister_read */
1448 void plugin_log_available_writers (void)
1450 log_list_callbacks (&list_write, "Available write targets:");
1453 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1455 read_func_t *rf = e->value;
1458 return strcmp (rf->rf_group, (const char *)group);
1459 } /* }}} int compare_read_func_group */
1461 int plugin_unregister_read_group (const char *group) /* {{{ */
1471 pthread_mutex_lock (&read_lock);
1473 if (read_list == NULL)
1475 pthread_mutex_unlock (&read_lock);
1481 le = llist_search_custom (read_list,
1482 compare_read_func_group, (void *)group);
1489 llist_remove (read_list, le);
1492 assert (rf != NULL);
1493 rf->rf_type = RF_REMOVE;
1495 llentry_destroy (le);
1497 DEBUG ("plugin_unregister_read_group: "
1498 "Marked `%s' (group `%s') for removal.",
1499 rf->rf_name, group);
1502 pthread_mutex_unlock (&read_lock);
1506 WARNING ("plugin_unregister_read_group: No such "
1507 "group of read function: %s", group);
1512 } /* }}} int plugin_unregister_read_group */
1514 int plugin_unregister_write (const char *name)
1516 return (plugin_unregister (list_write, name));
1519 int plugin_unregister_flush (const char *name)
1521 return (plugin_unregister (list_flush, name));
1524 int plugin_unregister_missing (const char *name)
1526 return (plugin_unregister (list_missing, name));
1529 int plugin_unregister_shutdown (const char *name)
1531 return (plugin_unregister (list_shutdown, name));
1534 int plugin_unregister_data_set (const char *name)
1538 if (data_sets == NULL)
1541 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1548 } /* int plugin_unregister_data_set */
1550 int plugin_unregister_log (const char *name)
1552 return (plugin_unregister (list_log, name));
1555 int plugin_unregister_notification (const char *name)
1557 return (plugin_unregister (list_notification, name));
1560 void plugin_init_all (void)
1562 char const *chain_name;
1563 long write_threads_num;
1567 /* Init the value cache */
1570 if (IS_TRUE (global_option_get ("CollectInternalStats")))
1571 record_statistics = 1;
1573 chain_name = global_option_get ("PreCacheChain");
1574 pre_cache_chain = fc_chain_get_by_name (chain_name);
1576 chain_name = global_option_get ("PostCacheChain");
1577 post_cache_chain = fc_chain_get_by_name (chain_name);
1579 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1581 if (write_limit_high < 0)
1583 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1584 write_limit_high = 0;
1587 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1588 /* default = */ write_limit_high / 2);
1589 if (write_limit_low < 0)
1591 ERROR ("WriteQueueLimitLow must be positive or zero.");
1592 write_limit_low = write_limit_high / 2;
1594 else if (write_limit_low > write_limit_high)
1596 ERROR ("WriteQueueLimitLow must not be larger than "
1597 "WriteQueueLimitHigh.");
1598 write_limit_low = write_limit_high;
1601 write_threads_num = global_option_get_long ("WriteThreads",
1603 if (write_threads_num < 1)
1605 ERROR ("WriteThreads must be positive.");
1606 write_threads_num = 5;
1609 start_write_threads ((size_t) write_threads_num);
1611 if ((list_init == NULL) && (read_heap == NULL))
1614 /* Calling all init callbacks before checking if read callbacks
1615 * are available allows the init callbacks to register the read
1617 le = llist_head (list_init);
1620 callback_func_t *cf;
1621 plugin_init_cb callback;
1622 plugin_ctx_t old_ctx;
1625 old_ctx = plugin_set_ctx (cf->cf_ctx);
1626 callback = cf->cf_callback;
1627 status = (*callback) ();
1628 plugin_set_ctx (old_ctx);
1632 ERROR ("Initialization of plugin `%s' "
1633 "failed with status %i. "
1634 "Plugin will be unloaded.",
1636 /* Plugins that register read callbacks from the init
1637 * callback should take care of appropriate error
1638 * handling themselves. */
1639 /* FIXME: Unload _all_ functions */
1640 plugin_unregister_read (le->key);
1646 max_read_interval = global_option_get_time ("MaxReadInterval",
1647 DEFAULT_MAX_READ_INTERVAL);
1649 /* Start read-threads */
1650 if (read_heap != NULL)
1655 rt = global_option_get ("ReadThreads");
1658 start_read_threads ((num > 0) ? num : 5);
1660 } /* void plugin_init_all */
1662 /* TODO: Rename this function. */
1663 void plugin_read_all (void)
1665 if(record_statistics) {
1666 plugin_update_internal_statistics ();
1668 uc_check_timeout ();
1671 } /* void plugin_read_all */
1673 /* Read function called when the `-T' command line argument is given. */
1674 int plugin_read_all_once (void)
1677 int return_status = 0;
1679 if (read_heap == NULL)
1681 NOTICE ("No read-functions are registered.");
1688 plugin_ctx_t old_ctx;
1690 rf = c_heap_get_root (read_heap);
1694 old_ctx = plugin_set_ctx (rf->rf_ctx);
1696 if (rf->rf_type == RF_SIMPLE)
1698 int (*callback) (void);
1700 callback = rf->rf_callback;
1701 status = (*callback) ();
1705 plugin_read_cb callback;
1707 callback = rf->rf_callback;
1708 status = (*callback) (&rf->rf_udata);
1711 plugin_set_ctx (old_ctx);
1715 NOTICE ("read-function of plugin `%s' failed.",
1720 destroy_callback ((void *) rf);
1723 return (return_status);
1724 } /* int plugin_read_all_once */
1726 int plugin_write (const char *plugin, /* {{{ */
1727 const data_set_t *ds, const value_list_t *vl)
1735 if (list_write == NULL)
1740 ds = plugin_get_ds (vl->type);
1743 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1753 le = llist_head (list_write);
1756 callback_func_t *cf = le->value;
1757 plugin_write_cb callback;
1759 /* do not switch plugin context; rather keep the context (interval)
1760 * information of the calling read plugin */
1762 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1763 callback = cf->cf_callback;
1764 status = (*callback) (ds, vl, &cf->cf_udata);
1773 if ((success == 0) && (failure != 0))
1778 else /* plugin != NULL */
1780 callback_func_t *cf;
1781 plugin_write_cb callback;
1783 le = llist_head (list_write);
1786 if (strcasecmp (plugin, le->key) == 0)
1797 /* do not switch plugin context; rather keep the context (interval)
1798 * information of the calling read plugin */
1800 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1801 callback = cf->cf_callback;
1802 status = (*callback) (ds, vl, &cf->cf_udata);
1806 } /* }}} int plugin_write */
1808 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1812 if (list_flush == NULL)
1815 le = llist_head (list_flush);
1818 callback_func_t *cf;
1819 plugin_flush_cb callback;
1820 plugin_ctx_t old_ctx;
1822 if ((plugin != NULL)
1823 && (strcmp (plugin, le->key) != 0))
1830 old_ctx = plugin_set_ctx (cf->cf_ctx);
1831 callback = cf->cf_callback;
1833 (*callback) (timeout, identifier, &cf->cf_udata);
1835 plugin_set_ctx (old_ctx);
1840 } /* int plugin_flush */
1842 void plugin_shutdown_all (void)
1846 stop_read_threads ();
1848 destroy_all_callbacks (&list_init);
1850 pthread_mutex_lock (&read_lock);
1851 llist_destroy (read_list);
1853 pthread_mutex_unlock (&read_lock);
1855 destroy_read_heap ();
1857 plugin_flush (/* plugin = */ NULL,
1859 /* identifier = */ NULL);
1862 if (list_shutdown != NULL)
1863 le = llist_head (list_shutdown);
1867 callback_func_t *cf;
1868 plugin_shutdown_cb callback;
1869 plugin_ctx_t old_ctx;
1872 old_ctx = plugin_set_ctx (cf->cf_ctx);
1873 callback = cf->cf_callback;
1875 /* Advance the pointer before calling the callback allows
1876 * shutdown functions to unregister themselves. If done the
1877 * other way around the memory `le' points to will be freed
1878 * after callback returns. */
1883 plugin_set_ctx (old_ctx);
1886 stop_write_threads ();
1888 /* Write plugins which use the `user_data' pointer usually need the
1889 * same data available to the flush callback. If this is the case, set
1890 * the free_function to NULL when registering the flush callback and to
1891 * the real free function when registering the write callback. This way
1892 * the data isn't freed twice. */
1893 destroy_all_callbacks (&list_flush);
1894 destroy_all_callbacks (&list_missing);
1895 destroy_all_callbacks (&list_write);
1897 destroy_all_callbacks (&list_notification);
1898 destroy_all_callbacks (&list_shutdown);
1899 destroy_all_callbacks (&list_log);
1901 plugin_free_loaded ();
1902 plugin_free_data_sets ();
1903 } /* void plugin_shutdown_all */
1905 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1909 if (list_missing == NULL)
1912 le = llist_head (list_missing);
1915 callback_func_t *cf;
1916 plugin_missing_cb callback;
1917 plugin_ctx_t old_ctx;
1921 old_ctx = plugin_set_ctx (cf->cf_ctx);
1922 callback = cf->cf_callback;
1924 status = (*callback) (vl, &cf->cf_udata);
1925 plugin_set_ctx (old_ctx);
1930 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1931 "failed with status %i.",
1944 } /* int }}} plugin_dispatch_missing */
1946 static int plugin_dispatch_values_internal (value_list_t *vl)
1949 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1951 value_t *saved_values;
1952 int saved_values_len;
1956 int free_meta_data = 0;
1958 if ((vl == NULL) || (vl->type[0] == 0)
1959 || (vl->values == NULL) || (vl->values_len < 1))
1961 ERROR ("plugin_dispatch_values: Invalid value list "
1962 "from plugin %s.", vl->plugin);
1966 /* Free meta data only if the calling function didn't specify any. In
1967 * this case matches and targets may add some and the calling function
1968 * may not expect (and therefore free) that data. */
1969 if (vl->meta == NULL)
1972 if (list_write == NULL)
1973 c_complain_once (LOG_WARNING, &no_write_complaint,
1974 "plugin_dispatch_values: No write callback has been "
1975 "registered. Please load at least one output plugin, "
1976 "if you want the collected data to be stored.");
1978 if (data_sets == NULL)
1980 ERROR ("plugin_dispatch_values: No data sets registered. "
1981 "Could the types database be read? Check "
1982 "your `TypesDB' setting!");
1986 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1988 char ident[6 * DATA_MAX_NAME_LEN];
1990 FORMAT_VL (ident, sizeof (ident), vl);
1991 INFO ("plugin_dispatch_values: Dataset not found: %s "
1992 "(from \"%s\"), check your types.db!",
1997 /* Assured by plugin_value_list_clone(). The time is determined at
1998 * _enqueue_ time. */
1999 assert (vl->time != 0);
2000 assert (vl->interval != 0);
2002 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
2004 "plugin = %s; plugin_instance = %s; "
2005 "type = %s; type_instance = %s;",
2006 CDTIME_T_TO_DOUBLE (vl->time),
2007 CDTIME_T_TO_DOUBLE (vl->interval),
2009 vl->plugin, vl->plugin_instance,
2010 vl->type, vl->type_instance);
2013 assert (0 == strcmp (ds->type, vl->type));
2015 if (0 != strcmp (ds->type, vl->type))
2016 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
2017 ds->type, vl->type);
2021 assert (ds->ds_num == vl->values_len);
2023 if (ds->ds_num != vl->values_len)
2025 ERROR ("plugin_dispatch_values: ds->type = %s: "
2026 "(ds->ds_num = %i) != "
2027 "(vl->values_len = %i)",
2028 ds->type, ds->ds_num, vl->values_len);
2033 escape_slashes (vl->host, sizeof (vl->host));
2034 escape_slashes (vl->plugin, sizeof (vl->plugin));
2035 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
2036 escape_slashes (vl->type, sizeof (vl->type));
2037 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
2039 /* Copy the values. This way, we can assure `targets' that they get
2040 * dynamically allocated values, which they can free and replace if
2042 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
2044 saved_values = vl->values;
2045 saved_values_len = vl->values_len;
2047 vl->values = (value_t *) calloc (vl->values_len,
2048 sizeof (*vl->values));
2049 if (vl->values == NULL)
2051 ERROR ("plugin_dispatch_values: calloc failed.");
2052 vl->values = saved_values;
2055 memcpy (vl->values, saved_values,
2056 vl->values_len * sizeof (*vl->values));
2058 else /* if ((pre == NULL) && (post == NULL)) */
2060 saved_values = NULL;
2061 saved_values_len = 0;
2064 if (pre_cache_chain != NULL)
2066 status = fc_process_chain (ds, vl, pre_cache_chain);
2069 WARNING ("plugin_dispatch_values: Running the "
2070 "pre-cache chain failed with "
2074 else if (status == FC_TARGET_STOP)
2076 /* Restore the state of the value_list so that plugins
2077 * don't get confused.. */
2078 if (saved_values != NULL)
2081 vl->values = saved_values;
2082 vl->values_len = saved_values_len;
2088 /* Update the value cache */
2091 if (post_cache_chain != NULL)
2093 status = fc_process_chain (ds, vl, post_cache_chain);
2096 WARNING ("plugin_dispatch_values: Running the "
2097 "post-cache chain failed with "
2103 fc_default_action (ds, vl);
2105 /* Restore the state of the value_list so that plugins don't get
2107 if (saved_values != NULL)
2110 vl->values = saved_values;
2111 vl->values_len = saved_values_len;
2114 if ((free_meta_data != 0) && (vl->meta != NULL))
2116 meta_data_destroy (vl->meta);
2121 } /* int plugin_dispatch_values_internal */
2123 static double get_drop_probability (void) /* {{{ */
2129 pthread_mutex_lock (&write_lock);
2130 wql = write_queue_length;
2131 pthread_mutex_unlock (&write_lock);
2133 if (wql < write_limit_low)
2135 if (wql >= write_limit_high)
2138 pos = 1 + wql - write_limit_low;
2139 size = 1 + write_limit_high - write_limit_low;
2141 return (((double) pos) / ((double) size));
2142 } /* }}} double get_drop_probability */
2144 static _Bool check_drop_value (void) /* {{{ */
2146 static cdtime_t last_message_time = 0;
2147 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2153 if (write_limit_high == 0)
2156 p = get_drop_probability ();
2160 status = pthread_mutex_trylock (&last_message_lock);
2166 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2168 last_message_time = now;
2169 ERROR ("plugin_dispatch_values: Low water mark "
2170 "reached. Dropping %.0f%% of metrics.",
2173 pthread_mutex_unlock (&last_message_lock);
2184 } /* }}} _Bool check_drop_value */
2186 int plugin_dispatch_values (value_list_t const *vl)
2189 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2191 if (check_drop_value ()) {
2192 if(record_statistics) {
2193 pthread_mutex_lock(&statistics_lock);
2194 stats_values_dropped++;
2195 pthread_mutex_unlock(&statistics_lock);
2200 status = plugin_write_enqueue (vl);
2204 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2205 "with status %i (%s).", status,
2206 sstrerror (status, errbuf, sizeof (errbuf)));
2213 __attribute__((sentinel))
2214 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2215 _Bool store_percentage, int store_type, ...)
2222 assert (template->values_len == 1);
2224 /* Calculate sum for Gauge to calculate percent if needed */
2225 if (DS_TYPE_GAUGE == store_type) {
2226 va_start (ap, store_type);
2232 name = va_arg (ap, char const *);
2236 value = va_arg (ap, gauge_t);
2244 vl = plugin_value_list_clone (template);
2245 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2246 if (store_percentage)
2247 sstrncpy (vl->type, "percent", sizeof (vl->type));
2249 va_start (ap, store_type);
2255 /* Set the type instance. */
2256 name = va_arg (ap, char const *);
2259 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2261 /* Set the value. */
2265 vl->values[0].gauge = va_arg (ap, gauge_t);
2266 if (store_percentage)
2267 vl->values[0].gauge *= 100.0 / sum;
2269 case DS_TYPE_ABSOLUTE:
2270 vl->values[0].absolute = va_arg (ap, absolute_t);
2272 case DS_TYPE_COUNTER:
2273 vl->values[0].counter = va_arg (ap, counter_t);
2275 case DS_TYPE_DERIVE:
2276 vl->values[0].derive = va_arg (ap, derive_t);
2279 ERROR ("plugin_dispatch_multivalue: given store_type is incorrect.");
2284 status = plugin_write_enqueue (vl);
2290 plugin_value_list_free (vl);
2292 } /* }}} int plugin_dispatch_multivalue */
2294 int plugin_dispatch_notification (const notification_t *notif)
2297 /* Possible TODO: Add flap detection here */
2299 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2300 "time = %.3f; host = %s;",
2301 notif->severity, notif->message,
2302 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2304 /* Nobody cares for notifications */
2305 if (list_notification == NULL)
2308 le = llist_head (list_notification);
2311 callback_func_t *cf;
2312 plugin_notification_cb callback;
2315 /* do not switch plugin context; rather keep the context
2316 * (interval) information of the calling plugin */
2319 callback = cf->cf_callback;
2320 status = (*callback) (notif, &cf->cf_udata);
2323 WARNING ("plugin_dispatch_notification: Notification "
2324 "callback %s returned %i.",
2332 } /* int plugin_dispatch_notification */
2334 void plugin_log (int level, const char *format, ...)
2341 if (level >= LOG_DEBUG)
2345 va_start (ap, format);
2346 vsnprintf (msg, sizeof (msg), format, ap);
2347 msg[sizeof (msg) - 1] = '\0';
2350 if (list_log == NULL)
2352 fprintf (stderr, "%s\n", msg);
2356 le = llist_head (list_log);
2359 callback_func_t *cf;
2360 plugin_log_cb callback;
2363 callback = cf->cf_callback;
2365 /* do not switch plugin context; rather keep the context
2366 * (interval) information of the calling plugin */
2368 (*callback) (level, msg, &cf->cf_udata);
2372 } /* void plugin_log */
2374 int parse_log_severity (const char *severity)
2378 if ((0 == strcasecmp (severity, "emerg"))
2379 || (0 == strcasecmp (severity, "alert"))
2380 || (0 == strcasecmp (severity, "crit"))
2381 || (0 == strcasecmp (severity, "err")))
2382 log_level = LOG_ERR;
2383 else if (0 == strcasecmp (severity, "warning"))
2384 log_level = LOG_WARNING;
2385 else if (0 == strcasecmp (severity, "notice"))
2386 log_level = LOG_NOTICE;
2387 else if (0 == strcasecmp (severity, "info"))
2388 log_level = LOG_INFO;
2390 else if (0 == strcasecmp (severity, "debug"))
2391 log_level = LOG_DEBUG;
2392 #endif /* COLLECT_DEBUG */
2395 } /* int parse_log_severity */
2397 int parse_notif_severity (const char *severity)
2399 int notif_severity = -1;
2401 if (strcasecmp (severity, "FAILURE") == 0)
2402 notif_severity = NOTIF_FAILURE;
2403 else if (strcmp (severity, "OKAY") == 0)
2404 notif_severity = NOTIF_OKAY;
2405 else if ((strcmp (severity, "WARNING") == 0)
2406 || (strcmp (severity, "WARN") == 0))
2407 notif_severity = NOTIF_WARNING;
2409 return (notif_severity);
2410 } /* int parse_notif_severity */
2412 const data_set_t *plugin_get_ds (const char *name)
2416 if (data_sets == NULL)
2418 ERROR ("plugin_get_ds: No data sets are defined yet.");
2422 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2424 DEBUG ("No such dataset registered: %s", name);
2429 } /* data_set_t *plugin_get_ds */
2431 static int plugin_notification_meta_add (notification_t *n,
2433 enum notification_meta_type_e type,
2436 notification_meta_t *meta;
2437 notification_meta_t *tail;
2439 if ((n == NULL) || (name == NULL) || (value == NULL))
2441 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2445 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2448 ERROR ("plugin_notification_meta_add: malloc failed.");
2451 memset (meta, 0, sizeof (notification_meta_t));
2453 sstrncpy (meta->name, name, sizeof (meta->name));
2458 case NM_TYPE_STRING:
2460 meta->nm_value.nm_string = strdup ((const char *) value);
2461 if (meta->nm_value.nm_string == NULL)
2463 ERROR ("plugin_notification_meta_add: strdup failed.");
2469 case NM_TYPE_SIGNED_INT:
2471 meta->nm_value.nm_signed_int = *((int64_t *) value);
2474 case NM_TYPE_UNSIGNED_INT:
2476 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2479 case NM_TYPE_DOUBLE:
2481 meta->nm_value.nm_double = *((double *) value);
2484 case NM_TYPE_BOOLEAN:
2486 meta->nm_value.nm_boolean = *((_Bool *) value);
2491 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2495 } /* switch (type) */
2499 while ((tail != NULL) && (tail->next != NULL))
2508 } /* int plugin_notification_meta_add */
2510 int plugin_notification_meta_add_string (notification_t *n,
2514 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2517 int plugin_notification_meta_add_signed_int (notification_t *n,
2521 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2524 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2528 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2531 int plugin_notification_meta_add_double (notification_t *n,
2535 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2538 int plugin_notification_meta_add_boolean (notification_t *n,
2542 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2545 int plugin_notification_meta_copy (notification_t *dst,
2546 const notification_t *src)
2548 notification_meta_t *meta;
2550 assert (dst != NULL);
2551 assert (src != NULL);
2552 assert (dst != src);
2553 assert ((src->meta == NULL) || (src->meta != dst->meta));
2555 for (meta = src->meta; meta != NULL; meta = meta->next)
2557 if (meta->type == NM_TYPE_STRING)
2558 plugin_notification_meta_add_string (dst, meta->name,
2559 meta->nm_value.nm_string);
2560 else if (meta->type == NM_TYPE_SIGNED_INT)
2561 plugin_notification_meta_add_signed_int (dst, meta->name,
2562 meta->nm_value.nm_signed_int);
2563 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2564 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2565 meta->nm_value.nm_unsigned_int);
2566 else if (meta->type == NM_TYPE_DOUBLE)
2567 plugin_notification_meta_add_double (dst, meta->name,
2568 meta->nm_value.nm_double);
2569 else if (meta->type == NM_TYPE_BOOLEAN)
2570 plugin_notification_meta_add_boolean (dst, meta->name,
2571 meta->nm_value.nm_boolean);
2575 } /* int plugin_notification_meta_copy */
2577 int plugin_notification_meta_free (notification_meta_t *n)
2579 notification_meta_t *this;
2580 notification_meta_t *next;
2584 ERROR ("plugin_notification_meta_free: n == NULL!");
2589 while (this != NULL)
2593 if (this->type == NM_TYPE_STRING)
2595 free ((char *)this->nm_value.nm_string);
2596 this->nm_value.nm_string = NULL;
2604 } /* int plugin_notification_meta_free */
2606 static void plugin_ctx_destructor (void *ctx)
2609 } /* void plugin_ctx_destructor */
2611 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2613 static plugin_ctx_t *plugin_ctx_create (void)
2617 ctx = malloc (sizeof (*ctx));
2620 ERROR ("Failed to allocate plugin context: %s",
2621 sstrerror (errno, errbuf, sizeof (errbuf)));
2626 assert (plugin_ctx_key_initialized);
2627 pthread_setspecific (plugin_ctx_key, ctx);
2628 DEBUG("Created new plugin context.");
2630 } /* int plugin_ctx_create */
2632 void plugin_init_ctx (void)
2634 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2635 plugin_ctx_key_initialized = 1;
2636 } /* void plugin_init_ctx */
2638 plugin_ctx_t plugin_get_ctx (void)
2642 assert (plugin_ctx_key_initialized);
2643 ctx = pthread_getspecific (plugin_ctx_key);
2646 ctx = plugin_ctx_create ();
2647 /* this must no happen -- exit() instead? */
2653 } /* plugin_ctx_t plugin_get_ctx */
2655 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2660 assert (plugin_ctx_key_initialized);
2661 c = pthread_getspecific (plugin_ctx_key);
2664 c = plugin_ctx_create ();
2665 /* this must no happen -- exit() instead? */
2674 } /* void plugin_set_ctx */
2676 cdtime_t plugin_get_interval (void)
2680 interval = plugin_get_ctx().interval;
2684 return cf_get_default_interval ();
2685 } /* cdtime_t plugin_get_interval */
2689 void *(*start_routine) (void *);
2693 static void *plugin_thread_start (void *arg)
2695 plugin_thread_t *plugin_thread = arg;
2697 void *(*start_routine) (void *) = plugin_thread->start_routine;
2698 void *plugin_arg = plugin_thread->arg;
2700 plugin_set_ctx (plugin_thread->ctx);
2702 free (plugin_thread);
2704 return start_routine (plugin_arg);
2705 } /* void *plugin_thread_start */
2707 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2708 void *(*start_routine) (void *), void *arg)
2710 plugin_thread_t *plugin_thread;
2712 plugin_thread = malloc (sizeof (*plugin_thread));
2713 if (plugin_thread == NULL)
2716 plugin_thread->ctx = plugin_get_ctx ();
2717 plugin_thread->start_routine = start_routine;
2718 plugin_thread->arg = arg;
2720 return pthread_create (thread, attr,
2721 plugin_thread_start, plugin_thread);
2722 } /* int plugin_thread_create */
2724 /* vim: set sw=8 ts=8 noet fdm=marker : */