1 /**
2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2014 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian Harl <sh at tokkee.org>
26 **/
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
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"
41 #include <ltdl.h>
43 /*
44 * Private structures
45 */
46 struct callback_func_s
47 {
48 void *cf_callback;
49 user_data_t cf_udata;
50 plugin_ctx_t cf_ctx;
51 };
52 typedef struct callback_func_s callback_func_t;
54 #define RF_SIMPLE 0
55 #define RF_COMPLEX 1
56 #define RF_REMOVE 65535
57 struct read_func_s
58 {
59 /* `read_func_t' "inherits" from `callback_func_t'.
60 * The `rf_super' member MUST be the first one in this structure! */
61 #define rf_callback rf_super.cf_callback
62 #define rf_udata rf_super.cf_udata
63 #define rf_ctx rf_super.cf_ctx
64 callback_func_t rf_super;
65 char rf_group[DATA_MAX_NAME_LEN];
66 char *rf_name;
67 int rf_type;
68 cdtime_t rf_interval;
69 cdtime_t rf_effective_interval;
70 cdtime_t rf_next_read;
71 };
72 typedef struct read_func_s read_func_t;
74 struct write_queue_s;
75 typedef struct write_queue_s write_queue_t;
76 struct write_queue_s
77 {
78 value_list_t *vl;
79 plugin_ctx_t ctx;
80 write_queue_t *next;
81 };
83 /*
84 * Private variables
85 */
86 static c_avl_tree_t *plugins_loaded = NULL;
88 static llist_t *list_init;
89 static llist_t *list_write;
90 static llist_t *list_flush;
91 static llist_t *list_missing;
92 static llist_t *list_shutdown;
93 static llist_t *list_log;
94 static llist_t *list_notification;
96 static fc_chain_t *pre_cache_chain = NULL;
97 static fc_chain_t *post_cache_chain = NULL;
99 static c_avl_tree_t *data_sets;
101 static char *plugindir = NULL;
103 #ifndef DEFAULT_MAX_READ_INTERVAL
104 # define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T (86400)
105 #endif
106 static c_heap_t *read_heap = NULL;
107 static llist_t *read_list;
108 static int read_loop = 1;
109 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
110 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
111 static pthread_t *read_threads = NULL;
112 static int read_threads_num = 0;
113 static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL;
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;
130 static derive_t stats_values_dropped = 0;
131 static _Bool record_statistics = 0;
133 /*
134 * Static functions
135 */
136 static int plugin_dispatch_values_internal (value_list_t *vl);
138 static const char *plugin_get_dir (void)
139 {
140 if (plugindir == NULL)
141 return (PLUGINDIR);
142 else
143 return (plugindir);
144 }
146 static void plugin_update_internal_statistics (void) { /* {{{ */
147 derive_t copy_write_queue_length;
148 value_list_t vl = VALUE_LIST_INIT;
149 value_t values[2];
151 copy_write_queue_length = write_queue_length;
153 /* Initialize `vl' */
154 vl.values = values;
155 vl.values_len = 2;
156 vl.time = 0;
157 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
158 sstrncpy (vl.plugin, "collectd", sizeof (vl.plugin));
160 vl.type_instance[0] = 0;
161 vl.values_len = 1;
163 /* Write queue */
164 sstrncpy (vl.plugin_instance, "write_queue",
165 sizeof (vl.plugin_instance));
167 /* Write queue : queue length */
168 vl.values[0].gauge = (gauge_t) copy_write_queue_length;
169 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
170 vl.type_instance[0] = 0;
171 plugin_dispatch_values (&vl);
173 /* Write queue : Values dropped (queue length > low limit) */
174 vl.values[0].derive = (derive_t) stats_values_dropped;
175 sstrncpy (vl.type, "derive", sizeof (vl.type));
176 sstrncpy (vl.type_instance, "dropped", sizeof (vl.type_instance));
177 plugin_dispatch_values (&vl);
179 /* Cache */
180 sstrncpy (vl.plugin_instance, "cache",
181 sizeof (vl.plugin_instance));
183 /* Cache : Nb entry in cache tree */
184 vl.values[0].gauge = (gauge_t) uc_get_size();
185 sstrncpy (vl.type, "cache_size", sizeof (vl.type));
186 vl.type_instance[0] = 0;
187 plugin_dispatch_values (&vl);
189 return;
190 } /* }}} void plugin_update_internal_statistics */
192 static void destroy_callback (callback_func_t *cf) /* {{{ */
193 {
194 if (cf == NULL)
195 return;
197 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
198 {
199 cf->cf_udata.free_func (cf->cf_udata.data);
200 cf->cf_udata.data = NULL;
201 cf->cf_udata.free_func = NULL;
202 }
203 sfree (cf);
204 } /* }}} void destroy_callback */
206 static void destroy_all_callbacks (llist_t **list) /* {{{ */
207 {
208 llentry_t *le;
210 if (*list == NULL)
211 return;
213 le = llist_head (*list);
214 while (le != NULL)
215 {
216 llentry_t *le_next;
218 le_next = le->next;
220 sfree (le->key);
221 destroy_callback (le->value);
222 le->value = NULL;
224 le = le_next;
225 }
227 llist_destroy (*list);
228 *list = NULL;
229 } /* }}} void destroy_all_callbacks */
231 static void destroy_read_heap (void) /* {{{ */
232 {
233 if (read_heap == NULL)
234 return;
236 while (42)
237 {
238 callback_func_t *cf;
240 cf = c_heap_get_root (read_heap);
241 if (cf == NULL)
242 break;
244 destroy_callback (cf);
245 }
247 c_heap_destroy (read_heap);
248 read_heap = NULL;
249 } /* }}} void destroy_read_heap */
251 static int register_callback (llist_t **list, /* {{{ */
252 const char *name, callback_func_t *cf)
253 {
254 llentry_t *le;
255 char *key;
257 if (*list == NULL)
258 {
259 *list = llist_create ();
260 if (*list == NULL)
261 {
262 ERROR ("plugin: register_callback: "
263 "llist_create failed.");
264 destroy_callback (cf);
265 return (-1);
266 }
267 }
269 key = strdup (name);
270 if (key == NULL)
271 {
272 ERROR ("plugin: register_callback: strdup failed.");
273 destroy_callback (cf);
274 return (-1);
275 }
277 le = llist_search (*list, name);
278 if (le == NULL)
279 {
280 le = llentry_create (key, cf);
281 if (le == NULL)
282 {
283 ERROR ("plugin: register_callback: "
284 "llentry_create failed.");
285 free (key);
286 destroy_callback (cf);
287 return (-1);
288 }
290 llist_append (*list, le);
291 }
292 else
293 {
294 callback_func_t *old_cf;
296 old_cf = le->value;
297 le->value = cf;
299 WARNING ("plugin: register_callback: "
300 "a callback named `%s' already exists - "
301 "overwriting the old entry!", name);
303 destroy_callback (old_cf);
304 sfree (key);
305 }
307 return (0);
308 } /* }}} int register_callback */
310 static int create_register_callback (llist_t **list, /* {{{ */
311 const char *name, void *callback, user_data_t *ud)
312 {
313 callback_func_t *cf;
315 cf = (callback_func_t *) malloc (sizeof (*cf));
316 if (cf == NULL)
317 {
318 ERROR ("plugin: create_register_callback: malloc failed.");
319 return (-1);
320 }
321 memset (cf, 0, sizeof (*cf));
323 cf->cf_callback = callback;
324 if (ud == NULL)
325 {
326 cf->cf_udata.data = NULL;
327 cf->cf_udata.free_func = NULL;
328 }
329 else
330 {
331 cf->cf_udata = *ud;
332 }
334 cf->cf_ctx = plugin_get_ctx ();
336 return (register_callback (list, name, cf));
337 } /* }}} int create_register_callback */
339 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
340 {
341 llentry_t *e;
343 if (list == NULL)
344 return (-1);
346 e = llist_search (list, name);
347 if (e == NULL)
348 return (-1);
350 llist_remove (list, e);
352 sfree (e->key);
353 destroy_callback (e->value);
355 llentry_destroy (e);
357 return (0);
358 } /* }}} int plugin_unregister */
360 /*
361 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
362 * object, but it will bitch about a shared object not having a
363 * ``module_register'' symbol..
364 */
365 static int plugin_load_file (char *file, uint32_t flags)
366 {
367 lt_dlhandle dlh;
368 void (*reg_handle) (void);
370 lt_dlinit ();
371 lt_dlerror (); /* clear errors */
373 #if LIBTOOL_VERSION == 2
374 if (flags & PLUGIN_FLAGS_GLOBAL) {
375 lt_dladvise advise;
376 lt_dladvise_init(&advise);
377 lt_dladvise_global(&advise);
378 dlh = lt_dlopenadvise(file, advise);
379 lt_dladvise_destroy(&advise);
380 } else {
381 dlh = lt_dlopen (file);
382 }
383 #else /* if LIBTOOL_VERSION == 1 */
384 if (flags & PLUGIN_FLAGS_GLOBAL)
385 WARNING ("plugin_load_file: The global flag is not supported, "
386 "libtool 2 is required for this.");
387 dlh = lt_dlopen (file);
388 #endif
390 if (dlh == NULL)
391 {
392 char errbuf[1024] = "";
394 ssnprintf (errbuf, sizeof (errbuf),
395 "lt_dlopen (\"%s\") failed: %s. "
396 "The most common cause for this problem is "
397 "missing dependencies. Use ldd(1) to check "
398 "the dependencies of the plugin "
399 "/ shared object.",
400 file, lt_dlerror ());
402 ERROR ("%s", errbuf);
403 /* Make sure this is printed to STDERR in any case, but also
404 * make sure it's printed only once. */
405 if (list_log != NULL)
406 fprintf (stderr, "ERROR: %s\n", errbuf);
408 return (1);
409 }
411 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
412 {
413 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
414 file, lt_dlerror ());
415 lt_dlclose (dlh);
416 return (-1);
417 }
419 (*reg_handle) ();
421 return (0);
422 }
424 static void *plugin_read_thread (void __attribute__((unused)) *args)
425 {
426 while (read_loop != 0)
427 {
428 read_func_t *rf;
429 plugin_ctx_t old_ctx;
430 cdtime_t now;
431 int status;
432 int rf_type;
433 int rc;
435 /* Get the read function that needs to be read next.
436 * We don't need to hold "read_lock" for the heap, but we need
437 * to call c_heap_get_root() and pthread_cond_wait() in the
438 * same protected block. */
439 pthread_mutex_lock (&read_lock);
440 rf = c_heap_get_root (read_heap);
441 if (rf == NULL)
442 {
443 pthread_cond_wait (&read_cond, &read_lock);
444 pthread_mutex_unlock (&read_lock);
445 continue;
446 }
447 pthread_mutex_unlock (&read_lock);
449 if (rf->rf_interval == 0)
450 {
451 /* this should not happen, because the interval is set
452 * for each plugin when loading it
453 * XXX: issue a warning? */
454 rf->rf_interval = plugin_get_interval ();
455 rf->rf_effective_interval = rf->rf_interval;
457 rf->rf_next_read = cdtime ();
458 }
460 /* sleep until this entry is due,
461 * using pthread_cond_timedwait */
462 pthread_mutex_lock (&read_lock);
463 /* In pthread_cond_timedwait, spurious wakeups are possible
464 * (and really happen, at least on NetBSD with > 1 CPU), thus
465 * we need to re-evaluate the condition every time
466 * pthread_cond_timedwait returns. */
467 rc = 0;
468 while ((read_loop != 0)
469 && (cdtime () < rf->rf_next_read)
470 && rc == 0)
471 {
472 struct timespec ts = { 0 };
474 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
476 rc = pthread_cond_timedwait (&read_cond, &read_lock,
477 &ts);
478 }
480 /* Must hold `read_lock' when accessing `rf->rf_type'. */
481 rf_type = rf->rf_type;
482 pthread_mutex_unlock (&read_lock);
484 /* Check if we're supposed to stop.. This may have interrupted
485 * the sleep, too. */
486 if (read_loop == 0)
487 {
488 /* Insert `rf' again, so it can be free'd correctly */
489 c_heap_insert (read_heap, rf);
490 break;
491 }
493 /* The entry has been marked for deletion. The linked list
494 * entry has already been removed by `plugin_unregister_read'.
495 * All we have to do here is free the `read_func_t' and
496 * continue. */
497 if (rf_type == RF_REMOVE)
498 {
499 DEBUG ("plugin_read_thread: Destroying the `%s' "
500 "callback.", rf->rf_name);
501 sfree (rf->rf_name);
502 destroy_callback ((callback_func_t *) rf);
503 rf = NULL;
504 continue;
505 }
507 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
509 old_ctx = plugin_set_ctx (rf->rf_ctx);
511 if (rf_type == RF_SIMPLE)
512 {
513 int (*callback) (void);
515 callback = rf->rf_callback;
516 status = (*callback) ();
517 }
518 else
519 {
520 plugin_read_cb callback;
522 assert (rf_type == RF_COMPLEX);
524 callback = rf->rf_callback;
525 status = (*callback) (&rf->rf_udata);
526 }
528 plugin_set_ctx (old_ctx);
530 /* If the function signals failure, we will increase the
531 * intervals in which it will be called. */
532 if (status != 0)
533 {
534 rf->rf_effective_interval *= 2;
535 if (rf->rf_effective_interval > max_read_interval)
536 rf->rf_effective_interval = max_read_interval;
538 NOTICE ("read-function of plugin `%s' failed. "
539 "Will suspend it for %.3f seconds.",
540 rf->rf_name,
541 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
542 }
543 else
544 {
545 /* Success: Restore the interval, if it was changed. */
546 rf->rf_effective_interval = rf->rf_interval;
547 }
549 /* update the ``next read due'' field */
550 now = cdtime ();
552 DEBUG ("plugin_read_thread: Effective interval of the "
553 "%s plugin is %.3f seconds.",
554 rf->rf_name,
555 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
557 /* Calculate the next (absolute) time at which this function
558 * should be called. */
559 rf->rf_next_read += rf->rf_effective_interval;
561 /* Check, if `rf_next_read' is in the past. */
562 if (rf->rf_next_read < now)
563 {
564 /* `rf_next_read' is in the past. Insert `now'
565 * so this value doesn't trail off into the
566 * past too much. */
567 rf->rf_next_read = now;
568 }
570 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
571 rf->rf_name,
572 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
574 /* Re-insert this read function into the heap again. */
575 c_heap_insert (read_heap, rf);
576 } /* while (read_loop) */
578 pthread_exit (NULL);
579 return ((void *) 0);
580 } /* void *plugin_read_thread */
582 static void start_read_threads (int num)
583 {
584 int i;
586 if (read_threads != NULL)
587 return;
589 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
590 if (read_threads == NULL)
591 {
592 ERROR ("plugin: start_read_threads: calloc failed.");
593 return;
594 }
596 read_threads_num = 0;
597 for (i = 0; i < num; i++)
598 {
599 if (pthread_create (read_threads + read_threads_num, NULL,
600 plugin_read_thread, NULL) == 0)
601 {
602 read_threads_num++;
603 }
604 else
605 {
606 ERROR ("plugin: start_read_threads: pthread_create failed.");
607 return;
608 }
609 } /* for (i) */
610 } /* void start_read_threads */
612 static void stop_read_threads (void)
613 {
614 int i;
616 if (read_threads == NULL)
617 return;
619 INFO ("collectd: Stopping %i read threads.", read_threads_num);
621 pthread_mutex_lock (&read_lock);
622 read_loop = 0;
623 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
624 pthread_cond_broadcast (&read_cond);
625 pthread_mutex_unlock (&read_lock);
627 for (i = 0; i < read_threads_num; i++)
628 {
629 if (pthread_join (read_threads[i], NULL) != 0)
630 {
631 ERROR ("plugin: stop_read_threads: pthread_join failed.");
632 }
633 read_threads[i] = (pthread_t) 0;
634 }
635 sfree (read_threads);
636 read_threads_num = 0;
637 } /* void stop_read_threads */
639 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
640 {
641 if (vl == NULL)
642 return;
644 meta_data_destroy (vl->meta);
645 sfree (vl->values);
646 sfree (vl);
647 } /* }}} void plugin_value_list_free */
649 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
650 {
651 value_list_t *vl;
653 if (vl_orig == NULL)
654 return (NULL);
656 vl = malloc (sizeof (*vl));
657 if (vl == NULL)
658 return (NULL);
659 memcpy (vl, vl_orig, sizeof (*vl));
661 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
662 if (vl->values == NULL)
663 {
664 plugin_value_list_free (vl);
665 return (NULL);
666 }
667 memcpy (vl->values, vl_orig->values,
668 vl_orig->values_len * sizeof (*vl->values));
670 vl->meta = meta_data_clone (vl->meta);
671 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
672 {
673 plugin_value_list_free (vl);
674 return (NULL);
675 }
677 if (vl->time == 0)
678 vl->time = cdtime ();
680 /* Fill in the interval from the thread context, if it is zero. */
681 if (vl->interval == 0)
682 {
683 plugin_ctx_t ctx = plugin_get_ctx ();
685 if (ctx.interval != 0)
686 vl->interval = ctx.interval;
687 else
688 {
689 char name[6 * DATA_MAX_NAME_LEN];
690 FORMAT_VL (name, sizeof (name), vl);
691 ERROR ("plugin_value_list_clone: Unable to determine "
692 "interval from context for "
693 "value list \"%s\". "
694 "This indicates a broken plugin. "
695 "Please report this problem to the "
696 "collectd mailing list or at "
697 "<http://collectd.org/bugs/>.", name);
698 vl->interval = cf_get_default_interval ();
699 }
700 }
702 return (vl);
703 } /* }}} value_list_t *plugin_value_list_clone */
705 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
706 {
707 write_queue_t *q;
709 q = malloc (sizeof (*q));
710 if (q == NULL)
711 return (ENOMEM);
712 q->next = NULL;
714 q->vl = plugin_value_list_clone (vl);
715 if (q->vl == NULL)
716 {
717 sfree (q);
718 return (ENOMEM);
719 }
721 /* Store context of caller (read plugin); otherwise, it would not be
722 * available to the write plugins when actually dispatching the
723 * value-list later on. */
724 q->ctx = plugin_get_ctx ();
726 pthread_mutex_lock (&write_lock);
728 if (write_queue_tail == NULL)
729 {
730 write_queue_head = q;
731 write_queue_tail = q;
732 write_queue_length = 1;
733 }
734 else
735 {
736 write_queue_tail->next = q;
737 write_queue_tail = q;
738 write_queue_length += 1;
739 }
741 pthread_cond_signal (&write_cond);
742 pthread_mutex_unlock (&write_lock);
744 return (0);
745 } /* }}} int plugin_write_enqueue */
747 static value_list_t *plugin_write_dequeue (void) /* {{{ */
748 {
749 write_queue_t *q;
750 value_list_t *vl;
752 pthread_mutex_lock (&write_lock);
754 while (write_loop && (write_queue_head == NULL))
755 pthread_cond_wait (&write_cond, &write_lock);
757 if (write_queue_head == NULL)
758 {
759 pthread_mutex_unlock (&write_lock);
760 return (NULL);
761 }
763 q = write_queue_head;
764 write_queue_head = q->next;
765 write_queue_length -= 1;
766 if (write_queue_head == NULL) {
767 write_queue_tail = NULL;
768 assert(0 == write_queue_length);
769 }
771 pthread_mutex_unlock (&write_lock);
773 (void) plugin_set_ctx (q->ctx);
775 vl = q->vl;
776 sfree (q);
777 return (vl);
778 } /* }}} value_list_t *plugin_write_dequeue */
780 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
781 {
782 while (write_loop)
783 {
784 value_list_t *vl = plugin_write_dequeue ();
785 if (vl == NULL)
786 continue;
788 plugin_dispatch_values_internal (vl);
790 plugin_value_list_free (vl);
791 }
793 pthread_exit (NULL);
794 return ((void *) 0);
795 } /* }}} void *plugin_write_thread */
797 static void start_write_threads (size_t num) /* {{{ */
798 {
799 size_t i;
801 if (write_threads != NULL)
802 return;
804 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
805 if (write_threads == NULL)
806 {
807 ERROR ("plugin: start_write_threads: calloc failed.");
808 return;
809 }
811 write_threads_num = 0;
812 for (i = 0; i < num; i++)
813 {
814 int status;
816 status = pthread_create (write_threads + write_threads_num,
817 /* attr = */ NULL,
818 plugin_write_thread,
819 /* arg = */ NULL);
820 if (status != 0)
821 {
822 char errbuf[1024];
823 ERROR ("plugin: start_write_threads: pthread_create failed "
824 "with status %i (%s).", status,
825 sstrerror (status, errbuf, sizeof (errbuf)));
826 return;
827 }
829 write_threads_num++;
830 } /* for (i) */
831 } /* }}} void start_write_threads */
833 static void stop_write_threads (void) /* {{{ */
834 {
835 write_queue_t *q;
836 int i;
838 if (write_threads == NULL)
839 return;
841 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
843 pthread_mutex_lock (&write_lock);
844 write_loop = 0;
845 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
846 pthread_cond_broadcast (&write_cond);
847 pthread_mutex_unlock (&write_lock);
849 for (i = 0; i < write_threads_num; i++)
850 {
851 if (pthread_join (write_threads[i], NULL) != 0)
852 {
853 ERROR ("plugin: stop_write_threads: pthread_join failed.");
854 }
855 write_threads[i] = (pthread_t) 0;
856 }
857 sfree (write_threads);
858 write_threads_num = 0;
860 pthread_mutex_lock (&write_lock);
861 i = 0;
862 for (q = write_queue_head; q != NULL; )
863 {
864 write_queue_t *q1 = q;
865 plugin_value_list_free (q->vl);
866 q = q->next;
867 sfree (q1);
868 i++;
869 }
870 write_queue_head = NULL;
871 write_queue_tail = NULL;
872 write_queue_length = 0;
873 pthread_mutex_unlock (&write_lock);
875 if (i > 0)
876 {
877 WARNING ("plugin: %i value list%s left after shutting down "
878 "the write threads.",
879 i, (i == 1) ? " was" : "s were");
880 }
881 } /* }}} void stop_write_threads */
883 /*
884 * Public functions
885 */
886 void plugin_set_dir (const char *dir)
887 {
888 if (plugindir != NULL)
889 free (plugindir);
891 if (dir == NULL)
892 plugindir = NULL;
893 else if ((plugindir = strdup (dir)) == NULL)
894 {
895 char errbuf[1024];
896 ERROR ("strdup failed: %s",
897 sstrerror (errno, errbuf, sizeof (errbuf)));
898 }
899 }
901 static _Bool plugin_is_loaded (char const *name)
902 {
903 int status;
905 if (plugins_loaded == NULL)
906 plugins_loaded = c_avl_create ((void *) strcasecmp);
907 assert (plugins_loaded != NULL);
909 status = c_avl_get (plugins_loaded, name, /* ret_value = */ NULL);
910 return (status == 0);
911 }
913 static int plugin_mark_loaded (char const *name)
914 {
915 char *name_copy;
916 int status;
918 name_copy = strdup (name);
919 if (name_copy == NULL)
920 return (ENOMEM);
922 status = c_avl_insert (plugins_loaded,
923 /* key = */ name_copy, /* value = */ NULL);
924 return (status);
925 }
927 static void plugin_free_loaded ()
928 {
929 void *key;
930 void *value;
932 if (plugins_loaded == NULL)
933 return;
935 while (c_avl_pick (plugins_loaded, &key, &value) == 0)
936 {
937 sfree (key);
938 assert (value == NULL);
939 }
941 c_avl_destroy (plugins_loaded);
942 plugins_loaded = NULL;
943 }
945 #define BUFSIZE 512
946 int plugin_load (char const *plugin_name, uint32_t flags)
947 {
948 DIR *dh;
949 const char *dir;
950 char filename[BUFSIZE] = "";
951 char typename[BUFSIZE];
952 int ret;
953 struct stat statbuf;
954 struct dirent *de;
955 int status;
957 if (plugin_name == NULL)
958 return (EINVAL);
960 /* Check if plugin is already loaded and don't do anything in this
961 * case. */
962 if (plugin_is_loaded (plugin_name))
963 return (0);
965 dir = plugin_get_dir ();
966 ret = 1;
968 /*
969 * XXX: Magic at work:
970 *
971 * Some of the language bindings, for example the Python and Perl
972 * plugins, need to be able to export symbols to the scripts they run.
973 * For this to happen, the "Globals" flag needs to be set.
974 * Unfortunately, this technical detail is hard to explain to the
975 * average user and she shouldn't have to worry about this, ideally.
976 * So in order to save everyone's sanity use a different default for a
977 * handful of special plugins. --octo
978 */
979 if ((strcasecmp ("perl", plugin_name) == 0)
980 || (strcasecmp ("python", plugin_name) == 0))
981 flags |= PLUGIN_FLAGS_GLOBAL;
983 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
984 * type when matching the filename */
985 status = ssnprintf (typename, sizeof (typename), "%s.so", plugin_name);
986 if ((status < 0) || ((size_t) status >= sizeof (typename)))
987 {
988 WARNING ("plugin_load: Filename too long: \"%s.so\"", plugin_name);
989 return (-1);
990 }
992 if ((dh = opendir (dir)) == NULL)
993 {
994 char errbuf[1024];
995 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
996 sstrerror (errno, errbuf, sizeof (errbuf)));
997 return (-1);
998 }
1000 while ((de = readdir (dh)) != NULL)
1001 {
1002 if (strcasecmp (de->d_name, typename))
1003 continue;
1005 status = ssnprintf (filename, sizeof (filename),
1006 "%s/%s", dir, de->d_name);
1007 if ((status < 0) || ((size_t) status >= sizeof (filename)))
1008 {
1009 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
1010 dir, de->d_name);
1011 continue;
1012 }
1014 if (lstat (filename, &statbuf) == -1)
1015 {
1016 char errbuf[1024];
1017 WARNING ("plugin_load: stat (\"%s\") failed: %s",
1018 filename,
1019 sstrerror (errno, errbuf, sizeof (errbuf)));
1020 continue;
1021 }
1022 else if (!S_ISREG (statbuf.st_mode))
1023 {
1024 /* don't follow symlinks */
1025 WARNING ("plugin_load: %s is not a regular file.",
1026 filename);
1027 continue;
1028 }
1030 status = plugin_load_file (filename, flags);
1031 if (status == 0)
1032 {
1033 /* success */
1034 plugin_mark_loaded (plugin_name);
1035 ret = 0;
1036 break;
1037 }
1038 else
1039 {
1040 ERROR ("plugin_load: Load plugin \"%s\" failed with "
1041 "status %i.", plugin_name, status);
1042 }
1043 }
1045 closedir (dh);
1047 if (filename[0] == 0)
1048 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
1049 plugin_name, dir);
1051 return (ret);
1052 }
1054 /*
1055 * The `register_*' functions follow
1056 */
1057 int plugin_register_config (const char *name,
1058 int (*callback) (const char *key, const char *val),
1059 const char **keys, int keys_num)
1060 {
1061 cf_register (name, callback, keys, keys_num);
1062 return (0);
1063 } /* int plugin_register_config */
1065 int plugin_register_complex_config (const char *type,
1066 int (*callback) (oconfig_item_t *))
1067 {
1068 return (cf_register_complex (type, callback));
1069 } /* int plugin_register_complex_config */
1071 int plugin_register_init (const char *name,
1072 int (*callback) (void))
1073 {
1074 return (create_register_callback (&list_init, name, (void *) callback,
1075 /* user_data = */ NULL));
1076 } /* plugin_register_init */
1078 static int plugin_compare_read_func (const void *arg0, const void *arg1)
1079 {
1080 const read_func_t *rf0;
1081 const read_func_t *rf1;
1083 rf0 = arg0;
1084 rf1 = arg1;
1086 if (rf0->rf_next_read < rf1->rf_next_read)
1087 return (-1);
1088 else if (rf0->rf_next_read > rf1->rf_next_read)
1089 return (1);
1090 else
1091 return (0);
1092 } /* int plugin_compare_read_func */
1094 /* Add a read function to both, the heap and a linked list. The linked list if
1095 * used to look-up read functions, especially for the remove function. The heap
1096 * is used to determine which plugin to read next. */
1097 static int plugin_insert_read (read_func_t *rf)
1098 {
1099 int status;
1100 llentry_t *le;
1102 rf->rf_next_read = cdtime ();
1103 rf->rf_effective_interval = rf->rf_interval;
1105 pthread_mutex_lock (&read_lock);
1107 if (read_list == NULL)
1108 {
1109 read_list = llist_create ();
1110 if (read_list == NULL)
1111 {
1112 pthread_mutex_unlock (&read_lock);
1113 ERROR ("plugin_insert_read: read_list failed.");
1114 return (-1);
1115 }
1116 }
1118 if (read_heap == NULL)
1119 {
1120 read_heap = c_heap_create (plugin_compare_read_func);
1121 if (read_heap == NULL)
1122 {
1123 pthread_mutex_unlock (&read_lock);
1124 ERROR ("plugin_insert_read: c_heap_create failed.");
1125 return (-1);
1126 }
1127 }
1129 le = llist_search (read_list, rf->rf_name);
1130 if (le != NULL)
1131 {
1132 pthread_mutex_unlock (&read_lock);
1133 WARNING ("The read function \"%s\" is already registered. "
1134 "Check for duplicate \"LoadPlugin\" lines "
1135 "in your configuration!",
1136 rf->rf_name);
1137 return (EINVAL);
1138 }
1140 le = llentry_create (rf->rf_name, rf);
1141 if (le == NULL)
1142 {
1143 pthread_mutex_unlock (&read_lock);
1144 ERROR ("plugin_insert_read: llentry_create failed.");
1145 return (-1);
1146 }
1148 status = c_heap_insert (read_heap, rf);
1149 if (status != 0)
1150 {
1151 pthread_mutex_unlock (&read_lock);
1152 ERROR ("plugin_insert_read: c_heap_insert failed.");
1153 llentry_destroy (le);
1154 return (-1);
1155 }
1157 /* This does not fail. */
1158 llist_append (read_list, le);
1160 /* Wake up all the read threads. */
1161 pthread_cond_broadcast (&read_cond);
1162 pthread_mutex_unlock (&read_lock);
1163 return (0);
1164 } /* int plugin_insert_read */
1166 int plugin_register_read (const char *name,
1167 int (*callback) (void))
1168 {
1169 read_func_t *rf;
1170 int status;
1172 rf = malloc (sizeof (*rf));
1173 if (rf == NULL)
1174 {
1175 ERROR ("plugin_register_read: malloc failed.");
1176 return (ENOMEM);
1177 }
1179 memset (rf, 0, sizeof (read_func_t));
1180 rf->rf_callback = (void *) callback;
1181 rf->rf_udata.data = NULL;
1182 rf->rf_udata.free_func = NULL;
1183 rf->rf_ctx = plugin_get_ctx ();
1184 rf->rf_group[0] = '\0';
1185 rf->rf_name = strdup (name);
1186 rf->rf_type = RF_SIMPLE;
1187 rf->rf_interval = plugin_get_interval ();
1189 status = plugin_insert_read (rf);
1190 if (status != 0)
1191 sfree (rf);
1193 return (status);
1194 } /* int plugin_register_read */
1196 int plugin_register_complex_read (const char *group, const char *name,
1197 plugin_read_cb callback,
1198 const struct timespec *interval,
1199 user_data_t *user_data)
1200 {
1201 read_func_t *rf;
1202 int status;
1204 rf = malloc (sizeof (*rf));
1205 if (rf == NULL)
1206 {
1207 ERROR ("plugin_register_complex_read: malloc failed.");
1208 return (ENOMEM);
1209 }
1211 memset (rf, 0, sizeof (read_func_t));
1212 rf->rf_callback = (void *) callback;
1213 if (group != NULL)
1214 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1215 else
1216 rf->rf_group[0] = '\0';
1217 rf->rf_name = strdup (name);
1218 rf->rf_type = RF_COMPLEX;
1219 if (interval != NULL)
1220 rf->rf_interval = TIMESPEC_TO_CDTIME_T (interval);
1221 else
1222 rf->rf_interval = plugin_get_interval ();
1224 /* Set user data */
1225 if (user_data == NULL)
1226 {
1227 rf->rf_udata.data = NULL;
1228 rf->rf_udata.free_func = NULL;
1229 }
1230 else
1231 {
1232 rf->rf_udata = *user_data;
1233 }
1235 rf->rf_ctx = plugin_get_ctx ();
1237 status = plugin_insert_read (rf);
1238 if (status != 0)
1239 sfree (rf);
1241 return (status);
1242 } /* int plugin_register_complex_read */
1244 int plugin_register_write (const char *name,
1245 plugin_write_cb callback, user_data_t *ud)
1246 {
1247 return (create_register_callback (&list_write, name,
1248 (void *) callback, ud));
1249 } /* int plugin_register_write */
1251 int plugin_register_flush (const char *name,
1252 plugin_flush_cb callback, user_data_t *ud)
1253 {
1254 return (create_register_callback (&list_flush, name,
1255 (void *) callback, ud));
1256 } /* int plugin_register_flush */
1258 int plugin_register_missing (const char *name,
1259 plugin_missing_cb callback, user_data_t *ud)
1260 {
1261 return (create_register_callback (&list_missing, name,
1262 (void *) callback, ud));
1263 } /* int plugin_register_missing */
1265 int plugin_register_shutdown (const char *name,
1266 int (*callback) (void))
1267 {
1268 return (create_register_callback (&list_shutdown, name,
1269 (void *) callback, /* user_data = */ NULL));
1270 } /* int plugin_register_shutdown */
1272 static void plugin_free_data_sets (void)
1273 {
1274 void *key;
1275 void *value;
1277 if (data_sets == NULL)
1278 return;
1280 while (c_avl_pick (data_sets, &key, &value) == 0)
1281 {
1282 data_set_t *ds = value;
1283 /* key is a pointer to ds->type */
1285 sfree (ds->ds);
1286 sfree (ds);
1287 }
1289 c_avl_destroy (data_sets);
1290 data_sets = NULL;
1291 } /* void plugin_free_data_sets */
1293 int plugin_register_data_set (const data_set_t *ds)
1294 {
1295 data_set_t *ds_copy;
1296 int i;
1298 if ((data_sets != NULL)
1299 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1300 {
1301 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1302 plugin_unregister_data_set (ds->type);
1303 }
1304 else if (data_sets == NULL)
1305 {
1306 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1307 if (data_sets == NULL)
1308 return (-1);
1309 }
1311 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1312 if (ds_copy == NULL)
1313 return (-1);
1314 memcpy(ds_copy, ds, sizeof (data_set_t));
1316 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1317 * ds->ds_num);
1318 if (ds_copy->ds == NULL)
1319 {
1320 free (ds_copy);
1321 return (-1);
1322 }
1324 for (i = 0; i < ds->ds_num; i++)
1325 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1327 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1328 } /* int plugin_register_data_set */
1330 int plugin_register_log (const char *name,
1331 plugin_log_cb callback, user_data_t *ud)
1332 {
1333 return (create_register_callback (&list_log, name,
1334 (void *) callback, ud));
1335 } /* int plugin_register_log */
1337 int plugin_register_notification (const char *name,
1338 plugin_notification_cb callback, user_data_t *ud)
1339 {
1340 return (create_register_callback (&list_notification, name,
1341 (void *) callback, ud));
1342 } /* int plugin_register_log */
1344 int plugin_unregister_config (const char *name)
1345 {
1346 cf_unregister (name);
1347 return (0);
1348 } /* int plugin_unregister_config */
1350 int plugin_unregister_complex_config (const char *name)
1351 {
1352 cf_unregister_complex (name);
1353 return (0);
1354 } /* int plugin_unregister_complex_config */
1356 int plugin_unregister_init (const char *name)
1357 {
1358 return (plugin_unregister (list_init, name));
1359 }
1361 int plugin_unregister_read (const char *name) /* {{{ */
1362 {
1363 llentry_t *le;
1364 read_func_t *rf;
1366 if (name == NULL)
1367 return (-ENOENT);
1369 pthread_mutex_lock (&read_lock);
1371 if (read_list == NULL)
1372 {
1373 pthread_mutex_unlock (&read_lock);
1374 return (-ENOENT);
1375 }
1377 le = llist_search (read_list, name);
1378 if (le == NULL)
1379 {
1380 pthread_mutex_unlock (&read_lock);
1381 WARNING ("plugin_unregister_read: No such read function: %s",
1382 name);
1383 return (-ENOENT);
1384 }
1386 llist_remove (read_list, le);
1388 rf = le->value;
1389 assert (rf != NULL);
1390 rf->rf_type = RF_REMOVE;
1392 pthread_mutex_unlock (&read_lock);
1394 llentry_destroy (le);
1396 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1398 return (0);
1399 } /* }}} int plugin_unregister_read */
1401 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1402 {
1403 read_func_t *rf = e->value;
1404 char *group = ud;
1406 return strcmp (rf->rf_group, (const char *)group);
1407 } /* }}} int compare_read_func_group */
1409 int plugin_unregister_read_group (const char *group) /* {{{ */
1410 {
1411 llentry_t *le;
1412 read_func_t *rf;
1414 int found = 0;
1416 if (group == NULL)
1417 return (-ENOENT);
1419 pthread_mutex_lock (&read_lock);
1421 if (read_list == NULL)
1422 {
1423 pthread_mutex_unlock (&read_lock);
1424 return (-ENOENT);
1425 }
1427 while (42)
1428 {
1429 le = llist_search_custom (read_list,
1430 compare_read_func_group, (void *)group);
1432 if (le == NULL)
1433 break;
1435 ++found;
1437 llist_remove (read_list, le);
1439 rf = le->value;
1440 assert (rf != NULL);
1441 rf->rf_type = RF_REMOVE;
1443 llentry_destroy (le);
1445 DEBUG ("plugin_unregister_read_group: "
1446 "Marked `%s' (group `%s') for removal.",
1447 rf->rf_name, group);
1448 }
1450 pthread_mutex_unlock (&read_lock);
1452 if (found == 0)
1453 {
1454 WARNING ("plugin_unregister_read_group: No such "
1455 "group of read function: %s", group);
1456 return (-ENOENT);
1457 }
1459 return (0);
1460 } /* }}} int plugin_unregister_read_group */
1462 int plugin_unregister_write (const char *name)
1463 {
1464 return (plugin_unregister (list_write, name));
1465 }
1467 int plugin_unregister_flush (const char *name)
1468 {
1469 return (plugin_unregister (list_flush, name));
1470 }
1472 int plugin_unregister_missing (const char *name)
1473 {
1474 return (plugin_unregister (list_missing, name));
1475 }
1477 int plugin_unregister_shutdown (const char *name)
1478 {
1479 return (plugin_unregister (list_shutdown, name));
1480 }
1482 int plugin_unregister_data_set (const char *name)
1483 {
1484 data_set_t *ds;
1486 if (data_sets == NULL)
1487 return (-1);
1489 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1490 return (-1);
1492 sfree (ds->ds);
1493 sfree (ds);
1495 return (0);
1496 } /* int plugin_unregister_data_set */
1498 int plugin_unregister_log (const char *name)
1499 {
1500 return (plugin_unregister (list_log, name));
1501 }
1503 int plugin_unregister_notification (const char *name)
1504 {
1505 return (plugin_unregister (list_notification, name));
1506 }
1508 void plugin_init_all (void)
1509 {
1510 char const *chain_name;
1511 long write_threads_num;
1512 llentry_t *le;
1513 int status;
1515 /* Init the value cache */
1516 uc_init ();
1518 if (IS_TRUE (global_option_get ("CollectInternalStats")))
1519 record_statistics = 1;
1521 chain_name = global_option_get ("PreCacheChain");
1522 pre_cache_chain = fc_chain_get_by_name (chain_name);
1524 chain_name = global_option_get ("PostCacheChain");
1525 post_cache_chain = fc_chain_get_by_name (chain_name);
1527 write_limit_high = global_option_get_long ("WriteQueueLimitHigh",
1528 /* default = */ 0);
1529 if (write_limit_high < 0)
1530 {
1531 ERROR ("WriteQueueLimitHigh must be positive or zero.");
1532 write_limit_high = 0;
1533 }
1535 write_limit_low = global_option_get_long ("WriteQueueLimitLow",
1536 /* default = */ write_limit_high / 2);
1537 if (write_limit_low < 0)
1538 {
1539 ERROR ("WriteQueueLimitLow must be positive or zero.");
1540 write_limit_low = write_limit_high / 2;
1541 }
1542 else if (write_limit_low > write_limit_high)
1543 {
1544 ERROR ("WriteQueueLimitLow must not be larger than "
1545 "WriteQueueLimitHigh.");
1546 write_limit_low = write_limit_high;
1547 }
1549 write_threads_num = global_option_get_long ("WriteThreads",
1550 /* default = */ 5);
1551 if (write_threads_num < 1)
1552 {
1553 ERROR ("WriteThreads must be positive.");
1554 write_threads_num = 5;
1555 }
1557 start_write_threads ((size_t) write_threads_num);
1559 if ((list_init == NULL) && (read_heap == NULL))
1560 return;
1562 /* Calling all init callbacks before checking if read callbacks
1563 * are available allows the init callbacks to register the read
1564 * callback. */
1565 le = llist_head (list_init);
1566 while (le != NULL)
1567 {
1568 callback_func_t *cf;
1569 plugin_init_cb callback;
1570 plugin_ctx_t old_ctx;
1572 cf = le->value;
1573 old_ctx = plugin_set_ctx (cf->cf_ctx);
1574 callback = cf->cf_callback;
1575 status = (*callback) ();
1576 plugin_set_ctx (old_ctx);
1578 if (status != 0)
1579 {
1580 ERROR ("Initialization of plugin `%s' "
1581 "failed with status %i. "
1582 "Plugin will be unloaded.",
1583 le->key, status);
1584 /* Plugins that register read callbacks from the init
1585 * callback should take care of appropriate error
1586 * handling themselves. */
1587 /* FIXME: Unload _all_ functions */
1588 plugin_unregister_read (le->key);
1589 }
1591 le = le->next;
1592 }
1594 max_read_interval = global_option_get_time ("MaxReadInterval",
1595 DEFAULT_MAX_READ_INTERVAL);
1597 /* Start read-threads */
1598 if (read_heap != NULL)
1599 {
1600 const char *rt;
1601 int num;
1603 rt = global_option_get ("ReadThreads");
1604 num = atoi (rt);
1605 if (num != -1)
1606 start_read_threads ((num > 0) ? num : 5);
1607 }
1608 } /* void plugin_init_all */
1610 /* TODO: Rename this function. */
1611 void plugin_read_all (void)
1612 {
1613 if(record_statistics) {
1614 plugin_update_internal_statistics ();
1615 }
1616 uc_check_timeout ();
1618 return;
1619 } /* void plugin_read_all */
1621 /* Read function called when the `-T' command line argument is given. */
1622 int plugin_read_all_once (void)
1623 {
1624 int status;
1625 int return_status = 0;
1627 if (read_heap == NULL)
1628 {
1629 NOTICE ("No read-functions are registered.");
1630 return (0);
1631 }
1633 while (42)
1634 {
1635 read_func_t *rf;
1636 plugin_ctx_t old_ctx;
1638 rf = c_heap_get_root (read_heap);
1639 if (rf == NULL)
1640 break;
1642 old_ctx = plugin_set_ctx (rf->rf_ctx);
1644 if (rf->rf_type == RF_SIMPLE)
1645 {
1646 int (*callback) (void);
1648 callback = rf->rf_callback;
1649 status = (*callback) ();
1650 }
1651 else
1652 {
1653 plugin_read_cb callback;
1655 callback = rf->rf_callback;
1656 status = (*callback) (&rf->rf_udata);
1657 }
1659 plugin_set_ctx (old_ctx);
1661 if (status != 0)
1662 {
1663 NOTICE ("read-function of plugin `%s' failed.",
1664 rf->rf_name);
1665 return_status = -1;
1666 }
1668 destroy_callback ((void *) rf);
1669 }
1671 return (return_status);
1672 } /* int plugin_read_all_once */
1674 int plugin_write (const char *plugin, /* {{{ */
1675 const data_set_t *ds, const value_list_t *vl)
1676 {
1677 llentry_t *le;
1678 int status;
1680 if (vl == NULL)
1681 return (EINVAL);
1683 if (list_write == NULL)
1684 return (ENOENT);
1686 if (ds == NULL)
1687 {
1688 ds = plugin_get_ds (vl->type);
1689 if (ds == NULL)
1690 {
1691 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1692 return (ENOENT);
1693 }
1694 }
1696 if (plugin == NULL)
1697 {
1698 int success = 0;
1699 int failure = 0;
1701 le = llist_head (list_write);
1702 while (le != NULL)
1703 {
1704 callback_func_t *cf = le->value;
1705 plugin_write_cb callback;
1707 /* do not switch plugin context; rather keep the context (interval)
1708 * information of the calling read plugin */
1710 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1711 callback = cf->cf_callback;
1712 status = (*callback) (ds, vl, &cf->cf_udata);
1713 if (status != 0)
1714 failure++;
1715 else
1716 success++;
1718 le = le->next;
1719 }
1721 if ((success == 0) && (failure != 0))
1722 status = -1;
1723 else
1724 status = 0;
1725 }
1726 else /* plugin != NULL */
1727 {
1728 callback_func_t *cf;
1729 plugin_write_cb callback;
1731 le = llist_head (list_write);
1732 while (le != NULL)
1733 {
1734 if (strcasecmp (plugin, le->key) == 0)
1735 break;
1737 le = le->next;
1738 }
1740 if (le == NULL)
1741 return (ENOENT);
1743 cf = le->value;
1745 /* do not switch plugin context; rather keep the context (interval)
1746 * information of the calling read plugin */
1748 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1749 callback = cf->cf_callback;
1750 status = (*callback) (ds, vl, &cf->cf_udata);
1751 }
1753 return (status);
1754 } /* }}} int plugin_write */
1756 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1757 {
1758 llentry_t *le;
1760 if (list_flush == NULL)
1761 return (0);
1763 le = llist_head (list_flush);
1764 while (le != NULL)
1765 {
1766 callback_func_t *cf;
1767 plugin_flush_cb callback;
1768 plugin_ctx_t old_ctx;
1770 if ((plugin != NULL)
1771 && (strcmp (plugin, le->key) != 0))
1772 {
1773 le = le->next;
1774 continue;
1775 }
1777 cf = le->value;
1778 old_ctx = plugin_set_ctx (cf->cf_ctx);
1779 callback = cf->cf_callback;
1781 (*callback) (timeout, identifier, &cf->cf_udata);
1783 plugin_set_ctx (old_ctx);
1785 le = le->next;
1786 }
1787 return (0);
1788 } /* int plugin_flush */
1790 void plugin_shutdown_all (void)
1791 {
1792 llentry_t *le;
1794 stop_read_threads ();
1796 destroy_all_callbacks (&list_init);
1798 pthread_mutex_lock (&read_lock);
1799 llist_destroy (read_list);
1800 read_list = NULL;
1801 pthread_mutex_unlock (&read_lock);
1803 destroy_read_heap ();
1805 plugin_flush (/* plugin = */ NULL,
1806 /* timeout = */ 0,
1807 /* identifier = */ NULL);
1809 le = NULL;
1810 if (list_shutdown != NULL)
1811 le = llist_head (list_shutdown);
1813 while (le != NULL)
1814 {
1815 callback_func_t *cf;
1816 plugin_shutdown_cb callback;
1817 plugin_ctx_t old_ctx;
1819 cf = le->value;
1820 old_ctx = plugin_set_ctx (cf->cf_ctx);
1821 callback = cf->cf_callback;
1823 /* Advance the pointer before calling the callback allows
1824 * shutdown functions to unregister themselves. If done the
1825 * other way around the memory `le' points to will be freed
1826 * after callback returns. */
1827 le = le->next;
1829 (*callback) ();
1831 plugin_set_ctx (old_ctx);
1832 }
1834 stop_write_threads ();
1836 /* Write plugins which use the `user_data' pointer usually need the
1837 * same data available to the flush callback. If this is the case, set
1838 * the free_function to NULL when registering the flush callback and to
1839 * the real free function when registering the write callback. This way
1840 * the data isn't freed twice. */
1841 destroy_all_callbacks (&list_flush);
1842 destroy_all_callbacks (&list_missing);
1843 destroy_all_callbacks (&list_write);
1845 destroy_all_callbacks (&list_notification);
1846 destroy_all_callbacks (&list_shutdown);
1847 destroy_all_callbacks (&list_log);
1849 plugin_free_loaded ();
1850 plugin_free_data_sets ();
1851 } /* void plugin_shutdown_all */
1853 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1854 {
1855 llentry_t *le;
1857 if (list_missing == NULL)
1858 return (0);
1860 le = llist_head (list_missing);
1861 while (le != NULL)
1862 {
1863 callback_func_t *cf;
1864 plugin_missing_cb callback;
1865 plugin_ctx_t old_ctx;
1866 int status;
1868 cf = le->value;
1869 old_ctx = plugin_set_ctx (cf->cf_ctx);
1870 callback = cf->cf_callback;
1872 status = (*callback) (vl, &cf->cf_udata);
1873 plugin_set_ctx (old_ctx);
1874 if (status != 0)
1875 {
1876 if (status < 0)
1877 {
1878 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1879 "failed with status %i.",
1880 le->key, status);
1881 return (status);
1882 }
1883 else
1884 {
1885 return (0);
1886 }
1887 }
1889 le = le->next;
1890 }
1891 return (0);
1892 } /* int }}} plugin_dispatch_missing */
1894 static int plugin_dispatch_values_internal (value_list_t *vl)
1895 {
1896 int status;
1897 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1899 value_t *saved_values;
1900 int saved_values_len;
1902 data_set_t *ds;
1904 int free_meta_data = 0;
1906 if ((vl == NULL) || (vl->type[0] == 0)
1907 || (vl->values == NULL) || (vl->values_len < 1))
1908 {
1909 ERROR ("plugin_dispatch_values: Invalid value list "
1910 "from plugin %s.", vl->plugin);
1911 return (-1);
1912 }
1914 /* Free meta data only if the calling function didn't specify any. In
1915 * this case matches and targets may add some and the calling function
1916 * may not expect (and therefore free) that data. */
1917 if (vl->meta == NULL)
1918 free_meta_data = 1;
1920 if (list_write == NULL)
1921 c_complain_once (LOG_WARNING, &no_write_complaint,
1922 "plugin_dispatch_values: No write callback has been "
1923 "registered. Please load at least one output plugin, "
1924 "if you want the collected data to be stored.");
1926 if (data_sets == NULL)
1927 {
1928 ERROR ("plugin_dispatch_values: No data sets registered. "
1929 "Could the types database be read? Check "
1930 "your `TypesDB' setting!");
1931 return (-1);
1932 }
1934 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1935 {
1936 char ident[6 * DATA_MAX_NAME_LEN];
1938 FORMAT_VL (ident, sizeof (ident), vl);
1939 INFO ("plugin_dispatch_values: Dataset not found: %s "
1940 "(from \"%s\"), check your types.db!",
1941 vl->type, ident);
1942 return (-1);
1943 }
1945 /* Assured by plugin_value_list_clone(). The time is determined at
1946 * _enqueue_ time. */
1947 assert (vl->time != 0);
1948 assert (vl->interval != 0);
1950 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1951 "host = %s; "
1952 "plugin = %s; plugin_instance = %s; "
1953 "type = %s; type_instance = %s;",
1954 CDTIME_T_TO_DOUBLE (vl->time),
1955 CDTIME_T_TO_DOUBLE (vl->interval),
1956 vl->host,
1957 vl->plugin, vl->plugin_instance,
1958 vl->type, vl->type_instance);
1960 #if COLLECT_DEBUG
1961 assert (0 == strcmp (ds->type, vl->type));
1962 #else
1963 if (0 != strcmp (ds->type, vl->type))
1964 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1965 ds->type, vl->type);
1966 #endif
1968 #if COLLECT_DEBUG
1969 assert (ds->ds_num == vl->values_len);
1970 #else
1971 if (ds->ds_num != vl->values_len)
1972 {
1973 ERROR ("plugin_dispatch_values: ds->type = %s: "
1974 "(ds->ds_num = %i) != "
1975 "(vl->values_len = %i)",
1976 ds->type, ds->ds_num, vl->values_len);
1977 return (-1);
1978 }
1979 #endif
1981 escape_slashes (vl->host, sizeof (vl->host));
1982 escape_slashes (vl->plugin, sizeof (vl->plugin));
1983 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1984 escape_slashes (vl->type, sizeof (vl->type));
1985 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1987 /* Copy the values. This way, we can assure `targets' that they get
1988 * dynamically allocated values, which they can free and replace if
1989 * they like. */
1990 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1991 {
1992 saved_values = vl->values;
1993 saved_values_len = vl->values_len;
1995 vl->values = (value_t *) calloc (vl->values_len,
1996 sizeof (*vl->values));
1997 if (vl->values == NULL)
1998 {
1999 ERROR ("plugin_dispatch_values: calloc failed.");
2000 vl->values = saved_values;
2001 return (-1);
2002 }
2003 memcpy (vl->values, saved_values,
2004 vl->values_len * sizeof (*vl->values));
2005 }
2006 else /* if ((pre == NULL) && (post == NULL)) */
2007 {
2008 saved_values = NULL;
2009 saved_values_len = 0;
2010 }
2012 if (pre_cache_chain != NULL)
2013 {
2014 status = fc_process_chain (ds, vl, pre_cache_chain);
2015 if (status < 0)
2016 {
2017 WARNING ("plugin_dispatch_values: Running the "
2018 "pre-cache chain failed with "
2019 "status %i (%#x).",
2020 status, status);
2021 }
2022 else if (status == FC_TARGET_STOP)
2023 {
2024 /* Restore the state of the value_list so that plugins
2025 * don't get confused.. */
2026 if (saved_values != NULL)
2027 {
2028 free (vl->values);
2029 vl->values = saved_values;
2030 vl->values_len = saved_values_len;
2031 }
2032 return (0);
2033 }
2034 }
2036 /* Update the value cache */
2037 uc_update (ds, vl);
2039 if (post_cache_chain != NULL)
2040 {
2041 status = fc_process_chain (ds, vl, post_cache_chain);
2042 if (status < 0)
2043 {
2044 WARNING ("plugin_dispatch_values: Running the "
2045 "post-cache chain failed with "
2046 "status %i (%#x).",
2047 status, status);
2048 }
2049 }
2050 else
2051 fc_default_action (ds, vl);
2053 /* Restore the state of the value_list so that plugins don't get
2054 * confused.. */
2055 if (saved_values != NULL)
2056 {
2057 free (vl->values);
2058 vl->values = saved_values;
2059 vl->values_len = saved_values_len;
2060 }
2062 if ((free_meta_data != 0) && (vl->meta != NULL))
2063 {
2064 meta_data_destroy (vl->meta);
2065 vl->meta = NULL;
2066 }
2068 return (0);
2069 } /* int plugin_dispatch_values_internal */
2071 static double get_drop_probability (void) /* {{{ */
2072 {
2073 long pos;
2074 long size;
2075 long wql;
2077 pthread_mutex_lock (&write_lock);
2078 wql = write_queue_length;
2079 pthread_mutex_unlock (&write_lock);
2081 if (wql < write_limit_low)
2082 return (0.0);
2083 if (wql >= write_limit_high)
2084 return (1.0);
2086 pos = 1 + wql - write_limit_low;
2087 size = 1 + write_limit_high - write_limit_low;
2089 return (((double) pos) / ((double) size));
2090 } /* }}} double get_drop_probability */
2092 static _Bool check_drop_value (void) /* {{{ */
2093 {
2094 static cdtime_t last_message_time = 0;
2095 static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER;
2097 double p;
2098 double q;
2099 int status;
2101 if (write_limit_high == 0)
2102 return (0);
2104 p = get_drop_probability ();
2105 if (p == 0.0)
2106 return (0);
2108 status = pthread_mutex_trylock (&last_message_lock);
2109 if (status == 0)
2110 {
2111 cdtime_t now;
2113 now = cdtime ();
2114 if ((now - last_message_time) > TIME_T_TO_CDTIME_T (1))
2115 {
2116 last_message_time = now;
2117 ERROR ("plugin_dispatch_values: Low water mark "
2118 "reached. Dropping %.0f%% of metrics.",
2119 100.0 * p);
2120 }
2121 pthread_mutex_unlock (&last_message_lock);
2122 }
2124 if (p == 1.0)
2125 return (1);
2127 q = cdrand_d ();
2128 if (q > p)
2129 return (1);
2130 else
2131 return (0);
2132 } /* }}} _Bool check_drop_value */
2134 int plugin_dispatch_values (value_list_t const *vl)
2135 {
2136 int status;
2137 static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER;
2139 if (check_drop_value ()) {
2140 if(record_statistics) {
2141 pthread_mutex_lock(&statistics_lock);
2142 stats_values_dropped++;
2143 pthread_mutex_unlock(&statistics_lock);
2144 }
2145 return (0);
2146 }
2148 status = plugin_write_enqueue (vl);
2149 if (status != 0)
2150 {
2151 char errbuf[1024];
2152 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
2153 "with status %i (%s).", status,
2154 sstrerror (status, errbuf, sizeof (errbuf)));
2155 return (status);
2156 }
2158 return (0);
2159 }
2161 __attribute__((sentinel))
2162 int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
2163 _Bool store_percentage, int store_type, ...)
2164 {
2165 value_list_t *vl;
2166 int failed = 0;
2167 gauge_t sum = 0.0;
2168 va_list ap;
2170 assert (template->values_len == 1);
2172 /* Calculate sum for Gauge to calculate percent if needed */
2173 if (DS_TYPE_GAUGE == store_type) {
2174 va_start (ap, store_type);
2175 while (42)
2176 {
2177 char const *name;
2178 gauge_t value;
2180 name = va_arg (ap, char const *);
2181 if (name == NULL)
2182 break;
2184 value = va_arg (ap, gauge_t);
2185 if (!isnan (value))
2186 sum += value;
2187 }
2188 va_end (ap);
2189 }
2192 vl = plugin_value_list_clone (template);
2193 /* plugin_value_list_clone makes sure vl->time is set to non-zero. */
2194 if (store_percentage)
2195 sstrncpy (vl->type, "percent", sizeof (vl->type));
2197 va_start (ap, store_type);
2198 while (42)
2199 {
2200 char const *name;
2201 int status;
2203 /* Set the type instance. */
2204 name = va_arg (ap, char const *);
2205 if (name == NULL)
2206 break;
2207 sstrncpy (vl->type_instance, name, sizeof (vl->type_instance));
2209 /* Set the value. */
2210 switch (store_type)
2211 {
2212 case DS_TYPE_GAUGE:
2213 vl->values[0].gauge = va_arg (ap, gauge_t);
2214 if (store_percentage)
2215 vl->values[0].gauge *= 100.0 / sum;
2216 break;
2217 case DS_TYPE_ABSOLUTE:
2218 vl->values[0].absolute = va_arg (ap, absolute_t);
2219 break;
2220 case DS_TYPE_COUNTER:
2221 vl->values[0].counter = va_arg (ap, counter_t);
2222 break;
2223 case DS_TYPE_DERIVE:
2224 vl->values[0].derive = va_arg (ap, derive_t);
2225 break;
2226 default:
2227 ERROR ("plugin_dispatch_multivalue: given store_type is incorrect.");
2228 failed++;
2229 }
2232 status = plugin_write_enqueue (vl);
2233 if (status != 0)
2234 failed++;
2235 }
2236 va_end (ap);
2238 plugin_value_list_free (vl);
2239 return (failed);
2240 } /* }}} int plugin_dispatch_multivalue */
2242 int plugin_dispatch_notification (const notification_t *notif)
2243 {
2244 llentry_t *le;
2245 /* Possible TODO: Add flap detection here */
2247 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
2248 "time = %.3f; host = %s;",
2249 notif->severity, notif->message,
2250 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
2252 /* Nobody cares for notifications */
2253 if (list_notification == NULL)
2254 return (-1);
2256 le = llist_head (list_notification);
2257 while (le != NULL)
2258 {
2259 callback_func_t *cf;
2260 plugin_notification_cb callback;
2261 int status;
2263 /* do not switch plugin context; rather keep the context
2264 * (interval) information of the calling plugin */
2266 cf = le->value;
2267 callback = cf->cf_callback;
2268 status = (*callback) (notif, &cf->cf_udata);
2269 if (status != 0)
2270 {
2271 WARNING ("plugin_dispatch_notification: Notification "
2272 "callback %s returned %i.",
2273 le->key, status);
2274 }
2276 le = le->next;
2277 }
2279 return (0);
2280 } /* int plugin_dispatch_notification */
2282 void plugin_log (int level, const char *format, ...)
2283 {
2284 char msg[1024];
2285 va_list ap;
2286 llentry_t *le;
2288 #if !COLLECT_DEBUG
2289 if (level >= LOG_DEBUG)
2290 return;
2291 #endif
2293 va_start (ap, format);
2294 vsnprintf (msg, sizeof (msg), format, ap);
2295 msg[sizeof (msg) - 1] = '\0';
2296 va_end (ap);
2298 if (list_log == NULL)
2299 {
2300 fprintf (stderr, "%s\n", msg);
2301 return;
2302 }
2304 le = llist_head (list_log);
2305 while (le != NULL)
2306 {
2307 callback_func_t *cf;
2308 plugin_log_cb callback;
2310 cf = le->value;
2311 callback = cf->cf_callback;
2313 /* do not switch plugin context; rather keep the context
2314 * (interval) information of the calling plugin */
2316 (*callback) (level, msg, &cf->cf_udata);
2318 le = le->next;
2319 }
2320 } /* void plugin_log */
2322 int parse_log_severity (const char *severity)
2323 {
2324 int log_level = -1;
2326 if ((0 == strcasecmp (severity, "emerg"))
2327 || (0 == strcasecmp (severity, "alert"))
2328 || (0 == strcasecmp (severity, "crit"))
2329 || (0 == strcasecmp (severity, "err")))
2330 log_level = LOG_ERR;
2331 else if (0 == strcasecmp (severity, "warning"))
2332 log_level = LOG_WARNING;
2333 else if (0 == strcasecmp (severity, "notice"))
2334 log_level = LOG_NOTICE;
2335 else if (0 == strcasecmp (severity, "info"))
2336 log_level = LOG_INFO;
2337 #if COLLECT_DEBUG
2338 else if (0 == strcasecmp (severity, "debug"))
2339 log_level = LOG_DEBUG;
2340 #endif /* COLLECT_DEBUG */
2342 return (log_level);
2343 } /* int parse_log_severity */
2345 int parse_notif_severity (const char *severity)
2346 {
2347 int notif_severity = -1;
2349 if (strcasecmp (severity, "FAILURE") == 0)
2350 notif_severity = NOTIF_FAILURE;
2351 else if (strcmp (severity, "OKAY") == 0)
2352 notif_severity = NOTIF_OKAY;
2353 else if ((strcmp (severity, "WARNING") == 0)
2354 || (strcmp (severity, "WARN") == 0))
2355 notif_severity = NOTIF_WARNING;
2357 return (notif_severity);
2358 } /* int parse_notif_severity */
2360 const data_set_t *plugin_get_ds (const char *name)
2361 {
2362 data_set_t *ds;
2364 if (data_sets == NULL)
2365 {
2366 ERROR ("plugin_get_ds: No data sets are defined yet.");
2367 return (NULL);
2368 }
2370 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2371 {
2372 DEBUG ("No such dataset registered: %s", name);
2373 return (NULL);
2374 }
2376 return (ds);
2377 } /* data_set_t *plugin_get_ds */
2379 static int plugin_notification_meta_add (notification_t *n,
2380 const char *name,
2381 enum notification_meta_type_e type,
2382 const void *value)
2383 {
2384 notification_meta_t *meta;
2385 notification_meta_t *tail;
2387 if ((n == NULL) || (name == NULL) || (value == NULL))
2388 {
2389 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2390 return (-1);
2391 }
2393 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2394 if (meta == NULL)
2395 {
2396 ERROR ("plugin_notification_meta_add: malloc failed.");
2397 return (-1);
2398 }
2399 memset (meta, 0, sizeof (notification_meta_t));
2401 sstrncpy (meta->name, name, sizeof (meta->name));
2402 meta->type = type;
2404 switch (type)
2405 {
2406 case NM_TYPE_STRING:
2407 {
2408 meta->nm_value.nm_string = strdup ((const char *) value);
2409 if (meta->nm_value.nm_string == NULL)
2410 {
2411 ERROR ("plugin_notification_meta_add: strdup failed.");
2412 sfree (meta);
2413 return (-1);
2414 }
2415 break;
2416 }
2417 case NM_TYPE_SIGNED_INT:
2418 {
2419 meta->nm_value.nm_signed_int = *((int64_t *) value);
2420 break;
2421 }
2422 case NM_TYPE_UNSIGNED_INT:
2423 {
2424 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2425 break;
2426 }
2427 case NM_TYPE_DOUBLE:
2428 {
2429 meta->nm_value.nm_double = *((double *) value);
2430 break;
2431 }
2432 case NM_TYPE_BOOLEAN:
2433 {
2434 meta->nm_value.nm_boolean = *((_Bool *) value);
2435 break;
2436 }
2437 default:
2438 {
2439 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2440 sfree (meta);
2441 return (-1);
2442 }
2443 } /* switch (type) */
2445 meta->next = NULL;
2446 tail = n->meta;
2447 while ((tail != NULL) && (tail->next != NULL))
2448 tail = tail->next;
2450 if (tail == NULL)
2451 n->meta = meta;
2452 else
2453 tail->next = meta;
2455 return (0);
2456 } /* int plugin_notification_meta_add */
2458 int plugin_notification_meta_add_string (notification_t *n,
2459 const char *name,
2460 const char *value)
2461 {
2462 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2463 }
2465 int plugin_notification_meta_add_signed_int (notification_t *n,
2466 const char *name,
2467 int64_t value)
2468 {
2469 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2470 }
2472 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2473 const char *name,
2474 uint64_t value)
2475 {
2476 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2477 }
2479 int plugin_notification_meta_add_double (notification_t *n,
2480 const char *name,
2481 double value)
2482 {
2483 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2484 }
2486 int plugin_notification_meta_add_boolean (notification_t *n,
2487 const char *name,
2488 _Bool value)
2489 {
2490 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2491 }
2493 int plugin_notification_meta_copy (notification_t *dst,
2494 const notification_t *src)
2495 {
2496 notification_meta_t *meta;
2498 assert (dst != NULL);
2499 assert (src != NULL);
2500 assert (dst != src);
2501 assert ((src->meta == NULL) || (src->meta != dst->meta));
2503 for (meta = src->meta; meta != NULL; meta = meta->next)
2504 {
2505 if (meta->type == NM_TYPE_STRING)
2506 plugin_notification_meta_add_string (dst, meta->name,
2507 meta->nm_value.nm_string);
2508 else if (meta->type == NM_TYPE_SIGNED_INT)
2509 plugin_notification_meta_add_signed_int (dst, meta->name,
2510 meta->nm_value.nm_signed_int);
2511 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2512 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2513 meta->nm_value.nm_unsigned_int);
2514 else if (meta->type == NM_TYPE_DOUBLE)
2515 plugin_notification_meta_add_double (dst, meta->name,
2516 meta->nm_value.nm_double);
2517 else if (meta->type == NM_TYPE_BOOLEAN)
2518 plugin_notification_meta_add_boolean (dst, meta->name,
2519 meta->nm_value.nm_boolean);
2520 }
2522 return (0);
2523 } /* int plugin_notification_meta_copy */
2525 int plugin_notification_meta_free (notification_meta_t *n)
2526 {
2527 notification_meta_t *this;
2528 notification_meta_t *next;
2530 if (n == NULL)
2531 {
2532 ERROR ("plugin_notification_meta_free: n == NULL!");
2533 return (-1);
2534 }
2536 this = n;
2537 while (this != NULL)
2538 {
2539 next = this->next;
2541 if (this->type == NM_TYPE_STRING)
2542 {
2543 free ((char *)this->nm_value.nm_string);
2544 this->nm_value.nm_string = NULL;
2545 }
2546 sfree (this);
2548 this = next;
2549 }
2551 return (0);
2552 } /* int plugin_notification_meta_free */
2554 static void plugin_ctx_destructor (void *ctx)
2555 {
2556 sfree (ctx);
2557 } /* void plugin_ctx_destructor */
2559 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2561 static plugin_ctx_t *plugin_ctx_create (void)
2562 {
2563 plugin_ctx_t *ctx;
2565 ctx = malloc (sizeof (*ctx));
2566 if (ctx == NULL) {
2567 char errbuf[1024];
2568 ERROR ("Failed to allocate plugin context: %s",
2569 sstrerror (errno, errbuf, sizeof (errbuf)));
2570 return NULL;
2571 }
2573 *ctx = ctx_init;
2574 assert (plugin_ctx_key_initialized);
2575 pthread_setspecific (plugin_ctx_key, ctx);
2576 DEBUG("Created new plugin context.");
2577 return (ctx);
2578 } /* int plugin_ctx_create */
2580 void plugin_init_ctx (void)
2581 {
2582 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2583 plugin_ctx_key_initialized = 1;
2584 } /* void plugin_init_ctx */
2586 plugin_ctx_t plugin_get_ctx (void)
2587 {
2588 plugin_ctx_t *ctx;
2590 assert (plugin_ctx_key_initialized);
2591 ctx = pthread_getspecific (plugin_ctx_key);
2593 if (ctx == NULL) {
2594 ctx = plugin_ctx_create ();
2595 /* this must no happen -- exit() instead? */
2596 if (ctx == NULL)
2597 return ctx_init;
2598 }
2600 return (*ctx);
2601 } /* plugin_ctx_t plugin_get_ctx */
2603 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2604 {
2605 plugin_ctx_t *c;
2606 plugin_ctx_t old;
2608 assert (plugin_ctx_key_initialized);
2609 c = pthread_getspecific (plugin_ctx_key);
2611 if (c == NULL) {
2612 c = plugin_ctx_create ();
2613 /* this must no happen -- exit() instead? */
2614 if (c == NULL)
2615 return ctx_init;
2616 }
2618 old = *c;
2619 *c = ctx;
2621 return (old);
2622 } /* void plugin_set_ctx */
2624 cdtime_t plugin_get_interval (void)
2625 {
2626 cdtime_t interval;
2628 interval = plugin_get_ctx().interval;
2629 if (interval > 0)
2630 return interval;
2632 return cf_get_default_interval ();
2633 } /* cdtime_t plugin_get_interval */
2635 typedef struct {
2636 plugin_ctx_t ctx;
2637 void *(*start_routine) (void *);
2638 void *arg;
2639 } plugin_thread_t;
2641 static void *plugin_thread_start (void *arg)
2642 {
2643 plugin_thread_t *plugin_thread = arg;
2645 void *(*start_routine) (void *) = plugin_thread->start_routine;
2646 void *plugin_arg = plugin_thread->arg;
2648 plugin_set_ctx (plugin_thread->ctx);
2650 free (plugin_thread);
2652 return start_routine (plugin_arg);
2653 } /* void *plugin_thread_start */
2655 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2656 void *(*start_routine) (void *), void *arg)
2657 {
2658 plugin_thread_t *plugin_thread;
2660 plugin_thread = malloc (sizeof (*plugin_thread));
2661 if (plugin_thread == NULL)
2662 return -1;
2664 plugin_thread->ctx = plugin_get_ctx ();
2665 plugin_thread->start_routine = start_routine;
2666 plugin_thread->arg = arg;
2668 return pthread_create (thread, attr,
2669 plugin_thread_start, plugin_thread);
2670 } /* int plugin_thread_create */
2672 /* vim: set sw=8 ts=8 noet fdm=marker : */