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>
24 #include "utils_complain.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_heap.h"
38 #include "utils_cache.h"
39 #include "filter_chain.h"
44 struct callback_func_s
50 typedef struct callback_func_s callback_func_t;
54 #define RF_REMOVE 65535
57 /* `read_func_t' "inherits" from `callback_func_t'.
58 * The `rf_super' member MUST be the first one in this structure! */
59 #define rf_callback rf_super.cf_callback
60 #define rf_udata rf_super.cf_udata
61 #define rf_ctx rf_super.cf_ctx
62 callback_func_t rf_super;
63 char rf_group[DATA_MAX_NAME_LEN];
64 char rf_name[DATA_MAX_NAME_LEN];
66 struct timespec rf_interval;
67 struct timespec rf_effective_interval;
68 struct timespec rf_next_read;
70 typedef struct read_func_s read_func_t;
73 typedef struct write_queue_s write_queue_t;
83 static llist_t *list_init;
84 static llist_t *list_write;
85 static llist_t *list_flush;
86 static llist_t *list_missing;
87 static llist_t *list_shutdown;
88 static llist_t *list_log;
89 static llist_t *list_notification;
91 static fc_chain_t *pre_cache_chain = NULL;
92 static fc_chain_t *post_cache_chain = NULL;
94 static c_avl_tree_t *data_sets;
96 static char *plugindir = NULL;
98 static c_heap_t *read_heap = NULL;
99 static llist_t *read_list;
100 static int read_loop = 1;
101 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
102 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
103 static pthread_t *read_threads = NULL;
104 static int read_threads_num = 0;
106 static write_queue_t *write_queue_head;
107 static write_queue_t *write_queue_tail;
108 static _Bool write_loop = 1;
109 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
110 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
111 static pthread_t *write_threads = NULL;
112 static size_t write_threads_num = 0;
114 static pthread_key_t plugin_ctx_key;
115 static _Bool plugin_ctx_key_initialized = 0;
120 static int plugin_dispatch_values_internal (value_list_t *vl);
122 static const char *plugin_get_dir (void)
124 if (plugindir == NULL)
130 static void destroy_callback (callback_func_t *cf) /* {{{ */
135 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
137 cf->cf_udata.free_func (cf->cf_udata.data);
138 cf->cf_udata.data = NULL;
139 cf->cf_udata.free_func = NULL;
142 } /* }}} void destroy_callback */
144 static void destroy_all_callbacks (llist_t **list) /* {{{ */
151 le = llist_head (*list);
159 destroy_callback (le->value);
165 llist_destroy (*list);
167 } /* }}} void destroy_all_callbacks */
169 static void destroy_read_heap (void) /* {{{ */
171 if (read_heap == NULL)
178 cf = c_heap_get_root (read_heap);
182 destroy_callback (cf);
185 c_heap_destroy (read_heap);
187 } /* }}} void destroy_read_heap */
189 static int register_callback (llist_t **list, /* {{{ */
190 const char *name, callback_func_t *cf)
197 *list = llist_create ();
200 ERROR ("plugin: register_callback: "
201 "llist_create failed.");
202 destroy_callback (cf);
210 ERROR ("plugin: register_callback: strdup failed.");
211 destroy_callback (cf);
215 le = llist_search (*list, name);
218 le = llentry_create (key, cf);
221 ERROR ("plugin: register_callback: "
222 "llentry_create failed.");
224 destroy_callback (cf);
228 llist_append (*list, le);
232 callback_func_t *old_cf;
237 WARNING ("plugin: register_callback: "
238 "a callback named `%s' already exists - "
239 "overwriting the old entry!", name);
241 destroy_callback (old_cf);
246 } /* }}} int register_callback */
248 static int create_register_callback (llist_t **list, /* {{{ */
249 const char *name, void *callback, user_data_t *ud)
253 cf = (callback_func_t *) malloc (sizeof (*cf));
256 ERROR ("plugin: create_register_callback: malloc failed.");
259 memset (cf, 0, sizeof (*cf));
261 cf->cf_callback = callback;
264 cf->cf_udata.data = NULL;
265 cf->cf_udata.free_func = NULL;
272 cf->cf_ctx = plugin_get_ctx ();
274 return (register_callback (list, name, cf));
275 } /* }}} int create_register_callback */
277 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
284 e = llist_search (list, name);
288 llist_remove (list, e);
291 destroy_callback (e->value);
296 } /* }}} int plugin_unregister */
299 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
300 * object, but it will bitch about a shared object not having a
301 * ``module_register'' symbol..
303 static int plugin_load_file (char *file, uint32_t flags)
306 void (*reg_handle) (void);
309 lt_dlerror (); /* clear errors */
311 #if LIBTOOL_VERSION == 2
312 if (flags & PLUGIN_FLAGS_GLOBAL) {
314 lt_dladvise_init(&advise);
315 lt_dladvise_global(&advise);
316 dlh = lt_dlopenadvise(file, advise);
317 lt_dladvise_destroy(&advise);
319 dlh = lt_dlopen (file);
321 #else /* if LIBTOOL_VERSION == 1 */
322 if (flags & PLUGIN_FLAGS_GLOBAL)
323 WARNING ("plugin_load_file: The global flag is not supported, "
324 "libtool 2 is required for this.");
325 dlh = lt_dlopen (file);
330 char errbuf[1024] = "";
332 ssnprintf (errbuf, sizeof (errbuf),
333 "lt_dlopen (\"%s\") failed: %s. "
334 "The most common cause for this problem are "
335 "missing dependencies. Use ldd(1) to check "
336 "the dependencies of the plugin "
338 file, lt_dlerror ());
340 ERROR ("%s", errbuf);
341 /* Make sure this is printed to STDERR in any case, but also
342 * make sure it's printed only once. */
343 if (list_log != NULL)
344 fprintf (stderr, "ERROR: %s\n", errbuf);
349 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
351 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
352 file, lt_dlerror ());
362 static _Bool timeout_reached(struct timespec timeout)
365 gettimeofday(&now, NULL);
366 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
369 static void *plugin_read_thread (void __attribute__((unused)) *args)
371 while (read_loop != 0)
374 plugin_ctx_t old_ctx;
380 /* Get the read function that needs to be read next.
381 * We don't need to hold "read_lock" for the heap, but we need
382 * to call c_heap_get_root() and pthread_cond_wait() in the
383 * same protected block. */
384 pthread_mutex_lock (&read_lock);
385 rf = c_heap_get_root (read_heap);
388 pthread_cond_wait (&read_cond, &read_lock);
389 pthread_mutex_unlock (&read_lock);
392 pthread_mutex_unlock (&read_lock);
394 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
396 /* this should not happen, because the interval is set
397 * for each plugin when loading it
398 * XXX: issue a warning? */
401 CDTIME_T_TO_TIMESPEC (plugin_get_interval (), &rf->rf_interval);
403 rf->rf_effective_interval = rf->rf_interval;
405 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
408 /* sleep until this entry is due,
409 * using pthread_cond_timedwait */
410 pthread_mutex_lock (&read_lock);
411 /* In pthread_cond_timedwait, spurious wakeups are possible
412 * (and really happen, at least on NetBSD with > 1 CPU), thus
413 * we need to re-evaluate the condition every time
414 * pthread_cond_timedwait returns. */
416 while ((read_loop != 0)
417 && !timeout_reached(rf->rf_next_read)
420 rc = pthread_cond_timedwait (&read_cond, &read_lock,
424 /* Must hold `read_lock' when accessing `rf->rf_type'. */
425 rf_type = rf->rf_type;
426 pthread_mutex_unlock (&read_lock);
428 /* Check if we're supposed to stop.. This may have interrupted
432 /* Insert `rf' again, so it can be free'd correctly */
433 c_heap_insert (read_heap, rf);
437 /* The entry has been marked for deletion. The linked list
438 * entry has already been removed by `plugin_unregister_read'.
439 * All we have to do here is free the `read_func_t' and
441 if (rf_type == RF_REMOVE)
443 DEBUG ("plugin_read_thread: Destroying the `%s' "
444 "callback.", rf->rf_name);
445 destroy_callback ((callback_func_t *) rf);
450 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
452 old_ctx = plugin_set_ctx (rf->rf_ctx);
454 if (rf_type == RF_SIMPLE)
456 int (*callback) (void);
458 callback = rf->rf_callback;
459 status = (*callback) ();
463 plugin_read_cb callback;
465 assert (rf_type == RF_COMPLEX);
467 callback = rf->rf_callback;
468 status = (*callback) (&rf->rf_udata);
471 plugin_set_ctx (old_ctx);
473 /* If the function signals failure, we will increase the
474 * intervals in which it will be called. */
477 rf->rf_effective_interval.tv_sec *= 2;
478 rf->rf_effective_interval.tv_nsec *= 2;
479 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
481 if (rf->rf_effective_interval.tv_sec >= 86400)
483 rf->rf_effective_interval.tv_sec = 86400;
484 rf->rf_effective_interval.tv_nsec = 0;
487 NOTICE ("read-function of plugin `%s' failed. "
488 "Will suspend it for %i seconds.",
490 (int) rf->rf_effective_interval.tv_sec);
494 /* Success: Restore the interval, if it was changed. */
495 rf->rf_effective_interval = rf->rf_interval;
498 /* update the ``next read due'' field */
501 DEBUG ("plugin_read_thread: Effective interval of the "
502 "%s plugin is %i.%09i.",
504 (int) rf->rf_effective_interval.tv_sec,
505 (int) rf->rf_effective_interval.tv_nsec);
507 /* Calculate the next (absolute) time at which this function
508 * should be called. */
509 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
510 + rf->rf_effective_interval.tv_sec;
511 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
512 + rf->rf_effective_interval.tv_nsec;
513 NORMALIZE_TIMESPEC (rf->rf_next_read);
515 /* Check, if `rf_next_read' is in the past. */
516 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
518 /* `rf_next_read' is in the past. Insert `now'
519 * so this value doesn't trail off into the
521 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
524 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
526 (int) rf->rf_next_read.tv_sec,
527 (int) rf->rf_next_read.tv_nsec);
529 /* Re-insert this read function into the heap again. */
530 c_heap_insert (read_heap, rf);
531 } /* while (read_loop) */
535 } /* void *plugin_read_thread */
537 static void start_read_threads (int num)
541 if (read_threads != NULL)
544 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
545 if (read_threads == NULL)
547 ERROR ("plugin: start_read_threads: calloc failed.");
551 read_threads_num = 0;
552 for (i = 0; i < num; i++)
554 if (pthread_create (read_threads + read_threads_num, NULL,
555 plugin_read_thread, NULL) == 0)
561 ERROR ("plugin: start_read_threads: pthread_create failed.");
565 } /* void start_read_threads */
567 static void stop_read_threads (void)
571 if (read_threads == NULL)
574 INFO ("collectd: Stopping %i read threads.", read_threads_num);
576 pthread_mutex_lock (&read_lock);
578 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
579 pthread_cond_broadcast (&read_cond);
580 pthread_mutex_unlock (&read_lock);
582 for (i = 0; i < read_threads_num; i++)
584 if (pthread_join (read_threads[i], NULL) != 0)
586 ERROR ("plugin: stop_read_threads: pthread_join failed.");
588 read_threads[i] = (pthread_t) 0;
590 sfree (read_threads);
591 read_threads_num = 0;
592 } /* void stop_read_threads */
594 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
599 meta_data_destroy (vl->meta);
602 } /* }}} void plugin_value_list_free */
604 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
611 vl = malloc (sizeof (*vl));
614 memcpy (vl, vl_orig, sizeof (*vl));
616 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
617 if (vl->values == NULL)
619 plugin_value_list_free (vl);
622 memcpy (vl->values, vl_orig->values,
623 vl_orig->values_len * sizeof (*vl->values));
625 vl->meta = meta_data_clone (vl->meta);
626 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
628 plugin_value_list_free (vl);
633 } /* }}} value_list_t *plugin_value_list_clone */
635 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
639 q = malloc (sizeof (*q));
644 q->vl = plugin_value_list_clone (vl);
651 pthread_mutex_lock (&write_lock);
653 if (write_queue_tail == NULL)
655 write_queue_head = q;
656 write_queue_tail = q;
660 write_queue_tail->next = q;
661 write_queue_tail = q;
664 pthread_cond_signal (&write_cond);
665 pthread_mutex_unlock (&write_lock);
668 } /* }}} int plugin_write_enqueue */
670 static value_list_t *plugin_write_dequeue (void) /* {{{ */
675 pthread_mutex_lock (&write_lock);
677 while (write_loop && (write_queue_head == NULL))
678 pthread_cond_wait (&write_cond, &write_lock);
680 if (write_queue_head == NULL)
682 pthread_mutex_unlock (&write_lock);
686 q = write_queue_head;
687 write_queue_head = q->next;
688 if (write_queue_head == NULL)
689 write_queue_tail = NULL;
691 pthread_mutex_unlock (&write_lock);
696 } /* }}} value_list_t *plugin_write_dequeue */
698 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
702 value_list_t *vl = plugin_write_dequeue ();
706 plugin_dispatch_values_internal (vl);
708 plugin_value_list_free (vl);
713 } /* }}} void *plugin_write_thread */
715 static void start_write_threads (size_t num) /* {{{ */
719 if (write_threads != NULL)
722 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
723 if (write_threads == NULL)
725 ERROR ("plugin: start_write_threads: calloc failed.");
729 write_threads_num = 0;
730 for (i = 0; i < num; i++)
734 status = pthread_create (write_threads + write_threads_num,
741 ERROR ("plugin: start_write_threads: pthread_create failed "
742 "with status %i (%s).", status,
743 sstrerror (status, errbuf, sizeof (errbuf)));
749 } /* }}} void start_write_threads */
751 static void stop_write_threads (void) /* {{{ */
756 if (write_threads == NULL)
759 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
761 pthread_mutex_lock (&write_lock);
763 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
764 pthread_cond_broadcast (&write_cond);
765 pthread_mutex_unlock (&write_lock);
767 for (i = 0; i < write_threads_num; i++)
769 if (pthread_join (write_threads[i], NULL) != 0)
771 ERROR ("plugin: stop_write_threads: pthread_join failed.");
773 write_threads[i] = (pthread_t) 0;
775 sfree (write_threads);
776 write_threads_num = 0;
778 pthread_mutex_lock (&write_lock);
780 for (q = write_queue_head; q != NULL; q = q->next)
782 plugin_value_list_free (q->vl);
786 write_queue_head = NULL;
787 write_queue_tail = NULL;
788 pthread_mutex_unlock (&write_lock);
792 WARNING ("plugin: %i value list%s left after shutting down "
793 "the write threads.",
794 i, (i == 1) ? " was" : "s were");
796 } /* }}} void stop_write_threads */
801 void plugin_set_dir (const char *dir)
803 if (plugindir != NULL)
808 else if ((plugindir = strdup (dir)) == NULL)
811 ERROR ("strdup failed: %s",
812 sstrerror (errno, errbuf, sizeof (errbuf)));
817 int plugin_load (const char *type, uint32_t flags)
821 char filename[BUFSIZE] = "";
822 char typename[BUFSIZE];
829 dir = plugin_get_dir ();
832 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
833 * type when matching the filename */
834 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
835 if ((status < 0) || ((size_t) status >= sizeof (typename)))
837 WARNING ("plugin_load: Filename too long: \"%s.so\"", type);
840 typename_len = strlen (typename);
842 if ((dh = opendir (dir)) == NULL)
845 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
846 sstrerror (errno, errbuf, sizeof (errbuf)));
850 while ((de = readdir (dh)) != NULL)
852 if (strncasecmp (de->d_name, typename, typename_len))
855 status = ssnprintf (filename, sizeof (filename),
856 "%s/%s", dir, de->d_name);
857 if ((status < 0) || ((size_t) status >= sizeof (filename)))
859 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
864 if (lstat (filename, &statbuf) == -1)
867 WARNING ("plugin_load: stat (\"%s\") failed: %s",
869 sstrerror (errno, errbuf, sizeof (errbuf)));
872 else if (!S_ISREG (statbuf.st_mode))
874 /* don't follow symlinks */
875 WARNING ("plugin_load: %s is not a regular file.",
880 status = plugin_load_file (filename, flags);
889 ERROR ("plugin_load: Load plugin \"%s\" failed with "
890 "status %i.", type, status);
896 if (filename[0] == 0)
897 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
904 * The `register_*' functions follow
906 int plugin_register_config (const char *name,
907 int (*callback) (const char *key, const char *val),
908 const char **keys, int keys_num)
910 cf_register (name, callback, keys, keys_num);
912 } /* int plugin_register_config */
914 int plugin_register_complex_config (const char *type,
915 int (*callback) (oconfig_item_t *))
917 return (cf_register_complex (type, callback));
918 } /* int plugin_register_complex_config */
920 int plugin_register_init (const char *name,
921 int (*callback) (void))
923 return (create_register_callback (&list_init, name, (void *) callback,
924 /* user_data = */ NULL));
925 } /* plugin_register_init */
927 static int plugin_compare_read_func (const void *arg0, const void *arg1)
929 const read_func_t *rf0;
930 const read_func_t *rf1;
935 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
937 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
939 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
941 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
945 } /* int plugin_compare_read_func */
947 /* Add a read function to both, the heap and a linked list. The linked list if
948 * used to look-up read functions, especially for the remove function. The heap
949 * is used to determine which plugin to read next. */
950 static int plugin_insert_read (read_func_t *rf)
955 pthread_mutex_lock (&read_lock);
957 if (read_list == NULL)
959 read_list = llist_create ();
960 if (read_list == NULL)
962 pthread_mutex_unlock (&read_lock);
963 ERROR ("plugin_insert_read: read_list failed.");
968 if (read_heap == NULL)
970 read_heap = c_heap_create (plugin_compare_read_func);
971 if (read_heap == NULL)
973 pthread_mutex_unlock (&read_lock);
974 ERROR ("plugin_insert_read: c_heap_create failed.");
979 le = llist_search (read_list, rf->rf_name);
982 pthread_mutex_unlock (&read_lock);
983 WARNING ("The read function \"%s\" is already registered. "
984 "Check for duplicate \"LoadPlugin\" lines "
985 "in your configuration!",
990 le = llentry_create (rf->rf_name, rf);
993 pthread_mutex_unlock (&read_lock);
994 ERROR ("plugin_insert_read: llentry_create failed.");
998 status = c_heap_insert (read_heap, rf);
1001 pthread_mutex_unlock (&read_lock);
1002 ERROR ("plugin_insert_read: c_heap_insert failed.");
1003 llentry_destroy (le);
1007 /* This does not fail. */
1008 llist_append (read_list, le);
1010 /* Wake up all the read threads. */
1011 pthread_cond_broadcast (&read_cond);
1012 pthread_mutex_unlock (&read_lock);
1014 } /* int plugin_insert_read */
1016 static int read_cb_wrapper (user_data_t *ud)
1018 int (*callback) (void);
1023 callback = ud->data;
1025 } /* int read_cb_wrapper */
1027 int plugin_register_read (const char *name,
1028 int (*callback) (void))
1031 plugin_ctx_t ctx = plugin_get_ctx ();
1034 if (ctx.interval != 0) {
1035 /* If ctx.interval is not zero (== use the plugin or global
1036 * interval), we need to use the "complex" read callback,
1037 * because only that allows to specify a different interval.
1038 * Wrap the callback using read_cb_wrapper(). */
1039 struct timespec interval;
1040 user_data_t user_data;
1042 user_data.data = callback;
1043 user_data.free_func = NULL;
1045 CDTIME_T_TO_TIMESPEC (ctx.interval, &interval);
1046 return plugin_register_complex_read (/* group = */ NULL,
1047 name, read_cb_wrapper, &interval, &user_data);
1050 DEBUG ("plugin_register_read: default_interval = %.3f",
1051 CDTIME_T_TO_DOUBLE(plugin_get_interval ()));
1053 rf = malloc (sizeof (*rf));
1056 ERROR ("plugin_register_read: malloc failed.");
1060 memset (rf, 0, sizeof (read_func_t));
1061 rf->rf_callback = (void *) callback;
1062 rf->rf_udata.data = NULL;
1063 rf->rf_udata.free_func = NULL;
1065 rf->rf_group[0] = '\0';
1066 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1067 rf->rf_type = RF_SIMPLE;
1068 rf->rf_interval.tv_sec = 0;
1069 rf->rf_interval.tv_nsec = 0;
1070 rf->rf_effective_interval = rf->rf_interval;
1072 status = plugin_insert_read (rf);
1077 } /* int plugin_register_read */
1079 int plugin_register_complex_read (const char *group, const char *name,
1080 plugin_read_cb callback,
1081 const struct timespec *interval,
1082 user_data_t *user_data)
1085 plugin_ctx_t ctx = plugin_get_ctx ();
1088 rf = malloc (sizeof (*rf));
1091 ERROR ("plugin_register_complex_read: malloc failed.");
1095 memset (rf, 0, sizeof (read_func_t));
1096 rf->rf_callback = (void *) callback;
1098 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1100 rf->rf_group[0] = '\0';
1101 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1102 rf->rf_type = RF_COMPLEX;
1103 if (interval != NULL)
1105 rf->rf_interval = *interval;
1107 else if (ctx.interval != 0)
1109 CDTIME_T_TO_TIMESPEC (ctx.interval, &rf->rf_interval);
1111 rf->rf_effective_interval = rf->rf_interval;
1113 DEBUG ("plugin_register_read: interval = %i.%09i",
1114 (int) rf->rf_interval.tv_sec,
1115 (int) rf->rf_interval.tv_nsec);
1118 if (user_data == NULL)
1120 rf->rf_udata.data = NULL;
1121 rf->rf_udata.free_func = NULL;
1125 rf->rf_udata = *user_data;
1130 status = plugin_insert_read (rf);
1135 } /* int plugin_register_complex_read */
1137 int plugin_register_write (const char *name,
1138 plugin_write_cb callback, user_data_t *ud)
1140 return (create_register_callback (&list_write, name,
1141 (void *) callback, ud));
1142 } /* int plugin_register_write */
1144 int plugin_register_flush (const char *name,
1145 plugin_flush_cb callback, user_data_t *ud)
1147 return (create_register_callback (&list_flush, name,
1148 (void *) callback, ud));
1149 } /* int plugin_register_flush */
1151 int plugin_register_missing (const char *name,
1152 plugin_missing_cb callback, user_data_t *ud)
1154 return (create_register_callback (&list_missing, name,
1155 (void *) callback, ud));
1156 } /* int plugin_register_missing */
1158 int plugin_register_shutdown (const char *name,
1159 int (*callback) (void))
1161 return (create_register_callback (&list_shutdown, name,
1162 (void *) callback, /* user_data = */ NULL));
1163 } /* int plugin_register_shutdown */
1165 int plugin_register_data_set (const data_set_t *ds)
1167 data_set_t *ds_copy;
1170 if ((data_sets != NULL)
1171 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1173 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1174 plugin_unregister_data_set (ds->type);
1176 else if (data_sets == NULL)
1178 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1179 if (data_sets == NULL)
1183 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1184 if (ds_copy == NULL)
1186 memcpy(ds_copy, ds, sizeof (data_set_t));
1188 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1190 if (ds_copy->ds == NULL)
1196 for (i = 0; i < ds->ds_num; i++)
1197 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1199 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1200 } /* int plugin_register_data_set */
1202 int plugin_register_log (const char *name,
1203 plugin_log_cb callback, user_data_t *ud)
1205 return (create_register_callback (&list_log, name,
1206 (void *) callback, ud));
1207 } /* int plugin_register_log */
1209 int plugin_register_notification (const char *name,
1210 plugin_notification_cb callback, user_data_t *ud)
1212 return (create_register_callback (&list_notification, name,
1213 (void *) callback, ud));
1214 } /* int plugin_register_log */
1216 int plugin_unregister_config (const char *name)
1218 cf_unregister (name);
1220 } /* int plugin_unregister_config */
1222 int plugin_unregister_complex_config (const char *name)
1224 cf_unregister_complex (name);
1226 } /* int plugin_unregister_complex_config */
1228 int plugin_unregister_init (const char *name)
1230 return (plugin_unregister (list_init, name));
1233 int plugin_unregister_read (const char *name) /* {{{ */
1241 pthread_mutex_lock (&read_lock);
1243 if (read_list == NULL)
1245 pthread_mutex_unlock (&read_lock);
1249 le = llist_search (read_list, name);
1252 pthread_mutex_unlock (&read_lock);
1253 WARNING ("plugin_unregister_read: No such read function: %s",
1258 llist_remove (read_list, le);
1261 assert (rf != NULL);
1262 rf->rf_type = RF_REMOVE;
1264 pthread_mutex_unlock (&read_lock);
1266 llentry_destroy (le);
1268 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1271 } /* }}} int plugin_unregister_read */
1273 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1275 read_func_t *rf = e->value;
1278 return strcmp (rf->rf_group, (const char *)group);
1279 } /* }}} int compare_read_func_group */
1281 int plugin_unregister_read_group (const char *group) /* {{{ */
1291 pthread_mutex_lock (&read_lock);
1293 if (read_list == NULL)
1295 pthread_mutex_unlock (&read_lock);
1301 le = llist_search_custom (read_list,
1302 compare_read_func_group, (void *)group);
1309 llist_remove (read_list, le);
1312 assert (rf != NULL);
1313 rf->rf_type = RF_REMOVE;
1315 llentry_destroy (le);
1317 DEBUG ("plugin_unregister_read_group: "
1318 "Marked `%s' (group `%s') for removal.",
1319 rf->rf_name, group);
1322 pthread_mutex_unlock (&read_lock);
1326 WARNING ("plugin_unregister_read_group: No such "
1327 "group of read function: %s", group);
1332 } /* }}} int plugin_unregister_read_group */
1334 int plugin_unregister_write (const char *name)
1336 return (plugin_unregister (list_write, name));
1339 int plugin_unregister_flush (const char *name)
1341 return (plugin_unregister (list_flush, name));
1344 int plugin_unregister_missing (const char *name)
1346 return (plugin_unregister (list_missing, name));
1349 int plugin_unregister_shutdown (const char *name)
1351 return (plugin_unregister (list_shutdown, name));
1354 int plugin_unregister_data_set (const char *name)
1358 if (data_sets == NULL)
1361 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1368 } /* int plugin_unregister_data_set */
1370 int plugin_unregister_log (const char *name)
1372 return (plugin_unregister (list_log, name));
1375 int plugin_unregister_notification (const char *name)
1377 return (plugin_unregister (list_notification, name));
1380 void plugin_init_all (void)
1382 const char *chain_name;
1386 /* Init the value cache */
1389 chain_name = global_option_get ("PreCacheChain");
1390 pre_cache_chain = fc_chain_get_by_name (chain_name);
1392 chain_name = global_option_get ("PostCacheChain");
1393 post_cache_chain = fc_chain_get_by_name (chain_name);
1396 char const *tmp = global_option_get ("WriteThreads");
1397 int num = atoi (tmp);
1402 start_write_threads ((size_t) num);
1405 if ((list_init == NULL) && (read_heap == NULL))
1408 /* Calling all init callbacks before checking if read callbacks
1409 * are available allows the init callbacks to register the read
1411 le = llist_head (list_init);
1414 callback_func_t *cf;
1415 plugin_init_cb callback;
1416 plugin_ctx_t old_ctx;
1419 old_ctx = plugin_set_ctx (cf->cf_ctx);
1420 callback = cf->cf_callback;
1421 status = (*callback) ();
1422 plugin_set_ctx (old_ctx);
1426 ERROR ("Initialization of plugin `%s' "
1427 "failed with status %i. "
1428 "Plugin will be unloaded.",
1430 /* Plugins that register read callbacks from the init
1431 * callback should take care of appropriate error
1432 * handling themselves. */
1433 /* FIXME: Unload _all_ functions */
1434 plugin_unregister_read (le->key);
1440 /* Start read-threads */
1441 if (read_heap != NULL)
1445 rt = global_option_get ("ReadThreads");
1448 start_read_threads ((num > 0) ? num : 5);
1450 } /* void plugin_init_all */
1452 /* TODO: Rename this function. */
1453 void plugin_read_all (void)
1455 uc_check_timeout ();
1458 } /* void plugin_read_all */
1460 /* Read function called when the `-T' command line argument is given. */
1461 int plugin_read_all_once (void)
1464 int return_status = 0;
1466 if (read_heap == NULL)
1468 NOTICE ("No read-functions are registered.");
1475 plugin_ctx_t old_ctx;
1477 rf = c_heap_get_root (read_heap);
1481 old_ctx = plugin_set_ctx (rf->rf_ctx);
1483 if (rf->rf_type == RF_SIMPLE)
1485 int (*callback) (void);
1487 callback = rf->rf_callback;
1488 status = (*callback) ();
1492 plugin_read_cb callback;
1494 callback = rf->rf_callback;
1495 status = (*callback) (&rf->rf_udata);
1498 plugin_set_ctx (old_ctx);
1502 NOTICE ("read-function of plugin `%s' failed.",
1507 destroy_callback ((void *) rf);
1510 return (return_status);
1511 } /* int plugin_read_all_once */
1513 int plugin_write (const char *plugin, /* {{{ */
1514 const data_set_t *ds, const value_list_t *vl)
1522 if (list_write == NULL)
1527 ds = plugin_get_ds (vl->type);
1530 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1540 le = llist_head (list_write);
1543 callback_func_t *cf = le->value;
1544 plugin_write_cb callback;
1546 /* do not switch plugin context; rather keep the context (interval)
1547 * information of the calling read plugin */
1549 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1550 callback = cf->cf_callback;
1551 status = (*callback) (ds, vl, &cf->cf_udata);
1560 if ((success == 0) && (failure != 0))
1565 else /* plugin != NULL */
1567 callback_func_t *cf;
1568 plugin_write_cb callback;
1570 le = llist_head (list_write);
1573 if (strcasecmp (plugin, le->key) == 0)
1584 /* do not switch plugin context; rather keep the context (interval)
1585 * information of the calling read plugin */
1587 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1588 callback = cf->cf_callback;
1589 status = (*callback) (ds, vl, &cf->cf_udata);
1593 } /* }}} int plugin_write */
1595 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1599 if (list_flush == NULL)
1602 le = llist_head (list_flush);
1605 callback_func_t *cf;
1606 plugin_flush_cb callback;
1607 plugin_ctx_t old_ctx;
1609 if ((plugin != NULL)
1610 && (strcmp (plugin, le->key) != 0))
1617 old_ctx = plugin_set_ctx (cf->cf_ctx);
1618 callback = cf->cf_callback;
1620 (*callback) (timeout, identifier, &cf->cf_udata);
1622 plugin_set_ctx (old_ctx);
1627 } /* int plugin_flush */
1629 void plugin_shutdown_all (void)
1633 stop_read_threads ();
1635 destroy_all_callbacks (&list_init);
1637 pthread_mutex_lock (&read_lock);
1638 llist_destroy (read_list);
1640 pthread_mutex_unlock (&read_lock);
1642 destroy_read_heap ();
1644 plugin_flush (/* plugin = */ NULL,
1646 /* identifier = */ NULL);
1649 if (list_shutdown != NULL)
1650 le = llist_head (list_shutdown);
1654 callback_func_t *cf;
1655 plugin_shutdown_cb callback;
1656 plugin_ctx_t old_ctx;
1659 old_ctx = plugin_set_ctx (cf->cf_ctx);
1660 callback = cf->cf_callback;
1662 /* Advance the pointer before calling the callback allows
1663 * shutdown functions to unregister themselves. If done the
1664 * other way around the memory `le' points to will be freed
1665 * after callback returns. */
1670 plugin_set_ctx (old_ctx);
1673 stop_write_threads ();
1675 /* Write plugins which use the `user_data' pointer usually need the
1676 * same data available to the flush callback. If this is the case, set
1677 * the free_function to NULL when registering the flush callback and to
1678 * the real free function when registering the write callback. This way
1679 * the data isn't freed twice. */
1680 destroy_all_callbacks (&list_flush);
1681 destroy_all_callbacks (&list_missing);
1682 destroy_all_callbacks (&list_write);
1684 destroy_all_callbacks (&list_notification);
1685 destroy_all_callbacks (&list_shutdown);
1686 destroy_all_callbacks (&list_log);
1687 } /* void plugin_shutdown_all */
1689 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1693 if (list_missing == NULL)
1696 le = llist_head (list_missing);
1699 callback_func_t *cf;
1700 plugin_missing_cb callback;
1701 plugin_ctx_t old_ctx;
1705 old_ctx = plugin_set_ctx (cf->cf_ctx);
1706 callback = cf->cf_callback;
1708 status = (*callback) (vl, &cf->cf_udata);
1709 plugin_set_ctx (old_ctx);
1714 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1715 "failed with status %i.",
1728 } /* int }}} plugin_dispatch_missing */
1730 static int plugin_dispatch_values_internal (value_list_t *vl)
1733 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1735 value_t *saved_values;
1736 int saved_values_len;
1740 int free_meta_data = 0;
1742 if ((vl == NULL) || (vl->type[0] == 0)
1743 || (vl->values == NULL) || (vl->values_len < 1))
1745 ERROR ("plugin_dispatch_values: Invalid value list "
1746 "from plugin %s.", vl->plugin);
1750 /* Free meta data only if the calling function didn't specify any. In
1751 * this case matches and targets may add some and the calling function
1752 * may not expect (and therefore free) that data. */
1753 if (vl->meta == NULL)
1756 if (list_write == NULL)
1757 c_complain_once (LOG_WARNING, &no_write_complaint,
1758 "plugin_dispatch_values: No write callback has been "
1759 "registered. Please load at least one output plugin, "
1760 "if you want the collected data to be stored.");
1762 if (data_sets == NULL)
1764 ERROR ("plugin_dispatch_values: No data sets registered. "
1765 "Could the types database be read? Check "
1766 "your `TypesDB' setting!");
1770 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1772 char ident[6 * DATA_MAX_NAME_LEN];
1774 FORMAT_VL (ident, sizeof (ident), vl);
1775 INFO ("plugin_dispatch_values: Dataset not found: %s "
1776 "(from \"%s\"), check your types.db!",
1782 vl->time = cdtime ();
1784 if (vl->interval <= 0)
1786 plugin_ctx_t ctx = plugin_get_ctx ();
1788 if (ctx.interval != 0)
1789 vl->interval = ctx.interval;
1792 char name[6 * DATA_MAX_NAME_LEN];
1793 FORMAT_VL (name, sizeof (name), vl);
1794 ERROR ("plugin_dispatch_values: Unable to determine "
1795 "interval from context for "
1796 "value list \"%s\". "
1797 "This indicates a broken plugin. "
1798 "Please report this problem to the "
1799 "collectd mailing list or at "
1800 "<http://collectd.org/bugs/>.", name);
1801 vl->interval = cf_get_default_interval ();
1805 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1807 "plugin = %s; plugin_instance = %s; "
1808 "type = %s; type_instance = %s;",
1809 CDTIME_T_TO_DOUBLE (vl->time),
1810 CDTIME_T_TO_DOUBLE (vl->interval),
1812 vl->plugin, vl->plugin_instance,
1813 vl->type, vl->type_instance);
1816 assert (0 == strcmp (ds->type, vl->type));
1818 if (0 != strcmp (ds->type, vl->type))
1819 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1820 ds->type, vl->type);
1824 assert (ds->ds_num == vl->values_len);
1826 if (ds->ds_num != vl->values_len)
1828 ERROR ("plugin_dispatch_values: ds->type = %s: "
1829 "(ds->ds_num = %i) != "
1830 "(vl->values_len = %i)",
1831 ds->type, ds->ds_num, vl->values_len);
1836 escape_slashes (vl->host, sizeof (vl->host));
1837 escape_slashes (vl->plugin, sizeof (vl->plugin));
1838 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1839 escape_slashes (vl->type, sizeof (vl->type));
1840 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1842 /* Copy the values. This way, we can assure `targets' that they get
1843 * dynamically allocated values, which they can free and replace if
1845 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1847 saved_values = vl->values;
1848 saved_values_len = vl->values_len;
1850 vl->values = (value_t *) calloc (vl->values_len,
1851 sizeof (*vl->values));
1852 if (vl->values == NULL)
1854 ERROR ("plugin_dispatch_values: calloc failed.");
1855 vl->values = saved_values;
1858 memcpy (vl->values, saved_values,
1859 vl->values_len * sizeof (*vl->values));
1861 else /* if ((pre == NULL) && (post == NULL)) */
1863 saved_values = NULL;
1864 saved_values_len = 0;
1867 if (pre_cache_chain != NULL)
1869 status = fc_process_chain (ds, vl, pre_cache_chain);
1872 WARNING ("plugin_dispatch_values: Running the "
1873 "pre-cache chain failed with "
1877 else if (status == FC_TARGET_STOP)
1879 /* Restore the state of the value_list so that plugins
1880 * don't get confused.. */
1881 if (saved_values != NULL)
1884 vl->values = saved_values;
1885 vl->values_len = saved_values_len;
1891 /* Update the value cache */
1894 if (post_cache_chain != NULL)
1896 status = fc_process_chain (ds, vl, post_cache_chain);
1899 WARNING ("plugin_dispatch_values: Running the "
1900 "post-cache chain failed with "
1906 fc_default_action (ds, vl);
1908 /* Restore the state of the value_list so that plugins don't get
1910 if (saved_values != NULL)
1913 vl->values = saved_values;
1914 vl->values_len = saved_values_len;
1917 if ((free_meta_data != 0) && (vl->meta != NULL))
1919 meta_data_destroy (vl->meta);
1924 } /* int plugin_dispatch_values_internal */
1926 int plugin_dispatch_values (value_list_t const *vl)
1930 status = plugin_write_enqueue (vl);
1934 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
1935 "with status %i (%s).", status,
1936 sstrerror (status, errbuf, sizeof (errbuf)));
1943 int plugin_dispatch_notification (const notification_t *notif)
1946 /* Possible TODO: Add flap detection here */
1948 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1949 "time = %.3f; host = %s;",
1950 notif->severity, notif->message,
1951 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1953 /* Nobody cares for notifications */
1954 if (list_notification == NULL)
1957 le = llist_head (list_notification);
1960 callback_func_t *cf;
1961 plugin_notification_cb callback;
1964 /* do not switch plugin context; rather keep the context
1965 * (interval) information of the calling plugin */
1968 callback = cf->cf_callback;
1969 status = (*callback) (notif, &cf->cf_udata);
1972 WARNING ("plugin_dispatch_notification: Notification "
1973 "callback %s returned %i.",
1981 } /* int plugin_dispatch_notification */
1983 void plugin_log (int level, const char *format, ...)
1990 if (level >= LOG_DEBUG)
1994 va_start (ap, format);
1995 vsnprintf (msg, sizeof (msg), format, ap);
1996 msg[sizeof (msg) - 1] = '\0';
1999 if (list_log == NULL)
2001 fprintf (stderr, "%s\n", msg);
2005 le = llist_head (list_log);
2008 callback_func_t *cf;
2009 plugin_log_cb callback;
2012 callback = cf->cf_callback;
2014 /* do not switch plugin context; rather keep the context
2015 * (interval) information of the calling plugin */
2017 (*callback) (level, msg, &cf->cf_udata);
2021 } /* void plugin_log */
2023 int parse_log_severity (const char *severity)
2027 if ((0 == strcasecmp (severity, "emerg"))
2028 || (0 == strcasecmp (severity, "alert"))
2029 || (0 == strcasecmp (severity, "crit"))
2030 || (0 == strcasecmp (severity, "err")))
2031 log_level = LOG_ERR;
2032 else if (0 == strcasecmp (severity, "warning"))
2033 log_level = LOG_WARNING;
2034 else if (0 == strcasecmp (severity, "notice"))
2035 log_level = LOG_NOTICE;
2036 else if (0 == strcasecmp (severity, "info"))
2037 log_level = LOG_INFO;
2039 else if (0 == strcasecmp (severity, "debug"))
2040 log_level = LOG_DEBUG;
2041 #endif /* COLLECT_DEBUG */
2044 } /* int parse_log_severity */
2046 int parse_notif_severity (const char *severity)
2048 int notif_severity = -1;
2050 if (strcasecmp (severity, "FAILURE") == 0)
2051 notif_severity = NOTIF_FAILURE;
2052 else if (strcmp (severity, "OKAY") == 0)
2053 notif_severity = NOTIF_OKAY;
2054 else if ((strcmp (severity, "WARNING") == 0)
2055 || (strcmp (severity, "WARN") == 0))
2056 notif_severity = NOTIF_WARNING;
2058 return (notif_severity);
2059 } /* int parse_notif_severity */
2061 const data_set_t *plugin_get_ds (const char *name)
2065 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2067 DEBUG ("No such dataset registered: %s", name);
2072 } /* data_set_t *plugin_get_ds */
2074 static int plugin_notification_meta_add (notification_t *n,
2076 enum notification_meta_type_e type,
2079 notification_meta_t *meta;
2080 notification_meta_t *tail;
2082 if ((n == NULL) || (name == NULL) || (value == NULL))
2084 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2088 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2091 ERROR ("plugin_notification_meta_add: malloc failed.");
2094 memset (meta, 0, sizeof (notification_meta_t));
2096 sstrncpy (meta->name, name, sizeof (meta->name));
2101 case NM_TYPE_STRING:
2103 meta->nm_value.nm_string = strdup ((const char *) value);
2104 if (meta->nm_value.nm_string == NULL)
2106 ERROR ("plugin_notification_meta_add: strdup failed.");
2112 case NM_TYPE_SIGNED_INT:
2114 meta->nm_value.nm_signed_int = *((int64_t *) value);
2117 case NM_TYPE_UNSIGNED_INT:
2119 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2122 case NM_TYPE_DOUBLE:
2124 meta->nm_value.nm_double = *((double *) value);
2127 case NM_TYPE_BOOLEAN:
2129 meta->nm_value.nm_boolean = *((_Bool *) value);
2134 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2138 } /* switch (type) */
2142 while ((tail != NULL) && (tail->next != NULL))
2151 } /* int plugin_notification_meta_add */
2153 int plugin_notification_meta_add_string (notification_t *n,
2157 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2160 int plugin_notification_meta_add_signed_int (notification_t *n,
2164 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2167 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2171 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2174 int plugin_notification_meta_add_double (notification_t *n,
2178 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2181 int plugin_notification_meta_add_boolean (notification_t *n,
2185 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2188 int plugin_notification_meta_copy (notification_t *dst,
2189 const notification_t *src)
2191 notification_meta_t *meta;
2193 assert (dst != NULL);
2194 assert (src != NULL);
2195 assert (dst != src);
2196 assert ((src->meta == NULL) || (src->meta != dst->meta));
2198 for (meta = src->meta; meta != NULL; meta = meta->next)
2200 if (meta->type == NM_TYPE_STRING)
2201 plugin_notification_meta_add_string (dst, meta->name,
2202 meta->nm_value.nm_string);
2203 else if (meta->type == NM_TYPE_SIGNED_INT)
2204 plugin_notification_meta_add_signed_int (dst, meta->name,
2205 meta->nm_value.nm_signed_int);
2206 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2207 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2208 meta->nm_value.nm_unsigned_int);
2209 else if (meta->type == NM_TYPE_DOUBLE)
2210 plugin_notification_meta_add_double (dst, meta->name,
2211 meta->nm_value.nm_double);
2212 else if (meta->type == NM_TYPE_BOOLEAN)
2213 plugin_notification_meta_add_boolean (dst, meta->name,
2214 meta->nm_value.nm_boolean);
2218 } /* int plugin_notification_meta_copy */
2220 int plugin_notification_meta_free (notification_meta_t *n)
2222 notification_meta_t *this;
2223 notification_meta_t *next;
2227 ERROR ("plugin_notification_meta_free: n == NULL!");
2232 while (this != NULL)
2236 if (this->type == NM_TYPE_STRING)
2238 free ((char *)this->nm_value.nm_string);
2239 this->nm_value.nm_string = NULL;
2247 } /* int plugin_notification_meta_free */
2249 static void plugin_ctx_destructor (void *ctx)
2252 } /* void plugin_ctx_destructor */
2254 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2256 static plugin_ctx_t *plugin_ctx_create (void)
2260 ctx = malloc (sizeof (*ctx));
2263 ERROR ("Failed to allocate plugin context: %s",
2264 sstrerror (errno, errbuf, sizeof (errbuf)));
2269 assert (plugin_ctx_key_initialized);
2270 pthread_setspecific (plugin_ctx_key, ctx);
2271 DEBUG("Created new plugin context.");
2273 } /* int plugin_ctx_create */
2275 void plugin_init_ctx (void)
2277 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2278 plugin_ctx_key_initialized = 1;
2279 } /* void plugin_init_ctx */
2281 plugin_ctx_t plugin_get_ctx (void)
2285 assert (plugin_ctx_key_initialized);
2286 ctx = pthread_getspecific (plugin_ctx_key);
2289 ctx = plugin_ctx_create ();
2290 /* this must no happen -- exit() instead? */
2296 } /* plugin_ctx_t plugin_get_ctx */
2298 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2303 assert (plugin_ctx_key_initialized);
2304 c = pthread_getspecific (plugin_ctx_key);
2307 c = plugin_ctx_create ();
2308 /* this must no happen -- exit() instead? */
2317 } /* void plugin_set_ctx */
2319 cdtime_t plugin_get_interval (void)
2323 interval = plugin_get_ctx().interval;
2327 return cf_get_default_interval ();
2328 } /* cdtime_t plugin_get_interval */
2332 void *(*start_routine) (void *);
2336 static void *plugin_thread_start (void *arg)
2338 plugin_thread_t *plugin_thread = arg;
2340 void *(*start_routine) (void *) = plugin_thread->start_routine;
2341 void *plugin_arg = plugin_thread->arg;
2343 plugin_set_ctx (plugin_thread->ctx);
2345 free (plugin_thread);
2347 return start_routine (plugin_arg);
2348 } /* void *plugin_thread_start */
2350 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2351 void *(*start_routine) (void *), void *arg)
2353 plugin_thread_t *plugin_thread;
2355 plugin_thread = malloc (sizeof (*plugin_thread));
2356 if (plugin_thread == NULL)
2359 plugin_thread->ctx = plugin_get_ctx ();
2360 plugin_thread->start_routine = start_routine;
2361 plugin_thread->arg = arg;
2363 return pthread_create (thread, attr,
2364 plugin_thread_start, plugin_thread);
2365 } /* int plugin_thread_create */
2367 /* vim: set sw=8 ts=8 noet fdm=marker : */