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; q = q->next)
815 plugin_value_list_free (q->vl);
819 write_queue_head = NULL;
820 write_queue_tail = NULL;
821 pthread_mutex_unlock (&write_lock);
825 WARNING ("plugin: %i value list%s left after shutting down "
826 "the write threads.",
827 i, (i == 1) ? " was" : "s were");
829 } /* }}} void stop_write_threads */
834 void plugin_set_dir (const char *dir)
836 if (plugindir != NULL)
841 else if ((plugindir = strdup (dir)) == NULL)
844 ERROR ("strdup failed: %s",
845 sstrerror (errno, errbuf, sizeof (errbuf)));
850 int plugin_load (const char *type, uint32_t flags)
854 char filename[BUFSIZE] = "";
855 char typename[BUFSIZE];
862 dir = plugin_get_dir ();
865 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
866 * type when matching the filename */
867 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
868 if ((status < 0) || ((size_t) status >= sizeof (typename)))
870 WARNING ("plugin_load: Filename too long: \"%s.so\"", type);
873 typename_len = strlen (typename);
875 if ((dh = opendir (dir)) == NULL)
878 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
879 sstrerror (errno, errbuf, sizeof (errbuf)));
883 while ((de = readdir (dh)) != NULL)
885 if (strncasecmp (de->d_name, typename, typename_len))
888 status = ssnprintf (filename, sizeof (filename),
889 "%s/%s", dir, de->d_name);
890 if ((status < 0) || ((size_t) status >= sizeof (filename)))
892 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
897 if (lstat (filename, &statbuf) == -1)
900 WARNING ("plugin_load: stat (\"%s\") failed: %s",
902 sstrerror (errno, errbuf, sizeof (errbuf)));
905 else if (!S_ISREG (statbuf.st_mode))
907 /* don't follow symlinks */
908 WARNING ("plugin_load: %s is not a regular file.",
913 status = plugin_load_file (filename, flags);
922 ERROR ("plugin_load: Load plugin \"%s\" failed with "
923 "status %i.", type, status);
929 if (filename[0] == 0)
930 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
937 * The `register_*' functions follow
939 int plugin_register_config (const char *name,
940 int (*callback) (const char *key, const char *val),
941 const char **keys, int keys_num)
943 cf_register (name, callback, keys, keys_num);
945 } /* int plugin_register_config */
947 int plugin_register_complex_config (const char *type,
948 int (*callback) (oconfig_item_t *))
950 return (cf_register_complex (type, callback));
951 } /* int plugin_register_complex_config */
953 int plugin_register_init (const char *name,
954 int (*callback) (void))
956 return (create_register_callback (&list_init, name, (void *) callback,
957 /* user_data = */ NULL));
958 } /* plugin_register_init */
960 static int plugin_compare_read_func (const void *arg0, const void *arg1)
962 const read_func_t *rf0;
963 const read_func_t *rf1;
968 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
970 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
972 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
974 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
978 } /* int plugin_compare_read_func */
980 /* Add a read function to both, the heap and a linked list. The linked list if
981 * used to look-up read functions, especially for the remove function. The heap
982 * is used to determine which plugin to read next. */
983 static int plugin_insert_read (read_func_t *rf)
988 pthread_mutex_lock (&read_lock);
990 if (read_list == NULL)
992 read_list = llist_create ();
993 if (read_list == NULL)
995 pthread_mutex_unlock (&read_lock);
996 ERROR ("plugin_insert_read: read_list failed.");
1001 if (read_heap == NULL)
1003 read_heap = c_heap_create (plugin_compare_read_func);
1004 if (read_heap == NULL)
1006 pthread_mutex_unlock (&read_lock);
1007 ERROR ("plugin_insert_read: c_heap_create failed.");
1012 le = llist_search (read_list, rf->rf_name);
1015 pthread_mutex_unlock (&read_lock);
1016 WARNING ("The read function \"%s\" is already registered. "
1017 "Check for duplicate \"LoadPlugin\" lines "
1018 "in your configuration!",
1023 le = llentry_create (rf->rf_name, rf);
1026 pthread_mutex_unlock (&read_lock);
1027 ERROR ("plugin_insert_read: llentry_create failed.");
1031 status = c_heap_insert (read_heap, rf);
1034 pthread_mutex_unlock (&read_lock);
1035 ERROR ("plugin_insert_read: c_heap_insert failed.");
1036 llentry_destroy (le);
1040 /* This does not fail. */
1041 llist_append (read_list, le);
1043 /* Wake up all the read threads. */
1044 pthread_cond_broadcast (&read_cond);
1045 pthread_mutex_unlock (&read_lock);
1047 } /* int plugin_insert_read */
1049 static int read_cb_wrapper (user_data_t *ud)
1051 int (*callback) (void);
1056 callback = ud->data;
1058 } /* int read_cb_wrapper */
1060 int plugin_register_read (const char *name,
1061 int (*callback) (void))
1064 plugin_ctx_t ctx = plugin_get_ctx ();
1067 if (ctx.interval != 0) {
1068 /* If ctx.interval is not zero (== use the plugin or global
1069 * interval), we need to use the "complex" read callback,
1070 * because only that allows to specify a different interval.
1071 * Wrap the callback using read_cb_wrapper(). */
1072 struct timespec interval;
1073 user_data_t user_data;
1075 user_data.data = callback;
1076 user_data.free_func = NULL;
1078 CDTIME_T_TO_TIMESPEC (ctx.interval, &interval);
1079 return plugin_register_complex_read (/* group = */ NULL,
1080 name, read_cb_wrapper, &interval, &user_data);
1083 DEBUG ("plugin_register_read: default_interval = %.3f",
1084 CDTIME_T_TO_DOUBLE(plugin_get_interval ()));
1086 rf = malloc (sizeof (*rf));
1089 ERROR ("plugin_register_read: malloc failed.");
1093 memset (rf, 0, sizeof (read_func_t));
1094 rf->rf_callback = (void *) callback;
1095 rf->rf_udata.data = NULL;
1096 rf->rf_udata.free_func = NULL;
1098 rf->rf_group[0] = '\0';
1099 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1100 rf->rf_type = RF_SIMPLE;
1101 rf->rf_interval.tv_sec = 0;
1102 rf->rf_interval.tv_nsec = 0;
1103 rf->rf_effective_interval = rf->rf_interval;
1105 status = plugin_insert_read (rf);
1110 } /* int plugin_register_read */
1112 int plugin_register_complex_read (const char *group, const char *name,
1113 plugin_read_cb callback,
1114 const struct timespec *interval,
1115 user_data_t *user_data)
1118 plugin_ctx_t ctx = plugin_get_ctx ();
1121 rf = malloc (sizeof (*rf));
1124 ERROR ("plugin_register_complex_read: malloc failed.");
1128 memset (rf, 0, sizeof (read_func_t));
1129 rf->rf_callback = (void *) callback;
1131 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1133 rf->rf_group[0] = '\0';
1134 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1135 rf->rf_type = RF_COMPLEX;
1136 if (interval != NULL)
1138 rf->rf_interval = *interval;
1140 else if (ctx.interval != 0)
1142 CDTIME_T_TO_TIMESPEC (ctx.interval, &rf->rf_interval);
1144 rf->rf_effective_interval = rf->rf_interval;
1146 DEBUG ("plugin_register_read: interval = %i.%09i",
1147 (int) rf->rf_interval.tv_sec,
1148 (int) rf->rf_interval.tv_nsec);
1151 if (user_data == NULL)
1153 rf->rf_udata.data = NULL;
1154 rf->rf_udata.free_func = NULL;
1158 rf->rf_udata = *user_data;
1163 status = plugin_insert_read (rf);
1168 } /* int plugin_register_complex_read */
1170 int plugin_register_write (const char *name,
1171 plugin_write_cb callback, user_data_t *ud)
1173 return (create_register_callback (&list_write, name,
1174 (void *) callback, ud));
1175 } /* int plugin_register_write */
1177 int plugin_register_flush (const char *name,
1178 plugin_flush_cb callback, user_data_t *ud)
1180 return (create_register_callback (&list_flush, name,
1181 (void *) callback, ud));
1182 } /* int plugin_register_flush */
1184 int plugin_register_missing (const char *name,
1185 plugin_missing_cb callback, user_data_t *ud)
1187 return (create_register_callback (&list_missing, name,
1188 (void *) callback, ud));
1189 } /* int plugin_register_missing */
1191 int plugin_register_shutdown (const char *name,
1192 int (*callback) (void))
1194 return (create_register_callback (&list_shutdown, name,
1195 (void *) callback, /* user_data = */ NULL));
1196 } /* int plugin_register_shutdown */
1198 int plugin_register_data_set (const data_set_t *ds)
1200 data_set_t *ds_copy;
1203 if ((data_sets != NULL)
1204 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1206 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1207 plugin_unregister_data_set (ds->type);
1209 else if (data_sets == NULL)
1211 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1212 if (data_sets == NULL)
1216 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1217 if (ds_copy == NULL)
1219 memcpy(ds_copy, ds, sizeof (data_set_t));
1221 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1223 if (ds_copy->ds == NULL)
1229 for (i = 0; i < ds->ds_num; i++)
1230 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1232 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1233 } /* int plugin_register_data_set */
1235 int plugin_register_log (const char *name,
1236 plugin_log_cb callback, user_data_t *ud)
1238 return (create_register_callback (&list_log, name,
1239 (void *) callback, ud));
1240 } /* int plugin_register_log */
1242 int plugin_register_notification (const char *name,
1243 plugin_notification_cb callback, user_data_t *ud)
1245 return (create_register_callback (&list_notification, name,
1246 (void *) callback, ud));
1247 } /* int plugin_register_log */
1249 int plugin_unregister_config (const char *name)
1251 cf_unregister (name);
1253 } /* int plugin_unregister_config */
1255 int plugin_unregister_complex_config (const char *name)
1257 cf_unregister_complex (name);
1259 } /* int plugin_unregister_complex_config */
1261 int plugin_unregister_init (const char *name)
1263 return (plugin_unregister (list_init, name));
1266 int plugin_unregister_read (const char *name) /* {{{ */
1274 pthread_mutex_lock (&read_lock);
1276 if (read_list == NULL)
1278 pthread_mutex_unlock (&read_lock);
1282 le = llist_search (read_list, name);
1285 pthread_mutex_unlock (&read_lock);
1286 WARNING ("plugin_unregister_read: No such read function: %s",
1291 llist_remove (read_list, le);
1294 assert (rf != NULL);
1295 rf->rf_type = RF_REMOVE;
1297 pthread_mutex_unlock (&read_lock);
1299 llentry_destroy (le);
1301 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1304 } /* }}} int plugin_unregister_read */
1306 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1308 read_func_t *rf = e->value;
1311 return strcmp (rf->rf_group, (const char *)group);
1312 } /* }}} int compare_read_func_group */
1314 int plugin_unregister_read_group (const char *group) /* {{{ */
1324 pthread_mutex_lock (&read_lock);
1326 if (read_list == NULL)
1328 pthread_mutex_unlock (&read_lock);
1334 le = llist_search_custom (read_list,
1335 compare_read_func_group, (void *)group);
1342 llist_remove (read_list, le);
1345 assert (rf != NULL);
1346 rf->rf_type = RF_REMOVE;
1348 llentry_destroy (le);
1350 DEBUG ("plugin_unregister_read_group: "
1351 "Marked `%s' (group `%s') for removal.",
1352 rf->rf_name, group);
1355 pthread_mutex_unlock (&read_lock);
1359 WARNING ("plugin_unregister_read_group: No such "
1360 "group of read function: %s", group);
1365 } /* }}} int plugin_unregister_read_group */
1367 int plugin_unregister_write (const char *name)
1369 return (plugin_unregister (list_write, name));
1372 int plugin_unregister_flush (const char *name)
1374 return (plugin_unregister (list_flush, name));
1377 int plugin_unregister_missing (const char *name)
1379 return (plugin_unregister (list_missing, name));
1382 int plugin_unregister_shutdown (const char *name)
1384 return (plugin_unregister (list_shutdown, name));
1387 int plugin_unregister_data_set (const char *name)
1391 if (data_sets == NULL)
1394 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1401 } /* int plugin_unregister_data_set */
1403 int plugin_unregister_log (const char *name)
1405 return (plugin_unregister (list_log, name));
1408 int plugin_unregister_notification (const char *name)
1410 return (plugin_unregister (list_notification, name));
1413 void plugin_init_all (void)
1415 const char *chain_name;
1419 /* Init the value cache */
1422 chain_name = global_option_get ("PreCacheChain");
1423 pre_cache_chain = fc_chain_get_by_name (chain_name);
1425 chain_name = global_option_get ("PostCacheChain");
1426 post_cache_chain = fc_chain_get_by_name (chain_name);
1429 char const *tmp = global_option_get ("WriteThreads");
1430 int num = atoi (tmp);
1435 start_write_threads ((size_t) num);
1438 if ((list_init == NULL) && (read_heap == NULL))
1441 /* Calling all init callbacks before checking if read callbacks
1442 * are available allows the init callbacks to register the read
1444 le = llist_head (list_init);
1447 callback_func_t *cf;
1448 plugin_init_cb callback;
1449 plugin_ctx_t old_ctx;
1452 old_ctx = plugin_set_ctx (cf->cf_ctx);
1453 callback = cf->cf_callback;
1454 status = (*callback) ();
1455 plugin_set_ctx (old_ctx);
1459 ERROR ("Initialization of plugin `%s' "
1460 "failed with status %i. "
1461 "Plugin will be unloaded.",
1463 /* Plugins that register read callbacks from the init
1464 * callback should take care of appropriate error
1465 * handling themselves. */
1466 /* FIXME: Unload _all_ functions */
1467 plugin_unregister_read (le->key);
1473 /* Start read-threads */
1474 if (read_heap != NULL)
1478 rt = global_option_get ("ReadThreads");
1481 start_read_threads ((num > 0) ? num : 5);
1483 } /* void plugin_init_all */
1485 /* TODO: Rename this function. */
1486 void plugin_read_all (void)
1488 uc_check_timeout ();
1491 } /* void plugin_read_all */
1493 /* Read function called when the `-T' command line argument is given. */
1494 int plugin_read_all_once (void)
1497 int return_status = 0;
1499 if (read_heap == NULL)
1501 NOTICE ("No read-functions are registered.");
1508 plugin_ctx_t old_ctx;
1510 rf = c_heap_get_root (read_heap);
1514 old_ctx = plugin_set_ctx (rf->rf_ctx);
1516 if (rf->rf_type == RF_SIMPLE)
1518 int (*callback) (void);
1520 callback = rf->rf_callback;
1521 status = (*callback) ();
1525 plugin_read_cb callback;
1527 callback = rf->rf_callback;
1528 status = (*callback) (&rf->rf_udata);
1531 plugin_set_ctx (old_ctx);
1535 NOTICE ("read-function of plugin `%s' failed.",
1540 destroy_callback ((void *) rf);
1543 return (return_status);
1544 } /* int plugin_read_all_once */
1546 int plugin_write (const char *plugin, /* {{{ */
1547 const data_set_t *ds, const value_list_t *vl)
1555 if (list_write == NULL)
1560 ds = plugin_get_ds (vl->type);
1563 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1573 le = llist_head (list_write);
1576 callback_func_t *cf = le->value;
1577 plugin_write_cb callback;
1579 /* do not switch plugin context; rather keep the context (interval)
1580 * information of the calling read plugin */
1582 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1583 callback = cf->cf_callback;
1584 status = (*callback) (ds, vl, &cf->cf_udata);
1593 if ((success == 0) && (failure != 0))
1598 else /* plugin != NULL */
1600 callback_func_t *cf;
1601 plugin_write_cb callback;
1603 le = llist_head (list_write);
1606 if (strcasecmp (plugin, le->key) == 0)
1617 /* do not switch plugin context; rather keep the context (interval)
1618 * information of the calling read plugin */
1620 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1621 callback = cf->cf_callback;
1622 status = (*callback) (ds, vl, &cf->cf_udata);
1626 } /* }}} int plugin_write */
1628 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1632 if (list_flush == NULL)
1635 le = llist_head (list_flush);
1638 callback_func_t *cf;
1639 plugin_flush_cb callback;
1640 plugin_ctx_t old_ctx;
1642 if ((plugin != NULL)
1643 && (strcmp (plugin, le->key) != 0))
1650 old_ctx = plugin_set_ctx (cf->cf_ctx);
1651 callback = cf->cf_callback;
1653 (*callback) (timeout, identifier, &cf->cf_udata);
1655 plugin_set_ctx (old_ctx);
1660 } /* int plugin_flush */
1662 void plugin_shutdown_all (void)
1666 stop_read_threads ();
1668 destroy_all_callbacks (&list_init);
1670 pthread_mutex_lock (&read_lock);
1671 llist_destroy (read_list);
1673 pthread_mutex_unlock (&read_lock);
1675 destroy_read_heap ();
1677 plugin_flush (/* plugin = */ NULL,
1679 /* identifier = */ NULL);
1682 if (list_shutdown != NULL)
1683 le = llist_head (list_shutdown);
1687 callback_func_t *cf;
1688 plugin_shutdown_cb callback;
1689 plugin_ctx_t old_ctx;
1692 old_ctx = plugin_set_ctx (cf->cf_ctx);
1693 callback = cf->cf_callback;
1695 /* Advance the pointer before calling the callback allows
1696 * shutdown functions to unregister themselves. If done the
1697 * other way around the memory `le' points to will be freed
1698 * after callback returns. */
1703 plugin_set_ctx (old_ctx);
1706 stop_write_threads ();
1708 /* Write plugins which use the `user_data' pointer usually need the
1709 * same data available to the flush callback. If this is the case, set
1710 * the free_function to NULL when registering the flush callback and to
1711 * the real free function when registering the write callback. This way
1712 * the data isn't freed twice. */
1713 destroy_all_callbacks (&list_flush);
1714 destroy_all_callbacks (&list_missing);
1715 destroy_all_callbacks (&list_write);
1717 destroy_all_callbacks (&list_notification);
1718 destroy_all_callbacks (&list_shutdown);
1719 destroy_all_callbacks (&list_log);
1720 } /* void plugin_shutdown_all */
1722 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1726 if (list_missing == NULL)
1729 le = llist_head (list_missing);
1732 callback_func_t *cf;
1733 plugin_missing_cb callback;
1734 plugin_ctx_t old_ctx;
1738 old_ctx = plugin_set_ctx (cf->cf_ctx);
1739 callback = cf->cf_callback;
1741 status = (*callback) (vl, &cf->cf_udata);
1742 plugin_set_ctx (old_ctx);
1747 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1748 "failed with status %i.",
1761 } /* int }}} plugin_dispatch_missing */
1763 static int plugin_dispatch_values_internal (value_list_t *vl)
1766 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1768 value_t *saved_values;
1769 int saved_values_len;
1773 int free_meta_data = 0;
1775 if ((vl == NULL) || (vl->type[0] == 0)
1776 || (vl->values == NULL) || (vl->values_len < 1))
1778 ERROR ("plugin_dispatch_values: Invalid value list "
1779 "from plugin %s.", vl->plugin);
1783 /* Free meta data only if the calling function didn't specify any. In
1784 * this case matches and targets may add some and the calling function
1785 * may not expect (and therefore free) that data. */
1786 if (vl->meta == NULL)
1789 if (list_write == NULL)
1790 c_complain_once (LOG_WARNING, &no_write_complaint,
1791 "plugin_dispatch_values: No write callback has been "
1792 "registered. Please load at least one output plugin, "
1793 "if you want the collected data to be stored.");
1795 if (data_sets == NULL)
1797 ERROR ("plugin_dispatch_values: No data sets registered. "
1798 "Could the types database be read? Check "
1799 "your `TypesDB' setting!");
1803 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1805 char ident[6 * DATA_MAX_NAME_LEN];
1807 FORMAT_VL (ident, sizeof (ident), vl);
1808 INFO ("plugin_dispatch_values: Dataset not found: %s "
1809 "(from \"%s\"), check your types.db!",
1814 /* Assured by plugin_value_list_clone(). The time is determined at
1815 * _enqueue_ time. */
1816 assert (vl->time != 0);
1817 assert (vl->interval != 0);
1819 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1821 "plugin = %s; plugin_instance = %s; "
1822 "type = %s; type_instance = %s;",
1823 CDTIME_T_TO_DOUBLE (vl->time),
1824 CDTIME_T_TO_DOUBLE (vl->interval),
1826 vl->plugin, vl->plugin_instance,
1827 vl->type, vl->type_instance);
1830 assert (0 == strcmp (ds->type, vl->type));
1832 if (0 != strcmp (ds->type, vl->type))
1833 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1834 ds->type, vl->type);
1838 assert (ds->ds_num == vl->values_len);
1840 if (ds->ds_num != vl->values_len)
1842 ERROR ("plugin_dispatch_values: ds->type = %s: "
1843 "(ds->ds_num = %i) != "
1844 "(vl->values_len = %i)",
1845 ds->type, ds->ds_num, vl->values_len);
1850 escape_slashes (vl->host, sizeof (vl->host));
1851 escape_slashes (vl->plugin, sizeof (vl->plugin));
1852 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1853 escape_slashes (vl->type, sizeof (vl->type));
1854 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1856 /* Copy the values. This way, we can assure `targets' that they get
1857 * dynamically allocated values, which they can free and replace if
1859 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1861 saved_values = vl->values;
1862 saved_values_len = vl->values_len;
1864 vl->values = (value_t *) calloc (vl->values_len,
1865 sizeof (*vl->values));
1866 if (vl->values == NULL)
1868 ERROR ("plugin_dispatch_values: calloc failed.");
1869 vl->values = saved_values;
1872 memcpy (vl->values, saved_values,
1873 vl->values_len * sizeof (*vl->values));
1875 else /* if ((pre == NULL) && (post == NULL)) */
1877 saved_values = NULL;
1878 saved_values_len = 0;
1881 if (pre_cache_chain != NULL)
1883 status = fc_process_chain (ds, vl, pre_cache_chain);
1886 WARNING ("plugin_dispatch_values: Running the "
1887 "pre-cache chain failed with "
1891 else if (status == FC_TARGET_STOP)
1893 /* Restore the state of the value_list so that plugins
1894 * don't get confused.. */
1895 if (saved_values != NULL)
1898 vl->values = saved_values;
1899 vl->values_len = saved_values_len;
1905 /* Update the value cache */
1908 if (post_cache_chain != NULL)
1910 status = fc_process_chain (ds, vl, post_cache_chain);
1913 WARNING ("plugin_dispatch_values: Running the "
1914 "post-cache chain failed with "
1920 fc_default_action (ds, vl);
1922 /* Restore the state of the value_list so that plugins don't get
1924 if (saved_values != NULL)
1927 vl->values = saved_values;
1928 vl->values_len = saved_values_len;
1931 if ((free_meta_data != 0) && (vl->meta != NULL))
1933 meta_data_destroy (vl->meta);
1938 } /* int plugin_dispatch_values_internal */
1940 int plugin_dispatch_values (value_list_t const *vl)
1944 status = plugin_write_enqueue (vl);
1948 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
1949 "with status %i (%s).", status,
1950 sstrerror (status, errbuf, sizeof (errbuf)));
1957 int plugin_dispatch_notification (const notification_t *notif)
1960 /* Possible TODO: Add flap detection here */
1962 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1963 "time = %.3f; host = %s;",
1964 notif->severity, notif->message,
1965 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1967 /* Nobody cares for notifications */
1968 if (list_notification == NULL)
1971 le = llist_head (list_notification);
1974 callback_func_t *cf;
1975 plugin_notification_cb callback;
1978 /* do not switch plugin context; rather keep the context
1979 * (interval) information of the calling plugin */
1982 callback = cf->cf_callback;
1983 status = (*callback) (notif, &cf->cf_udata);
1986 WARNING ("plugin_dispatch_notification: Notification "
1987 "callback %s returned %i.",
1995 } /* int plugin_dispatch_notification */
1997 void plugin_log (int level, const char *format, ...)
2004 if (level >= LOG_DEBUG)
2008 va_start (ap, format);
2009 vsnprintf (msg, sizeof (msg), format, ap);
2010 msg[sizeof (msg) - 1] = '\0';
2013 if (list_log == NULL)
2015 fprintf (stderr, "%s\n", msg);
2019 le = llist_head (list_log);
2022 callback_func_t *cf;
2023 plugin_log_cb callback;
2026 callback = cf->cf_callback;
2028 /* do not switch plugin context; rather keep the context
2029 * (interval) information of the calling plugin */
2031 (*callback) (level, msg, &cf->cf_udata);
2035 } /* void plugin_log */
2037 int parse_log_severity (const char *severity)
2041 if ((0 == strcasecmp (severity, "emerg"))
2042 || (0 == strcasecmp (severity, "alert"))
2043 || (0 == strcasecmp (severity, "crit"))
2044 || (0 == strcasecmp (severity, "err")))
2045 log_level = LOG_ERR;
2046 else if (0 == strcasecmp (severity, "warning"))
2047 log_level = LOG_WARNING;
2048 else if (0 == strcasecmp (severity, "notice"))
2049 log_level = LOG_NOTICE;
2050 else if (0 == strcasecmp (severity, "info"))
2051 log_level = LOG_INFO;
2053 else if (0 == strcasecmp (severity, "debug"))
2054 log_level = LOG_DEBUG;
2055 #endif /* COLLECT_DEBUG */
2058 } /* int parse_log_severity */
2060 int parse_notif_severity (const char *severity)
2062 int notif_severity = -1;
2064 if (strcasecmp (severity, "FAILURE") == 0)
2065 notif_severity = NOTIF_FAILURE;
2066 else if (strcmp (severity, "OKAY") == 0)
2067 notif_severity = NOTIF_OKAY;
2068 else if ((strcmp (severity, "WARNING") == 0)
2069 || (strcmp (severity, "WARN") == 0))
2070 notif_severity = NOTIF_WARNING;
2072 return (notif_severity);
2073 } /* int parse_notif_severity */
2075 const data_set_t *plugin_get_ds (const char *name)
2079 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2081 DEBUG ("No such dataset registered: %s", name);
2086 } /* data_set_t *plugin_get_ds */
2088 static int plugin_notification_meta_add (notification_t *n,
2090 enum notification_meta_type_e type,
2093 notification_meta_t *meta;
2094 notification_meta_t *tail;
2096 if ((n == NULL) || (name == NULL) || (value == NULL))
2098 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2102 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2105 ERROR ("plugin_notification_meta_add: malloc failed.");
2108 memset (meta, 0, sizeof (notification_meta_t));
2110 sstrncpy (meta->name, name, sizeof (meta->name));
2115 case NM_TYPE_STRING:
2117 meta->nm_value.nm_string = strdup ((const char *) value);
2118 if (meta->nm_value.nm_string == NULL)
2120 ERROR ("plugin_notification_meta_add: strdup failed.");
2126 case NM_TYPE_SIGNED_INT:
2128 meta->nm_value.nm_signed_int = *((int64_t *) value);
2131 case NM_TYPE_UNSIGNED_INT:
2133 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2136 case NM_TYPE_DOUBLE:
2138 meta->nm_value.nm_double = *((double *) value);
2141 case NM_TYPE_BOOLEAN:
2143 meta->nm_value.nm_boolean = *((_Bool *) value);
2148 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2152 } /* switch (type) */
2156 while ((tail != NULL) && (tail->next != NULL))
2165 } /* int plugin_notification_meta_add */
2167 int plugin_notification_meta_add_string (notification_t *n,
2171 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2174 int plugin_notification_meta_add_signed_int (notification_t *n,
2178 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2181 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2185 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2188 int plugin_notification_meta_add_double (notification_t *n,
2192 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2195 int plugin_notification_meta_add_boolean (notification_t *n,
2199 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2202 int plugin_notification_meta_copy (notification_t *dst,
2203 const notification_t *src)
2205 notification_meta_t *meta;
2207 assert (dst != NULL);
2208 assert (src != NULL);
2209 assert (dst != src);
2210 assert ((src->meta == NULL) || (src->meta != dst->meta));
2212 for (meta = src->meta; meta != NULL; meta = meta->next)
2214 if (meta->type == NM_TYPE_STRING)
2215 plugin_notification_meta_add_string (dst, meta->name,
2216 meta->nm_value.nm_string);
2217 else if (meta->type == NM_TYPE_SIGNED_INT)
2218 plugin_notification_meta_add_signed_int (dst, meta->name,
2219 meta->nm_value.nm_signed_int);
2220 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2221 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2222 meta->nm_value.nm_unsigned_int);
2223 else if (meta->type == NM_TYPE_DOUBLE)
2224 plugin_notification_meta_add_double (dst, meta->name,
2225 meta->nm_value.nm_double);
2226 else if (meta->type == NM_TYPE_BOOLEAN)
2227 plugin_notification_meta_add_boolean (dst, meta->name,
2228 meta->nm_value.nm_boolean);
2232 } /* int plugin_notification_meta_copy */
2234 int plugin_notification_meta_free (notification_meta_t *n)
2236 notification_meta_t *this;
2237 notification_meta_t *next;
2241 ERROR ("plugin_notification_meta_free: n == NULL!");
2246 while (this != NULL)
2250 if (this->type == NM_TYPE_STRING)
2252 free ((char *)this->nm_value.nm_string);
2253 this->nm_value.nm_string = NULL;
2261 } /* int plugin_notification_meta_free */
2263 static void plugin_ctx_destructor (void *ctx)
2266 } /* void plugin_ctx_destructor */
2268 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2270 static plugin_ctx_t *plugin_ctx_create (void)
2274 ctx = malloc (sizeof (*ctx));
2277 ERROR ("Failed to allocate plugin context: %s",
2278 sstrerror (errno, errbuf, sizeof (errbuf)));
2283 assert (plugin_ctx_key_initialized);
2284 pthread_setspecific (plugin_ctx_key, ctx);
2285 DEBUG("Created new plugin context.");
2287 } /* int plugin_ctx_create */
2289 void plugin_init_ctx (void)
2291 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2292 plugin_ctx_key_initialized = 1;
2293 } /* void plugin_init_ctx */
2295 plugin_ctx_t plugin_get_ctx (void)
2299 assert (plugin_ctx_key_initialized);
2300 ctx = pthread_getspecific (plugin_ctx_key);
2303 ctx = plugin_ctx_create ();
2304 /* this must no happen -- exit() instead? */
2310 } /* plugin_ctx_t plugin_get_ctx */
2312 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2317 assert (plugin_ctx_key_initialized);
2318 c = pthread_getspecific (plugin_ctx_key);
2321 c = plugin_ctx_create ();
2322 /* this must no happen -- exit() instead? */
2331 } /* void plugin_set_ctx */
2333 cdtime_t plugin_get_interval (void)
2337 interval = plugin_get_ctx().interval;
2341 return cf_get_default_interval ();
2342 } /* cdtime_t plugin_get_interval */
2346 void *(*start_routine) (void *);
2350 static void *plugin_thread_start (void *arg)
2352 plugin_thread_t *plugin_thread = arg;
2354 void *(*start_routine) (void *) = plugin_thread->start_routine;
2355 void *plugin_arg = plugin_thread->arg;
2357 plugin_set_ctx (plugin_thread->ctx);
2359 free (plugin_thread);
2361 return start_routine (plugin_arg);
2362 } /* void *plugin_thread_start */
2364 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2365 void *(*start_routine) (void *), void *arg)
2367 plugin_thread_t *plugin_thread;
2369 plugin_thread = malloc (sizeof (*plugin_thread));
2370 if (plugin_thread == NULL)
2373 plugin_thread->ctx = plugin_get_ctx ();
2374 plugin_thread->start_routine = start_routine;
2375 plugin_thread->arg = arg;
2377 return pthread_create (thread, attr,
2378 plugin_thread_start, plugin_thread);
2379 } /* int plugin_thread_create */
2381 /* vim: set sw=8 ts=8 noet fdm=marker : */