2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2014 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian Harl <sh at tokkee.org>
31 #include "configfile.h"
32 #include "filter_chain.h"
33 #include "utils_avltree.h"
34 #include "utils_cache.h"
35 #include "utils_complain.h"
36 #include "utils_llist.h"
37 #include "utils_heap.h"
38 #include "utils_time.h"
39 #include "utils_random.h"
46 struct callback_func_s
52 typedef struct callback_func_s callback_func_t;
56 #define RF_REMOVE 65535
59 /* `read_func_t' "inherits" from `callback_func_t'.
60 * The `rf_super' member MUST be the first one in this structure! */
61 #define rf_callback rf_super.cf_callback
62 #define rf_udata rf_super.cf_udata
63 #define rf_ctx rf_super.cf_ctx
64 callback_func_t rf_super;
65 char rf_group[DATA_MAX_NAME_LEN];
69 cdtime_t rf_effective_interval;
70 cdtime_t rf_next_read;
72 typedef struct read_func_s read_func_t;
75 typedef struct write_queue_s write_queue_t;
86 static c_avl_tree_t *plugins_loaded = NULL;
88 static llist_t *list_init;
89 static llist_t *list_write;
90 static llist_t *list_flush;
91 static llist_t *list_missing;
92 static llist_t *list_shutdown;
93 static llist_t *list_log;
94 static llist_t *list_notification;
96 static fc_chain_t *pre_cache_chain = NULL;
97 static fc_chain_t *post_cache_chain = NULL;
99 static c_avl_tree_t *data_sets;
101 static char *plugindir = NULL;
103 #ifndef DEFAULT_MAX_READ_INTERVAL
104 # define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T (86400)
106 static c_heap_t *read_heap = NULL;
107 static llist_t *read_list;
108 static int read_loop = 1;
109 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
110 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
111 static pthread_t *read_threads = NULL;
112 static int read_threads_num = 0;
113 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
115 static write_queue_t *write_queue_head;
116 static write_queue_t *write_queue_tail;
117 static long write_queue_length = 0;
118 static _Bool write_loop = 1;
119 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
120 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
121 static pthread_t *write_threads = NULL;
122 static size_t write_threads_num = 0;
124 static pthread_key_t plugin_ctx_key;
125 static _Bool plugin_ctx_key_initialized = 0;
127 static long write_limit_high = 0;
128 static long write_limit_low = 0;
130 static derive_t stats_values_dropped = 0;
131 static _Bool record_statistics = 0;
136 static int plugin_dispatch_values_internal (value_list_t *vl);
138 static const char *plugin_get_dir (void)
140 if (plugindir == NULL)
146 static void plugin_update_internal_statistics (void) { /* {{{ */
147 derive_t copy_write_queue_length;
148 value_list_t vl = VALUE_LIST_INIT;
151 copy_write_queue_length = write_queue_length;
153 /* Initialize `vl' */
157 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
158 sstrncpy (vl.plugin, "collectd", sizeof (vl.plugin));
160 vl.type_instance[0] = 0;
164 sstrncpy (vl.plugin_instance, "write_queue",
165 sizeof (vl.plugin_instance));
167 /* Write queue : queue length */
168 vl.values[0].gauge = (gauge_t) copy_write_queue_length;
169 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
170 vl.type_instance[0] = 0;
171 plugin_dispatch_values (&vl);
173 /* Write queue : Values dropped (queue length > low limit) */
174 vl.values[0].derive = (derive_t) stats_values_dropped;
175 sstrncpy (vl.type, "derive", sizeof (vl.type));
176 sstrncpy (vl.type_instance, "dropped", sizeof (vl.type_instance));
177 plugin_dispatch_values (&vl);
180 sstrncpy (vl.plugin_instance, "cache",
181 sizeof (vl.plugin_instance));
183 /* Cache : Nb entry in cache tree */
184 vl.values[0].gauge = (gauge_t) uc_get_size();
185 sstrncpy (vl.type, "cache_size", sizeof (vl.type));
186 vl.type_instance[0] = 0;
187 plugin_dispatch_values (&vl);
190 } /* }}} void plugin_update_internal_statistics */
192 static void destroy_callback (callback_func_t *cf) /* {{{ */
197 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
199 cf->cf_udata.free_func (cf->cf_udata.data);
200 cf->cf_udata.data = NULL;
201 cf->cf_udata.free_func = NULL;
204 } /* }}} void destroy_callback */
206 static void destroy_all_callbacks (llist_t **list) /* {{{ */
213 le = llist_head (*list);
221 destroy_callback (le->value);
227 llist_destroy (*list);
229 } /* }}} void destroy_all_callbacks */
231 static void destroy_read_heap (void) /* {{{ */
233 if (read_heap == NULL)
240 cf = c_heap_get_root (read_heap);
244 destroy_callback (cf);
247 c_heap_destroy (read_heap);
249 } /* }}} void destroy_read_heap */
251 static int register_callback (llist_t **list, /* {{{ */
252 const char *name, callback_func_t *cf)
259 *list = llist_create ();
262 ERROR ("plugin: register_callback: "
263 "llist_create failed.");
264 destroy_callback (cf);
272 ERROR ("plugin: register_callback: strdup failed.");
273 destroy_callback (cf);
277 le = llist_search (*list, name);
280 le = llentry_create (key, cf);
283 ERROR ("plugin: register_callback: "
284 "llentry_create failed.");
286 destroy_callback (cf);
290 llist_append (*list, le);
294 callback_func_t *old_cf;
299 WARNING ("plugin: register_callback: "
300 "a callback named `%s' already exists - "
301 "overwriting the old entry!", name);
303 destroy_callback (old_cf);
308 } /* }}} int register_callback */
310 static int create_register_callback (llist_t **list, /* {{{ */
311 const char *name, void *callback, user_data_t *ud)
315 cf = (callback_func_t *) malloc (sizeof (*cf));
318 ERROR ("plugin: create_register_callback: malloc failed.");
321 memset (cf, 0, sizeof (*cf));
323 cf->cf_callback = callback;
326 cf->cf_udata.data = NULL;
327 cf->cf_udata.free_func = NULL;
334 cf->cf_ctx = plugin_get_ctx ();
336 return (register_callback (list, name, cf));
337 } /* }}} int create_register_callback */
339 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
346 e = llist_search (list, name);
350 llist_remove (list, e);
353 destroy_callback (e->value);
358 } /* }}} int plugin_unregister */
361 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
362 * object, but it will bitch about a shared object not having a
363 * ``module_register'' symbol..
365 static int plugin_load_file (char *file, uint32_t flags)
368 void (*reg_handle) (void);
371 lt_dlerror (); /* clear errors */
373 #if LIBTOOL_VERSION == 2
374 if (flags & PLUGIN_FLAGS_GLOBAL) {
376 lt_dladvise_init(&advise);
377 lt_dladvise_global(&advise);
378 dlh = lt_dlopenadvise(file, advise);
379 lt_dladvise_destroy(&advise);
381 dlh = lt_dlopen (file);
383 #else /* if LIBTOOL_VERSION == 1 */
384 if (flags & PLUGIN_FLAGS_GLOBAL)
385 WARNING ("plugin_load_file: The global flag is not supported, "
386 "libtool 2 is required for this.");
387 dlh = lt_dlopen (file);
392 char errbuf[1024] = "";
394 ssnprintf (errbuf, sizeof (errbuf),
395 "lt_dlopen (\"%s\") failed: %s. "
396 "The most common cause for this problem is "
397 "missing dependencies. Use ldd(1) to check "
398 "the dependencies of the plugin "
400 file, lt_dlerror ());
402 ERROR ("%s", errbuf);
403 /* Make sure this is printed to STDERR in any case, but also
404 * make sure it's printed only once. */
405 if (list_log != NULL)
406 fprintf (stderr, "ERROR: %s\n", errbuf);
411 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
413 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
414 file, lt_dlerror ());
424 static void *plugin_read_thread (void __attribute__((unused)) *args)
426 while (read_loop != 0)
429 plugin_ctx_t old_ctx;
435 /* Get the read function that needs to be read next.
436 * We don't need to hold "read_lock" for the heap, but we need
437 * to call c_heap_get_root() and pthread_cond_wait() in the
438 * same protected block. */
439 pthread_mutex_lock (&read_lock);
440 rf = c_heap_get_root (read_heap);
443 pthread_cond_wait (&read_cond, &read_lock);
444 pthread_mutex_unlock (&read_lock);
447 pthread_mutex_unlock (&read_lock);
449 if (rf->rf_interval == 0)
451 /* this should not happen, because the interval is set
452 * for each plugin when loading it
453 * XXX: issue a warning? */
454 rf->rf_interval = plugin_get_interval ();
455 rf->rf_effective_interval = rf->rf_interval;
457 rf->rf_next_read = cdtime ();
460 /* sleep until this entry is due,
461 * using pthread_cond_timedwait */
462 pthread_mutex_lock (&read_lock);
463 /* In pthread_cond_timedwait, spurious wakeups are possible
464 * (and really happen, at least on NetBSD with > 1 CPU), thus
465 * we need to re-evaluate the condition every time
466 * pthread_cond_timedwait returns. */
468 while ((read_loop != 0)
469 && (cdtime () < rf->rf_next_read)
472 struct timespec ts = { 0 };
474 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
476 rc = pthread_cond_timedwait (&read_cond, &read_lock,
480 /* Must hold `read_lock' when accessing `rf->rf_type'. */
481 rf_type = rf->rf_type;
482 pthread_mutex_unlock (&read_lock);
484 /* Check if we're supposed to stop.. This may have interrupted
488 /* Insert `rf' again, so it can be free'd correctly */
489 c_heap_insert (read_heap, rf);
493 /* The entry has been marked for deletion. The linked list
494 * entry has already been removed by `plugin_unregister_read'.
495 * All we have to do here is free the `read_func_t' and
497 if (rf_type == RF_REMOVE)
499 DEBUG ("plugin_read_thread: Destroying the `%s' "
500 "callback.", rf->rf_name);
502 destroy_callback ((callback_func_t *) rf);
507 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
509 old_ctx = plugin_set_ctx (rf->rf_ctx);
511 if (rf_type == RF_SIMPLE)
513 int (*callback) (void);
515 callback = rf->rf_callback;
516 status = (*callback) ();
520 plugin_read_cb callback;
522 assert (rf_type == RF_COMPLEX);
524 callback = rf->rf_callback;
525 status = (*callback) (&rf->rf_udata);
528 plugin_set_ctx (old_ctx);
530 /* If the function signals failure, we will increase the
531 * intervals in which it will be called. */
534 rf->rf_effective_interval *= 2;
535 if (rf->rf_effective_interval > max_read_interval)
536 rf->rf_effective_interval = max_read_interval;
538 NOTICE ("read-function of plugin `%s' failed. "
539 "Will suspend it for %.3f seconds.",
541 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
545 /* Success: Restore the interval, if it was changed. */
546 rf->rf_effective_interval = rf->rf_interval;
549 /* update the ``next read due'' field */
552 DEBUG ("plugin_read_thread: Effective interval of the "
553 "%s plugin is %.3f seconds.",
555 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
557 /* Calculate the next (absolute) time at which this function
558 * should be called. */
559 rf->rf_next_read += rf->rf_effective_interval;
561 /* Check, if `rf_next_read' is in the past. */
562 if (rf->rf_next_read < now)
564 /* `rf_next_read' is in the past. Insert `now'
565 * so this value doesn't trail off into the
567 rf->rf_next_read = now;
570 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
572 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
574 /* Re-insert this read function into the heap again. */
575 c_heap_insert (read_heap, rf);
576 } /* while (read_loop) */
580 } /* void *plugin_read_thread */
582 static void start_read_threads (int num)
586 if (read_threads != NULL)
589 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
590 if (read_threads == NULL)
592 ERROR ("plugin: start_read_threads: calloc failed.");
596 read_threads_num = 0;
597 for (i = 0; i < num; i++)
599 if (pthread_create (read_threads + read_threads_num, NULL,
600 plugin_read_thread, NULL) == 0)
606 ERROR ("plugin: start_read_threads: pthread_create failed.");
610 } /* void start_read_threads */
612 static void stop_read_threads (void)
616 if (read_threads == NULL)
619 INFO ("collectd: Stopping %i read threads.", read_threads_num);
621 pthread_mutex_lock (&read_lock);
623 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
624 pthread_cond_broadcast (&read_cond);
625 pthread_mutex_unlock (&read_lock);
627 for (i = 0; i < read_threads_num; i++)
629 if (pthread_join (read_threads[i], NULL) != 0)
631 ERROR ("plugin: stop_read_threads: pthread_join failed.");
633 read_threads[i] = (pthread_t) 0;
635 sfree (read_threads);
636 read_threads_num = 0;
637 } /* void stop_read_threads */
639 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
644 meta_data_destroy (vl->meta);
647 } /* }}} void plugin_value_list_free */
649 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
656 vl = malloc (sizeof (*vl));
659 memcpy (vl, vl_orig, sizeof (*vl));
661 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
662 if (vl->values == NULL)
664 plugin_value_list_free (vl);
667 memcpy (vl->values, vl_orig->values,
668 vl_orig->values_len * sizeof (*vl->values));
670 vl->meta = meta_data_clone (vl->meta);
671 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
673 plugin_value_list_free (vl);
678 vl->time = cdtime ();
680 /* Fill in the interval from the thread context, if it is zero. */
681 if (vl->interval == 0)
683 plugin_ctx_t ctx = plugin_get_ctx ();
685 if (ctx.interval != 0)
686 vl->interval = ctx.interval;
689 char name[6 * DATA_MAX_NAME_LEN];
690 FORMAT_VL (name, sizeof (name), vl);
691 ERROR ("plugin_value_list_clone: Unable to determine "
692 "interval from context for "
693 "value list \"%s\". "
694 "This indicates a broken plugin. "
695 "Please report this problem to the "
696 "collectd mailing list or at "
697 "<http://collectd.org/bugs/>.", name);
698 vl->interval = cf_get_default_interval ();
703 } /* }}} value_list_t *plugin_value_list_clone */
705 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
709 q = malloc (sizeof (*q));
714 q->vl = plugin_value_list_clone (vl);
721 /* Store context of caller (read plugin); otherwise, it would not be
722 * available to the write plugins when actually dispatching the
723 * value-list later on. */
724 q->ctx = plugin_get_ctx ();
726 pthread_mutex_lock (&write_lock);
728 if (write_queue_tail == NULL)
730 write_queue_head = q;
731 write_queue_tail = q;
732 write_queue_length = 1;
736 write_queue_tail->next = q;
737 write_queue_tail = q;
738 write_queue_length += 1;
741 pthread_cond_signal (&write_cond);
742 pthread_mutex_unlock (&write_lock);
745 } /* }}} int plugin_write_enqueue */
747 static value_list_t *plugin_write_dequeue (void) /* {{{ */
752 pthread_mutex_lock (&write_lock);
754 while (write_loop && (write_queue_head == NULL))
755 pthread_cond_wait (&write_cond, &write_lock);
757 if (write_queue_head == NULL)
759 pthread_mutex_unlock (&write_lock);
763 q = write_queue_head;
764 write_queue_head = q->next;
765 write_queue_length -= 1;
766 if (write_queue_head == NULL) {
767 write_queue_tail = NULL;
768 assert(0 == write_queue_length);
771 pthread_mutex_unlock (&write_lock);
773 (void) plugin_set_ctx (q->ctx);
778 } /* }}} value_list_t *plugin_write_dequeue */
780 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
784 value_list_t *vl = plugin_write_dequeue ();
788 plugin_dispatch_values_internal (vl);
790 plugin_value_list_free (vl);
795 } /* }}} void *plugin_write_thread */
797 static void start_write_threads (size_t num) /* {{{ */
801 if (write_threads != NULL)
804 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
805 if (write_threads == NULL)
807 ERROR ("plugin: start_write_threads: calloc failed.");
811 write_threads_num = 0;
812 for (i = 0; i < num; i++)
816 status = pthread_create (write_threads + write_threads_num,
823 ERROR ("plugin: start_write_threads: pthread_create failed "
824 "with status %i (%s).", status,
825 sstrerror (status, errbuf, sizeof (errbuf)));
831 } /* }}} void start_write_threads */
833 static void stop_write_threads (void) /* {{{ */
838 if (write_threads == NULL)
841 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
843 pthread_mutex_lock (&write_lock);
845 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
846 pthread_cond_broadcast (&write_cond);
847 pthread_mutex_unlock (&write_lock);
849 for (i = 0; i < write_threads_num; i++)
851 if (pthread_join (write_threads[i], NULL) != 0)
853 ERROR ("plugin: stop_write_threads: pthread_join failed.");
855 write_threads[i] = (pthread_t) 0;
857 sfree (write_threads);
858 write_threads_num = 0;
860 pthread_mutex_lock (&write_lock);
862 for (q = write_queue_head; q != NULL; )
864 write_queue_t *q1 = q;
865 plugin_value_list_free (q->vl);
870 write_queue_head = NULL;
871 write_queue_tail = NULL;
872 write_queue_length = 0;
873 pthread_mutex_unlock (&write_lock);
877 WARNING ("plugin: %i value list%s left after shutting down "
878 "the write threads.",
879 i, (i == 1) ? " was" : "s were");
881 } /* }}} void stop_write_threads */
886 void plugin_set_dir (const char *dir)
888 if (plugindir != NULL)
893 else if ((plugindir = strdup (dir)) == NULL)
896 ERROR ("strdup failed: %s",
897 sstrerror (errno, errbuf, sizeof (errbuf)));
901 static _Bool plugin_is_loaded (char const *name)
905 if (plugins_loaded == NULL)
906 plugins_loaded = c_avl_create ((void *) strcasecmp);
907 assert (plugins_loaded != NULL);
909 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
910 return (status == 0);
913 static int plugin_mark_loaded (char const *name)
918 name_copy = strdup (name);
919 if (name_copy == NULL)
922 status = c_avl_insert (plugins_loaded,
923 /* key = */ name_copy, /* value = */ NULL);
927 static void plugin_free_loaded ()
932 if (plugins_loaded == NULL)
935 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
938 assert (value == NULL);
941 c_avl_destroy (plugins_loaded);
942 plugins_loaded = NULL;
946 int plugin_load (char const *plugin_name, uint32_t flags)
950 char filename[BUFSIZE] = "";
951 char typename[BUFSIZE];
958 if (plugin_name == NULL)
961 /* Check if plugin is already loaded and don't do anything in this
963 if (plugin_is_loaded (plugin_name))
966 dir = plugin_get_dir ();
970 * XXX: Magic at work:
972 * Some of the language bindings, for example the Python and Perl
973 * plugins, need to be able to export symbols to the scripts they run.
974 * For this to happen, the "Globals" flag needs to be set.
975 * Unfortunately, this technical detail is hard to explain to the
976 * average user and she shouldn't have to worry about this, ideally.
977 * So in order to save everyone's sanity use a different default for a
978 * handful of special plugins. --octo
980 if ((strcasecmp ("perl", plugin_name) == 0)
981 || (strcasecmp ("python", plugin_name) == 0))
982 flags |= PLUGIN_FLAGS_GLOBAL;
984 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
985 * type when matching the filename */
986 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
987 if ((status < 0) || ((size_t) status >= sizeof (typename)))
989 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
992 typename_len = strlen (typename);
994 if ((dh = opendir (dir)) == NULL)
997 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
998 sstrerror (errno, errbuf, sizeof (errbuf)));
1002 while ((de = readdir (dh)) != NULL)
1004 if (strncasecmp (de->d_name, typename, typename_len))
1007 status = ssnprintf (filename, sizeof (filename),
1008 "%s/%s", dir, de->d_name);
1009 if ((status < 0) || ((size_t) status >= sizeof (filename)))
1011 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
1016 if (lstat (filename, &statbuf) == -1)
1019 WARNING ("plugin_load: stat (\"%s\") failed: %s",
1021 sstrerror (errno, errbuf, sizeof (errbuf)));
1024 else if (!S_ISREG (statbuf.st_mode))
1026 /* don't follow symlinks */
1027 WARNING ("plugin_load: %s is not a regular file.",
1032 status = plugin_load_file (filename, flags);
1036 plugin_mark_loaded (plugin_name);
1042 ERROR ("plugin_load: Load plugin \"%s\" failed with "
1043 "status %i.", plugin_name, status);
1049 if (filename[0] == 0)
1050 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1057 * The `register_*' functions follow
1059 int plugin_register_config (const char *name,
1060 int (*callback) (const char *key, const char *val),
1061 const char **keys, int keys_num)
1063 cf_register (name, callback, keys, keys_num);
1065 } /* int plugin_register_config */
1067 int plugin_register_complex_config (const char *type,
1068 int (*callback) (oconfig_item_t *))
1070 return (cf_register_complex (type, callback));
1071 } /* int plugin_register_complex_config */
1073 int plugin_register_init (const char *name,
1074 int (*callback) (void))
1076 return (create_register_callback (&list_init, name, (void *) callback,
1077 /* user_data = */ NULL));
1078 } /* plugin_register_init */
1080 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1082 const read_func_t *rf0;
1083 const read_func_t *rf1;
1088 if (rf0->rf_next_read < rf1->rf_next_read)
1090 else if (rf0->rf_next_read > rf1->rf_next_read)
1094 } /* int plugin_compare_read_func */
1096 /* Add a read function to both, the heap and a linked list. The linked list if
1097 * used to look-up read functions, especially for the remove function. The heap
1098 * is used to determine which plugin to read next. */
1099 static int plugin_insert_read (read_func_t *rf)
1104 rf->rf_next_read = cdtime ();
1105 rf->rf_effective_interval = rf->rf_interval;
1107 pthread_mutex_lock (&read_lock);
1109 if (read_list == NULL)
1111 read_list = llist_create ();
1112 if (read_list == NULL)
1114 pthread_mutex_unlock (&read_lock);
1115 ERROR ("plugin_insert_read: read_list failed.");
1120 if (read_heap == NULL)
1122 read_heap = c_heap_create (plugin_compare_read_func);
1123 if (read_heap == NULL)
1125 pthread_mutex_unlock (&read_lock);
1126 ERROR ("plugin_insert_read: c_heap_create failed.");
1131 le = llist_search (read_list, rf->rf_name);
1134 pthread_mutex_unlock (&read_lock);
1135 WARNING ("The read function \"%s\" is already registered. "
1136 "Check for duplicate \"LoadPlugin\" lines "
1137 "in your configuration!",
1142 le = llentry_create (rf->rf_name, rf);
1145 pthread_mutex_unlock (&read_lock);
1146 ERROR ("plugin_insert_read: llentry_create failed.");
1150 status = c_heap_insert (read_heap, rf);
1153 pthread_mutex_unlock (&read_lock);
1154 ERROR ("plugin_insert_read: c_heap_insert failed.");
1155 llentry_destroy (le);
1159 /* This does not fail. */
1160 llist_append (read_list, le);
1162 /* Wake up all the read threads. */
1163 pthread_cond_broadcast (&read_cond);
1164 pthread_mutex_unlock (&read_lock);
1166 } /* int plugin_insert_read */
1168 int plugin_register_read (const char *name,
1169 int (*callback) (void))
1174 rf = malloc (sizeof (*rf));
1177 ERROR ("plugin_register_read: malloc failed.");
1181 memset (rf, 0, sizeof (read_func_t));
1182 rf->rf_callback = (void *) callback;
1183 rf->rf_udata.data = NULL;
1184 rf->rf_udata.free_func = NULL;
1185 rf->rf_ctx = plugin_get_ctx ();
1186 rf->rf_group[0] = '\0';
1187 rf->rf_name = strdup (name);
1188 rf->rf_type = RF_SIMPLE;
1189 rf->rf_interval = plugin_get_interval ();
1191 status = plugin_insert_read (rf);
1196 } /* int plugin_register_read */
1198 int plugin_register_complex_read (const char *group, const char *name,
1199 plugin_read_cb callback,
1200 const struct timespec *interval,
1201 user_data_t *user_data)
1206 rf = malloc (sizeof (*rf));
1209 ERROR ("plugin_register_complex_read: malloc failed.");
1213 memset (rf, 0, sizeof (read_func_t));
1214 rf->rf_callback = (void *) callback;
1216 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1218 rf->rf_group[0] = '\0';
1219 rf->rf_name = strdup (name);
1220 rf->rf_type = RF_COMPLEX;
1221 if (interval != NULL)
1222 rf->rf_interval = TIMESPEC_TO_CDTIME_T (interval);
1224 rf->rf_interval = plugin_get_interval ();
1227 if (user_data == NULL)
1229 rf->rf_udata.data = NULL;
1230 rf->rf_udata.free_func = NULL;
1234 rf->rf_udata = *user_data;
1237 rf->rf_ctx = plugin_get_ctx ();
1239 status = plugin_insert_read (rf);
1244 } /* int plugin_register_complex_read */
1246 int plugin_register_write (const char *name,
1247 plugin_write_cb callback, user_data_t *ud)
1249 return (create_register_callback (&list_write, name,
1250 (void *) callback, ud));
1251 } /* int plugin_register_write */
1253 int plugin_register_flush (const char *name,
1254 plugin_flush_cb callback, user_data_t *ud)
1256 return (create_register_callback (&list_flush, name,
1257 (void *) callback, ud));
1258 } /* int plugin_register_flush */
1260 int plugin_register_missing (const char *name,
1261 plugin_missing_cb callback, user_data_t *ud)
1263 return (create_register_callback (&list_missing, name,
1264 (void *) callback, ud));
1265 } /* int plugin_register_missing */
1267 int plugin_register_shutdown (const char *name,
1268 int (*callback) (void))
1270 return (create_register_callback (&list_shutdown, name,
1271 (void *) callback, /* user_data = */ NULL));
1272 } /* int plugin_register_shutdown */
1274 static void plugin_free_data_sets (void)
1279 if (data_sets == NULL)
1282 while (c_avl_pick (data_sets, &key, &value) == 0)
1284 data_set_t *ds = value;
1285 /* key is a pointer to ds->type */
1291 c_avl_destroy (data_sets);
1293 } /* void plugin_free_data_sets */
1295 int plugin_register_data_set (const data_set_t *ds)
1297 data_set_t *ds_copy;
1300 if ((data_sets != NULL)
1301 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1303 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1304 plugin_unregister_data_set (ds->type);
1306 else if (data_sets == NULL)
1308 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1309 if (data_sets == NULL)
1313 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1314 if (ds_copy == NULL)
1316 memcpy(ds_copy, ds, sizeof (data_set_t));
1318 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1320 if (ds_copy->ds == NULL)
1326 for (i = 0; i < ds->ds_num; i++)
1327 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1329 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1330 } /* int plugin_register_data_set */
1332 int plugin_register_log (const char *name,
1333 plugin_log_cb callback, user_data_t *ud)
1335 return (create_register_callback (&list_log, name,
1336 (void *) callback, ud));
1337 } /* int plugin_register_log */
1339 int plugin_register_notification (const char *name,
1340 plugin_notification_cb callback, user_data_t *ud)
1342 return (create_register_callback (&list_notification, name,
1343 (void *) callback, ud));
1344 } /* int plugin_register_log */
1346 int plugin_unregister_config (const char *name)
1348 cf_unregister (name);
1350 } /* int plugin_unregister_config */
1352 int plugin_unregister_complex_config (const char *name)
1354 cf_unregister_complex (name);
1356 } /* int plugin_unregister_complex_config */
1358 int plugin_unregister_init (const char *name)
1360 return (plugin_unregister (list_init, name));
1363 int plugin_unregister_read (const char *name) /* {{{ */
1371 pthread_mutex_lock (&read_lock);
1373 if (read_list == NULL)
1375 pthread_mutex_unlock (&read_lock);
1379 le = llist_search (read_list, name);
1382 pthread_mutex_unlock (&read_lock);
1383 WARNING ("plugin_unregister_read: No such read function: %s",
1388 llist_remove (read_list, le);
1391 assert (rf != NULL);
1392 rf->rf_type = RF_REMOVE;
1394 pthread_mutex_unlock (&read_lock);
1396 llentry_destroy (le);
1398 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1401 } /* }}} int plugin_unregister_read */
1403 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1405 read_func_t *rf = e->value;
1408 return strcmp (rf->rf_group, (const char *)group);
1409 } /* }}} int compare_read_func_group */
1411 int plugin_unregister_read_group (const char *group) /* {{{ */
1421 pthread_mutex_lock (&read_lock);
1423 if (read_list == NULL)
1425 pthread_mutex_unlock (&read_lock);
1431 le = llist_search_custom (read_list,
1432 compare_read_func_group, (void *)group);
1439 llist_remove (read_list, le);
1442 assert (rf != NULL);
1443 rf->rf_type = RF_REMOVE;
1445 llentry_destroy (le);
1447 DEBUG ("plugin_unregister_read_group: "
1448 "Marked `%s' (group `%s') for removal.",
1449 rf->rf_name, group);
1452 pthread_mutex_unlock (&read_lock);
1456 WARNING ("plugin_unregister_read_group: No such "
1457 "group of read function: %s", group);
1462 } /* }}} int plugin_unregister_read_group */
1464 int plugin_unregister_write (const char *name)
1466 return (plugin_unregister (list_write, name));
1469 int plugin_unregister_flush (const char *name)
1471 return (plugin_unregister (list_flush, name));
1474 int plugin_unregister_missing (const char *name)
1476 return (plugin_unregister (list_missing, name));
1479 int plugin_unregister_shutdown (const char *name)
1481 return (plugin_unregister (list_shutdown, name));
1484 int plugin_unregister_data_set (const char *name)
1488 if (data_sets == NULL)
1491 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1498 } /* int plugin_unregister_data_set */
1500 int plugin_unregister_log (const char *name)
1502 return (plugin_unregister (list_log, name));
1505 int plugin_unregister_notification (const char *name)
1507 return (plugin_unregister (list_notification, name));
1510 void plugin_init_all (void)
1512 char const *chain_name;
1513 long write_threads_num;
1517 /* Init the value cache */
1520 if (IS_TRUE (global_option_get ("CollectInternalStats")))
1521 record_statistics = 1;
1523 chain_name = global_option_get ("PreCacheChain");
1524 pre_cache_chain = fc_chain_get_by_name (chain_name);
1526 chain_name = global_option_get ("PostCacheChain");
1527 post_cache_chain = fc_chain_get_by_name (chain_name);
1529 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1531 if (write_limit_high < 0)
1533 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1534 write_limit_high = 0;
1537 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1538 /* default = */ write_limit_high / 2);
1539 if (write_limit_low < 0)
1541 ERROR ("WriteQueueLimitLow must be positive or zero.");
1542 write_limit_low = write_limit_high / 2;
1544 else if (write_limit_low > write_limit_high)
1546 ERROR ("WriteQueueLimitLow must not be larger than "
1547 "WriteQueueLimitHigh.");
1548 write_limit_low = write_limit_high;
1551 write_threads_num = global_option_get_long ("WriteThreads",
1553 if (write_threads_num < 1)
1555 ERROR ("WriteThreads must be positive.");
1556 write_threads_num = 5;
1559 start_write_threads ((size_t) write_threads_num);
1561 if ((list_init == NULL) && (read_heap == NULL))
1564 /* Calling all init callbacks before checking if read callbacks
1565 * are available allows the init callbacks to register the read
1567 le = llist_head (list_init);
1570 callback_func_t *cf;
1571 plugin_init_cb callback;
1572 plugin_ctx_t old_ctx;
1575 old_ctx = plugin_set_ctx (cf->cf_ctx);
1576 callback = cf->cf_callback;
1577 status = (*callback) ();
1578 plugin_set_ctx (old_ctx);
1582 ERROR ("Initialization of plugin `%s' "
1583 "failed with status %i. "
1584 "Plugin will be unloaded.",
1586 /* Plugins that register read callbacks from the init
1587 * callback should take care of appropriate error
1588 * handling themselves. */
1589 /* FIXME: Unload _all_ functions */
1590 plugin_unregister_read (le->key);
1596 max_read_interval = global_option_get_time ("MaxReadInterval",
1597 DEFAULT_MAX_READ_INTERVAL);
1599 /* Start read-threads */
1600 if (read_heap != NULL)
1605 rt = global_option_get ("ReadThreads");
1608 start_read_threads ((num > 0) ? num : 5);
1610 } /* void plugin_init_all */
1612 /* TODO: Rename this function. */
1613 void plugin_read_all (void)
1615 if(record_statistics) {
1616 plugin_update_internal_statistics ();
1618 uc_check_timeout ();
1621 } /* void plugin_read_all */
1623 /* Read function called when the `-T' command line argument is given. */
1624 int plugin_read_all_once (void)
1627 int return_status = 0;
1629 if (read_heap == NULL)
1631 NOTICE ("No read-functions are registered.");
1638 plugin_ctx_t old_ctx;
1640 rf = c_heap_get_root (read_heap);
1644 old_ctx = plugin_set_ctx (rf->rf_ctx);
1646 if (rf->rf_type == RF_SIMPLE)
1648 int (*callback) (void);
1650 callback = rf->rf_callback;
1651 status = (*callback) ();
1655 plugin_read_cb callback;
1657 callback = rf->rf_callback;
1658 status = (*callback) (&rf->rf_udata);
1661 plugin_set_ctx (old_ctx);
1665 NOTICE ("read-function of plugin `%s' failed.",
1670 destroy_callback ((void *) rf);
1673 return (return_status);
1674 } /* int plugin_read_all_once */
1676 int plugin_write (const char *plugin, /* {{{ */
1677 const data_set_t *ds, const value_list_t *vl)
1685 if (list_write == NULL)
1690 ds = plugin_get_ds (vl->type);
1693 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1703 le = llist_head (list_write);
1706 callback_func_t *cf = le->value;
1707 plugin_write_cb callback;
1709 /* do not switch plugin context; rather keep the context (interval)
1710 * information of the calling read plugin */
1712 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1713 callback = cf->cf_callback;
1714 status = (*callback) (ds, vl, &cf->cf_udata);
1723 if ((success == 0) && (failure != 0))
1728 else /* plugin != NULL */
1730 callback_func_t *cf;
1731 plugin_write_cb callback;
1733 le = llist_head (list_write);
1736 if (strcasecmp (plugin, le->key) == 0)
1747 /* do not switch plugin context; rather keep the context (interval)
1748 * information of the calling read plugin */
1750 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1751 callback = cf->cf_callback;
1752 status = (*callback) (ds, vl, &cf->cf_udata);
1756 } /* }}} int plugin_write */
1758 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1762 if (list_flush == NULL)
1765 le = llist_head (list_flush);
1768 callback_func_t *cf;
1769 plugin_flush_cb callback;
1770 plugin_ctx_t old_ctx;
1772 if ((plugin != NULL)
1773 && (strcmp (plugin, le->key) != 0))
1780 old_ctx = plugin_set_ctx (cf->cf_ctx);
1781 callback = cf->cf_callback;
1783 (*callback) (timeout, identifier, &cf->cf_udata);
1785 plugin_set_ctx (old_ctx);
1790 } /* int plugin_flush */
1792 void plugin_shutdown_all (void)
1796 stop_read_threads ();
1798 destroy_all_callbacks (&list_init);
1800 pthread_mutex_lock (&read_lock);
1801 llist_destroy (read_list);
1803 pthread_mutex_unlock (&read_lock);
1805 destroy_read_heap ();
1807 plugin_flush (/* plugin = */ NULL,
1809 /* identifier = */ NULL);
1812 if (list_shutdown != NULL)
1813 le = llist_head (list_shutdown);
1817 callback_func_t *cf;
1818 plugin_shutdown_cb callback;
1819 plugin_ctx_t old_ctx;
1822 old_ctx = plugin_set_ctx (cf->cf_ctx);
1823 callback = cf->cf_callback;
1825 /* Advance the pointer before calling the callback allows
1826 * shutdown functions to unregister themselves. If done the
1827 * other way around the memory `le' points to will be freed
1828 * after callback returns. */
1833 plugin_set_ctx (old_ctx);
1836 stop_write_threads ();
1838 /* Write plugins which use the `user_data' pointer usually need the
1839 * same data available to the flush callback. If this is the case, set
1840 * the free_function to NULL when registering the flush callback and to
1841 * the real free function when registering the write callback. This way
1842 * the data isn't freed twice. */
1843 destroy_all_callbacks (&list_flush);
1844 destroy_all_callbacks (&list_missing);
1845 destroy_all_callbacks (&list_write);
1847 destroy_all_callbacks (&list_notification);
1848 destroy_all_callbacks (&list_shutdown);
1849 destroy_all_callbacks (&list_log);
1851 plugin_free_loaded ();
1852 plugin_free_data_sets ();
1853 } /* void plugin_shutdown_all */
1855 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1859 if (list_missing == NULL)
1862 le = llist_head (list_missing);
1865 callback_func_t *cf;
1866 plugin_missing_cb callback;
1867 plugin_ctx_t old_ctx;
1871 old_ctx = plugin_set_ctx (cf->cf_ctx);
1872 callback = cf->cf_callback;
1874 status = (*callback) (vl, &cf->cf_udata);
1875 plugin_set_ctx (old_ctx);
1880 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1881 "failed with status %i.",
1894 } /* int }}} plugin_dispatch_missing */
1896 static int plugin_dispatch_values_internal (value_list_t *vl)
1899 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1901 value_t *saved_values;
1902 int saved_values_len;
1906 int free_meta_data = 0;
1908 if ((vl == NULL) || (vl->type[0] == 0)
1909 || (vl->values == NULL) || (vl->values_len < 1))
1911 ERROR ("plugin_dispatch_values: Invalid value list "
1912 "from plugin %s.", vl->plugin);
1916 /* Free meta data only if the calling function didn't specify any. In
1917 * this case matches and targets may add some and the calling function
1918 * may not expect (and therefore free) that data. */
1919 if (vl->meta == NULL)
1922 if (list_write == NULL)
1923 c_complain_once (LOG_WARNING, &no_write_complaint,
1924 "plugin_dispatch_values: No write callback has been "
1925 "registered. Please load at least one output plugin, "
1926 "if you want the collected data to be stored.");
1928 if (data_sets == NULL)
1930 ERROR ("plugin_dispatch_values: No data sets registered. "
1931 "Could the types database be read? Check "
1932 "your `TypesDB' setting!");
1936 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1938 char ident[6 * DATA_MAX_NAME_LEN];
1940 FORMAT_VL (ident, sizeof (ident), vl);
1941 INFO ("plugin_dispatch_values: Dataset not found: %s "
1942 "(from \"%s\"), check your types.db!",
1947 /* Assured by plugin_value_list_clone(). The time is determined at
1948 * _enqueue_ time. */
1949 assert (vl->time != 0);
1950 assert (vl->interval != 0);
1952 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1954 "plugin = %s; plugin_instance = %s; "
1955 "type = %s; type_instance = %s;",
1956 CDTIME_T_TO_DOUBLE (vl->time),
1957 CDTIME_T_TO_DOUBLE (vl->interval),
1959 vl->plugin, vl->plugin_instance,
1960 vl->type, vl->type_instance);
1963 assert (0 == strcmp (ds->type, vl->type));
1965 if (0 != strcmp (ds->type, vl->type))
1966 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1967 ds->type, vl->type);
1971 assert (ds->ds_num == vl->values_len);
1973 if (ds->ds_num != vl->values_len)
1975 ERROR ("plugin_dispatch_values: ds->type = %s: "
1976 "(ds->ds_num = %i) != "
1977 "(vl->values_len = %i)",
1978 ds->type, ds->ds_num, vl->values_len);
1983 escape_slashes (vl->host, sizeof (vl->host));
1984 escape_slashes (vl->plugin, sizeof (vl->plugin));
1985 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1986 escape_slashes (vl->type, sizeof (vl->type));
1987 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1989 /* Copy the values. This way, we can assure `targets' that they get
1990 * dynamically allocated values, which they can free and replace if
1992 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1994 saved_values = vl->values;
1995 saved_values_len = vl->values_len;
1997 vl->values = (value_t *) calloc (vl->values_len,
1998 sizeof (*vl->values));
1999 if (vl->values == NULL)
2001 ERROR ("plugin_dispatch_values: calloc failed.");
2002 vl->values = saved_values;
2005 memcpy (vl->values, saved_values,
2006 vl->values_len * sizeof (*vl->values));
2008 else /* if ((pre == NULL) && (post == NULL)) */
2010 saved_values = NULL;
2011 saved_values_len = 0;
2014 if (pre_cache_chain != NULL)
2016 status = fc_process_chain (ds, vl, pre_cache_chain);
2019 WARNING ("plugin_dispatch_values: Running the "
2020 "pre-cache chain failed with "
2024 else if (status == FC_TARGET_STOP)
2026 /* Restore the state of the value_list so that plugins
2027 * don't get confused.. */
2028 if (saved_values != NULL)
2031 vl->values = saved_values;
2032 vl->values_len = saved_values_len;
2038 /* Update the value cache */
2041 if (post_cache_chain != NULL)
2043 status = fc_process_chain (ds, vl, post_cache_chain);
2046 WARNING ("plugin_dispatch_values: Running the "
2047 "post-cache chain failed with "
2053 fc_default_action (ds, vl);
2055 /* Restore the state of the value_list so that plugins don't get
2057 if (saved_values != NULL)
2060 vl->values = saved_values;
2061 vl->values_len = saved_values_len;
2064 if ((free_meta_data != 0) && (vl->meta != NULL))
2066 meta_data_destroy (vl->meta);
2071 } /* int plugin_dispatch_values_internal */
2073 static double get_drop_probability (void) /* {{{ */
2079 pthread_mutex_lock (&write_lock);
2080 wql = write_queue_length;
2081 pthread_mutex_unlock (&write_lock);
2083 if (wql < write_limit_low)
2085 if (wql >= write_limit_high)
2088 pos = 1 + wql - write_limit_low;
2089 size = 1 + write_limit_high - write_limit_low;
2091 return (((double) pos) / ((double) size));
2092 } /* }}} double get_drop_probability */
2094 static _Bool check_drop_value (void) /* {{{ */
2096 static cdtime_t last_message_time = 0;
2097 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2103 if (write_limit_high == 0)
2106 p = get_drop_probability ();
2110 status = pthread_mutex_trylock (&last_message_lock);
2116 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2118 last_message_time = now;
2119 ERROR ("plugin_dispatch_values: Low water mark "
2120 "reached. Dropping %.0f%% of metrics.",
2123 pthread_mutex_unlock (&last_message_lock);
2134 } /* }}} _Bool check_drop_value */
2136 int plugin_dispatch_values (value_list_t const *vl)
2139 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2141 if (check_drop_value ()) {
2142 if(record_statistics) {
2143 pthread_mutex_lock(&statistics_lock);
2144 stats_values_dropped++;
2145 pthread_mutex_unlock(&statistics_lock);
2150 status = plugin_write_enqueue (vl);
2154 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2155 "with status %i (%s).", status,
2156 sstrerror (status, errbuf, sizeof (errbuf)));
2163 __attribute__((sentinel))
2164 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2165 _Bool store_percentage, int store_type, ...)
2172 assert (template->values_len == 1);
2174 /* Calculate sum for Gauge to calculate percent if needed */
2175 if (DS_TYPE_GAUGE == store_type) {
2176 va_start (ap, store_type);
2182 name = va_arg (ap, char const *);
2186 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_type);
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. */
2215 vl->values[0].gauge = va_arg (ap, gauge_t);
2216 if (store_percentage)
2217 vl->values[0].gauge *= 100.0 / sum;
2219 case DS_TYPE_ABSOLUTE:
2220 vl->values[0].absolute = va_arg (ap, absolute_t);
2222 case DS_TYPE_COUNTER:
2223 vl->values[0].counter = va_arg (ap, counter_t);
2225 case DS_TYPE_DERIVE:
2226 vl->values[0].derive = va_arg (ap, derive_t);
2229 ERROR ("plugin_dispatch_multivalue: given store_type is incorrect.");
2234 status = plugin_write_enqueue (vl);
2240 plugin_value_list_free (vl);
2242 } /* }}} int plugin_dispatch_multivalue */
2244 int plugin_dispatch_notification (const notification_t *notif)
2247 /* Possible TODO: Add flap detection here */
2249 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2250 "time = %.3f; host = %s;",
2251 notif->severity, notif->message,
2252 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2254 /* Nobody cares for notifications */
2255 if (list_notification == NULL)
2258 le = llist_head (list_notification);
2261 callback_func_t *cf;
2262 plugin_notification_cb callback;
2265 /* do not switch plugin context; rather keep the context
2266 * (interval) information of the calling plugin */
2269 callback = cf->cf_callback;
2270 status = (*callback) (notif, &cf->cf_udata);
2273 WARNING ("plugin_dispatch_notification: Notification "
2274 "callback %s returned %i.",
2282 } /* int plugin_dispatch_notification */
2284 void plugin_log (int level, const char *format, ...)
2291 if (level >= LOG_DEBUG)
2295 va_start (ap, format);
2296 vsnprintf (msg, sizeof (msg), format, ap);
2297 msg[sizeof (msg) - 1] = '\0';
2300 if (list_log == NULL)
2302 fprintf (stderr, "%s\n", msg);
2306 le = llist_head (list_log);
2309 callback_func_t *cf;
2310 plugin_log_cb callback;
2313 callback = cf->cf_callback;
2315 /* do not switch plugin context; rather keep the context
2316 * (interval) information of the calling plugin */
2318 (*callback) (level, msg, &cf->cf_udata);
2322 } /* void plugin_log */
2324 int parse_log_severity (const char *severity)
2328 if ((0 == strcasecmp (severity, "emerg"))
2329 || (0 == strcasecmp (severity, "alert"))
2330 || (0 == strcasecmp (severity, "crit"))
2331 || (0 == strcasecmp (severity, "err")))
2332 log_level = LOG_ERR;
2333 else if (0 == strcasecmp (severity, "warning"))
2334 log_level = LOG_WARNING;
2335 else if (0 == strcasecmp (severity, "notice"))
2336 log_level = LOG_NOTICE;
2337 else if (0 == strcasecmp (severity, "info"))
2338 log_level = LOG_INFO;
2340 else if (0 == strcasecmp (severity, "debug"))
2341 log_level = LOG_DEBUG;
2342 #endif /* COLLECT_DEBUG */
2345 } /* int parse_log_severity */
2347 int parse_notif_severity (const char *severity)
2349 int notif_severity = -1;
2351 if (strcasecmp (severity, "FAILURE") == 0)
2352 notif_severity = NOTIF_FAILURE;
2353 else if (strcmp (severity, "OKAY") == 0)
2354 notif_severity = NOTIF_OKAY;
2355 else if ((strcmp (severity, "WARNING") == 0)
2356 || (strcmp (severity, "WARN") == 0))
2357 notif_severity = NOTIF_WARNING;
2359 return (notif_severity);
2360 } /* int parse_notif_severity */
2362 const data_set_t *plugin_get_ds (const char *name)
2366 if (data_sets == NULL)
2368 ERROR ("plugin_get_ds: No data sets are defined yet.");
2372 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2374 DEBUG ("No such dataset registered: %s", name);
2379 } /* data_set_t *plugin_get_ds */
2381 static int plugin_notification_meta_add (notification_t *n,
2383 enum notification_meta_type_e type,
2386 notification_meta_t *meta;
2387 notification_meta_t *tail;
2389 if ((n == NULL) || (name == NULL) || (value == NULL))
2391 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2395 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2398 ERROR ("plugin_notification_meta_add: malloc failed.");
2401 memset (meta, 0, sizeof (notification_meta_t));
2403 sstrncpy (meta->name, name, sizeof (meta->name));
2408 case NM_TYPE_STRING:
2410 meta->nm_value.nm_string = strdup ((const char *) value);
2411 if (meta->nm_value.nm_string == NULL)
2413 ERROR ("plugin_notification_meta_add: strdup failed.");
2419 case NM_TYPE_SIGNED_INT:
2421 meta->nm_value.nm_signed_int = *((int64_t *) value);
2424 case NM_TYPE_UNSIGNED_INT:
2426 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2429 case NM_TYPE_DOUBLE:
2431 meta->nm_value.nm_double = *((double *) value);
2434 case NM_TYPE_BOOLEAN:
2436 meta->nm_value.nm_boolean = *((_Bool *) value);
2441 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2445 } /* switch (type) */
2449 while ((tail != NULL) && (tail->next != NULL))
2458 } /* int plugin_notification_meta_add */
2460 int plugin_notification_meta_add_string (notification_t *n,
2464 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2467 int plugin_notification_meta_add_signed_int (notification_t *n,
2471 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2474 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2478 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2481 int plugin_notification_meta_add_double (notification_t *n,
2485 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2488 int plugin_notification_meta_add_boolean (notification_t *n,
2492 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2495 int plugin_notification_meta_copy (notification_t *dst,
2496 const notification_t *src)
2498 notification_meta_t *meta;
2500 assert (dst != NULL);
2501 assert (src != NULL);
2502 assert (dst != src);
2503 assert ((src->meta == NULL) || (src->meta != dst->meta));
2505 for (meta = src->meta; meta != NULL; meta = meta->next)
2507 if (meta->type == NM_TYPE_STRING)
2508 plugin_notification_meta_add_string (dst, meta->name,
2509 meta->nm_value.nm_string);
2510 else if (meta->type == NM_TYPE_SIGNED_INT)
2511 plugin_notification_meta_add_signed_int (dst, meta->name,
2512 meta->nm_value.nm_signed_int);
2513 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2514 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2515 meta->nm_value.nm_unsigned_int);
2516 else if (meta->type == NM_TYPE_DOUBLE)
2517 plugin_notification_meta_add_double (dst, meta->name,
2518 meta->nm_value.nm_double);
2519 else if (meta->type == NM_TYPE_BOOLEAN)
2520 plugin_notification_meta_add_boolean (dst, meta->name,
2521 meta->nm_value.nm_boolean);
2525 } /* int plugin_notification_meta_copy */
2527 int plugin_notification_meta_free (notification_meta_t *n)
2529 notification_meta_t *this;
2530 notification_meta_t *next;
2534 ERROR ("plugin_notification_meta_free: n == NULL!");
2539 while (this != NULL)
2543 if (this->type == NM_TYPE_STRING)
2545 free ((char *)this->nm_value.nm_string);
2546 this->nm_value.nm_string = NULL;
2554 } /* int plugin_notification_meta_free */
2556 static void plugin_ctx_destructor (void *ctx)
2559 } /* void plugin_ctx_destructor */
2561 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2563 static plugin_ctx_t *plugin_ctx_create (void)
2567 ctx = malloc (sizeof (*ctx));
2570 ERROR ("Failed to allocate plugin context: %s",
2571 sstrerror (errno, errbuf, sizeof (errbuf)));
2576 assert (plugin_ctx_key_initialized);
2577 pthread_setspecific (plugin_ctx_key, ctx);
2578 DEBUG("Created new plugin context.");
2580 } /* int plugin_ctx_create */
2582 void plugin_init_ctx (void)
2584 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2585 plugin_ctx_key_initialized = 1;
2586 } /* void plugin_init_ctx */
2588 plugin_ctx_t plugin_get_ctx (void)
2592 assert (plugin_ctx_key_initialized);
2593 ctx = pthread_getspecific (plugin_ctx_key);
2596 ctx = plugin_ctx_create ();
2597 /* this must no happen -- exit() instead? */
2603 } /* plugin_ctx_t plugin_get_ctx */
2605 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2610 assert (plugin_ctx_key_initialized);
2611 c = pthread_getspecific (plugin_ctx_key);
2614 c = plugin_ctx_create ();
2615 /* this must no happen -- exit() instead? */
2624 } /* void plugin_set_ctx */
2626 cdtime_t plugin_get_interval (void)
2630 interval = plugin_get_ctx().interval;
2634 return cf_get_default_interval ();
2635 } /* cdtime_t plugin_get_interval */
2639 void *(*start_routine) (void *);
2643 static void *plugin_thread_start (void *arg)
2645 plugin_thread_t *plugin_thread = arg;
2647 void *(*start_routine) (void *) = plugin_thread->start_routine;
2648 void *plugin_arg = plugin_thread->arg;
2650 plugin_set_ctx (plugin_thread->ctx);
2652 free (plugin_thread);
2654 return start_routine (plugin_arg);
2655 } /* void *plugin_thread_start */
2657 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2658 void *(*start_routine) (void *), void *arg)
2660 plugin_thread_t *plugin_thread;
2662 plugin_thread = malloc (sizeof (*plugin_thread));
2663 if (plugin_thread == NULL)
2666 plugin_thread->ctx = plugin_get_ctx ();
2667 plugin_thread->start_routine = start_routine;
2668 plugin_thread->arg = arg;
2670 return pthread_create (thread, attr,
2671 plugin_thread_start, plugin_thread);
2672 } /* int plugin_thread_create */
2674 /* vim: set sw=8 ts=8 noet fdm=marker : */