2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2014 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian Harl <sh at tokkee.org>
31 #include "configfile.h"
32 #include "filter_chain.h"
33 #include "utils_avltree.h"
34 #include "utils_cache.h"
35 #include "utils_complain.h"
36 #include "utils_llist.h"
37 #include "utils_heap.h"
38 #include "utils_time.h"
39 #include "utils_random.h"
50 struct callback_func_s
56 typedef struct callback_func_s callback_func_t;
60 #define RF_REMOVE 65535
63 /* `read_func_t' "inherits" from `callback_func_t'.
64 * The `rf_super' member MUST be the first one in this structure! */
65 #define rf_callback rf_super.cf_callback
66 #define rf_udata rf_super.cf_udata
67 #define rf_ctx rf_super.cf_ctx
68 callback_func_t rf_super;
69 char rf_group[DATA_MAX_NAME_LEN];
73 cdtime_t rf_effective_interval;
74 cdtime_t rf_next_read;
76 typedef struct read_func_s read_func_t;
79 typedef struct write_queue_s write_queue_t;
90 static c_avl_tree_t *plugins_loaded = NULL;
92 static llist_t *list_init;
93 static llist_t *list_write;
94 static llist_t *list_flush;
95 static llist_t *list_missing;
96 static llist_t *list_shutdown;
97 static llist_t *list_log;
98 static llist_t *list_notification;
100 static fc_chain_t *pre_cache_chain = NULL;
101 static fc_chain_t *post_cache_chain = NULL;
103 static c_avl_tree_t *data_sets;
105 static char *plugindir = NULL;
107 #ifndef DEFAULT_MAX_READ_INTERVAL
108 # define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T (86400)
110 static c_heap_t *read_heap = NULL;
111 static llist_t *read_list;
112 static int read_loop = 1;
113 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
114 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
115 static pthread_t *read_threads = NULL;
116 static int read_threads_num = 0;
117 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
119 static write_queue_t *write_queue_head;
120 static write_queue_t *write_queue_tail;
121 static long write_queue_length = 0;
122 static _Bool write_loop = 1;
123 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
124 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
125 static pthread_t *write_threads = NULL;
126 static size_t write_threads_num = 0;
128 static pthread_key_t plugin_ctx_key;
129 static _Bool plugin_ctx_key_initialized = 0;
131 static long write_limit_high = 0;
132 static long write_limit_low = 0;
137 static int plugin_dispatch_values_internal (value_list_t *vl);
139 static const char *plugin_get_dir (void)
141 if (plugindir == NULL)
147 static void destroy_callback (callback_func_t *cf) /* {{{ */
152 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
154 cf->cf_udata.free_func (cf->cf_udata.data);
155 cf->cf_udata.data = NULL;
156 cf->cf_udata.free_func = NULL;
159 } /* }}} void destroy_callback */
161 static void destroy_all_callbacks (llist_t **list) /* {{{ */
168 le = llist_head (*list);
176 destroy_callback (le->value);
182 llist_destroy (*list);
184 } /* }}} void destroy_all_callbacks */
186 static void destroy_read_heap (void) /* {{{ */
188 if (read_heap == NULL)
195 cf = c_heap_get_root (read_heap);
199 destroy_callback (cf);
202 c_heap_destroy (read_heap);
204 } /* }}} void destroy_read_heap */
206 static int register_callback (llist_t **list, /* {{{ */
207 const char *name, callback_func_t *cf)
214 *list = llist_create ();
217 ERROR ("plugin: register_callback: "
218 "llist_create failed.");
219 destroy_callback (cf);
227 ERROR ("plugin: register_callback: strdup failed.");
228 destroy_callback (cf);
232 le = llist_search (*list, name);
235 le = llentry_create (key, cf);
238 ERROR ("plugin: register_callback: "
239 "llentry_create failed.");
241 destroy_callback (cf);
245 llist_append (*list, le);
249 callback_func_t *old_cf;
254 WARNING ("plugin: register_callback: "
255 "a callback named `%s' already exists - "
256 "overwriting the old entry!", name);
258 destroy_callback (old_cf);
263 } /* }}} int register_callback */
265 static int create_register_callback (llist_t **list, /* {{{ */
266 const char *name, void *callback, user_data_t *ud)
270 cf = (callback_func_t *) malloc (sizeof (*cf));
273 ERROR ("plugin: create_register_callback: malloc failed.");
276 memset (cf, 0, sizeof (*cf));
278 cf->cf_callback = callback;
281 cf->cf_udata.data = NULL;
282 cf->cf_udata.free_func = NULL;
289 cf->cf_ctx = plugin_get_ctx ();
291 return (register_callback (list, name, cf));
292 } /* }}} int create_register_callback */
294 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
301 e = llist_search (list, name);
305 llist_remove (list, e);
308 destroy_callback (e->value);
313 } /* }}} int plugin_unregister */
316 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
317 * object, but it will bitch about a shared object not having a
318 * ``module_register'' symbol..
320 static int plugin_load_file (char *file, uint32_t flags)
323 void (*reg_handle) (void);
326 lt_dlerror (); /* clear errors */
328 #if LIBTOOL_VERSION == 2
329 if (flags & PLUGIN_FLAGS_GLOBAL) {
331 lt_dladvise_init(&advise);
332 lt_dladvise_global(&advise);
333 dlh = lt_dlopenadvise(file, advise);
334 lt_dladvise_destroy(&advise);
336 dlh = lt_dlopen (file);
338 #else /* if LIBTOOL_VERSION == 1 */
339 if (flags & PLUGIN_FLAGS_GLOBAL)
340 WARNING ("plugin_load_file: The global flag is not supported, "
341 "libtool 2 is required for this.");
342 dlh = lt_dlopen (file);
347 char errbuf[1024] = "";
349 ssnprintf (errbuf, sizeof (errbuf),
350 "lt_dlopen (\"%s\") failed: %s. "
351 "The most common cause for this problem are "
352 "missing dependencies. Use ldd(1) to check "
353 "the dependencies of the plugin "
355 file, lt_dlerror ());
357 ERROR ("%s", errbuf);
358 /* Make sure this is printed to STDERR in any case, but also
359 * make sure it's printed only once. */
360 if (list_log != NULL)
361 fprintf (stderr, "ERROR: %s\n", errbuf);
366 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
368 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
369 file, lt_dlerror ());
379 static void *plugin_read_thread (void __attribute__((unused)) *args)
381 while (read_loop != 0)
384 plugin_ctx_t old_ctx;
390 /* Get the read function that needs to be read next.
391 * We don't need to hold "read_lock" for the heap, but we need
392 * to call c_heap_get_root() and pthread_cond_wait() in the
393 * same protected block. */
394 pthread_mutex_lock (&read_lock);
395 rf = c_heap_get_root (read_heap);
398 pthread_cond_wait (&read_cond, &read_lock);
399 pthread_mutex_unlock (&read_lock);
402 pthread_mutex_unlock (&read_lock);
404 if (rf->rf_interval == 0)
406 /* this should not happen, because the interval is set
407 * for each plugin when loading it
408 * XXX: issue a warning? */
409 rf->rf_interval = plugin_get_interval ();
410 rf->rf_effective_interval = rf->rf_interval;
412 rf->rf_next_read = cdtime ();
415 /* sleep until this entry is due,
416 * using pthread_cond_timedwait */
417 pthread_mutex_lock (&read_lock);
418 /* In pthread_cond_timedwait, spurious wakeups are possible
419 * (and really happen, at least on NetBSD with > 1 CPU), thus
420 * we need to re-evaluate the condition every time
421 * pthread_cond_timedwait returns. */
423 while ((read_loop != 0)
424 && (cdtime () < rf->rf_next_read)
427 struct timespec ts = { 0 };
429 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
431 rc = pthread_cond_timedwait (&read_cond, &read_lock,
435 /* Must hold `read_lock' when accessing `rf->rf_type'. */
436 rf_type = rf->rf_type;
437 pthread_mutex_unlock (&read_lock);
439 /* Check if we're supposed to stop.. This may have interrupted
443 /* Insert `rf' again, so it can be free'd correctly */
444 c_heap_insert (read_heap, rf);
448 /* The entry has been marked for deletion. The linked list
449 * entry has already been removed by `plugin_unregister_read'.
450 * All we have to do here is free the `read_func_t' and
452 if (rf_type == RF_REMOVE)
454 DEBUG ("plugin_read_thread: Destroying the `%s' "
455 "callback.", rf->rf_name);
457 destroy_callback ((callback_func_t *) rf);
462 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
464 old_ctx = plugin_set_ctx (rf->rf_ctx);
466 if (rf_type == RF_SIMPLE)
468 int (*callback) (void);
470 callback = rf->rf_callback;
471 status = (*callback) ();
475 plugin_read_cb callback;
477 assert (rf_type == RF_COMPLEX);
479 callback = rf->rf_callback;
480 status = (*callback) (&rf->rf_udata);
483 plugin_set_ctx (old_ctx);
485 /* If the function signals failure, we will increase the
486 * intervals in which it will be called. */
489 rf->rf_effective_interval *= 2;
490 if (rf->rf_effective_interval > max_read_interval)
491 rf->rf_effective_interval = max_read_interval;
493 NOTICE ("read-function of plugin `%s' failed. "
494 "Will suspend it for %.3f seconds.",
496 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
500 /* Success: Restore the interval, if it was changed. */
501 rf->rf_effective_interval = rf->rf_interval;
504 /* update the ``next read due'' field */
507 DEBUG ("plugin_read_thread: Effective interval of the "
508 "%s plugin is %.3f seconds.",
510 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
512 /* Calculate the next (absolute) time at which this function
513 * should be called. */
514 rf->rf_next_read += rf->rf_effective_interval;
516 /* Check, if `rf_next_read' is in the past. */
517 if (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 rf->rf_next_read = now;
525 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
527 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
529 /* Re-insert this read function into the heap again. */
530 c_heap_insert (read_heap, rf);
531 } /* while (read_loop) */
535 } /* void *plugin_read_thread */
537 static void start_read_threads (int num)
541 if (read_threads != NULL)
544 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
545 if (read_threads == NULL)
547 ERROR ("plugin: start_read_threads: calloc failed.");
551 read_threads_num = 0;
552 for (i = 0; i < num; i++)
554 if (pthread_create (read_threads + read_threads_num, NULL,
555 plugin_read_thread, NULL) == 0)
561 ERROR ("plugin: start_read_threads: pthread_create failed.");
565 } /* void start_read_threads */
567 static void stop_read_threads (void)
571 if (read_threads == NULL)
574 INFO ("collectd: Stopping %i read threads.", read_threads_num);
576 pthread_mutex_lock (&read_lock);
578 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
579 pthread_cond_broadcast (&read_cond);
580 pthread_mutex_unlock (&read_lock);
582 for (i = 0; i < read_threads_num; i++)
584 if (pthread_join (read_threads[i], NULL) != 0)
586 ERROR ("plugin: stop_read_threads: pthread_join failed.");
588 read_threads[i] = (pthread_t) 0;
590 sfree (read_threads);
591 read_threads_num = 0;
592 } /* void stop_read_threads */
594 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
599 meta_data_destroy (vl->meta);
602 } /* }}} void plugin_value_list_free */
604 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
611 vl = malloc (sizeof (*vl));
614 memcpy (vl, vl_orig, sizeof (*vl));
616 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
617 if (vl->values == NULL)
619 plugin_value_list_free (vl);
622 memcpy (vl->values, vl_orig->values,
623 vl_orig->values_len * sizeof (*vl->values));
625 vl->meta = meta_data_clone (vl->meta);
626 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
628 plugin_value_list_free (vl);
633 vl->time = cdtime ();
635 /* Fill in the interval from the thread context, if it is zero. */
636 if (vl->interval == 0)
638 plugin_ctx_t ctx = plugin_get_ctx ();
640 if (ctx.interval != 0)
641 vl->interval = ctx.interval;
644 char name[6 * DATA_MAX_NAME_LEN];
645 FORMAT_VL (name, sizeof (name), vl);
646 ERROR ("plugin_value_list_clone: Unable to determine "
647 "interval from context for "
648 "value list \"%s\". "
649 "This indicates a broken plugin. "
650 "Please report this problem to the "
651 "collectd mailing list or at "
652 "<http://collectd.org/bugs/>.", name);
653 vl->interval = cf_get_default_interval ();
658 } /* }}} value_list_t *plugin_value_list_clone */
660 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
664 q = malloc (sizeof (*q));
669 q->vl = plugin_value_list_clone (vl);
676 /* Store context of caller (read plugin); otherwise, it would not be
677 * available to the write plugins when actually dispatching the
678 * value-list later on. */
679 q->ctx = plugin_get_ctx ();
681 pthread_mutex_lock (&write_lock);
683 if (write_queue_tail == NULL)
685 write_queue_head = q;
686 write_queue_tail = q;
687 write_queue_length = 1;
691 write_queue_tail->next = q;
692 write_queue_tail = q;
693 write_queue_length += 1;
696 pthread_cond_signal (&write_cond);
697 pthread_mutex_unlock (&write_lock);
700 } /* }}} int plugin_write_enqueue */
702 static value_list_t *plugin_write_dequeue (void) /* {{{ */
707 pthread_mutex_lock (&write_lock);
709 while (write_loop && (write_queue_head == NULL))
710 pthread_cond_wait (&write_cond, &write_lock);
712 if (write_queue_head == NULL)
714 pthread_mutex_unlock (&write_lock);
718 q = write_queue_head;
719 write_queue_head = q->next;
720 write_queue_length -= 1;
721 if (write_queue_head == NULL) {
722 write_queue_tail = NULL;
723 assert(0 == write_queue_length);
726 pthread_mutex_unlock (&write_lock);
728 (void) plugin_set_ctx (q->ctx);
733 } /* }}} value_list_t *plugin_write_dequeue */
735 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
739 value_list_t *vl = plugin_write_dequeue ();
743 plugin_dispatch_values_internal (vl);
745 plugin_value_list_free (vl);
750 } /* }}} void *plugin_write_thread */
752 static void start_write_threads (size_t num) /* {{{ */
756 if (write_threads != NULL)
759 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
760 if (write_threads == NULL)
762 ERROR ("plugin: start_write_threads: calloc failed.");
766 write_threads_num = 0;
767 for (i = 0; i < num; i++)
771 status = pthread_create (write_threads + write_threads_num,
778 ERROR ("plugin: start_write_threads: pthread_create failed "
779 "with status %i (%s).", status,
780 sstrerror (status, errbuf, sizeof (errbuf)));
786 } /* }}} void start_write_threads */
788 static void stop_write_threads (void) /* {{{ */
793 if (write_threads == NULL)
796 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
798 pthread_mutex_lock (&write_lock);
800 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
801 pthread_cond_broadcast (&write_cond);
802 pthread_mutex_unlock (&write_lock);
804 for (i = 0; i < write_threads_num; i++)
806 if (pthread_join (write_threads[i], NULL) != 0)
808 ERROR ("plugin: stop_write_threads: pthread_join failed.");
810 write_threads[i] = (pthread_t) 0;
812 sfree (write_threads);
813 write_threads_num = 0;
815 pthread_mutex_lock (&write_lock);
817 for (q = write_queue_head; q != NULL; )
819 write_queue_t *q1 = q;
820 plugin_value_list_free (q->vl);
825 write_queue_head = NULL;
826 write_queue_tail = NULL;
827 write_queue_length = 0;
828 pthread_mutex_unlock (&write_lock);
832 WARNING ("plugin: %i value list%s left after shutting down "
833 "the write threads.",
834 i, (i == 1) ? " was" : "s were");
836 } /* }}} void stop_write_threads */
841 void plugin_set_dir (const char *dir)
843 if (plugindir != NULL)
848 else if ((plugindir = strdup (dir)) == NULL)
851 ERROR ("strdup failed: %s",
852 sstrerror (errno, errbuf, sizeof (errbuf)));
856 static _Bool plugin_is_loaded (char const *name)
860 if (plugins_loaded == NULL)
861 plugins_loaded = c_avl_create ((void *) strcasecmp);
862 assert (plugins_loaded != NULL);
864 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
865 return (status == 0);
868 static int plugin_mark_loaded (char const *name)
873 name_copy = strdup (name);
874 if (name_copy == NULL)
877 status = c_avl_insert (plugins_loaded,
878 /* key = */ name_copy, /* value = */ NULL);
882 static void plugin_free_loaded ()
887 if (plugins_loaded == NULL)
890 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
893 assert (value == NULL);
896 c_avl_destroy (plugins_loaded);
897 plugins_loaded = NULL;
901 int plugin_load (char const *plugin_name, uint32_t flags)
905 char filename[BUFSIZE] = "";
906 char typename[BUFSIZE];
913 if (plugin_name == NULL)
916 /* Check if plugin is already loaded and don't do anything in this
918 if (plugin_is_loaded (plugin_name))
921 dir = plugin_get_dir ();
925 * XXX: Magic at work:
927 * Some of the language bindings, for example the Python and Perl
928 * plugins, need to be able to export symbols to the scripts they run.
929 * For this to happen, the "Globals" flag needs to be set.
930 * Unfortunately, this technical detail is hard to explain to the
931 * average user and she shouldn't have to worry about this, ideally.
932 * So in order to save everyone's sanity use a different default for a
933 * handful of special plugins. --octo
935 if ((strcasecmp ("perl", plugin_name) == 0)
936 || (strcasecmp ("python", plugin_name) == 0))
937 flags |= PLUGIN_FLAGS_GLOBAL;
939 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
940 * type when matching the filename */
941 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
942 if ((status < 0) || ((size_t) status >= sizeof (typename)))
944 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
947 typename_len = strlen (typename);
949 if ((dh = opendir (dir)) == NULL)
952 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
953 sstrerror (errno, errbuf, sizeof (errbuf)));
957 while ((de = readdir (dh)) != NULL)
959 if (strncasecmp (de->d_name, typename, typename_len))
962 status = ssnprintf (filename, sizeof (filename),
963 "%s/%s", dir, de->d_name);
964 if ((status < 0) || ((size_t) status >= sizeof (filename)))
966 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
971 if (lstat (filename, &statbuf) == -1)
974 WARNING ("plugin_load: stat (\"%s\") failed: %s",
976 sstrerror (errno, errbuf, sizeof (errbuf)));
979 else if (!S_ISREG (statbuf.st_mode))
981 /* don't follow symlinks */
982 WARNING ("plugin_load: %s is not a regular file.",
987 status = plugin_load_file (filename, flags);
991 plugin_mark_loaded (plugin_name);
997 ERROR ("plugin_load: Load plugin \"%s\" failed with "
998 "status %i.", plugin_name, status);
1004 if (filename[0] == 0)
1005 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1012 * The `register_*' functions follow
1014 int plugin_register_config (const char *name,
1015 int (*callback) (const char *key, const char *val),
1016 const char **keys, int keys_num)
1018 cf_register (name, callback, keys, keys_num);
1020 } /* int plugin_register_config */
1022 int plugin_register_complex_config (const char *type,
1023 int (*callback) (oconfig_item_t *))
1025 return (cf_register_complex (type, callback));
1026 } /* int plugin_register_complex_config */
1028 int plugin_register_init (const char *name,
1029 int (*callback) (void))
1031 return (create_register_callback (&list_init, name, (void *) callback,
1032 /* user_data = */ NULL));
1033 } /* plugin_register_init */
1035 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1037 const read_func_t *rf0;
1038 const read_func_t *rf1;
1043 if (rf0->rf_next_read < rf1->rf_next_read)
1045 else if (rf0->rf_next_read > rf1->rf_next_read)
1049 } /* int plugin_compare_read_func */
1051 /* Add a read function to both, the heap and a linked list. The linked list if
1052 * used to look-up read functions, especially for the remove function. The heap
1053 * is used to determine which plugin to read next. */
1054 static int plugin_insert_read (read_func_t *rf)
1059 rf->rf_next_read = cdtime ();
1060 rf->rf_effective_interval = rf->rf_interval;
1062 pthread_mutex_lock (&read_lock);
1064 if (read_list == NULL)
1066 read_list = llist_create ();
1067 if (read_list == NULL)
1069 pthread_mutex_unlock (&read_lock);
1070 ERROR ("plugin_insert_read: read_list failed.");
1075 if (read_heap == NULL)
1077 read_heap = c_heap_create (plugin_compare_read_func);
1078 if (read_heap == NULL)
1080 pthread_mutex_unlock (&read_lock);
1081 ERROR ("plugin_insert_read: c_heap_create failed.");
1086 le = llist_search (read_list, rf->rf_name);
1089 pthread_mutex_unlock (&read_lock);
1090 WARNING ("The read function \"%s\" is already registered. "
1091 "Check for duplicate \"LoadPlugin\" lines "
1092 "in your configuration!",
1097 le = llentry_create (rf->rf_name, rf);
1100 pthread_mutex_unlock (&read_lock);
1101 ERROR ("plugin_insert_read: llentry_create failed.");
1105 status = c_heap_insert (read_heap, rf);
1108 pthread_mutex_unlock (&read_lock);
1109 ERROR ("plugin_insert_read: c_heap_insert failed.");
1110 llentry_destroy (le);
1114 /* This does not fail. */
1115 llist_append (read_list, le);
1117 /* Wake up all the read threads. */
1118 pthread_cond_broadcast (&read_cond);
1119 pthread_mutex_unlock (&read_lock);
1121 } /* int plugin_insert_read */
1123 int plugin_register_read (const char *name,
1124 int (*callback) (void))
1129 rf = malloc (sizeof (*rf));
1132 ERROR ("plugin_register_read: malloc failed.");
1136 memset (rf, 0, sizeof (read_func_t));
1137 rf->rf_callback = (void *) callback;
1138 rf->rf_udata.data = NULL;
1139 rf->rf_udata.free_func = NULL;
1140 rf->rf_ctx = plugin_get_ctx ();
1141 rf->rf_group[0] = '\0';
1142 rf->rf_name = strdup (name);
1143 rf->rf_type = RF_SIMPLE;
1144 rf->rf_interval = plugin_get_interval ();
1146 status = plugin_insert_read (rf);
1151 } /* int plugin_register_read */
1153 int plugin_register_complex_read (const char *group, const char *name,
1154 plugin_read_cb callback,
1155 const struct timespec *interval,
1156 user_data_t *user_data)
1161 rf = malloc (sizeof (*rf));
1164 ERROR ("plugin_register_complex_read: malloc failed.");
1168 memset (rf, 0, sizeof (read_func_t));
1169 rf->rf_callback = (void *) callback;
1171 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1173 rf->rf_group[0] = '\0';
1174 rf->rf_name = strdup (name);
1175 rf->rf_type = RF_COMPLEX;
1176 if (interval != NULL)
1177 rf->rf_interval = TIMESPEC_TO_CDTIME_T (interval);
1179 rf->rf_interval = plugin_get_interval ();
1182 if (user_data == NULL)
1184 rf->rf_udata.data = NULL;
1185 rf->rf_udata.free_func = NULL;
1189 rf->rf_udata = *user_data;
1192 rf->rf_ctx = plugin_get_ctx ();
1194 status = plugin_insert_read (rf);
1199 } /* int plugin_register_complex_read */
1201 int plugin_register_write (const char *name,
1202 plugin_write_cb callback, user_data_t *ud)
1204 return (create_register_callback (&list_write, name,
1205 (void *) callback, ud));
1206 } /* int plugin_register_write */
1208 int plugin_register_flush (const char *name,
1209 plugin_flush_cb callback, user_data_t *ud)
1211 return (create_register_callback (&list_flush, name,
1212 (void *) callback, ud));
1213 } /* int plugin_register_flush */
1215 int plugin_register_missing (const char *name,
1216 plugin_missing_cb callback, user_data_t *ud)
1218 return (create_register_callback (&list_missing, name,
1219 (void *) callback, ud));
1220 } /* int plugin_register_missing */
1222 int plugin_register_shutdown (const char *name,
1223 int (*callback) (void))
1225 return (create_register_callback (&list_shutdown, name,
1226 (void *) callback, /* user_data = */ NULL));
1227 } /* int plugin_register_shutdown */
1229 static void plugin_free_data_sets (void)
1234 if (data_sets == NULL)
1237 while (c_avl_pick (data_sets, &key, &value) == 0)
1239 data_set_t *ds = value;
1240 /* key is a pointer to ds->type */
1246 c_avl_destroy (data_sets);
1248 } /* void plugin_free_data_sets */
1250 int plugin_register_data_set (const data_set_t *ds)
1252 data_set_t *ds_copy;
1255 if ((data_sets != NULL)
1256 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1258 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1259 plugin_unregister_data_set (ds->type);
1261 else if (data_sets == NULL)
1263 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1264 if (data_sets == NULL)
1268 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1269 if (ds_copy == NULL)
1271 memcpy(ds_copy, ds, sizeof (data_set_t));
1273 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1275 if (ds_copy->ds == NULL)
1281 for (i = 0; i < ds->ds_num; i++)
1282 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1284 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1285 } /* int plugin_register_data_set */
1287 int plugin_register_log (const char *name,
1288 plugin_log_cb callback, user_data_t *ud)
1290 return (create_register_callback (&list_log, name,
1291 (void *) callback, ud));
1292 } /* int plugin_register_log */
1294 int plugin_register_notification (const char *name,
1295 plugin_notification_cb callback, user_data_t *ud)
1297 return (create_register_callback (&list_notification, name,
1298 (void *) callback, ud));
1299 } /* int plugin_register_log */
1301 int plugin_unregister_config (const char *name)
1303 cf_unregister (name);
1305 } /* int plugin_unregister_config */
1307 int plugin_unregister_complex_config (const char *name)
1309 cf_unregister_complex (name);
1311 } /* int plugin_unregister_complex_config */
1313 int plugin_unregister_init (const char *name)
1315 return (plugin_unregister (list_init, name));
1318 int plugin_unregister_read (const char *name) /* {{{ */
1326 pthread_mutex_lock (&read_lock);
1328 if (read_list == NULL)
1330 pthread_mutex_unlock (&read_lock);
1334 le = llist_search (read_list, name);
1337 pthread_mutex_unlock (&read_lock);
1338 WARNING ("plugin_unregister_read: No such read function: %s",
1343 llist_remove (read_list, le);
1346 assert (rf != NULL);
1347 rf->rf_type = RF_REMOVE;
1349 pthread_mutex_unlock (&read_lock);
1351 llentry_destroy (le);
1353 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1356 } /* }}} int plugin_unregister_read */
1358 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1360 read_func_t *rf = e->value;
1363 return strcmp (rf->rf_group, (const char *)group);
1364 } /* }}} int compare_read_func_group */
1366 int plugin_unregister_read_group (const char *group) /* {{{ */
1376 pthread_mutex_lock (&read_lock);
1378 if (read_list == NULL)
1380 pthread_mutex_unlock (&read_lock);
1386 le = llist_search_custom (read_list,
1387 compare_read_func_group, (void *)group);
1394 llist_remove (read_list, le);
1397 assert (rf != NULL);
1398 rf->rf_type = RF_REMOVE;
1400 llentry_destroy (le);
1402 DEBUG ("plugin_unregister_read_group: "
1403 "Marked `%s' (group `%s') for removal.",
1404 rf->rf_name, group);
1407 pthread_mutex_unlock (&read_lock);
1411 WARNING ("plugin_unregister_read_group: No such "
1412 "group of read function: %s", group);
1417 } /* }}} int plugin_unregister_read_group */
1419 int plugin_unregister_write (const char *name)
1421 return (plugin_unregister (list_write, name));
1424 int plugin_unregister_flush (const char *name)
1426 return (plugin_unregister (list_flush, name));
1429 int plugin_unregister_missing (const char *name)
1431 return (plugin_unregister (list_missing, name));
1434 int plugin_unregister_shutdown (const char *name)
1436 return (plugin_unregister (list_shutdown, name));
1439 int plugin_unregister_data_set (const char *name)
1443 if (data_sets == NULL)
1446 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1453 } /* int plugin_unregister_data_set */
1455 int plugin_unregister_log (const char *name)
1457 return (plugin_unregister (list_log, name));
1460 int plugin_unregister_notification (const char *name)
1462 return (plugin_unregister (list_notification, name));
1465 void plugin_init_all (void)
1467 char const *chain_name;
1468 long write_threads_num;
1472 /* Init the value cache */
1475 chain_name = global_option_get ("PreCacheChain");
1476 pre_cache_chain = fc_chain_get_by_name (chain_name);
1478 chain_name = global_option_get ("PostCacheChain");
1479 post_cache_chain = fc_chain_get_by_name (chain_name);
1481 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1483 if (write_limit_high < 0)
1485 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1486 write_limit_high = 0;
1489 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1490 /* default = */ write_limit_high / 2);
1491 if (write_limit_low < 0)
1493 ERROR ("WriteQueueLimitLow must be positive or zero.");
1494 write_limit_low = write_limit_high / 2;
1496 else if (write_limit_low > write_limit_high)
1498 ERROR ("WriteQueueLimitLow must not be larger than "
1499 "WriteQueueLimitHigh.");
1500 write_limit_low = write_limit_high;
1503 write_threads_num = global_option_get_long ("WriteThreads",
1505 if (write_threads_num < 1)
1507 ERROR ("WriteThreads must be positive.");
1508 write_threads_num = 5;
1511 start_write_threads ((size_t) write_threads_num);
1513 if ((list_init == NULL) && (read_heap == NULL))
1516 /* Calling all init callbacks before checking if read callbacks
1517 * are available allows the init callbacks to register the read
1519 le = llist_head (list_init);
1522 callback_func_t *cf;
1523 plugin_init_cb callback;
1524 plugin_ctx_t old_ctx;
1527 old_ctx = plugin_set_ctx (cf->cf_ctx);
1528 callback = cf->cf_callback;
1529 status = (*callback) ();
1530 plugin_set_ctx (old_ctx);
1534 ERROR ("Initialization of plugin `%s' "
1535 "failed with status %i. "
1536 "Plugin will be unloaded.",
1538 /* Plugins that register read callbacks from the init
1539 * callback should take care of appropriate error
1540 * handling themselves. */
1541 /* FIXME: Unload _all_ functions */
1542 plugin_unregister_read (le->key);
1548 max_read_interval = global_option_get_time ("MaxReadInterval",
1549 DEFAULT_MAX_READ_INTERVAL);
1551 /* Start read-threads */
1552 if (read_heap != NULL)
1557 rt = global_option_get ("ReadThreads");
1560 start_read_threads ((num > 0) ? num : 5);
1562 } /* void plugin_init_all */
1564 /* TODO: Rename this function. */
1565 void plugin_read_all (void)
1567 uc_check_timeout ();
1570 } /* void plugin_read_all */
1572 /* Read function called when the `-T' command line argument is given. */
1573 int plugin_read_all_once (void)
1576 int return_status = 0;
1578 if (read_heap == NULL)
1580 NOTICE ("No read-functions are registered.");
1587 plugin_ctx_t old_ctx;
1589 rf = c_heap_get_root (read_heap);
1593 old_ctx = plugin_set_ctx (rf->rf_ctx);
1595 if (rf->rf_type == RF_SIMPLE)
1597 int (*callback) (void);
1599 callback = rf->rf_callback;
1600 status = (*callback) ();
1604 plugin_read_cb callback;
1606 callback = rf->rf_callback;
1607 status = (*callback) (&rf->rf_udata);
1610 plugin_set_ctx (old_ctx);
1614 NOTICE ("read-function of plugin `%s' failed.",
1619 destroy_callback ((void *) rf);
1622 return (return_status);
1623 } /* int plugin_read_all_once */
1625 int plugin_write (const char *plugin, /* {{{ */
1626 const data_set_t *ds, const value_list_t *vl)
1634 if (list_write == NULL)
1639 ds = plugin_get_ds (vl->type);
1642 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1652 le = llist_head (list_write);
1655 callback_func_t *cf = le->value;
1656 plugin_write_cb callback;
1658 /* do not switch plugin context; rather keep the context (interval)
1659 * information of the calling read plugin */
1661 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1662 callback = cf->cf_callback;
1663 status = (*callback) (ds, vl, &cf->cf_udata);
1672 if ((success == 0) && (failure != 0))
1677 else /* plugin != NULL */
1679 callback_func_t *cf;
1680 plugin_write_cb callback;
1682 le = llist_head (list_write);
1685 if (strcasecmp (plugin, le->key) == 0)
1696 /* do not switch plugin context; rather keep the context (interval)
1697 * information of the calling read plugin */
1699 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1700 callback = cf->cf_callback;
1701 status = (*callback) (ds, vl, &cf->cf_udata);
1705 } /* }}} int plugin_write */
1707 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1711 if (list_flush == NULL)
1714 le = llist_head (list_flush);
1717 callback_func_t *cf;
1718 plugin_flush_cb callback;
1719 plugin_ctx_t old_ctx;
1721 if ((plugin != NULL)
1722 && (strcmp (plugin, le->key) != 0))
1729 old_ctx = plugin_set_ctx (cf->cf_ctx);
1730 callback = cf->cf_callback;
1732 (*callback) (timeout, identifier, &cf->cf_udata);
1734 plugin_set_ctx (old_ctx);
1739 } /* int plugin_flush */
1741 void plugin_shutdown_all (void)
1745 stop_read_threads ();
1747 destroy_all_callbacks (&list_init);
1749 pthread_mutex_lock (&read_lock);
1750 llist_destroy (read_list);
1752 pthread_mutex_unlock (&read_lock);
1754 destroy_read_heap ();
1756 plugin_flush (/* plugin = */ NULL,
1758 /* identifier = */ NULL);
1761 if (list_shutdown != NULL)
1762 le = llist_head (list_shutdown);
1766 callback_func_t *cf;
1767 plugin_shutdown_cb callback;
1768 plugin_ctx_t old_ctx;
1771 old_ctx = plugin_set_ctx (cf->cf_ctx);
1772 callback = cf->cf_callback;
1774 /* Advance the pointer before calling the callback allows
1775 * shutdown functions to unregister themselves. If done the
1776 * other way around the memory `le' points to will be freed
1777 * after callback returns. */
1782 plugin_set_ctx (old_ctx);
1785 stop_write_threads ();
1787 /* Write plugins which use the `user_data' pointer usually need the
1788 * same data available to the flush callback. If this is the case, set
1789 * the free_function to NULL when registering the flush callback and to
1790 * the real free function when registering the write callback. This way
1791 * the data isn't freed twice. */
1792 destroy_all_callbacks (&list_flush);
1793 destroy_all_callbacks (&list_missing);
1794 destroy_all_callbacks (&list_write);
1796 destroy_all_callbacks (&list_notification);
1797 destroy_all_callbacks (&list_shutdown);
1798 destroy_all_callbacks (&list_log);
1800 plugin_free_loaded ();
1801 plugin_free_data_sets ();
1802 } /* void plugin_shutdown_all */
1804 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1808 if (list_missing == NULL)
1811 le = llist_head (list_missing);
1814 callback_func_t *cf;
1815 plugin_missing_cb callback;
1816 plugin_ctx_t old_ctx;
1820 old_ctx = plugin_set_ctx (cf->cf_ctx);
1821 callback = cf->cf_callback;
1823 status = (*callback) (vl, &cf->cf_udata);
1824 plugin_set_ctx (old_ctx);
1829 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1830 "failed with status %i.",
1843 } /* int }}} plugin_dispatch_missing */
1845 static int plugin_dispatch_values_internal (value_list_t *vl)
1848 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1850 value_t *saved_values;
1851 int saved_values_len;
1855 int free_meta_data = 0;
1857 if ((vl == NULL) || (vl->type[0] == 0)
1858 || (vl->values == NULL) || (vl->values_len < 1))
1860 ERROR ("plugin_dispatch_values: Invalid value list "
1861 "from plugin %s.", vl->plugin);
1865 /* Free meta data only if the calling function didn't specify any. In
1866 * this case matches and targets may add some and the calling function
1867 * may not expect (and therefore free) that data. */
1868 if (vl->meta == NULL)
1871 if (list_write == NULL)
1872 c_complain_once (LOG_WARNING, &no_write_complaint,
1873 "plugin_dispatch_values: No write callback has been "
1874 "registered. Please load at least one output plugin, "
1875 "if you want the collected data to be stored.");
1877 if (data_sets == NULL)
1879 ERROR ("plugin_dispatch_values: No data sets registered. "
1880 "Could the types database be read? Check "
1881 "your `TypesDB' setting!");
1885 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1887 char ident[6 * DATA_MAX_NAME_LEN];
1889 FORMAT_VL (ident, sizeof (ident), vl);
1890 INFO ("plugin_dispatch_values: Dataset not found: %s "
1891 "(from \"%s\"), check your types.db!",
1896 /* Assured by plugin_value_list_clone(). The time is determined at
1897 * _enqueue_ time. */
1898 assert (vl->time != 0);
1899 assert (vl->interval != 0);
1901 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1903 "plugin = %s; plugin_instance = %s; "
1904 "type = %s; type_instance = %s;",
1905 CDTIME_T_TO_DOUBLE (vl->time),
1906 CDTIME_T_TO_DOUBLE (vl->interval),
1908 vl->plugin, vl->plugin_instance,
1909 vl->type, vl->type_instance);
1912 assert (0 == strcmp (ds->type, vl->type));
1914 if (0 != strcmp (ds->type, vl->type))
1915 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1916 ds->type, vl->type);
1920 assert (ds->ds_num == vl->values_len);
1922 if (ds->ds_num != vl->values_len)
1924 ERROR ("plugin_dispatch_values: ds->type = %s: "
1925 "(ds->ds_num = %i) != "
1926 "(vl->values_len = %i)",
1927 ds->type, ds->ds_num, vl->values_len);
1932 escape_slashes (vl->host, sizeof (vl->host));
1933 escape_slashes (vl->plugin, sizeof (vl->plugin));
1934 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1935 escape_slashes (vl->type, sizeof (vl->type));
1936 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1938 /* Copy the values. This way, we can assure `targets' that they get
1939 * dynamically allocated values, which they can free and replace if
1941 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1943 saved_values = vl->values;
1944 saved_values_len = vl->values_len;
1946 vl->values = (value_t *) calloc (vl->values_len,
1947 sizeof (*vl->values));
1948 if (vl->values == NULL)
1950 ERROR ("plugin_dispatch_values: calloc failed.");
1951 vl->values = saved_values;
1954 memcpy (vl->values, saved_values,
1955 vl->values_len * sizeof (*vl->values));
1957 else /* if ((pre == NULL) && (post == NULL)) */
1959 saved_values = NULL;
1960 saved_values_len = 0;
1963 if (pre_cache_chain != NULL)
1965 status = fc_process_chain (ds, vl, pre_cache_chain);
1968 WARNING ("plugin_dispatch_values: Running the "
1969 "pre-cache chain failed with "
1973 else if (status == FC_TARGET_STOP)
1975 /* Restore the state of the value_list so that plugins
1976 * don't get confused.. */
1977 if (saved_values != NULL)
1980 vl->values = saved_values;
1981 vl->values_len = saved_values_len;
1987 /* Update the value cache */
1990 if (post_cache_chain != NULL)
1992 status = fc_process_chain (ds, vl, post_cache_chain);
1995 WARNING ("plugin_dispatch_values: Running the "
1996 "post-cache chain failed with "
2002 fc_default_action (ds, vl);
2004 /* Restore the state of the value_list so that plugins don't get
2006 if (saved_values != NULL)
2009 vl->values = saved_values;
2010 vl->values_len = saved_values_len;
2013 if ((free_meta_data != 0) && (vl->meta != NULL))
2015 meta_data_destroy (vl->meta);
2020 } /* int plugin_dispatch_values_internal */
2022 static double get_drop_probability (void) /* {{{ */
2028 pthread_mutex_lock (&write_lock);
2029 wql = write_queue_length;
2030 pthread_mutex_unlock (&write_lock);
2032 if (wql < write_limit_low)
2034 if (wql >= write_limit_high)
2037 pos = 1 + wql - write_limit_low;
2038 size = 1 + write_limit_high - write_limit_low;
2040 return (((double) pos) / ((double) size));
2041 } /* }}} double get_drop_probability */
2043 static _Bool check_drop_value (void) /* {{{ */
2045 static cdtime_t last_message_time = 0;
2046 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2052 if (write_limit_high == 0)
2055 p = get_drop_probability ();
2059 status = pthread_mutex_trylock (&last_message_lock);
2065 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2067 last_message_time = now;
2068 ERROR ("plugin_dispatch_values: Low water mark "
2069 "reached. Dropping %.0f%% of metrics.",
2072 pthread_mutex_unlock (&last_message_lock);
2083 } /* }}} _Bool check_drop_value */
2085 int plugin_dispatch_values (value_list_t const *vl)
2089 if (check_drop_value ())
2092 status = plugin_write_enqueue (vl);
2096 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2097 "with status %i (%s).", status,
2098 sstrerror (status, errbuf, sizeof (errbuf)));
2105 __attribute__((sentinel))
2106 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2107 _Bool store_percentage, ...)
2114 assert (template->values_len == 1);
2116 va_start (ap, store_percentage);
2122 name = va_arg (ap, char const *);
2126 value = va_arg (ap, gauge_t);
2132 vl = plugin_value_list_clone (template);
2133 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2134 if (store_percentage)
2135 sstrncpy (vl->type, "percent", sizeof (vl->type));
2137 va_start (ap, store_percentage);
2143 /* Set the type instance. */
2144 name = va_arg (ap, char const *);
2147 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2149 /* Set the value. */
2150 vl->values[0].gauge = va_arg (ap, gauge_t);
2151 if (store_percentage)
2152 vl->values[0].gauge *= 100.0 / sum;
2154 status = plugin_write_enqueue (vl);
2160 plugin_value_list_free (vl);
2162 } /* }}} int plugin_dispatch_multivalue */
2164 int plugin_dispatch_notification (const notification_t *notif)
2167 /* Possible TODO: Add flap detection here */
2169 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2170 "time = %.3f; host = %s;",
2171 notif->severity, notif->message,
2172 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2174 /* Nobody cares for notifications */
2175 if (list_notification == NULL)
2178 le = llist_head (list_notification);
2181 callback_func_t *cf;
2182 plugin_notification_cb callback;
2185 /* do not switch plugin context; rather keep the context
2186 * (interval) information of the calling plugin */
2189 callback = cf->cf_callback;
2190 status = (*callback) (notif, &cf->cf_udata);
2193 WARNING ("plugin_dispatch_notification: Notification "
2194 "callback %s returned %i.",
2202 } /* int plugin_dispatch_notification */
2204 void plugin_log (int level, const char *format, ...)
2211 if (level >= LOG_DEBUG)
2215 va_start (ap, format);
2216 vsnprintf (msg, sizeof (msg), format, ap);
2217 msg[sizeof (msg) - 1] = '\0';
2220 if (list_log == NULL)
2222 fprintf (stderr, "%s\n", msg);
2226 le = llist_head (list_log);
2229 callback_func_t *cf;
2230 plugin_log_cb callback;
2233 callback = cf->cf_callback;
2235 /* do not switch plugin context; rather keep the context
2236 * (interval) information of the calling plugin */
2238 (*callback) (level, msg, &cf->cf_udata);
2242 } /* void plugin_log */
2244 int parse_log_severity (const char *severity)
2248 if ((0 == strcasecmp (severity, "emerg"))
2249 || (0 == strcasecmp (severity, "alert"))
2250 || (0 == strcasecmp (severity, "crit"))
2251 || (0 == strcasecmp (severity, "err")))
2252 log_level = LOG_ERR;
2253 else if (0 == strcasecmp (severity, "warning"))
2254 log_level = LOG_WARNING;
2255 else if (0 == strcasecmp (severity, "notice"))
2256 log_level = LOG_NOTICE;
2257 else if (0 == strcasecmp (severity, "info"))
2258 log_level = LOG_INFO;
2260 else if (0 == strcasecmp (severity, "debug"))
2261 log_level = LOG_DEBUG;
2262 #endif /* COLLECT_DEBUG */
2265 } /* int parse_log_severity */
2267 int parse_notif_severity (const char *severity)
2269 int notif_severity = -1;
2271 if (strcasecmp (severity, "FAILURE") == 0)
2272 notif_severity = NOTIF_FAILURE;
2273 else if (strcmp (severity, "OKAY") == 0)
2274 notif_severity = NOTIF_OKAY;
2275 else if ((strcmp (severity, "WARNING") == 0)
2276 || (strcmp (severity, "WARN") == 0))
2277 notif_severity = NOTIF_WARNING;
2279 return (notif_severity);
2280 } /* int parse_notif_severity */
2282 const data_set_t *plugin_get_ds (const char *name)
2286 if (data_sets == NULL)
2288 ERROR ("plugin_get_ds: No data sets are defined yet.");
2292 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2294 DEBUG ("No such dataset registered: %s", name);
2299 } /* data_set_t *plugin_get_ds */
2301 static int plugin_notification_meta_add (notification_t *n,
2303 enum notification_meta_type_e type,
2306 notification_meta_t *meta;
2307 notification_meta_t *tail;
2309 if ((n == NULL) || (name == NULL) || (value == NULL))
2311 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2315 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2318 ERROR ("plugin_notification_meta_add: malloc failed.");
2321 memset (meta, 0, sizeof (notification_meta_t));
2323 sstrncpy (meta->name, name, sizeof (meta->name));
2328 case NM_TYPE_STRING:
2330 meta->nm_value.nm_string = strdup ((const char *) value);
2331 if (meta->nm_value.nm_string == NULL)
2333 ERROR ("plugin_notification_meta_add: strdup failed.");
2339 case NM_TYPE_SIGNED_INT:
2341 meta->nm_value.nm_signed_int = *((int64_t *) value);
2344 case NM_TYPE_UNSIGNED_INT:
2346 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2349 case NM_TYPE_DOUBLE:
2351 meta->nm_value.nm_double = *((double *) value);
2354 case NM_TYPE_BOOLEAN:
2356 meta->nm_value.nm_boolean = *((_Bool *) value);
2361 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2365 } /* switch (type) */
2369 while ((tail != NULL) && (tail->next != NULL))
2378 } /* int plugin_notification_meta_add */
2380 int plugin_notification_meta_add_string (notification_t *n,
2384 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2387 int plugin_notification_meta_add_signed_int (notification_t *n,
2391 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2394 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2398 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2401 int plugin_notification_meta_add_double (notification_t *n,
2405 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2408 int plugin_notification_meta_add_boolean (notification_t *n,
2412 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2415 int plugin_notification_meta_copy (notification_t *dst,
2416 const notification_t *src)
2418 notification_meta_t *meta;
2420 assert (dst != NULL);
2421 assert (src != NULL);
2422 assert (dst != src);
2423 assert ((src->meta == NULL) || (src->meta != dst->meta));
2425 for (meta = src->meta; meta != NULL; meta = meta->next)
2427 if (meta->type == NM_TYPE_STRING)
2428 plugin_notification_meta_add_string (dst, meta->name,
2429 meta->nm_value.nm_string);
2430 else if (meta->type == NM_TYPE_SIGNED_INT)
2431 plugin_notification_meta_add_signed_int (dst, meta->name,
2432 meta->nm_value.nm_signed_int);
2433 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2434 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2435 meta->nm_value.nm_unsigned_int);
2436 else if (meta->type == NM_TYPE_DOUBLE)
2437 plugin_notification_meta_add_double (dst, meta->name,
2438 meta->nm_value.nm_double);
2439 else if (meta->type == NM_TYPE_BOOLEAN)
2440 plugin_notification_meta_add_boolean (dst, meta->name,
2441 meta->nm_value.nm_boolean);
2445 } /* int plugin_notification_meta_copy */
2447 int plugin_notification_meta_free (notification_meta_t *n)
2449 notification_meta_t *this;
2450 notification_meta_t *next;
2454 ERROR ("plugin_notification_meta_free: n == NULL!");
2459 while (this != NULL)
2463 if (this->type == NM_TYPE_STRING)
2465 free ((char *)this->nm_value.nm_string);
2466 this->nm_value.nm_string = NULL;
2474 } /* int plugin_notification_meta_free */
2476 static void plugin_ctx_destructor (void *ctx)
2479 } /* void plugin_ctx_destructor */
2481 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2483 static plugin_ctx_t *plugin_ctx_create (void)
2487 ctx = malloc (sizeof (*ctx));
2490 ERROR ("Failed to allocate plugin context: %s",
2491 sstrerror (errno, errbuf, sizeof (errbuf)));
2496 assert (plugin_ctx_key_initialized);
2497 pthread_setspecific (plugin_ctx_key, ctx);
2498 DEBUG("Created new plugin context.");
2500 } /* int plugin_ctx_create */
2502 void plugin_init_ctx (void)
2504 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2505 plugin_ctx_key_initialized = 1;
2506 } /* void plugin_init_ctx */
2508 plugin_ctx_t plugin_get_ctx (void)
2512 assert (plugin_ctx_key_initialized);
2513 ctx = pthread_getspecific (plugin_ctx_key);
2516 ctx = plugin_ctx_create ();
2517 /* this must no happen -- exit() instead? */
2523 } /* plugin_ctx_t plugin_get_ctx */
2525 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2530 assert (plugin_ctx_key_initialized);
2531 c = pthread_getspecific (plugin_ctx_key);
2534 c = plugin_ctx_create ();
2535 /* this must no happen -- exit() instead? */
2544 } /* void plugin_set_ctx */
2546 cdtime_t plugin_get_interval (void)
2550 interval = plugin_get_ctx().interval;
2554 return cf_get_default_interval ();
2555 } /* cdtime_t plugin_get_interval */
2559 void *(*start_routine) (void *);
2563 static void *plugin_thread_start (void *arg)
2565 plugin_thread_t *plugin_thread = arg;
2567 void *(*start_routine) (void *) = plugin_thread->start_routine;
2568 void *plugin_arg = plugin_thread->arg;
2570 plugin_set_ctx (plugin_thread->ctx);
2572 free (plugin_thread);
2574 return start_routine (plugin_arg);
2575 } /* void *plugin_thread_start */
2577 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2578 void *(*start_routine) (void *), void *arg)
2580 plugin_thread_t *plugin_thread;
2582 plugin_thread = malloc (sizeof (*plugin_thread));
2583 if (plugin_thread == NULL)
2586 plugin_thread->ctx = plugin_get_ctx ();
2587 plugin_thread->start_routine = start_routine;
2588 plugin_thread->arg = arg;
2590 return pthread_create (thread, attr,
2591 plugin_thread_start, plugin_thread);
2592 } /* int plugin_thread_create */
2594 /* vim: set sw=8 ts=8 noet fdm=marker : */