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 DEBUG ("type = %s", type);
831 dir = plugin_get_dir ();
834 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
835 * type when matching the filename */
836 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
837 if ((status < 0) || ((size_t) status >= sizeof (typename)))
839 WARNING ("snprintf: truncated: `%s.so'", type);
842 typename_len = strlen (typename);
844 if ((dh = opendir (dir)) == NULL)
847 ERROR ("opendir (%s): %s", dir,
848 sstrerror (errno, errbuf, sizeof (errbuf)));
852 while ((de = readdir (dh)) != NULL)
854 if (strncasecmp (de->d_name, typename, typename_len))
857 status = ssnprintf (filename, sizeof (filename),
858 "%s/%s", dir, de->d_name);
859 if ((status < 0) || ((size_t) status >= sizeof (filename)))
861 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
865 if (lstat (filename, &statbuf) == -1)
868 WARNING ("stat %s: %s", filename,
869 sstrerror (errno, errbuf, sizeof (errbuf)));
872 else if (!S_ISREG (statbuf.st_mode))
874 /* don't follow symlinks */
875 WARNING ("stat %s: not a regular file", filename);
879 if (plugin_load_file (filename, flags) == 0)
887 fprintf (stderr, "Unable to load plugin %s.\n", type);
893 if (filename[0] == '\0')
894 fprintf (stderr, "Could not find plugin %s.\n", type);
900 * The `register_*' functions follow
902 int plugin_register_config (const char *name,
903 int (*callback) (const char *key, const char *val),
904 const char **keys, int keys_num)
906 cf_register (name, callback, keys, keys_num);
908 } /* int plugin_register_config */
910 int plugin_register_complex_config (const char *type,
911 int (*callback) (oconfig_item_t *))
913 return (cf_register_complex (type, callback));
914 } /* int plugin_register_complex_config */
916 int plugin_register_init (const char *name,
917 int (*callback) (void))
919 return (create_register_callback (&list_init, name, (void *) callback,
920 /* user_data = */ NULL));
921 } /* plugin_register_init */
923 static int plugin_compare_read_func (const void *arg0, const void *arg1)
925 const read_func_t *rf0;
926 const read_func_t *rf1;
931 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
933 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
935 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
937 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
941 } /* int plugin_compare_read_func */
943 /* Add a read function to both, the heap and a linked list. The linked list if
944 * used to look-up read functions, especially for the remove function. The heap
945 * is used to determine which plugin to read next. */
946 static int plugin_insert_read (read_func_t *rf)
951 pthread_mutex_lock (&read_lock);
953 if (read_list == NULL)
955 read_list = llist_create ();
956 if (read_list == NULL)
958 pthread_mutex_unlock (&read_lock);
959 ERROR ("plugin_insert_read: read_list failed.");
964 if (read_heap == NULL)
966 read_heap = c_heap_create (plugin_compare_read_func);
967 if (read_heap == NULL)
969 pthread_mutex_unlock (&read_lock);
970 ERROR ("plugin_insert_read: c_heap_create failed.");
975 le = llist_search (read_list, rf->rf_name);
978 pthread_mutex_unlock (&read_lock);
979 WARNING ("The read function \"%s\" is already registered. "
980 "Check for duplicate \"LoadPlugin\" lines "
981 "in your configuration!",
986 le = llentry_create (rf->rf_name, rf);
989 pthread_mutex_unlock (&read_lock);
990 ERROR ("plugin_insert_read: llentry_create failed.");
994 status = c_heap_insert (read_heap, rf);
997 pthread_mutex_unlock (&read_lock);
998 ERROR ("plugin_insert_read: c_heap_insert failed.");
999 llentry_destroy (le);
1003 /* This does not fail. */
1004 llist_append (read_list, le);
1006 /* Wake up all the read threads. */
1007 pthread_cond_broadcast (&read_cond);
1008 pthread_mutex_unlock (&read_lock);
1010 } /* int plugin_insert_read */
1012 static int read_cb_wrapper (user_data_t *ud)
1014 int (*callback) (void);
1019 callback = ud->data;
1021 } /* int read_cb_wrapper */
1023 int plugin_register_read (const char *name,
1024 int (*callback) (void))
1027 plugin_ctx_t ctx = plugin_get_ctx ();
1030 if (ctx.interval != 0) {
1031 /* If ctx.interval is not zero (== use the plugin or global
1032 * interval), we need to use the "complex" read callback,
1033 * because only that allows to specify a different interval.
1034 * Wrap the callback using read_cb_wrapper(). */
1035 struct timespec interval;
1036 user_data_t user_data;
1038 user_data.data = callback;
1039 user_data.free_func = NULL;
1041 CDTIME_T_TO_TIMESPEC (ctx.interval, &interval);
1042 return plugin_register_complex_read (/* group = */ NULL,
1043 name, read_cb_wrapper, &interval, &user_data);
1046 DEBUG ("plugin_register_read: default_interval = %.3f",
1047 CDTIME_T_TO_DOUBLE(plugin_get_interval ()));
1049 rf = malloc (sizeof (*rf));
1052 ERROR ("plugin_register_read: malloc failed.");
1056 memset (rf, 0, sizeof (read_func_t));
1057 rf->rf_callback = (void *) callback;
1058 rf->rf_udata.data = NULL;
1059 rf->rf_udata.free_func = NULL;
1061 rf->rf_group[0] = '\0';
1062 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1063 rf->rf_type = RF_SIMPLE;
1064 rf->rf_interval.tv_sec = 0;
1065 rf->rf_interval.tv_nsec = 0;
1066 rf->rf_effective_interval = rf->rf_interval;
1068 status = plugin_insert_read (rf);
1073 } /* int plugin_register_read */
1075 int plugin_register_complex_read (const char *group, const char *name,
1076 plugin_read_cb callback,
1077 const struct timespec *interval,
1078 user_data_t *user_data)
1081 plugin_ctx_t ctx = plugin_get_ctx ();
1084 rf = malloc (sizeof (*rf));
1087 ERROR ("plugin_register_complex_read: malloc failed.");
1091 memset (rf, 0, sizeof (read_func_t));
1092 rf->rf_callback = (void *) callback;
1094 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1096 rf->rf_group[0] = '\0';
1097 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1098 rf->rf_type = RF_COMPLEX;
1099 if (interval != NULL)
1101 rf->rf_interval = *interval;
1103 else if (ctx.interval != 0)
1105 CDTIME_T_TO_TIMESPEC (ctx.interval, &rf->rf_interval);
1107 rf->rf_effective_interval = rf->rf_interval;
1109 DEBUG ("plugin_register_read: interval = %i.%09i",
1110 (int) rf->rf_interval.tv_sec,
1111 (int) rf->rf_interval.tv_nsec);
1114 if (user_data == NULL)
1116 rf->rf_udata.data = NULL;
1117 rf->rf_udata.free_func = NULL;
1121 rf->rf_udata = *user_data;
1126 status = plugin_insert_read (rf);
1131 } /* int plugin_register_complex_read */
1133 int plugin_register_write (const char *name,
1134 plugin_write_cb callback, user_data_t *ud)
1136 return (create_register_callback (&list_write, name,
1137 (void *) callback, ud));
1138 } /* int plugin_register_write */
1140 int plugin_register_flush (const char *name,
1141 plugin_flush_cb callback, user_data_t *ud)
1143 return (create_register_callback (&list_flush, name,
1144 (void *) callback, ud));
1145 } /* int plugin_register_flush */
1147 int plugin_register_missing (const char *name,
1148 plugin_missing_cb callback, user_data_t *ud)
1150 return (create_register_callback (&list_missing, name,
1151 (void *) callback, ud));
1152 } /* int plugin_register_missing */
1154 int plugin_register_shutdown (const char *name,
1155 int (*callback) (void))
1157 return (create_register_callback (&list_shutdown, name,
1158 (void *) callback, /* user_data = */ NULL));
1159 } /* int plugin_register_shutdown */
1161 int plugin_register_data_set (const data_set_t *ds)
1163 data_set_t *ds_copy;
1166 if ((data_sets != NULL)
1167 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1169 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1170 plugin_unregister_data_set (ds->type);
1172 else if (data_sets == NULL)
1174 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1175 if (data_sets == NULL)
1179 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1180 if (ds_copy == NULL)
1182 memcpy(ds_copy, ds, sizeof (data_set_t));
1184 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1186 if (ds_copy->ds == NULL)
1192 for (i = 0; i < ds->ds_num; i++)
1193 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1195 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1196 } /* int plugin_register_data_set */
1198 int plugin_register_log (const char *name,
1199 plugin_log_cb callback, user_data_t *ud)
1201 return (create_register_callback (&list_log, name,
1202 (void *) callback, ud));
1203 } /* int plugin_register_log */
1205 int plugin_register_notification (const char *name,
1206 plugin_notification_cb callback, user_data_t *ud)
1208 return (create_register_callback (&list_notification, name,
1209 (void *) callback, ud));
1210 } /* int plugin_register_log */
1212 int plugin_unregister_config (const char *name)
1214 cf_unregister (name);
1216 } /* int plugin_unregister_config */
1218 int plugin_unregister_complex_config (const char *name)
1220 cf_unregister_complex (name);
1222 } /* int plugin_unregister_complex_config */
1224 int plugin_unregister_init (const char *name)
1226 return (plugin_unregister (list_init, name));
1229 int plugin_unregister_read (const char *name) /* {{{ */
1237 pthread_mutex_lock (&read_lock);
1239 if (read_list == NULL)
1241 pthread_mutex_unlock (&read_lock);
1245 le = llist_search (read_list, name);
1248 pthread_mutex_unlock (&read_lock);
1249 WARNING ("plugin_unregister_read: No such read function: %s",
1254 llist_remove (read_list, le);
1257 assert (rf != NULL);
1258 rf->rf_type = RF_REMOVE;
1260 pthread_mutex_unlock (&read_lock);
1262 llentry_destroy (le);
1264 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1267 } /* }}} int plugin_unregister_read */
1269 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1271 read_func_t *rf = e->value;
1274 return strcmp (rf->rf_group, (const char *)group);
1275 } /* }}} int compare_read_func_group */
1277 int plugin_unregister_read_group (const char *group) /* {{{ */
1287 pthread_mutex_lock (&read_lock);
1289 if (read_list == NULL)
1291 pthread_mutex_unlock (&read_lock);
1297 le = llist_search_custom (read_list,
1298 compare_read_func_group, (void *)group);
1305 llist_remove (read_list, le);
1308 assert (rf != NULL);
1309 rf->rf_type = RF_REMOVE;
1311 llentry_destroy (le);
1313 DEBUG ("plugin_unregister_read_group: "
1314 "Marked `%s' (group `%s') for removal.",
1315 rf->rf_name, group);
1318 pthread_mutex_unlock (&read_lock);
1322 WARNING ("plugin_unregister_read_group: No such "
1323 "group of read function: %s", group);
1328 } /* }}} int plugin_unregister_read_group */
1330 int plugin_unregister_write (const char *name)
1332 return (plugin_unregister (list_write, name));
1335 int plugin_unregister_flush (const char *name)
1337 return (plugin_unregister (list_flush, name));
1340 int plugin_unregister_missing (const char *name)
1342 return (plugin_unregister (list_missing, name));
1345 int plugin_unregister_shutdown (const char *name)
1347 return (plugin_unregister (list_shutdown, name));
1350 int plugin_unregister_data_set (const char *name)
1354 if (data_sets == NULL)
1357 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1364 } /* int plugin_unregister_data_set */
1366 int plugin_unregister_log (const char *name)
1368 return (plugin_unregister (list_log, name));
1371 int plugin_unregister_notification (const char *name)
1373 return (plugin_unregister (list_notification, name));
1376 void plugin_init_all (void)
1378 const char *chain_name;
1382 /* Init the value cache */
1385 chain_name = global_option_get ("PreCacheChain");
1386 pre_cache_chain = fc_chain_get_by_name (chain_name);
1388 chain_name = global_option_get ("PostCacheChain");
1389 post_cache_chain = fc_chain_get_by_name (chain_name);
1392 char const *tmp = global_option_get ("WriteThreads");
1393 int num = atoi (tmp);
1398 start_write_threads ((size_t) num);
1401 if ((list_init == NULL) && (read_heap == NULL))
1404 /* Calling all init callbacks before checking if read callbacks
1405 * are available allows the init callbacks to register the read
1407 le = llist_head (list_init);
1410 callback_func_t *cf;
1411 plugin_init_cb callback;
1412 plugin_ctx_t old_ctx;
1415 old_ctx = plugin_set_ctx (cf->cf_ctx);
1416 callback = cf->cf_callback;
1417 status = (*callback) ();
1418 plugin_set_ctx (old_ctx);
1422 ERROR ("Initialization of plugin `%s' "
1423 "failed with status %i. "
1424 "Plugin will be unloaded.",
1426 /* Plugins that register read callbacks from the init
1427 * callback should take care of appropriate error
1428 * handling themselves. */
1429 /* FIXME: Unload _all_ functions */
1430 plugin_unregister_read (le->key);
1436 /* Start read-threads */
1437 if (read_heap != NULL)
1441 rt = global_option_get ("ReadThreads");
1444 start_read_threads ((num > 0) ? num : 5);
1446 } /* void plugin_init_all */
1448 /* TODO: Rename this function. */
1449 void plugin_read_all (void)
1451 uc_check_timeout ();
1454 } /* void plugin_read_all */
1456 /* Read function called when the `-T' command line argument is given. */
1457 int plugin_read_all_once (void)
1460 int return_status = 0;
1462 if (read_heap == NULL)
1464 NOTICE ("No read-functions are registered.");
1471 plugin_ctx_t old_ctx;
1473 rf = c_heap_get_root (read_heap);
1477 old_ctx = plugin_set_ctx (rf->rf_ctx);
1479 if (rf->rf_type == RF_SIMPLE)
1481 int (*callback) (void);
1483 callback = rf->rf_callback;
1484 status = (*callback) ();
1488 plugin_read_cb callback;
1490 callback = rf->rf_callback;
1491 status = (*callback) (&rf->rf_udata);
1494 plugin_set_ctx (old_ctx);
1498 NOTICE ("read-function of plugin `%s' failed.",
1503 destroy_callback ((void *) rf);
1506 return (return_status);
1507 } /* int plugin_read_all_once */
1509 int plugin_write (const char *plugin, /* {{{ */
1510 const data_set_t *ds, const value_list_t *vl)
1518 if (list_write == NULL)
1523 ds = plugin_get_ds (vl->type);
1526 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1536 le = llist_head (list_write);
1539 callback_func_t *cf = le->value;
1540 plugin_write_cb callback;
1542 /* do not switch plugin context; rather keep the context (interval)
1543 * information of the calling read plugin */
1545 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1546 callback = cf->cf_callback;
1547 status = (*callback) (ds, vl, &cf->cf_udata);
1556 if ((success == 0) && (failure != 0))
1561 else /* plugin != NULL */
1563 callback_func_t *cf;
1564 plugin_write_cb callback;
1566 le = llist_head (list_write);
1569 if (strcasecmp (plugin, le->key) == 0)
1580 /* do not switch plugin context; rather keep the context (interval)
1581 * information of the calling read plugin */
1583 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1584 callback = cf->cf_callback;
1585 status = (*callback) (ds, vl, &cf->cf_udata);
1589 } /* }}} int plugin_write */
1591 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1595 if (list_flush == NULL)
1598 le = llist_head (list_flush);
1601 callback_func_t *cf;
1602 plugin_flush_cb callback;
1603 plugin_ctx_t old_ctx;
1605 if ((plugin != NULL)
1606 && (strcmp (plugin, le->key) != 0))
1613 old_ctx = plugin_set_ctx (cf->cf_ctx);
1614 callback = cf->cf_callback;
1616 (*callback) (timeout, identifier, &cf->cf_udata);
1618 plugin_set_ctx (old_ctx);
1623 } /* int plugin_flush */
1625 void plugin_shutdown_all (void)
1629 stop_read_threads ();
1631 destroy_all_callbacks (&list_init);
1633 pthread_mutex_lock (&read_lock);
1634 llist_destroy (read_list);
1636 pthread_mutex_unlock (&read_lock);
1638 destroy_read_heap ();
1640 plugin_flush (/* plugin = */ NULL,
1642 /* identifier = */ NULL);
1645 if (list_shutdown != NULL)
1646 le = llist_head (list_shutdown);
1650 callback_func_t *cf;
1651 plugin_shutdown_cb callback;
1652 plugin_ctx_t old_ctx;
1655 old_ctx = plugin_set_ctx (cf->cf_ctx);
1656 callback = cf->cf_callback;
1658 /* Advance the pointer before calling the callback allows
1659 * shutdown functions to unregister themselves. If done the
1660 * other way around the memory `le' points to will be freed
1661 * after callback returns. */
1666 plugin_set_ctx (old_ctx);
1669 stop_write_threads ();
1671 /* Write plugins which use the `user_data' pointer usually need the
1672 * same data available to the flush callback. If this is the case, set
1673 * the free_function to NULL when registering the flush callback and to
1674 * the real free function when registering the write callback. This way
1675 * the data isn't freed twice. */
1676 destroy_all_callbacks (&list_flush);
1677 destroy_all_callbacks (&list_missing);
1678 destroy_all_callbacks (&list_write);
1680 destroy_all_callbacks (&list_notification);
1681 destroy_all_callbacks (&list_shutdown);
1682 destroy_all_callbacks (&list_log);
1683 } /* void plugin_shutdown_all */
1685 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1689 if (list_missing == NULL)
1692 le = llist_head (list_missing);
1695 callback_func_t *cf;
1696 plugin_missing_cb callback;
1697 plugin_ctx_t old_ctx;
1701 old_ctx = plugin_set_ctx (cf->cf_ctx);
1702 callback = cf->cf_callback;
1704 status = (*callback) (vl, &cf->cf_udata);
1705 plugin_set_ctx (old_ctx);
1710 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1711 "failed with status %i.",
1724 } /* int }}} plugin_dispatch_missing */
1726 static int plugin_dispatch_values_internal (value_list_t *vl)
1729 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1731 value_t *saved_values;
1732 int saved_values_len;
1736 int free_meta_data = 0;
1738 if ((vl == NULL) || (vl->type[0] == 0)
1739 || (vl->values == NULL) || (vl->values_len < 1))
1741 ERROR ("plugin_dispatch_values: Invalid value list "
1742 "from plugin %s.", vl->plugin);
1746 /* Free meta data only if the calling function didn't specify any. In
1747 * this case matches and targets may add some and the calling function
1748 * may not expect (and therefore free) that data. */
1749 if (vl->meta == NULL)
1752 if (list_write == NULL)
1753 c_complain_once (LOG_WARNING, &no_write_complaint,
1754 "plugin_dispatch_values: No write callback has been "
1755 "registered. Please load at least one output plugin, "
1756 "if you want the collected data to be stored.");
1758 if (data_sets == NULL)
1760 ERROR ("plugin_dispatch_values: No data sets registered. "
1761 "Could the types database be read? Check "
1762 "your `TypesDB' setting!");
1766 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1768 char ident[6 * DATA_MAX_NAME_LEN];
1770 FORMAT_VL (ident, sizeof (ident), vl);
1771 INFO ("plugin_dispatch_values: Dataset not found: %s "
1772 "(from \"%s\"), check your types.db!",
1778 vl->time = cdtime ();
1780 if (vl->interval <= 0)
1782 plugin_ctx_t ctx = plugin_get_ctx ();
1784 if (ctx.interval != 0)
1785 vl->interval = ctx.interval;
1788 char name[6 * DATA_MAX_NAME_LEN];
1789 FORMAT_VL (name, sizeof (name), vl);
1790 ERROR ("plugin_dispatch_values: Unable to determine "
1791 "interval from context for "
1792 "value list \"%s\". "
1793 "This indicates a broken plugin. "
1794 "Please report this problem to the "
1795 "collectd mailing list or at "
1796 "<http://collectd.org/bugs/>.", name);
1797 vl->interval = cf_get_default_interval ();
1801 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1803 "plugin = %s; plugin_instance = %s; "
1804 "type = %s; type_instance = %s;",
1805 CDTIME_T_TO_DOUBLE (vl->time),
1806 CDTIME_T_TO_DOUBLE (vl->interval),
1808 vl->plugin, vl->plugin_instance,
1809 vl->type, vl->type_instance);
1812 assert (0 == strcmp (ds->type, vl->type));
1814 if (0 != strcmp (ds->type, vl->type))
1815 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1816 ds->type, vl->type);
1820 assert (ds->ds_num == vl->values_len);
1822 if (ds->ds_num != vl->values_len)
1824 ERROR ("plugin_dispatch_values: ds->type = %s: "
1825 "(ds->ds_num = %i) != "
1826 "(vl->values_len = %i)",
1827 ds->type, ds->ds_num, vl->values_len);
1832 escape_slashes (vl->host, sizeof (vl->host));
1833 escape_slashes (vl->plugin, sizeof (vl->plugin));
1834 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1835 escape_slashes (vl->type, sizeof (vl->type));
1836 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1838 /* Copy the values. This way, we can assure `targets' that they get
1839 * dynamically allocated values, which they can free and replace if
1841 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1843 saved_values = vl->values;
1844 saved_values_len = vl->values_len;
1846 vl->values = (value_t *) calloc (vl->values_len,
1847 sizeof (*vl->values));
1848 if (vl->values == NULL)
1850 ERROR ("plugin_dispatch_values: calloc failed.");
1851 vl->values = saved_values;
1854 memcpy (vl->values, saved_values,
1855 vl->values_len * sizeof (*vl->values));
1857 else /* if ((pre == NULL) && (post == NULL)) */
1859 saved_values = NULL;
1860 saved_values_len = 0;
1863 if (pre_cache_chain != NULL)
1865 status = fc_process_chain (ds, vl, pre_cache_chain);
1868 WARNING ("plugin_dispatch_values: Running the "
1869 "pre-cache chain failed with "
1873 else if (status == FC_TARGET_STOP)
1875 /* Restore the state of the value_list so that plugins
1876 * don't get confused.. */
1877 if (saved_values != NULL)
1880 vl->values = saved_values;
1881 vl->values_len = saved_values_len;
1887 /* Update the value cache */
1890 if (post_cache_chain != NULL)
1892 status = fc_process_chain (ds, vl, post_cache_chain);
1895 WARNING ("plugin_dispatch_values: Running the "
1896 "post-cache chain failed with "
1902 fc_default_action (ds, vl);
1904 /* Restore the state of the value_list so that plugins don't get
1906 if (saved_values != NULL)
1909 vl->values = saved_values;
1910 vl->values_len = saved_values_len;
1913 if ((free_meta_data != 0) && (vl->meta != NULL))
1915 meta_data_destroy (vl->meta);
1920 } /* int plugin_dispatch_values_internal */
1922 int plugin_dispatch_values (value_list_t const *vl)
1926 status = plugin_write_enqueue (vl);
1930 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
1931 "with status %i (%s).", status,
1932 sstrerror (status, errbuf, sizeof (errbuf)));
1939 int plugin_dispatch_notification (const notification_t *notif)
1942 /* Possible TODO: Add flap detection here */
1944 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1945 "time = %.3f; host = %s;",
1946 notif->severity, notif->message,
1947 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1949 /* Nobody cares for notifications */
1950 if (list_notification == NULL)
1953 le = llist_head (list_notification);
1956 callback_func_t *cf;
1957 plugin_notification_cb callback;
1960 /* do not switch plugin context; rather keep the context
1961 * (interval) information of the calling plugin */
1964 callback = cf->cf_callback;
1965 status = (*callback) (notif, &cf->cf_udata);
1968 WARNING ("plugin_dispatch_notification: Notification "
1969 "callback %s returned %i.",
1977 } /* int plugin_dispatch_notification */
1979 void plugin_log (int level, const char *format, ...)
1986 if (level >= LOG_DEBUG)
1990 va_start (ap, format);
1991 vsnprintf (msg, sizeof (msg), format, ap);
1992 msg[sizeof (msg) - 1] = '\0';
1995 if (list_log == NULL)
1997 fprintf (stderr, "%s\n", msg);
2001 le = llist_head (list_log);
2004 callback_func_t *cf;
2005 plugin_log_cb callback;
2008 callback = cf->cf_callback;
2010 /* do not switch plugin context; rather keep the context
2011 * (interval) information of the calling plugin */
2013 (*callback) (level, msg, &cf->cf_udata);
2017 } /* void plugin_log */
2019 int parse_log_severity (const char *severity)
2023 if ((0 == strcasecmp (severity, "emerg"))
2024 || (0 == strcasecmp (severity, "alert"))
2025 || (0 == strcasecmp (severity, "crit"))
2026 || (0 == strcasecmp (severity, "err")))
2027 log_level = LOG_ERR;
2028 else if (0 == strcasecmp (severity, "warning"))
2029 log_level = LOG_WARNING;
2030 else if (0 == strcasecmp (severity, "notice"))
2031 log_level = LOG_NOTICE;
2032 else if (0 == strcasecmp (severity, "info"))
2033 log_level = LOG_INFO;
2035 else if (0 == strcasecmp (severity, "debug"))
2036 log_level = LOG_DEBUG;
2037 #endif /* COLLECT_DEBUG */
2040 } /* int parse_log_severity */
2042 int parse_notif_severity (const char *severity)
2044 int notif_severity = -1;
2046 if (strcasecmp (severity, "FAILURE") == 0)
2047 notif_severity = NOTIF_FAILURE;
2048 else if (strcmp (severity, "OKAY") == 0)
2049 notif_severity = NOTIF_OKAY;
2050 else if ((strcmp (severity, "WARNING") == 0)
2051 || (strcmp (severity, "WARN") == 0))
2052 notif_severity = NOTIF_WARNING;
2054 return (notif_severity);
2055 } /* int parse_notif_severity */
2057 const data_set_t *plugin_get_ds (const char *name)
2061 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2063 DEBUG ("No such dataset registered: %s", name);
2068 } /* data_set_t *plugin_get_ds */
2070 static int plugin_notification_meta_add (notification_t *n,
2072 enum notification_meta_type_e type,
2075 notification_meta_t *meta;
2076 notification_meta_t *tail;
2078 if ((n == NULL) || (name == NULL) || (value == NULL))
2080 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2084 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2087 ERROR ("plugin_notification_meta_add: malloc failed.");
2090 memset (meta, 0, sizeof (notification_meta_t));
2092 sstrncpy (meta->name, name, sizeof (meta->name));
2097 case NM_TYPE_STRING:
2099 meta->nm_value.nm_string = strdup ((const char *) value);
2100 if (meta->nm_value.nm_string == NULL)
2102 ERROR ("plugin_notification_meta_add: strdup failed.");
2108 case NM_TYPE_SIGNED_INT:
2110 meta->nm_value.nm_signed_int = *((int64_t *) value);
2113 case NM_TYPE_UNSIGNED_INT:
2115 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2118 case NM_TYPE_DOUBLE:
2120 meta->nm_value.nm_double = *((double *) value);
2123 case NM_TYPE_BOOLEAN:
2125 meta->nm_value.nm_boolean = *((_Bool *) value);
2130 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2134 } /* switch (type) */
2138 while ((tail != NULL) && (tail->next != NULL))
2147 } /* int plugin_notification_meta_add */
2149 int plugin_notification_meta_add_string (notification_t *n,
2153 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2156 int plugin_notification_meta_add_signed_int (notification_t *n,
2160 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2163 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2167 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2170 int plugin_notification_meta_add_double (notification_t *n,
2174 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2177 int plugin_notification_meta_add_boolean (notification_t *n,
2181 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2184 int plugin_notification_meta_copy (notification_t *dst,
2185 const notification_t *src)
2187 notification_meta_t *meta;
2189 assert (dst != NULL);
2190 assert (src != NULL);
2191 assert (dst != src);
2192 assert ((src->meta == NULL) || (src->meta != dst->meta));
2194 for (meta = src->meta; meta != NULL; meta = meta->next)
2196 if (meta->type == NM_TYPE_STRING)
2197 plugin_notification_meta_add_string (dst, meta->name,
2198 meta->nm_value.nm_string);
2199 else if (meta->type == NM_TYPE_SIGNED_INT)
2200 plugin_notification_meta_add_signed_int (dst, meta->name,
2201 meta->nm_value.nm_signed_int);
2202 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2203 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2204 meta->nm_value.nm_unsigned_int);
2205 else if (meta->type == NM_TYPE_DOUBLE)
2206 plugin_notification_meta_add_double (dst, meta->name,
2207 meta->nm_value.nm_double);
2208 else if (meta->type == NM_TYPE_BOOLEAN)
2209 plugin_notification_meta_add_boolean (dst, meta->name,
2210 meta->nm_value.nm_boolean);
2214 } /* int plugin_notification_meta_copy */
2216 int plugin_notification_meta_free (notification_meta_t *n)
2218 notification_meta_t *this;
2219 notification_meta_t *next;
2223 ERROR ("plugin_notification_meta_free: n == NULL!");
2228 while (this != NULL)
2232 if (this->type == NM_TYPE_STRING)
2234 free ((char *)this->nm_value.nm_string);
2235 this->nm_value.nm_string = NULL;
2243 } /* int plugin_notification_meta_free */
2245 static void plugin_ctx_destructor (void *ctx)
2248 } /* void plugin_ctx_destructor */
2250 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2252 static plugin_ctx_t *plugin_ctx_create (void)
2256 ctx = malloc (sizeof (*ctx));
2259 ERROR ("Failed to allocate plugin context: %s",
2260 sstrerror (errno, errbuf, sizeof (errbuf)));
2265 assert (plugin_ctx_key_initialized);
2266 pthread_setspecific (plugin_ctx_key, ctx);
2267 DEBUG("Created new plugin context.");
2269 } /* int plugin_ctx_create */
2271 void plugin_init_ctx (void)
2273 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2274 plugin_ctx_key_initialized = 1;
2275 } /* void plugin_init_ctx */
2277 plugin_ctx_t plugin_get_ctx (void)
2281 assert (plugin_ctx_key_initialized);
2282 ctx = pthread_getspecific (plugin_ctx_key);
2285 ctx = plugin_ctx_create ();
2286 /* this must no happen -- exit() instead? */
2292 } /* plugin_ctx_t plugin_get_ctx */
2294 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2299 assert (plugin_ctx_key_initialized);
2300 c = pthread_getspecific (plugin_ctx_key);
2303 c = plugin_ctx_create ();
2304 /* this must no happen -- exit() instead? */
2313 } /* void plugin_set_ctx */
2315 cdtime_t plugin_get_interval (void)
2319 interval = plugin_get_ctx().interval;
2323 return cf_get_default_interval ();
2324 } /* cdtime_t plugin_get_interval */
2328 void *(*start_routine) (void *);
2332 static void *plugin_thread_start (void *arg)
2334 plugin_thread_t *plugin_thread = arg;
2336 void *(*start_routine) (void *) = plugin_thread->start_routine;
2337 void *plugin_arg = plugin_thread->arg;
2339 plugin_set_ctx (plugin_thread->ctx);
2341 free (plugin_thread);
2343 return start_routine (plugin_arg);
2344 } /* void *plugin_thread_start */
2346 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2347 void *(*start_routine) (void *), void *arg)
2349 plugin_thread_t *plugin_thread;
2351 plugin_thread = malloc (sizeof (*plugin_thread));
2352 if (plugin_thread == NULL)
2355 plugin_thread->ctx = plugin_get_ctx ();
2356 plugin_thread->start_routine = start_routine;
2357 plugin_thread->arg = arg;
2359 return pthread_create (thread, attr,
2360 plugin_thread_start, plugin_thread);
2361 } /* int plugin_thread_create */
2363 /* vim: set sw=8 ts=8 noet fdm=marker : */