2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2014 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian Harl <sh at tokkee.org>
31 #include "configfile.h"
32 #include "filter_chain.h"
33 #include "utils_avltree.h"
34 #include "utils_cache.h"
35 #include "utils_complain.h"
36 #include "utils_llist.h"
37 #include "utils_heap.h"
38 #include "utils_time.h"
39 #include "utils_random.h"
46 struct callback_func_s
52 typedef struct callback_func_s callback_func_t;
56 #define RF_REMOVE 65535
59 /* `read_func_t' "inherits" from `callback_func_t'.
60 * The `rf_super' member MUST be the first one in this structure! */
61 #define rf_callback rf_super.cf_callback
62 #define rf_udata rf_super.cf_udata
63 #define rf_ctx rf_super.cf_ctx
64 callback_func_t rf_super;
65 char rf_group[DATA_MAX_NAME_LEN];
69 cdtime_t rf_effective_interval;
70 cdtime_t rf_next_read;
72 typedef struct read_func_s read_func_t;
75 typedef struct write_queue_s write_queue_t;
83 struct flush_callback_s {
87 typedef struct flush_callback_s flush_callback_t;
92 static c_avl_tree_t *plugins_loaded = NULL;
94 static llist_t *list_init;
95 static llist_t *list_write;
96 static llist_t *list_flush;
97 static llist_t *list_missing;
98 static llist_t *list_shutdown;
99 static llist_t *list_log;
100 static llist_t *list_notification;
102 static fc_chain_t *pre_cache_chain = NULL;
103 static fc_chain_t *post_cache_chain = NULL;
105 static c_avl_tree_t *data_sets;
107 static char *plugindir = NULL;
109 #ifndef DEFAULT_MAX_READ_INTERVAL
110 # define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T (86400)
112 static c_heap_t *read_heap = NULL;
113 static llist_t *read_list;
114 static int read_loop = 1;
115 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
116 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
117 static pthread_t *read_threads = NULL;
118 static int read_threads_num = 0;
119 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
121 static write_queue_t *write_queue_head;
122 static write_queue_t *write_queue_tail;
123 static long write_queue_length = 0;
124 static _Bool write_loop = 1;
125 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
126 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
127 static pthread_t *write_threads = NULL;
128 static size_t write_threads_num = 0;
130 static pthread_key_t plugin_ctx_key;
131 static _Bool plugin_ctx_key_initialized = 0;
133 static long write_limit_high = 0;
134 static long write_limit_low = 0;
136 static derive_t stats_values_dropped = 0;
137 static _Bool record_statistics = 0;
142 static int plugin_dispatch_values_internal (value_list_t *vl);
144 static const char *plugin_get_dir (void)
146 if (plugindir == NULL)
152 static void plugin_update_internal_statistics (void) { /* {{{ */
153 derive_t copy_write_queue_length;
154 value_list_t vl = VALUE_LIST_INIT;
157 copy_write_queue_length = write_queue_length;
159 /* Initialize `vl' */
163 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
164 sstrncpy (vl.plugin, "collectd", sizeof (vl.plugin));
166 vl.type_instance[0] = 0;
170 sstrncpy (vl.plugin_instance, "write_queue",
171 sizeof (vl.plugin_instance));
173 /* Write queue : queue length */
174 vl.values[0].gauge = (gauge_t) copy_write_queue_length;
175 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
176 vl.type_instance[0] = 0;
177 plugin_dispatch_values (&vl);
179 /* Write queue : Values dropped (queue length > low limit) */
180 vl.values[0].derive = (derive_t) stats_values_dropped;
181 sstrncpy (vl.type, "derive", sizeof (vl.type));
182 sstrncpy (vl.type_instance, "dropped", sizeof (vl.type_instance));
183 plugin_dispatch_values (&vl);
186 sstrncpy (vl.plugin_instance, "cache",
187 sizeof (vl.plugin_instance));
189 /* Cache : Nb entry in cache tree */
190 vl.values[0].gauge = (gauge_t) uc_get_size();
191 sstrncpy (vl.type, "cache_size", sizeof (vl.type));
192 vl.type_instance[0] = 0;
193 plugin_dispatch_values (&vl);
196 } /* }}} void plugin_update_internal_statistics */
198 static void destroy_callback (callback_func_t *cf) /* {{{ */
203 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
205 cf->cf_udata.free_func (cf->cf_udata.data);
206 cf->cf_udata.data = NULL;
207 cf->cf_udata.free_func = NULL;
210 } /* }}} void destroy_callback */
212 static void destroy_all_callbacks (llist_t **list) /* {{{ */
219 le = llist_head (*list);
227 destroy_callback (le->value);
233 llist_destroy (*list);
235 } /* }}} void destroy_all_callbacks */
237 static void destroy_read_heap (void) /* {{{ */
239 if (read_heap == NULL)
246 rf = c_heap_get_root (read_heap);
250 destroy_callback ((callback_func_t *) rf);
253 c_heap_destroy (read_heap);
255 } /* }}} void destroy_read_heap */
257 static int register_callback (llist_t **list, /* {{{ */
258 const char *name, callback_func_t *cf)
265 *list = llist_create ();
268 ERROR ("plugin: register_callback: "
269 "llist_create failed.");
270 destroy_callback (cf);
278 ERROR ("plugin: register_callback: strdup failed.");
279 destroy_callback (cf);
283 le = llist_search (*list, name);
286 le = llentry_create (key, cf);
289 ERROR ("plugin: register_callback: "
290 "llentry_create failed.");
292 destroy_callback (cf);
296 llist_append (*list, le);
300 callback_func_t *old_cf;
305 WARNING ("plugin: register_callback: "
306 "a callback named `%s' already exists - "
307 "overwriting the old entry!", name);
309 destroy_callback (old_cf);
314 } /* }}} int register_callback */
316 static void log_list_callbacks (llist_t **list, /* {{{ */
326 n = llist_size(*list);
329 INFO("%s [none]", comment);
333 keys = calloc(n, sizeof(char*));
337 ERROR("%s: failed to allocate memory for list of callbacks",
343 for (le = llist_head (*list), i = 0, len = 0;
348 len += strlen(le->key) + 6;
350 str = malloc(len + 10);
353 ERROR("%s: failed to allocate memory for list of callbacks",
359 strjoin(str, len, keys, n, "', '");
360 INFO("%s ['%s']", comment, str);
364 } /* }}} void log_list_callbacks */
366 static int create_register_callback (llist_t **list, /* {{{ */
367 const char *name, void *callback, user_data_t *ud)
371 cf = (callback_func_t *) malloc (sizeof (*cf));
374 ERROR ("plugin: create_register_callback: malloc failed.");
377 memset (cf, 0, sizeof (*cf));
379 cf->cf_callback = callback;
382 cf->cf_udata.data = NULL;
383 cf->cf_udata.free_func = NULL;
390 cf->cf_ctx = plugin_get_ctx ();
392 return (register_callback (list, name, cf));
393 } /* }}} int create_register_callback */
395 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
402 e = llist_search (list, name);
406 llist_remove (list, e);
409 destroy_callback (e->value);
414 } /* }}} int plugin_unregister */
417 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
418 * object, but it will bitch about a shared object not having a
419 * ``module_register'' symbol..
421 static int plugin_load_file (char *file, uint32_t flags)
424 void (*reg_handle) (void);
427 lt_dlerror (); /* clear errors */
429 #if LIBTOOL_VERSION == 2
430 if (flags & PLUGIN_FLAGS_GLOBAL) {
432 lt_dladvise_init(&advise);
433 lt_dladvise_global(&advise);
434 dlh = lt_dlopenadvise(file, advise);
435 lt_dladvise_destroy(&advise);
437 dlh = lt_dlopen (file);
439 #else /* if LIBTOOL_VERSION == 1 */
440 if (flags & PLUGIN_FLAGS_GLOBAL)
441 WARNING ("plugin_load_file: The global flag is not supported, "
442 "libtool 2 is required for this.");
443 dlh = lt_dlopen (file);
448 char errbuf[1024] = "";
450 ssnprintf (errbuf, sizeof (errbuf),
451 "lt_dlopen (\"%s\") failed: %s. "
452 "The most common cause for this problem is "
453 "missing dependencies. Use ldd(1) to check "
454 "the dependencies of the plugin "
456 file, lt_dlerror ());
458 ERROR ("%s", errbuf);
459 /* Make sure this is printed to STDERR in any case, but also
460 * make sure it's printed only once. */
461 if (list_log != NULL)
462 fprintf (stderr, "ERROR: %s\n", errbuf);
467 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
469 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
470 file, lt_dlerror ());
480 static void *plugin_read_thread (void __attribute__((unused)) *args)
482 while (read_loop != 0)
485 plugin_ctx_t old_ctx;
491 /* Get the read function that needs to be read next.
492 * We don't need to hold "read_lock" for the heap, but we need
493 * to call c_heap_get_root() and pthread_cond_wait() in the
494 * same protected block. */
495 pthread_mutex_lock (&read_lock);
496 rf = c_heap_get_root (read_heap);
499 pthread_cond_wait (&read_cond, &read_lock);
500 pthread_mutex_unlock (&read_lock);
503 pthread_mutex_unlock (&read_lock);
505 if (rf->rf_interval == 0)
507 /* this should not happen, because the interval is set
508 * for each plugin when loading it
509 * XXX: issue a warning? */
510 rf->rf_interval = plugin_get_interval ();
511 rf->rf_effective_interval = rf->rf_interval;
513 rf->rf_next_read = cdtime ();
516 /* sleep until this entry is due,
517 * using pthread_cond_timedwait */
518 pthread_mutex_lock (&read_lock);
519 /* In pthread_cond_timedwait, spurious wakeups are possible
520 * (and really happen, at least on NetBSD with > 1 CPU), thus
521 * we need to re-evaluate the condition every time
522 * pthread_cond_timedwait returns. */
524 while ((read_loop != 0)
525 && (cdtime () < rf->rf_next_read)
528 struct timespec ts = { 0 };
530 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
532 rc = pthread_cond_timedwait (&read_cond, &read_lock,
536 /* Must hold `read_lock' when accessing `rf->rf_type'. */
537 rf_type = rf->rf_type;
538 pthread_mutex_unlock (&read_lock);
540 /* Check if we're supposed to stop.. This may have interrupted
544 /* Insert `rf' again, so it can be free'd correctly */
545 c_heap_insert (read_heap, rf);
549 /* The entry has been marked for deletion. The linked list
550 * entry has already been removed by `plugin_unregister_read'.
551 * All we have to do here is free the `read_func_t' and
553 if (rf_type == RF_REMOVE)
555 DEBUG ("plugin_read_thread: Destroying the `%s' "
556 "callback.", rf->rf_name);
558 destroy_callback ((callback_func_t *) rf);
563 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
565 old_ctx = plugin_set_ctx (rf->rf_ctx);
567 if (rf_type == RF_SIMPLE)
569 int (*callback) (void);
571 callback = rf->rf_callback;
572 status = (*callback) ();
576 plugin_read_cb callback;
578 assert (rf_type == RF_COMPLEX);
580 callback = rf->rf_callback;
581 status = (*callback) (&rf->rf_udata);
584 plugin_set_ctx (old_ctx);
586 /* If the function signals failure, we will increase the
587 * intervals in which it will be called. */
590 rf->rf_effective_interval *= 2;
591 if (rf->rf_effective_interval > max_read_interval)
592 rf->rf_effective_interval = max_read_interval;
594 NOTICE ("read-function of plugin `%s' failed. "
595 "Will suspend it for %.3f seconds.",
597 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
601 /* Success: Restore the interval, if it was changed. */
602 rf->rf_effective_interval = rf->rf_interval;
605 /* update the ``next read due'' field */
608 DEBUG ("plugin_read_thread: Effective interval of the "
609 "%s plugin is %.3f seconds.",
611 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
613 /* Calculate the next (absolute) time at which this function
614 * should be called. */
615 rf->rf_next_read += rf->rf_effective_interval;
617 /* Check, if `rf_next_read' is in the past. */
618 if (rf->rf_next_read < now)
620 /* `rf_next_read' is in the past. Insert `now'
621 * so this value doesn't trail off into the
623 rf->rf_next_read = now;
626 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
628 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
630 /* Re-insert this read function into the heap again. */
631 c_heap_insert (read_heap, rf);
632 } /* while (read_loop) */
636 } /* void *plugin_read_thread */
638 static void start_read_threads (int num)
642 if (read_threads != NULL)
645 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
646 if (read_threads == NULL)
648 ERROR ("plugin: start_read_threads: calloc failed.");
652 read_threads_num = 0;
653 for (i = 0; i < num; i++)
655 if (pthread_create (read_threads + read_threads_num, NULL,
656 plugin_read_thread, NULL) == 0)
662 ERROR ("plugin: start_read_threads: pthread_create failed.");
666 } /* void start_read_threads */
668 static void stop_read_threads (void)
672 if (read_threads == NULL)
675 INFO ("collectd: Stopping %i read threads.", read_threads_num);
677 pthread_mutex_lock (&read_lock);
679 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
680 pthread_cond_broadcast (&read_cond);
681 pthread_mutex_unlock (&read_lock);
683 for (i = 0; i < read_threads_num; i++)
685 if (pthread_join (read_threads[i], NULL) != 0)
687 ERROR ("plugin: stop_read_threads: pthread_join failed.");
689 read_threads[i] = (pthread_t) 0;
691 sfree (read_threads);
692 read_threads_num = 0;
693 } /* void stop_read_threads */
695 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
700 meta_data_destroy (vl->meta);
703 } /* }}} void plugin_value_list_free */
705 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
712 vl = malloc (sizeof (*vl));
715 memcpy (vl, vl_orig, sizeof (*vl));
717 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
718 if (vl->values == NULL)
720 plugin_value_list_free (vl);
723 memcpy (vl->values, vl_orig->values,
724 vl_orig->values_len * sizeof (*vl->values));
726 vl->meta = meta_data_clone (vl->meta);
727 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
729 plugin_value_list_free (vl);
734 vl->time = cdtime ();
736 /* Fill in the interval from the thread context, if it is zero. */
737 if (vl->interval == 0)
739 plugin_ctx_t ctx = plugin_get_ctx ();
741 if (ctx.interval != 0)
742 vl->interval = ctx.interval;
745 char name[6 * DATA_MAX_NAME_LEN];
746 FORMAT_VL (name, sizeof (name), vl);
747 ERROR ("plugin_value_list_clone: Unable to determine "
748 "interval from context for "
749 "value list \"%s\". "
750 "This indicates a broken plugin. "
751 "Please report this problem to the "
752 "collectd mailing list or at "
753 "<http://collectd.org/bugs/>.", name);
754 vl->interval = cf_get_default_interval ();
759 } /* }}} value_list_t *plugin_value_list_clone */
761 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
765 q = malloc (sizeof (*q));
770 q->vl = plugin_value_list_clone (vl);
777 /* Store context of caller (read plugin); otherwise, it would not be
778 * available to the write plugins when actually dispatching the
779 * value-list later on. */
780 q->ctx = plugin_get_ctx ();
782 pthread_mutex_lock (&write_lock);
784 if (write_queue_tail == NULL)
786 write_queue_head = q;
787 write_queue_tail = q;
788 write_queue_length = 1;
792 write_queue_tail->next = q;
793 write_queue_tail = q;
794 write_queue_length += 1;
797 pthread_cond_signal (&write_cond);
798 pthread_mutex_unlock (&write_lock);
801 } /* }}} int plugin_write_enqueue */
803 static value_list_t *plugin_write_dequeue (void) /* {{{ */
808 pthread_mutex_lock (&write_lock);
810 while (write_loop && (write_queue_head == NULL))
811 pthread_cond_wait (&write_cond, &write_lock);
813 if (write_queue_head == NULL)
815 pthread_mutex_unlock (&write_lock);
819 q = write_queue_head;
820 write_queue_head = q->next;
821 write_queue_length -= 1;
822 if (write_queue_head == NULL) {
823 write_queue_tail = NULL;
824 assert(0 == write_queue_length);
827 pthread_mutex_unlock (&write_lock);
829 (void) plugin_set_ctx (q->ctx);
834 } /* }}} value_list_t *plugin_write_dequeue */
836 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
840 value_list_t *vl = plugin_write_dequeue ();
844 plugin_dispatch_values_internal (vl);
846 plugin_value_list_free (vl);
851 } /* }}} void *plugin_write_thread */
853 static void start_write_threads (size_t num) /* {{{ */
857 if (write_threads != NULL)
860 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
861 if (write_threads == NULL)
863 ERROR ("plugin: start_write_threads: calloc failed.");
867 write_threads_num = 0;
868 for (i = 0; i < num; i++)
872 status = pthread_create (write_threads + write_threads_num,
879 ERROR ("plugin: start_write_threads: pthread_create failed "
880 "with status %i (%s).", status,
881 sstrerror (status, errbuf, sizeof (errbuf)));
887 } /* }}} void start_write_threads */
889 static void stop_write_threads (void) /* {{{ */
894 if (write_threads == NULL)
897 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
899 pthread_mutex_lock (&write_lock);
901 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
902 pthread_cond_broadcast (&write_cond);
903 pthread_mutex_unlock (&write_lock);
905 for (i = 0; i < write_threads_num; i++)
907 if (pthread_join (write_threads[i], NULL) != 0)
909 ERROR ("plugin: stop_write_threads: pthread_join failed.");
911 write_threads[i] = (pthread_t) 0;
913 sfree (write_threads);
914 write_threads_num = 0;
916 pthread_mutex_lock (&write_lock);
918 for (q = write_queue_head; q != NULL; )
920 write_queue_t *q1 = q;
921 plugin_value_list_free (q->vl);
926 write_queue_head = NULL;
927 write_queue_tail = NULL;
928 write_queue_length = 0;
929 pthread_mutex_unlock (&write_lock);
933 WARNING ("plugin: %zu value list%s left after shutting down "
934 "the write threads.",
935 i, (i == 1) ? " was" : "s were");
937 } /* }}} void stop_write_threads */
942 void plugin_set_dir (const char *dir)
944 if (plugindir != NULL)
949 else if ((plugindir = strdup (dir)) == NULL)
952 ERROR ("strdup failed: %s",
953 sstrerror (errno, errbuf, sizeof (errbuf)));
957 static _Bool plugin_is_loaded (char const *name)
961 if (plugins_loaded == NULL)
962 plugins_loaded = c_avl_create ((void *) strcasecmp);
963 assert (plugins_loaded != NULL);
965 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
966 return (status == 0);
969 static int plugin_mark_loaded (char const *name)
974 name_copy = strdup (name);
975 if (name_copy == NULL)
978 status = c_avl_insert (plugins_loaded,
979 /* key = */ name_copy, /* value = */ NULL);
983 static void plugin_free_loaded ()
988 if (plugins_loaded == NULL)
991 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
994 assert (value == NULL);
997 c_avl_destroy (plugins_loaded);
998 plugins_loaded = NULL;
1002 int plugin_load (char const *plugin_name, uint32_t flags)
1006 char filename[BUFSIZE] = "";
1007 char typename[BUFSIZE];
1009 struct stat statbuf;
1013 if (plugin_name == NULL)
1016 /* Check if plugin is already loaded and don't do anything in this
1018 if (plugin_is_loaded (plugin_name))
1021 dir = plugin_get_dir ();
1025 * XXX: Magic at work:
1027 * Some of the language bindings, for example the Python and Perl
1028 * plugins, need to be able to export symbols to the scripts they run.
1029 * For this to happen, the "Globals" flag needs to be set.
1030 * Unfortunately, this technical detail is hard to explain to the
1031 * average user and she shouldn't have to worry about this, ideally.
1032 * So in order to save everyone's sanity use a different default for a
1033 * handful of special plugins. --octo
1035 if ((strcasecmp ("perl", plugin_name) == 0)
1036 || (strcasecmp ("python", plugin_name) == 0))
1037 flags |= PLUGIN_FLAGS_GLOBAL;
1039 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
1040 * type when matching the filename */
1041 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
1042 if ((status < 0) || ((size_t) status >= sizeof (typename)))
1044 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
1048 if ((dh = opendir (dir)) == NULL)
1051 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
1052 sstrerror (errno, errbuf, sizeof (errbuf)));
1056 while ((de = readdir (dh)) != NULL)
1058 if (strcasecmp (de->d_name, typename))
1061 status = ssnprintf (filename, sizeof (filename),
1062 "%s/%s", dir, de->d_name);
1063 if ((status < 0) || ((size_t) status >= sizeof (filename)))
1065 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
1070 if (lstat (filename, &statbuf) == -1)
1073 WARNING ("plugin_load: stat (\"%s\") failed: %s",
1075 sstrerror (errno, errbuf, sizeof (errbuf)));
1078 else if (!S_ISREG (statbuf.st_mode))
1080 /* don't follow symlinks */
1081 WARNING ("plugin_load: %s is not a regular file.",
1086 status = plugin_load_file (filename, flags);
1090 plugin_mark_loaded (plugin_name);
1092 INFO ("plugin_load: plugin \"%s\" successfully loaded.", plugin_name);
1097 ERROR ("plugin_load: Load plugin \"%s\" failed with "
1098 "status %i.", plugin_name, status);
1104 if (filename[0] == 0)
1105 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1112 * The `register_*' functions follow
1114 int plugin_register_config (const char *name,
1115 int (*callback) (const char *key, const char *val),
1116 const char **keys, int keys_num)
1118 cf_register (name, callback, keys, keys_num);
1120 } /* int plugin_register_config */
1122 int plugin_register_complex_config (const char *type,
1123 int (*callback) (oconfig_item_t *))
1125 return (cf_register_complex (type, callback));
1126 } /* int plugin_register_complex_config */
1128 int plugin_register_init (const char *name,
1129 int (*callback) (void))
1131 return (create_register_callback (&list_init, name, (void *) callback,
1132 /* user_data = */ NULL));
1133 } /* plugin_register_init */
1135 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1137 const read_func_t *rf0;
1138 const read_func_t *rf1;
1143 if (rf0->rf_next_read < rf1->rf_next_read)
1145 else if (rf0->rf_next_read > rf1->rf_next_read)
1149 } /* int plugin_compare_read_func */
1151 /* Add a read function to both, the heap and a linked list. The linked list if
1152 * used to look-up read functions, especially for the remove function. The heap
1153 * is used to determine which plugin to read next. */
1154 static int plugin_insert_read (read_func_t *rf)
1159 rf->rf_next_read = cdtime ();
1160 rf->rf_effective_interval = rf->rf_interval;
1162 pthread_mutex_lock (&read_lock);
1164 if (read_list == NULL)
1166 read_list = llist_create ();
1167 if (read_list == NULL)
1169 pthread_mutex_unlock (&read_lock);
1170 ERROR ("plugin_insert_read: read_list failed.");
1175 if (read_heap == NULL)
1177 read_heap = c_heap_create (plugin_compare_read_func);
1178 if (read_heap == NULL)
1180 pthread_mutex_unlock (&read_lock);
1181 ERROR ("plugin_insert_read: c_heap_create failed.");
1186 le = llist_search (read_list, rf->rf_name);
1189 pthread_mutex_unlock (&read_lock);
1190 WARNING ("The read function \"%s\" is already registered. "
1191 "Check for duplicate \"LoadPlugin\" lines "
1192 "in your configuration!",
1197 le = llentry_create (rf->rf_name, rf);
1200 pthread_mutex_unlock (&read_lock);
1201 ERROR ("plugin_insert_read: llentry_create failed.");
1205 status = c_heap_insert (read_heap, rf);
1208 pthread_mutex_unlock (&read_lock);
1209 ERROR ("plugin_insert_read: c_heap_insert failed.");
1210 llentry_destroy (le);
1214 /* This does not fail. */
1215 llist_append (read_list, le);
1217 /* Wake up all the read threads. */
1218 pthread_cond_broadcast (&read_cond);
1219 pthread_mutex_unlock (&read_lock);
1221 } /* int plugin_insert_read */
1223 int plugin_register_read (const char *name,
1224 int (*callback) (void))
1229 rf = malloc (sizeof (*rf));
1232 ERROR ("plugin_register_read: malloc failed.");
1236 memset (rf, 0, sizeof (read_func_t));
1237 rf->rf_callback = (void *) callback;
1238 rf->rf_udata.data = NULL;
1239 rf->rf_udata.free_func = NULL;
1240 rf->rf_ctx = plugin_get_ctx ();
1241 rf->rf_group[0] = '\0';
1242 rf->rf_name = strdup (name);
1243 rf->rf_type = RF_SIMPLE;
1244 rf->rf_interval = plugin_get_interval ();
1246 status = plugin_insert_read (rf);
1248 sfree (rf->rf_name);
1253 } /* int plugin_register_read */
1255 int plugin_register_complex_read (const char *group, const char *name,
1256 plugin_read_cb callback,
1258 user_data_t *user_data)
1263 rf = malloc (sizeof (*rf));
1266 ERROR ("plugin_register_complex_read: malloc failed.");
1270 memset (rf, 0, sizeof (read_func_t));
1271 rf->rf_callback = (void *) callback;
1273 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1275 rf->rf_group[0] = '\0';
1276 rf->rf_name = strdup (name);
1277 rf->rf_type = RF_COMPLEX;
1278 rf->rf_interval = (interval != 0) ? interval : plugin_get_interval ();
1281 if (user_data == NULL)
1283 rf->rf_udata.data = NULL;
1284 rf->rf_udata.free_func = NULL;
1288 rf->rf_udata = *user_data;
1291 rf->rf_ctx = plugin_get_ctx ();
1293 status = plugin_insert_read (rf);
1295 sfree (rf->rf_name);
1300 } /* int plugin_register_complex_read */
1302 int plugin_register_write (const char *name,
1303 plugin_write_cb callback, user_data_t *ud)
1305 return (create_register_callback (&list_write, name,
1306 (void *) callback, ud));
1307 } /* int plugin_register_write */
1309 static int plugin_flush_timeout_callback (user_data_t *ud)
1311 flush_callback_t *cb = ud->data;
1313 return plugin_flush (cb->name, cb->timeout, /* identifier = */ NULL);
1314 } /* static int plugin_flush_callback */
1316 static void plugin_flush_timeout_callback_free (void *data)
1318 flush_callback_t *cb = data;
1320 if (cb == NULL) return;
1324 } /* static void plugin_flush_callback_free */
1326 static char *plugin_flush_callback_name (const char *name)
1328 char *flush_prefix = "flush/";
1333 prefix_size = strlen(flush_prefix);
1334 name_size = strlen(name);
1336 flush_name = malloc (sizeof(char) * (name_size + prefix_size + 1));
1337 if (flush_name == NULL)
1339 ERROR ("plugin_flush_callback_name: malloc failed.");
1343 sstrncpy (flush_name, flush_prefix, prefix_size + 1);
1344 sstrncpy (flush_name + prefix_size, name, name_size + 1);
1347 } /* static char *plugin_flush_callback_name */
1349 int plugin_register_flush (const char *name,
1350 plugin_flush_cb callback, user_data_t *ud)
1353 plugin_ctx_t ctx = plugin_get_ctx ();
1355 status = create_register_callback (&list_flush, name,
1356 (void *) callback, ud);
1360 if (ctx.flush_interval != 0)
1364 flush_callback_t *cb;
1366 flush_name = plugin_flush_callback_name (name);
1367 if (flush_name == NULL)
1370 cb = malloc(sizeof(flush_callback_t));
1373 ERROR ("plugin_register_flush: malloc failed.");
1378 cb->name = strdup (name);
1379 if (cb->name == NULL)
1381 ERROR ("plugin_register_flush: strdup failed.");
1386 cb->timeout = ctx.flush_timeout;
1389 ud.free_func = plugin_flush_timeout_callback_free;
1391 status = plugin_register_complex_read (
1392 /* group = */ "flush",
1393 /* name = */ flush_name,
1394 /* callback = */ plugin_flush_timeout_callback,
1395 /* interval = */ ctx.flush_interval,
1396 /* user data = */ &ud);
1408 } /* int plugin_register_flush */
1410 int plugin_register_missing (const char *name,
1411 plugin_missing_cb callback, user_data_t *ud)
1413 return (create_register_callback (&list_missing, name,
1414 (void *) callback, ud));
1415 } /* int plugin_register_missing */
1417 int plugin_register_shutdown (const char *name,
1418 int (*callback) (void))
1420 return (create_register_callback (&list_shutdown, name,
1421 (void *) callback, /* user_data = */ NULL));
1422 } /* int plugin_register_shutdown */
1424 static void plugin_free_data_sets (void)
1429 if (data_sets == NULL)
1432 while (c_avl_pick (data_sets, &key, &value) == 0)
1434 data_set_t *ds = value;
1435 /* key is a pointer to ds->type */
1441 c_avl_destroy (data_sets);
1443 } /* void plugin_free_data_sets */
1445 int plugin_register_data_set (const data_set_t *ds)
1447 data_set_t *ds_copy;
1450 if ((data_sets != NULL)
1451 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1453 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1454 plugin_unregister_data_set (ds->type);
1456 else if (data_sets == NULL)
1458 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1459 if (data_sets == NULL)
1463 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1464 if (ds_copy == NULL)
1466 memcpy(ds_copy, ds, sizeof (data_set_t));
1468 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1470 if (ds_copy->ds == NULL)
1476 for (i = 0; i < ds->ds_num; i++)
1477 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1479 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1480 } /* int plugin_register_data_set */
1482 int plugin_register_log (const char *name,
1483 plugin_log_cb callback, user_data_t *ud)
1485 return (create_register_callback (&list_log, name,
1486 (void *) callback, ud));
1487 } /* int plugin_register_log */
1489 int plugin_register_notification (const char *name,
1490 plugin_notification_cb callback, user_data_t *ud)
1492 return (create_register_callback (&list_notification, name,
1493 (void *) callback, ud));
1494 } /* int plugin_register_log */
1496 int plugin_unregister_config (const char *name)
1498 cf_unregister (name);
1500 } /* int plugin_unregister_config */
1502 int plugin_unregister_complex_config (const char *name)
1504 cf_unregister_complex (name);
1506 } /* int plugin_unregister_complex_config */
1508 int plugin_unregister_init (const char *name)
1510 return (plugin_unregister (list_init, name));
1513 int plugin_unregister_read (const char *name) /* {{{ */
1521 pthread_mutex_lock (&read_lock);
1523 if (read_list == NULL)
1525 pthread_mutex_unlock (&read_lock);
1529 le = llist_search (read_list, name);
1532 pthread_mutex_unlock (&read_lock);
1533 WARNING ("plugin_unregister_read: No such read function: %s",
1538 llist_remove (read_list, le);
1541 assert (rf != NULL);
1542 rf->rf_type = RF_REMOVE;
1544 pthread_mutex_unlock (&read_lock);
1546 llentry_destroy (le);
1548 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1551 } /* }}} int plugin_unregister_read */
1553 void plugin_log_available_writers (void)
1555 log_list_callbacks (&list_write, "Available write targets:");
1558 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1560 read_func_t *rf = e->value;
1563 return strcmp (rf->rf_group, (const char *)group);
1564 } /* }}} int compare_read_func_group */
1566 int plugin_unregister_read_group (const char *group) /* {{{ */
1576 pthread_mutex_lock (&read_lock);
1578 if (read_list == NULL)
1580 pthread_mutex_unlock (&read_lock);
1586 le = llist_search_custom (read_list,
1587 compare_read_func_group, (void *)group);
1594 llist_remove (read_list, le);
1597 assert (rf != NULL);
1598 rf->rf_type = RF_REMOVE;
1600 llentry_destroy (le);
1602 DEBUG ("plugin_unregister_read_group: "
1603 "Marked `%s' (group `%s') for removal.",
1604 rf->rf_name, group);
1607 pthread_mutex_unlock (&read_lock);
1611 WARNING ("plugin_unregister_read_group: No such "
1612 "group of read function: %s", group);
1617 } /* }}} int plugin_unregister_read_group */
1619 int plugin_unregister_write (const char *name)
1621 return (plugin_unregister (list_write, name));
1624 int plugin_unregister_flush (const char *name)
1626 plugin_ctx_t ctx = plugin_get_ctx ();
1628 if (ctx.flush_interval != 0)
1632 flush_name = plugin_flush_callback_name (name);
1633 if (flush_name != NULL)
1635 plugin_unregister_read(flush_name);
1640 return plugin_unregister (list_flush, name);
1643 int plugin_unregister_missing (const char *name)
1645 return (plugin_unregister (list_missing, name));
1648 int plugin_unregister_shutdown (const char *name)
1650 return (plugin_unregister (list_shutdown, name));
1653 int plugin_unregister_data_set (const char *name)
1657 if (data_sets == NULL)
1660 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1667 } /* int plugin_unregister_data_set */
1669 int plugin_unregister_log (const char *name)
1671 return (plugin_unregister (list_log, name));
1674 int plugin_unregister_notification (const char *name)
1676 return (plugin_unregister (list_notification, name));
1679 void plugin_init_all (void)
1681 char const *chain_name;
1682 long write_threads_num;
1686 /* Init the value cache */
1689 if (IS_TRUE (global_option_get ("CollectInternalStats")))
1690 record_statistics = 1;
1692 chain_name = global_option_get ("PreCacheChain");
1693 pre_cache_chain = fc_chain_get_by_name (chain_name);
1695 chain_name = global_option_get ("PostCacheChain");
1696 post_cache_chain = fc_chain_get_by_name (chain_name);
1698 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1700 if (write_limit_high < 0)
1702 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1703 write_limit_high = 0;
1706 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1707 /* default = */ write_limit_high / 2);
1708 if (write_limit_low < 0)
1710 ERROR ("WriteQueueLimitLow must be positive or zero.");
1711 write_limit_low = write_limit_high / 2;
1713 else if (write_limit_low > write_limit_high)
1715 ERROR ("WriteQueueLimitLow must not be larger than "
1716 "WriteQueueLimitHigh.");
1717 write_limit_low = write_limit_high;
1720 write_threads_num = global_option_get_long ("WriteThreads",
1722 if (write_threads_num < 1)
1724 ERROR ("WriteThreads must be positive.");
1725 write_threads_num = 5;
1728 if ((list_init == NULL) && (read_heap == NULL))
1731 /* Calling all init callbacks before checking if read callbacks
1732 * are available allows the init callbacks to register the read
1734 le = llist_head (list_init);
1737 callback_func_t *cf;
1738 plugin_init_cb callback;
1739 plugin_ctx_t old_ctx;
1742 old_ctx = plugin_set_ctx (cf->cf_ctx);
1743 callback = cf->cf_callback;
1744 status = (*callback) ();
1745 plugin_set_ctx (old_ctx);
1749 ERROR ("Initialization of plugin `%s' "
1750 "failed with status %i. "
1751 "Plugin will be unloaded.",
1753 /* Plugins that register read callbacks from the init
1754 * callback should take care of appropriate error
1755 * handling themselves. */
1756 /* FIXME: Unload _all_ functions */
1757 plugin_unregister_read (le->key);
1763 start_write_threads ((size_t) write_threads_num);
1765 max_read_interval = global_option_get_time ("MaxReadInterval",
1766 DEFAULT_MAX_READ_INTERVAL);
1768 /* Start read-threads */
1769 if (read_heap != NULL)
1774 rt = global_option_get ("ReadThreads");
1777 start_read_threads ((num > 0) ? num : 5);
1779 } /* void plugin_init_all */
1781 /* TODO: Rename this function. */
1782 void plugin_read_all (void)
1784 if(record_statistics) {
1785 plugin_update_internal_statistics ();
1787 uc_check_timeout ();
1790 } /* void plugin_read_all */
1792 /* Read function called when the `-T' command line argument is given. */
1793 int plugin_read_all_once (void)
1796 int return_status = 0;
1798 if (read_heap == NULL)
1800 NOTICE ("No read-functions are registered.");
1807 plugin_ctx_t old_ctx;
1809 rf = c_heap_get_root (read_heap);
1813 old_ctx = plugin_set_ctx (rf->rf_ctx);
1815 if (rf->rf_type == RF_SIMPLE)
1817 int (*callback) (void);
1819 callback = rf->rf_callback;
1820 status = (*callback) ();
1824 plugin_read_cb callback;
1826 callback = rf->rf_callback;
1827 status = (*callback) (&rf->rf_udata);
1830 plugin_set_ctx (old_ctx);
1834 NOTICE ("read-function of plugin `%s' failed.",
1839 sfree (rf->rf_name);
1840 destroy_callback ((void *) rf);
1843 return (return_status);
1844 } /* int plugin_read_all_once */
1846 int plugin_write (const char *plugin, /* {{{ */
1847 const data_set_t *ds, const value_list_t *vl)
1855 if (list_write == NULL)
1860 ds = plugin_get_ds (vl->type);
1863 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1873 le = llist_head (list_write);
1876 callback_func_t *cf = le->value;
1877 plugin_write_cb callback;
1879 /* do not switch plugin context; rather keep the context (interval)
1880 * information of the calling read plugin */
1882 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1883 callback = cf->cf_callback;
1884 status = (*callback) (ds, vl, &cf->cf_udata);
1893 if ((success == 0) && (failure != 0))
1898 else /* plugin != NULL */
1900 callback_func_t *cf;
1901 plugin_write_cb callback;
1903 le = llist_head (list_write);
1906 if (strcasecmp (plugin, le->key) == 0)
1917 /* do not switch plugin context; rather keep the context (interval)
1918 * information of the calling read plugin */
1920 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1921 callback = cf->cf_callback;
1922 status = (*callback) (ds, vl, &cf->cf_udata);
1926 } /* }}} int plugin_write */
1928 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1932 if (list_flush == NULL)
1935 le = llist_head (list_flush);
1938 callback_func_t *cf;
1939 plugin_flush_cb callback;
1940 plugin_ctx_t old_ctx;
1942 if ((plugin != NULL)
1943 && (strcmp (plugin, le->key) != 0))
1950 old_ctx = plugin_set_ctx (cf->cf_ctx);
1951 callback = cf->cf_callback;
1953 (*callback) (timeout, identifier, &cf->cf_udata);
1955 plugin_set_ctx (old_ctx);
1960 } /* int plugin_flush */
1962 void plugin_shutdown_all (void)
1966 stop_read_threads ();
1968 destroy_all_callbacks (&list_init);
1970 pthread_mutex_lock (&read_lock);
1971 llist_destroy (read_list);
1973 pthread_mutex_unlock (&read_lock);
1975 destroy_read_heap ();
1977 plugin_flush (/* plugin = */ NULL,
1979 /* identifier = */ NULL);
1982 if (list_shutdown != NULL)
1983 le = llist_head (list_shutdown);
1987 callback_func_t *cf;
1988 plugin_shutdown_cb callback;
1989 plugin_ctx_t old_ctx;
1992 old_ctx = plugin_set_ctx (cf->cf_ctx);
1993 callback = cf->cf_callback;
1995 /* Advance the pointer before calling the callback allows
1996 * shutdown functions to unregister themselves. If done the
1997 * other way around the memory `le' points to will be freed
1998 * after callback returns. */
2003 plugin_set_ctx (old_ctx);
2006 stop_write_threads ();
2008 /* Write plugins which use the `user_data' pointer usually need the
2009 * same data available to the flush callback. If this is the case, set
2010 * the free_function to NULL when registering the flush callback and to
2011 * the real free function when registering the write callback. This way
2012 * the data isn't freed twice. */
2013 destroy_all_callbacks (&list_flush);
2014 destroy_all_callbacks (&list_missing);
2015 destroy_all_callbacks (&list_write);
2017 destroy_all_callbacks (&list_notification);
2018 destroy_all_callbacks (&list_shutdown);
2019 destroy_all_callbacks (&list_log);
2021 plugin_free_loaded ();
2022 plugin_free_data_sets ();
2023 } /* void plugin_shutdown_all */
2025 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
2029 if (list_missing == NULL)
2032 le = llist_head (list_missing);
2035 callback_func_t *cf;
2036 plugin_missing_cb callback;
2037 plugin_ctx_t old_ctx;
2041 old_ctx = plugin_set_ctx (cf->cf_ctx);
2042 callback = cf->cf_callback;
2044 status = (*callback) (vl, &cf->cf_udata);
2045 plugin_set_ctx (old_ctx);
2050 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
2051 "failed with status %i.",
2064 } /* int }}} plugin_dispatch_missing */
2066 static int plugin_dispatch_values_internal (value_list_t *vl)
2069 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
2071 value_t *saved_values;
2072 int saved_values_len;
2076 int free_meta_data = 0;
2078 if ((vl == NULL) || (vl->type[0] == 0)
2079 || (vl->values == NULL) || (vl->values_len < 1))
2081 ERROR ("plugin_dispatch_values: Invalid value list "
2082 "from plugin %s.", vl->plugin);
2086 /* Free meta data only if the calling function didn't specify any. In
2087 * this case matches and targets may add some and the calling function
2088 * may not expect (and therefore free) that data. */
2089 if (vl->meta == NULL)
2092 if (list_write == NULL)
2093 c_complain_once (LOG_WARNING, &no_write_complaint,
2094 "plugin_dispatch_values: No write callback has been "
2095 "registered. Please load at least one output plugin, "
2096 "if you want the collected data to be stored.");
2098 if (data_sets == NULL)
2100 ERROR ("plugin_dispatch_values: No data sets registered. "
2101 "Could the types database be read? Check "
2102 "your `TypesDB' setting!");
2106 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
2108 char ident[6 * DATA_MAX_NAME_LEN];
2110 FORMAT_VL (ident, sizeof (ident), vl);
2111 INFO ("plugin_dispatch_values: Dataset not found: %s "
2112 "(from \"%s\"), check your types.db!",
2117 /* Assured by plugin_value_list_clone(). The time is determined at
2118 * _enqueue_ time. */
2119 assert (vl->time != 0);
2120 assert (vl->interval != 0);
2122 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
2124 "plugin = %s; plugin_instance = %s; "
2125 "type = %s; type_instance = %s;",
2126 CDTIME_T_TO_DOUBLE (vl->time),
2127 CDTIME_T_TO_DOUBLE (vl->interval),
2129 vl->plugin, vl->plugin_instance,
2130 vl->type, vl->type_instance);
2133 assert (0 == strcmp (ds->type, vl->type));
2135 if (0 != strcmp (ds->type, vl->type))
2136 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
2137 ds->type, vl->type);
2141 assert (ds->ds_num == vl->values_len);
2143 if (ds->ds_num != vl->values_len)
2145 ERROR ("plugin_dispatch_values: ds->type = %s: "
2146 "(ds->ds_num = %zu) != "
2147 "(vl->values_len = %zu)",
2148 ds->type, ds->ds_num, vl->values_len);
2153 escape_slashes (vl->host, sizeof (vl->host));
2154 escape_slashes (vl->plugin, sizeof (vl->plugin));
2155 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
2156 escape_slashes (vl->type, sizeof (vl->type));
2157 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
2159 /* Copy the values. This way, we can assure `targets' that they get
2160 * dynamically allocated values, which they can free and replace if
2162 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
2164 saved_values = vl->values;
2165 saved_values_len = vl->values_len;
2167 vl->values = (value_t *) calloc (vl->values_len,
2168 sizeof (*vl->values));
2169 if (vl->values == NULL)
2171 ERROR ("plugin_dispatch_values: calloc failed.");
2172 vl->values = saved_values;
2175 memcpy (vl->values, saved_values,
2176 vl->values_len * sizeof (*vl->values));
2178 else /* if ((pre == NULL) && (post == NULL)) */
2180 saved_values = NULL;
2181 saved_values_len = 0;
2184 if (pre_cache_chain != NULL)
2186 status = fc_process_chain (ds, vl, pre_cache_chain);
2189 WARNING ("plugin_dispatch_values: Running the "
2190 "pre-cache chain failed with "
2194 else if (status == FC_TARGET_STOP)
2196 /* Restore the state of the value_list so that plugins
2197 * don't get confused.. */
2198 if (saved_values != NULL)
2201 vl->values = saved_values;
2202 vl->values_len = saved_values_len;
2208 /* Update the value cache */
2211 if (post_cache_chain != NULL)
2213 status = fc_process_chain (ds, vl, post_cache_chain);
2216 WARNING ("plugin_dispatch_values: Running the "
2217 "post-cache chain failed with "
2223 fc_default_action (ds, vl);
2225 /* Restore the state of the value_list so that plugins don't get
2227 if (saved_values != NULL)
2230 vl->values = saved_values;
2231 vl->values_len = saved_values_len;
2234 if ((free_meta_data != 0) && (vl->meta != NULL))
2236 meta_data_destroy (vl->meta);
2241 } /* int plugin_dispatch_values_internal */
2243 static double get_drop_probability (void) /* {{{ */
2249 pthread_mutex_lock (&write_lock);
2250 wql = write_queue_length;
2251 pthread_mutex_unlock (&write_lock);
2253 if (wql < write_limit_low)
2255 if (wql >= write_limit_high)
2258 pos = 1 + wql - write_limit_low;
2259 size = 1 + write_limit_high - write_limit_low;
2261 return (((double) pos) / ((double) size));
2262 } /* }}} double get_drop_probability */
2264 static _Bool check_drop_value (void) /* {{{ */
2266 static cdtime_t last_message_time = 0;
2267 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2273 if (write_limit_high == 0)
2276 p = get_drop_probability ();
2280 status = pthread_mutex_trylock (&last_message_lock);
2286 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2288 last_message_time = now;
2289 ERROR ("plugin_dispatch_values: Low water mark "
2290 "reached. Dropping %.0f%% of metrics.",
2293 pthread_mutex_unlock (&last_message_lock);
2304 } /* }}} _Bool check_drop_value */
2306 int plugin_dispatch_values (value_list_t const *vl)
2309 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2311 if (check_drop_value ()) {
2312 if(record_statistics) {
2313 pthread_mutex_lock(&statistics_lock);
2314 stats_values_dropped++;
2315 pthread_mutex_unlock(&statistics_lock);
2320 status = plugin_write_enqueue (vl);
2324 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2325 "with status %i (%s).", status,
2326 sstrerror (status, errbuf, sizeof (errbuf)));
2333 __attribute__((sentinel))
2334 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2335 _Bool store_percentage, int store_type, ...)
2342 assert (template->values_len == 1);
2344 /* Calculate sum for Gauge to calculate percent if needed */
2345 if (DS_TYPE_GAUGE == store_type) {
2346 va_start (ap, store_type);
2352 name = va_arg (ap, char const *);
2356 value = va_arg (ap, gauge_t);
2364 vl = plugin_value_list_clone (template);
2365 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2366 if (store_percentage)
2367 sstrncpy (vl->type, "percent", sizeof (vl->type));
2369 va_start (ap, store_type);
2375 /* Set the type instance. */
2376 name = va_arg (ap, char const *);
2379 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2381 /* Set the value. */
2385 vl->values[0].gauge = va_arg (ap, gauge_t);
2386 if (store_percentage)
2387 vl->values[0].gauge *= 100.0 / sum;
2389 case DS_TYPE_ABSOLUTE:
2390 vl->values[0].absolute = va_arg (ap, absolute_t);
2392 case DS_TYPE_COUNTER:
2393 vl->values[0].counter = va_arg (ap, counter_t);
2395 case DS_TYPE_DERIVE:
2396 vl->values[0].derive = va_arg (ap, derive_t);
2399 ERROR ("plugin_dispatch_multivalue: given store_type is incorrect.");
2404 status = plugin_write_enqueue (vl);
2410 plugin_value_list_free (vl);
2412 } /* }}} int plugin_dispatch_multivalue */
2414 int plugin_dispatch_notification (const notification_t *notif)
2417 /* Possible TODO: Add flap detection here */
2419 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2420 "time = %.3f; host = %s;",
2421 notif->severity, notif->message,
2422 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2424 /* Nobody cares for notifications */
2425 if (list_notification == NULL)
2428 le = llist_head (list_notification);
2431 callback_func_t *cf;
2432 plugin_notification_cb callback;
2435 /* do not switch plugin context; rather keep the context
2436 * (interval) information of the calling plugin */
2439 callback = cf->cf_callback;
2440 status = (*callback) (notif, &cf->cf_udata);
2443 WARNING ("plugin_dispatch_notification: Notification "
2444 "callback %s returned %i.",
2452 } /* int plugin_dispatch_notification */
2454 void plugin_log (int level, const char *format, ...)
2461 if (level >= LOG_DEBUG)
2465 va_start (ap, format);
2466 vsnprintf (msg, sizeof (msg), format, ap);
2467 msg[sizeof (msg) - 1] = '\0';
2470 if (list_log == NULL)
2472 fprintf (stderr, "%s\n", msg);
2476 le = llist_head (list_log);
2479 callback_func_t *cf;
2480 plugin_log_cb callback;
2483 callback = cf->cf_callback;
2485 /* do not switch plugin context; rather keep the context
2486 * (interval) information of the calling plugin */
2488 (*callback) (level, msg, &cf->cf_udata);
2492 } /* void plugin_log */
2494 int parse_log_severity (const char *severity)
2498 if ((0 == strcasecmp (severity, "emerg"))
2499 || (0 == strcasecmp (severity, "alert"))
2500 || (0 == strcasecmp (severity, "crit"))
2501 || (0 == strcasecmp (severity, "err")))
2502 log_level = LOG_ERR;
2503 else if (0 == strcasecmp (severity, "warning"))
2504 log_level = LOG_WARNING;
2505 else if (0 == strcasecmp (severity, "notice"))
2506 log_level = LOG_NOTICE;
2507 else if (0 == strcasecmp (severity, "info"))
2508 log_level = LOG_INFO;
2510 else if (0 == strcasecmp (severity, "debug"))
2511 log_level = LOG_DEBUG;
2512 #endif /* COLLECT_DEBUG */
2515 } /* int parse_log_severity */
2517 int parse_notif_severity (const char *severity)
2519 int notif_severity = -1;
2521 if (strcasecmp (severity, "FAILURE") == 0)
2522 notif_severity = NOTIF_FAILURE;
2523 else if (strcmp (severity, "OKAY") == 0)
2524 notif_severity = NOTIF_OKAY;
2525 else if ((strcmp (severity, "WARNING") == 0)
2526 || (strcmp (severity, "WARN") == 0))
2527 notif_severity = NOTIF_WARNING;
2529 return (notif_severity);
2530 } /* int parse_notif_severity */
2532 const data_set_t *plugin_get_ds (const char *name)
2536 if (data_sets == NULL)
2538 ERROR ("plugin_get_ds: No data sets are defined yet.");
2542 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2544 DEBUG ("No such dataset registered: %s", name);
2549 } /* data_set_t *plugin_get_ds */
2551 static int plugin_notification_meta_add (notification_t *n,
2553 enum notification_meta_type_e type,
2556 notification_meta_t *meta;
2557 notification_meta_t *tail;
2559 if ((n == NULL) || (name == NULL) || (value == NULL))
2561 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2565 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2568 ERROR ("plugin_notification_meta_add: malloc failed.");
2571 memset (meta, 0, sizeof (notification_meta_t));
2573 sstrncpy (meta->name, name, sizeof (meta->name));
2578 case NM_TYPE_STRING:
2580 meta->nm_value.nm_string = strdup ((const char *) value);
2581 if (meta->nm_value.nm_string == NULL)
2583 ERROR ("plugin_notification_meta_add: strdup failed.");
2589 case NM_TYPE_SIGNED_INT:
2591 meta->nm_value.nm_signed_int = *((int64_t *) value);
2594 case NM_TYPE_UNSIGNED_INT:
2596 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2599 case NM_TYPE_DOUBLE:
2601 meta->nm_value.nm_double = *((double *) value);
2604 case NM_TYPE_BOOLEAN:
2606 meta->nm_value.nm_boolean = *((_Bool *) value);
2611 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2615 } /* switch (type) */
2619 while ((tail != NULL) && (tail->next != NULL))
2628 } /* int plugin_notification_meta_add */
2630 int plugin_notification_meta_add_string (notification_t *n,
2634 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2637 int plugin_notification_meta_add_signed_int (notification_t *n,
2641 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2644 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2648 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2651 int plugin_notification_meta_add_double (notification_t *n,
2655 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2658 int plugin_notification_meta_add_boolean (notification_t *n,
2662 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2665 int plugin_notification_meta_copy (notification_t *dst,
2666 const notification_t *src)
2668 notification_meta_t *meta;
2670 assert (dst != NULL);
2671 assert (src != NULL);
2672 assert (dst != src);
2673 assert ((src->meta == NULL) || (src->meta != dst->meta));
2675 for (meta = src->meta; meta != NULL; meta = meta->next)
2677 if (meta->type == NM_TYPE_STRING)
2678 plugin_notification_meta_add_string (dst, meta->name,
2679 meta->nm_value.nm_string);
2680 else if (meta->type == NM_TYPE_SIGNED_INT)
2681 plugin_notification_meta_add_signed_int (dst, meta->name,
2682 meta->nm_value.nm_signed_int);
2683 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2684 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2685 meta->nm_value.nm_unsigned_int);
2686 else if (meta->type == NM_TYPE_DOUBLE)
2687 plugin_notification_meta_add_double (dst, meta->name,
2688 meta->nm_value.nm_double);
2689 else if (meta->type == NM_TYPE_BOOLEAN)
2690 plugin_notification_meta_add_boolean (dst, meta->name,
2691 meta->nm_value.nm_boolean);
2695 } /* int plugin_notification_meta_copy */
2697 int plugin_notification_meta_free (notification_meta_t *n)
2699 notification_meta_t *this;
2700 notification_meta_t *next;
2704 ERROR ("plugin_notification_meta_free: n == NULL!");
2709 while (this != NULL)
2713 if (this->type == NM_TYPE_STRING)
2715 free ((char *)this->nm_value.nm_string);
2716 this->nm_value.nm_string = NULL;
2724 } /* int plugin_notification_meta_free */
2726 static void plugin_ctx_destructor (void *ctx)
2729 } /* void plugin_ctx_destructor */
2731 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2733 static plugin_ctx_t *plugin_ctx_create (void)
2737 ctx = malloc (sizeof (*ctx));
2740 ERROR ("Failed to allocate plugin context: %s",
2741 sstrerror (errno, errbuf, sizeof (errbuf)));
2746 assert (plugin_ctx_key_initialized);
2747 pthread_setspecific (plugin_ctx_key, ctx);
2748 DEBUG("Created new plugin context.");
2750 } /* int plugin_ctx_create */
2752 void plugin_init_ctx (void)
2754 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2755 plugin_ctx_key_initialized = 1;
2756 } /* void plugin_init_ctx */
2758 plugin_ctx_t plugin_get_ctx (void)
2762 assert (plugin_ctx_key_initialized);
2763 ctx = pthread_getspecific (plugin_ctx_key);
2766 ctx = plugin_ctx_create ();
2767 /* this must no happen -- exit() instead? */
2773 } /* plugin_ctx_t plugin_get_ctx */
2775 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2780 assert (plugin_ctx_key_initialized);
2781 c = pthread_getspecific (plugin_ctx_key);
2784 c = plugin_ctx_create ();
2785 /* this must no happen -- exit() instead? */
2794 } /* void plugin_set_ctx */
2796 cdtime_t plugin_get_interval (void)
2800 interval = plugin_get_ctx().interval;
2804 return cf_get_default_interval ();
2805 } /* cdtime_t plugin_get_interval */
2809 void *(*start_routine) (void *);
2813 static void *plugin_thread_start (void *arg)
2815 plugin_thread_t *plugin_thread = arg;
2817 void *(*start_routine) (void *) = plugin_thread->start_routine;
2818 void *plugin_arg = plugin_thread->arg;
2820 plugin_set_ctx (plugin_thread->ctx);
2822 free (plugin_thread);
2824 return start_routine (plugin_arg);
2825 } /* void *plugin_thread_start */
2827 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2828 void *(*start_routine) (void *), void *arg)
2830 plugin_thread_t *plugin_thread;
2832 plugin_thread = malloc (sizeof (*plugin_thread));
2833 if (plugin_thread == NULL)
2836 plugin_thread->ctx = plugin_get_ctx ();
2837 plugin_thread->start_routine = start_routine;
2838 plugin_thread->arg = arg;
2840 return pthread_create (thread, attr,
2841 plugin_thread_start, plugin_thread);
2842 } /* int plugin_thread_create */
2844 /* vim: set sw=8 ts=8 noet fdm=marker : */