2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2013 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
20 * Sebastian Harl <sh at tokkee.org>
26 #include "configfile.h"
27 #include "filter_chain.h"
28 #include "utils_avltree.h"
29 #include "utils_cache.h"
30 #include "utils_complain.h"
31 #include "utils_llist.h"
32 #include "utils_heap.h"
33 #include "utils_time.h"
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];
67 cdtime_t rf_effective_interval;
68 cdtime_t rf_next_read;
70 typedef struct read_func_s read_func_t;
73 typedef struct write_queue_s write_queue_t;
84 static c_avl_tree_t *plugins_loaded = NULL;
86 static llist_t *list_init;
87 static llist_t *list_write;
88 static llist_t *list_flush;
89 static llist_t *list_missing;
90 static llist_t *list_shutdown;
91 static llist_t *list_log;
92 static llist_t *list_notification;
94 static fc_chain_t *pre_cache_chain = NULL;
95 static fc_chain_t *post_cache_chain = NULL;
97 static c_avl_tree_t *data_sets;
99 static char *plugindir = NULL;
101 static c_heap_t *read_heap = NULL;
102 static llist_t *read_list;
103 static int read_loop = 1;
104 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
105 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
106 static pthread_t *read_threads = NULL;
107 static int read_threads_num = 0;
109 static write_queue_t *write_queue_head;
110 static write_queue_t *write_queue_tail;
111 static long write_queue_size = 0;
112 static _Bool write_loop = 1;
113 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
114 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
115 static pthread_t *write_threads = NULL;
116 static size_t write_threads_num = 0;
118 static pthread_key_t plugin_ctx_key;
119 static _Bool plugin_ctx_key_initialized = 0;
121 static long writequeuelengthlimit_high = 0;
122 static long writequeuelengthlimit_low = 0;
124 static unsigned int random_seed;
129 static int plugin_dispatch_values_internal (value_list_t *vl);
131 static const char *plugin_get_dir (void)
133 if (plugindir == NULL)
139 static void destroy_callback (callback_func_t *cf) /* {{{ */
144 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
146 cf->cf_udata.free_func (cf->cf_udata.data);
147 cf->cf_udata.data = NULL;
148 cf->cf_udata.free_func = NULL;
151 } /* }}} void destroy_callback */
153 static void destroy_all_callbacks (llist_t **list) /* {{{ */
160 le = llist_head (*list);
168 destroy_callback (le->value);
174 llist_destroy (*list);
176 } /* }}} void destroy_all_callbacks */
178 static void destroy_read_heap (void) /* {{{ */
180 if (read_heap == NULL)
187 cf = c_heap_get_root (read_heap);
191 destroy_callback (cf);
194 c_heap_destroy (read_heap);
196 } /* }}} void destroy_read_heap */
198 static int register_callback (llist_t **list, /* {{{ */
199 const char *name, callback_func_t *cf)
206 *list = llist_create ();
209 ERROR ("plugin: register_callback: "
210 "llist_create failed.");
211 destroy_callback (cf);
219 ERROR ("plugin: register_callback: strdup failed.");
220 destroy_callback (cf);
224 le = llist_search (*list, name);
227 le = llentry_create (key, cf);
230 ERROR ("plugin: register_callback: "
231 "llentry_create failed.");
233 destroy_callback (cf);
237 llist_append (*list, le);
241 callback_func_t *old_cf;
246 WARNING ("plugin: register_callback: "
247 "a callback named `%s' already exists - "
248 "overwriting the old entry!", name);
250 destroy_callback (old_cf);
255 } /* }}} int register_callback */
257 static int create_register_callback (llist_t **list, /* {{{ */
258 const char *name, void *callback, user_data_t *ud)
262 cf = (callback_func_t *) malloc (sizeof (*cf));
265 ERROR ("plugin: create_register_callback: malloc failed.");
268 memset (cf, 0, sizeof (*cf));
270 cf->cf_callback = callback;
273 cf->cf_udata.data = NULL;
274 cf->cf_udata.free_func = NULL;
281 cf->cf_ctx = plugin_get_ctx ();
283 return (register_callback (list, name, cf));
284 } /* }}} int create_register_callback */
286 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
293 e = llist_search (list, name);
297 llist_remove (list, e);
300 destroy_callback (e->value);
305 } /* }}} int plugin_unregister */
308 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
309 * object, but it will bitch about a shared object not having a
310 * ``module_register'' symbol..
312 static int plugin_load_file (char *file, uint32_t flags)
315 void (*reg_handle) (void);
318 lt_dlerror (); /* clear errors */
320 #if LIBTOOL_VERSION == 2
321 if (flags & PLUGIN_FLAGS_GLOBAL) {
323 lt_dladvise_init(&advise);
324 lt_dladvise_global(&advise);
325 dlh = lt_dlopenadvise(file, advise);
326 lt_dladvise_destroy(&advise);
328 dlh = lt_dlopen (file);
330 #else /* if LIBTOOL_VERSION == 1 */
331 if (flags & PLUGIN_FLAGS_GLOBAL)
332 WARNING ("plugin_load_file: The global flag is not supported, "
333 "libtool 2 is required for this.");
334 dlh = lt_dlopen (file);
339 char errbuf[1024] = "";
341 ssnprintf (errbuf, sizeof (errbuf),
342 "lt_dlopen (\"%s\") failed: %s. "
343 "The most common cause for this problem are "
344 "missing dependencies. Use ldd(1) to check "
345 "the dependencies of the plugin "
347 file, lt_dlerror ());
349 ERROR ("%s", errbuf);
350 /* Make sure this is printed to STDERR in any case, but also
351 * make sure it's printed only once. */
352 if (list_log != NULL)
353 fprintf (stderr, "ERROR: %s\n", errbuf);
358 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
360 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
361 file, lt_dlerror ());
371 static void *plugin_read_thread (void __attribute__((unused)) *args)
373 while (read_loop != 0)
376 plugin_ctx_t old_ctx;
382 /* Get the read function that needs to be read next.
383 * We don't need to hold "read_lock" for the heap, but we need
384 * to call c_heap_get_root() and pthread_cond_wait() in the
385 * same protected block. */
386 pthread_mutex_lock (&read_lock);
387 rf = c_heap_get_root (read_heap);
390 pthread_cond_wait (&read_cond, &read_lock);
391 pthread_mutex_unlock (&read_lock);
394 pthread_mutex_unlock (&read_lock);
396 if (rf->rf_interval == 0)
398 /* this should not happen, because the interval is set
399 * for each plugin when loading it
400 * XXX: issue a warning? */
401 rf->rf_interval = plugin_get_interval ();
402 rf->rf_effective_interval = rf->rf_interval;
404 rf->rf_next_read = cdtime ();
407 /* sleep until this entry is due,
408 * using pthread_cond_timedwait */
409 pthread_mutex_lock (&read_lock);
410 /* In pthread_cond_timedwait, spurious wakeups are possible
411 * (and really happen, at least on NetBSD with > 1 CPU), thus
412 * we need to re-evaluate the condition every time
413 * pthread_cond_timedwait returns. */
415 while ((read_loop != 0)
416 && (cdtime () < rf->rf_next_read)
419 struct timespec ts = { 0 };
421 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
423 rc = pthread_cond_timedwait (&read_cond, &read_lock,
427 /* Must hold `read_lock' when accessing `rf->rf_type'. */
428 rf_type = rf->rf_type;
429 pthread_mutex_unlock (&read_lock);
431 /* Check if we're supposed to stop.. This may have interrupted
435 /* Insert `rf' again, so it can be free'd correctly */
436 c_heap_insert (read_heap, rf);
440 /* The entry has been marked for deletion. The linked list
441 * entry has already been removed by `plugin_unregister_read'.
442 * All we have to do here is free the `read_func_t' and
444 if (rf_type == RF_REMOVE)
446 DEBUG ("plugin_read_thread: Destroying the `%s' "
447 "callback.", rf->rf_name);
449 destroy_callback ((callback_func_t *) rf);
454 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
456 old_ctx = plugin_set_ctx (rf->rf_ctx);
458 if (rf_type == RF_SIMPLE)
460 int (*callback) (void);
462 callback = rf->rf_callback;
463 status = (*callback) ();
467 plugin_read_cb callback;
469 assert (rf_type == RF_COMPLEX);
471 callback = rf->rf_callback;
472 status = (*callback) (&rf->rf_udata);
475 plugin_set_ctx (old_ctx);
477 /* If the function signals failure, we will increase the
478 * intervals in which it will be called. */
481 rf->rf_effective_interval *= 2;
482 if (rf->rf_effective_interval > TIME_T_TO_CDTIME_T (86400))
483 rf->rf_effective_interval = TIME_T_TO_CDTIME_T (86400);
485 NOTICE ("read-function of plugin `%s' failed. "
486 "Will suspend it for %.3f seconds.",
488 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
492 /* Success: Restore the interval, if it was changed. */
493 rf->rf_effective_interval = rf->rf_interval;
496 /* update the ``next read due'' field */
499 DEBUG ("plugin_read_thread: Effective interval of the "
500 "%s plugin is %.3f seconds.",
502 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
504 /* Calculate the next (absolute) time at which this function
505 * should be called. */
506 rf->rf_next_read += rf->rf_effective_interval;
508 /* Check, if `rf_next_read' is in the past. */
509 if (rf->rf_next_read < now)
511 /* `rf_next_read' is in the past. Insert `now'
512 * so this value doesn't trail off into the
514 rf->rf_next_read = now;
517 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
519 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
521 /* Re-insert this read function into the heap again. */
522 c_heap_insert (read_heap, rf);
523 } /* while (read_loop) */
527 } /* void *plugin_read_thread */
529 static void start_read_threads (int num)
533 if (read_threads != NULL)
536 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
537 if (read_threads == NULL)
539 ERROR ("plugin: start_read_threads: calloc failed.");
543 read_threads_num = 0;
544 for (i = 0; i < num; i++)
546 if (pthread_create (read_threads + read_threads_num, NULL,
547 plugin_read_thread, NULL) == 0)
553 ERROR ("plugin: start_read_threads: pthread_create failed.");
557 } /* void start_read_threads */
559 static void stop_read_threads (void)
563 if (read_threads == NULL)
566 INFO ("collectd: Stopping %i read threads.", read_threads_num);
568 pthread_mutex_lock (&read_lock);
570 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
571 pthread_cond_broadcast (&read_cond);
572 pthread_mutex_unlock (&read_lock);
574 for (i = 0; i < read_threads_num; i++)
576 if (pthread_join (read_threads[i], NULL) != 0)
578 ERROR ("plugin: stop_read_threads: pthread_join failed.");
580 read_threads[i] = (pthread_t) 0;
582 sfree (read_threads);
583 read_threads_num = 0;
584 } /* void stop_read_threads */
586 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
591 meta_data_destroy (vl->meta);
594 } /* }}} void plugin_value_list_free */
596 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
603 vl = malloc (sizeof (*vl));
606 memcpy (vl, vl_orig, sizeof (*vl));
608 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
609 if (vl->values == NULL)
611 plugin_value_list_free (vl);
614 memcpy (vl->values, vl_orig->values,
615 vl_orig->values_len * sizeof (*vl->values));
617 vl->meta = meta_data_clone (vl->meta);
618 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
620 plugin_value_list_free (vl);
625 vl->time = cdtime ();
627 /* Fill in the interval from the thread context, if it is zero. */
628 if (vl->interval == 0)
630 plugin_ctx_t ctx = plugin_get_ctx ();
632 if (ctx.interval != 0)
633 vl->interval = ctx.interval;
636 char name[6 * DATA_MAX_NAME_LEN];
637 FORMAT_VL (name, sizeof (name), vl);
638 ERROR ("plugin_value_list_clone: Unable to determine "
639 "interval from context for "
640 "value list \"%s\". "
641 "This indicates a broken plugin. "
642 "Please report this problem to the "
643 "collectd mailing list or at "
644 "<http://collectd.org/bugs/>.", name);
645 vl->interval = cf_get_default_interval ();
650 } /* }}} value_list_t *plugin_value_list_clone */
652 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
656 q = malloc (sizeof (*q));
661 q->vl = plugin_value_list_clone (vl);
668 /* Store context of caller (read plugin); otherwise, it would not be
669 * available to the write plugins when actually dispatching the
670 * value-list later on. */
671 q->ctx = plugin_get_ctx ();
673 pthread_mutex_lock (&write_lock);
675 if (write_queue_tail == NULL)
677 write_queue_head = q;
678 write_queue_tail = q;
679 write_queue_size = 1;
683 write_queue_tail->next = q;
684 write_queue_tail = q;
685 write_queue_size += 1;
688 pthread_cond_signal (&write_cond);
689 pthread_mutex_unlock (&write_lock);
692 } /* }}} int plugin_write_enqueue */
694 static value_list_t *plugin_write_dequeue (void) /* {{{ */
699 pthread_mutex_lock (&write_lock);
701 while (write_loop && (write_queue_head == NULL))
702 pthread_cond_wait (&write_cond, &write_lock);
704 if (write_queue_head == NULL)
706 pthread_mutex_unlock (&write_lock);
710 q = write_queue_head;
711 write_queue_head = q->next;
712 write_queue_size -= 1;
713 if (write_queue_head == NULL) {
714 write_queue_tail = NULL;
715 write_queue_size = 0; /* Maybe instead of setting write_queue_size to 0, we should assert(write_queue_size == 0) ? */
718 pthread_mutex_unlock (&write_lock);
720 (void) plugin_set_ctx (q->ctx);
725 } /* }}} value_list_t *plugin_write_dequeue */
727 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
731 value_list_t *vl = plugin_write_dequeue ();
735 plugin_dispatch_values_internal (vl);
737 plugin_value_list_free (vl);
742 } /* }}} void *plugin_write_thread */
744 static void start_write_threads (size_t num) /* {{{ */
748 if (write_threads != NULL)
751 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
752 if (write_threads == NULL)
754 ERROR ("plugin: start_write_threads: calloc failed.");
758 write_threads_num = 0;
759 for (i = 0; i < num; i++)
763 status = pthread_create (write_threads + write_threads_num,
770 ERROR ("plugin: start_write_threads: pthread_create failed "
771 "with status %i (%s).", status,
772 sstrerror (status, errbuf, sizeof (errbuf)));
778 } /* }}} void start_write_threads */
780 static void stop_write_threads (void) /* {{{ */
785 if (write_threads == NULL)
788 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
790 pthread_mutex_lock (&write_lock);
792 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
793 pthread_cond_broadcast (&write_cond);
794 pthread_mutex_unlock (&write_lock);
796 for (i = 0; i < write_threads_num; i++)
798 if (pthread_join (write_threads[i], NULL) != 0)
800 ERROR ("plugin: stop_write_threads: pthread_join failed.");
802 write_threads[i] = (pthread_t) 0;
804 sfree (write_threads);
805 write_threads_num = 0;
807 pthread_mutex_lock (&write_lock);
809 for (q = write_queue_head; q != NULL; )
811 write_queue_t *q1 = q;
812 plugin_value_list_free (q->vl);
817 write_queue_head = NULL;
818 write_queue_tail = NULL;
819 write_queue_size = 0;
820 pthread_mutex_unlock (&write_lock);
824 WARNING ("plugin: %i value list%s left after shutting down "
825 "the write threads.",
826 i, (i == 1) ? " was" : "s were");
828 } /* }}} void stop_write_threads */
833 void plugin_set_dir (const char *dir)
835 if (plugindir != NULL)
840 else if ((plugindir = strdup (dir)) == NULL)
843 ERROR ("strdup failed: %s",
844 sstrerror (errno, errbuf, sizeof (errbuf)));
848 static _Bool plugin_is_loaded (char const *name)
852 if (plugins_loaded == NULL)
853 plugins_loaded = c_avl_create ((void *) strcasecmp);
854 assert (plugins_loaded != NULL);
856 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
857 return (status == 0);
860 static int plugin_mark_loaded (char const *name)
865 name_copy = strdup (name);
866 if (name_copy == NULL)
869 status = c_avl_insert (plugins_loaded,
870 /* key = */ name_copy, /* value = */ NULL);
874 static void plugin_free_loaded ()
879 if (plugins_loaded == NULL)
882 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
885 assert (value == NULL);
888 c_avl_destroy (plugins_loaded);
889 plugins_loaded = NULL;
893 int plugin_load (char const *plugin_name, uint32_t flags)
897 char filename[BUFSIZE] = "";
898 char typename[BUFSIZE];
905 if (plugin_name == NULL)
908 /* Check if plugin is already loaded and don't do anything in this
910 if (plugin_is_loaded (plugin_name))
913 dir = plugin_get_dir ();
917 * XXX: Magic at work:
919 * Some of the language bindings, for example the Python and Perl
920 * plugins, need to be able to export symbols to the scripts they run.
921 * For this to happen, the "Globals" flag needs to be set.
922 * Unfortunately, this technical detail is hard to explain to the
923 * average user and she shouldn't have to worry about this, ideally.
924 * So in order to save everyone's sanity use a different default for a
925 * handful of special plugins. --octo
927 if ((strcasecmp ("perl", plugin_name) == 0)
928 || (strcasecmp ("python", plugin_name) == 0))
929 flags |= PLUGIN_FLAGS_GLOBAL;
931 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
932 * type when matching the filename */
933 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
934 if ((status < 0) || ((size_t) status >= sizeof (typename)))
936 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
939 typename_len = strlen (typename);
941 if ((dh = opendir (dir)) == NULL)
944 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
945 sstrerror (errno, errbuf, sizeof (errbuf)));
949 while ((de = readdir (dh)) != NULL)
951 if (strncasecmp (de->d_name, typename, typename_len))
954 status = ssnprintf (filename, sizeof (filename),
955 "%s/%s", dir, de->d_name);
956 if ((status < 0) || ((size_t) status >= sizeof (filename)))
958 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
963 if (lstat (filename, &statbuf) == -1)
966 WARNING ("plugin_load: stat (\"%s\") failed: %s",
968 sstrerror (errno, errbuf, sizeof (errbuf)));
971 else if (!S_ISREG (statbuf.st_mode))
973 /* don't follow symlinks */
974 WARNING ("plugin_load: %s is not a regular file.",
979 status = plugin_load_file (filename, flags);
983 plugin_mark_loaded (plugin_name);
989 ERROR ("plugin_load: Load plugin \"%s\" failed with "
990 "status %i.", plugin_name, status);
996 if (filename[0] == 0)
997 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1004 * The `register_*' functions follow
1006 int plugin_register_config (const char *name,
1007 int (*callback) (const char *key, const char *val),
1008 const char **keys, int keys_num)
1010 cf_register (name, callback, keys, keys_num);
1012 } /* int plugin_register_config */
1014 int plugin_register_complex_config (const char *type,
1015 int (*callback) (oconfig_item_t *))
1017 return (cf_register_complex (type, callback));
1018 } /* int plugin_register_complex_config */
1020 int plugin_register_init (const char *name,
1021 int (*callback) (void))
1023 return (create_register_callback (&list_init, name, (void *) callback,
1024 /* user_data = */ NULL));
1025 } /* plugin_register_init */
1027 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1029 const read_func_t *rf0;
1030 const read_func_t *rf1;
1035 if (rf0->rf_next_read < rf1->rf_next_read)
1037 else if (rf0->rf_next_read > rf1->rf_next_read)
1041 } /* int plugin_compare_read_func */
1043 /* Add a read function to both, the heap and a linked list. The linked list if
1044 * used to look-up read functions, especially for the remove function. The heap
1045 * is used to determine which plugin to read next. */
1046 static int plugin_insert_read (read_func_t *rf)
1051 rf->rf_next_read = cdtime ();
1052 rf->rf_effective_interval = rf->rf_interval;
1054 pthread_mutex_lock (&read_lock);
1056 if (read_list == NULL)
1058 read_list = llist_create ();
1059 if (read_list == NULL)
1061 pthread_mutex_unlock (&read_lock);
1062 ERROR ("plugin_insert_read: read_list failed.");
1067 if (read_heap == NULL)
1069 read_heap = c_heap_create (plugin_compare_read_func);
1070 if (read_heap == NULL)
1072 pthread_mutex_unlock (&read_lock);
1073 ERROR ("plugin_insert_read: c_heap_create failed.");
1078 le = llist_search (read_list, rf->rf_name);
1081 pthread_mutex_unlock (&read_lock);
1082 WARNING ("The read function \"%s\" is already registered. "
1083 "Check for duplicate \"LoadPlugin\" lines "
1084 "in your configuration!",
1089 le = llentry_create (rf->rf_name, rf);
1092 pthread_mutex_unlock (&read_lock);
1093 ERROR ("plugin_insert_read: llentry_create failed.");
1097 status = c_heap_insert (read_heap, rf);
1100 pthread_mutex_unlock (&read_lock);
1101 ERROR ("plugin_insert_read: c_heap_insert failed.");
1102 llentry_destroy (le);
1106 /* This does not fail. */
1107 llist_append (read_list, le);
1109 /* Wake up all the read threads. */
1110 pthread_cond_broadcast (&read_cond);
1111 pthread_mutex_unlock (&read_lock);
1113 } /* int plugin_insert_read */
1115 int plugin_register_read (const char *name,
1116 int (*callback) (void))
1121 rf = malloc (sizeof (*rf));
1124 ERROR ("plugin_register_read: malloc failed.");
1128 memset (rf, 0, sizeof (read_func_t));
1129 rf->rf_callback = (void *) callback;
1130 rf->rf_udata.data = NULL;
1131 rf->rf_udata.free_func = NULL;
1132 rf->rf_ctx = plugin_get_ctx ();
1133 rf->rf_group[0] = '\0';
1134 rf->rf_name = strdup (name);
1135 rf->rf_type = RF_SIMPLE;
1136 rf->rf_interval = plugin_get_interval ();
1138 status = plugin_insert_read (rf);
1143 } /* int plugin_register_read */
1145 int plugin_register_complex_read (const char *group, const char *name,
1146 plugin_read_cb callback,
1147 const struct timespec *interval,
1148 user_data_t *user_data)
1153 rf = malloc (sizeof (*rf));
1156 ERROR ("plugin_register_complex_read: malloc failed.");
1160 memset (rf, 0, sizeof (read_func_t));
1161 rf->rf_callback = (void *) callback;
1163 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1165 rf->rf_group[0] = '\0';
1166 rf->rf_name = strdup (name);
1167 rf->rf_type = RF_COMPLEX;
1168 if (interval != NULL)
1169 rf->rf_interval = TIMESPEC_TO_CDTIME_T (interval);
1171 rf->rf_interval = plugin_get_interval ();
1174 if (user_data == NULL)
1176 rf->rf_udata.data = NULL;
1177 rf->rf_udata.free_func = NULL;
1181 rf->rf_udata = *user_data;
1184 rf->rf_ctx = plugin_get_ctx ();
1186 status = plugin_insert_read (rf);
1191 } /* int plugin_register_complex_read */
1193 int plugin_register_write (const char *name,
1194 plugin_write_cb callback, user_data_t *ud)
1196 return (create_register_callback (&list_write, name,
1197 (void *) callback, ud));
1198 } /* int plugin_register_write */
1200 int plugin_register_flush (const char *name,
1201 plugin_flush_cb callback, user_data_t *ud)
1203 return (create_register_callback (&list_flush, name,
1204 (void *) callback, ud));
1205 } /* int plugin_register_flush */
1207 int plugin_register_missing (const char *name,
1208 plugin_missing_cb callback, user_data_t *ud)
1210 return (create_register_callback (&list_missing, name,
1211 (void *) callback, ud));
1212 } /* int plugin_register_missing */
1214 int plugin_register_shutdown (const char *name,
1215 int (*callback) (void))
1217 return (create_register_callback (&list_shutdown, name,
1218 (void *) callback, /* user_data = */ NULL));
1219 } /* int plugin_register_shutdown */
1221 int plugin_register_data_set (const data_set_t *ds)
1223 data_set_t *ds_copy;
1226 if ((data_sets != NULL)
1227 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1229 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1230 plugin_unregister_data_set (ds->type);
1232 else if (data_sets == NULL)
1234 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1235 if (data_sets == NULL)
1239 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1240 if (ds_copy == NULL)
1242 memcpy(ds_copy, ds, sizeof (data_set_t));
1244 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1246 if (ds_copy->ds == NULL)
1252 for (i = 0; i < ds->ds_num; i++)
1253 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1255 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1256 } /* int plugin_register_data_set */
1258 int plugin_register_log (const char *name,
1259 plugin_log_cb callback, user_data_t *ud)
1261 return (create_register_callback (&list_log, name,
1262 (void *) callback, ud));
1263 } /* int plugin_register_log */
1265 int plugin_register_notification (const char *name,
1266 plugin_notification_cb callback, user_data_t *ud)
1268 return (create_register_callback (&list_notification, name,
1269 (void *) callback, ud));
1270 } /* int plugin_register_log */
1272 int plugin_unregister_config (const char *name)
1274 cf_unregister (name);
1276 } /* int plugin_unregister_config */
1278 int plugin_unregister_complex_config (const char *name)
1280 cf_unregister_complex (name);
1282 } /* int plugin_unregister_complex_config */
1284 int plugin_unregister_init (const char *name)
1286 return (plugin_unregister (list_init, name));
1289 int plugin_unregister_read (const char *name) /* {{{ */
1297 pthread_mutex_lock (&read_lock);
1299 if (read_list == NULL)
1301 pthread_mutex_unlock (&read_lock);
1305 le = llist_search (read_list, name);
1308 pthread_mutex_unlock (&read_lock);
1309 WARNING ("plugin_unregister_read: No such read function: %s",
1314 llist_remove (read_list, le);
1317 assert (rf != NULL);
1318 rf->rf_type = RF_REMOVE;
1320 pthread_mutex_unlock (&read_lock);
1322 llentry_destroy (le);
1324 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1327 } /* }}} int plugin_unregister_read */
1329 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1331 read_func_t *rf = e->value;
1334 return strcmp (rf->rf_group, (const char *)group);
1335 } /* }}} int compare_read_func_group */
1337 int plugin_unregister_read_group (const char *group) /* {{{ */
1347 pthread_mutex_lock (&read_lock);
1349 if (read_list == NULL)
1351 pthread_mutex_unlock (&read_lock);
1357 le = llist_search_custom (read_list,
1358 compare_read_func_group, (void *)group);
1365 llist_remove (read_list, le);
1368 assert (rf != NULL);
1369 rf->rf_type = RF_REMOVE;
1371 llentry_destroy (le);
1373 DEBUG ("plugin_unregister_read_group: "
1374 "Marked `%s' (group `%s') for removal.",
1375 rf->rf_name, group);
1378 pthread_mutex_unlock (&read_lock);
1382 WARNING ("plugin_unregister_read_group: No such "
1383 "group of read function: %s", group);
1388 } /* }}} int plugin_unregister_read_group */
1390 int plugin_unregister_write (const char *name)
1392 return (plugin_unregister (list_write, name));
1395 int plugin_unregister_flush (const char *name)
1397 return (plugin_unregister (list_flush, name));
1400 int plugin_unregister_missing (const char *name)
1402 return (plugin_unregister (list_missing, name));
1405 int plugin_unregister_shutdown (const char *name)
1407 return (plugin_unregister (list_shutdown, name));
1410 int plugin_unregister_data_set (const char *name)
1414 if (data_sets == NULL)
1417 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1424 } /* int plugin_unregister_data_set */
1426 int plugin_unregister_log (const char *name)
1428 return (plugin_unregister (list_log, name));
1431 int plugin_unregister_notification (const char *name)
1433 return (plugin_unregister (list_notification, name));
1436 void plugin_init_all (void)
1438 const char *chain_name;
1442 /* Init the value cache */
1445 chain_name = global_option_get ("PreCacheChain");
1446 pre_cache_chain = fc_chain_get_by_name (chain_name);
1448 chain_name = global_option_get ("PostCacheChain");
1449 post_cache_chain = fc_chain_get_by_name (chain_name);
1452 const char *str_writequeuelengthlimithigh = global_option_get ("WriteQueueLengthLimitHigh");
1453 const char *str_writequeuelengthlimitlow = global_option_get ("WriteQueueLengthLimitLow");
1455 writequeuelengthlimit_high = 0;
1456 writequeuelengthlimit_low = 0;
1458 if(NULL != str_writequeuelengthlimithigh) {
1460 /* get high limit */
1461 writequeuelengthlimit_high = strtol(str_writequeuelengthlimithigh, NULL, 10);
1462 if ((errno == ERANGE && (writequeuelengthlimit_high == LONG_MAX || writequeuelengthlimit_high == LONG_MIN))
1463 || (errno != 0 && writequeuelengthlimit_high == 0)
1465 writequeuelengthlimit_high = 0;
1466 ERROR("Config 'WriteQueueLengthLimitHigh' accepts one integer value only. Running with no limit !");
1468 if(writequeuelengthlimit_high < 0) {
1469 ERROR("Config 'WriteQueueLengthLimitHigh' accepts positive values only. Running with no limit !");
1470 writequeuelengthlimit_high = 0;
1474 if((writequeuelengthlimit_high > 0) && (NULL != str_writequeuelengthlimitlow)) {
1477 writequeuelengthlimit_low = strtol(str_writequeuelengthlimitlow, NULL, 10);
1478 if ((errno == ERANGE && (writequeuelengthlimit_low == LONG_MAX || writequeuelengthlimit_low == LONG_MIN))
1479 || (errno != 0 && writequeuelengthlimit_low == 0)
1481 writequeuelengthlimit_low = 0;
1482 ERROR("Config 'WriteQueueLengthLimitLow' accepts one integer value only. Using default low limit instead");
1485 if(writequeuelengthlimit_low < 0) {
1486 ERROR("Config 'WriteQueueLengthLimitLow' accepts positive values only. Using default low limit instead");
1487 writequeuelengthlimit_low = 0;
1488 } else if(writequeuelengthlimit_low > writequeuelengthlimit_high) {
1489 ERROR("Config 'WriteQueueLengthLimitLow' (%ld) cannot be bigger than high limit (%ld). Using default low limit instead",
1490 writequeuelengthlimit_low, writequeuelengthlimit_high);
1491 writequeuelengthlimit_low = 0;
1494 /* Check/compute low limit if not/badly defined */
1495 if(writequeuelengthlimit_high > 0) {
1496 if(0 == writequeuelengthlimit_low) {
1497 writequeuelengthlimit_low = .5 * writequeuelengthlimit_high;
1499 INFO("Config 'WriteQueueLengthLimit*' : Running with limits high=%ld low=%ld", writequeuelengthlimit_high, writequeuelengthlimit_low);
1500 random_seed = time(0);
1502 writequeuelengthlimit_low = 0; /* This should be useless, but in case... */
1507 char const *tmp = global_option_get ("WriteThreads");
1508 int num = atoi (tmp);
1513 start_write_threads ((size_t) num);
1516 if ((list_init == NULL) && (read_heap == NULL))
1519 /* Calling all init callbacks before checking if read callbacks
1520 * are available allows the init callbacks to register the read
1522 le = llist_head (list_init);
1525 callback_func_t *cf;
1526 plugin_init_cb callback;
1527 plugin_ctx_t old_ctx;
1530 old_ctx = plugin_set_ctx (cf->cf_ctx);
1531 callback = cf->cf_callback;
1532 status = (*callback) ();
1533 plugin_set_ctx (old_ctx);
1537 ERROR ("Initialization of plugin `%s' "
1538 "failed with status %i. "
1539 "Plugin will be unloaded.",
1541 /* Plugins that register read callbacks from the init
1542 * callback should take care of appropriate error
1543 * handling themselves. */
1544 /* FIXME: Unload _all_ functions */
1545 plugin_unregister_read (le->key);
1551 /* Start read-threads */
1552 if (read_heap != NULL)
1556 rt = global_option_get ("ReadThreads");
1559 start_read_threads ((num > 0) ? num : 5);
1561 } /* void plugin_init_all */
1563 /* TODO: Rename this function. */
1564 void plugin_read_all (void)
1566 uc_check_timeout ();
1569 } /* void plugin_read_all */
1571 /* Read function called when the `-T' command line argument is given. */
1572 int plugin_read_all_once (void)
1575 int return_status = 0;
1577 if (read_heap == NULL)
1579 NOTICE ("No read-functions are registered.");
1586 plugin_ctx_t old_ctx;
1588 rf = c_heap_get_root (read_heap);
1592 old_ctx = plugin_set_ctx (rf->rf_ctx);
1594 if (rf->rf_type == RF_SIMPLE)
1596 int (*callback) (void);
1598 callback = rf->rf_callback;
1599 status = (*callback) ();
1603 plugin_read_cb callback;
1605 callback = rf->rf_callback;
1606 status = (*callback) (&rf->rf_udata);
1609 plugin_set_ctx (old_ctx);
1613 NOTICE ("read-function of plugin `%s' failed.",
1618 destroy_callback ((void *) rf);
1621 return (return_status);
1622 } /* int plugin_read_all_once */
1624 int plugin_write (const char *plugin, /* {{{ */
1625 const data_set_t *ds, const value_list_t *vl)
1633 if (list_write == NULL)
1638 ds = plugin_get_ds (vl->type);
1641 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1651 le = llist_head (list_write);
1654 callback_func_t *cf = le->value;
1655 plugin_write_cb callback;
1657 /* do not switch plugin context; rather keep the context (interval)
1658 * information of the calling read plugin */
1660 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1661 callback = cf->cf_callback;
1662 status = (*callback) (ds, vl, &cf->cf_udata);
1671 if ((success == 0) && (failure != 0))
1676 else /* plugin != NULL */
1678 callback_func_t *cf;
1679 plugin_write_cb callback;
1681 le = llist_head (list_write);
1684 if (strcasecmp (plugin, le->key) == 0)
1695 /* do not switch plugin context; rather keep the context (interval)
1696 * information of the calling read plugin */
1698 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1699 callback = cf->cf_callback;
1700 status = (*callback) (ds, vl, &cf->cf_udata);
1704 } /* }}} int plugin_write */
1706 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1710 if (list_flush == NULL)
1713 le = llist_head (list_flush);
1716 callback_func_t *cf;
1717 plugin_flush_cb callback;
1718 plugin_ctx_t old_ctx;
1720 if ((plugin != NULL)
1721 && (strcmp (plugin, le->key) != 0))
1728 old_ctx = plugin_set_ctx (cf->cf_ctx);
1729 callback = cf->cf_callback;
1731 (*callback) (timeout, identifier, &cf->cf_udata);
1733 plugin_set_ctx (old_ctx);
1738 } /* int plugin_flush */
1740 void plugin_shutdown_all (void)
1744 stop_read_threads ();
1746 destroy_all_callbacks (&list_init);
1748 pthread_mutex_lock (&read_lock);
1749 llist_destroy (read_list);
1751 pthread_mutex_unlock (&read_lock);
1753 destroy_read_heap ();
1755 plugin_flush (/* plugin = */ NULL,
1757 /* identifier = */ NULL);
1760 if (list_shutdown != NULL)
1761 le = llist_head (list_shutdown);
1765 callback_func_t *cf;
1766 plugin_shutdown_cb callback;
1767 plugin_ctx_t old_ctx;
1770 old_ctx = plugin_set_ctx (cf->cf_ctx);
1771 callback = cf->cf_callback;
1773 /* Advance the pointer before calling the callback allows
1774 * shutdown functions to unregister themselves. If done the
1775 * other way around the memory `le' points to will be freed
1776 * after callback returns. */
1781 plugin_set_ctx (old_ctx);
1784 stop_write_threads ();
1786 /* Write plugins which use the `user_data' pointer usually need the
1787 * same data available to the flush callback. If this is the case, set
1788 * the free_function to NULL when registering the flush callback and to
1789 * the real free function when registering the write callback. This way
1790 * the data isn't freed twice. */
1791 destroy_all_callbacks (&list_flush);
1792 destroy_all_callbacks (&list_missing);
1793 destroy_all_callbacks (&list_write);
1795 destroy_all_callbacks (&list_notification);
1796 destroy_all_callbacks (&list_shutdown);
1797 destroy_all_callbacks (&list_log);
1799 plugin_free_loaded ();
1800 } /* void plugin_shutdown_all */
1802 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1806 if (list_missing == NULL)
1809 le = llist_head (list_missing);
1812 callback_func_t *cf;
1813 plugin_missing_cb callback;
1814 plugin_ctx_t old_ctx;
1818 old_ctx = plugin_set_ctx (cf->cf_ctx);
1819 callback = cf->cf_callback;
1821 status = (*callback) (vl, &cf->cf_udata);
1822 plugin_set_ctx (old_ctx);
1827 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1828 "failed with status %i.",
1841 } /* int }}} plugin_dispatch_missing */
1843 static int plugin_dispatch_values_internal (value_list_t *vl)
1846 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1848 value_t *saved_values;
1849 int saved_values_len;
1853 int free_meta_data = 0;
1855 if ((vl == NULL) || (vl->type[0] == 0)
1856 || (vl->values == NULL) || (vl->values_len < 1))
1858 ERROR ("plugin_dispatch_values: Invalid value list "
1859 "from plugin %s.", vl->plugin);
1863 /* Free meta data only if the calling function didn't specify any. In
1864 * this case matches and targets may add some and the calling function
1865 * may not expect (and therefore free) that data. */
1866 if (vl->meta == NULL)
1869 if (list_write == NULL)
1870 c_complain_once (LOG_WARNING, &no_write_complaint,
1871 "plugin_dispatch_values: No write callback has been "
1872 "registered. Please load at least one output plugin, "
1873 "if you want the collected data to be stored.");
1875 if (data_sets == NULL)
1877 ERROR ("plugin_dispatch_values: No data sets registered. "
1878 "Could the types database be read? Check "
1879 "your `TypesDB' setting!");
1883 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1885 char ident[6 * DATA_MAX_NAME_LEN];
1887 FORMAT_VL (ident, sizeof (ident), vl);
1888 INFO ("plugin_dispatch_values: Dataset not found: %s "
1889 "(from \"%s\"), check your types.db!",
1894 /* Assured by plugin_value_list_clone(). The time is determined at
1895 * _enqueue_ time. */
1896 assert (vl->time != 0);
1897 assert (vl->interval != 0);
1899 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1901 "plugin = %s; plugin_instance = %s; "
1902 "type = %s; type_instance = %s;",
1903 CDTIME_T_TO_DOUBLE (vl->time),
1904 CDTIME_T_TO_DOUBLE (vl->interval),
1906 vl->plugin, vl->plugin_instance,
1907 vl->type, vl->type_instance);
1910 assert (0 == strcmp (ds->type, vl->type));
1912 if (0 != strcmp (ds->type, vl->type))
1913 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1914 ds->type, vl->type);
1918 assert (ds->ds_num == vl->values_len);
1920 if (ds->ds_num != vl->values_len)
1922 ERROR ("plugin_dispatch_values: ds->type = %s: "
1923 "(ds->ds_num = %i) != "
1924 "(vl->values_len = %i)",
1925 ds->type, ds->ds_num, vl->values_len);
1930 escape_slashes (vl->host, sizeof (vl->host));
1931 escape_slashes (vl->plugin, sizeof (vl->plugin));
1932 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1933 escape_slashes (vl->type, sizeof (vl->type));
1934 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1936 /* Copy the values. This way, we can assure `targets' that they get
1937 * dynamically allocated values, which they can free and replace if
1939 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1941 saved_values = vl->values;
1942 saved_values_len = vl->values_len;
1944 vl->values = (value_t *) calloc (vl->values_len,
1945 sizeof (*vl->values));
1946 if (vl->values == NULL)
1948 ERROR ("plugin_dispatch_values: calloc failed.");
1949 vl->values = saved_values;
1952 memcpy (vl->values, saved_values,
1953 vl->values_len * sizeof (*vl->values));
1955 else /* if ((pre == NULL) && (post == NULL)) */
1957 saved_values = NULL;
1958 saved_values_len = 0;
1961 if (pre_cache_chain != NULL)
1963 status = fc_process_chain (ds, vl, pre_cache_chain);
1966 WARNING ("plugin_dispatch_values: Running the "
1967 "pre-cache chain failed with "
1971 else if (status == FC_TARGET_STOP)
1973 /* Restore the state of the value_list so that plugins
1974 * don't get confused.. */
1975 if (saved_values != NULL)
1978 vl->values = saved_values;
1979 vl->values_len = saved_values_len;
1985 /* Update the value cache */
1988 if (post_cache_chain != NULL)
1990 status = fc_process_chain (ds, vl, post_cache_chain);
1993 WARNING ("plugin_dispatch_values: Running the "
1994 "post-cache chain failed with "
2000 fc_default_action (ds, vl);
2002 /* Restore the state of the value_list so that plugins don't get
2004 if (saved_values != NULL)
2007 vl->values = saved_values;
2008 vl->values_len = saved_values_len;
2011 if ((free_meta_data != 0) && (vl->meta != NULL))
2013 meta_data_destroy (vl->meta);
2018 } /* int plugin_dispatch_values_internal */
2020 int plugin_dispatch_values (value_list_t const *vl)
2023 int wq_size = write_queue_size;
2024 /* We store write_queue_size in a local variable because other threads may update write_queue_size.
2025 * Having this in a local variable (like a cache) is better : we do not need a lock */
2026 short metric_will_be_dropped = 0;
2028 if((writequeuelengthlimit_high > 0) && (wq_size > writequeuelengthlimit_low)) {
2029 if(wq_size >= writequeuelengthlimit_high) {
2030 /* if high == low, we come here too */
2031 metric_will_be_dropped = 1;
2033 /* here, high != low */
2034 long probability_to_drop;
2037 probability_to_drop = (wq_size - writequeuelengthlimit_low);
2039 /* We use rand_r with its bad RNG because it's enough for playing dices.
2040 * There is no security consideration here so rand_r() should be enough here.
2042 n = rand_r(&random_seed) % (writequeuelengthlimit_high - writequeuelengthlimit_low) ;
2044 /* Let's have X = high - low.
2045 * n is in range [0..X]
2046 * probability_to_drop is in range [1..X[
2047 * probability_to_drop gets bigger when wq_size gets bigger.
2049 if(n <= probability_to_drop) {
2050 metric_will_be_dropped = 1;
2055 if( ! metric_will_be_dropped) {
2056 status = plugin_write_enqueue (vl);
2060 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2061 "with status %i (%s).", status,
2062 sstrerror (status, errbuf, sizeof (errbuf)));
2068 /* If you want to count dropped metrics, don't forget to add a lock here */
2069 /* dropped_metrics++; */
2070 ERROR ("plugin_dispatch_values: value dropped (write queue %ld > %ld) : time = %.3f; interval = %.3f ; %s/%s%s%s/%s%s%s",
2071 write_queue_size, writequeuelengthlimit_low,
2072 CDTIME_T_TO_DOUBLE (vl->time),
2073 CDTIME_T_TO_DOUBLE (vl->interval),
2075 vl->plugin, vl->plugin_instance[0]?"-":"", vl->plugin_instance,
2076 vl->type, vl->type_instance[0]?"-":"", vl->type_instance);
2082 int plugin_dispatch_notification (const notification_t *notif)
2085 /* Possible TODO: Add flap detection here */
2087 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2088 "time = %.3f; host = %s;",
2089 notif->severity, notif->message,
2090 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2092 /* Nobody cares for notifications */
2093 if (list_notification == NULL)
2096 le = llist_head (list_notification);
2099 callback_func_t *cf;
2100 plugin_notification_cb callback;
2103 /* do not switch plugin context; rather keep the context
2104 * (interval) information of the calling plugin */
2107 callback = cf->cf_callback;
2108 status = (*callback) (notif, &cf->cf_udata);
2111 WARNING ("plugin_dispatch_notification: Notification "
2112 "callback %s returned %i.",
2120 } /* int plugin_dispatch_notification */
2122 void plugin_log (int level, const char *format, ...)
2129 if (level >= LOG_DEBUG)
2133 va_start (ap, format);
2134 vsnprintf (msg, sizeof (msg), format, ap);
2135 msg[sizeof (msg) - 1] = '\0';
2138 if (list_log == NULL)
2140 fprintf (stderr, "%s\n", msg);
2144 le = llist_head (list_log);
2147 callback_func_t *cf;
2148 plugin_log_cb callback;
2151 callback = cf->cf_callback;
2153 /* do not switch plugin context; rather keep the context
2154 * (interval) information of the calling plugin */
2156 (*callback) (level, msg, &cf->cf_udata);
2160 } /* void plugin_log */
2162 int parse_log_severity (const char *severity)
2166 if ((0 == strcasecmp (severity, "emerg"))
2167 || (0 == strcasecmp (severity, "alert"))
2168 || (0 == strcasecmp (severity, "crit"))
2169 || (0 == strcasecmp (severity, "err")))
2170 log_level = LOG_ERR;
2171 else if (0 == strcasecmp (severity, "warning"))
2172 log_level = LOG_WARNING;
2173 else if (0 == strcasecmp (severity, "notice"))
2174 log_level = LOG_NOTICE;
2175 else if (0 == strcasecmp (severity, "info"))
2176 log_level = LOG_INFO;
2178 else if (0 == strcasecmp (severity, "debug"))
2179 log_level = LOG_DEBUG;
2180 #endif /* COLLECT_DEBUG */
2183 } /* int parse_log_severity */
2185 int parse_notif_severity (const char *severity)
2187 int notif_severity = -1;
2189 if (strcasecmp (severity, "FAILURE") == 0)
2190 notif_severity = NOTIF_FAILURE;
2191 else if (strcmp (severity, "OKAY") == 0)
2192 notif_severity = NOTIF_OKAY;
2193 else if ((strcmp (severity, "WARNING") == 0)
2194 || (strcmp (severity, "WARN") == 0))
2195 notif_severity = NOTIF_WARNING;
2197 return (notif_severity);
2198 } /* int parse_notif_severity */
2200 const data_set_t *plugin_get_ds (const char *name)
2204 if (data_sets == NULL)
2206 ERROR ("plugin_get_ds: No data sets are defined yet.");
2210 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2212 DEBUG ("No such dataset registered: %s", name);
2217 } /* data_set_t *plugin_get_ds */
2219 static int plugin_notification_meta_add (notification_t *n,
2221 enum notification_meta_type_e type,
2224 notification_meta_t *meta;
2225 notification_meta_t *tail;
2227 if ((n == NULL) || (name == NULL) || (value == NULL))
2229 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2233 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2236 ERROR ("plugin_notification_meta_add: malloc failed.");
2239 memset (meta, 0, sizeof (notification_meta_t));
2241 sstrncpy (meta->name, name, sizeof (meta->name));
2246 case NM_TYPE_STRING:
2248 meta->nm_value.nm_string = strdup ((const char *) value);
2249 if (meta->nm_value.nm_string == NULL)
2251 ERROR ("plugin_notification_meta_add: strdup failed.");
2257 case NM_TYPE_SIGNED_INT:
2259 meta->nm_value.nm_signed_int = *((int64_t *) value);
2262 case NM_TYPE_UNSIGNED_INT:
2264 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2267 case NM_TYPE_DOUBLE:
2269 meta->nm_value.nm_double = *((double *) value);
2272 case NM_TYPE_BOOLEAN:
2274 meta->nm_value.nm_boolean = *((_Bool *) value);
2279 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2283 } /* switch (type) */
2287 while ((tail != NULL) && (tail->next != NULL))
2296 } /* int plugin_notification_meta_add */
2298 int plugin_notification_meta_add_string (notification_t *n,
2302 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2305 int plugin_notification_meta_add_signed_int (notification_t *n,
2309 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2312 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2316 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2319 int plugin_notification_meta_add_double (notification_t *n,
2323 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2326 int plugin_notification_meta_add_boolean (notification_t *n,
2330 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2333 int plugin_notification_meta_copy (notification_t *dst,
2334 const notification_t *src)
2336 notification_meta_t *meta;
2338 assert (dst != NULL);
2339 assert (src != NULL);
2340 assert (dst != src);
2341 assert ((src->meta == NULL) || (src->meta != dst->meta));
2343 for (meta = src->meta; meta != NULL; meta = meta->next)
2345 if (meta->type == NM_TYPE_STRING)
2346 plugin_notification_meta_add_string (dst, meta->name,
2347 meta->nm_value.nm_string);
2348 else if (meta->type == NM_TYPE_SIGNED_INT)
2349 plugin_notification_meta_add_signed_int (dst, meta->name,
2350 meta->nm_value.nm_signed_int);
2351 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2352 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2353 meta->nm_value.nm_unsigned_int);
2354 else if (meta->type == NM_TYPE_DOUBLE)
2355 plugin_notification_meta_add_double (dst, meta->name,
2356 meta->nm_value.nm_double);
2357 else if (meta->type == NM_TYPE_BOOLEAN)
2358 plugin_notification_meta_add_boolean (dst, meta->name,
2359 meta->nm_value.nm_boolean);
2363 } /* int plugin_notification_meta_copy */
2365 int plugin_notification_meta_free (notification_meta_t *n)
2367 notification_meta_t *this;
2368 notification_meta_t *next;
2372 ERROR ("plugin_notification_meta_free: n == NULL!");
2377 while (this != NULL)
2381 if (this->type == NM_TYPE_STRING)
2383 free ((char *)this->nm_value.nm_string);
2384 this->nm_value.nm_string = NULL;
2392 } /* int plugin_notification_meta_free */
2394 static void plugin_ctx_destructor (void *ctx)
2397 } /* void plugin_ctx_destructor */
2399 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2401 static plugin_ctx_t *plugin_ctx_create (void)
2405 ctx = malloc (sizeof (*ctx));
2408 ERROR ("Failed to allocate plugin context: %s",
2409 sstrerror (errno, errbuf, sizeof (errbuf)));
2414 assert (plugin_ctx_key_initialized);
2415 pthread_setspecific (plugin_ctx_key, ctx);
2416 DEBUG("Created new plugin context.");
2418 } /* int plugin_ctx_create */
2420 void plugin_init_ctx (void)
2422 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2423 plugin_ctx_key_initialized = 1;
2424 } /* void plugin_init_ctx */
2426 plugin_ctx_t plugin_get_ctx (void)
2430 assert (plugin_ctx_key_initialized);
2431 ctx = pthread_getspecific (plugin_ctx_key);
2434 ctx = plugin_ctx_create ();
2435 /* this must no happen -- exit() instead? */
2441 } /* plugin_ctx_t plugin_get_ctx */
2443 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2448 assert (plugin_ctx_key_initialized);
2449 c = pthread_getspecific (plugin_ctx_key);
2452 c = plugin_ctx_create ();
2453 /* this must no happen -- exit() instead? */
2462 } /* void plugin_set_ctx */
2464 cdtime_t plugin_get_interval (void)
2468 interval = plugin_get_ctx().interval;
2472 return cf_get_default_interval ();
2473 } /* cdtime_t plugin_get_interval */
2477 void *(*start_routine) (void *);
2481 static void *plugin_thread_start (void *arg)
2483 plugin_thread_t *plugin_thread = arg;
2485 void *(*start_routine) (void *) = plugin_thread->start_routine;
2486 void *plugin_arg = plugin_thread->arg;
2488 plugin_set_ctx (plugin_thread->ctx);
2490 free (plugin_thread);
2492 return start_routine (plugin_arg);
2493 } /* void *plugin_thread_start */
2495 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2496 void *(*start_routine) (void *), void *arg)
2498 plugin_thread_t *plugin_thread;
2500 plugin_thread = malloc (sizeof (*plugin_thread));
2501 if (plugin_thread == NULL)
2504 plugin_thread->ctx = plugin_get_ctx ();
2505 plugin_thread->start_routine = start_routine;
2506 plugin_thread->arg = arg;
2508 return pthread_create (thread, attr,
2509 plugin_thread_start, plugin_thread);
2510 } /* int plugin_thread_create */
2512 /* vim: set sw=8 ts=8 noet fdm=marker : */