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 #define DEFAULT_MAX_READ_INTERVAL 86400
108 static c_heap_t *read_heap = NULL;
109 static llist_t *read_list;
110 static int read_loop = 1;
111 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
112 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
113 static pthread_t *read_threads = NULL;
114 static int read_threads_num = 0;
115 static int max_read_interval = DEFAULT_MAX_READ_INTERVAL;
117 static write_queue_t *write_queue_head;
118 static write_queue_t *write_queue_tail;
119 static long write_queue_length = 0;
120 static _Bool write_loop = 1;
121 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
122 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
123 static pthread_t *write_threads = NULL;
124 static size_t write_threads_num = 0;
126 static pthread_key_t plugin_ctx_key;
127 static _Bool plugin_ctx_key_initialized = 0;
129 static long write_limit_high = 0;
130 static long write_limit_low = 0;
135 static int plugin_dispatch_values_internal (value_list_t *vl);
137 static const char *plugin_get_dir (void)
139 if (plugindir == NULL)
145 static void destroy_callback (callback_func_t *cf) /* {{{ */
150 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
152 cf->cf_udata.free_func (cf->cf_udata.data);
153 cf->cf_udata.data = NULL;
154 cf->cf_udata.free_func = NULL;
157 } /* }}} void destroy_callback */
159 static void destroy_all_callbacks (llist_t **list) /* {{{ */
166 le = llist_head (*list);
174 destroy_callback (le->value);
180 llist_destroy (*list);
182 } /* }}} void destroy_all_callbacks */
184 static void destroy_read_heap (void) /* {{{ */
186 if (read_heap == NULL)
193 cf = c_heap_get_root (read_heap);
197 destroy_callback (cf);
200 c_heap_destroy (read_heap);
202 } /* }}} void destroy_read_heap */
204 static int register_callback (llist_t **list, /* {{{ */
205 const char *name, callback_func_t *cf)
212 *list = llist_create ();
215 ERROR ("plugin: register_callback: "
216 "llist_create failed.");
217 destroy_callback (cf);
225 ERROR ("plugin: register_callback: strdup failed.");
226 destroy_callback (cf);
230 le = llist_search (*list, name);
233 le = llentry_create (key, cf);
236 ERROR ("plugin: register_callback: "
237 "llentry_create failed.");
239 destroy_callback (cf);
243 llist_append (*list, le);
247 callback_func_t *old_cf;
252 WARNING ("plugin: register_callback: "
253 "a callback named `%s' already exists - "
254 "overwriting the old entry!", name);
256 destroy_callback (old_cf);
261 } /* }}} int register_callback */
263 static int create_register_callback (llist_t **list, /* {{{ */
264 const char *name, void *callback, user_data_t *ud)
268 cf = (callback_func_t *) malloc (sizeof (*cf));
271 ERROR ("plugin: create_register_callback: malloc failed.");
274 memset (cf, 0, sizeof (*cf));
276 cf->cf_callback = callback;
279 cf->cf_udata.data = NULL;
280 cf->cf_udata.free_func = NULL;
287 cf->cf_ctx = plugin_get_ctx ();
289 return (register_callback (list, name, cf));
290 } /* }}} int create_register_callback */
292 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
299 e = llist_search (list, name);
303 llist_remove (list, e);
306 destroy_callback (e->value);
311 } /* }}} int plugin_unregister */
314 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
315 * object, but it will bitch about a shared object not having a
316 * ``module_register'' symbol..
318 static int plugin_load_file (char *file, uint32_t flags)
321 void (*reg_handle) (void);
324 lt_dlerror (); /* clear errors */
326 #if LIBTOOL_VERSION == 2
327 if (flags & PLUGIN_FLAGS_GLOBAL) {
329 lt_dladvise_init(&advise);
330 lt_dladvise_global(&advise);
331 dlh = lt_dlopenadvise(file, advise);
332 lt_dladvise_destroy(&advise);
334 dlh = lt_dlopen (file);
336 #else /* if LIBTOOL_VERSION == 1 */
337 if (flags & PLUGIN_FLAGS_GLOBAL)
338 WARNING ("plugin_load_file: The global flag is not supported, "
339 "libtool 2 is required for this.");
340 dlh = lt_dlopen (file);
345 char errbuf[1024] = "";
347 ssnprintf (errbuf, sizeof (errbuf),
348 "lt_dlopen (\"%s\") failed: %s. "
349 "The most common cause for this problem are "
350 "missing dependencies. Use ldd(1) to check "
351 "the dependencies of the plugin "
353 file, lt_dlerror ());
355 ERROR ("%s", errbuf);
356 /* Make sure this is printed to STDERR in any case, but also
357 * make sure it's printed only once. */
358 if (list_log != NULL)
359 fprintf (stderr, "ERROR: %s\n", errbuf);
364 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
366 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
367 file, lt_dlerror ());
377 static void *plugin_read_thread (void __attribute__((unused)) *args)
379 while (read_loop != 0)
382 plugin_ctx_t old_ctx;
388 /* Get the read function that needs to be read next.
389 * We don't need to hold "read_lock" for the heap, but we need
390 * to call c_heap_get_root() and pthread_cond_wait() in the
391 * same protected block. */
392 pthread_mutex_lock (&read_lock);
393 rf = c_heap_get_root (read_heap);
396 pthread_cond_wait (&read_cond, &read_lock);
397 pthread_mutex_unlock (&read_lock);
400 pthread_mutex_unlock (&read_lock);
402 if (rf->rf_interval == 0)
404 /* this should not happen, because the interval is set
405 * for each plugin when loading it
406 * XXX: issue a warning? */
407 rf->rf_interval = plugin_get_interval ();
408 rf->rf_effective_interval = rf->rf_interval;
410 rf->rf_next_read = cdtime ();
413 /* sleep until this entry is due,
414 * using pthread_cond_timedwait */
415 pthread_mutex_lock (&read_lock);
416 /* In pthread_cond_timedwait, spurious wakeups are possible
417 * (and really happen, at least on NetBSD with > 1 CPU), thus
418 * we need to re-evaluate the condition every time
419 * pthread_cond_timedwait returns. */
421 while ((read_loop != 0)
422 && (cdtime () < rf->rf_next_read)
425 struct timespec ts = { 0 };
427 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
429 rc = pthread_cond_timedwait (&read_cond, &read_lock,
433 /* Must hold `read_lock' when accessing `rf->rf_type'. */
434 rf_type = rf->rf_type;
435 pthread_mutex_unlock (&read_lock);
437 /* Check if we're supposed to stop.. This may have interrupted
441 /* Insert `rf' again, so it can be free'd correctly */
442 c_heap_insert (read_heap, rf);
446 /* The entry has been marked for deletion. The linked list
447 * entry has already been removed by `plugin_unregister_read'.
448 * All we have to do here is free the `read_func_t' and
450 if (rf_type == RF_REMOVE)
452 DEBUG ("plugin_read_thread: Destroying the `%s' "
453 "callback.", rf->rf_name);
455 destroy_callback ((callback_func_t *) rf);
460 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
462 old_ctx = plugin_set_ctx (rf->rf_ctx);
464 if (rf_type == RF_SIMPLE)
466 int (*callback) (void);
468 callback = rf->rf_callback;
469 status = (*callback) ();
473 plugin_read_cb callback;
475 assert (rf_type == RF_COMPLEX);
477 callback = rf->rf_callback;
478 status = (*callback) (&rf->rf_udata);
481 plugin_set_ctx (old_ctx);
483 /* If the function signals failure, we will increase the
484 * intervals in which it will be called. */
487 rf->rf_effective_interval *= 2;
488 if (rf->rf_effective_interval > TIME_T_TO_CDTIME_T (max_read_interval))
489 rf->rf_effective_interval = TIME_T_TO_CDTIME_T (max_read_interval);
491 NOTICE ("read-function of plugin `%s' failed. "
492 "Will suspend it for %.3f seconds.",
494 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
498 /* Success: Restore the interval, if it was changed. */
499 rf->rf_effective_interval = rf->rf_interval;
502 /* update the ``next read due'' field */
505 DEBUG ("plugin_read_thread: Effective interval of the "
506 "%s plugin is %.3f seconds.",
508 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
510 /* Calculate the next (absolute) time at which this function
511 * should be called. */
512 rf->rf_next_read += rf->rf_effective_interval;
514 /* Check, if `rf_next_read' is in the past. */
515 if (rf->rf_next_read < now)
517 /* `rf_next_read' is in the past. Insert `now'
518 * so this value doesn't trail off into the
520 rf->rf_next_read = now;
523 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
525 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
527 /* Re-insert this read function into the heap again. */
528 c_heap_insert (read_heap, rf);
529 } /* while (read_loop) */
533 } /* void *plugin_read_thread */
535 static void start_read_threads (int num)
539 if (read_threads != NULL)
542 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
543 if (read_threads == NULL)
545 ERROR ("plugin: start_read_threads: calloc failed.");
549 read_threads_num = 0;
550 for (i = 0; i < num; i++)
552 if (pthread_create (read_threads + read_threads_num, NULL,
553 plugin_read_thread, NULL) == 0)
559 ERROR ("plugin: start_read_threads: pthread_create failed.");
563 } /* void start_read_threads */
565 static void stop_read_threads (void)
569 if (read_threads == NULL)
572 INFO ("collectd: Stopping %i read threads.", read_threads_num);
574 pthread_mutex_lock (&read_lock);
576 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
577 pthread_cond_broadcast (&read_cond);
578 pthread_mutex_unlock (&read_lock);
580 for (i = 0; i < read_threads_num; i++)
582 if (pthread_join (read_threads[i], NULL) != 0)
584 ERROR ("plugin: stop_read_threads: pthread_join failed.");
586 read_threads[i] = (pthread_t) 0;
588 sfree (read_threads);
589 read_threads_num = 0;
590 } /* void stop_read_threads */
592 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
597 meta_data_destroy (vl->meta);
600 } /* }}} void plugin_value_list_free */
602 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
609 vl = malloc (sizeof (*vl));
612 memcpy (vl, vl_orig, sizeof (*vl));
614 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
615 if (vl->values == NULL)
617 plugin_value_list_free (vl);
620 memcpy (vl->values, vl_orig->values,
621 vl_orig->values_len * sizeof (*vl->values));
623 vl->meta = meta_data_clone (vl->meta);
624 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
626 plugin_value_list_free (vl);
631 vl->time = cdtime ();
633 /* Fill in the interval from the thread context, if it is zero. */
634 if (vl->interval == 0)
636 plugin_ctx_t ctx = plugin_get_ctx ();
638 if (ctx.interval != 0)
639 vl->interval = ctx.interval;
642 char name[6 * DATA_MAX_NAME_LEN];
643 FORMAT_VL (name, sizeof (name), vl);
644 ERROR ("plugin_value_list_clone: Unable to determine "
645 "interval from context for "
646 "value list \"%s\". "
647 "This indicates a broken plugin. "
648 "Please report this problem to the "
649 "collectd mailing list or at "
650 "<http://collectd.org/bugs/>.", name);
651 vl->interval = cf_get_default_interval ();
656 } /* }}} value_list_t *plugin_value_list_clone */
658 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
662 q = malloc (sizeof (*q));
667 q->vl = plugin_value_list_clone (vl);
674 /* Store context of caller (read plugin); otherwise, it would not be
675 * available to the write plugins when actually dispatching the
676 * value-list later on. */
677 q->ctx = plugin_get_ctx ();
679 pthread_mutex_lock (&write_lock);
681 if (write_queue_tail == NULL)
683 write_queue_head = q;
684 write_queue_tail = q;
685 write_queue_length = 1;
689 write_queue_tail->next = q;
690 write_queue_tail = q;
691 write_queue_length += 1;
694 pthread_cond_signal (&write_cond);
695 pthread_mutex_unlock (&write_lock);
698 } /* }}} int plugin_write_enqueue */
700 static value_list_t *plugin_write_dequeue (void) /* {{{ */
705 pthread_mutex_lock (&write_lock);
707 while (write_loop && (write_queue_head == NULL))
708 pthread_cond_wait (&write_cond, &write_lock);
710 if (write_queue_head == NULL)
712 pthread_mutex_unlock (&write_lock);
716 q = write_queue_head;
717 write_queue_head = q->next;
718 write_queue_length -= 1;
719 if (write_queue_head == NULL) {
720 write_queue_tail = NULL;
721 assert(0 == write_queue_length);
724 pthread_mutex_unlock (&write_lock);
726 (void) plugin_set_ctx (q->ctx);
731 } /* }}} value_list_t *plugin_write_dequeue */
733 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
737 value_list_t *vl = plugin_write_dequeue ();
741 plugin_dispatch_values_internal (vl);
743 plugin_value_list_free (vl);
748 } /* }}} void *plugin_write_thread */
750 static void start_write_threads (size_t num) /* {{{ */
754 if (write_threads != NULL)
757 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
758 if (write_threads == NULL)
760 ERROR ("plugin: start_write_threads: calloc failed.");
764 write_threads_num = 0;
765 for (i = 0; i < num; i++)
769 status = pthread_create (write_threads + write_threads_num,
776 ERROR ("plugin: start_write_threads: pthread_create failed "
777 "with status %i (%s).", status,
778 sstrerror (status, errbuf, sizeof (errbuf)));
784 } /* }}} void start_write_threads */
786 static void stop_write_threads (void) /* {{{ */
791 if (write_threads == NULL)
794 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
796 pthread_mutex_lock (&write_lock);
798 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
799 pthread_cond_broadcast (&write_cond);
800 pthread_mutex_unlock (&write_lock);
802 for (i = 0; i < write_threads_num; i++)
804 if (pthread_join (write_threads[i], NULL) != 0)
806 ERROR ("plugin: stop_write_threads: pthread_join failed.");
808 write_threads[i] = (pthread_t) 0;
810 sfree (write_threads);
811 write_threads_num = 0;
813 pthread_mutex_lock (&write_lock);
815 for (q = write_queue_head; q != NULL; )
817 write_queue_t *q1 = q;
818 plugin_value_list_free (q->vl);
823 write_queue_head = NULL;
824 write_queue_tail = NULL;
825 write_queue_length = 0;
826 pthread_mutex_unlock (&write_lock);
830 WARNING ("plugin: %i value list%s left after shutting down "
831 "the write threads.",
832 i, (i == 1) ? " was" : "s were");
834 } /* }}} void stop_write_threads */
839 void plugin_set_dir (const char *dir)
841 if (plugindir != NULL)
846 else if ((plugindir = strdup (dir)) == NULL)
849 ERROR ("strdup failed: %s",
850 sstrerror (errno, errbuf, sizeof (errbuf)));
854 static _Bool plugin_is_loaded (char const *name)
858 if (plugins_loaded == NULL)
859 plugins_loaded = c_avl_create ((void *) strcasecmp);
860 assert (plugins_loaded != NULL);
862 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
863 return (status == 0);
866 static int plugin_mark_loaded (char const *name)
871 name_copy = strdup (name);
872 if (name_copy == NULL)
875 status = c_avl_insert (plugins_loaded,
876 /* key = */ name_copy, /* value = */ NULL);
880 static void plugin_free_loaded ()
885 if (plugins_loaded == NULL)
888 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
891 assert (value == NULL);
894 c_avl_destroy (plugins_loaded);
895 plugins_loaded = NULL;
899 int plugin_load (char const *plugin_name, uint32_t flags)
903 char filename[BUFSIZE] = "";
904 char typename[BUFSIZE];
911 if (plugin_name == NULL)
914 /* Check if plugin is already loaded and don't do anything in this
916 if (plugin_is_loaded (plugin_name))
919 dir = plugin_get_dir ();
923 * XXX: Magic at work:
925 * Some of the language bindings, for example the Python and Perl
926 * plugins, need to be able to export symbols to the scripts they run.
927 * For this to happen, the "Globals" flag needs to be set.
928 * Unfortunately, this technical detail is hard to explain to the
929 * average user and she shouldn't have to worry about this, ideally.
930 * So in order to save everyone's sanity use a different default for a
931 * handful of special plugins. --octo
933 if ((strcasecmp ("perl", plugin_name) == 0)
934 || (strcasecmp ("python", plugin_name) == 0))
935 flags |= PLUGIN_FLAGS_GLOBAL;
937 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
938 * type when matching the filename */
939 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
940 if ((status < 0) || ((size_t) status >= sizeof (typename)))
942 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
945 typename_len = strlen (typename);
947 if ((dh = opendir (dir)) == NULL)
950 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
951 sstrerror (errno, errbuf, sizeof (errbuf)));
955 while ((de = readdir (dh)) != NULL)
957 if (strncasecmp (de->d_name, typename, typename_len))
960 status = ssnprintf (filename, sizeof (filename),
961 "%s/%s", dir, de->d_name);
962 if ((status < 0) || ((size_t) status >= sizeof (filename)))
964 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
969 if (lstat (filename, &statbuf) == -1)
972 WARNING ("plugin_load: stat (\"%s\") failed: %s",
974 sstrerror (errno, errbuf, sizeof (errbuf)));
977 else if (!S_ISREG (statbuf.st_mode))
979 /* don't follow symlinks */
980 WARNING ("plugin_load: %s is not a regular file.",
985 status = plugin_load_file (filename, flags);
989 plugin_mark_loaded (plugin_name);
995 ERROR ("plugin_load: Load plugin \"%s\" failed with "
996 "status %i.", plugin_name, status);
1002 if (filename[0] == 0)
1003 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1010 * The `register_*' functions follow
1012 int plugin_register_config (const char *name,
1013 int (*callback) (const char *key, const char *val),
1014 const char **keys, int keys_num)
1016 cf_register (name, callback, keys, keys_num);
1018 } /* int plugin_register_config */
1020 int plugin_register_complex_config (const char *type,
1021 int (*callback) (oconfig_item_t *))
1023 return (cf_register_complex (type, callback));
1024 } /* int plugin_register_complex_config */
1026 int plugin_register_init (const char *name,
1027 int (*callback) (void))
1029 return (create_register_callback (&list_init, name, (void *) callback,
1030 /* user_data = */ NULL));
1031 } /* plugin_register_init */
1033 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1035 const read_func_t *rf0;
1036 const read_func_t *rf1;
1041 if (rf0->rf_next_read < rf1->rf_next_read)
1043 else if (rf0->rf_next_read > rf1->rf_next_read)
1047 } /* int plugin_compare_read_func */
1049 /* Add a read function to both, the heap and a linked list. The linked list if
1050 * used to look-up read functions, especially for the remove function. The heap
1051 * is used to determine which plugin to read next. */
1052 static int plugin_insert_read (read_func_t *rf)
1057 rf->rf_next_read = cdtime ();
1058 rf->rf_effective_interval = rf->rf_interval;
1060 pthread_mutex_lock (&read_lock);
1062 if (read_list == NULL)
1064 read_list = llist_create ();
1065 if (read_list == NULL)
1067 pthread_mutex_unlock (&read_lock);
1068 ERROR ("plugin_insert_read: read_list failed.");
1073 if (read_heap == NULL)
1075 read_heap = c_heap_create (plugin_compare_read_func);
1076 if (read_heap == NULL)
1078 pthread_mutex_unlock (&read_lock);
1079 ERROR ("plugin_insert_read: c_heap_create failed.");
1084 le = llist_search (read_list, rf->rf_name);
1087 pthread_mutex_unlock (&read_lock);
1088 WARNING ("The read function \"%s\" is already registered. "
1089 "Check for duplicate \"LoadPlugin\" lines "
1090 "in your configuration!",
1095 le = llentry_create (rf->rf_name, rf);
1098 pthread_mutex_unlock (&read_lock);
1099 ERROR ("plugin_insert_read: llentry_create failed.");
1103 status = c_heap_insert (read_heap, rf);
1106 pthread_mutex_unlock (&read_lock);
1107 ERROR ("plugin_insert_read: c_heap_insert failed.");
1108 llentry_destroy (le);
1112 /* This does not fail. */
1113 llist_append (read_list, le);
1115 /* Wake up all the read threads. */
1116 pthread_cond_broadcast (&read_cond);
1117 pthread_mutex_unlock (&read_lock);
1119 } /* int plugin_insert_read */
1121 int plugin_register_read (const char *name,
1122 int (*callback) (void))
1127 rf = malloc (sizeof (*rf));
1130 ERROR ("plugin_register_read: malloc failed.");
1134 memset (rf, 0, sizeof (read_func_t));
1135 rf->rf_callback = (void *) callback;
1136 rf->rf_udata.data = NULL;
1137 rf->rf_udata.free_func = NULL;
1138 rf->rf_ctx = plugin_get_ctx ();
1139 rf->rf_group[0] = '\0';
1140 rf->rf_name = strdup (name);
1141 rf->rf_type = RF_SIMPLE;
1142 rf->rf_interval = plugin_get_interval ();
1144 status = plugin_insert_read (rf);
1149 } /* int plugin_register_read */
1151 int plugin_register_complex_read (const char *group, const char *name,
1152 plugin_read_cb callback,
1153 const struct timespec *interval,
1154 user_data_t *user_data)
1159 rf = malloc (sizeof (*rf));
1162 ERROR ("plugin_register_complex_read: malloc failed.");
1166 memset (rf, 0, sizeof (read_func_t));
1167 rf->rf_callback = (void *) callback;
1169 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1171 rf->rf_group[0] = '\0';
1172 rf->rf_name = strdup (name);
1173 rf->rf_type = RF_COMPLEX;
1174 if (interval != NULL)
1175 rf->rf_interval = TIMESPEC_TO_CDTIME_T (interval);
1177 rf->rf_interval = plugin_get_interval ();
1180 if (user_data == NULL)
1182 rf->rf_udata.data = NULL;
1183 rf->rf_udata.free_func = NULL;
1187 rf->rf_udata = *user_data;
1190 rf->rf_ctx = plugin_get_ctx ();
1192 status = plugin_insert_read (rf);
1197 } /* int plugin_register_complex_read */
1199 int plugin_register_write (const char *name,
1200 plugin_write_cb callback, user_data_t *ud)
1202 return (create_register_callback (&list_write, name,
1203 (void *) callback, ud));
1204 } /* int plugin_register_write */
1206 int plugin_register_flush (const char *name,
1207 plugin_flush_cb callback, user_data_t *ud)
1209 return (create_register_callback (&list_flush, name,
1210 (void *) callback, ud));
1211 } /* int plugin_register_flush */
1213 int plugin_register_missing (const char *name,
1214 plugin_missing_cb callback, user_data_t *ud)
1216 return (create_register_callback (&list_missing, name,
1217 (void *) callback, ud));
1218 } /* int plugin_register_missing */
1220 int plugin_register_shutdown (const char *name,
1221 int (*callback) (void))
1223 return (create_register_callback (&list_shutdown, name,
1224 (void *) callback, /* user_data = */ NULL));
1225 } /* int plugin_register_shutdown */
1227 static void plugin_free_data_sets (void)
1232 if (data_sets == NULL)
1235 while (c_avl_pick (data_sets, &key, &value) == 0)
1237 data_set_t *ds = value;
1238 /* key is a pointer to ds->type */
1244 c_avl_destroy (data_sets);
1246 } /* void plugin_free_data_sets */
1248 int plugin_register_data_set (const data_set_t *ds)
1250 data_set_t *ds_copy;
1253 if ((data_sets != NULL)
1254 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1256 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1257 plugin_unregister_data_set (ds->type);
1259 else if (data_sets == NULL)
1261 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1262 if (data_sets == NULL)
1266 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1267 if (ds_copy == NULL)
1269 memcpy(ds_copy, ds, sizeof (data_set_t));
1271 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1273 if (ds_copy->ds == NULL)
1279 for (i = 0; i < ds->ds_num; i++)
1280 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1282 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1283 } /* int plugin_register_data_set */
1285 int plugin_register_log (const char *name,
1286 plugin_log_cb callback, user_data_t *ud)
1288 return (create_register_callback (&list_log, name,
1289 (void *) callback, ud));
1290 } /* int plugin_register_log */
1292 int plugin_register_notification (const char *name,
1293 plugin_notification_cb callback, user_data_t *ud)
1295 return (create_register_callback (&list_notification, name,
1296 (void *) callback, ud));
1297 } /* int plugin_register_log */
1299 int plugin_unregister_config (const char *name)
1301 cf_unregister (name);
1303 } /* int plugin_unregister_config */
1305 int plugin_unregister_complex_config (const char *name)
1307 cf_unregister_complex (name);
1309 } /* int plugin_unregister_complex_config */
1311 int plugin_unregister_init (const char *name)
1313 return (plugin_unregister (list_init, name));
1316 int plugin_unregister_read (const char *name) /* {{{ */
1324 pthread_mutex_lock (&read_lock);
1326 if (read_list == NULL)
1328 pthread_mutex_unlock (&read_lock);
1332 le = llist_search (read_list, name);
1335 pthread_mutex_unlock (&read_lock);
1336 WARNING ("plugin_unregister_read: No such read function: %s",
1341 llist_remove (read_list, le);
1344 assert (rf != NULL);
1345 rf->rf_type = RF_REMOVE;
1347 pthread_mutex_unlock (&read_lock);
1349 llentry_destroy (le);
1351 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1354 } /* }}} int plugin_unregister_read */
1356 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1358 read_func_t *rf = e->value;
1361 return strcmp (rf->rf_group, (const char *)group);
1362 } /* }}} int compare_read_func_group */
1364 int plugin_unregister_read_group (const char *group) /* {{{ */
1374 pthread_mutex_lock (&read_lock);
1376 if (read_list == NULL)
1378 pthread_mutex_unlock (&read_lock);
1384 le = llist_search_custom (read_list,
1385 compare_read_func_group, (void *)group);
1392 llist_remove (read_list, le);
1395 assert (rf != NULL);
1396 rf->rf_type = RF_REMOVE;
1398 llentry_destroy (le);
1400 DEBUG ("plugin_unregister_read_group: "
1401 "Marked `%s' (group `%s') for removal.",
1402 rf->rf_name, group);
1405 pthread_mutex_unlock (&read_lock);
1409 WARNING ("plugin_unregister_read_group: No such "
1410 "group of read function: %s", group);
1415 } /* }}} int plugin_unregister_read_group */
1417 int plugin_unregister_write (const char *name)
1419 return (plugin_unregister (list_write, name));
1422 int plugin_unregister_flush (const char *name)
1424 return (plugin_unregister (list_flush, name));
1427 int plugin_unregister_missing (const char *name)
1429 return (plugin_unregister (list_missing, name));
1432 int plugin_unregister_shutdown (const char *name)
1434 return (plugin_unregister (list_shutdown, name));
1437 int plugin_unregister_data_set (const char *name)
1441 if (data_sets == NULL)
1444 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1451 } /* int plugin_unregister_data_set */
1453 int plugin_unregister_log (const char *name)
1455 return (plugin_unregister (list_log, name));
1458 int plugin_unregister_notification (const char *name)
1460 return (plugin_unregister (list_notification, name));
1463 void plugin_init_all (void)
1465 char const *chain_name;
1466 long write_threads_num;
1470 /* Init the value cache */
1473 chain_name = global_option_get ("PreCacheChain");
1474 pre_cache_chain = fc_chain_get_by_name (chain_name);
1476 chain_name = global_option_get ("PostCacheChain");
1477 post_cache_chain = fc_chain_get_by_name (chain_name);
1479 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1481 if (write_limit_high < 0)
1483 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1484 write_limit_high = 0;
1487 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1488 /* default = */ write_limit_high / 2);
1489 if (write_limit_low < 0)
1491 ERROR ("WriteQueueLimitLow must be positive or zero.");
1492 write_limit_low = write_limit_high / 2;
1494 else if (write_limit_low > write_limit_high)
1496 ERROR ("WriteQueueLimitLow must not be larger than "
1497 "WriteQueueLimitHigh.");
1498 write_limit_low = write_limit_high;
1501 write_threads_num = global_option_get_long ("WriteThreads",
1503 if (write_threads_num < 1)
1505 ERROR ("WriteThreads must be positive.");
1506 write_threads_num = 5;
1509 start_write_threads ((size_t) write_threads_num);
1511 if ((list_init == NULL) && (read_heap == NULL))
1514 /* Calling all init callbacks before checking if read callbacks
1515 * are available allows the init callbacks to register the read
1517 le = llist_head (list_init);
1520 callback_func_t *cf;
1521 plugin_init_cb callback;
1522 plugin_ctx_t old_ctx;
1525 old_ctx = plugin_set_ctx (cf->cf_ctx);
1526 callback = cf->cf_callback;
1527 status = (*callback) ();
1528 plugin_set_ctx (old_ctx);
1532 ERROR ("Initialization of plugin `%s' "
1533 "failed with status %i. "
1534 "Plugin will be unloaded.",
1536 /* Plugins that register read callbacks from the init
1537 * callback should take care of appropriate error
1538 * handling themselves. */
1539 /* FIXME: Unload _all_ functions */
1540 plugin_unregister_read (le->key);
1546 /* Start read-threads */
1547 if (read_heap != NULL)
1552 max_read_interval = global_option_get_long ("MaxReadInterval",
1553 DEFAULT_MAX_READ_INTERVAL);
1554 if (max_read_interval <= 0) {
1555 ERROR ("MaxReadInterval must be positive");
1556 max_read_interval = DEFAULT_MAX_READ_INTERVAL;
1559 rt = global_option_get ("ReadThreads");
1562 start_read_threads ((num > 0) ? num : 5);
1564 } /* void plugin_init_all */
1566 /* TODO: Rename this function. */
1567 void plugin_read_all (void)
1569 uc_check_timeout ();
1572 } /* void plugin_read_all */
1574 /* Read function called when the `-T' command line argument is given. */
1575 int plugin_read_all_once (void)
1578 int return_status = 0;
1580 if (read_heap == NULL)
1582 NOTICE ("No read-functions are registered.");
1589 plugin_ctx_t old_ctx;
1591 rf = c_heap_get_root (read_heap);
1595 old_ctx = plugin_set_ctx (rf->rf_ctx);
1597 if (rf->rf_type == RF_SIMPLE)
1599 int (*callback) (void);
1601 callback = rf->rf_callback;
1602 status = (*callback) ();
1606 plugin_read_cb callback;
1608 callback = rf->rf_callback;
1609 status = (*callback) (&rf->rf_udata);
1612 plugin_set_ctx (old_ctx);
1616 NOTICE ("read-function of plugin `%s' failed.",
1621 destroy_callback ((void *) rf);
1624 return (return_status);
1625 } /* int plugin_read_all_once */
1627 int plugin_write (const char *plugin, /* {{{ */
1628 const data_set_t *ds, const value_list_t *vl)
1636 if (list_write == NULL)
1641 ds = plugin_get_ds (vl->type);
1644 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1654 le = llist_head (list_write);
1657 callback_func_t *cf = le->value;
1658 plugin_write_cb callback;
1660 /* do not switch plugin context; rather keep the context (interval)
1661 * information of the calling read plugin */
1663 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1664 callback = cf->cf_callback;
1665 status = (*callback) (ds, vl, &cf->cf_udata);
1674 if ((success == 0) && (failure != 0))
1679 else /* plugin != NULL */
1681 callback_func_t *cf;
1682 plugin_write_cb callback;
1684 le = llist_head (list_write);
1687 if (strcasecmp (plugin, le->key) == 0)
1698 /* do not switch plugin context; rather keep the context (interval)
1699 * information of the calling read plugin */
1701 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1702 callback = cf->cf_callback;
1703 status = (*callback) (ds, vl, &cf->cf_udata);
1707 } /* }}} int plugin_write */
1709 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1713 if (list_flush == NULL)
1716 le = llist_head (list_flush);
1719 callback_func_t *cf;
1720 plugin_flush_cb callback;
1721 plugin_ctx_t old_ctx;
1723 if ((plugin != NULL)
1724 && (strcmp (plugin, le->key) != 0))
1731 old_ctx = plugin_set_ctx (cf->cf_ctx);
1732 callback = cf->cf_callback;
1734 (*callback) (timeout, identifier, &cf->cf_udata);
1736 plugin_set_ctx (old_ctx);
1741 } /* int plugin_flush */
1743 void plugin_shutdown_all (void)
1747 stop_read_threads ();
1749 destroy_all_callbacks (&list_init);
1751 pthread_mutex_lock (&read_lock);
1752 llist_destroy (read_list);
1754 pthread_mutex_unlock (&read_lock);
1756 destroy_read_heap ();
1758 plugin_flush (/* plugin = */ NULL,
1760 /* identifier = */ NULL);
1763 if (list_shutdown != NULL)
1764 le = llist_head (list_shutdown);
1768 callback_func_t *cf;
1769 plugin_shutdown_cb callback;
1770 plugin_ctx_t old_ctx;
1773 old_ctx = plugin_set_ctx (cf->cf_ctx);
1774 callback = cf->cf_callback;
1776 /* Advance the pointer before calling the callback allows
1777 * shutdown functions to unregister themselves. If done the
1778 * other way around the memory `le' points to will be freed
1779 * after callback returns. */
1784 plugin_set_ctx (old_ctx);
1787 stop_write_threads ();
1789 /* Write plugins which use the `user_data' pointer usually need the
1790 * same data available to the flush callback. If this is the case, set
1791 * the free_function to NULL when registering the flush callback and to
1792 * the real free function when registering the write callback. This way
1793 * the data isn't freed twice. */
1794 destroy_all_callbacks (&list_flush);
1795 destroy_all_callbacks (&list_missing);
1796 destroy_all_callbacks (&list_write);
1798 destroy_all_callbacks (&list_notification);
1799 destroy_all_callbacks (&list_shutdown);
1800 destroy_all_callbacks (&list_log);
1802 plugin_free_loaded ();
1803 plugin_free_data_sets ();
1804 } /* void plugin_shutdown_all */
1806 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1810 if (list_missing == NULL)
1813 le = llist_head (list_missing);
1816 callback_func_t *cf;
1817 plugin_missing_cb callback;
1818 plugin_ctx_t old_ctx;
1822 old_ctx = plugin_set_ctx (cf->cf_ctx);
1823 callback = cf->cf_callback;
1825 status = (*callback) (vl, &cf->cf_udata);
1826 plugin_set_ctx (old_ctx);
1831 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1832 "failed with status %i.",
1845 } /* int }}} plugin_dispatch_missing */
1847 static int plugin_dispatch_values_internal (value_list_t *vl)
1850 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1852 value_t *saved_values;
1853 int saved_values_len;
1857 int free_meta_data = 0;
1859 if ((vl == NULL) || (vl->type[0] == 0)
1860 || (vl->values == NULL) || (vl->values_len < 1))
1862 ERROR ("plugin_dispatch_values: Invalid value list "
1863 "from plugin %s.", vl->plugin);
1867 /* Free meta data only if the calling function didn't specify any. In
1868 * this case matches and targets may add some and the calling function
1869 * may not expect (and therefore free) that data. */
1870 if (vl->meta == NULL)
1873 if (list_write == NULL)
1874 c_complain_once (LOG_WARNING, &no_write_complaint,
1875 "plugin_dispatch_values: No write callback has been "
1876 "registered. Please load at least one output plugin, "
1877 "if you want the collected data to be stored.");
1879 if (data_sets == NULL)
1881 ERROR ("plugin_dispatch_values: No data sets registered. "
1882 "Could the types database be read? Check "
1883 "your `TypesDB' setting!");
1887 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1889 char ident[6 * DATA_MAX_NAME_LEN];
1891 FORMAT_VL (ident, sizeof (ident), vl);
1892 INFO ("plugin_dispatch_values: Dataset not found: %s "
1893 "(from \"%s\"), check your types.db!",
1898 /* Assured by plugin_value_list_clone(). The time is determined at
1899 * _enqueue_ time. */
1900 assert (vl->time != 0);
1901 assert (vl->interval != 0);
1903 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1905 "plugin = %s; plugin_instance = %s; "
1906 "type = %s; type_instance = %s;",
1907 CDTIME_T_TO_DOUBLE (vl->time),
1908 CDTIME_T_TO_DOUBLE (vl->interval),
1910 vl->plugin, vl->plugin_instance,
1911 vl->type, vl->type_instance);
1914 assert (0 == strcmp (ds->type, vl->type));
1916 if (0 != strcmp (ds->type, vl->type))
1917 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1918 ds->type, vl->type);
1922 assert (ds->ds_num == vl->values_len);
1924 if (ds->ds_num != vl->values_len)
1926 ERROR ("plugin_dispatch_values: ds->type = %s: "
1927 "(ds->ds_num = %i) != "
1928 "(vl->values_len = %i)",
1929 ds->type, ds->ds_num, vl->values_len);
1934 escape_slashes (vl->host, sizeof (vl->host));
1935 escape_slashes (vl->plugin, sizeof (vl->plugin));
1936 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1937 escape_slashes (vl->type, sizeof (vl->type));
1938 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1940 /* Copy the values. This way, we can assure `targets' that they get
1941 * dynamically allocated values, which they can free and replace if
1943 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1945 saved_values = vl->values;
1946 saved_values_len = vl->values_len;
1948 vl->values = (value_t *) calloc (vl->values_len,
1949 sizeof (*vl->values));
1950 if (vl->values == NULL)
1952 ERROR ("plugin_dispatch_values: calloc failed.");
1953 vl->values = saved_values;
1956 memcpy (vl->values, saved_values,
1957 vl->values_len * sizeof (*vl->values));
1959 else /* if ((pre == NULL) && (post == NULL)) */
1961 saved_values = NULL;
1962 saved_values_len = 0;
1965 if (pre_cache_chain != NULL)
1967 status = fc_process_chain (ds, vl, pre_cache_chain);
1970 WARNING ("plugin_dispatch_values: Running the "
1971 "pre-cache chain failed with "
1975 else if (status == FC_TARGET_STOP)
1977 /* Restore the state of the value_list so that plugins
1978 * don't get confused.. */
1979 if (saved_values != NULL)
1982 vl->values = saved_values;
1983 vl->values_len = saved_values_len;
1989 /* Update the value cache */
1992 if (post_cache_chain != NULL)
1994 status = fc_process_chain (ds, vl, post_cache_chain);
1997 WARNING ("plugin_dispatch_values: Running the "
1998 "post-cache chain failed with "
2004 fc_default_action (ds, vl);
2006 /* Restore the state of the value_list so that plugins don't get
2008 if (saved_values != NULL)
2011 vl->values = saved_values;
2012 vl->values_len = saved_values_len;
2015 if ((free_meta_data != 0) && (vl->meta != NULL))
2017 meta_data_destroy (vl->meta);
2022 } /* int plugin_dispatch_values_internal */
2024 static double get_drop_probability (void) /* {{{ */
2030 pthread_mutex_lock (&write_lock);
2031 wql = write_queue_length;
2032 pthread_mutex_unlock (&write_lock);
2034 if (wql < write_limit_low)
2036 if (wql >= write_limit_high)
2039 pos = 1 + wql - write_limit_low;
2040 size = 1 + write_limit_high - write_limit_low;
2042 return (((double) pos) / ((double) size));
2043 } /* }}} double get_drop_probability */
2045 static _Bool check_drop_value (void) /* {{{ */
2047 static cdtime_t last_message_time = 0;
2048 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2054 if (write_limit_high == 0)
2057 p = get_drop_probability ();
2061 status = pthread_mutex_trylock (&last_message_lock);
2067 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2069 last_message_time = now;
2070 ERROR ("plugin_dispatch_values: Low water mark "
2071 "reached. Dropping %.0f%% of metrics.",
2074 pthread_mutex_unlock (&last_message_lock);
2085 } /* }}} _Bool check_drop_value */
2087 int plugin_dispatch_values (value_list_t const *vl)
2091 if (check_drop_value ())
2094 status = plugin_write_enqueue (vl);
2098 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2099 "with status %i (%s).", status,
2100 sstrerror (status, errbuf, sizeof (errbuf)));
2107 __attribute__((sentinel))
2108 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2109 _Bool store_percentage, ...)
2116 assert (template->values_len == 1);
2118 va_start (ap, store_percentage);
2124 name = va_arg (ap, char const *);
2128 value = va_arg (ap, gauge_t);
2134 vl = plugin_value_list_clone (template);
2135 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2136 if (store_percentage)
2137 sstrncpy (vl->type, "percent", sizeof (vl->type));
2139 va_start (ap, store_percentage);
2145 /* Set the type instance. */
2146 name = va_arg (ap, char const *);
2149 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2151 /* Set the value. */
2152 vl->values[0].gauge = va_arg (ap, gauge_t);
2153 if (store_percentage)
2154 vl->values[0].gauge *= 100.0 / sum;
2156 status = plugin_write_enqueue (vl);
2162 plugin_value_list_free (vl);
2164 } /* }}} int plugin_dispatch_multivalue */
2166 int plugin_dispatch_notification (const notification_t *notif)
2169 /* Possible TODO: Add flap detection here */
2171 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2172 "time = %.3f; host = %s;",
2173 notif->severity, notif->message,
2174 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2176 /* Nobody cares for notifications */
2177 if (list_notification == NULL)
2180 le = llist_head (list_notification);
2183 callback_func_t *cf;
2184 plugin_notification_cb callback;
2187 /* do not switch plugin context; rather keep the context
2188 * (interval) information of the calling plugin */
2191 callback = cf->cf_callback;
2192 status = (*callback) (notif, &cf->cf_udata);
2195 WARNING ("plugin_dispatch_notification: Notification "
2196 "callback %s returned %i.",
2204 } /* int plugin_dispatch_notification */
2206 void plugin_log (int level, const char *format, ...)
2213 if (level >= LOG_DEBUG)
2217 va_start (ap, format);
2218 vsnprintf (msg, sizeof (msg), format, ap);
2219 msg[sizeof (msg) - 1] = '\0';
2222 if (list_log == NULL)
2224 fprintf (stderr, "%s\n", msg);
2228 le = llist_head (list_log);
2231 callback_func_t *cf;
2232 plugin_log_cb callback;
2235 callback = cf->cf_callback;
2237 /* do not switch plugin context; rather keep the context
2238 * (interval) information of the calling plugin */
2240 (*callback) (level, msg, &cf->cf_udata);
2244 } /* void plugin_log */
2246 int parse_log_severity (const char *severity)
2250 if ((0 == strcasecmp (severity, "emerg"))
2251 || (0 == strcasecmp (severity, "alert"))
2252 || (0 == strcasecmp (severity, "crit"))
2253 || (0 == strcasecmp (severity, "err")))
2254 log_level = LOG_ERR;
2255 else if (0 == strcasecmp (severity, "warning"))
2256 log_level = LOG_WARNING;
2257 else if (0 == strcasecmp (severity, "notice"))
2258 log_level = LOG_NOTICE;
2259 else if (0 == strcasecmp (severity, "info"))
2260 log_level = LOG_INFO;
2262 else if (0 == strcasecmp (severity, "debug"))
2263 log_level = LOG_DEBUG;
2264 #endif /* COLLECT_DEBUG */
2267 } /* int parse_log_severity */
2269 int parse_notif_severity (const char *severity)
2271 int notif_severity = -1;
2273 if (strcasecmp (severity, "FAILURE") == 0)
2274 notif_severity = NOTIF_FAILURE;
2275 else if (strcmp (severity, "OKAY") == 0)
2276 notif_severity = NOTIF_OKAY;
2277 else if ((strcmp (severity, "WARNING") == 0)
2278 || (strcmp (severity, "WARN") == 0))
2279 notif_severity = NOTIF_WARNING;
2281 return (notif_severity);
2282 } /* int parse_notif_severity */
2284 const data_set_t *plugin_get_ds (const char *name)
2288 if (data_sets == NULL)
2290 ERROR ("plugin_get_ds: No data sets are defined yet.");
2294 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2296 DEBUG ("No such dataset registered: %s", name);
2301 } /* data_set_t *plugin_get_ds */
2303 static int plugin_notification_meta_add (notification_t *n,
2305 enum notification_meta_type_e type,
2308 notification_meta_t *meta;
2309 notification_meta_t *tail;
2311 if ((n == NULL) || (name == NULL) || (value == NULL))
2313 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2317 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2320 ERROR ("plugin_notification_meta_add: malloc failed.");
2323 memset (meta, 0, sizeof (notification_meta_t));
2325 sstrncpy (meta->name, name, sizeof (meta->name));
2330 case NM_TYPE_STRING:
2332 meta->nm_value.nm_string = strdup ((const char *) value);
2333 if (meta->nm_value.nm_string == NULL)
2335 ERROR ("plugin_notification_meta_add: strdup failed.");
2341 case NM_TYPE_SIGNED_INT:
2343 meta->nm_value.nm_signed_int = *((int64_t *) value);
2346 case NM_TYPE_UNSIGNED_INT:
2348 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2351 case NM_TYPE_DOUBLE:
2353 meta->nm_value.nm_double = *((double *) value);
2356 case NM_TYPE_BOOLEAN:
2358 meta->nm_value.nm_boolean = *((_Bool *) value);
2363 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2367 } /* switch (type) */
2371 while ((tail != NULL) && (tail->next != NULL))
2380 } /* int plugin_notification_meta_add */
2382 int plugin_notification_meta_add_string (notification_t *n,
2386 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2389 int plugin_notification_meta_add_signed_int (notification_t *n,
2393 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2396 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2400 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2403 int plugin_notification_meta_add_double (notification_t *n,
2407 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2410 int plugin_notification_meta_add_boolean (notification_t *n,
2414 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2417 int plugin_notification_meta_copy (notification_t *dst,
2418 const notification_t *src)
2420 notification_meta_t *meta;
2422 assert (dst != NULL);
2423 assert (src != NULL);
2424 assert (dst != src);
2425 assert ((src->meta == NULL) || (src->meta != dst->meta));
2427 for (meta = src->meta; meta != NULL; meta = meta->next)
2429 if (meta->type == NM_TYPE_STRING)
2430 plugin_notification_meta_add_string (dst, meta->name,
2431 meta->nm_value.nm_string);
2432 else if (meta->type == NM_TYPE_SIGNED_INT)
2433 plugin_notification_meta_add_signed_int (dst, meta->name,
2434 meta->nm_value.nm_signed_int);
2435 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2436 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2437 meta->nm_value.nm_unsigned_int);
2438 else if (meta->type == NM_TYPE_DOUBLE)
2439 plugin_notification_meta_add_double (dst, meta->name,
2440 meta->nm_value.nm_double);
2441 else if (meta->type == NM_TYPE_BOOLEAN)
2442 plugin_notification_meta_add_boolean (dst, meta->name,
2443 meta->nm_value.nm_boolean);
2447 } /* int plugin_notification_meta_copy */
2449 int plugin_notification_meta_free (notification_meta_t *n)
2451 notification_meta_t *this;
2452 notification_meta_t *next;
2456 ERROR ("plugin_notification_meta_free: n == NULL!");
2461 while (this != NULL)
2465 if (this->type == NM_TYPE_STRING)
2467 free ((char *)this->nm_value.nm_string);
2468 this->nm_value.nm_string = NULL;
2476 } /* int plugin_notification_meta_free */
2478 static void plugin_ctx_destructor (void *ctx)
2481 } /* void plugin_ctx_destructor */
2483 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2485 static plugin_ctx_t *plugin_ctx_create (void)
2489 ctx = malloc (sizeof (*ctx));
2492 ERROR ("Failed to allocate plugin context: %s",
2493 sstrerror (errno, errbuf, sizeof (errbuf)));
2498 assert (plugin_ctx_key_initialized);
2499 pthread_setspecific (plugin_ctx_key, ctx);
2500 DEBUG("Created new plugin context.");
2502 } /* int plugin_ctx_create */
2504 void plugin_init_ctx (void)
2506 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2507 plugin_ctx_key_initialized = 1;
2508 } /* void plugin_init_ctx */
2510 plugin_ctx_t plugin_get_ctx (void)
2514 assert (plugin_ctx_key_initialized);
2515 ctx = pthread_getspecific (plugin_ctx_key);
2518 ctx = plugin_ctx_create ();
2519 /* this must no happen -- exit() instead? */
2525 } /* plugin_ctx_t plugin_get_ctx */
2527 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2532 assert (plugin_ctx_key_initialized);
2533 c = pthread_getspecific (plugin_ctx_key);
2536 c = plugin_ctx_create ();
2537 /* this must no happen -- exit() instead? */
2546 } /* void plugin_set_ctx */
2548 cdtime_t plugin_get_interval (void)
2552 interval = plugin_get_ctx().interval;
2556 return cf_get_default_interval ();
2557 } /* cdtime_t plugin_get_interval */
2561 void *(*start_routine) (void *);
2565 static void *plugin_thread_start (void *arg)
2567 plugin_thread_t *plugin_thread = arg;
2569 void *(*start_routine) (void *) = plugin_thread->start_routine;
2570 void *plugin_arg = plugin_thread->arg;
2572 plugin_set_ctx (plugin_thread->ctx);
2574 free (plugin_thread);
2576 return start_routine (plugin_arg);
2577 } /* void *plugin_thread_start */
2579 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2580 void *(*start_routine) (void *), void *arg)
2582 plugin_thread_t *plugin_thread;
2584 plugin_thread = malloc (sizeof (*plugin_thread));
2585 if (plugin_thread == NULL)
2588 plugin_thread->ctx = plugin_get_ctx ();
2589 plugin_thread->start_routine = start_routine;
2590 plugin_thread->arg = arg;
2592 return pthread_create (thread, attr,
2593 plugin_thread_start, plugin_thread);
2594 } /* int plugin_thread_create */
2596 /* vim: set sw=8 ts=8 noet fdm=marker : */