2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2013 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
20 * Sebastian Harl <sh at tokkee.org>
26 #include "configfile.h"
27 #include "filter_chain.h"
28 #include "utils_avltree.h"
29 #include "utils_cache.h"
30 #include "utils_complain.h"
31 #include "utils_llist.h"
32 #include "utils_heap.h"
33 #include "utils_time.h"
34 #include "utils_random.h"
45 struct callback_func_s
51 typedef struct callback_func_s callback_func_t;
55 #define RF_REMOVE 65535
58 /* `read_func_t' "inherits" from `callback_func_t'.
59 * The `rf_super' member MUST be the first one in this structure! */
60 #define rf_callback rf_super.cf_callback
61 #define rf_udata rf_super.cf_udata
62 #define rf_ctx rf_super.cf_ctx
63 callback_func_t rf_super;
64 char rf_group[DATA_MAX_NAME_LEN];
68 cdtime_t rf_effective_interval;
69 cdtime_t rf_next_read;
71 typedef struct read_func_s read_func_t;
74 typedef struct write_queue_s write_queue_t;
85 static c_avl_tree_t *plugins_loaded = NULL;
87 static llist_t *list_init;
88 static llist_t *list_write;
89 static llist_t *list_flush;
90 static llist_t *list_missing;
91 static llist_t *list_shutdown;
92 static llist_t *list_log;
93 static llist_t *list_notification;
95 static fc_chain_t *pre_cache_chain = NULL;
96 static fc_chain_t *post_cache_chain = NULL;
98 static c_avl_tree_t *data_sets;
100 static char *plugindir = NULL;
102 static c_heap_t *read_heap = NULL;
103 static llist_t *read_list;
104 static int read_loop = 1;
105 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
106 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
107 static pthread_t *read_threads = NULL;
108 static int read_threads_num = 0;
110 static write_queue_t *write_queue_head;
111 static write_queue_t *write_queue_tail;
112 static long write_queue_length = 0;
113 static _Bool write_loop = 1;
114 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
115 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
116 static pthread_t *write_threads = NULL;
117 static size_t write_threads_num = 0;
119 static pthread_key_t plugin_ctx_key;
120 static _Bool plugin_ctx_key_initialized = 0;
122 static long write_limit_high = 0;
123 static long write_limit_low = 0;
128 static int plugin_dispatch_values_internal (value_list_t *vl);
130 static const char *plugin_get_dir (void)
132 if (plugindir == NULL)
138 static void destroy_callback (callback_func_t *cf) /* {{{ */
143 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
145 cf->cf_udata.free_func (cf->cf_udata.data);
146 cf->cf_udata.data = NULL;
147 cf->cf_udata.free_func = NULL;
150 } /* }}} void destroy_callback */
152 static void destroy_all_callbacks (llist_t **list) /* {{{ */
159 le = llist_head (*list);
167 destroy_callback (le->value);
173 llist_destroy (*list);
175 } /* }}} void destroy_all_callbacks */
177 static void destroy_read_heap (void) /* {{{ */
179 if (read_heap == NULL)
186 cf = c_heap_get_root (read_heap);
190 destroy_callback (cf);
193 c_heap_destroy (read_heap);
195 } /* }}} void destroy_read_heap */
197 static int register_callback (llist_t **list, /* {{{ */
198 const char *name, callback_func_t *cf)
205 *list = llist_create ();
208 ERROR ("plugin: register_callback: "
209 "llist_create failed.");
210 destroy_callback (cf);
218 ERROR ("plugin: register_callback: strdup failed.");
219 destroy_callback (cf);
223 le = llist_search (*list, name);
226 le = llentry_create (key, cf);
229 ERROR ("plugin: register_callback: "
230 "llentry_create failed.");
232 destroy_callback (cf);
236 llist_append (*list, le);
240 callback_func_t *old_cf;
245 WARNING ("plugin: register_callback: "
246 "a callback named `%s' already exists - "
247 "overwriting the old entry!", name);
249 destroy_callback (old_cf);
254 } /* }}} int register_callback */
256 static int create_register_callback (llist_t **list, /* {{{ */
257 const char *name, void *callback, user_data_t *ud)
261 cf = (callback_func_t *) malloc (sizeof (*cf));
264 ERROR ("plugin: create_register_callback: malloc failed.");
267 memset (cf, 0, sizeof (*cf));
269 cf->cf_callback = callback;
272 cf->cf_udata.data = NULL;
273 cf->cf_udata.free_func = NULL;
280 cf->cf_ctx = plugin_get_ctx ();
282 return (register_callback (list, name, cf));
283 } /* }}} int create_register_callback */
285 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
292 e = llist_search (list, name);
296 llist_remove (list, e);
299 destroy_callback (e->value);
304 } /* }}} int plugin_unregister */
307 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
308 * object, but it will bitch about a shared object not having a
309 * ``module_register'' symbol..
311 static int plugin_load_file (char *file, uint32_t flags)
314 void (*reg_handle) (void);
317 lt_dlerror (); /* clear errors */
319 #if LIBTOOL_VERSION == 2
320 if (flags & PLUGIN_FLAGS_GLOBAL) {
322 lt_dladvise_init(&advise);
323 lt_dladvise_global(&advise);
324 dlh = lt_dlopenadvise(file, advise);
325 lt_dladvise_destroy(&advise);
327 dlh = lt_dlopen (file);
329 #else /* if LIBTOOL_VERSION == 1 */
330 if (flags & PLUGIN_FLAGS_GLOBAL)
331 WARNING ("plugin_load_file: The global flag is not supported, "
332 "libtool 2 is required for this.");
333 dlh = lt_dlopen (file);
338 char errbuf[1024] = "";
340 ssnprintf (errbuf, sizeof (errbuf),
341 "lt_dlopen (\"%s\") failed: %s. "
342 "The most common cause for this problem are "
343 "missing dependencies. Use ldd(1) to check "
344 "the dependencies of the plugin "
346 file, lt_dlerror ());
348 ERROR ("%s", errbuf);
349 /* Make sure this is printed to STDERR in any case, but also
350 * make sure it's printed only once. */
351 if (list_log != NULL)
352 fprintf (stderr, "ERROR: %s\n", errbuf);
357 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
359 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
360 file, lt_dlerror ());
370 static void *plugin_read_thread (void __attribute__((unused)) *args)
372 while (read_loop != 0)
375 plugin_ctx_t old_ctx;
381 /* Get the read function that needs to be read next.
382 * We don't need to hold "read_lock" for the heap, but we need
383 * to call c_heap_get_root() and pthread_cond_wait() in the
384 * same protected block. */
385 pthread_mutex_lock (&read_lock);
386 rf = c_heap_get_root (read_heap);
389 pthread_cond_wait (&read_cond, &read_lock);
390 pthread_mutex_unlock (&read_lock);
393 pthread_mutex_unlock (&read_lock);
395 if (rf->rf_interval == 0)
397 /* this should not happen, because the interval is set
398 * for each plugin when loading it
399 * XXX: issue a warning? */
400 rf->rf_interval = plugin_get_interval ();
401 rf->rf_effective_interval = rf->rf_interval;
403 rf->rf_next_read = cdtime ();
406 /* sleep until this entry is due,
407 * using pthread_cond_timedwait */
408 pthread_mutex_lock (&read_lock);
409 /* In pthread_cond_timedwait, spurious wakeups are possible
410 * (and really happen, at least on NetBSD with > 1 CPU), thus
411 * we need to re-evaluate the condition every time
412 * pthread_cond_timedwait returns. */
414 while ((read_loop != 0)
415 && (cdtime () < rf->rf_next_read)
418 struct timespec ts = { 0 };
420 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
422 rc = pthread_cond_timedwait (&read_cond, &read_lock,
426 /* Must hold `read_lock' when accessing `rf->rf_type'. */
427 rf_type = rf->rf_type;
428 pthread_mutex_unlock (&read_lock);
430 /* Check if we're supposed to stop.. This may have interrupted
434 /* Insert `rf' again, so it can be free'd correctly */
435 c_heap_insert (read_heap, rf);
439 /* The entry has been marked for deletion. The linked list
440 * entry has already been removed by `plugin_unregister_read'.
441 * All we have to do here is free the `read_func_t' and
443 if (rf_type == RF_REMOVE)
445 DEBUG ("plugin_read_thread: Destroying the `%s' "
446 "callback.", rf->rf_name);
448 destroy_callback ((callback_func_t *) rf);
453 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
455 old_ctx = plugin_set_ctx (rf->rf_ctx);
457 if (rf_type == RF_SIMPLE)
459 int (*callback) (void);
461 callback = rf->rf_callback;
462 status = (*callback) ();
466 plugin_read_cb callback;
468 assert (rf_type == RF_COMPLEX);
470 callback = rf->rf_callback;
471 status = (*callback) (&rf->rf_udata);
474 plugin_set_ctx (old_ctx);
476 /* If the function signals failure, we will increase the
477 * intervals in which it will be called. */
480 rf->rf_effective_interval *= 2;
481 if (rf->rf_effective_interval > TIME_T_TO_CDTIME_T (86400))
482 rf->rf_effective_interval = TIME_T_TO_CDTIME_T (86400);
484 NOTICE ("read-function of plugin `%s' failed. "
485 "Will suspend it for %.3f seconds.",
487 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
491 /* Success: Restore the interval, if it was changed. */
492 rf->rf_effective_interval = rf->rf_interval;
495 /* update the ``next read due'' field */
498 DEBUG ("plugin_read_thread: Effective interval of the "
499 "%s plugin is %.3f seconds.",
501 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
503 /* Calculate the next (absolute) time at which this function
504 * should be called. */
505 rf->rf_next_read += rf->rf_effective_interval;
507 /* Check, if `rf_next_read' is in the past. */
508 if (rf->rf_next_read < now)
510 /* `rf_next_read' is in the past. Insert `now'
511 * so this value doesn't trail off into the
513 rf->rf_next_read = now;
516 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
518 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
520 /* Re-insert this read function into the heap again. */
521 c_heap_insert (read_heap, rf);
522 } /* while (read_loop) */
526 } /* void *plugin_read_thread */
528 static void start_read_threads (int num)
532 if (read_threads != NULL)
535 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
536 if (read_threads == NULL)
538 ERROR ("plugin: start_read_threads: calloc failed.");
542 read_threads_num = 0;
543 for (i = 0; i < num; i++)
545 if (pthread_create (read_threads + read_threads_num, NULL,
546 plugin_read_thread, NULL) == 0)
552 ERROR ("plugin: start_read_threads: pthread_create failed.");
556 } /* void start_read_threads */
558 static void stop_read_threads (void)
562 if (read_threads == NULL)
565 INFO ("collectd: Stopping %i read threads.", read_threads_num);
567 pthread_mutex_lock (&read_lock);
569 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
570 pthread_cond_broadcast (&read_cond);
571 pthread_mutex_unlock (&read_lock);
573 for (i = 0; i < read_threads_num; i++)
575 if (pthread_join (read_threads[i], NULL) != 0)
577 ERROR ("plugin: stop_read_threads: pthread_join failed.");
579 read_threads[i] = (pthread_t) 0;
581 sfree (read_threads);
582 read_threads_num = 0;
583 } /* void stop_read_threads */
585 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
590 meta_data_destroy (vl->meta);
593 } /* }}} void plugin_value_list_free */
595 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
602 vl = malloc (sizeof (*vl));
605 memcpy (vl, vl_orig, sizeof (*vl));
607 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
608 if (vl->values == NULL)
610 plugin_value_list_free (vl);
613 memcpy (vl->values, vl_orig->values,
614 vl_orig->values_len * sizeof (*vl->values));
616 vl->meta = meta_data_clone (vl->meta);
617 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
619 plugin_value_list_free (vl);
624 vl->time = cdtime ();
626 /* Fill in the interval from the thread context, if it is zero. */
627 if (vl->interval == 0)
629 plugin_ctx_t ctx = plugin_get_ctx ();
631 if (ctx.interval != 0)
632 vl->interval = ctx.interval;
635 char name[6 * DATA_MAX_NAME_LEN];
636 FORMAT_VL (name, sizeof (name), vl);
637 ERROR ("plugin_value_list_clone: Unable to determine "
638 "interval from context for "
639 "value list \"%s\". "
640 "This indicates a broken plugin. "
641 "Please report this problem to the "
642 "collectd mailing list or at "
643 "<http://collectd.org/bugs/>.", name);
644 vl->interval = cf_get_default_interval ();
649 } /* }}} value_list_t *plugin_value_list_clone */
651 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
655 q = malloc (sizeof (*q));
660 q->vl = plugin_value_list_clone (vl);
667 /* Store context of caller (read plugin); otherwise, it would not be
668 * available to the write plugins when actually dispatching the
669 * value-list later on. */
670 q->ctx = plugin_get_ctx ();
672 pthread_mutex_lock (&write_lock);
674 if (write_queue_tail == NULL)
676 write_queue_head = q;
677 write_queue_tail = q;
678 write_queue_length = 1;
682 write_queue_tail->next = q;
683 write_queue_tail = q;
684 write_queue_length += 1;
687 pthread_cond_signal (&write_cond);
688 pthread_mutex_unlock (&write_lock);
691 } /* }}} int plugin_write_enqueue */
693 static value_list_t *plugin_write_dequeue (void) /* {{{ */
698 pthread_mutex_lock (&write_lock);
700 while (write_loop && (write_queue_head == NULL))
701 pthread_cond_wait (&write_cond, &write_lock);
703 if (write_queue_head == NULL)
705 pthread_mutex_unlock (&write_lock);
709 q = write_queue_head;
710 write_queue_head = q->next;
711 write_queue_length -= 1;
712 if (write_queue_head == NULL) {
713 write_queue_tail = NULL;
714 assert(0 == write_queue_length);
717 pthread_mutex_unlock (&write_lock);
719 (void) plugin_set_ctx (q->ctx);
724 } /* }}} value_list_t *plugin_write_dequeue */
726 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
730 value_list_t *vl = plugin_write_dequeue ();
734 plugin_dispatch_values_internal (vl);
736 plugin_value_list_free (vl);
741 } /* }}} void *plugin_write_thread */
743 static void start_write_threads (size_t num) /* {{{ */
747 if (write_threads != NULL)
750 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
751 if (write_threads == NULL)
753 ERROR ("plugin: start_write_threads: calloc failed.");
757 write_threads_num = 0;
758 for (i = 0; i < num; i++)
762 status = pthread_create (write_threads + write_threads_num,
769 ERROR ("plugin: start_write_threads: pthread_create failed "
770 "with status %i (%s).", status,
771 sstrerror (status, errbuf, sizeof (errbuf)));
777 } /* }}} void start_write_threads */
779 static void stop_write_threads (void) /* {{{ */
784 if (write_threads == NULL)
787 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
789 pthread_mutex_lock (&write_lock);
791 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
792 pthread_cond_broadcast (&write_cond);
793 pthread_mutex_unlock (&write_lock);
795 for (i = 0; i < write_threads_num; i++)
797 if (pthread_join (write_threads[i], NULL) != 0)
799 ERROR ("plugin: stop_write_threads: pthread_join failed.");
801 write_threads[i] = (pthread_t) 0;
803 sfree (write_threads);
804 write_threads_num = 0;
806 pthread_mutex_lock (&write_lock);
808 for (q = write_queue_head; q != NULL; )
810 write_queue_t *q1 = q;
811 plugin_value_list_free (q->vl);
816 write_queue_head = NULL;
817 write_queue_tail = NULL;
818 write_queue_length = 0;
819 pthread_mutex_unlock (&write_lock);
823 WARNING ("plugin: %i value list%s left after shutting down "
824 "the write threads.",
825 i, (i == 1) ? " was" : "s were");
827 } /* }}} void stop_write_threads */
832 void plugin_set_dir (const char *dir)
834 if (plugindir != NULL)
839 else if ((plugindir = strdup (dir)) == NULL)
842 ERROR ("strdup failed: %s",
843 sstrerror (errno, errbuf, sizeof (errbuf)));
847 static _Bool plugin_is_loaded (char const *name)
851 if (plugins_loaded == NULL)
852 plugins_loaded = c_avl_create ((void *) strcasecmp);
853 assert (plugins_loaded != NULL);
855 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
856 return (status == 0);
859 static int plugin_mark_loaded (char const *name)
864 name_copy = strdup (name);
865 if (name_copy == NULL)
868 status = c_avl_insert (plugins_loaded,
869 /* key = */ name_copy, /* value = */ NULL);
873 static void plugin_free_loaded ()
878 if (plugins_loaded == NULL)
881 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
884 assert (value == NULL);
887 c_avl_destroy (plugins_loaded);
888 plugins_loaded = NULL;
892 int plugin_load (char const *plugin_name, uint32_t flags)
896 char filename[BUFSIZE] = "";
897 char typename[BUFSIZE];
904 if (plugin_name == NULL)
907 /* Check if plugin is already loaded and don't do anything in this
909 if (plugin_is_loaded (plugin_name))
912 dir = plugin_get_dir ();
916 * XXX: Magic at work:
918 * Some of the language bindings, for example the Python and Perl
919 * plugins, need to be able to export symbols to the scripts they run.
920 * For this to happen, the "Globals" flag needs to be set.
921 * Unfortunately, this technical detail is hard to explain to the
922 * average user and she shouldn't have to worry about this, ideally.
923 * So in order to save everyone's sanity use a different default for a
924 * handful of special plugins. --octo
926 if ((strcasecmp ("perl", plugin_name) == 0)
927 || (strcasecmp ("python", plugin_name) == 0))
928 flags |= PLUGIN_FLAGS_GLOBAL;
930 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
931 * type when matching the filename */
932 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
933 if ((status < 0) || ((size_t) status >= sizeof (typename)))
935 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
938 typename_len = strlen (typename);
940 if ((dh = opendir (dir)) == NULL)
943 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
944 sstrerror (errno, errbuf, sizeof (errbuf)));
948 while ((de = readdir (dh)) != NULL)
950 if (strncasecmp (de->d_name, typename, typename_len))
953 status = ssnprintf (filename, sizeof (filename),
954 "%s/%s", dir, de->d_name);
955 if ((status < 0) || ((size_t) status >= sizeof (filename)))
957 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
962 if (lstat (filename, &statbuf) == -1)
965 WARNING ("plugin_load: stat (\"%s\") failed: %s",
967 sstrerror (errno, errbuf, sizeof (errbuf)));
970 else if (!S_ISREG (statbuf.st_mode))
972 /* don't follow symlinks */
973 WARNING ("plugin_load: %s is not a regular file.",
978 status = plugin_load_file (filename, flags);
982 plugin_mark_loaded (plugin_name);
988 ERROR ("plugin_load: Load plugin \"%s\" failed with "
989 "status %i.", plugin_name, status);
995 if (filename[0] == 0)
996 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1003 * The `register_*' functions follow
1005 int plugin_register_config (const char *name,
1006 int (*callback) (const char *key, const char *val),
1007 const char **keys, int keys_num)
1009 cf_register (name, callback, keys, keys_num);
1011 } /* int plugin_register_config */
1013 int plugin_register_complex_config (const char *type,
1014 int (*callback) (oconfig_item_t *))
1016 return (cf_register_complex (type, callback));
1017 } /* int plugin_register_complex_config */
1019 int plugin_register_init (const char *name,
1020 int (*callback) (void))
1022 return (create_register_callback (&list_init, name, (void *) callback,
1023 /* user_data = */ NULL));
1024 } /* plugin_register_init */
1026 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1028 const read_func_t *rf0;
1029 const read_func_t *rf1;
1034 if (rf0->rf_next_read < rf1->rf_next_read)
1036 else if (rf0->rf_next_read > rf1->rf_next_read)
1040 } /* int plugin_compare_read_func */
1042 /* Add a read function to both, the heap and a linked list. The linked list if
1043 * used to look-up read functions, especially for the remove function. The heap
1044 * is used to determine which plugin to read next. */
1045 static int plugin_insert_read (read_func_t *rf)
1050 rf->rf_next_read = cdtime ();
1051 rf->rf_effective_interval = rf->rf_interval;
1053 pthread_mutex_lock (&read_lock);
1055 if (read_list == NULL)
1057 read_list = llist_create ();
1058 if (read_list == NULL)
1060 pthread_mutex_unlock (&read_lock);
1061 ERROR ("plugin_insert_read: read_list failed.");
1066 if (read_heap == NULL)
1068 read_heap = c_heap_create (plugin_compare_read_func);
1069 if (read_heap == NULL)
1071 pthread_mutex_unlock (&read_lock);
1072 ERROR ("plugin_insert_read: c_heap_create failed.");
1077 le = llist_search (read_list, rf->rf_name);
1080 pthread_mutex_unlock (&read_lock);
1081 WARNING ("The read function \"%s\" is already registered. "
1082 "Check for duplicate \"LoadPlugin\" lines "
1083 "in your configuration!",
1088 le = llentry_create (rf->rf_name, rf);
1091 pthread_mutex_unlock (&read_lock);
1092 ERROR ("plugin_insert_read: llentry_create failed.");
1096 status = c_heap_insert (read_heap, rf);
1099 pthread_mutex_unlock (&read_lock);
1100 ERROR ("plugin_insert_read: c_heap_insert failed.");
1101 llentry_destroy (le);
1105 /* This does not fail. */
1106 llist_append (read_list, le);
1108 /* Wake up all the read threads. */
1109 pthread_cond_broadcast (&read_cond);
1110 pthread_mutex_unlock (&read_lock);
1112 } /* int plugin_insert_read */
1114 int plugin_register_read (const char *name,
1115 int (*callback) (void))
1120 rf = malloc (sizeof (*rf));
1123 ERROR ("plugin_register_read: malloc failed.");
1127 memset (rf, 0, sizeof (read_func_t));
1128 rf->rf_callback = (void *) callback;
1129 rf->rf_udata.data = NULL;
1130 rf->rf_udata.free_func = NULL;
1131 rf->rf_ctx = plugin_get_ctx ();
1132 rf->rf_group[0] = '\0';
1133 rf->rf_name = strdup (name);
1134 rf->rf_type = RF_SIMPLE;
1135 rf->rf_interval = plugin_get_interval ();
1137 status = plugin_insert_read (rf);
1142 } /* int plugin_register_read */
1144 int plugin_register_complex_read (const char *group, const char *name,
1145 plugin_read_cb callback,
1146 const struct timespec *interval,
1147 user_data_t *user_data)
1152 rf = malloc (sizeof (*rf));
1155 ERROR ("plugin_register_complex_read: malloc failed.");
1159 memset (rf, 0, sizeof (read_func_t));
1160 rf->rf_callback = (void *) callback;
1162 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1164 rf->rf_group[0] = '\0';
1165 rf->rf_name = strdup (name);
1166 rf->rf_type = RF_COMPLEX;
1167 if (interval != NULL)
1168 rf->rf_interval = TIMESPEC_TO_CDTIME_T (interval);
1170 rf->rf_interval = plugin_get_interval ();
1173 if (user_data == NULL)
1175 rf->rf_udata.data = NULL;
1176 rf->rf_udata.free_func = NULL;
1180 rf->rf_udata = *user_data;
1183 rf->rf_ctx = plugin_get_ctx ();
1185 status = plugin_insert_read (rf);
1190 } /* int plugin_register_complex_read */
1192 int plugin_register_write (const char *name,
1193 plugin_write_cb callback, user_data_t *ud)
1195 return (create_register_callback (&list_write, name,
1196 (void *) callback, ud));
1197 } /* int plugin_register_write */
1199 int plugin_register_flush (const char *name,
1200 plugin_flush_cb callback, user_data_t *ud)
1202 return (create_register_callback (&list_flush, name,
1203 (void *) callback, ud));
1204 } /* int plugin_register_flush */
1206 int plugin_register_missing (const char *name,
1207 plugin_missing_cb callback, user_data_t *ud)
1209 return (create_register_callback (&list_missing, name,
1210 (void *) callback, ud));
1211 } /* int plugin_register_missing */
1213 int plugin_register_shutdown (const char *name,
1214 int (*callback) (void))
1216 return (create_register_callback (&list_shutdown, name,
1217 (void *) callback, /* user_data = */ NULL));
1218 } /* int plugin_register_shutdown */
1220 int plugin_register_data_set (const data_set_t *ds)
1222 data_set_t *ds_copy;
1225 if ((data_sets != NULL)
1226 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1228 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1229 plugin_unregister_data_set (ds->type);
1231 else if (data_sets == NULL)
1233 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1234 if (data_sets == NULL)
1238 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1239 if (ds_copy == NULL)
1241 memcpy(ds_copy, ds, sizeof (data_set_t));
1243 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1245 if (ds_copy->ds == NULL)
1251 for (i = 0; i < ds->ds_num; i++)
1252 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1254 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1255 } /* int plugin_register_data_set */
1257 int plugin_register_log (const char *name,
1258 plugin_log_cb callback, user_data_t *ud)
1260 return (create_register_callback (&list_log, name,
1261 (void *) callback, ud));
1262 } /* int plugin_register_log */
1264 int plugin_register_notification (const char *name,
1265 plugin_notification_cb callback, user_data_t *ud)
1267 return (create_register_callback (&list_notification, name,
1268 (void *) callback, ud));
1269 } /* int plugin_register_log */
1271 int plugin_unregister_config (const char *name)
1273 cf_unregister (name);
1275 } /* int plugin_unregister_config */
1277 int plugin_unregister_complex_config (const char *name)
1279 cf_unregister_complex (name);
1281 } /* int plugin_unregister_complex_config */
1283 int plugin_unregister_init (const char *name)
1285 return (plugin_unregister (list_init, name));
1288 int plugin_unregister_read (const char *name) /* {{{ */
1296 pthread_mutex_lock (&read_lock);
1298 if (read_list == NULL)
1300 pthread_mutex_unlock (&read_lock);
1304 le = llist_search (read_list, name);
1307 pthread_mutex_unlock (&read_lock);
1308 WARNING ("plugin_unregister_read: No such read function: %s",
1313 llist_remove (read_list, le);
1316 assert (rf != NULL);
1317 rf->rf_type = RF_REMOVE;
1319 pthread_mutex_unlock (&read_lock);
1321 llentry_destroy (le);
1323 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1326 } /* }}} int plugin_unregister_read */
1328 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1330 read_func_t *rf = e->value;
1333 return strcmp (rf->rf_group, (const char *)group);
1334 } /* }}} int compare_read_func_group */
1336 int plugin_unregister_read_group (const char *group) /* {{{ */
1346 pthread_mutex_lock (&read_lock);
1348 if (read_list == NULL)
1350 pthread_mutex_unlock (&read_lock);
1356 le = llist_search_custom (read_list,
1357 compare_read_func_group, (void *)group);
1364 llist_remove (read_list, le);
1367 assert (rf != NULL);
1368 rf->rf_type = RF_REMOVE;
1370 llentry_destroy (le);
1372 DEBUG ("plugin_unregister_read_group: "
1373 "Marked `%s' (group `%s') for removal.",
1374 rf->rf_name, group);
1377 pthread_mutex_unlock (&read_lock);
1381 WARNING ("plugin_unregister_read_group: No such "
1382 "group of read function: %s", group);
1387 } /* }}} int plugin_unregister_read_group */
1389 int plugin_unregister_write (const char *name)
1391 return (plugin_unregister (list_write, name));
1394 int plugin_unregister_flush (const char *name)
1396 return (plugin_unregister (list_flush, name));
1399 int plugin_unregister_missing (const char *name)
1401 return (plugin_unregister (list_missing, name));
1404 int plugin_unregister_shutdown (const char *name)
1406 return (plugin_unregister (list_shutdown, name));
1409 int plugin_unregister_data_set (const char *name)
1413 if (data_sets == NULL)
1416 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1423 } /* int plugin_unregister_data_set */
1425 int plugin_unregister_log (const char *name)
1427 return (plugin_unregister (list_log, name));
1430 int plugin_unregister_notification (const char *name)
1432 return (plugin_unregister (list_notification, name));
1435 void plugin_init_all (void)
1437 char const *chain_name;
1438 long write_threads_num;
1442 /* Init the value cache */
1445 chain_name = global_option_get ("PreCacheChain");
1446 pre_cache_chain = fc_chain_get_by_name (chain_name);
1448 chain_name = global_option_get ("PostCacheChain");
1449 post_cache_chain = fc_chain_get_by_name (chain_name);
1451 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1453 if (write_limit_high < 0)
1455 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1456 write_limit_high = 0;
1459 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1460 /* default = */ write_limit_high / 2);
1461 if (write_limit_low < 0)
1463 ERROR ("WriteQueueLimitLow must be positive or zero.");
1464 write_limit_low = write_limit_high / 2;
1466 else if (write_limit_low > write_limit_high)
1468 ERROR ("WriteQueueLimitLow must not be larger than "
1469 "WriteQueueLimitHigh.");
1470 write_limit_low = write_limit_high;
1473 write_threads_num = global_option_get_long ("WriteThreads",
1475 if (write_threads_num < 1)
1477 ERROR ("WriteThreads must be positive.");
1478 write_threads_num = 5;
1481 start_write_threads ((size_t) write_threads_num);
1483 if ((list_init == NULL) && (read_heap == NULL))
1486 /* Calling all init callbacks before checking if read callbacks
1487 * are available allows the init callbacks to register the read
1489 le = llist_head (list_init);
1492 callback_func_t *cf;
1493 plugin_init_cb callback;
1494 plugin_ctx_t old_ctx;
1497 old_ctx = plugin_set_ctx (cf->cf_ctx);
1498 callback = cf->cf_callback;
1499 status = (*callback) ();
1500 plugin_set_ctx (old_ctx);
1504 ERROR ("Initialization of plugin `%s' "
1505 "failed with status %i. "
1506 "Plugin will be unloaded.",
1508 /* Plugins that register read callbacks from the init
1509 * callback should take care of appropriate error
1510 * handling themselves. */
1511 /* FIXME: Unload _all_ functions */
1512 plugin_unregister_read (le->key);
1518 /* Start read-threads */
1519 if (read_heap != NULL)
1523 rt = global_option_get ("ReadThreads");
1526 start_read_threads ((num > 0) ? num : 5);
1528 } /* void plugin_init_all */
1530 /* TODO: Rename this function. */
1531 void plugin_read_all (void)
1533 uc_check_timeout ();
1536 } /* void plugin_read_all */
1538 /* Read function called when the `-T' command line argument is given. */
1539 int plugin_read_all_once (void)
1542 int return_status = 0;
1544 if (read_heap == NULL)
1546 NOTICE ("No read-functions are registered.");
1553 plugin_ctx_t old_ctx;
1555 rf = c_heap_get_root (read_heap);
1559 old_ctx = plugin_set_ctx (rf->rf_ctx);
1561 if (rf->rf_type == RF_SIMPLE)
1563 int (*callback) (void);
1565 callback = rf->rf_callback;
1566 status = (*callback) ();
1570 plugin_read_cb callback;
1572 callback = rf->rf_callback;
1573 status = (*callback) (&rf->rf_udata);
1576 plugin_set_ctx (old_ctx);
1580 NOTICE ("read-function of plugin `%s' failed.",
1585 destroy_callback ((void *) rf);
1588 return (return_status);
1589 } /* int plugin_read_all_once */
1591 int plugin_write (const char *plugin, /* {{{ */
1592 const data_set_t *ds, const value_list_t *vl)
1600 if (list_write == NULL)
1605 ds = plugin_get_ds (vl->type);
1608 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1618 le = llist_head (list_write);
1621 callback_func_t *cf = le->value;
1622 plugin_write_cb callback;
1624 /* do not switch plugin context; rather keep the context (interval)
1625 * information of the calling read plugin */
1627 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1628 callback = cf->cf_callback;
1629 status = (*callback) (ds, vl, &cf->cf_udata);
1638 if ((success == 0) && (failure != 0))
1643 else /* plugin != NULL */
1645 callback_func_t *cf;
1646 plugin_write_cb callback;
1648 le = llist_head (list_write);
1651 if (strcasecmp (plugin, le->key) == 0)
1662 /* do not switch plugin context; rather keep the context (interval)
1663 * information of the calling read plugin */
1665 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1666 callback = cf->cf_callback;
1667 status = (*callback) (ds, vl, &cf->cf_udata);
1671 } /* }}} int plugin_write */
1673 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1677 if (list_flush == NULL)
1680 le = llist_head (list_flush);
1683 callback_func_t *cf;
1684 plugin_flush_cb callback;
1685 plugin_ctx_t old_ctx;
1687 if ((plugin != NULL)
1688 && (strcmp (plugin, le->key) != 0))
1695 old_ctx = plugin_set_ctx (cf->cf_ctx);
1696 callback = cf->cf_callback;
1698 (*callback) (timeout, identifier, &cf->cf_udata);
1700 plugin_set_ctx (old_ctx);
1705 } /* int plugin_flush */
1707 void plugin_shutdown_all (void)
1711 stop_read_threads ();
1713 destroy_all_callbacks (&list_init);
1715 pthread_mutex_lock (&read_lock);
1716 llist_destroy (read_list);
1718 pthread_mutex_unlock (&read_lock);
1720 destroy_read_heap ();
1722 plugin_flush (/* plugin = */ NULL,
1724 /* identifier = */ NULL);
1727 if (list_shutdown != NULL)
1728 le = llist_head (list_shutdown);
1732 callback_func_t *cf;
1733 plugin_shutdown_cb callback;
1734 plugin_ctx_t old_ctx;
1737 old_ctx = plugin_set_ctx (cf->cf_ctx);
1738 callback = cf->cf_callback;
1740 /* Advance the pointer before calling the callback allows
1741 * shutdown functions to unregister themselves. If done the
1742 * other way around the memory `le' points to will be freed
1743 * after callback returns. */
1748 plugin_set_ctx (old_ctx);
1751 stop_write_threads ();
1753 /* Write plugins which use the `user_data' pointer usually need the
1754 * same data available to the flush callback. If this is the case, set
1755 * the free_function to NULL when registering the flush callback and to
1756 * the real free function when registering the write callback. This way
1757 * the data isn't freed twice. */
1758 destroy_all_callbacks (&list_flush);
1759 destroy_all_callbacks (&list_missing);
1760 destroy_all_callbacks (&list_write);
1762 destroy_all_callbacks (&list_notification);
1763 destroy_all_callbacks (&list_shutdown);
1764 destroy_all_callbacks (&list_log);
1766 plugin_free_loaded ();
1767 } /* void plugin_shutdown_all */
1769 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1773 if (list_missing == NULL)
1776 le = llist_head (list_missing);
1779 callback_func_t *cf;
1780 plugin_missing_cb callback;
1781 plugin_ctx_t old_ctx;
1785 old_ctx = plugin_set_ctx (cf->cf_ctx);
1786 callback = cf->cf_callback;
1788 status = (*callback) (vl, &cf->cf_udata);
1789 plugin_set_ctx (old_ctx);
1794 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1795 "failed with status %i.",
1808 } /* int }}} plugin_dispatch_missing */
1810 static int plugin_dispatch_values_internal (value_list_t *vl)
1813 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1815 value_t *saved_values;
1816 int saved_values_len;
1820 int free_meta_data = 0;
1822 if ((vl == NULL) || (vl->type[0] == 0)
1823 || (vl->values == NULL) || (vl->values_len < 1))
1825 ERROR ("plugin_dispatch_values: Invalid value list "
1826 "from plugin %s.", vl->plugin);
1830 /* Free meta data only if the calling function didn't specify any. In
1831 * this case matches and targets may add some and the calling function
1832 * may not expect (and therefore free) that data. */
1833 if (vl->meta == NULL)
1836 if (list_write == NULL)
1837 c_complain_once (LOG_WARNING, &no_write_complaint,
1838 "plugin_dispatch_values: No write callback has been "
1839 "registered. Please load at least one output plugin, "
1840 "if you want the collected data to be stored.");
1842 if (data_sets == NULL)
1844 ERROR ("plugin_dispatch_values: No data sets registered. "
1845 "Could the types database be read? Check "
1846 "your `TypesDB' setting!");
1850 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1852 char ident[6 * DATA_MAX_NAME_LEN];
1854 FORMAT_VL (ident, sizeof (ident), vl);
1855 INFO ("plugin_dispatch_values: Dataset not found: %s "
1856 "(from \"%s\"), check your types.db!",
1861 /* Assured by plugin_value_list_clone(). The time is determined at
1862 * _enqueue_ time. */
1863 assert (vl->time != 0);
1864 assert (vl->interval != 0);
1866 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1868 "plugin = %s; plugin_instance = %s; "
1869 "type = %s; type_instance = %s;",
1870 CDTIME_T_TO_DOUBLE (vl->time),
1871 CDTIME_T_TO_DOUBLE (vl->interval),
1873 vl->plugin, vl->plugin_instance,
1874 vl->type, vl->type_instance);
1877 assert (0 == strcmp (ds->type, vl->type));
1879 if (0 != strcmp (ds->type, vl->type))
1880 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1881 ds->type, vl->type);
1885 assert (ds->ds_num == vl->values_len);
1887 if (ds->ds_num != vl->values_len)
1889 ERROR ("plugin_dispatch_values: ds->type = %s: "
1890 "(ds->ds_num = %i) != "
1891 "(vl->values_len = %i)",
1892 ds->type, ds->ds_num, vl->values_len);
1897 escape_slashes (vl->host, sizeof (vl->host));
1898 escape_slashes (vl->plugin, sizeof (vl->plugin));
1899 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1900 escape_slashes (vl->type, sizeof (vl->type));
1901 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1903 /* Copy the values. This way, we can assure `targets' that they get
1904 * dynamically allocated values, which they can free and replace if
1906 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1908 saved_values = vl->values;
1909 saved_values_len = vl->values_len;
1911 vl->values = (value_t *) calloc (vl->values_len,
1912 sizeof (*vl->values));
1913 if (vl->values == NULL)
1915 ERROR ("plugin_dispatch_values: calloc failed.");
1916 vl->values = saved_values;
1919 memcpy (vl->values, saved_values,
1920 vl->values_len * sizeof (*vl->values));
1922 else /* if ((pre == NULL) && (post == NULL)) */
1924 saved_values = NULL;
1925 saved_values_len = 0;
1928 if (pre_cache_chain != NULL)
1930 status = fc_process_chain (ds, vl, pre_cache_chain);
1933 WARNING ("plugin_dispatch_values: Running the "
1934 "pre-cache chain failed with "
1938 else if (status == FC_TARGET_STOP)
1940 /* Restore the state of the value_list so that plugins
1941 * don't get confused.. */
1942 if (saved_values != NULL)
1945 vl->values = saved_values;
1946 vl->values_len = saved_values_len;
1952 /* Update the value cache */
1955 if (post_cache_chain != NULL)
1957 status = fc_process_chain (ds, vl, post_cache_chain);
1960 WARNING ("plugin_dispatch_values: Running the "
1961 "post-cache chain failed with "
1967 fc_default_action (ds, vl);
1969 /* Restore the state of the value_list so that plugins don't get
1971 if (saved_values != NULL)
1974 vl->values = saved_values;
1975 vl->values_len = saved_values_len;
1978 if ((free_meta_data != 0) && (vl->meta != NULL))
1980 meta_data_destroy (vl->meta);
1985 } /* int plugin_dispatch_values_internal */
1987 static double get_drop_probability (void) /* {{{ */
1993 pthread_mutex_lock (&write_lock);
1994 wql = write_queue_length;
1995 pthread_mutex_unlock (&write_lock);
1997 if (wql < write_limit_low)
1999 if (wql >= write_limit_high)
2002 pos = 1 + wql - write_limit_low;
2003 size = 1 + write_limit_high - write_limit_low;
2005 return (((double) pos) / ((double) size));
2006 } /* }}} double get_drop_probability */
2008 static _Bool check_drop_value (void) /* {{{ */
2010 static cdtime_t last_message_time = 0;
2011 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2017 if (write_limit_high == 0)
2020 p = get_drop_probability ();
2024 status = pthread_mutex_trylock (&last_message_lock);
2030 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2032 last_message_time = now;
2033 ERROR ("plugin_dispatch_values: Low water mark "
2034 "reached. Dropping %.0f%% of metrics.",
2037 pthread_mutex_unlock (&last_message_lock);
2048 } /* }}} _Bool check_drop_value */
2050 int plugin_dispatch_values (value_list_t const *vl)
2054 if (check_drop_value ())
2057 status = plugin_write_enqueue (vl);
2061 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2062 "with status %i (%s).", status,
2063 sstrerror (status, errbuf, sizeof (errbuf)));
2070 int plugin_dispatch_notification (const notification_t *notif)
2073 /* Possible TODO: Add flap detection here */
2075 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2076 "time = %.3f; host = %s;",
2077 notif->severity, notif->message,
2078 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2080 /* Nobody cares for notifications */
2081 if (list_notification == NULL)
2084 le = llist_head (list_notification);
2087 callback_func_t *cf;
2088 plugin_notification_cb callback;
2091 /* do not switch plugin context; rather keep the context
2092 * (interval) information of the calling plugin */
2095 callback = cf->cf_callback;
2096 status = (*callback) (notif, &cf->cf_udata);
2099 WARNING ("plugin_dispatch_notification: Notification "
2100 "callback %s returned %i.",
2108 } /* int plugin_dispatch_notification */
2110 void plugin_log (int level, const char *format, ...)
2117 if (level >= LOG_DEBUG)
2121 va_start (ap, format);
2122 vsnprintf (msg, sizeof (msg), format, ap);
2123 msg[sizeof (msg) - 1] = '\0';
2126 if (list_log == NULL)
2128 fprintf (stderr, "%s\n", msg);
2132 le = llist_head (list_log);
2135 callback_func_t *cf;
2136 plugin_log_cb callback;
2139 callback = cf->cf_callback;
2141 /* do not switch plugin context; rather keep the context
2142 * (interval) information of the calling plugin */
2144 (*callback) (level, msg, &cf->cf_udata);
2148 } /* void plugin_log */
2150 int parse_log_severity (const char *severity)
2154 if ((0 == strcasecmp (severity, "emerg"))
2155 || (0 == strcasecmp (severity, "alert"))
2156 || (0 == strcasecmp (severity, "crit"))
2157 || (0 == strcasecmp (severity, "err")))
2158 log_level = LOG_ERR;
2159 else if (0 == strcasecmp (severity, "warning"))
2160 log_level = LOG_WARNING;
2161 else if (0 == strcasecmp (severity, "notice"))
2162 log_level = LOG_NOTICE;
2163 else if (0 == strcasecmp (severity, "info"))
2164 log_level = LOG_INFO;
2166 else if (0 == strcasecmp (severity, "debug"))
2167 log_level = LOG_DEBUG;
2168 #endif /* COLLECT_DEBUG */
2171 } /* int parse_log_severity */
2173 int parse_notif_severity (const char *severity)
2175 int notif_severity = -1;
2177 if (strcasecmp (severity, "FAILURE") == 0)
2178 notif_severity = NOTIF_FAILURE;
2179 else if (strcmp (severity, "OKAY") == 0)
2180 notif_severity = NOTIF_OKAY;
2181 else if ((strcmp (severity, "WARNING") == 0)
2182 || (strcmp (severity, "WARN") == 0))
2183 notif_severity = NOTIF_WARNING;
2185 return (notif_severity);
2186 } /* int parse_notif_severity */
2188 const data_set_t *plugin_get_ds (const char *name)
2192 if (data_sets == NULL)
2194 ERROR ("plugin_get_ds: No data sets are defined yet.");
2198 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2200 DEBUG ("No such dataset registered: %s", name);
2205 } /* data_set_t *plugin_get_ds */
2207 static int plugin_notification_meta_add (notification_t *n,
2209 enum notification_meta_type_e type,
2212 notification_meta_t *meta;
2213 notification_meta_t *tail;
2215 if ((n == NULL) || (name == NULL) || (value == NULL))
2217 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2221 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2224 ERROR ("plugin_notification_meta_add: malloc failed.");
2227 memset (meta, 0, sizeof (notification_meta_t));
2229 sstrncpy (meta->name, name, sizeof (meta->name));
2234 case NM_TYPE_STRING:
2236 meta->nm_value.nm_string = strdup ((const char *) value);
2237 if (meta->nm_value.nm_string == NULL)
2239 ERROR ("plugin_notification_meta_add: strdup failed.");
2245 case NM_TYPE_SIGNED_INT:
2247 meta->nm_value.nm_signed_int = *((int64_t *) value);
2250 case NM_TYPE_UNSIGNED_INT:
2252 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2255 case NM_TYPE_DOUBLE:
2257 meta->nm_value.nm_double = *((double *) value);
2260 case NM_TYPE_BOOLEAN:
2262 meta->nm_value.nm_boolean = *((_Bool *) value);
2267 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2271 } /* switch (type) */
2275 while ((tail != NULL) && (tail->next != NULL))
2284 } /* int plugin_notification_meta_add */
2286 int plugin_notification_meta_add_string (notification_t *n,
2290 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2293 int plugin_notification_meta_add_signed_int (notification_t *n,
2297 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2300 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2304 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2307 int plugin_notification_meta_add_double (notification_t *n,
2311 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2314 int plugin_notification_meta_add_boolean (notification_t *n,
2318 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2321 int plugin_notification_meta_copy (notification_t *dst,
2322 const notification_t *src)
2324 notification_meta_t *meta;
2326 assert (dst != NULL);
2327 assert (src != NULL);
2328 assert (dst != src);
2329 assert ((src->meta == NULL) || (src->meta != dst->meta));
2331 for (meta = src->meta; meta != NULL; meta = meta->next)
2333 if (meta->type == NM_TYPE_STRING)
2334 plugin_notification_meta_add_string (dst, meta->name,
2335 meta->nm_value.nm_string);
2336 else if (meta->type == NM_TYPE_SIGNED_INT)
2337 plugin_notification_meta_add_signed_int (dst, meta->name,
2338 meta->nm_value.nm_signed_int);
2339 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2340 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2341 meta->nm_value.nm_unsigned_int);
2342 else if (meta->type == NM_TYPE_DOUBLE)
2343 plugin_notification_meta_add_double (dst, meta->name,
2344 meta->nm_value.nm_double);
2345 else if (meta->type == NM_TYPE_BOOLEAN)
2346 plugin_notification_meta_add_boolean (dst, meta->name,
2347 meta->nm_value.nm_boolean);
2351 } /* int plugin_notification_meta_copy */
2353 int plugin_notification_meta_free (notification_meta_t *n)
2355 notification_meta_t *this;
2356 notification_meta_t *next;
2360 ERROR ("plugin_notification_meta_free: n == NULL!");
2365 while (this != NULL)
2369 if (this->type == NM_TYPE_STRING)
2371 free ((char *)this->nm_value.nm_string);
2372 this->nm_value.nm_string = NULL;
2380 } /* int plugin_notification_meta_free */
2382 static void plugin_ctx_destructor (void *ctx)
2385 } /* void plugin_ctx_destructor */
2387 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2389 static plugin_ctx_t *plugin_ctx_create (void)
2393 ctx = malloc (sizeof (*ctx));
2396 ERROR ("Failed to allocate plugin context: %s",
2397 sstrerror (errno, errbuf, sizeof (errbuf)));
2402 assert (plugin_ctx_key_initialized);
2403 pthread_setspecific (plugin_ctx_key, ctx);
2404 DEBUG("Created new plugin context.");
2406 } /* int plugin_ctx_create */
2408 void plugin_init_ctx (void)
2410 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2411 plugin_ctx_key_initialized = 1;
2412 } /* void plugin_init_ctx */
2414 plugin_ctx_t plugin_get_ctx (void)
2418 assert (plugin_ctx_key_initialized);
2419 ctx = pthread_getspecific (plugin_ctx_key);
2422 ctx = plugin_ctx_create ();
2423 /* this must no happen -- exit() instead? */
2429 } /* plugin_ctx_t plugin_get_ctx */
2431 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2436 assert (plugin_ctx_key_initialized);
2437 c = pthread_getspecific (plugin_ctx_key);
2440 c = plugin_ctx_create ();
2441 /* this must no happen -- exit() instead? */
2450 } /* void plugin_set_ctx */
2452 cdtime_t plugin_get_interval (void)
2456 interval = plugin_get_ctx().interval;
2460 return cf_get_default_interval ();
2461 } /* cdtime_t plugin_get_interval */
2465 void *(*start_routine) (void *);
2469 static void *plugin_thread_start (void *arg)
2471 plugin_thread_t *plugin_thread = arg;
2473 void *(*start_routine) (void *) = plugin_thread->start_routine;
2474 void *plugin_arg = plugin_thread->arg;
2476 plugin_set_ctx (plugin_thread->ctx);
2478 free (plugin_thread);
2480 return start_routine (plugin_arg);
2481 } /* void *plugin_thread_start */
2483 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2484 void *(*start_routine) (void *), void *arg)
2486 plugin_thread_t *plugin_thread;
2488 plugin_thread = malloc (sizeof (*plugin_thread));
2489 if (plugin_thread == NULL)
2492 plugin_thread->ctx = plugin_get_ctx ();
2493 plugin_thread->start_routine = start_routine;
2494 plugin_thread->arg = arg;
2496 return pthread_create (thread, attr,
2497 plugin_thread_start, plugin_thread);
2498 } /* int plugin_thread_create */
2500 /* vim: set sw=8 ts=8 noet fdm=marker : */