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"
50 struct callback_func_s
56 typedef struct callback_func_s callback_func_t;
60 #define RF_REMOVE 65535
63 /* `read_func_t' "inherits" from `callback_func_t'.
64 * The `rf_super' member MUST be the first one in this structure! */
65 #define rf_callback rf_super.cf_callback
66 #define rf_udata rf_super.cf_udata
67 #define rf_ctx rf_super.cf_ctx
68 callback_func_t rf_super;
69 char rf_group[DATA_MAX_NAME_LEN];
73 cdtime_t rf_effective_interval;
74 cdtime_t rf_next_read;
76 typedef struct read_func_s read_func_t;
79 typedef struct write_queue_s write_queue_t;
90 static c_avl_tree_t *plugins_loaded = NULL;
92 static llist_t *list_init;
93 static llist_t *list_write;
94 static llist_t *list_flush;
95 static llist_t *list_missing;
96 static llist_t *list_shutdown;
97 static llist_t *list_log;
98 static llist_t *list_notification;
100 static fc_chain_t *pre_cache_chain = NULL;
101 static fc_chain_t *post_cache_chain = NULL;
103 static c_avl_tree_t *data_sets;
105 static char *plugindir = NULL;
107 #ifndef DEFAULT_MAX_READ_INTERVAL
108 # define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T (86400)
110 static c_heap_t *read_heap = NULL;
111 static llist_t *read_list;
112 static int read_loop = 1;
113 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
114 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
115 static pthread_t *read_threads = NULL;
116 static int read_threads_num = 0;
117 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
119 static write_queue_t *write_queue_head;
120 static write_queue_t *write_queue_tail;
121 static long write_queue_length = 0;
122 static _Bool write_loop = 1;
123 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
124 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
125 static pthread_t *write_threads = NULL;
126 static size_t write_threads_num = 0;
128 static pthread_key_t plugin_ctx_key;
129 static _Bool plugin_ctx_key_initialized = 0;
131 static long write_limit_high = 0;
132 static long write_limit_low = 0;
134 static derive_t stats_values_dropped = 0;
135 static _Bool record_statistics = 0;
140 static int plugin_dispatch_values_internal (value_list_t *vl);
142 static const char *plugin_get_dir (void)
144 if (plugindir == NULL)
150 static void plugin_update_internal_statistics (void) { /* {{{ */
151 derive_t copy_write_queue_length;
152 value_list_t vl = VALUE_LIST_INIT;
155 copy_write_queue_length = write_queue_length;
157 /* Initialize `vl' */
161 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
162 sstrncpy (vl.plugin, "collectd", sizeof (vl.plugin));
164 vl.type_instance[0] = 0;
168 sstrncpy (vl.plugin_instance, "write_queue",
169 sizeof (vl.plugin_instance));
171 /* Write queue : queue length */
172 vl.values[0].gauge = (gauge_t) copy_write_queue_length;
173 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
174 vl.type_instance[0] = 0;
175 plugin_dispatch_values (&vl);
177 /* Write queue : Values dropped (queue length > low limit) */
178 vl.values[0].derive = (derive_t) stats_values_dropped;
179 sstrncpy (vl.type, "derive", sizeof (vl.type));
180 sstrncpy (vl.type_instance, "dropped", sizeof (vl.type_instance));
181 plugin_dispatch_values (&vl);
184 sstrncpy (vl.plugin_instance, "cache",
185 sizeof (vl.plugin_instance));
187 /* Cache : Nb entry in cache tree */
188 vl.values[0].gauge = (gauge_t) uc_get_size();
189 sstrncpy (vl.type, "cache_size", sizeof (vl.type));
190 vl.type_instance[0] = 0;
191 plugin_dispatch_values (&vl);
194 } /* }}} void plugin_update_internal_statistics */
196 static void destroy_callback (callback_func_t *cf) /* {{{ */
201 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
203 cf->cf_udata.free_func (cf->cf_udata.data);
204 cf->cf_udata.data = NULL;
205 cf->cf_udata.free_func = NULL;
208 } /* }}} void destroy_callback */
210 static void destroy_all_callbacks (llist_t **list) /* {{{ */
217 le = llist_head (*list);
225 destroy_callback (le->value);
231 llist_destroy (*list);
233 } /* }}} void destroy_all_callbacks */
235 static void destroy_read_heap (void) /* {{{ */
237 if (read_heap == NULL)
244 cf = c_heap_get_root (read_heap);
248 destroy_callback (cf);
251 c_heap_destroy (read_heap);
253 } /* }}} void destroy_read_heap */
255 static int register_callback (llist_t **list, /* {{{ */
256 const char *name, callback_func_t *cf)
263 *list = llist_create ();
266 ERROR ("plugin: register_callback: "
267 "llist_create failed.");
268 destroy_callback (cf);
276 ERROR ("plugin: register_callback: strdup failed.");
277 destroy_callback (cf);
281 le = llist_search (*list, name);
284 le = llentry_create (key, cf);
287 ERROR ("plugin: register_callback: "
288 "llentry_create failed.");
290 destroy_callback (cf);
294 llist_append (*list, le);
298 callback_func_t *old_cf;
303 WARNING ("plugin: register_callback: "
304 "a callback named `%s' already exists - "
305 "overwriting the old entry!", name);
307 destroy_callback (old_cf);
312 } /* }}} int register_callback */
314 static int create_register_callback (llist_t **list, /* {{{ */
315 const char *name, void *callback, user_data_t *ud)
319 cf = (callback_func_t *) malloc (sizeof (*cf));
322 ERROR ("plugin: create_register_callback: malloc failed.");
325 memset (cf, 0, sizeof (*cf));
327 cf->cf_callback = callback;
330 cf->cf_udata.data = NULL;
331 cf->cf_udata.free_func = NULL;
338 cf->cf_ctx = plugin_get_ctx ();
340 return (register_callback (list, name, cf));
341 } /* }}} int create_register_callback */
343 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
350 e = llist_search (list, name);
354 llist_remove (list, e);
357 destroy_callback (e->value);
362 } /* }}} int plugin_unregister */
365 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
366 * object, but it will bitch about a shared object not having a
367 * ``module_register'' symbol..
369 static int plugin_load_file (char *file, uint32_t flags)
372 void (*reg_handle) (void);
375 lt_dlerror (); /* clear errors */
377 #if LIBTOOL_VERSION == 2
378 if (flags & PLUGIN_FLAGS_GLOBAL) {
380 lt_dladvise_init(&advise);
381 lt_dladvise_global(&advise);
382 dlh = lt_dlopenadvise(file, advise);
383 lt_dladvise_destroy(&advise);
385 dlh = lt_dlopen (file);
387 #else /* if LIBTOOL_VERSION == 1 */
388 if (flags & PLUGIN_FLAGS_GLOBAL)
389 WARNING ("plugin_load_file: The global flag is not supported, "
390 "libtool 2 is required for this.");
391 dlh = lt_dlopen (file);
396 char errbuf[1024] = "";
398 ssnprintf (errbuf, sizeof (errbuf),
399 "lt_dlopen (\"%s\") failed: %s. "
400 "The most common cause for this problem are "
401 "missing dependencies. Use ldd(1) to check "
402 "the dependencies of the plugin "
404 file, lt_dlerror ());
406 ERROR ("%s", errbuf);
407 /* Make sure this is printed to STDERR in any case, but also
408 * make sure it's printed only once. */
409 if (list_log != NULL)
410 fprintf (stderr, "ERROR: %s\n", errbuf);
415 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
417 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
418 file, lt_dlerror ());
428 static void *plugin_read_thread (void __attribute__((unused)) *args)
430 while (read_loop != 0)
433 plugin_ctx_t old_ctx;
439 /* Get the read function that needs to be read next.
440 * We don't need to hold "read_lock" for the heap, but we need
441 * to call c_heap_get_root() and pthread_cond_wait() in the
442 * same protected block. */
443 pthread_mutex_lock (&read_lock);
444 rf = c_heap_get_root (read_heap);
447 pthread_cond_wait (&read_cond, &read_lock);
448 pthread_mutex_unlock (&read_lock);
451 pthread_mutex_unlock (&read_lock);
453 if (rf->rf_interval == 0)
455 /* this should not happen, because the interval is set
456 * for each plugin when loading it
457 * XXX: issue a warning? */
458 rf->rf_interval = plugin_get_interval ();
459 rf->rf_effective_interval = rf->rf_interval;
461 rf->rf_next_read = cdtime ();
464 /* sleep until this entry is due,
465 * using pthread_cond_timedwait */
466 pthread_mutex_lock (&read_lock);
467 /* In pthread_cond_timedwait, spurious wakeups are possible
468 * (and really happen, at least on NetBSD with > 1 CPU), thus
469 * we need to re-evaluate the condition every time
470 * pthread_cond_timedwait returns. */
472 while ((read_loop != 0)
473 && (cdtime () < rf->rf_next_read)
476 struct timespec ts = { 0 };
478 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
480 rc = pthread_cond_timedwait (&read_cond, &read_lock,
484 /* Must hold `read_lock' when accessing `rf->rf_type'. */
485 rf_type = rf->rf_type;
486 pthread_mutex_unlock (&read_lock);
488 /* Check if we're supposed to stop.. This may have interrupted
492 /* Insert `rf' again, so it can be free'd correctly */
493 c_heap_insert (read_heap, rf);
497 /* The entry has been marked for deletion. The linked list
498 * entry has already been removed by `plugin_unregister_read'.
499 * All we have to do here is free the `read_func_t' and
501 if (rf_type == RF_REMOVE)
503 DEBUG ("plugin_read_thread: Destroying the `%s' "
504 "callback.", rf->rf_name);
506 destroy_callback ((callback_func_t *) rf);
511 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
513 old_ctx = plugin_set_ctx (rf->rf_ctx);
515 if (rf_type == RF_SIMPLE)
517 int (*callback) (void);
519 callback = rf->rf_callback;
520 status = (*callback) ();
524 plugin_read_cb callback;
526 assert (rf_type == RF_COMPLEX);
528 callback = rf->rf_callback;
529 status = (*callback) (&rf->rf_udata);
532 plugin_set_ctx (old_ctx);
534 /* If the function signals failure, we will increase the
535 * intervals in which it will be called. */
538 rf->rf_effective_interval *= 2;
539 if (rf->rf_effective_interval > max_read_interval)
540 rf->rf_effective_interval = max_read_interval;
542 NOTICE ("read-function of plugin `%s' failed. "
543 "Will suspend it for %.3f seconds.",
545 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
549 /* Success: Restore the interval, if it was changed. */
550 rf->rf_effective_interval = rf->rf_interval;
553 /* update the ``next read due'' field */
556 DEBUG ("plugin_read_thread: Effective interval of the "
557 "%s plugin is %.3f seconds.",
559 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
561 /* Calculate the next (absolute) time at which this function
562 * should be called. */
563 rf->rf_next_read += rf->rf_effective_interval;
565 /* Check, if `rf_next_read' is in the past. */
566 if (rf->rf_next_read < now)
568 /* `rf_next_read' is in the past. Insert `now'
569 * so this value doesn't trail off into the
571 rf->rf_next_read = now;
574 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
576 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
578 /* Re-insert this read function into the heap again. */
579 c_heap_insert (read_heap, rf);
580 } /* while (read_loop) */
584 } /* void *plugin_read_thread */
586 static void start_read_threads (int num)
590 if (read_threads != NULL)
593 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
594 if (read_threads == NULL)
596 ERROR ("plugin: start_read_threads: calloc failed.");
600 read_threads_num = 0;
601 for (i = 0; i < num; i++)
603 if (pthread_create (read_threads + read_threads_num, NULL,
604 plugin_read_thread, NULL) == 0)
610 ERROR ("plugin: start_read_threads: pthread_create failed.");
614 } /* void start_read_threads */
616 static void stop_read_threads (void)
620 if (read_threads == NULL)
623 INFO ("collectd: Stopping %i read threads.", read_threads_num);
625 pthread_mutex_lock (&read_lock);
627 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
628 pthread_cond_broadcast (&read_cond);
629 pthread_mutex_unlock (&read_lock);
631 for (i = 0; i < read_threads_num; i++)
633 if (pthread_join (read_threads[i], NULL) != 0)
635 ERROR ("plugin: stop_read_threads: pthread_join failed.");
637 read_threads[i] = (pthread_t) 0;
639 sfree (read_threads);
640 read_threads_num = 0;
641 } /* void stop_read_threads */
643 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
648 meta_data_destroy (vl->meta);
651 } /* }}} void plugin_value_list_free */
653 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
660 vl = malloc (sizeof (*vl));
663 memcpy (vl, vl_orig, sizeof (*vl));
665 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
666 if (vl->values == NULL)
668 plugin_value_list_free (vl);
671 memcpy (vl->values, vl_orig->values,
672 vl_orig->values_len * sizeof (*vl->values));
674 vl->meta = meta_data_clone (vl->meta);
675 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
677 plugin_value_list_free (vl);
682 vl->time = cdtime ();
684 /* Fill in the interval from the thread context, if it is zero. */
685 if (vl->interval == 0)
687 plugin_ctx_t ctx = plugin_get_ctx ();
689 if (ctx.interval != 0)
690 vl->interval = ctx.interval;
693 char name[6 * DATA_MAX_NAME_LEN];
694 FORMAT_VL (name, sizeof (name), vl);
695 ERROR ("plugin_value_list_clone: Unable to determine "
696 "interval from context for "
697 "value list \"%s\". "
698 "This indicates a broken plugin. "
699 "Please report this problem to the "
700 "collectd mailing list or at "
701 "<http://collectd.org/bugs/>.", name);
702 vl->interval = cf_get_default_interval ();
707 } /* }}} value_list_t *plugin_value_list_clone */
709 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
713 q = malloc (sizeof (*q));
718 q->vl = plugin_value_list_clone (vl);
725 /* Store context of caller (read plugin); otherwise, it would not be
726 * available to the write plugins when actually dispatching the
727 * value-list later on. */
728 q->ctx = plugin_get_ctx ();
730 pthread_mutex_lock (&write_lock);
732 if (write_queue_tail == NULL)
734 write_queue_head = q;
735 write_queue_tail = q;
736 write_queue_length = 1;
740 write_queue_tail->next = q;
741 write_queue_tail = q;
742 write_queue_length += 1;
745 pthread_cond_signal (&write_cond);
746 pthread_mutex_unlock (&write_lock);
749 } /* }}} int plugin_write_enqueue */
751 static value_list_t *plugin_write_dequeue (void) /* {{{ */
756 pthread_mutex_lock (&write_lock);
758 while (write_loop && (write_queue_head == NULL))
759 pthread_cond_wait (&write_cond, &write_lock);
761 if (write_queue_head == NULL)
763 pthread_mutex_unlock (&write_lock);
767 q = write_queue_head;
768 write_queue_head = q->next;
769 write_queue_length -= 1;
770 if (write_queue_head == NULL) {
771 write_queue_tail = NULL;
772 assert(0 == write_queue_length);
775 pthread_mutex_unlock (&write_lock);
777 (void) plugin_set_ctx (q->ctx);
782 } /* }}} value_list_t *plugin_write_dequeue */
784 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
788 value_list_t *vl = plugin_write_dequeue ();
792 plugin_dispatch_values_internal (vl);
794 plugin_value_list_free (vl);
799 } /* }}} void *plugin_write_thread */
801 static void start_write_threads (size_t num) /* {{{ */
805 if (write_threads != NULL)
808 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
809 if (write_threads == NULL)
811 ERROR ("plugin: start_write_threads: calloc failed.");
815 write_threads_num = 0;
816 for (i = 0; i < num; i++)
820 status = pthread_create (write_threads + write_threads_num,
827 ERROR ("plugin: start_write_threads: pthread_create failed "
828 "with status %i (%s).", status,
829 sstrerror (status, errbuf, sizeof (errbuf)));
835 } /* }}} void start_write_threads */
837 static void stop_write_threads (void) /* {{{ */
842 if (write_threads == NULL)
845 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
847 pthread_mutex_lock (&write_lock);
849 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
850 pthread_cond_broadcast (&write_cond);
851 pthread_mutex_unlock (&write_lock);
853 for (i = 0; i < write_threads_num; i++)
855 if (pthread_join (write_threads[i], NULL) != 0)
857 ERROR ("plugin: stop_write_threads: pthread_join failed.");
859 write_threads[i] = (pthread_t) 0;
861 sfree (write_threads);
862 write_threads_num = 0;
864 pthread_mutex_lock (&write_lock);
866 for (q = write_queue_head; q != NULL; )
868 write_queue_t *q1 = q;
869 plugin_value_list_free (q->vl);
874 write_queue_head = NULL;
875 write_queue_tail = NULL;
876 write_queue_length = 0;
877 pthread_mutex_unlock (&write_lock);
881 WARNING ("plugin: %i value list%s left after shutting down "
882 "the write threads.",
883 i, (i == 1) ? " was" : "s were");
885 } /* }}} void stop_write_threads */
890 void plugin_set_dir (const char *dir)
892 if (plugindir != NULL)
897 else if ((plugindir = strdup (dir)) == NULL)
900 ERROR ("strdup failed: %s",
901 sstrerror (errno, errbuf, sizeof (errbuf)));
905 static _Bool plugin_is_loaded (char const *name)
909 if (plugins_loaded == NULL)
910 plugins_loaded = c_avl_create ((void *) strcasecmp);
911 assert (plugins_loaded != NULL);
913 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
914 return (status == 0);
917 static int plugin_mark_loaded (char const *name)
922 name_copy = strdup (name);
923 if (name_copy == NULL)
926 status = c_avl_insert (plugins_loaded,
927 /* key = */ name_copy, /* value = */ NULL);
931 static void plugin_free_loaded ()
936 if (plugins_loaded == NULL)
939 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
942 assert (value == NULL);
945 c_avl_destroy (plugins_loaded);
946 plugins_loaded = NULL;
950 int plugin_load (char const *plugin_name, uint32_t flags)
954 char filename[BUFSIZE] = "";
955 char typename[BUFSIZE];
962 if (plugin_name == NULL)
965 /* Check if plugin is already loaded and don't do anything in this
967 if (plugin_is_loaded (plugin_name))
970 dir = plugin_get_dir ();
974 * XXX: Magic at work:
976 * Some of the language bindings, for example the Python and Perl
977 * plugins, need to be able to export symbols to the scripts they run.
978 * For this to happen, the "Globals" flag needs to be set.
979 * Unfortunately, this technical detail is hard to explain to the
980 * average user and she shouldn't have to worry about this, ideally.
981 * So in order to save everyone's sanity use a different default for a
982 * handful of special plugins. --octo
984 if ((strcasecmp ("perl", plugin_name) == 0)
985 || (strcasecmp ("python", plugin_name) == 0))
986 flags |= PLUGIN_FLAGS_GLOBAL;
988 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
989 * type when matching the filename */
990 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
991 if ((status < 0) || ((size_t) status >= sizeof (typename)))
993 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
996 typename_len = strlen (typename);
998 if ((dh = opendir (dir)) == NULL)
1001 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
1002 sstrerror (errno, errbuf, sizeof (errbuf)));
1006 while ((de = readdir (dh)) != NULL)
1008 if (strncasecmp (de->d_name, typename, typename_len))
1011 status = ssnprintf (filename, sizeof (filename),
1012 "%s/%s", dir, de->d_name);
1013 if ((status < 0) || ((size_t) status >= sizeof (filename)))
1015 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
1020 if (lstat (filename, &statbuf) == -1)
1023 WARNING ("plugin_load: stat (\"%s\") failed: %s",
1025 sstrerror (errno, errbuf, sizeof (errbuf)));
1028 else if (!S_ISREG (statbuf.st_mode))
1030 /* don't follow symlinks */
1031 WARNING ("plugin_load: %s is not a regular file.",
1036 status = plugin_load_file (filename, flags);
1040 plugin_mark_loaded (plugin_name);
1046 ERROR ("plugin_load: Load plugin \"%s\" failed with "
1047 "status %i.", plugin_name, status);
1053 if (filename[0] == 0)
1054 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1061 * The `register_*' functions follow
1063 int plugin_register_config (const char *name,
1064 int (*callback) (const char *key, const char *val),
1065 const char **keys, int keys_num)
1067 cf_register (name, callback, keys, keys_num);
1069 } /* int plugin_register_config */
1071 int plugin_register_complex_config (const char *type,
1072 int (*callback) (oconfig_item_t *))
1074 return (cf_register_complex (type, callback));
1075 } /* int plugin_register_complex_config */
1077 int plugin_register_init (const char *name,
1078 int (*callback) (void))
1080 return (create_register_callback (&list_init, name, (void *) callback,
1081 /* user_data = */ NULL));
1082 } /* plugin_register_init */
1084 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1086 const read_func_t *rf0;
1087 const read_func_t *rf1;
1092 if (rf0->rf_next_read < rf1->rf_next_read)
1094 else if (rf0->rf_next_read > rf1->rf_next_read)
1098 } /* int plugin_compare_read_func */
1100 /* Add a read function to both, the heap and a linked list. The linked list if
1101 * used to look-up read functions, especially for the remove function. The heap
1102 * is used to determine which plugin to read next. */
1103 static int plugin_insert_read (read_func_t *rf)
1108 rf->rf_next_read = cdtime ();
1109 rf->rf_effective_interval = rf->rf_interval;
1111 pthread_mutex_lock (&read_lock);
1113 if (read_list == NULL)
1115 read_list = llist_create ();
1116 if (read_list == NULL)
1118 pthread_mutex_unlock (&read_lock);
1119 ERROR ("plugin_insert_read: read_list failed.");
1124 if (read_heap == NULL)
1126 read_heap = c_heap_create (plugin_compare_read_func);
1127 if (read_heap == NULL)
1129 pthread_mutex_unlock (&read_lock);
1130 ERROR ("plugin_insert_read: c_heap_create failed.");
1135 le = llist_search (read_list, rf->rf_name);
1138 pthread_mutex_unlock (&read_lock);
1139 WARNING ("The read function \"%s\" is already registered. "
1140 "Check for duplicate \"LoadPlugin\" lines "
1141 "in your configuration!",
1146 le = llentry_create (rf->rf_name, rf);
1149 pthread_mutex_unlock (&read_lock);
1150 ERROR ("plugin_insert_read: llentry_create failed.");
1154 status = c_heap_insert (read_heap, rf);
1157 pthread_mutex_unlock (&read_lock);
1158 ERROR ("plugin_insert_read: c_heap_insert failed.");
1159 llentry_destroy (le);
1163 /* This does not fail. */
1164 llist_append (read_list, le);
1166 /* Wake up all the read threads. */
1167 pthread_cond_broadcast (&read_cond);
1168 pthread_mutex_unlock (&read_lock);
1170 } /* int plugin_insert_read */
1172 int plugin_register_read (const char *name,
1173 int (*callback) (void))
1178 rf = malloc (sizeof (*rf));
1181 ERROR ("plugin_register_read: malloc failed.");
1185 memset (rf, 0, sizeof (read_func_t));
1186 rf->rf_callback = (void *) callback;
1187 rf->rf_udata.data = NULL;
1188 rf->rf_udata.free_func = NULL;
1189 rf->rf_ctx = plugin_get_ctx ();
1190 rf->rf_group[0] = '\0';
1191 rf->rf_name = strdup (name);
1192 rf->rf_type = RF_SIMPLE;
1193 rf->rf_interval = plugin_get_interval ();
1195 status = plugin_insert_read (rf);
1200 } /* int plugin_register_read */
1202 int plugin_register_complex_read (const char *group, const char *name,
1203 plugin_read_cb callback,
1204 const struct timespec *interval,
1205 user_data_t *user_data)
1210 rf = malloc (sizeof (*rf));
1213 ERROR ("plugin_register_complex_read: malloc failed.");
1217 memset (rf, 0, sizeof (read_func_t));
1218 rf->rf_callback = (void *) callback;
1220 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1222 rf->rf_group[0] = '\0';
1223 rf->rf_name = strdup (name);
1224 rf->rf_type = RF_COMPLEX;
1225 if (interval != NULL)
1226 rf->rf_interval = TIMESPEC_TO_CDTIME_T (interval);
1228 rf->rf_interval = plugin_get_interval ();
1231 if (user_data == NULL)
1233 rf->rf_udata.data = NULL;
1234 rf->rf_udata.free_func = NULL;
1238 rf->rf_udata = *user_data;
1241 rf->rf_ctx = plugin_get_ctx ();
1243 status = plugin_insert_read (rf);
1248 } /* int plugin_register_complex_read */
1250 int plugin_register_write (const char *name,
1251 plugin_write_cb callback, user_data_t *ud)
1253 return (create_register_callback (&list_write, name,
1254 (void *) callback, ud));
1255 } /* int plugin_register_write */
1257 int plugin_register_flush (const char *name,
1258 plugin_flush_cb callback, user_data_t *ud)
1260 return (create_register_callback (&list_flush, name,
1261 (void *) callback, ud));
1262 } /* int plugin_register_flush */
1264 int plugin_register_missing (const char *name,
1265 plugin_missing_cb callback, user_data_t *ud)
1267 return (create_register_callback (&list_missing, name,
1268 (void *) callback, ud));
1269 } /* int plugin_register_missing */
1271 int plugin_register_shutdown (const char *name,
1272 int (*callback) (void))
1274 return (create_register_callback (&list_shutdown, name,
1275 (void *) callback, /* user_data = */ NULL));
1276 } /* int plugin_register_shutdown */
1278 static void plugin_free_data_sets (void)
1283 if (data_sets == NULL)
1286 while (c_avl_pick (data_sets, &key, &value) == 0)
1288 data_set_t *ds = value;
1289 /* key is a pointer to ds->type */
1295 c_avl_destroy (data_sets);
1297 } /* void plugin_free_data_sets */
1299 int plugin_register_data_set (const data_set_t *ds)
1301 data_set_t *ds_copy;
1304 if ((data_sets != NULL)
1305 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1307 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1308 plugin_unregister_data_set (ds->type);
1310 else if (data_sets == NULL)
1312 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1313 if (data_sets == NULL)
1317 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1318 if (ds_copy == NULL)
1320 memcpy(ds_copy, ds, sizeof (data_set_t));
1322 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1324 if (ds_copy->ds == NULL)
1330 for (i = 0; i < ds->ds_num; i++)
1331 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1333 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1334 } /* int plugin_register_data_set */
1336 int plugin_register_log (const char *name,
1337 plugin_log_cb callback, user_data_t *ud)
1339 return (create_register_callback (&list_log, name,
1340 (void *) callback, ud));
1341 } /* int plugin_register_log */
1343 int plugin_register_notification (const char *name,
1344 plugin_notification_cb callback, user_data_t *ud)
1346 return (create_register_callback (&list_notification, name,
1347 (void *) callback, ud));
1348 } /* int plugin_register_log */
1350 int plugin_unregister_config (const char *name)
1352 cf_unregister (name);
1354 } /* int plugin_unregister_config */
1356 int plugin_unregister_complex_config (const char *name)
1358 cf_unregister_complex (name);
1360 } /* int plugin_unregister_complex_config */
1362 int plugin_unregister_init (const char *name)
1364 return (plugin_unregister (list_init, name));
1367 int plugin_unregister_read (const char *name) /* {{{ */
1375 pthread_mutex_lock (&read_lock);
1377 if (read_list == NULL)
1379 pthread_mutex_unlock (&read_lock);
1383 le = llist_search (read_list, name);
1386 pthread_mutex_unlock (&read_lock);
1387 WARNING ("plugin_unregister_read: No such read function: %s",
1392 llist_remove (read_list, le);
1395 assert (rf != NULL);
1396 rf->rf_type = RF_REMOVE;
1398 pthread_mutex_unlock (&read_lock);
1400 llentry_destroy (le);
1402 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1405 } /* }}} int plugin_unregister_read */
1407 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1409 read_func_t *rf = e->value;
1412 return strcmp (rf->rf_group, (const char *)group);
1413 } /* }}} int compare_read_func_group */
1415 int plugin_unregister_read_group (const char *group) /* {{{ */
1425 pthread_mutex_lock (&read_lock);
1427 if (read_list == NULL)
1429 pthread_mutex_unlock (&read_lock);
1435 le = llist_search_custom (read_list,
1436 compare_read_func_group, (void *)group);
1443 llist_remove (read_list, le);
1446 assert (rf != NULL);
1447 rf->rf_type = RF_REMOVE;
1449 llentry_destroy (le);
1451 DEBUG ("plugin_unregister_read_group: "
1452 "Marked `%s' (group `%s') for removal.",
1453 rf->rf_name, group);
1456 pthread_mutex_unlock (&read_lock);
1460 WARNING ("plugin_unregister_read_group: No such "
1461 "group of read function: %s", group);
1466 } /* }}} int plugin_unregister_read_group */
1468 int plugin_unregister_write (const char *name)
1470 return (plugin_unregister (list_write, name));
1473 int plugin_unregister_flush (const char *name)
1475 return (plugin_unregister (list_flush, name));
1478 int plugin_unregister_missing (const char *name)
1480 return (plugin_unregister (list_missing, name));
1483 int plugin_unregister_shutdown (const char *name)
1485 return (plugin_unregister (list_shutdown, name));
1488 int plugin_unregister_data_set (const char *name)
1492 if (data_sets == NULL)
1495 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1502 } /* int plugin_unregister_data_set */
1504 int plugin_unregister_log (const char *name)
1506 return (plugin_unregister (list_log, name));
1509 int plugin_unregister_notification (const char *name)
1511 return (plugin_unregister (list_notification, name));
1514 void plugin_init_all (void)
1516 char const *chain_name;
1517 long write_threads_num;
1521 /* Init the value cache */
1524 if (IS_TRUE (global_option_get ("CollectInternalStats")))
1525 record_statistics = 1;
1527 chain_name = global_option_get ("PreCacheChain");
1528 pre_cache_chain = fc_chain_get_by_name (chain_name);
1530 chain_name = global_option_get ("PostCacheChain");
1531 post_cache_chain = fc_chain_get_by_name (chain_name);
1533 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1535 if (write_limit_high < 0)
1537 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1538 write_limit_high = 0;
1541 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1542 /* default = */ write_limit_high / 2);
1543 if (write_limit_low < 0)
1545 ERROR ("WriteQueueLimitLow must be positive or zero.");
1546 write_limit_low = write_limit_high / 2;
1548 else if (write_limit_low > write_limit_high)
1550 ERROR ("WriteQueueLimitLow must not be larger than "
1551 "WriteQueueLimitHigh.");
1552 write_limit_low = write_limit_high;
1555 write_threads_num = global_option_get_long ("WriteThreads",
1557 if (write_threads_num < 1)
1559 ERROR ("WriteThreads must be positive.");
1560 write_threads_num = 5;
1563 start_write_threads ((size_t) write_threads_num);
1565 if ((list_init == NULL) && (read_heap == NULL))
1568 /* Calling all init callbacks before checking if read callbacks
1569 * are available allows the init callbacks to register the read
1571 le = llist_head (list_init);
1574 callback_func_t *cf;
1575 plugin_init_cb callback;
1576 plugin_ctx_t old_ctx;
1579 old_ctx = plugin_set_ctx (cf->cf_ctx);
1580 callback = cf->cf_callback;
1581 status = (*callback) ();
1582 plugin_set_ctx (old_ctx);
1586 ERROR ("Initialization of plugin `%s' "
1587 "failed with status %i. "
1588 "Plugin will be unloaded.",
1590 /* Plugins that register read callbacks from the init
1591 * callback should take care of appropriate error
1592 * handling themselves. */
1593 /* FIXME: Unload _all_ functions */
1594 plugin_unregister_read (le->key);
1600 max_read_interval = global_option_get_time ("MaxReadInterval",
1601 DEFAULT_MAX_READ_INTERVAL);
1603 /* Start read-threads */
1604 if (read_heap != NULL)
1609 rt = global_option_get ("ReadThreads");
1612 start_read_threads ((num > 0) ? num : 5);
1614 } /* void plugin_init_all */
1616 /* TODO: Rename this function. */
1617 void plugin_read_all (void)
1619 if(record_statistics) {
1620 plugin_update_internal_statistics ();
1622 uc_check_timeout ();
1625 } /* void plugin_read_all */
1627 /* Read function called when the `-T' command line argument is given. */
1628 int plugin_read_all_once (void)
1631 int return_status = 0;
1633 if (read_heap == NULL)
1635 NOTICE ("No read-functions are registered.");
1642 plugin_ctx_t old_ctx;
1644 rf = c_heap_get_root (read_heap);
1648 old_ctx = plugin_set_ctx (rf->rf_ctx);
1650 if (rf->rf_type == RF_SIMPLE)
1652 int (*callback) (void);
1654 callback = rf->rf_callback;
1655 status = (*callback) ();
1659 plugin_read_cb callback;
1661 callback = rf->rf_callback;
1662 status = (*callback) (&rf->rf_udata);
1665 plugin_set_ctx (old_ctx);
1669 NOTICE ("read-function of plugin `%s' failed.",
1674 destroy_callback ((void *) rf);
1677 return (return_status);
1678 } /* int plugin_read_all_once */
1680 int plugin_write (const char *plugin, /* {{{ */
1681 const data_set_t *ds, const value_list_t *vl)
1689 if (list_write == NULL)
1694 ds = plugin_get_ds (vl->type);
1697 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1707 le = llist_head (list_write);
1710 callback_func_t *cf = le->value;
1711 plugin_write_cb callback;
1713 /* do not switch plugin context; rather keep the context (interval)
1714 * information of the calling read plugin */
1716 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1717 callback = cf->cf_callback;
1718 status = (*callback) (ds, vl, &cf->cf_udata);
1727 if ((success == 0) && (failure != 0))
1732 else /* plugin != NULL */
1734 callback_func_t *cf;
1735 plugin_write_cb callback;
1737 le = llist_head (list_write);
1740 if (strcasecmp (plugin, le->key) == 0)
1751 /* do not switch plugin context; rather keep the context (interval)
1752 * information of the calling read plugin */
1754 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1755 callback = cf->cf_callback;
1756 status = (*callback) (ds, vl, &cf->cf_udata);
1760 } /* }}} int plugin_write */
1762 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1766 if (list_flush == NULL)
1769 le = llist_head (list_flush);
1772 callback_func_t *cf;
1773 plugin_flush_cb callback;
1774 plugin_ctx_t old_ctx;
1776 if ((plugin != NULL)
1777 && (strcmp (plugin, le->key) != 0))
1784 old_ctx = plugin_set_ctx (cf->cf_ctx);
1785 callback = cf->cf_callback;
1787 (*callback) (timeout, identifier, &cf->cf_udata);
1789 plugin_set_ctx (old_ctx);
1794 } /* int plugin_flush */
1796 void plugin_shutdown_all (void)
1800 stop_read_threads ();
1802 destroy_all_callbacks (&list_init);
1804 pthread_mutex_lock (&read_lock);
1805 llist_destroy (read_list);
1807 pthread_mutex_unlock (&read_lock);
1809 destroy_read_heap ();
1811 plugin_flush (/* plugin = */ NULL,
1813 /* identifier = */ NULL);
1816 if (list_shutdown != NULL)
1817 le = llist_head (list_shutdown);
1821 callback_func_t *cf;
1822 plugin_shutdown_cb callback;
1823 plugin_ctx_t old_ctx;
1826 old_ctx = plugin_set_ctx (cf->cf_ctx);
1827 callback = cf->cf_callback;
1829 /* Advance the pointer before calling the callback allows
1830 * shutdown functions to unregister themselves. If done the
1831 * other way around the memory `le' points to will be freed
1832 * after callback returns. */
1837 plugin_set_ctx (old_ctx);
1840 stop_write_threads ();
1842 /* Write plugins which use the `user_data' pointer usually need the
1843 * same data available to the flush callback. If this is the case, set
1844 * the free_function to NULL when registering the flush callback and to
1845 * the real free function when registering the write callback. This way
1846 * the data isn't freed twice. */
1847 destroy_all_callbacks (&list_flush);
1848 destroy_all_callbacks (&list_missing);
1849 destroy_all_callbacks (&list_write);
1851 destroy_all_callbacks (&list_notification);
1852 destroy_all_callbacks (&list_shutdown);
1853 destroy_all_callbacks (&list_log);
1855 plugin_free_loaded ();
1856 plugin_free_data_sets ();
1857 } /* void plugin_shutdown_all */
1859 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1863 if (list_missing == NULL)
1866 le = llist_head (list_missing);
1869 callback_func_t *cf;
1870 plugin_missing_cb callback;
1871 plugin_ctx_t old_ctx;
1875 old_ctx = plugin_set_ctx (cf->cf_ctx);
1876 callback = cf->cf_callback;
1878 status = (*callback) (vl, &cf->cf_udata);
1879 plugin_set_ctx (old_ctx);
1884 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1885 "failed with status %i.",
1898 } /* int }}} plugin_dispatch_missing */
1900 static int plugin_dispatch_values_internal (value_list_t *vl)
1903 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1905 value_t *saved_values;
1906 int saved_values_len;
1910 int free_meta_data = 0;
1912 if ((vl == NULL) || (vl->type[0] == 0)
1913 || (vl->values == NULL) || (vl->values_len < 1))
1915 ERROR ("plugin_dispatch_values: Invalid value list "
1916 "from plugin %s.", vl->plugin);
1920 /* Free meta data only if the calling function didn't specify any. In
1921 * this case matches and targets may add some and the calling function
1922 * may not expect (and therefore free) that data. */
1923 if (vl->meta == NULL)
1926 if (list_write == NULL)
1927 c_complain_once (LOG_WARNING, &no_write_complaint,
1928 "plugin_dispatch_values: No write callback has been "
1929 "registered. Please load at least one output plugin, "
1930 "if you want the collected data to be stored.");
1932 if (data_sets == NULL)
1934 ERROR ("plugin_dispatch_values: No data sets registered. "
1935 "Could the types database be read? Check "
1936 "your `TypesDB' setting!");
1940 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1942 char ident[6 * DATA_MAX_NAME_LEN];
1944 FORMAT_VL (ident, sizeof (ident), vl);
1945 INFO ("plugin_dispatch_values: Dataset not found: %s "
1946 "(from \"%s\"), check your types.db!",
1951 /* Assured by plugin_value_list_clone(). The time is determined at
1952 * _enqueue_ time. */
1953 assert (vl->time != 0);
1954 assert (vl->interval != 0);
1956 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1958 "plugin = %s; plugin_instance = %s; "
1959 "type = %s; type_instance = %s;",
1960 CDTIME_T_TO_DOUBLE (vl->time),
1961 CDTIME_T_TO_DOUBLE (vl->interval),
1963 vl->plugin, vl->plugin_instance,
1964 vl->type, vl->type_instance);
1967 assert (0 == strcmp (ds->type, vl->type));
1969 if (0 != strcmp (ds->type, vl->type))
1970 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1971 ds->type, vl->type);
1975 assert (ds->ds_num == vl->values_len);
1977 if (ds->ds_num != vl->values_len)
1979 ERROR ("plugin_dispatch_values: ds->type = %s: "
1980 "(ds->ds_num = %i) != "
1981 "(vl->values_len = %i)",
1982 ds->type, ds->ds_num, vl->values_len);
1987 escape_slashes (vl->host, sizeof (vl->host));
1988 escape_slashes (vl->plugin, sizeof (vl->plugin));
1989 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1990 escape_slashes (vl->type, sizeof (vl->type));
1991 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1993 /* Copy the values. This way, we can assure `targets' that they get
1994 * dynamically allocated values, which they can free and replace if
1996 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1998 saved_values = vl->values;
1999 saved_values_len = vl->values_len;
2001 vl->values = (value_t *) calloc (vl->values_len,
2002 sizeof (*vl->values));
2003 if (vl->values == NULL)
2005 ERROR ("plugin_dispatch_values: calloc failed.");
2006 vl->values = saved_values;
2009 memcpy (vl->values, saved_values,
2010 vl->values_len * sizeof (*vl->values));
2012 else /* if ((pre == NULL) && (post == NULL)) */
2014 saved_values = NULL;
2015 saved_values_len = 0;
2018 if (pre_cache_chain != NULL)
2020 status = fc_process_chain (ds, vl, pre_cache_chain);
2023 WARNING ("plugin_dispatch_values: Running the "
2024 "pre-cache chain failed with "
2028 else if (status == FC_TARGET_STOP)
2030 /* Restore the state of the value_list so that plugins
2031 * don't get confused.. */
2032 if (saved_values != NULL)
2035 vl->values = saved_values;
2036 vl->values_len = saved_values_len;
2042 /* Update the value cache */
2045 if (post_cache_chain != NULL)
2047 status = fc_process_chain (ds, vl, post_cache_chain);
2050 WARNING ("plugin_dispatch_values: Running the "
2051 "post-cache chain failed with "
2057 fc_default_action (ds, vl);
2059 /* Restore the state of the value_list so that plugins don't get
2061 if (saved_values != NULL)
2064 vl->values = saved_values;
2065 vl->values_len = saved_values_len;
2068 if ((free_meta_data != 0) && (vl->meta != NULL))
2070 meta_data_destroy (vl->meta);
2075 } /* int plugin_dispatch_values_internal */
2077 static double get_drop_probability (void) /* {{{ */
2083 pthread_mutex_lock (&write_lock);
2084 wql = write_queue_length;
2085 pthread_mutex_unlock (&write_lock);
2087 if (wql < write_limit_low)
2089 if (wql >= write_limit_high)
2092 pos = 1 + wql - write_limit_low;
2093 size = 1 + write_limit_high - write_limit_low;
2095 return (((double) pos) / ((double) size));
2096 } /* }}} double get_drop_probability */
2098 static _Bool check_drop_value (void) /* {{{ */
2100 static cdtime_t last_message_time = 0;
2101 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2107 if (write_limit_high == 0)
2110 p = get_drop_probability ();
2114 status = pthread_mutex_trylock (&last_message_lock);
2120 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2122 last_message_time = now;
2123 ERROR ("plugin_dispatch_values: Low water mark "
2124 "reached. Dropping %.0f%% of metrics.",
2127 pthread_mutex_unlock (&last_message_lock);
2138 } /* }}} _Bool check_drop_value */
2140 int plugin_dispatch_values (value_list_t const *vl)
2143 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2145 if (check_drop_value ()) {
2146 if(record_statistics) {
2147 pthread_mutex_lock(&statistics_lock);
2148 stats_values_dropped++;
2149 pthread_mutex_unlock(&statistics_lock);
2154 status = plugin_write_enqueue (vl);
2158 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2159 "with status %i (%s).", status,
2160 sstrerror (status, errbuf, sizeof (errbuf)));
2167 __attribute__((sentinel))
2168 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2169 _Bool store_percentage, ...)
2176 assert (template->values_len == 1);
2178 va_start (ap, store_percentage);
2184 name = va_arg (ap, char const *);
2188 value = va_arg (ap, gauge_t);
2194 vl = plugin_value_list_clone (template);
2195 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2196 if (store_percentage)
2197 sstrncpy (vl->type, "percent", sizeof (vl->type));
2199 va_start (ap, store_percentage);
2205 /* Set the type instance. */
2206 name = va_arg (ap, char const *);
2209 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2211 /* Set the value. */
2212 vl->values[0].gauge = va_arg (ap, gauge_t);
2213 if (store_percentage)
2214 vl->values[0].gauge *= 100.0 / sum;
2216 status = plugin_write_enqueue (vl);
2222 plugin_value_list_free (vl);
2224 } /* }}} int plugin_dispatch_multivalue */
2226 int plugin_dispatch_notification (const notification_t *notif)
2229 /* Possible TODO: Add flap detection here */
2231 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2232 "time = %.3f; host = %s;",
2233 notif->severity, notif->message,
2234 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2236 /* Nobody cares for notifications */
2237 if (list_notification == NULL)
2240 le = llist_head (list_notification);
2243 callback_func_t *cf;
2244 plugin_notification_cb callback;
2247 /* do not switch plugin context; rather keep the context
2248 * (interval) information of the calling plugin */
2251 callback = cf->cf_callback;
2252 status = (*callback) (notif, &cf->cf_udata);
2255 WARNING ("plugin_dispatch_notification: Notification "
2256 "callback %s returned %i.",
2264 } /* int plugin_dispatch_notification */
2266 void plugin_log (int level, const char *format, ...)
2273 if (level >= LOG_DEBUG)
2277 va_start (ap, format);
2278 vsnprintf (msg, sizeof (msg), format, ap);
2279 msg[sizeof (msg) - 1] = '\0';
2282 if (list_log == NULL)
2284 fprintf (stderr, "%s\n", msg);
2288 le = llist_head (list_log);
2291 callback_func_t *cf;
2292 plugin_log_cb callback;
2295 callback = cf->cf_callback;
2297 /* do not switch plugin context; rather keep the context
2298 * (interval) information of the calling plugin */
2300 (*callback) (level, msg, &cf->cf_udata);
2304 } /* void plugin_log */
2306 int parse_log_severity (const char *severity)
2310 if ((0 == strcasecmp (severity, "emerg"))
2311 || (0 == strcasecmp (severity, "alert"))
2312 || (0 == strcasecmp (severity, "crit"))
2313 || (0 == strcasecmp (severity, "err")))
2314 log_level = LOG_ERR;
2315 else if (0 == strcasecmp (severity, "warning"))
2316 log_level = LOG_WARNING;
2317 else if (0 == strcasecmp (severity, "notice"))
2318 log_level = LOG_NOTICE;
2319 else if (0 == strcasecmp (severity, "info"))
2320 log_level = LOG_INFO;
2322 else if (0 == strcasecmp (severity, "debug"))
2323 log_level = LOG_DEBUG;
2324 #endif /* COLLECT_DEBUG */
2327 } /* int parse_log_severity */
2329 int parse_notif_severity (const char *severity)
2331 int notif_severity = -1;
2333 if (strcasecmp (severity, "FAILURE") == 0)
2334 notif_severity = NOTIF_FAILURE;
2335 else if (strcmp (severity, "OKAY") == 0)
2336 notif_severity = NOTIF_OKAY;
2337 else if ((strcmp (severity, "WARNING") == 0)
2338 || (strcmp (severity, "WARN") == 0))
2339 notif_severity = NOTIF_WARNING;
2341 return (notif_severity);
2342 } /* int parse_notif_severity */
2344 const data_set_t *plugin_get_ds (const char *name)
2348 if (data_sets == NULL)
2350 ERROR ("plugin_get_ds: No data sets are defined yet.");
2354 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2356 DEBUG ("No such dataset registered: %s", name);
2361 } /* data_set_t *plugin_get_ds */
2363 static int plugin_notification_meta_add (notification_t *n,
2365 enum notification_meta_type_e type,
2368 notification_meta_t *meta;
2369 notification_meta_t *tail;
2371 if ((n == NULL) || (name == NULL) || (value == NULL))
2373 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2377 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2380 ERROR ("plugin_notification_meta_add: malloc failed.");
2383 memset (meta, 0, sizeof (notification_meta_t));
2385 sstrncpy (meta->name, name, sizeof (meta->name));
2390 case NM_TYPE_STRING:
2392 meta->nm_value.nm_string = strdup ((const char *) value);
2393 if (meta->nm_value.nm_string == NULL)
2395 ERROR ("plugin_notification_meta_add: strdup failed.");
2401 case NM_TYPE_SIGNED_INT:
2403 meta->nm_value.nm_signed_int = *((int64_t *) value);
2406 case NM_TYPE_UNSIGNED_INT:
2408 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2411 case NM_TYPE_DOUBLE:
2413 meta->nm_value.nm_double = *((double *) value);
2416 case NM_TYPE_BOOLEAN:
2418 meta->nm_value.nm_boolean = *((_Bool *) value);
2423 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2427 } /* switch (type) */
2431 while ((tail != NULL) && (tail->next != NULL))
2440 } /* int plugin_notification_meta_add */
2442 int plugin_notification_meta_add_string (notification_t *n,
2446 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2449 int plugin_notification_meta_add_signed_int (notification_t *n,
2453 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2456 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2460 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2463 int plugin_notification_meta_add_double (notification_t *n,
2467 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2470 int plugin_notification_meta_add_boolean (notification_t *n,
2474 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2477 int plugin_notification_meta_copy (notification_t *dst,
2478 const notification_t *src)
2480 notification_meta_t *meta;
2482 assert (dst != NULL);
2483 assert (src != NULL);
2484 assert (dst != src);
2485 assert ((src->meta == NULL) || (src->meta != dst->meta));
2487 for (meta = src->meta; meta != NULL; meta = meta->next)
2489 if (meta->type == NM_TYPE_STRING)
2490 plugin_notification_meta_add_string (dst, meta->name,
2491 meta->nm_value.nm_string);
2492 else if (meta->type == NM_TYPE_SIGNED_INT)
2493 plugin_notification_meta_add_signed_int (dst, meta->name,
2494 meta->nm_value.nm_signed_int);
2495 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2496 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2497 meta->nm_value.nm_unsigned_int);
2498 else if (meta->type == NM_TYPE_DOUBLE)
2499 plugin_notification_meta_add_double (dst, meta->name,
2500 meta->nm_value.nm_double);
2501 else if (meta->type == NM_TYPE_BOOLEAN)
2502 plugin_notification_meta_add_boolean (dst, meta->name,
2503 meta->nm_value.nm_boolean);
2507 } /* int plugin_notification_meta_copy */
2509 int plugin_notification_meta_free (notification_meta_t *n)
2511 notification_meta_t *this;
2512 notification_meta_t *next;
2516 ERROR ("plugin_notification_meta_free: n == NULL!");
2521 while (this != NULL)
2525 if (this->type == NM_TYPE_STRING)
2527 free ((char *)this->nm_value.nm_string);
2528 this->nm_value.nm_string = NULL;
2536 } /* int plugin_notification_meta_free */
2538 static void plugin_ctx_destructor (void *ctx)
2541 } /* void plugin_ctx_destructor */
2543 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2545 static plugin_ctx_t *plugin_ctx_create (void)
2549 ctx = malloc (sizeof (*ctx));
2552 ERROR ("Failed to allocate plugin context: %s",
2553 sstrerror (errno, errbuf, sizeof (errbuf)));
2558 assert (plugin_ctx_key_initialized);
2559 pthread_setspecific (plugin_ctx_key, ctx);
2560 DEBUG("Created new plugin context.");
2562 } /* int plugin_ctx_create */
2564 void plugin_init_ctx (void)
2566 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2567 plugin_ctx_key_initialized = 1;
2568 } /* void plugin_init_ctx */
2570 plugin_ctx_t plugin_get_ctx (void)
2574 assert (plugin_ctx_key_initialized);
2575 ctx = pthread_getspecific (plugin_ctx_key);
2578 ctx = plugin_ctx_create ();
2579 /* this must no happen -- exit() instead? */
2585 } /* plugin_ctx_t plugin_get_ctx */
2587 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2592 assert (plugin_ctx_key_initialized);
2593 c = pthread_getspecific (plugin_ctx_key);
2596 c = plugin_ctx_create ();
2597 /* this must no happen -- exit() instead? */
2606 } /* void plugin_set_ctx */
2608 cdtime_t plugin_get_interval (void)
2612 interval = plugin_get_ctx().interval;
2616 return cf_get_default_interval ();
2617 } /* cdtime_t plugin_get_interval */
2621 void *(*start_routine) (void *);
2625 static void *plugin_thread_start (void *arg)
2627 plugin_thread_t *plugin_thread = arg;
2629 void *(*start_routine) (void *) = plugin_thread->start_routine;
2630 void *plugin_arg = plugin_thread->arg;
2632 plugin_set_ctx (plugin_thread->ctx);
2634 free (plugin_thread);
2636 return start_routine (plugin_arg);
2637 } /* void *plugin_thread_start */
2639 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2640 void *(*start_routine) (void *), void *arg)
2642 plugin_thread_t *plugin_thread;
2644 plugin_thread = malloc (sizeof (*plugin_thread));
2645 if (plugin_thread == NULL)
2648 plugin_thread->ctx = plugin_get_ctx ();
2649 plugin_thread->start_routine = start_routine;
2650 plugin_thread->arg = arg;
2652 return pthread_create (thread, attr,
2653 plugin_thread_start, plugin_thread);
2654 } /* int plugin_thread_create */
2656 /* vim: set sw=8 ts=8 noet fdm=marker : */