1 /**
2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2013 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at collectd.org>
20 * Sebastian Harl <sh at tokkee.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "filter_chain.h"
28 #include "utils_avltree.h"
29 #include "utils_cache.h"
30 #include "utils_complain.h"
31 #include "utils_llist.h"
32 #include "utils_heap.h"
33 #include "utils_time.h"
35 #if HAVE_PTHREAD_H
36 # include <pthread.h>
37 #endif
39 #include <ltdl.h>
41 /*
42 * Private structures
43 */
44 struct callback_func_s
45 {
46 void *cf_callback;
47 user_data_t cf_udata;
48 plugin_ctx_t cf_ctx;
49 };
50 typedef struct callback_func_s callback_func_t;
52 #define RF_SIMPLE 0
53 #define RF_COMPLEX 1
54 #define RF_REMOVE 65535
55 struct read_func_s
56 {
57 /* `read_func_t' "inherits" from `callback_func_t'.
58 * The `rf_super' member MUST be the first one in this structure! */
59 #define rf_callback rf_super.cf_callback
60 #define rf_udata rf_super.cf_udata
61 #define rf_ctx rf_super.cf_ctx
62 callback_func_t rf_super;
63 char rf_group[DATA_MAX_NAME_LEN];
64 char rf_name[DATA_MAX_NAME_LEN];
65 int rf_type;
66 cdtime_t rf_interval;
67 cdtime_t rf_effective_interval;
68 cdtime_t rf_next_read;
69 };
70 typedef struct read_func_s read_func_t;
72 struct write_queue_s;
73 typedef struct write_queue_s write_queue_t;
74 struct write_queue_s
75 {
76 value_list_t *vl;
77 plugin_ctx_t ctx;
78 write_queue_t *next;
79 };
81 /*
82 * Private variables
83 */
84 static llist_t *list_init;
85 static llist_t *list_write;
86 static llist_t *list_flush;
87 static llist_t *list_missing;
88 static llist_t *list_shutdown;
89 static llist_t *list_log;
90 static llist_t *list_notification;
92 static fc_chain_t *pre_cache_chain = NULL;
93 static fc_chain_t *post_cache_chain = NULL;
95 static c_avl_tree_t *data_sets;
97 static char *plugindir = NULL;
99 static c_heap_t *read_heap = NULL;
100 static llist_t *read_list;
101 static int read_loop = 1;
102 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
103 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
104 static pthread_t *read_threads = NULL;
105 static int read_threads_num = 0;
107 static write_queue_t *write_queue_head;
108 static write_queue_t *write_queue_tail;
109 static _Bool write_loop = 1;
110 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
111 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
112 static pthread_t *write_threads = NULL;
113 static size_t write_threads_num = 0;
115 static pthread_key_t plugin_ctx_key;
116 static _Bool plugin_ctx_key_initialized = 0;
118 /*
119 * Static functions
120 */
121 static int plugin_dispatch_values_internal (value_list_t *vl);
123 static const char *plugin_get_dir (void)
124 {
125 if (plugindir == NULL)
126 return (PLUGINDIR);
127 else
128 return (plugindir);
129 }
131 static void destroy_callback (callback_func_t *cf) /* {{{ */
132 {
133 if (cf == NULL)
134 return;
136 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
137 {
138 cf->cf_udata.free_func (cf->cf_udata.data);
139 cf->cf_udata.data = NULL;
140 cf->cf_udata.free_func = NULL;
141 }
142 sfree (cf);
143 } /* }}} void destroy_callback */
145 static void destroy_all_callbacks (llist_t **list) /* {{{ */
146 {
147 llentry_t *le;
149 if (*list == NULL)
150 return;
152 le = llist_head (*list);
153 while (le != NULL)
154 {
155 llentry_t *le_next;
157 le_next = le->next;
159 sfree (le->key);
160 destroy_callback (le->value);
161 le->value = NULL;
163 le = le_next;
164 }
166 llist_destroy (*list);
167 *list = NULL;
168 } /* }}} void destroy_all_callbacks */
170 static void destroy_read_heap (void) /* {{{ */
171 {
172 if (read_heap == NULL)
173 return;
175 while (42)
176 {
177 callback_func_t *cf;
179 cf = c_heap_get_root (read_heap);
180 if (cf == NULL)
181 break;
183 destroy_callback (cf);
184 }
186 c_heap_destroy (read_heap);
187 read_heap = NULL;
188 } /* }}} void destroy_read_heap */
190 static int register_callback (llist_t **list, /* {{{ */
191 const char *name, callback_func_t *cf)
192 {
193 llentry_t *le;
194 char *key;
196 if (*list == NULL)
197 {
198 *list = llist_create ();
199 if (*list == NULL)
200 {
201 ERROR ("plugin: register_callback: "
202 "llist_create failed.");
203 destroy_callback (cf);
204 return (-1);
205 }
206 }
208 key = strdup (name);
209 if (key == NULL)
210 {
211 ERROR ("plugin: register_callback: strdup failed.");
212 destroy_callback (cf);
213 return (-1);
214 }
216 le = llist_search (*list, name);
217 if (le == NULL)
218 {
219 le = llentry_create (key, cf);
220 if (le == NULL)
221 {
222 ERROR ("plugin: register_callback: "
223 "llentry_create failed.");
224 free (key);
225 destroy_callback (cf);
226 return (-1);
227 }
229 llist_append (*list, le);
230 }
231 else
232 {
233 callback_func_t *old_cf;
235 old_cf = le->value;
236 le->value = cf;
238 WARNING ("plugin: register_callback: "
239 "a callback named `%s' already exists - "
240 "overwriting the old entry!", name);
242 destroy_callback (old_cf);
243 sfree (key);
244 }
246 return (0);
247 } /* }}} int register_callback */
249 static int create_register_callback (llist_t **list, /* {{{ */
250 const char *name, void *callback, user_data_t *ud)
251 {
252 callback_func_t *cf;
254 cf = (callback_func_t *) malloc (sizeof (*cf));
255 if (cf == NULL)
256 {
257 ERROR ("plugin: create_register_callback: malloc failed.");
258 return (-1);
259 }
260 memset (cf, 0, sizeof (*cf));
262 cf->cf_callback = callback;
263 if (ud == NULL)
264 {
265 cf->cf_udata.data = NULL;
266 cf->cf_udata.free_func = NULL;
267 }
268 else
269 {
270 cf->cf_udata = *ud;
271 }
273 cf->cf_ctx = plugin_get_ctx ();
275 return (register_callback (list, name, cf));
276 } /* }}} int create_register_callback */
278 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
279 {
280 llentry_t *e;
282 if (list == NULL)
283 return (-1);
285 e = llist_search (list, name);
286 if (e == NULL)
287 return (-1);
289 llist_remove (list, e);
291 sfree (e->key);
292 destroy_callback (e->value);
294 llentry_destroy (e);
296 return (0);
297 } /* }}} int plugin_unregister */
299 /*
300 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
301 * object, but it will bitch about a shared object not having a
302 * ``module_register'' symbol..
303 */
304 static int plugin_load_file (char *file, uint32_t flags)
305 {
306 lt_dlhandle dlh;
307 void (*reg_handle) (void);
309 lt_dlinit ();
310 lt_dlerror (); /* clear errors */
312 #if LIBTOOL_VERSION == 2
313 if (flags & PLUGIN_FLAGS_GLOBAL) {
314 lt_dladvise advise;
315 lt_dladvise_init(&advise);
316 lt_dladvise_global(&advise);
317 dlh = lt_dlopenadvise(file, advise);
318 lt_dladvise_destroy(&advise);
319 } else {
320 dlh = lt_dlopen (file);
321 }
322 #else /* if LIBTOOL_VERSION == 1 */
323 if (flags & PLUGIN_FLAGS_GLOBAL)
324 WARNING ("plugin_load_file: The global flag is not supported, "
325 "libtool 2 is required for this.");
326 dlh = lt_dlopen (file);
327 #endif
329 if (dlh == NULL)
330 {
331 char errbuf[1024] = "";
333 ssnprintf (errbuf, sizeof (errbuf),
334 "lt_dlopen (\"%s\") failed: %s. "
335 "The most common cause for this problem are "
336 "missing dependencies. Use ldd(1) to check "
337 "the dependencies of the plugin "
338 "/ shared object.",
339 file, lt_dlerror ());
341 ERROR ("%s", errbuf);
342 /* Make sure this is printed to STDERR in any case, but also
343 * make sure it's printed only once. */
344 if (list_log != NULL)
345 fprintf (stderr, "ERROR: %s\n", errbuf);
347 return (1);
348 }
350 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
351 {
352 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
353 file, lt_dlerror ());
354 lt_dlclose (dlh);
355 return (-1);
356 }
358 (*reg_handle) ();
360 return (0);
361 }
363 static void *plugin_read_thread (void __attribute__((unused)) *args)
364 {
365 while (read_loop != 0)
366 {
367 read_func_t *rf;
368 plugin_ctx_t old_ctx;
369 cdtime_t now;
370 int status;
371 int rf_type;
372 int rc;
374 /* Get the read function that needs to be read next.
375 * We don't need to hold "read_lock" for the heap, but we need
376 * to call c_heap_get_root() and pthread_cond_wait() in the
377 * same protected block. */
378 pthread_mutex_lock (&read_lock);
379 rf = c_heap_get_root (read_heap);
380 if (rf == NULL)
381 {
382 pthread_cond_wait (&read_cond, &read_lock);
383 pthread_mutex_unlock (&read_lock);
384 continue;
385 }
386 pthread_mutex_unlock (&read_lock);
388 if (rf->rf_interval == 0)
389 {
390 /* this should not happen, because the interval is set
391 * for each plugin when loading it
392 * XXX: issue a warning? */
393 rf->rf_interval = plugin_get_interval ();
394 rf->rf_effective_interval = rf->rf_interval;
396 rf->rf_next_read = cdtime ();
397 }
399 /* sleep until this entry is due,
400 * using pthread_cond_timedwait */
401 pthread_mutex_lock (&read_lock);
402 /* In pthread_cond_timedwait, spurious wakeups are possible
403 * (and really happen, at least on NetBSD with > 1 CPU), thus
404 * we need to re-evaluate the condition every time
405 * pthread_cond_timedwait returns. */
406 rc = 0;
407 while ((read_loop != 0)
408 && (cdtime () < rf->rf_next_read)
409 && rc == 0)
410 {
411 struct timespec ts = { 0 };
413 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
415 rc = pthread_cond_timedwait (&read_cond, &read_lock,
416 &ts);
417 }
419 /* Must hold `read_lock' when accessing `rf->rf_type'. */
420 rf_type = rf->rf_type;
421 pthread_mutex_unlock (&read_lock);
423 /* Check if we're supposed to stop.. This may have interrupted
424 * the sleep, too. */
425 if (read_loop == 0)
426 {
427 /* Insert `rf' again, so it can be free'd correctly */
428 c_heap_insert (read_heap, rf);
429 break;
430 }
432 /* The entry has been marked for deletion. The linked list
433 * entry has already been removed by `plugin_unregister_read'.
434 * All we have to do here is free the `read_func_t' and
435 * continue. */
436 if (rf_type == RF_REMOVE)
437 {
438 DEBUG ("plugin_read_thread: Destroying the `%s' "
439 "callback.", rf->rf_name);
440 destroy_callback ((callback_func_t *) rf);
441 rf = NULL;
442 continue;
443 }
445 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
447 old_ctx = plugin_set_ctx (rf->rf_ctx);
449 if (rf_type == RF_SIMPLE)
450 {
451 int (*callback) (void);
453 callback = rf->rf_callback;
454 status = (*callback) ();
455 }
456 else
457 {
458 plugin_read_cb callback;
460 assert (rf_type == RF_COMPLEX);
462 callback = rf->rf_callback;
463 status = (*callback) (&rf->rf_udata);
464 }
466 plugin_set_ctx (old_ctx);
468 /* If the function signals failure, we will increase the
469 * intervals in which it will be called. */
470 if (status != 0)
471 {
472 rf->rf_effective_interval *= 2;
473 if (rf->rf_effective_interval > TIME_T_TO_CDTIME_T (86400))
474 rf->rf_effective_interval = TIME_T_TO_CDTIME_T (86400);
476 NOTICE ("read-function of plugin `%s' failed. "
477 "Will suspend it for %.3f seconds.",
478 rf->rf_name,
479 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
480 }
481 else
482 {
483 /* Success: Restore the interval, if it was changed. */
484 rf->rf_effective_interval = rf->rf_interval;
485 }
487 /* update the ``next read due'' field */
488 now = cdtime ();
490 DEBUG ("plugin_read_thread: Effective interval of the "
491 "%s plugin is %.3f seconds.",
492 rf->rf_name,
493 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
495 /* Calculate the next (absolute) time at which this function
496 * should be called. */
497 rf->rf_next_read += rf->rf_effective_interval;
499 /* Check, if `rf_next_read' is in the past. */
500 if (rf->rf_next_read < now)
501 {
502 /* `rf_next_read' is in the past. Insert `now'
503 * so this value doesn't trail off into the
504 * past too much. */
505 rf->rf_next_read = now;
506 }
508 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
509 rf->rf_name,
510 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
512 /* Re-insert this read function into the heap again. */
513 c_heap_insert (read_heap, rf);
514 } /* while (read_loop) */
516 pthread_exit (NULL);
517 return ((void *) 0);
518 } /* void *plugin_read_thread */
520 static void start_read_threads (int num)
521 {
522 int i;
524 if (read_threads != NULL)
525 return;
527 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
528 if (read_threads == NULL)
529 {
530 ERROR ("plugin: start_read_threads: calloc failed.");
531 return;
532 }
534 read_threads_num = 0;
535 for (i = 0; i < num; i++)
536 {
537 if (pthread_create (read_threads + read_threads_num, NULL,
538 plugin_read_thread, NULL) == 0)
539 {
540 read_threads_num++;
541 }
542 else
543 {
544 ERROR ("plugin: start_read_threads: pthread_create failed.");
545 return;
546 }
547 } /* for (i) */
548 } /* void start_read_threads */
550 static void stop_read_threads (void)
551 {
552 int i;
554 if (read_threads == NULL)
555 return;
557 INFO ("collectd: Stopping %i read threads.", read_threads_num);
559 pthread_mutex_lock (&read_lock);
560 read_loop = 0;
561 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
562 pthread_cond_broadcast (&read_cond);
563 pthread_mutex_unlock (&read_lock);
565 for (i = 0; i < read_threads_num; i++)
566 {
567 if (pthread_join (read_threads[i], NULL) != 0)
568 {
569 ERROR ("plugin: stop_read_threads: pthread_join failed.");
570 }
571 read_threads[i] = (pthread_t) 0;
572 }
573 sfree (read_threads);
574 read_threads_num = 0;
575 } /* void stop_read_threads */
577 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
578 {
579 if (vl == NULL)
580 return;
582 meta_data_destroy (vl->meta);
583 sfree (vl->values);
584 sfree (vl);
585 } /* }}} void plugin_value_list_free */
587 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
588 {
589 value_list_t *vl;
591 if (vl_orig == NULL)
592 return (NULL);
594 vl = malloc (sizeof (*vl));
595 if (vl == NULL)
596 return (NULL);
597 memcpy (vl, vl_orig, sizeof (*vl));
599 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
600 if (vl->values == NULL)
601 {
602 plugin_value_list_free (vl);
603 return (NULL);
604 }
605 memcpy (vl->values, vl_orig->values,
606 vl_orig->values_len * sizeof (*vl->values));
608 vl->meta = meta_data_clone (vl->meta);
609 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
610 {
611 plugin_value_list_free (vl);
612 return (NULL);
613 }
615 if (vl->time == 0)
616 vl->time = cdtime ();
618 /* Fill in the interval from the thread context, if it is zero. */
619 if (vl->interval == 0)
620 {
621 plugin_ctx_t ctx = plugin_get_ctx ();
623 if (ctx.interval != 0)
624 vl->interval = ctx.interval;
625 else
626 {
627 char name[6 * DATA_MAX_NAME_LEN];
628 FORMAT_VL (name, sizeof (name), vl);
629 ERROR ("plugin_value_list_clone: Unable to determine "
630 "interval from context for "
631 "value list \"%s\". "
632 "This indicates a broken plugin. "
633 "Please report this problem to the "
634 "collectd mailing list or at "
635 "<http://collectd.org/bugs/>.", name);
636 vl->interval = cf_get_default_interval ();
637 }
638 }
640 return (vl);
641 } /* }}} value_list_t *plugin_value_list_clone */
643 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
644 {
645 write_queue_t *q;
647 q = malloc (sizeof (*q));
648 if (q == NULL)
649 return (ENOMEM);
650 q->next = NULL;
652 q->vl = plugin_value_list_clone (vl);
653 if (q->vl == NULL)
654 {
655 sfree (q);
656 return (ENOMEM);
657 }
659 /* Store context of caller (read plugin); otherwise, it would not be
660 * available to the write plugins when actually dispatching the
661 * value-list later on. */
662 q->ctx = plugin_get_ctx ();
664 pthread_mutex_lock (&write_lock);
666 if (write_queue_tail == NULL)
667 {
668 write_queue_head = q;
669 write_queue_tail = q;
670 }
671 else
672 {
673 write_queue_tail->next = q;
674 write_queue_tail = q;
675 }
677 pthread_cond_signal (&write_cond);
678 pthread_mutex_unlock (&write_lock);
680 return (0);
681 } /* }}} int plugin_write_enqueue */
683 static value_list_t *plugin_write_dequeue (void) /* {{{ */
684 {
685 write_queue_t *q;
686 value_list_t *vl;
688 pthread_mutex_lock (&write_lock);
690 while (write_loop && (write_queue_head == NULL))
691 pthread_cond_wait (&write_cond, &write_lock);
693 if (write_queue_head == NULL)
694 {
695 pthread_mutex_unlock (&write_lock);
696 return (NULL);
697 }
699 q = write_queue_head;
700 write_queue_head = q->next;
701 if (write_queue_head == NULL)
702 write_queue_tail = NULL;
704 pthread_mutex_unlock (&write_lock);
706 (void) plugin_set_ctx (q->ctx);
708 vl = q->vl;
709 sfree (q);
710 return (vl);
711 } /* }}} value_list_t *plugin_write_dequeue */
713 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
714 {
715 while (write_loop)
716 {
717 value_list_t *vl = plugin_write_dequeue ();
718 if (vl == NULL)
719 continue;
721 plugin_dispatch_values_internal (vl);
723 plugin_value_list_free (vl);
724 }
726 pthread_exit (NULL);
727 return ((void *) 0);
728 } /* }}} void *plugin_write_thread */
730 static void start_write_threads (size_t num) /* {{{ */
731 {
732 size_t i;
734 if (write_threads != NULL)
735 return;
737 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
738 if (write_threads == NULL)
739 {
740 ERROR ("plugin: start_write_threads: calloc failed.");
741 return;
742 }
744 write_threads_num = 0;
745 for (i = 0; i < num; i++)
746 {
747 int status;
749 status = pthread_create (write_threads + write_threads_num,
750 /* attr = */ NULL,
751 plugin_write_thread,
752 /* arg = */ NULL);
753 if (status != 0)
754 {
755 char errbuf[1024];
756 ERROR ("plugin: start_write_threads: pthread_create failed "
757 "with status %i (%s).", status,
758 sstrerror (status, errbuf, sizeof (errbuf)));
759 return;
760 }
762 write_threads_num++;
763 } /* for (i) */
764 } /* }}} void start_write_threads */
766 static void stop_write_threads (void) /* {{{ */
767 {
768 write_queue_t *q;
769 int i;
771 if (write_threads == NULL)
772 return;
774 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
776 pthread_mutex_lock (&write_lock);
777 write_loop = 0;
778 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
779 pthread_cond_broadcast (&write_cond);
780 pthread_mutex_unlock (&write_lock);
782 for (i = 0; i < write_threads_num; i++)
783 {
784 if (pthread_join (write_threads[i], NULL) != 0)
785 {
786 ERROR ("plugin: stop_write_threads: pthread_join failed.");
787 }
788 write_threads[i] = (pthread_t) 0;
789 }
790 sfree (write_threads);
791 write_threads_num = 0;
793 pthread_mutex_lock (&write_lock);
794 i = 0;
795 for (q = write_queue_head; q != NULL; )
796 {
797 write_queue_t *q1 = q;
798 plugin_value_list_free (q->vl);
799 q = q->next;
800 sfree (q1);
801 i++;
802 }
803 write_queue_head = NULL;
804 write_queue_tail = NULL;
805 pthread_mutex_unlock (&write_lock);
807 if (i > 0)
808 {
809 WARNING ("plugin: %i value list%s left after shutting down "
810 "the write threads.",
811 i, (i == 1) ? " was" : "s were");
812 }
813 } /* }}} void stop_write_threads */
815 /*
816 * Public functions
817 */
818 void plugin_set_dir (const char *dir)
819 {
820 if (plugindir != NULL)
821 free (plugindir);
823 if (dir == NULL)
824 plugindir = NULL;
825 else if ((plugindir = strdup (dir)) == NULL)
826 {
827 char errbuf[1024];
828 ERROR ("strdup failed: %s",
829 sstrerror (errno, errbuf, sizeof (errbuf)));
830 }
831 }
833 #define BUFSIZE 512
834 int plugin_load (const char *type, uint32_t flags)
835 {
836 DIR *dh;
837 const char *dir;
838 char filename[BUFSIZE] = "";
839 char typename[BUFSIZE];
840 int typename_len;
841 int ret;
842 struct stat statbuf;
843 struct dirent *de;
844 int status;
846 dir = plugin_get_dir ();
847 ret = 1;
849 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
850 * type when matching the filename */
851 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
852 if ((status < 0) || ((size_t) status >= sizeof (typename)))
853 {
854 WARNING ("plugin_load: Filename too long: \"%s.so\"", type);
855 return (-1);
856 }
857 typename_len = strlen (typename);
859 if ((dh = opendir (dir)) == NULL)
860 {
861 char errbuf[1024];
862 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
863 sstrerror (errno, errbuf, sizeof (errbuf)));
864 return (-1);
865 }
867 while ((de = readdir (dh)) != NULL)
868 {
869 if (strncasecmp (de->d_name, typename, typename_len))
870 continue;
872 status = ssnprintf (filename, sizeof (filename),
873 "%s/%s", dir, de->d_name);
874 if ((status < 0) || ((size_t) status >= sizeof (filename)))
875 {
876 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
877 dir, de->d_name);
878 continue;
879 }
881 if (lstat (filename, &statbuf) == -1)
882 {
883 char errbuf[1024];
884 WARNING ("plugin_load: stat (\"%s\") failed: %s",
885 filename,
886 sstrerror (errno, errbuf, sizeof (errbuf)));
887 continue;
888 }
889 else if (!S_ISREG (statbuf.st_mode))
890 {
891 /* don't follow symlinks */
892 WARNING ("plugin_load: %s is not a regular file.",
893 filename);
894 continue;
895 }
897 status = plugin_load_file (filename, flags);
898 if (status == 0)
899 {
900 /* success */
901 ret = 0;
902 break;
903 }
904 else
905 {
906 ERROR ("plugin_load: Load plugin \"%s\" failed with "
907 "status %i.", type, status);
908 }
909 }
911 closedir (dh);
913 if (filename[0] == 0)
914 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
915 type, dir);
917 return (ret);
918 }
920 /*
921 * The `register_*' functions follow
922 */
923 int plugin_register_config (const char *name,
924 int (*callback) (const char *key, const char *val),
925 const char **keys, int keys_num)
926 {
927 cf_register (name, callback, keys, keys_num);
928 return (0);
929 } /* int plugin_register_config */
931 int plugin_register_complex_config (const char *type,
932 int (*callback) (oconfig_item_t *))
933 {
934 return (cf_register_complex (type, callback));
935 } /* int plugin_register_complex_config */
937 int plugin_register_init (const char *name,
938 int (*callback) (void))
939 {
940 return (create_register_callback (&list_init, name, (void *) callback,
941 /* user_data = */ NULL));
942 } /* plugin_register_init */
944 static int plugin_compare_read_func (const void *arg0, const void *arg1)
945 {
946 const read_func_t *rf0;
947 const read_func_t *rf1;
949 rf0 = arg0;
950 rf1 = arg1;
952 if (rf0->rf_next_read < rf1->rf_next_read)
953 return (-1);
954 else if (rf0->rf_next_read > rf1->rf_next_read)
955 return (1);
956 else
957 return (0);
958 } /* int plugin_compare_read_func */
960 /* Add a read function to both, the heap and a linked list. The linked list if
961 * used to look-up read functions, especially for the remove function. The heap
962 * is used to determine which plugin to read next. */
963 static int plugin_insert_read (read_func_t *rf)
964 {
965 int status;
966 llentry_t *le;
968 pthread_mutex_lock (&read_lock);
970 if (read_list == NULL)
971 {
972 read_list = llist_create ();
973 if (read_list == NULL)
974 {
975 pthread_mutex_unlock (&read_lock);
976 ERROR ("plugin_insert_read: read_list failed.");
977 return (-1);
978 }
979 }
981 if (read_heap == NULL)
982 {
983 read_heap = c_heap_create (plugin_compare_read_func);
984 if (read_heap == NULL)
985 {
986 pthread_mutex_unlock (&read_lock);
987 ERROR ("plugin_insert_read: c_heap_create failed.");
988 return (-1);
989 }
990 }
992 le = llist_search (read_list, rf->rf_name);
993 if (le != NULL)
994 {
995 pthread_mutex_unlock (&read_lock);
996 WARNING ("The read function \"%s\" is already registered. "
997 "Check for duplicate \"LoadPlugin\" lines "
998 "in your configuration!",
999 rf->rf_name);
1000 return (EINVAL);
1001 }
1003 le = llentry_create (rf->rf_name, rf);
1004 if (le == NULL)
1005 {
1006 pthread_mutex_unlock (&read_lock);
1007 ERROR ("plugin_insert_read: llentry_create failed.");
1008 return (-1);
1009 }
1011 status = c_heap_insert (read_heap, rf);
1012 if (status != 0)
1013 {
1014 pthread_mutex_unlock (&read_lock);
1015 ERROR ("plugin_insert_read: c_heap_insert failed.");
1016 llentry_destroy (le);
1017 return (-1);
1018 }
1020 /* This does not fail. */
1021 llist_append (read_list, le);
1023 /* Wake up all the read threads. */
1024 pthread_cond_broadcast (&read_cond);
1025 pthread_mutex_unlock (&read_lock);
1026 return (0);
1027 } /* int plugin_insert_read */
1029 static int read_cb_wrapper (user_data_t *ud)
1030 {
1031 int (*callback) (void);
1033 if (ud == NULL)
1034 return -1;
1036 callback = ud->data;
1037 return callback();
1038 } /* int read_cb_wrapper */
1040 int plugin_register_read (const char *name,
1041 int (*callback) (void))
1042 {
1043 read_func_t *rf;
1044 plugin_ctx_t ctx = plugin_get_ctx ();
1045 int status;
1047 if (ctx.interval != 0) {
1048 /* If ctx.interval is not zero (== use the plugin or global
1049 * interval), we need to use the "complex" read callback,
1050 * because only that allows to specify a different interval.
1051 * Wrap the callback using read_cb_wrapper(). */
1052 struct timespec interval;
1053 user_data_t user_data;
1055 user_data.data = callback;
1056 user_data.free_func = NULL;
1058 CDTIME_T_TO_TIMESPEC (ctx.interval, &interval);
1059 return plugin_register_complex_read (/* group = */ NULL,
1060 name, read_cb_wrapper, &interval, &user_data);
1061 }
1063 DEBUG ("plugin_register_read: default_interval = %.3f",
1064 CDTIME_T_TO_DOUBLE(plugin_get_interval ()));
1066 rf = malloc (sizeof (*rf));
1067 if (rf == NULL)
1068 {
1069 ERROR ("plugin_register_read: malloc failed.");
1070 return (ENOMEM);
1071 }
1073 memset (rf, 0, sizeof (read_func_t));
1074 rf->rf_callback = (void *) callback;
1075 rf->rf_udata.data = NULL;
1076 rf->rf_udata.free_func = NULL;
1077 rf->rf_ctx = ctx;
1078 rf->rf_group[0] = '\0';
1079 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1080 rf->rf_type = RF_SIMPLE;
1081 rf->rf_interval = 0;
1082 rf->rf_effective_interval = rf->rf_interval;
1084 status = plugin_insert_read (rf);
1085 if (status != 0)
1086 sfree (rf);
1088 return (status);
1089 } /* int plugin_register_read */
1091 int plugin_register_complex_read (const char *group, const char *name,
1092 plugin_read_cb callback,
1093 const struct timespec *interval,
1094 user_data_t *user_data)
1095 {
1096 read_func_t *rf;
1097 plugin_ctx_t ctx = plugin_get_ctx ();
1098 int status;
1100 rf = malloc (sizeof (*rf));
1101 if (rf == NULL)
1102 {
1103 ERROR ("plugin_register_complex_read: malloc failed.");
1104 return (ENOMEM);
1105 }
1107 memset (rf, 0, sizeof (read_func_t));
1108 rf->rf_callback = (void *) callback;
1109 if (group != NULL)
1110 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1111 else
1112 rf->rf_group[0] = '\0';
1113 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1114 rf->rf_type = RF_COMPLEX;
1115 if (interval != NULL)
1116 {
1117 rf->rf_interval = TIMESPEC_TO_CDTIME_T (interval);
1118 }
1119 else if (ctx.interval != 0)
1120 {
1121 rf->rf_interval = ctx.interval;
1122 }
1123 rf->rf_effective_interval = rf->rf_interval;
1125 DEBUG ("plugin_register_read: interval = %.3f",
1126 CDTIME_T_TO_DOUBLE (rf->rf_interval));
1128 /* Set user data */
1129 if (user_data == NULL)
1130 {
1131 rf->rf_udata.data = NULL;
1132 rf->rf_udata.free_func = NULL;
1133 }
1134 else
1135 {
1136 rf->rf_udata = *user_data;
1137 }
1139 rf->rf_ctx = ctx;
1141 status = plugin_insert_read (rf);
1142 if (status != 0)
1143 sfree (rf);
1145 return (status);
1146 } /* int plugin_register_complex_read */
1148 int plugin_register_write (const char *name,
1149 plugin_write_cb callback, user_data_t *ud)
1150 {
1151 return (create_register_callback (&list_write, name,
1152 (void *) callback, ud));
1153 } /* int plugin_register_write */
1155 int plugin_register_flush (const char *name,
1156 plugin_flush_cb callback, user_data_t *ud)
1157 {
1158 return (create_register_callback (&list_flush, name,
1159 (void *) callback, ud));
1160 } /* int plugin_register_flush */
1162 int plugin_register_missing (const char *name,
1163 plugin_missing_cb callback, user_data_t *ud)
1164 {
1165 return (create_register_callback (&list_missing, name,
1166 (void *) callback, ud));
1167 } /* int plugin_register_missing */
1169 int plugin_register_shutdown (const char *name,
1170 int (*callback) (void))
1171 {
1172 return (create_register_callback (&list_shutdown, name,
1173 (void *) callback, /* user_data = */ NULL));
1174 } /* int plugin_register_shutdown */
1176 int plugin_register_data_set (const data_set_t *ds)
1177 {
1178 data_set_t *ds_copy;
1179 int i;
1181 if ((data_sets != NULL)
1182 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1183 {
1184 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1185 plugin_unregister_data_set (ds->type);
1186 }
1187 else if (data_sets == NULL)
1188 {
1189 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1190 if (data_sets == NULL)
1191 return (-1);
1192 }
1194 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1195 if (ds_copy == NULL)
1196 return (-1);
1197 memcpy(ds_copy, ds, sizeof (data_set_t));
1199 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1200 * ds->ds_num);
1201 if (ds_copy->ds == NULL)
1202 {
1203 free (ds_copy);
1204 return (-1);
1205 }
1207 for (i = 0; i < ds->ds_num; i++)
1208 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1210 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1211 } /* int plugin_register_data_set */
1213 int plugin_register_log (const char *name,
1214 plugin_log_cb callback, user_data_t *ud)
1215 {
1216 return (create_register_callback (&list_log, name,
1217 (void *) callback, ud));
1218 } /* int plugin_register_log */
1220 int plugin_register_notification (const char *name,
1221 plugin_notification_cb callback, user_data_t *ud)
1222 {
1223 return (create_register_callback (&list_notification, name,
1224 (void *) callback, ud));
1225 } /* int plugin_register_log */
1227 int plugin_unregister_config (const char *name)
1228 {
1229 cf_unregister (name);
1230 return (0);
1231 } /* int plugin_unregister_config */
1233 int plugin_unregister_complex_config (const char *name)
1234 {
1235 cf_unregister_complex (name);
1236 return (0);
1237 } /* int plugin_unregister_complex_config */
1239 int plugin_unregister_init (const char *name)
1240 {
1241 return (plugin_unregister (list_init, name));
1242 }
1244 int plugin_unregister_read (const char *name) /* {{{ */
1245 {
1246 llentry_t *le;
1247 read_func_t *rf;
1249 if (name == NULL)
1250 return (-ENOENT);
1252 pthread_mutex_lock (&read_lock);
1254 if (read_list == NULL)
1255 {
1256 pthread_mutex_unlock (&read_lock);
1257 return (-ENOENT);
1258 }
1260 le = llist_search (read_list, name);
1261 if (le == NULL)
1262 {
1263 pthread_mutex_unlock (&read_lock);
1264 WARNING ("plugin_unregister_read: No such read function: %s",
1265 name);
1266 return (-ENOENT);
1267 }
1269 llist_remove (read_list, le);
1271 rf = le->value;
1272 assert (rf != NULL);
1273 rf->rf_type = RF_REMOVE;
1275 pthread_mutex_unlock (&read_lock);
1277 llentry_destroy (le);
1279 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1281 return (0);
1282 } /* }}} int plugin_unregister_read */
1284 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1285 {
1286 read_func_t *rf = e->value;
1287 char *group = ud;
1289 return strcmp (rf->rf_group, (const char *)group);
1290 } /* }}} int compare_read_func_group */
1292 int plugin_unregister_read_group (const char *group) /* {{{ */
1293 {
1294 llentry_t *le;
1295 read_func_t *rf;
1297 int found = 0;
1299 if (group == NULL)
1300 return (-ENOENT);
1302 pthread_mutex_lock (&read_lock);
1304 if (read_list == NULL)
1305 {
1306 pthread_mutex_unlock (&read_lock);
1307 return (-ENOENT);
1308 }
1310 while (42)
1311 {
1312 le = llist_search_custom (read_list,
1313 compare_read_func_group, (void *)group);
1315 if (le == NULL)
1316 break;
1318 ++found;
1320 llist_remove (read_list, le);
1322 rf = le->value;
1323 assert (rf != NULL);
1324 rf->rf_type = RF_REMOVE;
1326 llentry_destroy (le);
1328 DEBUG ("plugin_unregister_read_group: "
1329 "Marked `%s' (group `%s') for removal.",
1330 rf->rf_name, group);
1331 }
1333 pthread_mutex_unlock (&read_lock);
1335 if (found == 0)
1336 {
1337 WARNING ("plugin_unregister_read_group: No such "
1338 "group of read function: %s", group);
1339 return (-ENOENT);
1340 }
1342 return (0);
1343 } /* }}} int plugin_unregister_read_group */
1345 int plugin_unregister_write (const char *name)
1346 {
1347 return (plugin_unregister (list_write, name));
1348 }
1350 int plugin_unregister_flush (const char *name)
1351 {
1352 return (plugin_unregister (list_flush, name));
1353 }
1355 int plugin_unregister_missing (const char *name)
1356 {
1357 return (plugin_unregister (list_missing, name));
1358 }
1360 int plugin_unregister_shutdown (const char *name)
1361 {
1362 return (plugin_unregister (list_shutdown, name));
1363 }
1365 int plugin_unregister_data_set (const char *name)
1366 {
1367 data_set_t *ds;
1369 if (data_sets == NULL)
1370 return (-1);
1372 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1373 return (-1);
1375 sfree (ds->ds);
1376 sfree (ds);
1378 return (0);
1379 } /* int plugin_unregister_data_set */
1381 int plugin_unregister_log (const char *name)
1382 {
1383 return (plugin_unregister (list_log, name));
1384 }
1386 int plugin_unregister_notification (const char *name)
1387 {
1388 return (plugin_unregister (list_notification, name));
1389 }
1391 void plugin_init_all (void)
1392 {
1393 const char *chain_name;
1394 llentry_t *le;
1395 int status;
1397 /* Init the value cache */
1398 uc_init ();
1400 chain_name = global_option_get ("PreCacheChain");
1401 pre_cache_chain = fc_chain_get_by_name (chain_name);
1403 chain_name = global_option_get ("PostCacheChain");
1404 post_cache_chain = fc_chain_get_by_name (chain_name);
1406 {
1407 char const *tmp = global_option_get ("WriteThreads");
1408 int num = atoi (tmp);
1410 if (num < 1)
1411 num = 5;
1413 start_write_threads ((size_t) num);
1414 }
1416 if ((list_init == NULL) && (read_heap == NULL))
1417 return;
1419 /* Calling all init callbacks before checking if read callbacks
1420 * are available allows the init callbacks to register the read
1421 * callback. */
1422 le = llist_head (list_init);
1423 while (le != NULL)
1424 {
1425 callback_func_t *cf;
1426 plugin_init_cb callback;
1427 plugin_ctx_t old_ctx;
1429 cf = le->value;
1430 old_ctx = plugin_set_ctx (cf->cf_ctx);
1431 callback = cf->cf_callback;
1432 status = (*callback) ();
1433 plugin_set_ctx (old_ctx);
1435 if (status != 0)
1436 {
1437 ERROR ("Initialization of plugin `%s' "
1438 "failed with status %i. "
1439 "Plugin will be unloaded.",
1440 le->key, status);
1441 /* Plugins that register read callbacks from the init
1442 * callback should take care of appropriate error
1443 * handling themselves. */
1444 /* FIXME: Unload _all_ functions */
1445 plugin_unregister_read (le->key);
1446 }
1448 le = le->next;
1449 }
1451 /* Start read-threads */
1452 if (read_heap != NULL)
1453 {
1454 const char *rt;
1455 int num;
1456 rt = global_option_get ("ReadThreads");
1457 num = atoi (rt);
1458 if (num != -1)
1459 start_read_threads ((num > 0) ? num : 5);
1460 }
1461 } /* void plugin_init_all */
1463 /* TODO: Rename this function. */
1464 void plugin_read_all (void)
1465 {
1466 uc_check_timeout ();
1468 return;
1469 } /* void plugin_read_all */
1471 /* Read function called when the `-T' command line argument is given. */
1472 int plugin_read_all_once (void)
1473 {
1474 int status;
1475 int return_status = 0;
1477 if (read_heap == NULL)
1478 {
1479 NOTICE ("No read-functions are registered.");
1480 return (0);
1481 }
1483 while (42)
1484 {
1485 read_func_t *rf;
1486 plugin_ctx_t old_ctx;
1488 rf = c_heap_get_root (read_heap);
1489 if (rf == NULL)
1490 break;
1492 old_ctx = plugin_set_ctx (rf->rf_ctx);
1494 if (rf->rf_type == RF_SIMPLE)
1495 {
1496 int (*callback) (void);
1498 callback = rf->rf_callback;
1499 status = (*callback) ();
1500 }
1501 else
1502 {
1503 plugin_read_cb callback;
1505 callback = rf->rf_callback;
1506 status = (*callback) (&rf->rf_udata);
1507 }
1509 plugin_set_ctx (old_ctx);
1511 if (status != 0)
1512 {
1513 NOTICE ("read-function of plugin `%s' failed.",
1514 rf->rf_name);
1515 return_status = -1;
1516 }
1518 destroy_callback ((void *) rf);
1519 }
1521 return (return_status);
1522 } /* int plugin_read_all_once */
1524 int plugin_write (const char *plugin, /* {{{ */
1525 const data_set_t *ds, const value_list_t *vl)
1526 {
1527 llentry_t *le;
1528 int status;
1530 if (vl == NULL)
1531 return (EINVAL);
1533 if (list_write == NULL)
1534 return (ENOENT);
1536 if (ds == NULL)
1537 {
1538 ds = plugin_get_ds (vl->type);
1539 if (ds == NULL)
1540 {
1541 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1542 return (ENOENT);
1543 }
1544 }
1546 if (plugin == NULL)
1547 {
1548 int success = 0;
1549 int failure = 0;
1551 le = llist_head (list_write);
1552 while (le != NULL)
1553 {
1554 callback_func_t *cf = le->value;
1555 plugin_write_cb callback;
1557 /* do not switch plugin context; rather keep the context (interval)
1558 * information of the calling read plugin */
1560 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1561 callback = cf->cf_callback;
1562 status = (*callback) (ds, vl, &cf->cf_udata);
1563 if (status != 0)
1564 failure++;
1565 else
1566 success++;
1568 le = le->next;
1569 }
1571 if ((success == 0) && (failure != 0))
1572 status = -1;
1573 else
1574 status = 0;
1575 }
1576 else /* plugin != NULL */
1577 {
1578 callback_func_t *cf;
1579 plugin_write_cb callback;
1581 le = llist_head (list_write);
1582 while (le != NULL)
1583 {
1584 if (strcasecmp (plugin, le->key) == 0)
1585 break;
1587 le = le->next;
1588 }
1590 if (le == NULL)
1591 return (ENOENT);
1593 cf = le->value;
1595 /* do not switch plugin context; rather keep the context (interval)
1596 * information of the calling read plugin */
1598 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1599 callback = cf->cf_callback;
1600 status = (*callback) (ds, vl, &cf->cf_udata);
1601 }
1603 return (status);
1604 } /* }}} int plugin_write */
1606 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1607 {
1608 llentry_t *le;
1610 if (list_flush == NULL)
1611 return (0);
1613 le = llist_head (list_flush);
1614 while (le != NULL)
1615 {
1616 callback_func_t *cf;
1617 plugin_flush_cb callback;
1618 plugin_ctx_t old_ctx;
1620 if ((plugin != NULL)
1621 && (strcmp (plugin, le->key) != 0))
1622 {
1623 le = le->next;
1624 continue;
1625 }
1627 cf = le->value;
1628 old_ctx = plugin_set_ctx (cf->cf_ctx);
1629 callback = cf->cf_callback;
1631 (*callback) (timeout, identifier, &cf->cf_udata);
1633 plugin_set_ctx (old_ctx);
1635 le = le->next;
1636 }
1637 return (0);
1638 } /* int plugin_flush */
1640 void plugin_shutdown_all (void)
1641 {
1642 llentry_t *le;
1644 stop_read_threads ();
1646 destroy_all_callbacks (&list_init);
1648 pthread_mutex_lock (&read_lock);
1649 llist_destroy (read_list);
1650 read_list = NULL;
1651 pthread_mutex_unlock (&read_lock);
1653 destroy_read_heap ();
1655 plugin_flush (/* plugin = */ NULL,
1656 /* timeout = */ 0,
1657 /* identifier = */ NULL);
1659 le = NULL;
1660 if (list_shutdown != NULL)
1661 le = llist_head (list_shutdown);
1663 while (le != NULL)
1664 {
1665 callback_func_t *cf;
1666 plugin_shutdown_cb callback;
1667 plugin_ctx_t old_ctx;
1669 cf = le->value;
1670 old_ctx = plugin_set_ctx (cf->cf_ctx);
1671 callback = cf->cf_callback;
1673 /* Advance the pointer before calling the callback allows
1674 * shutdown functions to unregister themselves. If done the
1675 * other way around the memory `le' points to will be freed
1676 * after callback returns. */
1677 le = le->next;
1679 (*callback) ();
1681 plugin_set_ctx (old_ctx);
1682 }
1684 stop_write_threads ();
1686 /* Write plugins which use the `user_data' pointer usually need the
1687 * same data available to the flush callback. If this is the case, set
1688 * the free_function to NULL when registering the flush callback and to
1689 * the real free function when registering the write callback. This way
1690 * the data isn't freed twice. */
1691 destroy_all_callbacks (&list_flush);
1692 destroy_all_callbacks (&list_missing);
1693 destroy_all_callbacks (&list_write);
1695 destroy_all_callbacks (&list_notification);
1696 destroy_all_callbacks (&list_shutdown);
1697 destroy_all_callbacks (&list_log);
1698 } /* void plugin_shutdown_all */
1700 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1701 {
1702 llentry_t *le;
1704 if (list_missing == NULL)
1705 return (0);
1707 le = llist_head (list_missing);
1708 while (le != NULL)
1709 {
1710 callback_func_t *cf;
1711 plugin_missing_cb callback;
1712 plugin_ctx_t old_ctx;
1713 int status;
1715 cf = le->value;
1716 old_ctx = plugin_set_ctx (cf->cf_ctx);
1717 callback = cf->cf_callback;
1719 status = (*callback) (vl, &cf->cf_udata);
1720 plugin_set_ctx (old_ctx);
1721 if (status != 0)
1722 {
1723 if (status < 0)
1724 {
1725 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1726 "failed with status %i.",
1727 le->key, status);
1728 return (status);
1729 }
1730 else
1731 {
1732 return (0);
1733 }
1734 }
1736 le = le->next;
1737 }
1738 return (0);
1739 } /* int }}} plugin_dispatch_missing */
1741 static int plugin_dispatch_values_internal (value_list_t *vl)
1742 {
1743 int status;
1744 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1746 value_t *saved_values;
1747 int saved_values_len;
1749 data_set_t *ds;
1751 int free_meta_data = 0;
1753 if ((vl == NULL) || (vl->type[0] == 0)
1754 || (vl->values == NULL) || (vl->values_len < 1))
1755 {
1756 ERROR ("plugin_dispatch_values: Invalid value list "
1757 "from plugin %s.", vl->plugin);
1758 return (-1);
1759 }
1761 /* Free meta data only if the calling function didn't specify any. In
1762 * this case matches and targets may add some and the calling function
1763 * may not expect (and therefore free) that data. */
1764 if (vl->meta == NULL)
1765 free_meta_data = 1;
1767 if (list_write == NULL)
1768 c_complain_once (LOG_WARNING, &no_write_complaint,
1769 "plugin_dispatch_values: No write callback has been "
1770 "registered. Please load at least one output plugin, "
1771 "if you want the collected data to be stored.");
1773 if (data_sets == NULL)
1774 {
1775 ERROR ("plugin_dispatch_values: No data sets registered. "
1776 "Could the types database be read? Check "
1777 "your `TypesDB' setting!");
1778 return (-1);
1779 }
1781 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1782 {
1783 char ident[6 * DATA_MAX_NAME_LEN];
1785 FORMAT_VL (ident, sizeof (ident), vl);
1786 INFO ("plugin_dispatch_values: Dataset not found: %s "
1787 "(from \"%s\"), check your types.db!",
1788 vl->type, ident);
1789 return (-1);
1790 }
1792 /* Assured by plugin_value_list_clone(). The time is determined at
1793 * _enqueue_ time. */
1794 assert (vl->time != 0);
1795 assert (vl->interval != 0);
1797 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1798 "host = %s; "
1799 "plugin = %s; plugin_instance = %s; "
1800 "type = %s; type_instance = %s;",
1801 CDTIME_T_TO_DOUBLE (vl->time),
1802 CDTIME_T_TO_DOUBLE (vl->interval),
1803 vl->host,
1804 vl->plugin, vl->plugin_instance,
1805 vl->type, vl->type_instance);
1807 #if COLLECT_DEBUG
1808 assert (0 == strcmp (ds->type, vl->type));
1809 #else
1810 if (0 != strcmp (ds->type, vl->type))
1811 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1812 ds->type, vl->type);
1813 #endif
1815 #if COLLECT_DEBUG
1816 assert (ds->ds_num == vl->values_len);
1817 #else
1818 if (ds->ds_num != vl->values_len)
1819 {
1820 ERROR ("plugin_dispatch_values: ds->type = %s: "
1821 "(ds->ds_num = %i) != "
1822 "(vl->values_len = %i)",
1823 ds->type, ds->ds_num, vl->values_len);
1824 return (-1);
1825 }
1826 #endif
1828 escape_slashes (vl->host, sizeof (vl->host));
1829 escape_slashes (vl->plugin, sizeof (vl->plugin));
1830 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1831 escape_slashes (vl->type, sizeof (vl->type));
1832 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1834 /* Copy the values. This way, we can assure `targets' that they get
1835 * dynamically allocated values, which they can free and replace if
1836 * they like. */
1837 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1838 {
1839 saved_values = vl->values;
1840 saved_values_len = vl->values_len;
1842 vl->values = (value_t *) calloc (vl->values_len,
1843 sizeof (*vl->values));
1844 if (vl->values == NULL)
1845 {
1846 ERROR ("plugin_dispatch_values: calloc failed.");
1847 vl->values = saved_values;
1848 return (-1);
1849 }
1850 memcpy (vl->values, saved_values,
1851 vl->values_len * sizeof (*vl->values));
1852 }
1853 else /* if ((pre == NULL) && (post == NULL)) */
1854 {
1855 saved_values = NULL;
1856 saved_values_len = 0;
1857 }
1859 if (pre_cache_chain != NULL)
1860 {
1861 status = fc_process_chain (ds, vl, pre_cache_chain);
1862 if (status < 0)
1863 {
1864 WARNING ("plugin_dispatch_values: Running the "
1865 "pre-cache chain failed with "
1866 "status %i (%#x).",
1867 status, status);
1868 }
1869 else if (status == FC_TARGET_STOP)
1870 {
1871 /* Restore the state of the value_list so that plugins
1872 * don't get confused.. */
1873 if (saved_values != NULL)
1874 {
1875 free (vl->values);
1876 vl->values = saved_values;
1877 vl->values_len = saved_values_len;
1878 }
1879 return (0);
1880 }
1881 }
1883 /* Update the value cache */
1884 uc_update (ds, vl);
1886 if (post_cache_chain != NULL)
1887 {
1888 status = fc_process_chain (ds, vl, post_cache_chain);
1889 if (status < 0)
1890 {
1891 WARNING ("plugin_dispatch_values: Running the "
1892 "post-cache chain failed with "
1893 "status %i (%#x).",
1894 status, status);
1895 }
1896 }
1897 else
1898 fc_default_action (ds, vl);
1900 /* Restore the state of the value_list so that plugins don't get
1901 * confused.. */
1902 if (saved_values != NULL)
1903 {
1904 free (vl->values);
1905 vl->values = saved_values;
1906 vl->values_len = saved_values_len;
1907 }
1909 if ((free_meta_data != 0) && (vl->meta != NULL))
1910 {
1911 meta_data_destroy (vl->meta);
1912 vl->meta = NULL;
1913 }
1915 return (0);
1916 } /* int plugin_dispatch_values_internal */
1918 int plugin_dispatch_values (value_list_t const *vl)
1919 {
1920 int status;
1922 status = plugin_write_enqueue (vl);
1923 if (status != 0)
1924 {
1925 char errbuf[1024];
1926 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
1927 "with status %i (%s).", status,
1928 sstrerror (status, errbuf, sizeof (errbuf)));
1929 return (status);
1930 }
1932 return (0);
1933 }
1935 int plugin_dispatch_notification (const notification_t *notif)
1936 {
1937 llentry_t *le;
1938 /* Possible TODO: Add flap detection here */
1940 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1941 "time = %.3f; host = %s;",
1942 notif->severity, notif->message,
1943 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1945 /* Nobody cares for notifications */
1946 if (list_notification == NULL)
1947 return (-1);
1949 le = llist_head (list_notification);
1950 while (le != NULL)
1951 {
1952 callback_func_t *cf;
1953 plugin_notification_cb callback;
1954 int status;
1956 /* do not switch plugin context; rather keep the context
1957 * (interval) information of the calling plugin */
1959 cf = le->value;
1960 callback = cf->cf_callback;
1961 status = (*callback) (notif, &cf->cf_udata);
1962 if (status != 0)
1963 {
1964 WARNING ("plugin_dispatch_notification: Notification "
1965 "callback %s returned %i.",
1966 le->key, status);
1967 }
1969 le = le->next;
1970 }
1972 return (0);
1973 } /* int plugin_dispatch_notification */
1975 void plugin_log (int level, const char *format, ...)
1976 {
1977 char msg[1024];
1978 va_list ap;
1979 llentry_t *le;
1981 #if !COLLECT_DEBUG
1982 if (level >= LOG_DEBUG)
1983 return;
1984 #endif
1986 va_start (ap, format);
1987 vsnprintf (msg, sizeof (msg), format, ap);
1988 msg[sizeof (msg) - 1] = '\0';
1989 va_end (ap);
1991 if (list_log == NULL)
1992 {
1993 fprintf (stderr, "%s\n", msg);
1994 return;
1995 }
1997 le = llist_head (list_log);
1998 while (le != NULL)
1999 {
2000 callback_func_t *cf;
2001 plugin_log_cb callback;
2003 cf = le->value;
2004 callback = cf->cf_callback;
2006 /* do not switch plugin context; rather keep the context
2007 * (interval) information of the calling plugin */
2009 (*callback) (level, msg, &cf->cf_udata);
2011 le = le->next;
2012 }
2013 } /* void plugin_log */
2015 int parse_log_severity (const char *severity)
2016 {
2017 int log_level = -1;
2019 if ((0 == strcasecmp (severity, "emerg"))
2020 || (0 == strcasecmp (severity, "alert"))
2021 || (0 == strcasecmp (severity, "crit"))
2022 || (0 == strcasecmp (severity, "err")))
2023 log_level = LOG_ERR;
2024 else if (0 == strcasecmp (severity, "warning"))
2025 log_level = LOG_WARNING;
2026 else if (0 == strcasecmp (severity, "notice"))
2027 log_level = LOG_NOTICE;
2028 else if (0 == strcasecmp (severity, "info"))
2029 log_level = LOG_INFO;
2030 #if COLLECT_DEBUG
2031 else if (0 == strcasecmp (severity, "debug"))
2032 log_level = LOG_DEBUG;
2033 #endif /* COLLECT_DEBUG */
2035 return (log_level);
2036 } /* int parse_log_severity */
2038 int parse_notif_severity (const char *severity)
2039 {
2040 int notif_severity = -1;
2042 if (strcasecmp (severity, "FAILURE") == 0)
2043 notif_severity = NOTIF_FAILURE;
2044 else if (strcmp (severity, "OKAY") == 0)
2045 notif_severity = NOTIF_OKAY;
2046 else if ((strcmp (severity, "WARNING") == 0)
2047 || (strcmp (severity, "WARN") == 0))
2048 notif_severity = NOTIF_WARNING;
2050 return (notif_severity);
2051 } /* int parse_notif_severity */
2053 const data_set_t *plugin_get_ds (const char *name)
2054 {
2055 data_set_t *ds;
2057 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2058 {
2059 DEBUG ("No such dataset registered: %s", name);
2060 return (NULL);
2061 }
2063 return (ds);
2064 } /* data_set_t *plugin_get_ds */
2066 static int plugin_notification_meta_add (notification_t *n,
2067 const char *name,
2068 enum notification_meta_type_e type,
2069 const void *value)
2070 {
2071 notification_meta_t *meta;
2072 notification_meta_t *tail;
2074 if ((n == NULL) || (name == NULL) || (value == NULL))
2075 {
2076 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2077 return (-1);
2078 }
2080 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2081 if (meta == NULL)
2082 {
2083 ERROR ("plugin_notification_meta_add: malloc failed.");
2084 return (-1);
2085 }
2086 memset (meta, 0, sizeof (notification_meta_t));
2088 sstrncpy (meta->name, name, sizeof (meta->name));
2089 meta->type = type;
2091 switch (type)
2092 {
2093 case NM_TYPE_STRING:
2094 {
2095 meta->nm_value.nm_string = strdup ((const char *) value);
2096 if (meta->nm_value.nm_string == NULL)
2097 {
2098 ERROR ("plugin_notification_meta_add: strdup failed.");
2099 sfree (meta);
2100 return (-1);
2101 }
2102 break;
2103 }
2104 case NM_TYPE_SIGNED_INT:
2105 {
2106 meta->nm_value.nm_signed_int = *((int64_t *) value);
2107 break;
2108 }
2109 case NM_TYPE_UNSIGNED_INT:
2110 {
2111 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2112 break;
2113 }
2114 case NM_TYPE_DOUBLE:
2115 {
2116 meta->nm_value.nm_double = *((double *) value);
2117 break;
2118 }
2119 case NM_TYPE_BOOLEAN:
2120 {
2121 meta->nm_value.nm_boolean = *((_Bool *) value);
2122 break;
2123 }
2124 default:
2125 {
2126 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2127 sfree (meta);
2128 return (-1);
2129 }
2130 } /* switch (type) */
2132 meta->next = NULL;
2133 tail = n->meta;
2134 while ((tail != NULL) && (tail->next != NULL))
2135 tail = tail->next;
2137 if (tail == NULL)
2138 n->meta = meta;
2139 else
2140 tail->next = meta;
2142 return (0);
2143 } /* int plugin_notification_meta_add */
2145 int plugin_notification_meta_add_string (notification_t *n,
2146 const char *name,
2147 const char *value)
2148 {
2149 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2150 }
2152 int plugin_notification_meta_add_signed_int (notification_t *n,
2153 const char *name,
2154 int64_t value)
2155 {
2156 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2157 }
2159 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2160 const char *name,
2161 uint64_t value)
2162 {
2163 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2164 }
2166 int plugin_notification_meta_add_double (notification_t *n,
2167 const char *name,
2168 double value)
2169 {
2170 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2171 }
2173 int plugin_notification_meta_add_boolean (notification_t *n,
2174 const char *name,
2175 _Bool value)
2176 {
2177 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2178 }
2180 int plugin_notification_meta_copy (notification_t *dst,
2181 const notification_t *src)
2182 {
2183 notification_meta_t *meta;
2185 assert (dst != NULL);
2186 assert (src != NULL);
2187 assert (dst != src);
2188 assert ((src->meta == NULL) || (src->meta != dst->meta));
2190 for (meta = src->meta; meta != NULL; meta = meta->next)
2191 {
2192 if (meta->type == NM_TYPE_STRING)
2193 plugin_notification_meta_add_string (dst, meta->name,
2194 meta->nm_value.nm_string);
2195 else if (meta->type == NM_TYPE_SIGNED_INT)
2196 plugin_notification_meta_add_signed_int (dst, meta->name,
2197 meta->nm_value.nm_signed_int);
2198 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2199 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2200 meta->nm_value.nm_unsigned_int);
2201 else if (meta->type == NM_TYPE_DOUBLE)
2202 plugin_notification_meta_add_double (dst, meta->name,
2203 meta->nm_value.nm_double);
2204 else if (meta->type == NM_TYPE_BOOLEAN)
2205 plugin_notification_meta_add_boolean (dst, meta->name,
2206 meta->nm_value.nm_boolean);
2207 }
2209 return (0);
2210 } /* int plugin_notification_meta_copy */
2212 int plugin_notification_meta_free (notification_meta_t *n)
2213 {
2214 notification_meta_t *this;
2215 notification_meta_t *next;
2217 if (n == NULL)
2218 {
2219 ERROR ("plugin_notification_meta_free: n == NULL!");
2220 return (-1);
2221 }
2223 this = n;
2224 while (this != NULL)
2225 {
2226 next = this->next;
2228 if (this->type == NM_TYPE_STRING)
2229 {
2230 free ((char *)this->nm_value.nm_string);
2231 this->nm_value.nm_string = NULL;
2232 }
2233 sfree (this);
2235 this = next;
2236 }
2238 return (0);
2239 } /* int plugin_notification_meta_free */
2241 static void plugin_ctx_destructor (void *ctx)
2242 {
2243 sfree (ctx);
2244 } /* void plugin_ctx_destructor */
2246 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2248 static plugin_ctx_t *plugin_ctx_create (void)
2249 {
2250 plugin_ctx_t *ctx;
2252 ctx = malloc (sizeof (*ctx));
2253 if (ctx == NULL) {
2254 char errbuf[1024];
2255 ERROR ("Failed to allocate plugin context: %s",
2256 sstrerror (errno, errbuf, sizeof (errbuf)));
2257 return NULL;
2258 }
2260 *ctx = ctx_init;
2261 assert (plugin_ctx_key_initialized);
2262 pthread_setspecific (plugin_ctx_key, ctx);
2263 DEBUG("Created new plugin context.");
2264 return (ctx);
2265 } /* int plugin_ctx_create */
2267 void plugin_init_ctx (void)
2268 {
2269 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2270 plugin_ctx_key_initialized = 1;
2271 } /* void plugin_init_ctx */
2273 plugin_ctx_t plugin_get_ctx (void)
2274 {
2275 plugin_ctx_t *ctx;
2277 assert (plugin_ctx_key_initialized);
2278 ctx = pthread_getspecific (plugin_ctx_key);
2280 if (ctx == NULL) {
2281 ctx = plugin_ctx_create ();
2282 /* this must no happen -- exit() instead? */
2283 if (ctx == NULL)
2284 return ctx_init;
2285 }
2287 return (*ctx);
2288 } /* plugin_ctx_t plugin_get_ctx */
2290 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2291 {
2292 plugin_ctx_t *c;
2293 plugin_ctx_t old;
2295 assert (plugin_ctx_key_initialized);
2296 c = pthread_getspecific (plugin_ctx_key);
2298 if (c == NULL) {
2299 c = plugin_ctx_create ();
2300 /* this must no happen -- exit() instead? */
2301 if (c == NULL)
2302 return ctx_init;
2303 }
2305 old = *c;
2306 *c = ctx;
2308 return (old);
2309 } /* void plugin_set_ctx */
2311 cdtime_t plugin_get_interval (void)
2312 {
2313 cdtime_t interval;
2315 interval = plugin_get_ctx().interval;
2316 if (interval > 0)
2317 return interval;
2319 return cf_get_default_interval ();
2320 } /* cdtime_t plugin_get_interval */
2322 typedef struct {
2323 plugin_ctx_t ctx;
2324 void *(*start_routine) (void *);
2325 void *arg;
2326 } plugin_thread_t;
2328 static void *plugin_thread_start (void *arg)
2329 {
2330 plugin_thread_t *plugin_thread = arg;
2332 void *(*start_routine) (void *) = plugin_thread->start_routine;
2333 void *plugin_arg = plugin_thread->arg;
2335 plugin_set_ctx (plugin_thread->ctx);
2337 free (plugin_thread);
2339 return start_routine (plugin_arg);
2340 } /* void *plugin_thread_start */
2342 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2343 void *(*start_routine) (void *), void *arg)
2344 {
2345 plugin_thread_t *plugin_thread;
2347 plugin_thread = malloc (sizeof (*plugin_thread));
2348 if (plugin_thread == NULL)
2349 return -1;
2351 plugin_thread->ctx = plugin_get_ctx ();
2352 plugin_thread->start_routine = start_routine;
2353 plugin_thread->arg = arg;
2355 return pthread_create (thread, attr,
2356 plugin_thread_start, plugin_thread);
2357 } /* int plugin_thread_create */
2359 /* vim: set sw=8 ts=8 noet fdm=marker : */