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;
84 static llist_t *list_init;
85 static llist_t *list_write;
86 static llist_t *list_flush;
87 static llist_t *list_missing;
88 static llist_t *list_shutdown;
89 static llist_t *list_log;
90 static llist_t *list_notification;
92 static fc_chain_t *pre_cache_chain = NULL;
93 static fc_chain_t *post_cache_chain = NULL;
95 static c_avl_tree_t *data_sets;
97 static char *plugindir = NULL;
99 static c_heap_t *read_heap = NULL;
100 static llist_t *read_list;
101 static int read_loop = 1;
102 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
103 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
104 static pthread_t *read_threads = NULL;
105 static int read_threads_num = 0;
107 static write_queue_t *write_queue_head;
108 static write_queue_t *write_queue_tail;
109 static _Bool write_loop = 1;
110 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
111 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
112 static pthread_t *write_threads = NULL;
113 static size_t write_threads_num = 0;
115 static pthread_key_t plugin_ctx_key;
116 static _Bool plugin_ctx_key_initialized = 0;
121 static int plugin_dispatch_values_internal (value_list_t *vl);
123 static const char *plugin_get_dir (void)
125 if (plugindir == NULL)
131 static void destroy_callback (callback_func_t *cf) /* {{{ */
136 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
138 cf->cf_udata.free_func (cf->cf_udata.data);
139 cf->cf_udata.data = NULL;
140 cf->cf_udata.free_func = NULL;
143 } /* }}} void destroy_callback */
145 static void destroy_all_callbacks (llist_t **list) /* {{{ */
152 le = llist_head (*list);
160 destroy_callback (le->value);
166 llist_destroy (*list);
168 } /* }}} void destroy_all_callbacks */
170 static void destroy_read_heap (void) /* {{{ */
172 if (read_heap == NULL)
179 cf = c_heap_get_root (read_heap);
183 destroy_callback (cf);
186 c_heap_destroy (read_heap);
188 } /* }}} void destroy_read_heap */
190 static int register_callback (llist_t **list, /* {{{ */
191 const char *name, callback_func_t *cf)
198 *list = llist_create ();
201 ERROR ("plugin: register_callback: "
202 "llist_create failed.");
203 destroy_callback (cf);
211 ERROR ("plugin: register_callback: strdup failed.");
212 destroy_callback (cf);
216 le = llist_search (*list, name);
219 le = llentry_create (key, cf);
222 ERROR ("plugin: register_callback: "
223 "llentry_create failed.");
225 destroy_callback (cf);
229 llist_append (*list, le);
233 callback_func_t *old_cf;
238 WARNING ("plugin: register_callback: "
239 "a callback named `%s' already exists - "
240 "overwriting the old entry!", name);
242 destroy_callback (old_cf);
247 } /* }}} int register_callback */
249 static int create_register_callback (llist_t **list, /* {{{ */
250 const char *name, void *callback, user_data_t *ud)
254 cf = (callback_func_t *) malloc (sizeof (*cf));
257 ERROR ("plugin: create_register_callback: malloc failed.");
260 memset (cf, 0, sizeof (*cf));
262 cf->cf_callback = callback;
265 cf->cf_udata.data = NULL;
266 cf->cf_udata.free_func = NULL;
273 cf->cf_ctx = plugin_get_ctx ();
275 return (register_callback (list, name, cf));
276 } /* }}} int create_register_callback */
278 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
285 e = llist_search (list, name);
289 llist_remove (list, e);
292 destroy_callback (e->value);
297 } /* }}} int plugin_unregister */
300 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
301 * object, but it will bitch about a shared object not having a
302 * ``module_register'' symbol..
304 static int plugin_load_file (char *file, uint32_t flags)
307 void (*reg_handle) (void);
310 lt_dlerror (); /* clear errors */
312 #if LIBTOOL_VERSION == 2
313 if (flags & PLUGIN_FLAGS_GLOBAL) {
315 lt_dladvise_init(&advise);
316 lt_dladvise_global(&advise);
317 dlh = lt_dlopenadvise(file, advise);
318 lt_dladvise_destroy(&advise);
320 dlh = lt_dlopen (file);
322 #else /* if LIBTOOL_VERSION == 1 */
323 if (flags & PLUGIN_FLAGS_GLOBAL)
324 WARNING ("plugin_load_file: The global flag is not supported, "
325 "libtool 2 is required for this.");
326 dlh = lt_dlopen (file);
331 char errbuf[1024] = "";
333 ssnprintf (errbuf, sizeof (errbuf),
334 "lt_dlopen (\"%s\") failed: %s. "
335 "The most common cause for this problem are "
336 "missing dependencies. Use ldd(1) to check "
337 "the dependencies of the plugin "
339 file, lt_dlerror ());
341 ERROR ("%s", errbuf);
342 /* Make sure this is printed to STDERR in any case, but also
343 * make sure it's printed only once. */
344 if (list_log != NULL)
345 fprintf (stderr, "ERROR: %s\n", errbuf);
350 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
352 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
353 file, lt_dlerror ());
363 static _Bool timeout_reached(struct timespec timeout)
366 gettimeofday(&now, NULL);
367 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
370 static void *plugin_read_thread (void __attribute__((unused)) *args)
372 while (read_loop != 0)
375 plugin_ctx_t old_ctx;
381 /* Get the read function that needs to be read next.
382 * We don't need to hold "read_lock" for the heap, but we need
383 * to call c_heap_get_root() and pthread_cond_wait() in the
384 * same protected block. */
385 pthread_mutex_lock (&read_lock);
386 rf = c_heap_get_root (read_heap);
389 pthread_cond_wait (&read_cond, &read_lock);
390 pthread_mutex_unlock (&read_lock);
393 pthread_mutex_unlock (&read_lock);
395 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
397 /* this should not happen, because the interval is set
398 * for each plugin when loading it
399 * XXX: issue a warning? */
402 CDTIME_T_TO_TIMESPEC (plugin_get_interval (), &rf->rf_interval);
404 rf->rf_effective_interval = rf->rf_interval;
406 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
409 /* sleep until this entry is due,
410 * using pthread_cond_timedwait */
411 pthread_mutex_lock (&read_lock);
412 /* In pthread_cond_timedwait, spurious wakeups are possible
413 * (and really happen, at least on NetBSD with > 1 CPU), thus
414 * we need to re-evaluate the condition every time
415 * pthread_cond_timedwait returns. */
417 while ((read_loop != 0)
418 && !timeout_reached(rf->rf_next_read)
421 rc = pthread_cond_timedwait (&read_cond, &read_lock,
425 /* Must hold `read_lock' when accessing `rf->rf_type'. */
426 rf_type = rf->rf_type;
427 pthread_mutex_unlock (&read_lock);
429 /* Check if we're supposed to stop.. This may have interrupted
433 /* Insert `rf' again, so it can be free'd correctly */
434 c_heap_insert (read_heap, rf);
438 /* The entry has been marked for deletion. The linked list
439 * entry has already been removed by `plugin_unregister_read'.
440 * All we have to do here is free the `read_func_t' and
442 if (rf_type == RF_REMOVE)
444 DEBUG ("plugin_read_thread: Destroying the `%s' "
445 "callback.", rf->rf_name);
446 destroy_callback ((callback_func_t *) rf);
451 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
453 old_ctx = plugin_set_ctx (rf->rf_ctx);
455 if (rf_type == RF_SIMPLE)
457 int (*callback) (void);
459 callback = rf->rf_callback;
460 status = (*callback) ();
464 plugin_read_cb callback;
466 assert (rf_type == RF_COMPLEX);
468 callback = rf->rf_callback;
469 status = (*callback) (&rf->rf_udata);
472 plugin_set_ctx (old_ctx);
474 /* If the function signals failure, we will increase the
475 * intervals in which it will be called. */
478 rf->rf_effective_interval.tv_sec *= 2;
479 rf->rf_effective_interval.tv_nsec *= 2;
480 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
482 if (rf->rf_effective_interval.tv_sec >= 86400)
484 rf->rf_effective_interval.tv_sec = 86400;
485 rf->rf_effective_interval.tv_nsec = 0;
488 NOTICE ("read-function of plugin `%s' failed. "
489 "Will suspend it for %i seconds.",
491 (int) rf->rf_effective_interval.tv_sec);
495 /* Success: Restore the interval, if it was changed. */
496 rf->rf_effective_interval = rf->rf_interval;
499 /* update the ``next read due'' field */
502 DEBUG ("plugin_read_thread: Effective interval of the "
503 "%s plugin is %i.%09i.",
505 (int) rf->rf_effective_interval.tv_sec,
506 (int) rf->rf_effective_interval.tv_nsec);
508 /* Calculate the next (absolute) time at which this function
509 * should be called. */
510 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
511 + rf->rf_effective_interval.tv_sec;
512 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
513 + rf->rf_effective_interval.tv_nsec;
514 NORMALIZE_TIMESPEC (rf->rf_next_read);
516 /* Check, if `rf_next_read' is in the past. */
517 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
519 /* `rf_next_read' is in the past. Insert `now'
520 * so this value doesn't trail off into the
522 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
525 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
527 (int) rf->rf_next_read.tv_sec,
528 (int) rf->rf_next_read.tv_nsec);
530 /* Re-insert this read function into the heap again. */
531 c_heap_insert (read_heap, rf);
532 } /* while (read_loop) */
536 } /* void *plugin_read_thread */
538 static void start_read_threads (int num)
542 if (read_threads != NULL)
545 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
546 if (read_threads == NULL)
548 ERROR ("plugin: start_read_threads: calloc failed.");
552 read_threads_num = 0;
553 for (i = 0; i < num; i++)
555 if (pthread_create (read_threads + read_threads_num, NULL,
556 plugin_read_thread, NULL) == 0)
562 ERROR ("plugin: start_read_threads: pthread_create failed.");
566 } /* void start_read_threads */
568 static void stop_read_threads (void)
572 if (read_threads == NULL)
575 INFO ("collectd: Stopping %i read threads.", read_threads_num);
577 pthread_mutex_lock (&read_lock);
579 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
580 pthread_cond_broadcast (&read_cond);
581 pthread_mutex_unlock (&read_lock);
583 for (i = 0; i < read_threads_num; i++)
585 if (pthread_join (read_threads[i], NULL) != 0)
587 ERROR ("plugin: stop_read_threads: pthread_join failed.");
589 read_threads[i] = (pthread_t) 0;
591 sfree (read_threads);
592 read_threads_num = 0;
593 } /* void stop_read_threads */
595 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
600 meta_data_destroy (vl->meta);
603 } /* }}} void plugin_value_list_free */
605 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
612 vl = malloc (sizeof (*vl));
615 memcpy (vl, vl_orig, sizeof (*vl));
617 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
618 if (vl->values == NULL)
620 plugin_value_list_free (vl);
623 memcpy (vl->values, vl_orig->values,
624 vl_orig->values_len * sizeof (*vl->values));
626 vl->meta = meta_data_clone (vl->meta);
627 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
629 plugin_value_list_free (vl);
634 vl->time = cdtime ();
636 /* Fill in the interval from the thread context, if it is zero. */
637 if (vl->interval == 0)
639 plugin_ctx_t ctx = plugin_get_ctx ();
641 if (ctx.interval != 0)
642 vl->interval = ctx.interval;
645 char name[6 * DATA_MAX_NAME_LEN];
646 FORMAT_VL (name, sizeof (name), vl);
647 ERROR ("plugin_value_list_clone: Unable to determine "
648 "interval from context for "
649 "value list \"%s\". "
650 "This indicates a broken plugin. "
651 "Please report this problem to the "
652 "collectd mailing list or at "
653 "<http://collectd.org/bugs/>.", name);
654 vl->interval = cf_get_default_interval ();
659 } /* }}} value_list_t *plugin_value_list_clone */
661 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
665 q = malloc (sizeof (*q));
670 q->vl = plugin_value_list_clone (vl);
677 /* Store context of caller (read plugin); otherwise, it would not be
678 * available to the write plugins when actually dispatching the
679 * value-list later on. */
680 q->ctx = plugin_get_ctx ();
682 pthread_mutex_lock (&write_lock);
684 if (write_queue_tail == NULL)
686 write_queue_head = q;
687 write_queue_tail = q;
691 write_queue_tail->next = q;
692 write_queue_tail = q;
695 pthread_cond_signal (&write_cond);
696 pthread_mutex_unlock (&write_lock);
699 } /* }}} int plugin_write_enqueue */
701 static value_list_t *plugin_write_dequeue (void) /* {{{ */
706 pthread_mutex_lock (&write_lock);
708 while (write_loop && (write_queue_head == NULL))
709 pthread_cond_wait (&write_cond, &write_lock);
711 if (write_queue_head == NULL)
713 pthread_mutex_unlock (&write_lock);
717 q = write_queue_head;
718 write_queue_head = q->next;
719 if (write_queue_head == NULL)
720 write_queue_tail = NULL;
722 pthread_mutex_unlock (&write_lock);
724 (void) plugin_set_ctx (q->ctx);
729 } /* }}} value_list_t *plugin_write_dequeue */
731 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
735 value_list_t *vl = plugin_write_dequeue ();
739 plugin_dispatch_values_internal (vl);
741 plugin_value_list_free (vl);
746 } /* }}} void *plugin_write_thread */
748 static void start_write_threads (size_t num) /* {{{ */
752 if (write_threads != NULL)
755 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
756 if (write_threads == NULL)
758 ERROR ("plugin: start_write_threads: calloc failed.");
762 write_threads_num = 0;
763 for (i = 0; i < num; i++)
767 status = pthread_create (write_threads + write_threads_num,
774 ERROR ("plugin: start_write_threads: pthread_create failed "
775 "with status %i (%s).", status,
776 sstrerror (status, errbuf, sizeof (errbuf)));
782 } /* }}} void start_write_threads */
784 static void stop_write_threads (void) /* {{{ */
789 if (write_threads == NULL)
792 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
794 pthread_mutex_lock (&write_lock);
796 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
797 pthread_cond_broadcast (&write_cond);
798 pthread_mutex_unlock (&write_lock);
800 for (i = 0; i < write_threads_num; i++)
802 if (pthread_join (write_threads[i], NULL) != 0)
804 ERROR ("plugin: stop_write_threads: pthread_join failed.");
806 write_threads[i] = (pthread_t) 0;
808 sfree (write_threads);
809 write_threads_num = 0;
811 pthread_mutex_lock (&write_lock);
813 for (q = write_queue_head; q != NULL; )
815 write_queue_t *q1 = q;
816 plugin_value_list_free (q->vl);
821 write_queue_head = NULL;
822 write_queue_tail = NULL;
823 pthread_mutex_unlock (&write_lock);
827 WARNING ("plugin: %i value list%s left after shutting down "
828 "the write threads.",
829 i, (i == 1) ? " was" : "s were");
831 } /* }}} void stop_write_threads */
836 void plugin_set_dir (const char *dir)
838 if (plugindir != NULL)
843 else if ((plugindir = strdup (dir)) == NULL)
846 ERROR ("strdup failed: %s",
847 sstrerror (errno, errbuf, sizeof (errbuf)));
852 int plugin_load (const char *type, uint32_t flags)
856 char filename[BUFSIZE] = "";
857 char typename[BUFSIZE];
864 dir = plugin_get_dir ();
867 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
868 * type when matching the filename */
869 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
870 if ((status < 0) || ((size_t) status >= sizeof (typename)))
872 WARNING ("plugin_load: Filename too long: \"%s.so\"", type);
875 typename_len = strlen (typename);
877 if ((dh = opendir (dir)) == NULL)
880 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
881 sstrerror (errno, errbuf, sizeof (errbuf)));
885 while ((de = readdir (dh)) != NULL)
887 if (strncasecmp (de->d_name, typename, typename_len))
890 status = ssnprintf (filename, sizeof (filename),
891 "%s/%s", dir, de->d_name);
892 if ((status < 0) || ((size_t) status >= sizeof (filename)))
894 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
899 if (lstat (filename, &statbuf) == -1)
902 WARNING ("plugin_load: stat (\"%s\") failed: %s",
904 sstrerror (errno, errbuf, sizeof (errbuf)));
907 else if (!S_ISREG (statbuf.st_mode))
909 /* don't follow symlinks */
910 WARNING ("plugin_load: %s is not a regular file.",
915 status = plugin_load_file (filename, flags);
924 ERROR ("plugin_load: Load plugin \"%s\" failed with "
925 "status %i.", type, status);
931 if (filename[0] == 0)
932 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
939 * The `register_*' functions follow
941 int plugin_register_config (const char *name,
942 int (*callback) (const char *key, const char *val),
943 const char **keys, int keys_num)
945 cf_register (name, callback, keys, keys_num);
947 } /* int plugin_register_config */
949 int plugin_register_complex_config (const char *type,
950 int (*callback) (oconfig_item_t *))
952 return (cf_register_complex (type, callback));
953 } /* int plugin_register_complex_config */
955 int plugin_register_init (const char *name,
956 int (*callback) (void))
958 return (create_register_callback (&list_init, name, (void *) callback,
959 /* user_data = */ NULL));
960 } /* plugin_register_init */
962 static int plugin_compare_read_func (const void *arg0, const void *arg1)
964 const read_func_t *rf0;
965 const read_func_t *rf1;
970 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
972 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
974 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
976 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
980 } /* int plugin_compare_read_func */
982 /* Add a read function to both, the heap and a linked list. The linked list if
983 * used to look-up read functions, especially for the remove function. The heap
984 * is used to determine which plugin to read next. */
985 static int plugin_insert_read (read_func_t *rf)
990 pthread_mutex_lock (&read_lock);
992 if (read_list == NULL)
994 read_list = llist_create ();
995 if (read_list == NULL)
997 pthread_mutex_unlock (&read_lock);
998 ERROR ("plugin_insert_read: read_list failed.");
1003 if (read_heap == NULL)
1005 read_heap = c_heap_create (plugin_compare_read_func);
1006 if (read_heap == NULL)
1008 pthread_mutex_unlock (&read_lock);
1009 ERROR ("plugin_insert_read: c_heap_create failed.");
1014 le = llist_search (read_list, rf->rf_name);
1017 pthread_mutex_unlock (&read_lock);
1018 WARNING ("The read function \"%s\" is already registered. "
1019 "Check for duplicate \"LoadPlugin\" lines "
1020 "in your configuration!",
1025 le = llentry_create (rf->rf_name, rf);
1028 pthread_mutex_unlock (&read_lock);
1029 ERROR ("plugin_insert_read: llentry_create failed.");
1033 status = c_heap_insert (read_heap, rf);
1036 pthread_mutex_unlock (&read_lock);
1037 ERROR ("plugin_insert_read: c_heap_insert failed.");
1038 llentry_destroy (le);
1042 /* This does not fail. */
1043 llist_append (read_list, le);
1045 /* Wake up all the read threads. */
1046 pthread_cond_broadcast (&read_cond);
1047 pthread_mutex_unlock (&read_lock);
1049 } /* int plugin_insert_read */
1051 static int read_cb_wrapper (user_data_t *ud)
1053 int (*callback) (void);
1058 callback = ud->data;
1060 } /* int read_cb_wrapper */
1062 int plugin_register_read (const char *name,
1063 int (*callback) (void))
1066 plugin_ctx_t ctx = plugin_get_ctx ();
1069 if (ctx.interval != 0) {
1070 /* If ctx.interval is not zero (== use the plugin or global
1071 * interval), we need to use the "complex" read callback,
1072 * because only that allows to specify a different interval.
1073 * Wrap the callback using read_cb_wrapper(). */
1074 struct timespec interval;
1075 user_data_t user_data;
1077 user_data.data = callback;
1078 user_data.free_func = NULL;
1080 CDTIME_T_TO_TIMESPEC (ctx.interval, &interval);
1081 return plugin_register_complex_read (/* group = */ NULL,
1082 name, read_cb_wrapper, &interval, &user_data);
1085 DEBUG ("plugin_register_read: default_interval = %.3f",
1086 CDTIME_T_TO_DOUBLE(plugin_get_interval ()));
1088 rf = malloc (sizeof (*rf));
1091 ERROR ("plugin_register_read: malloc failed.");
1095 memset (rf, 0, sizeof (read_func_t));
1096 rf->rf_callback = (void *) callback;
1097 rf->rf_udata.data = NULL;
1098 rf->rf_udata.free_func = NULL;
1100 rf->rf_group[0] = '\0';
1101 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1102 rf->rf_type = RF_SIMPLE;
1103 rf->rf_interval.tv_sec = 0;
1104 rf->rf_interval.tv_nsec = 0;
1105 rf->rf_effective_interval = rf->rf_interval;
1107 status = plugin_insert_read (rf);
1112 } /* int plugin_register_read */
1114 int plugin_register_complex_read (const char *group, const char *name,
1115 plugin_read_cb callback,
1116 const struct timespec *interval,
1117 user_data_t *user_data)
1120 plugin_ctx_t ctx = plugin_get_ctx ();
1123 rf = malloc (sizeof (*rf));
1126 ERROR ("plugin_register_complex_read: malloc failed.");
1130 memset (rf, 0, sizeof (read_func_t));
1131 rf->rf_callback = (void *) callback;
1133 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1135 rf->rf_group[0] = '\0';
1136 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1137 rf->rf_type = RF_COMPLEX;
1138 if (interval != NULL)
1140 rf->rf_interval = *interval;
1142 else if (ctx.interval != 0)
1144 CDTIME_T_TO_TIMESPEC (ctx.interval, &rf->rf_interval);
1146 rf->rf_effective_interval = rf->rf_interval;
1148 DEBUG ("plugin_register_read: interval = %i.%09i",
1149 (int) rf->rf_interval.tv_sec,
1150 (int) rf->rf_interval.tv_nsec);
1153 if (user_data == NULL)
1155 rf->rf_udata.data = NULL;
1156 rf->rf_udata.free_func = NULL;
1160 rf->rf_udata = *user_data;
1165 status = plugin_insert_read (rf);
1170 } /* int plugin_register_complex_read */
1172 int plugin_register_write (const char *name,
1173 plugin_write_cb callback, user_data_t *ud)
1175 return (create_register_callback (&list_write, name,
1176 (void *) callback, ud));
1177 } /* int plugin_register_write */
1179 int plugin_register_flush (const char *name,
1180 plugin_flush_cb callback, user_data_t *ud)
1182 return (create_register_callback (&list_flush, name,
1183 (void *) callback, ud));
1184 } /* int plugin_register_flush */
1186 int plugin_register_missing (const char *name,
1187 plugin_missing_cb callback, user_data_t *ud)
1189 return (create_register_callback (&list_missing, name,
1190 (void *) callback, ud));
1191 } /* int plugin_register_missing */
1193 int plugin_register_shutdown (const char *name,
1194 int (*callback) (void))
1196 return (create_register_callback (&list_shutdown, name,
1197 (void *) callback, /* user_data = */ NULL));
1198 } /* int plugin_register_shutdown */
1200 int plugin_register_data_set (const data_set_t *ds)
1202 data_set_t *ds_copy;
1205 if ((data_sets != NULL)
1206 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1208 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1209 plugin_unregister_data_set (ds->type);
1211 else if (data_sets == NULL)
1213 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1214 if (data_sets == NULL)
1218 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1219 if (ds_copy == NULL)
1221 memcpy(ds_copy, ds, sizeof (data_set_t));
1223 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1225 if (ds_copy->ds == NULL)
1231 for (i = 0; i < ds->ds_num; i++)
1232 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1234 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1235 } /* int plugin_register_data_set */
1237 int plugin_register_log (const char *name,
1238 plugin_log_cb callback, user_data_t *ud)
1240 return (create_register_callback (&list_log, name,
1241 (void *) callback, ud));
1242 } /* int plugin_register_log */
1244 int plugin_register_notification (const char *name,
1245 plugin_notification_cb callback, user_data_t *ud)
1247 return (create_register_callback (&list_notification, name,
1248 (void *) callback, ud));
1249 } /* int plugin_register_log */
1251 int plugin_unregister_config (const char *name)
1253 cf_unregister (name);
1255 } /* int plugin_unregister_config */
1257 int plugin_unregister_complex_config (const char *name)
1259 cf_unregister_complex (name);
1261 } /* int plugin_unregister_complex_config */
1263 int plugin_unregister_init (const char *name)
1265 return (plugin_unregister (list_init, name));
1268 int plugin_unregister_read (const char *name) /* {{{ */
1276 pthread_mutex_lock (&read_lock);
1278 if (read_list == NULL)
1280 pthread_mutex_unlock (&read_lock);
1284 le = llist_search (read_list, name);
1287 pthread_mutex_unlock (&read_lock);
1288 WARNING ("plugin_unregister_read: No such read function: %s",
1293 llist_remove (read_list, le);
1296 assert (rf != NULL);
1297 rf->rf_type = RF_REMOVE;
1299 pthread_mutex_unlock (&read_lock);
1301 llentry_destroy (le);
1303 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1306 } /* }}} int plugin_unregister_read */
1308 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1310 read_func_t *rf = e->value;
1313 return strcmp (rf->rf_group, (const char *)group);
1314 } /* }}} int compare_read_func_group */
1316 int plugin_unregister_read_group (const char *group) /* {{{ */
1326 pthread_mutex_lock (&read_lock);
1328 if (read_list == NULL)
1330 pthread_mutex_unlock (&read_lock);
1336 le = llist_search_custom (read_list,
1337 compare_read_func_group, (void *)group);
1344 llist_remove (read_list, le);
1347 assert (rf != NULL);
1348 rf->rf_type = RF_REMOVE;
1350 llentry_destroy (le);
1352 DEBUG ("plugin_unregister_read_group: "
1353 "Marked `%s' (group `%s') for removal.",
1354 rf->rf_name, group);
1357 pthread_mutex_unlock (&read_lock);
1361 WARNING ("plugin_unregister_read_group: No such "
1362 "group of read function: %s", group);
1367 } /* }}} int plugin_unregister_read_group */
1369 int plugin_unregister_write (const char *name)
1371 return (plugin_unregister (list_write, name));
1374 int plugin_unregister_flush (const char *name)
1376 return (plugin_unregister (list_flush, name));
1379 int plugin_unregister_missing (const char *name)
1381 return (plugin_unregister (list_missing, name));
1384 int plugin_unregister_shutdown (const char *name)
1386 return (plugin_unregister (list_shutdown, name));
1389 int plugin_unregister_data_set (const char *name)
1393 if (data_sets == NULL)
1396 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1403 } /* int plugin_unregister_data_set */
1405 int plugin_unregister_log (const char *name)
1407 return (plugin_unregister (list_log, name));
1410 int plugin_unregister_notification (const char *name)
1412 return (plugin_unregister (list_notification, name));
1415 void plugin_init_all (void)
1417 const char *chain_name;
1421 /* Init the value cache */
1424 chain_name = global_option_get ("PreCacheChain");
1425 pre_cache_chain = fc_chain_get_by_name (chain_name);
1427 chain_name = global_option_get ("PostCacheChain");
1428 post_cache_chain = fc_chain_get_by_name (chain_name);
1431 char const *tmp = global_option_get ("WriteThreads");
1432 int num = atoi (tmp);
1437 start_write_threads ((size_t) num);
1440 if ((list_init == NULL) && (read_heap == NULL))
1443 /* Calling all init callbacks before checking if read callbacks
1444 * are available allows the init callbacks to register the read
1446 le = llist_head (list_init);
1449 callback_func_t *cf;
1450 plugin_init_cb callback;
1451 plugin_ctx_t old_ctx;
1454 old_ctx = plugin_set_ctx (cf->cf_ctx);
1455 callback = cf->cf_callback;
1456 status = (*callback) ();
1457 plugin_set_ctx (old_ctx);
1461 ERROR ("Initialization of plugin `%s' "
1462 "failed with status %i. "
1463 "Plugin will be unloaded.",
1465 /* Plugins that register read callbacks from the init
1466 * callback should take care of appropriate error
1467 * handling themselves. */
1468 /* FIXME: Unload _all_ functions */
1469 plugin_unregister_read (le->key);
1475 /* Start read-threads */
1476 if (read_heap != NULL)
1480 rt = global_option_get ("ReadThreads");
1483 start_read_threads ((num > 0) ? num : 5);
1485 } /* void plugin_init_all */
1487 /* TODO: Rename this function. */
1488 void plugin_read_all (void)
1490 uc_check_timeout ();
1493 } /* void plugin_read_all */
1495 /* Read function called when the `-T' command line argument is given. */
1496 int plugin_read_all_once (void)
1499 int return_status = 0;
1501 if (read_heap == NULL)
1503 NOTICE ("No read-functions are registered.");
1510 plugin_ctx_t old_ctx;
1512 rf = c_heap_get_root (read_heap);
1516 old_ctx = plugin_set_ctx (rf->rf_ctx);
1518 if (rf->rf_type == RF_SIMPLE)
1520 int (*callback) (void);
1522 callback = rf->rf_callback;
1523 status = (*callback) ();
1527 plugin_read_cb callback;
1529 callback = rf->rf_callback;
1530 status = (*callback) (&rf->rf_udata);
1533 plugin_set_ctx (old_ctx);
1537 NOTICE ("read-function of plugin `%s' failed.",
1542 destroy_callback ((void *) rf);
1545 return (return_status);
1546 } /* int plugin_read_all_once */
1548 int plugin_write (const char *plugin, /* {{{ */
1549 const data_set_t *ds, const value_list_t *vl)
1557 if (list_write == NULL)
1562 ds = plugin_get_ds (vl->type);
1565 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1575 le = llist_head (list_write);
1578 callback_func_t *cf = le->value;
1579 plugin_write_cb callback;
1581 /* do not switch plugin context; rather keep the context (interval)
1582 * information of the calling read plugin */
1584 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1585 callback = cf->cf_callback;
1586 status = (*callback) (ds, vl, &cf->cf_udata);
1595 if ((success == 0) && (failure != 0))
1600 else /* plugin != NULL */
1602 callback_func_t *cf;
1603 plugin_write_cb callback;
1605 le = llist_head (list_write);
1608 if (strcasecmp (plugin, le->key) == 0)
1619 /* do not switch plugin context; rather keep the context (interval)
1620 * information of the calling read plugin */
1622 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1623 callback = cf->cf_callback;
1624 status = (*callback) (ds, vl, &cf->cf_udata);
1628 } /* }}} int plugin_write */
1630 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1634 if (list_flush == NULL)
1637 le = llist_head (list_flush);
1640 callback_func_t *cf;
1641 plugin_flush_cb callback;
1642 plugin_ctx_t old_ctx;
1644 if ((plugin != NULL)
1645 && (strcmp (plugin, le->key) != 0))
1652 old_ctx = plugin_set_ctx (cf->cf_ctx);
1653 callback = cf->cf_callback;
1655 (*callback) (timeout, identifier, &cf->cf_udata);
1657 plugin_set_ctx (old_ctx);
1662 } /* int plugin_flush */
1664 void plugin_shutdown_all (void)
1668 stop_read_threads ();
1670 destroy_all_callbacks (&list_init);
1672 pthread_mutex_lock (&read_lock);
1673 llist_destroy (read_list);
1675 pthread_mutex_unlock (&read_lock);
1677 destroy_read_heap ();
1679 plugin_flush (/* plugin = */ NULL,
1681 /* identifier = */ NULL);
1684 if (list_shutdown != NULL)
1685 le = llist_head (list_shutdown);
1689 callback_func_t *cf;
1690 plugin_shutdown_cb callback;
1691 plugin_ctx_t old_ctx;
1694 old_ctx = plugin_set_ctx (cf->cf_ctx);
1695 callback = cf->cf_callback;
1697 /* Advance the pointer before calling the callback allows
1698 * shutdown functions to unregister themselves. If done the
1699 * other way around the memory `le' points to will be freed
1700 * after callback returns. */
1705 plugin_set_ctx (old_ctx);
1708 stop_write_threads ();
1710 /* Write plugins which use the `user_data' pointer usually need the
1711 * same data available to the flush callback. If this is the case, set
1712 * the free_function to NULL when registering the flush callback and to
1713 * the real free function when registering the write callback. This way
1714 * the data isn't freed twice. */
1715 destroy_all_callbacks (&list_flush);
1716 destroy_all_callbacks (&list_missing);
1717 destroy_all_callbacks (&list_write);
1719 destroy_all_callbacks (&list_notification);
1720 destroy_all_callbacks (&list_shutdown);
1721 destroy_all_callbacks (&list_log);
1722 } /* void plugin_shutdown_all */
1724 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1728 if (list_missing == NULL)
1731 le = llist_head (list_missing);
1734 callback_func_t *cf;
1735 plugin_missing_cb callback;
1736 plugin_ctx_t old_ctx;
1740 old_ctx = plugin_set_ctx (cf->cf_ctx);
1741 callback = cf->cf_callback;
1743 status = (*callback) (vl, &cf->cf_udata);
1744 plugin_set_ctx (old_ctx);
1749 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1750 "failed with status %i.",
1763 } /* int }}} plugin_dispatch_missing */
1765 static int plugin_dispatch_values_internal (value_list_t *vl)
1768 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1770 value_t *saved_values;
1771 int saved_values_len;
1775 int free_meta_data = 0;
1777 if ((vl == NULL) || (vl->type[0] == 0)
1778 || (vl->values == NULL) || (vl->values_len < 1))
1780 ERROR ("plugin_dispatch_values: Invalid value list "
1781 "from plugin %s.", vl->plugin);
1785 /* Free meta data only if the calling function didn't specify any. In
1786 * this case matches and targets may add some and the calling function
1787 * may not expect (and therefore free) that data. */
1788 if (vl->meta == NULL)
1791 if (list_write == NULL)
1792 c_complain_once (LOG_WARNING, &no_write_complaint,
1793 "plugin_dispatch_values: No write callback has been "
1794 "registered. Please load at least one output plugin, "
1795 "if you want the collected data to be stored.");
1797 if (data_sets == NULL)
1799 ERROR ("plugin_dispatch_values: No data sets registered. "
1800 "Could the types database be read? Check "
1801 "your `TypesDB' setting!");
1805 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1807 char ident[6 * DATA_MAX_NAME_LEN];
1809 FORMAT_VL (ident, sizeof (ident), vl);
1810 INFO ("plugin_dispatch_values: Dataset not found: %s "
1811 "(from \"%s\"), check your types.db!",
1816 /* Assured by plugin_value_list_clone(). The time is determined at
1817 * _enqueue_ time. */
1818 assert (vl->time != 0);
1819 assert (vl->interval != 0);
1821 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1823 "plugin = %s; plugin_instance = %s; "
1824 "type = %s; type_instance = %s;",
1825 CDTIME_T_TO_DOUBLE (vl->time),
1826 CDTIME_T_TO_DOUBLE (vl->interval),
1828 vl->plugin, vl->plugin_instance,
1829 vl->type, vl->type_instance);
1832 assert (0 == strcmp (ds->type, vl->type));
1834 if (0 != strcmp (ds->type, vl->type))
1835 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1836 ds->type, vl->type);
1840 assert (ds->ds_num == vl->values_len);
1842 if (ds->ds_num != vl->values_len)
1844 ERROR ("plugin_dispatch_values: ds->type = %s: "
1845 "(ds->ds_num = %i) != "
1846 "(vl->values_len = %i)",
1847 ds->type, ds->ds_num, vl->values_len);
1852 escape_slashes (vl->host, sizeof (vl->host));
1853 escape_slashes (vl->plugin, sizeof (vl->plugin));
1854 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1855 escape_slashes (vl->type, sizeof (vl->type));
1856 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1858 /* Copy the values. This way, we can assure `targets' that they get
1859 * dynamically allocated values, which they can free and replace if
1861 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1863 saved_values = vl->values;
1864 saved_values_len = vl->values_len;
1866 vl->values = (value_t *) calloc (vl->values_len,
1867 sizeof (*vl->values));
1868 if (vl->values == NULL)
1870 ERROR ("plugin_dispatch_values: calloc failed.");
1871 vl->values = saved_values;
1874 memcpy (vl->values, saved_values,
1875 vl->values_len * sizeof (*vl->values));
1877 else /* if ((pre == NULL) && (post == NULL)) */
1879 saved_values = NULL;
1880 saved_values_len = 0;
1883 if (pre_cache_chain != NULL)
1885 status = fc_process_chain (ds, vl, pre_cache_chain);
1888 WARNING ("plugin_dispatch_values: Running the "
1889 "pre-cache chain failed with "
1893 else if (status == FC_TARGET_STOP)
1895 /* Restore the state of the value_list so that plugins
1896 * don't get confused.. */
1897 if (saved_values != NULL)
1900 vl->values = saved_values;
1901 vl->values_len = saved_values_len;
1907 /* Update the value cache */
1910 if (post_cache_chain != NULL)
1912 status = fc_process_chain (ds, vl, post_cache_chain);
1915 WARNING ("plugin_dispatch_values: Running the "
1916 "post-cache chain failed with "
1922 fc_default_action (ds, vl);
1924 /* Restore the state of the value_list so that plugins don't get
1926 if (saved_values != NULL)
1929 vl->values = saved_values;
1930 vl->values_len = saved_values_len;
1933 if ((free_meta_data != 0) && (vl->meta != NULL))
1935 meta_data_destroy (vl->meta);
1940 } /* int plugin_dispatch_values_internal */
1942 int plugin_dispatch_values (value_list_t const *vl)
1946 status = plugin_write_enqueue (vl);
1950 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
1951 "with status %i (%s).", status,
1952 sstrerror (status, errbuf, sizeof (errbuf)));
1959 int plugin_dispatch_notification (const notification_t *notif)
1962 /* Possible TODO: Add flap detection here */
1964 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1965 "time = %.3f; host = %s;",
1966 notif->severity, notif->message,
1967 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1969 /* Nobody cares for notifications */
1970 if (list_notification == NULL)
1973 le = llist_head (list_notification);
1976 callback_func_t *cf;
1977 plugin_notification_cb callback;
1980 /* do not switch plugin context; rather keep the context
1981 * (interval) information of the calling plugin */
1984 callback = cf->cf_callback;
1985 status = (*callback) (notif, &cf->cf_udata);
1988 WARNING ("plugin_dispatch_notification: Notification "
1989 "callback %s returned %i.",
1997 } /* int plugin_dispatch_notification */
1999 void plugin_log (int level, const char *format, ...)
2006 if (level >= LOG_DEBUG)
2010 va_start (ap, format);
2011 vsnprintf (msg, sizeof (msg), format, ap);
2012 msg[sizeof (msg) - 1] = '\0';
2015 if (list_log == NULL)
2017 fprintf (stderr, "%s\n", msg);
2021 le = llist_head (list_log);
2024 callback_func_t *cf;
2025 plugin_log_cb callback;
2028 callback = cf->cf_callback;
2030 /* do not switch plugin context; rather keep the context
2031 * (interval) information of the calling plugin */
2033 (*callback) (level, msg, &cf->cf_udata);
2037 } /* void plugin_log */
2039 int parse_log_severity (const char *severity)
2043 if ((0 == strcasecmp (severity, "emerg"))
2044 || (0 == strcasecmp (severity, "alert"))
2045 || (0 == strcasecmp (severity, "crit"))
2046 || (0 == strcasecmp (severity, "err")))
2047 log_level = LOG_ERR;
2048 else if (0 == strcasecmp (severity, "warning"))
2049 log_level = LOG_WARNING;
2050 else if (0 == strcasecmp (severity, "notice"))
2051 log_level = LOG_NOTICE;
2052 else if (0 == strcasecmp (severity, "info"))
2053 log_level = LOG_INFO;
2055 else if (0 == strcasecmp (severity, "debug"))
2056 log_level = LOG_DEBUG;
2057 #endif /* COLLECT_DEBUG */
2060 } /* int parse_log_severity */
2062 int parse_notif_severity (const char *severity)
2064 int notif_severity = -1;
2066 if (strcasecmp (severity, "FAILURE") == 0)
2067 notif_severity = NOTIF_FAILURE;
2068 else if (strcmp (severity, "OKAY") == 0)
2069 notif_severity = NOTIF_OKAY;
2070 else if ((strcmp (severity, "WARNING") == 0)
2071 || (strcmp (severity, "WARN") == 0))
2072 notif_severity = NOTIF_WARNING;
2074 return (notif_severity);
2075 } /* int parse_notif_severity */
2077 const data_set_t *plugin_get_ds (const char *name)
2081 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2083 DEBUG ("No such dataset registered: %s", name);
2088 } /* data_set_t *plugin_get_ds */
2090 static int plugin_notification_meta_add (notification_t *n,
2092 enum notification_meta_type_e type,
2095 notification_meta_t *meta;
2096 notification_meta_t *tail;
2098 if ((n == NULL) || (name == NULL) || (value == NULL))
2100 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2104 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2107 ERROR ("plugin_notification_meta_add: malloc failed.");
2110 memset (meta, 0, sizeof (notification_meta_t));
2112 sstrncpy (meta->name, name, sizeof (meta->name));
2117 case NM_TYPE_STRING:
2119 meta->nm_value.nm_string = strdup ((const char *) value);
2120 if (meta->nm_value.nm_string == NULL)
2122 ERROR ("plugin_notification_meta_add: strdup failed.");
2128 case NM_TYPE_SIGNED_INT:
2130 meta->nm_value.nm_signed_int = *((int64_t *) value);
2133 case NM_TYPE_UNSIGNED_INT:
2135 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2138 case NM_TYPE_DOUBLE:
2140 meta->nm_value.nm_double = *((double *) value);
2143 case NM_TYPE_BOOLEAN:
2145 meta->nm_value.nm_boolean = *((_Bool *) value);
2150 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2154 } /* switch (type) */
2158 while ((tail != NULL) && (tail->next != NULL))
2167 } /* int plugin_notification_meta_add */
2169 int plugin_notification_meta_add_string (notification_t *n,
2173 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2176 int plugin_notification_meta_add_signed_int (notification_t *n,
2180 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2183 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2187 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2190 int plugin_notification_meta_add_double (notification_t *n,
2194 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2197 int plugin_notification_meta_add_boolean (notification_t *n,
2201 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2204 int plugin_notification_meta_copy (notification_t *dst,
2205 const notification_t *src)
2207 notification_meta_t *meta;
2209 assert (dst != NULL);
2210 assert (src != NULL);
2211 assert (dst != src);
2212 assert ((src->meta == NULL) || (src->meta != dst->meta));
2214 for (meta = src->meta; meta != NULL; meta = meta->next)
2216 if (meta->type == NM_TYPE_STRING)
2217 plugin_notification_meta_add_string (dst, meta->name,
2218 meta->nm_value.nm_string);
2219 else if (meta->type == NM_TYPE_SIGNED_INT)
2220 plugin_notification_meta_add_signed_int (dst, meta->name,
2221 meta->nm_value.nm_signed_int);
2222 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2223 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2224 meta->nm_value.nm_unsigned_int);
2225 else if (meta->type == NM_TYPE_DOUBLE)
2226 plugin_notification_meta_add_double (dst, meta->name,
2227 meta->nm_value.nm_double);
2228 else if (meta->type == NM_TYPE_BOOLEAN)
2229 plugin_notification_meta_add_boolean (dst, meta->name,
2230 meta->nm_value.nm_boolean);
2234 } /* int plugin_notification_meta_copy */
2236 int plugin_notification_meta_free (notification_meta_t *n)
2238 notification_meta_t *this;
2239 notification_meta_t *next;
2243 ERROR ("plugin_notification_meta_free: n == NULL!");
2248 while (this != NULL)
2252 if (this->type == NM_TYPE_STRING)
2254 free ((char *)this->nm_value.nm_string);
2255 this->nm_value.nm_string = NULL;
2263 } /* int plugin_notification_meta_free */
2265 static void plugin_ctx_destructor (void *ctx)
2268 } /* void plugin_ctx_destructor */
2270 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2272 static plugin_ctx_t *plugin_ctx_create (void)
2276 ctx = malloc (sizeof (*ctx));
2279 ERROR ("Failed to allocate plugin context: %s",
2280 sstrerror (errno, errbuf, sizeof (errbuf)));
2285 assert (plugin_ctx_key_initialized);
2286 pthread_setspecific (plugin_ctx_key, ctx);
2287 DEBUG("Created new plugin context.");
2289 } /* int plugin_ctx_create */
2291 void plugin_init_ctx (void)
2293 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2294 plugin_ctx_key_initialized = 1;
2295 } /* void plugin_init_ctx */
2297 plugin_ctx_t plugin_get_ctx (void)
2301 assert (plugin_ctx_key_initialized);
2302 ctx = pthread_getspecific (plugin_ctx_key);
2305 ctx = plugin_ctx_create ();
2306 /* this must no happen -- exit() instead? */
2312 } /* plugin_ctx_t plugin_get_ctx */
2314 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2319 assert (plugin_ctx_key_initialized);
2320 c = pthread_getspecific (plugin_ctx_key);
2323 c = plugin_ctx_create ();
2324 /* this must no happen -- exit() instead? */
2333 } /* void plugin_set_ctx */
2335 cdtime_t plugin_get_interval (void)
2339 interval = plugin_get_ctx().interval;
2343 return cf_get_default_interval ();
2344 } /* cdtime_t plugin_get_interval */
2348 void *(*start_routine) (void *);
2352 static void *plugin_thread_start (void *arg)
2354 plugin_thread_t *plugin_thread = arg;
2356 void *(*start_routine) (void *) = plugin_thread->start_routine;
2357 void *plugin_arg = plugin_thread->arg;
2359 plugin_set_ctx (plugin_thread->ctx);
2361 free (plugin_thread);
2363 return start_routine (plugin_arg);
2364 } /* void *plugin_thread_start */
2366 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2367 void *(*start_routine) (void *), void *arg)
2369 plugin_thread_t *plugin_thread;
2371 plugin_thread = malloc (sizeof (*plugin_thread));
2372 if (plugin_thread == NULL)
2375 plugin_thread->ctx = plugin_get_ctx ();
2376 plugin_thread->start_routine = start_routine;
2377 plugin_thread->arg = arg;
2379 return pthread_create (thread, attr,
2380 plugin_thread_start, plugin_thread);
2381 } /* int plugin_thread_create */
2383 /* vim: set sw=8 ts=8 noet fdm=marker : */