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 static c_heap_t *read_heap = NULL;
108 static llist_t *read_list;
109 static int read_loop = 1;
110 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
111 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
112 static pthread_t *read_threads = NULL;
113 static int read_threads_num = 0;
115 static write_queue_t *write_queue_head;
116 static write_queue_t *write_queue_tail;
117 static long write_queue_length = 0;
118 static _Bool write_loop = 1;
119 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
120 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
121 static pthread_t *write_threads = NULL;
122 static size_t write_threads_num = 0;
124 static pthread_key_t plugin_ctx_key;
125 static _Bool plugin_ctx_key_initialized = 0;
127 static long write_limit_high = 0;
128 static long write_limit_low = 0;
133 static int plugin_dispatch_values_internal (value_list_t *vl);
135 static const char *plugin_get_dir (void)
137 if (plugindir == NULL)
143 static void destroy_callback (callback_func_t *cf) /* {{{ */
148 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
150 cf->cf_udata.free_func (cf->cf_udata.data);
151 cf->cf_udata.data = NULL;
152 cf->cf_udata.free_func = NULL;
155 } /* }}} void destroy_callback */
157 static void destroy_all_callbacks (llist_t **list) /* {{{ */
164 le = llist_head (*list);
172 destroy_callback (le->value);
178 llist_destroy (*list);
180 } /* }}} void destroy_all_callbacks */
182 static void destroy_read_heap (void) /* {{{ */
184 if (read_heap == NULL)
191 cf = c_heap_get_root (read_heap);
195 destroy_callback (cf);
198 c_heap_destroy (read_heap);
200 } /* }}} void destroy_read_heap */
202 static int register_callback (llist_t **list, /* {{{ */
203 const char *name, callback_func_t *cf)
210 *list = llist_create ();
213 ERROR ("plugin: register_callback: "
214 "llist_create failed.");
215 destroy_callback (cf);
223 ERROR ("plugin: register_callback: strdup failed.");
224 destroy_callback (cf);
228 le = llist_search (*list, name);
231 le = llentry_create (key, cf);
234 ERROR ("plugin: register_callback: "
235 "llentry_create failed.");
237 destroy_callback (cf);
241 llist_append (*list, le);
245 callback_func_t *old_cf;
250 WARNING ("plugin: register_callback: "
251 "a callback named `%s' already exists - "
252 "overwriting the old entry!", name);
254 destroy_callback (old_cf);
259 } /* }}} int register_callback */
261 static int create_register_callback (llist_t **list, /* {{{ */
262 const char *name, void *callback, user_data_t *ud)
266 cf = (callback_func_t *) malloc (sizeof (*cf));
269 ERROR ("plugin: create_register_callback: malloc failed.");
272 memset (cf, 0, sizeof (*cf));
274 cf->cf_callback = callback;
277 cf->cf_udata.data = NULL;
278 cf->cf_udata.free_func = NULL;
285 cf->cf_ctx = plugin_get_ctx ();
287 return (register_callback (list, name, cf));
288 } /* }}} int create_register_callback */
290 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
297 e = llist_search (list, name);
301 llist_remove (list, e);
304 destroy_callback (e->value);
309 } /* }}} int plugin_unregister */
312 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
313 * object, but it will bitch about a shared object not having a
314 * ``module_register'' symbol..
316 static int plugin_load_file (char *file, uint32_t flags)
319 void (*reg_handle) (void);
322 lt_dlerror (); /* clear errors */
324 #if LIBTOOL_VERSION == 2
325 if (flags & PLUGIN_FLAGS_GLOBAL) {
327 lt_dladvise_init(&advise);
328 lt_dladvise_global(&advise);
329 dlh = lt_dlopenadvise(file, advise);
330 lt_dladvise_destroy(&advise);
332 dlh = lt_dlopen (file);
334 #else /* if LIBTOOL_VERSION == 1 */
335 if (flags & PLUGIN_FLAGS_GLOBAL)
336 WARNING ("plugin_load_file: The global flag is not supported, "
337 "libtool 2 is required for this.");
338 dlh = lt_dlopen (file);
343 char errbuf[1024] = "";
345 ssnprintf (errbuf, sizeof (errbuf),
346 "lt_dlopen (\"%s\") failed: %s. "
347 "The most common cause for this problem are "
348 "missing dependencies. Use ldd(1) to check "
349 "the dependencies of the plugin "
351 file, lt_dlerror ());
353 ERROR ("%s", errbuf);
354 /* Make sure this is printed to STDERR in any case, but also
355 * make sure it's printed only once. */
356 if (list_log != NULL)
357 fprintf (stderr, "ERROR: %s\n", errbuf);
362 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
364 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
365 file, lt_dlerror ());
375 static void *plugin_read_thread (void __attribute__((unused)) *args)
377 while (read_loop != 0)
380 plugin_ctx_t old_ctx;
386 /* Get the read function that needs to be read next.
387 * We don't need to hold "read_lock" for the heap, but we need
388 * to call c_heap_get_root() and pthread_cond_wait() in the
389 * same protected block. */
390 pthread_mutex_lock (&read_lock);
391 rf = c_heap_get_root (read_heap);
394 pthread_cond_wait (&read_cond, &read_lock);
395 pthread_mutex_unlock (&read_lock);
398 pthread_mutex_unlock (&read_lock);
400 if (rf->rf_interval == 0)
402 /* this should not happen, because the interval is set
403 * for each plugin when loading it
404 * XXX: issue a warning? */
405 rf->rf_interval = plugin_get_interval ();
406 rf->rf_effective_interval = rf->rf_interval;
408 rf->rf_next_read = cdtime ();
411 /* sleep until this entry is due,
412 * using pthread_cond_timedwait */
413 pthread_mutex_lock (&read_lock);
414 /* In pthread_cond_timedwait, spurious wakeups are possible
415 * (and really happen, at least on NetBSD with > 1 CPU), thus
416 * we need to re-evaluate the condition every time
417 * pthread_cond_timedwait returns. */
419 while ((read_loop != 0)
420 && (cdtime () < rf->rf_next_read)
423 struct timespec ts = { 0 };
425 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
427 rc = pthread_cond_timedwait (&read_cond, &read_lock,
431 /* Must hold `read_lock' when accessing `rf->rf_type'. */
432 rf_type = rf->rf_type;
433 pthread_mutex_unlock (&read_lock);
435 /* Check if we're supposed to stop.. This may have interrupted
439 /* Insert `rf' again, so it can be free'd correctly */
440 c_heap_insert (read_heap, rf);
444 /* The entry has been marked for deletion. The linked list
445 * entry has already been removed by `plugin_unregister_read'.
446 * All we have to do here is free the `read_func_t' and
448 if (rf_type == RF_REMOVE)
450 DEBUG ("plugin_read_thread: Destroying the `%s' "
451 "callback.", rf->rf_name);
453 destroy_callback ((callback_func_t *) rf);
458 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
460 old_ctx = plugin_set_ctx (rf->rf_ctx);
462 if (rf_type == RF_SIMPLE)
464 int (*callback) (void);
466 callback = rf->rf_callback;
467 status = (*callback) ();
471 plugin_read_cb callback;
473 assert (rf_type == RF_COMPLEX);
475 callback = rf->rf_callback;
476 status = (*callback) (&rf->rf_udata);
479 plugin_set_ctx (old_ctx);
481 /* If the function signals failure, we will increase the
482 * intervals in which it will be called. */
485 rf->rf_effective_interval *= 2;
486 if (rf->rf_effective_interval > TIME_T_TO_CDTIME_T (86400))
487 rf->rf_effective_interval = TIME_T_TO_CDTIME_T (86400);
489 NOTICE ("read-function of plugin `%s' failed. "
490 "Will suspend it for %.3f seconds.",
492 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
496 /* Success: Restore the interval, if it was changed. */
497 rf->rf_effective_interval = rf->rf_interval;
500 /* update the ``next read due'' field */
503 DEBUG ("plugin_read_thread: Effective interval of the "
504 "%s plugin is %.3f seconds.",
506 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
508 /* Calculate the next (absolute) time at which this function
509 * should be called. */
510 rf->rf_next_read += rf->rf_effective_interval;
512 /* Check, if `rf_next_read' is in the past. */
513 if (rf->rf_next_read < now)
515 /* `rf_next_read' is in the past. Insert `now'
516 * so this value doesn't trail off into the
518 rf->rf_next_read = now;
521 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
523 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
525 /* Re-insert this read function into the heap again. */
526 c_heap_insert (read_heap, rf);
527 } /* while (read_loop) */
531 } /* void *plugin_read_thread */
533 static void start_read_threads (int num)
537 if (read_threads != NULL)
540 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
541 if (read_threads == NULL)
543 ERROR ("plugin: start_read_threads: calloc failed.");
547 read_threads_num = 0;
548 for (i = 0; i < num; i++)
550 if (pthread_create (read_threads + read_threads_num, NULL,
551 plugin_read_thread, NULL) == 0)
557 ERROR ("plugin: start_read_threads: pthread_create failed.");
561 } /* void start_read_threads */
563 static void stop_read_threads (void)
567 if (read_threads == NULL)
570 INFO ("collectd: Stopping %i read threads.", read_threads_num);
572 pthread_mutex_lock (&read_lock);
574 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
575 pthread_cond_broadcast (&read_cond);
576 pthread_mutex_unlock (&read_lock);
578 for (i = 0; i < read_threads_num; i++)
580 if (pthread_join (read_threads[i], NULL) != 0)
582 ERROR ("plugin: stop_read_threads: pthread_join failed.");
584 read_threads[i] = (pthread_t) 0;
586 sfree (read_threads);
587 read_threads_num = 0;
588 } /* void stop_read_threads */
590 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
595 meta_data_destroy (vl->meta);
598 } /* }}} void plugin_value_list_free */
600 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
607 vl = malloc (sizeof (*vl));
610 memcpy (vl, vl_orig, sizeof (*vl));
612 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
613 if (vl->values == NULL)
615 plugin_value_list_free (vl);
618 memcpy (vl->values, vl_orig->values,
619 vl_orig->values_len * sizeof (*vl->values));
621 vl->meta = meta_data_clone (vl->meta);
622 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
624 plugin_value_list_free (vl);
629 vl->time = cdtime ();
631 /* Fill in the interval from the thread context, if it is zero. */
632 if (vl->interval == 0)
634 plugin_ctx_t ctx = plugin_get_ctx ();
636 if (ctx.interval != 0)
637 vl->interval = ctx.interval;
640 char name[6 * DATA_MAX_NAME_LEN];
641 FORMAT_VL (name, sizeof (name), vl);
642 ERROR ("plugin_value_list_clone: Unable to determine "
643 "interval from context for "
644 "value list \"%s\". "
645 "This indicates a broken plugin. "
646 "Please report this problem to the "
647 "collectd mailing list or at "
648 "<http://collectd.org/bugs/>.", name);
649 vl->interval = cf_get_default_interval ();
654 } /* }}} value_list_t *plugin_value_list_clone */
656 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
660 q = malloc (sizeof (*q));
665 q->vl = plugin_value_list_clone (vl);
672 /* Store context of caller (read plugin); otherwise, it would not be
673 * available to the write plugins when actually dispatching the
674 * value-list later on. */
675 q->ctx = plugin_get_ctx ();
677 pthread_mutex_lock (&write_lock);
679 if (write_queue_tail == NULL)
681 write_queue_head = q;
682 write_queue_tail = q;
683 write_queue_length = 1;
687 write_queue_tail->next = q;
688 write_queue_tail = q;
689 write_queue_length += 1;
692 pthread_cond_signal (&write_cond);
693 pthread_mutex_unlock (&write_lock);
696 } /* }}} int plugin_write_enqueue */
698 static value_list_t *plugin_write_dequeue (void) /* {{{ */
703 pthread_mutex_lock (&write_lock);
705 while (write_loop && (write_queue_head == NULL))
706 pthread_cond_wait (&write_cond, &write_lock);
708 if (write_queue_head == NULL)
710 pthread_mutex_unlock (&write_lock);
714 q = write_queue_head;
715 write_queue_head = q->next;
716 write_queue_length -= 1;
717 if (write_queue_head == NULL) {
718 write_queue_tail = NULL;
719 assert(0 == write_queue_length);
722 pthread_mutex_unlock (&write_lock);
724 (void) plugin_set_ctx (q->ctx);
729 } /* }}} value_list_t *plugin_write_dequeue */
731 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
735 value_list_t *vl = plugin_write_dequeue ();
739 plugin_dispatch_values_internal (vl);
741 plugin_value_list_free (vl);
746 } /* }}} void *plugin_write_thread */
748 static void start_write_threads (size_t num) /* {{{ */
752 if (write_threads != NULL)
755 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
756 if (write_threads == NULL)
758 ERROR ("plugin: start_write_threads: calloc failed.");
762 write_threads_num = 0;
763 for (i = 0; i < num; i++)
767 status = pthread_create (write_threads + write_threads_num,
774 ERROR ("plugin: start_write_threads: pthread_create failed "
775 "with status %i (%s).", status,
776 sstrerror (status, errbuf, sizeof (errbuf)));
782 } /* }}} void start_write_threads */
784 static void stop_write_threads (void) /* {{{ */
789 if (write_threads == NULL)
792 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
794 pthread_mutex_lock (&write_lock);
796 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
797 pthread_cond_broadcast (&write_cond);
798 pthread_mutex_unlock (&write_lock);
800 for (i = 0; i < write_threads_num; i++)
802 if (pthread_join (write_threads[i], NULL) != 0)
804 ERROR ("plugin: stop_write_threads: pthread_join failed.");
806 write_threads[i] = (pthread_t) 0;
808 sfree (write_threads);
809 write_threads_num = 0;
811 pthread_mutex_lock (&write_lock);
813 for (q = write_queue_head; q != NULL; )
815 write_queue_t *q1 = q;
816 plugin_value_list_free (q->vl);
821 write_queue_head = NULL;
822 write_queue_tail = NULL;
823 write_queue_length = 0;
824 pthread_mutex_unlock (&write_lock);
828 WARNING ("plugin: %i value list%s left after shutting down "
829 "the write threads.",
830 i, (i == 1) ? " was" : "s were");
832 } /* }}} void stop_write_threads */
837 void plugin_set_dir (const char *dir)
839 if (plugindir != NULL)
844 else if ((plugindir = strdup (dir)) == NULL)
847 ERROR ("strdup failed: %s",
848 sstrerror (errno, errbuf, sizeof (errbuf)));
852 static _Bool plugin_is_loaded (char const *name)
856 if (plugins_loaded == NULL)
857 plugins_loaded = c_avl_create ((void *) strcasecmp);
858 assert (plugins_loaded != NULL);
860 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
861 return (status == 0);
864 static int plugin_mark_loaded (char const *name)
869 name_copy = strdup (name);
870 if (name_copy == NULL)
873 status = c_avl_insert (plugins_loaded,
874 /* key = */ name_copy, /* value = */ NULL);
878 static void plugin_free_loaded ()
883 if (plugins_loaded == NULL)
886 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
889 assert (value == NULL);
892 c_avl_destroy (plugins_loaded);
893 plugins_loaded = NULL;
897 int plugin_load (char const *plugin_name, uint32_t flags)
901 char filename[BUFSIZE] = "";
902 char typename[BUFSIZE];
909 if (plugin_name == NULL)
912 /* Check if plugin is already loaded and don't do anything in this
914 if (plugin_is_loaded (plugin_name))
917 dir = plugin_get_dir ();
921 * XXX: Magic at work:
923 * Some of the language bindings, for example the Python and Perl
924 * plugins, need to be able to export symbols to the scripts they run.
925 * For this to happen, the "Globals" flag needs to be set.
926 * Unfortunately, this technical detail is hard to explain to the
927 * average user and she shouldn't have to worry about this, ideally.
928 * So in order to save everyone's sanity use a different default for a
929 * handful of special plugins. --octo
931 if ((strcasecmp ("perl", plugin_name) == 0)
932 || (strcasecmp ("python", plugin_name) == 0))
933 flags |= PLUGIN_FLAGS_GLOBAL;
935 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
936 * type when matching the filename */
937 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
938 if ((status < 0) || ((size_t) status >= sizeof (typename)))
940 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
943 typename_len = strlen (typename);
945 if ((dh = opendir (dir)) == NULL)
948 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
949 sstrerror (errno, errbuf, sizeof (errbuf)));
953 while ((de = readdir (dh)) != NULL)
955 if (strncasecmp (de->d_name, typename, typename_len))
958 status = ssnprintf (filename, sizeof (filename),
959 "%s/%s", dir, de->d_name);
960 if ((status < 0) || ((size_t) status >= sizeof (filename)))
962 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
967 if (lstat (filename, &statbuf) == -1)
970 WARNING ("plugin_load: stat (\"%s\") failed: %s",
972 sstrerror (errno, errbuf, sizeof (errbuf)));
975 else if (!S_ISREG (statbuf.st_mode))
977 /* don't follow symlinks */
978 WARNING ("plugin_load: %s is not a regular file.",
983 status = plugin_load_file (filename, flags);
987 plugin_mark_loaded (plugin_name);
993 ERROR ("plugin_load: Load plugin \"%s\" failed with "
994 "status %i.", plugin_name, status);
1000 if (filename[0] == 0)
1001 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1008 * The `register_*' functions follow
1010 int plugin_register_config (const char *name,
1011 int (*callback) (const char *key, const char *val),
1012 const char **keys, int keys_num)
1014 cf_register (name, callback, keys, keys_num);
1016 } /* int plugin_register_config */
1018 int plugin_register_complex_config (const char *type,
1019 int (*callback) (oconfig_item_t *))
1021 return (cf_register_complex (type, callback));
1022 } /* int plugin_register_complex_config */
1024 int plugin_register_init (const char *name,
1025 int (*callback) (void))
1027 return (create_register_callback (&list_init, name, (void *) callback,
1028 /* user_data = */ NULL));
1029 } /* plugin_register_init */
1031 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1033 const read_func_t *rf0;
1034 const read_func_t *rf1;
1039 if (rf0->rf_next_read < rf1->rf_next_read)
1041 else if (rf0->rf_next_read > rf1->rf_next_read)
1045 } /* int plugin_compare_read_func */
1047 /* Add a read function to both, the heap and a linked list. The linked list if
1048 * used to look-up read functions, especially for the remove function. The heap
1049 * is used to determine which plugin to read next. */
1050 static int plugin_insert_read (read_func_t *rf)
1055 rf->rf_next_read = cdtime ();
1056 rf->rf_effective_interval = rf->rf_interval;
1058 pthread_mutex_lock (&read_lock);
1060 if (read_list == NULL)
1062 read_list = llist_create ();
1063 if (read_list == NULL)
1065 pthread_mutex_unlock (&read_lock);
1066 ERROR ("plugin_insert_read: read_list failed.");
1071 if (read_heap == NULL)
1073 read_heap = c_heap_create (plugin_compare_read_func);
1074 if (read_heap == NULL)
1076 pthread_mutex_unlock (&read_lock);
1077 ERROR ("plugin_insert_read: c_heap_create failed.");
1082 le = llist_search (read_list, rf->rf_name);
1085 pthread_mutex_unlock (&read_lock);
1086 WARNING ("The read function \"%s\" is already registered. "
1087 "Check for duplicate \"LoadPlugin\" lines "
1088 "in your configuration!",
1093 le = llentry_create (rf->rf_name, rf);
1096 pthread_mutex_unlock (&read_lock);
1097 ERROR ("plugin_insert_read: llentry_create failed.");
1101 status = c_heap_insert (read_heap, rf);
1104 pthread_mutex_unlock (&read_lock);
1105 ERROR ("plugin_insert_read: c_heap_insert failed.");
1106 llentry_destroy (le);
1110 /* This does not fail. */
1111 llist_append (read_list, le);
1113 /* Wake up all the read threads. */
1114 pthread_cond_broadcast (&read_cond);
1115 pthread_mutex_unlock (&read_lock);
1117 } /* int plugin_insert_read */
1119 int plugin_register_read (const char *name,
1120 int (*callback) (void))
1125 rf = malloc (sizeof (*rf));
1128 ERROR ("plugin_register_read: malloc failed.");
1132 memset (rf, 0, sizeof (read_func_t));
1133 rf->rf_callback = (void *) callback;
1134 rf->rf_udata.data = NULL;
1135 rf->rf_udata.free_func = NULL;
1136 rf->rf_ctx = plugin_get_ctx ();
1137 rf->rf_group[0] = '\0';
1138 rf->rf_name = strdup (name);
1139 rf->rf_type = RF_SIMPLE;
1140 rf->rf_interval = plugin_get_interval ();
1142 status = plugin_insert_read (rf);
1147 } /* int plugin_register_read */
1149 int plugin_register_complex_read (const char *group, const char *name,
1150 plugin_read_cb callback,
1151 const struct timespec *interval,
1152 user_data_t *user_data)
1157 rf = malloc (sizeof (*rf));
1160 ERROR ("plugin_register_complex_read: malloc failed.");
1164 memset (rf, 0, sizeof (read_func_t));
1165 rf->rf_callback = (void *) callback;
1167 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1169 rf->rf_group[0] = '\0';
1170 rf->rf_name = strdup (name);
1171 rf->rf_type = RF_COMPLEX;
1172 if (interval != NULL)
1173 rf->rf_interval = TIMESPEC_TO_CDTIME_T (interval);
1175 rf->rf_interval = plugin_get_interval ();
1178 if (user_data == NULL)
1180 rf->rf_udata.data = NULL;
1181 rf->rf_udata.free_func = NULL;
1185 rf->rf_udata = *user_data;
1188 rf->rf_ctx = plugin_get_ctx ();
1190 status = plugin_insert_read (rf);
1195 } /* int plugin_register_complex_read */
1197 int plugin_register_write (const char *name,
1198 plugin_write_cb callback, user_data_t *ud)
1200 return (create_register_callback (&list_write, name,
1201 (void *) callback, ud));
1202 } /* int plugin_register_write */
1204 int plugin_register_flush (const char *name,
1205 plugin_flush_cb callback, user_data_t *ud)
1207 return (create_register_callback (&list_flush, name,
1208 (void *) callback, ud));
1209 } /* int plugin_register_flush */
1211 int plugin_register_missing (const char *name,
1212 plugin_missing_cb callback, user_data_t *ud)
1214 return (create_register_callback (&list_missing, name,
1215 (void *) callback, ud));
1216 } /* int plugin_register_missing */
1218 int plugin_register_shutdown (const char *name,
1219 int (*callback) (void))
1221 return (create_register_callback (&list_shutdown, name,
1222 (void *) callback, /* user_data = */ NULL));
1223 } /* int plugin_register_shutdown */
1225 static void plugin_free_data_sets (void)
1230 if (data_sets == NULL)
1233 while (c_avl_pick (data_sets, &key, &value) == 0)
1235 data_set_t *ds = value;
1236 /* key is a pointer to ds->type */
1242 c_avl_destroy (data_sets);
1244 } /* void plugin_free_data_sets */
1246 int plugin_register_data_set (const data_set_t *ds)
1248 data_set_t *ds_copy;
1251 if ((data_sets != NULL)
1252 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1254 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1255 plugin_unregister_data_set (ds->type);
1257 else if (data_sets == NULL)
1259 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1260 if (data_sets == NULL)
1264 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1265 if (ds_copy == NULL)
1267 memcpy(ds_copy, ds, sizeof (data_set_t));
1269 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1271 if (ds_copy->ds == NULL)
1277 for (i = 0; i < ds->ds_num; i++)
1278 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1280 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1281 } /* int plugin_register_data_set */
1283 int plugin_register_log (const char *name,
1284 plugin_log_cb callback, user_data_t *ud)
1286 return (create_register_callback (&list_log, name,
1287 (void *) callback, ud));
1288 } /* int plugin_register_log */
1290 int plugin_register_notification (const char *name,
1291 plugin_notification_cb callback, user_data_t *ud)
1293 return (create_register_callback (&list_notification, name,
1294 (void *) callback, ud));
1295 } /* int plugin_register_log */
1297 int plugin_unregister_config (const char *name)
1299 cf_unregister (name);
1301 } /* int plugin_unregister_config */
1303 int plugin_unregister_complex_config (const char *name)
1305 cf_unregister_complex (name);
1307 } /* int plugin_unregister_complex_config */
1309 int plugin_unregister_init (const char *name)
1311 return (plugin_unregister (list_init, name));
1314 int plugin_unregister_read (const char *name) /* {{{ */
1322 pthread_mutex_lock (&read_lock);
1324 if (read_list == NULL)
1326 pthread_mutex_unlock (&read_lock);
1330 le = llist_search (read_list, name);
1333 pthread_mutex_unlock (&read_lock);
1334 WARNING ("plugin_unregister_read: No such read function: %s",
1339 llist_remove (read_list, le);
1342 assert (rf != NULL);
1343 rf->rf_type = RF_REMOVE;
1345 pthread_mutex_unlock (&read_lock);
1347 llentry_destroy (le);
1349 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1352 } /* }}} int plugin_unregister_read */
1354 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1356 read_func_t *rf = e->value;
1359 return strcmp (rf->rf_group, (const char *)group);
1360 } /* }}} int compare_read_func_group */
1362 int plugin_unregister_read_group (const char *group) /* {{{ */
1372 pthread_mutex_lock (&read_lock);
1374 if (read_list == NULL)
1376 pthread_mutex_unlock (&read_lock);
1382 le = llist_search_custom (read_list,
1383 compare_read_func_group, (void *)group);
1390 llist_remove (read_list, le);
1393 assert (rf != NULL);
1394 rf->rf_type = RF_REMOVE;
1396 llentry_destroy (le);
1398 DEBUG ("plugin_unregister_read_group: "
1399 "Marked `%s' (group `%s') for removal.",
1400 rf->rf_name, group);
1403 pthread_mutex_unlock (&read_lock);
1407 WARNING ("plugin_unregister_read_group: No such "
1408 "group of read function: %s", group);
1413 } /* }}} int plugin_unregister_read_group */
1415 int plugin_unregister_write (const char *name)
1417 return (plugin_unregister (list_write, name));
1420 int plugin_unregister_flush (const char *name)
1422 return (plugin_unregister (list_flush, name));
1425 int plugin_unregister_missing (const char *name)
1427 return (plugin_unregister (list_missing, name));
1430 int plugin_unregister_shutdown (const char *name)
1432 return (plugin_unregister (list_shutdown, name));
1435 int plugin_unregister_data_set (const char *name)
1439 if (data_sets == NULL)
1442 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1449 } /* int plugin_unregister_data_set */
1451 int plugin_unregister_log (const char *name)
1453 return (plugin_unregister (list_log, name));
1456 int plugin_unregister_notification (const char *name)
1458 return (plugin_unregister (list_notification, name));
1461 void plugin_init_all (void)
1463 char const *chain_name;
1464 long write_threads_num;
1468 /* Init the value cache */
1471 chain_name = global_option_get ("PreCacheChain");
1472 pre_cache_chain = fc_chain_get_by_name (chain_name);
1474 chain_name = global_option_get ("PostCacheChain");
1475 post_cache_chain = fc_chain_get_by_name (chain_name);
1477 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1479 if (write_limit_high < 0)
1481 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1482 write_limit_high = 0;
1485 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1486 /* default = */ write_limit_high / 2);
1487 if (write_limit_low < 0)
1489 ERROR ("WriteQueueLimitLow must be positive or zero.");
1490 write_limit_low = write_limit_high / 2;
1492 else if (write_limit_low > write_limit_high)
1494 ERROR ("WriteQueueLimitLow must not be larger than "
1495 "WriteQueueLimitHigh.");
1496 write_limit_low = write_limit_high;
1499 write_threads_num = global_option_get_long ("WriteThreads",
1501 if (write_threads_num < 1)
1503 ERROR ("WriteThreads must be positive.");
1504 write_threads_num = 5;
1507 start_write_threads ((size_t) write_threads_num);
1509 if ((list_init == NULL) && (read_heap == NULL))
1512 /* Calling all init callbacks before checking if read callbacks
1513 * are available allows the init callbacks to register the read
1515 le = llist_head (list_init);
1518 callback_func_t *cf;
1519 plugin_init_cb callback;
1520 plugin_ctx_t old_ctx;
1523 old_ctx = plugin_set_ctx (cf->cf_ctx);
1524 callback = cf->cf_callback;
1525 status = (*callback) ();
1526 plugin_set_ctx (old_ctx);
1530 ERROR ("Initialization of plugin `%s' "
1531 "failed with status %i. "
1532 "Plugin will be unloaded.",
1534 /* Plugins that register read callbacks from the init
1535 * callback should take care of appropriate error
1536 * handling themselves. */
1537 /* FIXME: Unload _all_ functions */
1538 plugin_unregister_read (le->key);
1544 /* Start read-threads */
1545 if (read_heap != NULL)
1549 rt = global_option_get ("ReadThreads");
1552 start_read_threads ((num > 0) ? num : 5);
1554 } /* void plugin_init_all */
1556 /* TODO: Rename this function. */
1557 void plugin_read_all (void)
1559 uc_check_timeout ();
1562 } /* void plugin_read_all */
1564 /* Read function called when the `-T' command line argument is given. */
1565 int plugin_read_all_once (void)
1568 int return_status = 0;
1570 if (read_heap == NULL)
1572 NOTICE ("No read-functions are registered.");
1579 plugin_ctx_t old_ctx;
1581 rf = c_heap_get_root (read_heap);
1585 old_ctx = plugin_set_ctx (rf->rf_ctx);
1587 if (rf->rf_type == RF_SIMPLE)
1589 int (*callback) (void);
1591 callback = rf->rf_callback;
1592 status = (*callback) ();
1596 plugin_read_cb callback;
1598 callback = rf->rf_callback;
1599 status = (*callback) (&rf->rf_udata);
1602 plugin_set_ctx (old_ctx);
1606 NOTICE ("read-function of plugin `%s' failed.",
1611 destroy_callback ((void *) rf);
1614 return (return_status);
1615 } /* int plugin_read_all_once */
1617 int plugin_write (const char *plugin, /* {{{ */
1618 const data_set_t *ds, const value_list_t *vl)
1626 if (list_write == NULL)
1631 ds = plugin_get_ds (vl->type);
1634 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1644 le = llist_head (list_write);
1647 callback_func_t *cf = le->value;
1648 plugin_write_cb callback;
1650 /* do not switch plugin context; rather keep the context (interval)
1651 * information of the calling read plugin */
1653 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1654 callback = cf->cf_callback;
1655 status = (*callback) (ds, vl, &cf->cf_udata);
1664 if ((success == 0) && (failure != 0))
1669 else /* plugin != NULL */
1671 callback_func_t *cf;
1672 plugin_write_cb callback;
1674 le = llist_head (list_write);
1677 if (strcasecmp (plugin, le->key) == 0)
1688 /* do not switch plugin context; rather keep the context (interval)
1689 * information of the calling read plugin */
1691 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1692 callback = cf->cf_callback;
1693 status = (*callback) (ds, vl, &cf->cf_udata);
1697 } /* }}} int plugin_write */
1699 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1703 if (list_flush == NULL)
1706 le = llist_head (list_flush);
1709 callback_func_t *cf;
1710 plugin_flush_cb callback;
1711 plugin_ctx_t old_ctx;
1713 if ((plugin != NULL)
1714 && (strcmp (plugin, le->key) != 0))
1721 old_ctx = plugin_set_ctx (cf->cf_ctx);
1722 callback = cf->cf_callback;
1724 (*callback) (timeout, identifier, &cf->cf_udata);
1726 plugin_set_ctx (old_ctx);
1731 } /* int plugin_flush */
1733 void plugin_shutdown_all (void)
1737 stop_read_threads ();
1739 destroy_all_callbacks (&list_init);
1741 pthread_mutex_lock (&read_lock);
1742 llist_destroy (read_list);
1744 pthread_mutex_unlock (&read_lock);
1746 destroy_read_heap ();
1748 plugin_flush (/* plugin = */ NULL,
1750 /* identifier = */ NULL);
1753 if (list_shutdown != NULL)
1754 le = llist_head (list_shutdown);
1758 callback_func_t *cf;
1759 plugin_shutdown_cb callback;
1760 plugin_ctx_t old_ctx;
1763 old_ctx = plugin_set_ctx (cf->cf_ctx);
1764 callback = cf->cf_callback;
1766 /* Advance the pointer before calling the callback allows
1767 * shutdown functions to unregister themselves. If done the
1768 * other way around the memory `le' points to will be freed
1769 * after callback returns. */
1774 plugin_set_ctx (old_ctx);
1777 stop_write_threads ();
1779 /* Write plugins which use the `user_data' pointer usually need the
1780 * same data available to the flush callback. If this is the case, set
1781 * the free_function to NULL when registering the flush callback and to
1782 * the real free function when registering the write callback. This way
1783 * the data isn't freed twice. */
1784 destroy_all_callbacks (&list_flush);
1785 destroy_all_callbacks (&list_missing);
1786 destroy_all_callbacks (&list_write);
1788 destroy_all_callbacks (&list_notification);
1789 destroy_all_callbacks (&list_shutdown);
1790 destroy_all_callbacks (&list_log);
1792 plugin_free_loaded ();
1793 plugin_free_data_sets ();
1794 } /* void plugin_shutdown_all */
1796 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1800 if (list_missing == NULL)
1803 le = llist_head (list_missing);
1806 callback_func_t *cf;
1807 plugin_missing_cb callback;
1808 plugin_ctx_t old_ctx;
1812 old_ctx = plugin_set_ctx (cf->cf_ctx);
1813 callback = cf->cf_callback;
1815 status = (*callback) (vl, &cf->cf_udata);
1816 plugin_set_ctx (old_ctx);
1821 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1822 "failed with status %i.",
1835 } /* int }}} plugin_dispatch_missing */
1837 static int plugin_dispatch_values_internal (value_list_t *vl)
1840 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1842 value_t *saved_values;
1843 int saved_values_len;
1847 int free_meta_data = 0;
1849 if ((vl == NULL) || (vl->type[0] == 0)
1850 || (vl->values == NULL) || (vl->values_len < 1))
1852 ERROR ("plugin_dispatch_values: Invalid value list "
1853 "from plugin %s.", vl->plugin);
1857 /* Free meta data only if the calling function didn't specify any. In
1858 * this case matches and targets may add some and the calling function
1859 * may not expect (and therefore free) that data. */
1860 if (vl->meta == NULL)
1863 if (list_write == NULL)
1864 c_complain_once (LOG_WARNING, &no_write_complaint,
1865 "plugin_dispatch_values: No write callback has been "
1866 "registered. Please load at least one output plugin, "
1867 "if you want the collected data to be stored.");
1869 if (data_sets == NULL)
1871 ERROR ("plugin_dispatch_values: No data sets registered. "
1872 "Could the types database be read? Check "
1873 "your `TypesDB' setting!");
1877 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1879 char ident[6 * DATA_MAX_NAME_LEN];
1881 FORMAT_VL (ident, sizeof (ident), vl);
1882 INFO ("plugin_dispatch_values: Dataset not found: %s "
1883 "(from \"%s\"), check your types.db!",
1888 /* Assured by plugin_value_list_clone(). The time is determined at
1889 * _enqueue_ time. */
1890 assert (vl->time != 0);
1891 assert (vl->interval != 0);
1893 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1895 "plugin = %s; plugin_instance = %s; "
1896 "type = %s; type_instance = %s;",
1897 CDTIME_T_TO_DOUBLE (vl->time),
1898 CDTIME_T_TO_DOUBLE (vl->interval),
1900 vl->plugin, vl->plugin_instance,
1901 vl->type, vl->type_instance);
1904 assert (0 == strcmp (ds->type, vl->type));
1906 if (0 != strcmp (ds->type, vl->type))
1907 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1908 ds->type, vl->type);
1912 assert (ds->ds_num == vl->values_len);
1914 if (ds->ds_num != vl->values_len)
1916 ERROR ("plugin_dispatch_values: ds->type = %s: "
1917 "(ds->ds_num = %i) != "
1918 "(vl->values_len = %i)",
1919 ds->type, ds->ds_num, vl->values_len);
1924 escape_slashes (vl->host, sizeof (vl->host));
1925 escape_slashes (vl->plugin, sizeof (vl->plugin));
1926 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1927 escape_slashes (vl->type, sizeof (vl->type));
1928 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1930 /* Copy the values. This way, we can assure `targets' that they get
1931 * dynamically allocated values, which they can free and replace if
1933 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1935 saved_values = vl->values;
1936 saved_values_len = vl->values_len;
1938 vl->values = (value_t *) calloc (vl->values_len,
1939 sizeof (*vl->values));
1940 if (vl->values == NULL)
1942 ERROR ("plugin_dispatch_values: calloc failed.");
1943 vl->values = saved_values;
1946 memcpy (vl->values, saved_values,
1947 vl->values_len * sizeof (*vl->values));
1949 else /* if ((pre == NULL) && (post == NULL)) */
1951 saved_values = NULL;
1952 saved_values_len = 0;
1955 if (pre_cache_chain != NULL)
1957 status = fc_process_chain (ds, vl, pre_cache_chain);
1960 WARNING ("plugin_dispatch_values: Running the "
1961 "pre-cache chain failed with "
1965 else if (status == FC_TARGET_STOP)
1967 /* Restore the state of the value_list so that plugins
1968 * don't get confused.. */
1969 if (saved_values != NULL)
1972 vl->values = saved_values;
1973 vl->values_len = saved_values_len;
1979 /* Update the value cache */
1982 if (post_cache_chain != NULL)
1984 status = fc_process_chain (ds, vl, post_cache_chain);
1987 WARNING ("plugin_dispatch_values: Running the "
1988 "post-cache chain failed with "
1994 fc_default_action (ds, vl);
1996 /* Restore the state of the value_list so that plugins don't get
1998 if (saved_values != NULL)
2001 vl->values = saved_values;
2002 vl->values_len = saved_values_len;
2005 if ((free_meta_data != 0) && (vl->meta != NULL))
2007 meta_data_destroy (vl->meta);
2012 } /* int plugin_dispatch_values_internal */
2014 static double get_drop_probability (void) /* {{{ */
2020 pthread_mutex_lock (&write_lock);
2021 wql = write_queue_length;
2022 pthread_mutex_unlock (&write_lock);
2024 if (wql < write_limit_low)
2026 if (wql >= write_limit_high)
2029 pos = 1 + wql - write_limit_low;
2030 size = 1 + write_limit_high - write_limit_low;
2032 return (((double) pos) / ((double) size));
2033 } /* }}} double get_drop_probability */
2035 static _Bool check_drop_value (void) /* {{{ */
2037 static cdtime_t last_message_time = 0;
2038 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2044 if (write_limit_high == 0)
2047 p = get_drop_probability ();
2051 status = pthread_mutex_trylock (&last_message_lock);
2057 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2059 last_message_time = now;
2060 ERROR ("plugin_dispatch_values: Low water mark "
2061 "reached. Dropping %.0f%% of metrics.",
2064 pthread_mutex_unlock (&last_message_lock);
2075 } /* }}} _Bool check_drop_value */
2077 int plugin_dispatch_values (value_list_t const *vl)
2081 if (check_drop_value ())
2084 status = plugin_write_enqueue (vl);
2088 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2089 "with status %i (%s).", status,
2090 sstrerror (status, errbuf, sizeof (errbuf)));
2097 __attribute__((sentinel))
2098 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2099 _Bool store_percentage, ...)
2106 assert (template->values_len == 1);
2108 va_start (ap, store_percentage);
2114 name = va_arg (ap, char const *);
2118 value = va_arg (ap, gauge_t);
2124 vl = plugin_value_list_clone (template);
2125 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2126 if (store_percentage)
2127 sstrncpy (vl->type, "percent", sizeof (vl->type));
2129 va_start (ap, store_percentage);
2135 /* Set the type instance. */
2136 name = va_arg (ap, char const *);
2139 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2141 /* Set the value. */
2142 vl->values[0].gauge = va_arg (ap, gauge_t);
2143 if (store_percentage)
2144 vl->values[0].gauge *= 100.0 / sum;
2146 status = plugin_write_enqueue (vl);
2152 plugin_value_list_free (vl);
2154 } /* }}} int plugin_dispatch_multivalue */
2156 int plugin_dispatch_notification (const notification_t *notif)
2159 /* Possible TODO: Add flap detection here */
2161 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2162 "time = %.3f; host = %s;",
2163 notif->severity, notif->message,
2164 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2166 /* Nobody cares for notifications */
2167 if (list_notification == NULL)
2170 le = llist_head (list_notification);
2173 callback_func_t *cf;
2174 plugin_notification_cb callback;
2177 /* do not switch plugin context; rather keep the context
2178 * (interval) information of the calling plugin */
2181 callback = cf->cf_callback;
2182 status = (*callback) (notif, &cf->cf_udata);
2185 WARNING ("plugin_dispatch_notification: Notification "
2186 "callback %s returned %i.",
2194 } /* int plugin_dispatch_notification */
2196 void plugin_log (int level, const char *format, ...)
2203 if (level >= LOG_DEBUG)
2207 va_start (ap, format);
2208 vsnprintf (msg, sizeof (msg), format, ap);
2209 msg[sizeof (msg) - 1] = '\0';
2212 if (list_log == NULL)
2214 fprintf (stderr, "%s\n", msg);
2218 le = llist_head (list_log);
2221 callback_func_t *cf;
2222 plugin_log_cb callback;
2225 callback = cf->cf_callback;
2227 /* do not switch plugin context; rather keep the context
2228 * (interval) information of the calling plugin */
2230 (*callback) (level, msg, &cf->cf_udata);
2234 } /* void plugin_log */
2236 int parse_log_severity (const char *severity)
2240 if ((0 == strcasecmp (severity, "emerg"))
2241 || (0 == strcasecmp (severity, "alert"))
2242 || (0 == strcasecmp (severity, "crit"))
2243 || (0 == strcasecmp (severity, "err")))
2244 log_level = LOG_ERR;
2245 else if (0 == strcasecmp (severity, "warning"))
2246 log_level = LOG_WARNING;
2247 else if (0 == strcasecmp (severity, "notice"))
2248 log_level = LOG_NOTICE;
2249 else if (0 == strcasecmp (severity, "info"))
2250 log_level = LOG_INFO;
2252 else if (0 == strcasecmp (severity, "debug"))
2253 log_level = LOG_DEBUG;
2254 #endif /* COLLECT_DEBUG */
2257 } /* int parse_log_severity */
2259 int parse_notif_severity (const char *severity)
2261 int notif_severity = -1;
2263 if (strcasecmp (severity, "FAILURE") == 0)
2264 notif_severity = NOTIF_FAILURE;
2265 else if (strcmp (severity, "OKAY") == 0)
2266 notif_severity = NOTIF_OKAY;
2267 else if ((strcmp (severity, "WARNING") == 0)
2268 || (strcmp (severity, "WARN") == 0))
2269 notif_severity = NOTIF_WARNING;
2271 return (notif_severity);
2272 } /* int parse_notif_severity */
2274 const data_set_t *plugin_get_ds (const char *name)
2278 if (data_sets == NULL)
2280 ERROR ("plugin_get_ds: No data sets are defined yet.");
2284 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2286 DEBUG ("No such dataset registered: %s", name);
2291 } /* data_set_t *plugin_get_ds */
2293 static int plugin_notification_meta_add (notification_t *n,
2295 enum notification_meta_type_e type,
2298 notification_meta_t *meta;
2299 notification_meta_t *tail;
2301 if ((n == NULL) || (name == NULL) || (value == NULL))
2303 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2307 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2310 ERROR ("plugin_notification_meta_add: malloc failed.");
2313 memset (meta, 0, sizeof (notification_meta_t));
2315 sstrncpy (meta->name, name, sizeof (meta->name));
2320 case NM_TYPE_STRING:
2322 meta->nm_value.nm_string = strdup ((const char *) value);
2323 if (meta->nm_value.nm_string == NULL)
2325 ERROR ("plugin_notification_meta_add: strdup failed.");
2331 case NM_TYPE_SIGNED_INT:
2333 meta->nm_value.nm_signed_int = *((int64_t *) value);
2336 case NM_TYPE_UNSIGNED_INT:
2338 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2341 case NM_TYPE_DOUBLE:
2343 meta->nm_value.nm_double = *((double *) value);
2346 case NM_TYPE_BOOLEAN:
2348 meta->nm_value.nm_boolean = *((_Bool *) value);
2353 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2357 } /* switch (type) */
2361 while ((tail != NULL) && (tail->next != NULL))
2370 } /* int plugin_notification_meta_add */
2372 int plugin_notification_meta_add_string (notification_t *n,
2376 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2379 int plugin_notification_meta_add_signed_int (notification_t *n,
2383 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2386 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2390 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2393 int plugin_notification_meta_add_double (notification_t *n,
2397 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2400 int plugin_notification_meta_add_boolean (notification_t *n,
2404 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2407 int plugin_notification_meta_copy (notification_t *dst,
2408 const notification_t *src)
2410 notification_meta_t *meta;
2412 assert (dst != NULL);
2413 assert (src != NULL);
2414 assert (dst != src);
2415 assert ((src->meta == NULL) || (src->meta != dst->meta));
2417 for (meta = src->meta; meta != NULL; meta = meta->next)
2419 if (meta->type == NM_TYPE_STRING)
2420 plugin_notification_meta_add_string (dst, meta->name,
2421 meta->nm_value.nm_string);
2422 else if (meta->type == NM_TYPE_SIGNED_INT)
2423 plugin_notification_meta_add_signed_int (dst, meta->name,
2424 meta->nm_value.nm_signed_int);
2425 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2426 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2427 meta->nm_value.nm_unsigned_int);
2428 else if (meta->type == NM_TYPE_DOUBLE)
2429 plugin_notification_meta_add_double (dst, meta->name,
2430 meta->nm_value.nm_double);
2431 else if (meta->type == NM_TYPE_BOOLEAN)
2432 plugin_notification_meta_add_boolean (dst, meta->name,
2433 meta->nm_value.nm_boolean);
2437 } /* int plugin_notification_meta_copy */
2439 int plugin_notification_meta_free (notification_meta_t *n)
2441 notification_meta_t *this;
2442 notification_meta_t *next;
2446 ERROR ("plugin_notification_meta_free: n == NULL!");
2451 while (this != NULL)
2455 if (this->type == NM_TYPE_STRING)
2457 free ((char *)this->nm_value.nm_string);
2458 this->nm_value.nm_string = NULL;
2466 } /* int plugin_notification_meta_free */
2468 static void plugin_ctx_destructor (void *ctx)
2471 } /* void plugin_ctx_destructor */
2473 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2475 static plugin_ctx_t *plugin_ctx_create (void)
2479 ctx = malloc (sizeof (*ctx));
2482 ERROR ("Failed to allocate plugin context: %s",
2483 sstrerror (errno, errbuf, sizeof (errbuf)));
2488 assert (plugin_ctx_key_initialized);
2489 pthread_setspecific (plugin_ctx_key, ctx);
2490 DEBUG("Created new plugin context.");
2492 } /* int plugin_ctx_create */
2494 void plugin_init_ctx (void)
2496 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2497 plugin_ctx_key_initialized = 1;
2498 } /* void plugin_init_ctx */
2500 plugin_ctx_t plugin_get_ctx (void)
2504 assert (plugin_ctx_key_initialized);
2505 ctx = pthread_getspecific (plugin_ctx_key);
2508 ctx = plugin_ctx_create ();
2509 /* this must no happen -- exit() instead? */
2515 } /* plugin_ctx_t plugin_get_ctx */
2517 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2522 assert (plugin_ctx_key_initialized);
2523 c = pthread_getspecific (plugin_ctx_key);
2526 c = plugin_ctx_create ();
2527 /* this must no happen -- exit() instead? */
2536 } /* void plugin_set_ctx */
2538 cdtime_t plugin_get_interval (void)
2542 interval = plugin_get_ctx().interval;
2546 return cf_get_default_interval ();
2547 } /* cdtime_t plugin_get_interval */
2551 void *(*start_routine) (void *);
2555 static void *plugin_thread_start (void *arg)
2557 plugin_thread_t *plugin_thread = arg;
2559 void *(*start_routine) (void *) = plugin_thread->start_routine;
2560 void *plugin_arg = plugin_thread->arg;
2562 plugin_set_ctx (plugin_thread->ctx);
2564 free (plugin_thread);
2566 return start_routine (plugin_arg);
2567 } /* void *plugin_thread_start */
2569 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2570 void *(*start_routine) (void *), void *arg)
2572 plugin_thread_t *plugin_thread;
2574 plugin_thread = malloc (sizeof (*plugin_thread));
2575 if (plugin_thread == NULL)
2578 plugin_thread->ctx = plugin_get_ctx ();
2579 plugin_thread->start_routine = start_routine;
2580 plugin_thread->arg = arg;
2582 return pthread_create (thread, attr,
2583 plugin_thread_start, plugin_thread);
2584 } /* int plugin_thread_create */
2586 /* vim: set sw=8 ts=8 noet fdm=marker : */