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 "utils_complain.h"
26 #include <ltdl.h>
28 #if HAVE_PTHREAD_H
29 # include <pthread.h>
30 #endif
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_heap.h"
38 #include "utils_cache.h"
39 #include "filter_chain.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 struct timespec rf_interval;
67 struct timespec rf_effective_interval;
68 struct timespec 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 _Bool timeout_reached(struct timespec timeout)
364 {
365 struct timeval now;
366 gettimeofday(&now, NULL);
367 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
368 }
370 static void *plugin_read_thread (void __attribute__((unused)) *args)
371 {
372 while (read_loop != 0)
373 {
374 read_func_t *rf;
375 plugin_ctx_t old_ctx;
376 cdtime_t now;
377 int status;
378 int rf_type;
379 int rc;
381 /* Get the read function that needs to be read next.
382 * We don't need to hold "read_lock" for the heap, but we need
383 * to call c_heap_get_root() and pthread_cond_wait() in the
384 * same protected block. */
385 pthread_mutex_lock (&read_lock);
386 rf = c_heap_get_root (read_heap);
387 if (rf == NULL)
388 {
389 pthread_cond_wait (&read_cond, &read_lock);
390 pthread_mutex_unlock (&read_lock);
391 continue;
392 }
393 pthread_mutex_unlock (&read_lock);
395 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
396 {
397 /* this should not happen, because the interval is set
398 * for each plugin when loading it
399 * XXX: issue a warning? */
400 now = cdtime ();
402 CDTIME_T_TO_TIMESPEC (plugin_get_interval (), &rf->rf_interval);
404 rf->rf_effective_interval = rf->rf_interval;
406 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
407 }
409 /* sleep until this entry is due,
410 * using pthread_cond_timedwait */
411 pthread_mutex_lock (&read_lock);
412 /* In pthread_cond_timedwait, spurious wakeups are possible
413 * (and really happen, at least on NetBSD with > 1 CPU), thus
414 * we need to re-evaluate the condition every time
415 * pthread_cond_timedwait returns. */
416 rc = 0;
417 while ((read_loop != 0)
418 && !timeout_reached(rf->rf_next_read)
419 && rc == 0)
420 {
421 rc = pthread_cond_timedwait (&read_cond, &read_lock,
422 &rf->rf_next_read);
423 }
425 /* Must hold `read_lock' when accessing `rf->rf_type'. */
426 rf_type = rf->rf_type;
427 pthread_mutex_unlock (&read_lock);
429 /* Check if we're supposed to stop.. This may have interrupted
430 * the sleep, too. */
431 if (read_loop == 0)
432 {
433 /* Insert `rf' again, so it can be free'd correctly */
434 c_heap_insert (read_heap, rf);
435 break;
436 }
438 /* The entry has been marked for deletion. The linked list
439 * entry has already been removed by `plugin_unregister_read'.
440 * All we have to do here is free the `read_func_t' and
441 * continue. */
442 if (rf_type == RF_REMOVE)
443 {
444 DEBUG ("plugin_read_thread: Destroying the `%s' "
445 "callback.", rf->rf_name);
446 destroy_callback ((callback_func_t *) rf);
447 rf = NULL;
448 continue;
449 }
451 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
453 old_ctx = plugin_set_ctx (rf->rf_ctx);
455 if (rf_type == RF_SIMPLE)
456 {
457 int (*callback) (void);
459 callback = rf->rf_callback;
460 status = (*callback) ();
461 }
462 else
463 {
464 plugin_read_cb callback;
466 assert (rf_type == RF_COMPLEX);
468 callback = rf->rf_callback;
469 status = (*callback) (&rf->rf_udata);
470 }
472 plugin_set_ctx (old_ctx);
474 /* If the function signals failure, we will increase the
475 * intervals in which it will be called. */
476 if (status != 0)
477 {
478 rf->rf_effective_interval.tv_sec *= 2;
479 rf->rf_effective_interval.tv_nsec *= 2;
480 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
482 if (rf->rf_effective_interval.tv_sec >= 86400)
483 {
484 rf->rf_effective_interval.tv_sec = 86400;
485 rf->rf_effective_interval.tv_nsec = 0;
486 }
488 NOTICE ("read-function of plugin `%s' failed. "
489 "Will suspend it for %i seconds.",
490 rf->rf_name,
491 (int) rf->rf_effective_interval.tv_sec);
492 }
493 else
494 {
495 /* Success: Restore the interval, if it was changed. */
496 rf->rf_effective_interval = rf->rf_interval;
497 }
499 /* update the ``next read due'' field */
500 now = cdtime ();
502 DEBUG ("plugin_read_thread: Effective interval of the "
503 "%s plugin is %i.%09i.",
504 rf->rf_name,
505 (int) rf->rf_effective_interval.tv_sec,
506 (int) rf->rf_effective_interval.tv_nsec);
508 /* Calculate the next (absolute) time at which this function
509 * should be called. */
510 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
511 + rf->rf_effective_interval.tv_sec;
512 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
513 + rf->rf_effective_interval.tv_nsec;
514 NORMALIZE_TIMESPEC (rf->rf_next_read);
516 /* Check, if `rf_next_read' is in the past. */
517 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
518 {
519 /* `rf_next_read' is in the past. Insert `now'
520 * so this value doesn't trail off into the
521 * past too much. */
522 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
523 }
525 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
526 rf->rf_name,
527 (int) rf->rf_next_read.tv_sec,
528 (int) rf->rf_next_read.tv_nsec);
530 /* Re-insert this read function into the heap again. */
531 c_heap_insert (read_heap, rf);
532 } /* while (read_loop) */
534 pthread_exit (NULL);
535 return ((void *) 0);
536 } /* void *plugin_read_thread */
538 static void start_read_threads (int num)
539 {
540 int i;
542 if (read_threads != NULL)
543 return;
545 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
546 if (read_threads == NULL)
547 {
548 ERROR ("plugin: start_read_threads: calloc failed.");
549 return;
550 }
552 read_threads_num = 0;
553 for (i = 0; i < num; i++)
554 {
555 if (pthread_create (read_threads + read_threads_num, NULL,
556 plugin_read_thread, NULL) == 0)
557 {
558 read_threads_num++;
559 }
560 else
561 {
562 ERROR ("plugin: start_read_threads: pthread_create failed.");
563 return;
564 }
565 } /* for (i) */
566 } /* void start_read_threads */
568 static void stop_read_threads (void)
569 {
570 int i;
572 if (read_threads == NULL)
573 return;
575 INFO ("collectd: Stopping %i read threads.", read_threads_num);
577 pthread_mutex_lock (&read_lock);
578 read_loop = 0;
579 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
580 pthread_cond_broadcast (&read_cond);
581 pthread_mutex_unlock (&read_lock);
583 for (i = 0; i < read_threads_num; i++)
584 {
585 if (pthread_join (read_threads[i], NULL) != 0)
586 {
587 ERROR ("plugin: stop_read_threads: pthread_join failed.");
588 }
589 read_threads[i] = (pthread_t) 0;
590 }
591 sfree (read_threads);
592 read_threads_num = 0;
593 } /* void stop_read_threads */
595 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
596 {
597 if (vl == NULL)
598 return;
600 meta_data_destroy (vl->meta);
601 sfree (vl->values);
602 sfree (vl);
603 } /* }}} void plugin_value_list_free */
605 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
606 {
607 value_list_t *vl;
609 if (vl_orig == NULL)
610 return (NULL);
612 vl = malloc (sizeof (*vl));
613 if (vl == NULL)
614 return (NULL);
615 memcpy (vl, vl_orig, sizeof (*vl));
617 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
618 if (vl->values == NULL)
619 {
620 plugin_value_list_free (vl);
621 return (NULL);
622 }
623 memcpy (vl->values, vl_orig->values,
624 vl_orig->values_len * sizeof (*vl->values));
626 vl->meta = meta_data_clone (vl->meta);
627 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
628 {
629 plugin_value_list_free (vl);
630 return (NULL);
631 }
633 if (vl->time == 0)
634 vl->time = cdtime ();
636 /* Fill in the interval from the thread context, if it is zero. */
637 if (vl->interval == 0)
638 {
639 plugin_ctx_t ctx = plugin_get_ctx ();
641 if (ctx.interval != 0)
642 vl->interval = ctx.interval;
643 else
644 {
645 char name[6 * DATA_MAX_NAME_LEN];
646 FORMAT_VL (name, sizeof (name), vl);
647 ERROR ("plugin_value_list_clone: Unable to determine "
648 "interval from context for "
649 "value list \"%s\". "
650 "This indicates a broken plugin. "
651 "Please report this problem to the "
652 "collectd mailing list or at "
653 "<http://collectd.org/bugs/>.", name);
654 vl->interval = cf_get_default_interval ();
655 }
656 }
658 return (vl);
659 } /* }}} value_list_t *plugin_value_list_clone */
661 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
662 {
663 write_queue_t *q;
665 q = malloc (sizeof (*q));
666 if (q == NULL)
667 return (ENOMEM);
668 q->next = NULL;
670 q->vl = plugin_value_list_clone (vl);
671 if (q->vl == NULL)
672 {
673 sfree (q);
674 return (ENOMEM);
675 }
677 /* Store context of caller (read plugin); otherwise, it would not be
678 * available to the write plugins when actually dispatching the
679 * value-list later on. */
680 q->ctx = plugin_get_ctx ();
682 pthread_mutex_lock (&write_lock);
684 if (write_queue_tail == NULL)
685 {
686 write_queue_head = q;
687 write_queue_tail = q;
688 }
689 else
690 {
691 write_queue_tail->next = q;
692 write_queue_tail = q;
693 }
695 pthread_cond_signal (&write_cond);
696 pthread_mutex_unlock (&write_lock);
698 return (0);
699 } /* }}} int plugin_write_enqueue */
701 static value_list_t *plugin_write_dequeue (void) /* {{{ */
702 {
703 write_queue_t *q;
704 value_list_t *vl;
706 pthread_mutex_lock (&write_lock);
708 while (write_loop && (write_queue_head == NULL))
709 pthread_cond_wait (&write_cond, &write_lock);
711 if (write_queue_head == NULL)
712 {
713 pthread_mutex_unlock (&write_lock);
714 return (NULL);
715 }
717 q = write_queue_head;
718 write_queue_head = q->next;
719 if (write_queue_head == NULL)
720 write_queue_tail = NULL;
722 pthread_mutex_unlock (&write_lock);
724 (void) plugin_set_ctx (q->ctx);
726 vl = q->vl;
727 sfree (q);
728 return (vl);
729 } /* }}} value_list_t *plugin_write_dequeue */
731 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
732 {
733 while (write_loop)
734 {
735 value_list_t *vl = plugin_write_dequeue ();
736 if (vl == NULL)
737 continue;
739 plugin_dispatch_values_internal (vl);
741 plugin_value_list_free (vl);
742 }
744 pthread_exit (NULL);
745 return ((void *) 0);
746 } /* }}} void *plugin_write_thread */
748 static void start_write_threads (size_t num) /* {{{ */
749 {
750 size_t i;
752 if (write_threads != NULL)
753 return;
755 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
756 if (write_threads == NULL)
757 {
758 ERROR ("plugin: start_write_threads: calloc failed.");
759 return;
760 }
762 write_threads_num = 0;
763 for (i = 0; i < num; i++)
764 {
765 int status;
767 status = pthread_create (write_threads + write_threads_num,
768 /* attr = */ NULL,
769 plugin_write_thread,
770 /* arg = */ NULL);
771 if (status != 0)
772 {
773 char errbuf[1024];
774 ERROR ("plugin: start_write_threads: pthread_create failed "
775 "with status %i (%s).", status,
776 sstrerror (status, errbuf, sizeof (errbuf)));
777 return;
778 }
780 write_threads_num++;
781 } /* for (i) */
782 } /* }}} void start_write_threads */
784 static void stop_write_threads (void) /* {{{ */
785 {
786 write_queue_t *q;
787 int i;
789 if (write_threads == NULL)
790 return;
792 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
794 pthread_mutex_lock (&write_lock);
795 write_loop = 0;
796 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
797 pthread_cond_broadcast (&write_cond);
798 pthread_mutex_unlock (&write_lock);
800 for (i = 0; i < write_threads_num; i++)
801 {
802 if (pthread_join (write_threads[i], NULL) != 0)
803 {
804 ERROR ("plugin: stop_write_threads: pthread_join failed.");
805 }
806 write_threads[i] = (pthread_t) 0;
807 }
808 sfree (write_threads);
809 write_threads_num = 0;
811 pthread_mutex_lock (&write_lock);
812 i = 0;
813 for (q = write_queue_head; q != NULL; q = q->next)
814 {
815 plugin_value_list_free (q->vl);
816 sfree (q);
817 i++;
818 }
819 write_queue_head = NULL;
820 write_queue_tail = NULL;
821 pthread_mutex_unlock (&write_lock);
823 if (i > 0)
824 {
825 WARNING ("plugin: %i value list%s left after shutting down "
826 "the write threads.",
827 i, (i == 1) ? " was" : "s were");
828 }
829 } /* }}} void stop_write_threads */
831 /*
832 * Public functions
833 */
834 void plugin_set_dir (const char *dir)
835 {
836 if (plugindir != NULL)
837 free (plugindir);
839 if (dir == NULL)
840 plugindir = NULL;
841 else if ((plugindir = strdup (dir)) == NULL)
842 {
843 char errbuf[1024];
844 ERROR ("strdup failed: %s",
845 sstrerror (errno, errbuf, sizeof (errbuf)));
846 }
847 }
849 #define BUFSIZE 512
850 int plugin_load (const char *type, uint32_t flags)
851 {
852 DIR *dh;
853 const char *dir;
854 char filename[BUFSIZE] = "";
855 char typename[BUFSIZE];
856 int typename_len;
857 int ret;
858 struct stat statbuf;
859 struct dirent *de;
860 int status;
862 dir = plugin_get_dir ();
863 ret = 1;
865 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
866 * type when matching the filename */
867 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
868 if ((status < 0) || ((size_t) status >= sizeof (typename)))
869 {
870 WARNING ("plugin_load: Filename too long: \"%s.so\"", type);
871 return (-1);
872 }
873 typename_len = strlen (typename);
875 if ((dh = opendir (dir)) == NULL)
876 {
877 char errbuf[1024];
878 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
879 sstrerror (errno, errbuf, sizeof (errbuf)));
880 return (-1);
881 }
883 while ((de = readdir (dh)) != NULL)
884 {
885 if (strncasecmp (de->d_name, typename, typename_len))
886 continue;
888 status = ssnprintf (filename, sizeof (filename),
889 "%s/%s", dir, de->d_name);
890 if ((status < 0) || ((size_t) status >= sizeof (filename)))
891 {
892 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
893 dir, de->d_name);
894 continue;
895 }
897 if (lstat (filename, &statbuf) == -1)
898 {
899 char errbuf[1024];
900 WARNING ("plugin_load: stat (\"%s\") failed: %s",
901 filename,
902 sstrerror (errno, errbuf, sizeof (errbuf)));
903 continue;
904 }
905 else if (!S_ISREG (statbuf.st_mode))
906 {
907 /* don't follow symlinks */
908 WARNING ("plugin_load: %s is not a regular file.",
909 filename);
910 continue;
911 }
913 status = plugin_load_file (filename, flags);
914 if (status == 0)
915 {
916 /* success */
917 ret = 0;
918 break;
919 }
920 else
921 {
922 ERROR ("plugin_load: Load plugin \"%s\" failed with "
923 "status %i.", type, status);
924 }
925 }
927 closedir (dh);
929 if (filename[0] == 0)
930 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
931 type, dir);
933 return (ret);
934 }
936 /*
937 * The `register_*' functions follow
938 */
939 int plugin_register_config (const char *name,
940 int (*callback) (const char *key, const char *val),
941 const char **keys, int keys_num)
942 {
943 cf_register (name, callback, keys, keys_num);
944 return (0);
945 } /* int plugin_register_config */
947 int plugin_register_complex_config (const char *type,
948 int (*callback) (oconfig_item_t *))
949 {
950 return (cf_register_complex (type, callback));
951 } /* int plugin_register_complex_config */
953 int plugin_register_init (const char *name,
954 int (*callback) (void))
955 {
956 return (create_register_callback (&list_init, name, (void *) callback,
957 /* user_data = */ NULL));
958 } /* plugin_register_init */
960 static int plugin_compare_read_func (const void *arg0, const void *arg1)
961 {
962 const read_func_t *rf0;
963 const read_func_t *rf1;
965 rf0 = arg0;
966 rf1 = arg1;
968 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
969 return (-1);
970 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
971 return (1);
972 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
973 return (-1);
974 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
975 return (1);
976 else
977 return (0);
978 } /* int plugin_compare_read_func */
980 /* Add a read function to both, the heap and a linked list. The linked list if
981 * used to look-up read functions, especially for the remove function. The heap
982 * is used to determine which plugin to read next. */
983 static int plugin_insert_read (read_func_t *rf)
984 {
985 int status;
986 llentry_t *le;
988 pthread_mutex_lock (&read_lock);
990 if (read_list == NULL)
991 {
992 read_list = llist_create ();
993 if (read_list == NULL)
994 {
995 pthread_mutex_unlock (&read_lock);
996 ERROR ("plugin_insert_read: read_list failed.");
997 return (-1);
998 }
999 }
1001 if (read_heap == NULL)
1002 {
1003 read_heap = c_heap_create (plugin_compare_read_func);
1004 if (read_heap == NULL)
1005 {
1006 pthread_mutex_unlock (&read_lock);
1007 ERROR ("plugin_insert_read: c_heap_create failed.");
1008 return (-1);
1009 }
1010 }
1012 le = llist_search (read_list, rf->rf_name);
1013 if (le != NULL)
1014 {
1015 pthread_mutex_unlock (&read_lock);
1016 WARNING ("The read function \"%s\" is already registered. "
1017 "Check for duplicate \"LoadPlugin\" lines "
1018 "in your configuration!",
1019 rf->rf_name);
1020 return (EINVAL);
1021 }
1023 le = llentry_create (rf->rf_name, rf);
1024 if (le == NULL)
1025 {
1026 pthread_mutex_unlock (&read_lock);
1027 ERROR ("plugin_insert_read: llentry_create failed.");
1028 return (-1);
1029 }
1031 status = c_heap_insert (read_heap, rf);
1032 if (status != 0)
1033 {
1034 pthread_mutex_unlock (&read_lock);
1035 ERROR ("plugin_insert_read: c_heap_insert failed.");
1036 llentry_destroy (le);
1037 return (-1);
1038 }
1040 /* This does not fail. */
1041 llist_append (read_list, le);
1043 /* Wake up all the read threads. */
1044 pthread_cond_broadcast (&read_cond);
1045 pthread_mutex_unlock (&read_lock);
1046 return (0);
1047 } /* int plugin_insert_read */
1049 static int read_cb_wrapper (user_data_t *ud)
1050 {
1051 int (*callback) (void);
1053 if (ud == NULL)
1054 return -1;
1056 callback = ud->data;
1057 return callback();
1058 } /* int read_cb_wrapper */
1060 int plugin_register_read (const char *name,
1061 int (*callback) (void))
1062 {
1063 read_func_t *rf;
1064 plugin_ctx_t ctx = plugin_get_ctx ();
1065 int status;
1067 if (ctx.interval != 0) {
1068 /* If ctx.interval is not zero (== use the plugin or global
1069 * interval), we need to use the "complex" read callback,
1070 * because only that allows to specify a different interval.
1071 * Wrap the callback using read_cb_wrapper(). */
1072 struct timespec interval;
1073 user_data_t user_data;
1075 user_data.data = callback;
1076 user_data.free_func = NULL;
1078 CDTIME_T_TO_TIMESPEC (ctx.interval, &interval);
1079 return plugin_register_complex_read (/* group = */ NULL,
1080 name, read_cb_wrapper, &interval, &user_data);
1081 }
1083 DEBUG ("plugin_register_read: default_interval = %.3f",
1084 CDTIME_T_TO_DOUBLE(plugin_get_interval ()));
1086 rf = malloc (sizeof (*rf));
1087 if (rf == NULL)
1088 {
1089 ERROR ("plugin_register_read: malloc failed.");
1090 return (ENOMEM);
1091 }
1093 memset (rf, 0, sizeof (read_func_t));
1094 rf->rf_callback = (void *) callback;
1095 rf->rf_udata.data = NULL;
1096 rf->rf_udata.free_func = NULL;
1097 rf->rf_ctx = ctx;
1098 rf->rf_group[0] = '\0';
1099 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1100 rf->rf_type = RF_SIMPLE;
1101 rf->rf_interval.tv_sec = 0;
1102 rf->rf_interval.tv_nsec = 0;
1103 rf->rf_effective_interval = rf->rf_interval;
1105 status = plugin_insert_read (rf);
1106 if (status != 0)
1107 sfree (rf);
1109 return (status);
1110 } /* int plugin_register_read */
1112 int plugin_register_complex_read (const char *group, const char *name,
1113 plugin_read_cb callback,
1114 const struct timespec *interval,
1115 user_data_t *user_data)
1116 {
1117 read_func_t *rf;
1118 plugin_ctx_t ctx = plugin_get_ctx ();
1119 int status;
1121 rf = malloc (sizeof (*rf));
1122 if (rf == NULL)
1123 {
1124 ERROR ("plugin_register_complex_read: malloc failed.");
1125 return (ENOMEM);
1126 }
1128 memset (rf, 0, sizeof (read_func_t));
1129 rf->rf_callback = (void *) callback;
1130 if (group != NULL)
1131 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1132 else
1133 rf->rf_group[0] = '\0';
1134 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1135 rf->rf_type = RF_COMPLEX;
1136 if (interval != NULL)
1137 {
1138 rf->rf_interval = *interval;
1139 }
1140 else if (ctx.interval != 0)
1141 {
1142 CDTIME_T_TO_TIMESPEC (ctx.interval, &rf->rf_interval);
1143 }
1144 rf->rf_effective_interval = rf->rf_interval;
1146 DEBUG ("plugin_register_read: interval = %i.%09i",
1147 (int) rf->rf_interval.tv_sec,
1148 (int) rf->rf_interval.tv_nsec);
1150 /* Set user data */
1151 if (user_data == NULL)
1152 {
1153 rf->rf_udata.data = NULL;
1154 rf->rf_udata.free_func = NULL;
1155 }
1156 else
1157 {
1158 rf->rf_udata = *user_data;
1159 }
1161 rf->rf_ctx = ctx;
1163 status = plugin_insert_read (rf);
1164 if (status != 0)
1165 sfree (rf);
1167 return (status);
1168 } /* int plugin_register_complex_read */
1170 int plugin_register_write (const char *name,
1171 plugin_write_cb callback, user_data_t *ud)
1172 {
1173 return (create_register_callback (&list_write, name,
1174 (void *) callback, ud));
1175 } /* int plugin_register_write */
1177 int plugin_register_flush (const char *name,
1178 plugin_flush_cb callback, user_data_t *ud)
1179 {
1180 return (create_register_callback (&list_flush, name,
1181 (void *) callback, ud));
1182 } /* int plugin_register_flush */
1184 int plugin_register_missing (const char *name,
1185 plugin_missing_cb callback, user_data_t *ud)
1186 {
1187 return (create_register_callback (&list_missing, name,
1188 (void *) callback, ud));
1189 } /* int plugin_register_missing */
1191 int plugin_register_shutdown (const char *name,
1192 int (*callback) (void))
1193 {
1194 return (create_register_callback (&list_shutdown, name,
1195 (void *) callback, /* user_data = */ NULL));
1196 } /* int plugin_register_shutdown */
1198 int plugin_register_data_set (const data_set_t *ds)
1199 {
1200 data_set_t *ds_copy;
1201 int i;
1203 if ((data_sets != NULL)
1204 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1205 {
1206 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1207 plugin_unregister_data_set (ds->type);
1208 }
1209 else if (data_sets == NULL)
1210 {
1211 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1212 if (data_sets == NULL)
1213 return (-1);
1214 }
1216 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1217 if (ds_copy == NULL)
1218 return (-1);
1219 memcpy(ds_copy, ds, sizeof (data_set_t));
1221 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1222 * ds->ds_num);
1223 if (ds_copy->ds == NULL)
1224 {
1225 free (ds_copy);
1226 return (-1);
1227 }
1229 for (i = 0; i < ds->ds_num; i++)
1230 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1232 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1233 } /* int plugin_register_data_set */
1235 int plugin_register_log (const char *name,
1236 plugin_log_cb callback, user_data_t *ud)
1237 {
1238 return (create_register_callback (&list_log, name,
1239 (void *) callback, ud));
1240 } /* int plugin_register_log */
1242 int plugin_register_notification (const char *name,
1243 plugin_notification_cb callback, user_data_t *ud)
1244 {
1245 return (create_register_callback (&list_notification, name,
1246 (void *) callback, ud));
1247 } /* int plugin_register_log */
1249 int plugin_unregister_config (const char *name)
1250 {
1251 cf_unregister (name);
1252 return (0);
1253 } /* int plugin_unregister_config */
1255 int plugin_unregister_complex_config (const char *name)
1256 {
1257 cf_unregister_complex (name);
1258 return (0);
1259 } /* int plugin_unregister_complex_config */
1261 int plugin_unregister_init (const char *name)
1262 {
1263 return (plugin_unregister (list_init, name));
1264 }
1266 int plugin_unregister_read (const char *name) /* {{{ */
1267 {
1268 llentry_t *le;
1269 read_func_t *rf;
1271 if (name == NULL)
1272 return (-ENOENT);
1274 pthread_mutex_lock (&read_lock);
1276 if (read_list == NULL)
1277 {
1278 pthread_mutex_unlock (&read_lock);
1279 return (-ENOENT);
1280 }
1282 le = llist_search (read_list, name);
1283 if (le == NULL)
1284 {
1285 pthread_mutex_unlock (&read_lock);
1286 WARNING ("plugin_unregister_read: No such read function: %s",
1287 name);
1288 return (-ENOENT);
1289 }
1291 llist_remove (read_list, le);
1293 rf = le->value;
1294 assert (rf != NULL);
1295 rf->rf_type = RF_REMOVE;
1297 pthread_mutex_unlock (&read_lock);
1299 llentry_destroy (le);
1301 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1303 return (0);
1304 } /* }}} int plugin_unregister_read */
1306 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1307 {
1308 read_func_t *rf = e->value;
1309 char *group = ud;
1311 return strcmp (rf->rf_group, (const char *)group);
1312 } /* }}} int compare_read_func_group */
1314 int plugin_unregister_read_group (const char *group) /* {{{ */
1315 {
1316 llentry_t *le;
1317 read_func_t *rf;
1319 int found = 0;
1321 if (group == NULL)
1322 return (-ENOENT);
1324 pthread_mutex_lock (&read_lock);
1326 if (read_list == NULL)
1327 {
1328 pthread_mutex_unlock (&read_lock);
1329 return (-ENOENT);
1330 }
1332 while (42)
1333 {
1334 le = llist_search_custom (read_list,
1335 compare_read_func_group, (void *)group);
1337 if (le == NULL)
1338 break;
1340 ++found;
1342 llist_remove (read_list, le);
1344 rf = le->value;
1345 assert (rf != NULL);
1346 rf->rf_type = RF_REMOVE;
1348 llentry_destroy (le);
1350 DEBUG ("plugin_unregister_read_group: "
1351 "Marked `%s' (group `%s') for removal.",
1352 rf->rf_name, group);
1353 }
1355 pthread_mutex_unlock (&read_lock);
1357 if (found == 0)
1358 {
1359 WARNING ("plugin_unregister_read_group: No such "
1360 "group of read function: %s", group);
1361 return (-ENOENT);
1362 }
1364 return (0);
1365 } /* }}} int plugin_unregister_read_group */
1367 int plugin_unregister_write (const char *name)
1368 {
1369 return (plugin_unregister (list_write, name));
1370 }
1372 int plugin_unregister_flush (const char *name)
1373 {
1374 return (plugin_unregister (list_flush, name));
1375 }
1377 int plugin_unregister_missing (const char *name)
1378 {
1379 return (plugin_unregister (list_missing, name));
1380 }
1382 int plugin_unregister_shutdown (const char *name)
1383 {
1384 return (plugin_unregister (list_shutdown, name));
1385 }
1387 int plugin_unregister_data_set (const char *name)
1388 {
1389 data_set_t *ds;
1391 if (data_sets == NULL)
1392 return (-1);
1394 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1395 return (-1);
1397 sfree (ds->ds);
1398 sfree (ds);
1400 return (0);
1401 } /* int plugin_unregister_data_set */
1403 int plugin_unregister_log (const char *name)
1404 {
1405 return (plugin_unregister (list_log, name));
1406 }
1408 int plugin_unregister_notification (const char *name)
1409 {
1410 return (plugin_unregister (list_notification, name));
1411 }
1413 void plugin_init_all (void)
1414 {
1415 const char *chain_name;
1416 llentry_t *le;
1417 int status;
1419 /* Init the value cache */
1420 uc_init ();
1422 chain_name = global_option_get ("PreCacheChain");
1423 pre_cache_chain = fc_chain_get_by_name (chain_name);
1425 chain_name = global_option_get ("PostCacheChain");
1426 post_cache_chain = fc_chain_get_by_name (chain_name);
1428 {
1429 char const *tmp = global_option_get ("WriteThreads");
1430 int num = atoi (tmp);
1432 if (num < 1)
1433 num = 5;
1435 start_write_threads ((size_t) num);
1436 }
1438 if ((list_init == NULL) && (read_heap == NULL))
1439 return;
1441 /* Calling all init callbacks before checking if read callbacks
1442 * are available allows the init callbacks to register the read
1443 * callback. */
1444 le = llist_head (list_init);
1445 while (le != NULL)
1446 {
1447 callback_func_t *cf;
1448 plugin_init_cb callback;
1449 plugin_ctx_t old_ctx;
1451 cf = le->value;
1452 old_ctx = plugin_set_ctx (cf->cf_ctx);
1453 callback = cf->cf_callback;
1454 status = (*callback) ();
1455 plugin_set_ctx (old_ctx);
1457 if (status != 0)
1458 {
1459 ERROR ("Initialization of plugin `%s' "
1460 "failed with status %i. "
1461 "Plugin will be unloaded.",
1462 le->key, status);
1463 /* Plugins that register read callbacks from the init
1464 * callback should take care of appropriate error
1465 * handling themselves. */
1466 /* FIXME: Unload _all_ functions */
1467 plugin_unregister_read (le->key);
1468 }
1470 le = le->next;
1471 }
1473 /* Start read-threads */
1474 if (read_heap != NULL)
1475 {
1476 const char *rt;
1477 int num;
1478 rt = global_option_get ("ReadThreads");
1479 num = atoi (rt);
1480 if (num != -1)
1481 start_read_threads ((num > 0) ? num : 5);
1482 }
1483 } /* void plugin_init_all */
1485 /* TODO: Rename this function. */
1486 void plugin_read_all (void)
1487 {
1488 uc_check_timeout ();
1490 return;
1491 } /* void plugin_read_all */
1493 /* Read function called when the `-T' command line argument is given. */
1494 int plugin_read_all_once (void)
1495 {
1496 int status;
1497 int return_status = 0;
1499 if (read_heap == NULL)
1500 {
1501 NOTICE ("No read-functions are registered.");
1502 return (0);
1503 }
1505 while (42)
1506 {
1507 read_func_t *rf;
1508 plugin_ctx_t old_ctx;
1510 rf = c_heap_get_root (read_heap);
1511 if (rf == NULL)
1512 break;
1514 old_ctx = plugin_set_ctx (rf->rf_ctx);
1516 if (rf->rf_type == RF_SIMPLE)
1517 {
1518 int (*callback) (void);
1520 callback = rf->rf_callback;
1521 status = (*callback) ();
1522 }
1523 else
1524 {
1525 plugin_read_cb callback;
1527 callback = rf->rf_callback;
1528 status = (*callback) (&rf->rf_udata);
1529 }
1531 plugin_set_ctx (old_ctx);
1533 if (status != 0)
1534 {
1535 NOTICE ("read-function of plugin `%s' failed.",
1536 rf->rf_name);
1537 return_status = -1;
1538 }
1540 destroy_callback ((void *) rf);
1541 }
1543 return (return_status);
1544 } /* int plugin_read_all_once */
1546 int plugin_write (const char *plugin, /* {{{ */
1547 const data_set_t *ds, const value_list_t *vl)
1548 {
1549 llentry_t *le;
1550 int status;
1552 if (vl == NULL)
1553 return (EINVAL);
1555 if (list_write == NULL)
1556 return (ENOENT);
1558 if (ds == NULL)
1559 {
1560 ds = plugin_get_ds (vl->type);
1561 if (ds == NULL)
1562 {
1563 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1564 return (ENOENT);
1565 }
1566 }
1568 if (plugin == NULL)
1569 {
1570 int success = 0;
1571 int failure = 0;
1573 le = llist_head (list_write);
1574 while (le != NULL)
1575 {
1576 callback_func_t *cf = le->value;
1577 plugin_write_cb callback;
1579 /* do not switch plugin context; rather keep the context (interval)
1580 * information of the calling read plugin */
1582 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1583 callback = cf->cf_callback;
1584 status = (*callback) (ds, vl, &cf->cf_udata);
1585 if (status != 0)
1586 failure++;
1587 else
1588 success++;
1590 le = le->next;
1591 }
1593 if ((success == 0) && (failure != 0))
1594 status = -1;
1595 else
1596 status = 0;
1597 }
1598 else /* plugin != NULL */
1599 {
1600 callback_func_t *cf;
1601 plugin_write_cb callback;
1603 le = llist_head (list_write);
1604 while (le != NULL)
1605 {
1606 if (strcasecmp (plugin, le->key) == 0)
1607 break;
1609 le = le->next;
1610 }
1612 if (le == NULL)
1613 return (ENOENT);
1615 cf = le->value;
1617 /* do not switch plugin context; rather keep the context (interval)
1618 * information of the calling read plugin */
1620 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1621 callback = cf->cf_callback;
1622 status = (*callback) (ds, vl, &cf->cf_udata);
1623 }
1625 return (status);
1626 } /* }}} int plugin_write */
1628 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1629 {
1630 llentry_t *le;
1632 if (list_flush == NULL)
1633 return (0);
1635 le = llist_head (list_flush);
1636 while (le != NULL)
1637 {
1638 callback_func_t *cf;
1639 plugin_flush_cb callback;
1640 plugin_ctx_t old_ctx;
1642 if ((plugin != NULL)
1643 && (strcmp (plugin, le->key) != 0))
1644 {
1645 le = le->next;
1646 continue;
1647 }
1649 cf = le->value;
1650 old_ctx = plugin_set_ctx (cf->cf_ctx);
1651 callback = cf->cf_callback;
1653 (*callback) (timeout, identifier, &cf->cf_udata);
1655 plugin_set_ctx (old_ctx);
1657 le = le->next;
1658 }
1659 return (0);
1660 } /* int plugin_flush */
1662 void plugin_shutdown_all (void)
1663 {
1664 llentry_t *le;
1666 stop_read_threads ();
1668 destroy_all_callbacks (&list_init);
1670 pthread_mutex_lock (&read_lock);
1671 llist_destroy (read_list);
1672 read_list = NULL;
1673 pthread_mutex_unlock (&read_lock);
1675 destroy_read_heap ();
1677 plugin_flush (/* plugin = */ NULL,
1678 /* timeout = */ 0,
1679 /* identifier = */ NULL);
1681 le = NULL;
1682 if (list_shutdown != NULL)
1683 le = llist_head (list_shutdown);
1685 while (le != NULL)
1686 {
1687 callback_func_t *cf;
1688 plugin_shutdown_cb callback;
1689 plugin_ctx_t old_ctx;
1691 cf = le->value;
1692 old_ctx = plugin_set_ctx (cf->cf_ctx);
1693 callback = cf->cf_callback;
1695 /* Advance the pointer before calling the callback allows
1696 * shutdown functions to unregister themselves. If done the
1697 * other way around the memory `le' points to will be freed
1698 * after callback returns. */
1699 le = le->next;
1701 (*callback) ();
1703 plugin_set_ctx (old_ctx);
1704 }
1706 stop_write_threads ();
1708 /* Write plugins which use the `user_data' pointer usually need the
1709 * same data available to the flush callback. If this is the case, set
1710 * the free_function to NULL when registering the flush callback and to
1711 * the real free function when registering the write callback. This way
1712 * the data isn't freed twice. */
1713 destroy_all_callbacks (&list_flush);
1714 destroy_all_callbacks (&list_missing);
1715 destroy_all_callbacks (&list_write);
1717 destroy_all_callbacks (&list_notification);
1718 destroy_all_callbacks (&list_shutdown);
1719 destroy_all_callbacks (&list_log);
1720 } /* void plugin_shutdown_all */
1722 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1723 {
1724 llentry_t *le;
1726 if (list_missing == NULL)
1727 return (0);
1729 le = llist_head (list_missing);
1730 while (le != NULL)
1731 {
1732 callback_func_t *cf;
1733 plugin_missing_cb callback;
1734 plugin_ctx_t old_ctx;
1735 int status;
1737 cf = le->value;
1738 old_ctx = plugin_set_ctx (cf->cf_ctx);
1739 callback = cf->cf_callback;
1741 status = (*callback) (vl, &cf->cf_udata);
1742 plugin_set_ctx (old_ctx);
1743 if (status != 0)
1744 {
1745 if (status < 0)
1746 {
1747 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1748 "failed with status %i.",
1749 le->key, status);
1750 return (status);
1751 }
1752 else
1753 {
1754 return (0);
1755 }
1756 }
1758 le = le->next;
1759 }
1760 return (0);
1761 } /* int }}} plugin_dispatch_missing */
1763 static int plugin_dispatch_values_internal (value_list_t *vl)
1764 {
1765 int status;
1766 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1768 value_t *saved_values;
1769 int saved_values_len;
1771 data_set_t *ds;
1773 int free_meta_data = 0;
1775 if ((vl == NULL) || (vl->type[0] == 0)
1776 || (vl->values == NULL) || (vl->values_len < 1))
1777 {
1778 ERROR ("plugin_dispatch_values: Invalid value list "
1779 "from plugin %s.", vl->plugin);
1780 return (-1);
1781 }
1783 /* Free meta data only if the calling function didn't specify any. In
1784 * this case matches and targets may add some and the calling function
1785 * may not expect (and therefore free) that data. */
1786 if (vl->meta == NULL)
1787 free_meta_data = 1;
1789 if (list_write == NULL)
1790 c_complain_once (LOG_WARNING, &no_write_complaint,
1791 "plugin_dispatch_values: No write callback has been "
1792 "registered. Please load at least one output plugin, "
1793 "if you want the collected data to be stored.");
1795 if (data_sets == NULL)
1796 {
1797 ERROR ("plugin_dispatch_values: No data sets registered. "
1798 "Could the types database be read? Check "
1799 "your `TypesDB' setting!");
1800 return (-1);
1801 }
1803 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1804 {
1805 char ident[6 * DATA_MAX_NAME_LEN];
1807 FORMAT_VL (ident, sizeof (ident), vl);
1808 INFO ("plugin_dispatch_values: Dataset not found: %s "
1809 "(from \"%s\"), check your types.db!",
1810 vl->type, ident);
1811 return (-1);
1812 }
1814 /* Assured by plugin_value_list_clone(). The time is determined at
1815 * _enqueue_ time. */
1816 assert (vl->time != 0);
1817 assert (vl->interval != 0);
1819 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1820 "host = %s; "
1821 "plugin = %s; plugin_instance = %s; "
1822 "type = %s; type_instance = %s;",
1823 CDTIME_T_TO_DOUBLE (vl->time),
1824 CDTIME_T_TO_DOUBLE (vl->interval),
1825 vl->host,
1826 vl->plugin, vl->plugin_instance,
1827 vl->type, vl->type_instance);
1829 #if COLLECT_DEBUG
1830 assert (0 == strcmp (ds->type, vl->type));
1831 #else
1832 if (0 != strcmp (ds->type, vl->type))
1833 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1834 ds->type, vl->type);
1835 #endif
1837 #if COLLECT_DEBUG
1838 assert (ds->ds_num == vl->values_len);
1839 #else
1840 if (ds->ds_num != vl->values_len)
1841 {
1842 ERROR ("plugin_dispatch_values: ds->type = %s: "
1843 "(ds->ds_num = %i) != "
1844 "(vl->values_len = %i)",
1845 ds->type, ds->ds_num, vl->values_len);
1846 return (-1);
1847 }
1848 #endif
1850 escape_slashes (vl->host, sizeof (vl->host));
1851 escape_slashes (vl->plugin, sizeof (vl->plugin));
1852 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1853 escape_slashes (vl->type, sizeof (vl->type));
1854 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1856 /* Copy the values. This way, we can assure `targets' that they get
1857 * dynamically allocated values, which they can free and replace if
1858 * they like. */
1859 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1860 {
1861 saved_values = vl->values;
1862 saved_values_len = vl->values_len;
1864 vl->values = (value_t *) calloc (vl->values_len,
1865 sizeof (*vl->values));
1866 if (vl->values == NULL)
1867 {
1868 ERROR ("plugin_dispatch_values: calloc failed.");
1869 vl->values = saved_values;
1870 return (-1);
1871 }
1872 memcpy (vl->values, saved_values,
1873 vl->values_len * sizeof (*vl->values));
1874 }
1875 else /* if ((pre == NULL) && (post == NULL)) */
1876 {
1877 saved_values = NULL;
1878 saved_values_len = 0;
1879 }
1881 if (pre_cache_chain != NULL)
1882 {
1883 status = fc_process_chain (ds, vl, pre_cache_chain);
1884 if (status < 0)
1885 {
1886 WARNING ("plugin_dispatch_values: Running the "
1887 "pre-cache chain failed with "
1888 "status %i (%#x).",
1889 status, status);
1890 }
1891 else if (status == FC_TARGET_STOP)
1892 {
1893 /* Restore the state of the value_list so that plugins
1894 * don't get confused.. */
1895 if (saved_values != NULL)
1896 {
1897 free (vl->values);
1898 vl->values = saved_values;
1899 vl->values_len = saved_values_len;
1900 }
1901 return (0);
1902 }
1903 }
1905 /* Update the value cache */
1906 uc_update (ds, vl);
1908 if (post_cache_chain != NULL)
1909 {
1910 status = fc_process_chain (ds, vl, post_cache_chain);
1911 if (status < 0)
1912 {
1913 WARNING ("plugin_dispatch_values: Running the "
1914 "post-cache chain failed with "
1915 "status %i (%#x).",
1916 status, status);
1917 }
1918 }
1919 else
1920 fc_default_action (ds, vl);
1922 /* Restore the state of the value_list so that plugins don't get
1923 * confused.. */
1924 if (saved_values != NULL)
1925 {
1926 free (vl->values);
1927 vl->values = saved_values;
1928 vl->values_len = saved_values_len;
1929 }
1931 if ((free_meta_data != 0) && (vl->meta != NULL))
1932 {
1933 meta_data_destroy (vl->meta);
1934 vl->meta = NULL;
1935 }
1937 return (0);
1938 } /* int plugin_dispatch_values_internal */
1940 int plugin_dispatch_values (value_list_t const *vl)
1941 {
1942 int status;
1944 status = plugin_write_enqueue (vl);
1945 if (status != 0)
1946 {
1947 char errbuf[1024];
1948 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
1949 "with status %i (%s).", status,
1950 sstrerror (status, errbuf, sizeof (errbuf)));
1951 return (status);
1952 }
1954 return (0);
1955 }
1957 int plugin_dispatch_notification (const notification_t *notif)
1958 {
1959 llentry_t *le;
1960 /* Possible TODO: Add flap detection here */
1962 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1963 "time = %.3f; host = %s;",
1964 notif->severity, notif->message,
1965 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1967 /* Nobody cares for notifications */
1968 if (list_notification == NULL)
1969 return (-1);
1971 le = llist_head (list_notification);
1972 while (le != NULL)
1973 {
1974 callback_func_t *cf;
1975 plugin_notification_cb callback;
1976 int status;
1978 /* do not switch plugin context; rather keep the context
1979 * (interval) information of the calling plugin */
1981 cf = le->value;
1982 callback = cf->cf_callback;
1983 status = (*callback) (notif, &cf->cf_udata);
1984 if (status != 0)
1985 {
1986 WARNING ("plugin_dispatch_notification: Notification "
1987 "callback %s returned %i.",
1988 le->key, status);
1989 }
1991 le = le->next;
1992 }
1994 return (0);
1995 } /* int plugin_dispatch_notification */
1997 void plugin_log (int level, const char *format, ...)
1998 {
1999 char msg[1024];
2000 va_list ap;
2001 llentry_t *le;
2003 #if !COLLECT_DEBUG
2004 if (level >= LOG_DEBUG)
2005 return;
2006 #endif
2008 va_start (ap, format);
2009 vsnprintf (msg, sizeof (msg), format, ap);
2010 msg[sizeof (msg) - 1] = '\0';
2011 va_end (ap);
2013 if (list_log == NULL)
2014 {
2015 fprintf (stderr, "%s\n", msg);
2016 return;
2017 }
2019 le = llist_head (list_log);
2020 while (le != NULL)
2021 {
2022 callback_func_t *cf;
2023 plugin_log_cb callback;
2025 cf = le->value;
2026 callback = cf->cf_callback;
2028 /* do not switch plugin context; rather keep the context
2029 * (interval) information of the calling plugin */
2031 (*callback) (level, msg, &cf->cf_udata);
2033 le = le->next;
2034 }
2035 } /* void plugin_log */
2037 int parse_log_severity (const char *severity)
2038 {
2039 int log_level = -1;
2041 if ((0 == strcasecmp (severity, "emerg"))
2042 || (0 == strcasecmp (severity, "alert"))
2043 || (0 == strcasecmp (severity, "crit"))
2044 || (0 == strcasecmp (severity, "err")))
2045 log_level = LOG_ERR;
2046 else if (0 == strcasecmp (severity, "warning"))
2047 log_level = LOG_WARNING;
2048 else if (0 == strcasecmp (severity, "notice"))
2049 log_level = LOG_NOTICE;
2050 else if (0 == strcasecmp (severity, "info"))
2051 log_level = LOG_INFO;
2052 #if COLLECT_DEBUG
2053 else if (0 == strcasecmp (severity, "debug"))
2054 log_level = LOG_DEBUG;
2055 #endif /* COLLECT_DEBUG */
2057 return (log_level);
2058 } /* int parse_log_severity */
2060 int parse_notif_severity (const char *severity)
2061 {
2062 int notif_severity = -1;
2064 if (strcasecmp (severity, "FAILURE") == 0)
2065 notif_severity = NOTIF_FAILURE;
2066 else if (strcmp (severity, "OKAY") == 0)
2067 notif_severity = NOTIF_OKAY;
2068 else if ((strcmp (severity, "WARNING") == 0)
2069 || (strcmp (severity, "WARN") == 0))
2070 notif_severity = NOTIF_WARNING;
2072 return (notif_severity);
2073 } /* int parse_notif_severity */
2075 const data_set_t *plugin_get_ds (const char *name)
2076 {
2077 data_set_t *ds;
2079 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2080 {
2081 DEBUG ("No such dataset registered: %s", name);
2082 return (NULL);
2083 }
2085 return (ds);
2086 } /* data_set_t *plugin_get_ds */
2088 static int plugin_notification_meta_add (notification_t *n,
2089 const char *name,
2090 enum notification_meta_type_e type,
2091 const void *value)
2092 {
2093 notification_meta_t *meta;
2094 notification_meta_t *tail;
2096 if ((n == NULL) || (name == NULL) || (value == NULL))
2097 {
2098 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2099 return (-1);
2100 }
2102 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2103 if (meta == NULL)
2104 {
2105 ERROR ("plugin_notification_meta_add: malloc failed.");
2106 return (-1);
2107 }
2108 memset (meta, 0, sizeof (notification_meta_t));
2110 sstrncpy (meta->name, name, sizeof (meta->name));
2111 meta->type = type;
2113 switch (type)
2114 {
2115 case NM_TYPE_STRING:
2116 {
2117 meta->nm_value.nm_string = strdup ((const char *) value);
2118 if (meta->nm_value.nm_string == NULL)
2119 {
2120 ERROR ("plugin_notification_meta_add: strdup failed.");
2121 sfree (meta);
2122 return (-1);
2123 }
2124 break;
2125 }
2126 case NM_TYPE_SIGNED_INT:
2127 {
2128 meta->nm_value.nm_signed_int = *((int64_t *) value);
2129 break;
2130 }
2131 case NM_TYPE_UNSIGNED_INT:
2132 {
2133 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2134 break;
2135 }
2136 case NM_TYPE_DOUBLE:
2137 {
2138 meta->nm_value.nm_double = *((double *) value);
2139 break;
2140 }
2141 case NM_TYPE_BOOLEAN:
2142 {
2143 meta->nm_value.nm_boolean = *((_Bool *) value);
2144 break;
2145 }
2146 default:
2147 {
2148 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2149 sfree (meta);
2150 return (-1);
2151 }
2152 } /* switch (type) */
2154 meta->next = NULL;
2155 tail = n->meta;
2156 while ((tail != NULL) && (tail->next != NULL))
2157 tail = tail->next;
2159 if (tail == NULL)
2160 n->meta = meta;
2161 else
2162 tail->next = meta;
2164 return (0);
2165 } /* int plugin_notification_meta_add */
2167 int plugin_notification_meta_add_string (notification_t *n,
2168 const char *name,
2169 const char *value)
2170 {
2171 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2172 }
2174 int plugin_notification_meta_add_signed_int (notification_t *n,
2175 const char *name,
2176 int64_t value)
2177 {
2178 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2179 }
2181 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2182 const char *name,
2183 uint64_t value)
2184 {
2185 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2186 }
2188 int plugin_notification_meta_add_double (notification_t *n,
2189 const char *name,
2190 double value)
2191 {
2192 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2193 }
2195 int plugin_notification_meta_add_boolean (notification_t *n,
2196 const char *name,
2197 _Bool value)
2198 {
2199 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2200 }
2202 int plugin_notification_meta_copy (notification_t *dst,
2203 const notification_t *src)
2204 {
2205 notification_meta_t *meta;
2207 assert (dst != NULL);
2208 assert (src != NULL);
2209 assert (dst != src);
2210 assert ((src->meta == NULL) || (src->meta != dst->meta));
2212 for (meta = src->meta; meta != NULL; meta = meta->next)
2213 {
2214 if (meta->type == NM_TYPE_STRING)
2215 plugin_notification_meta_add_string (dst, meta->name,
2216 meta->nm_value.nm_string);
2217 else if (meta->type == NM_TYPE_SIGNED_INT)
2218 plugin_notification_meta_add_signed_int (dst, meta->name,
2219 meta->nm_value.nm_signed_int);
2220 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2221 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2222 meta->nm_value.nm_unsigned_int);
2223 else if (meta->type == NM_TYPE_DOUBLE)
2224 plugin_notification_meta_add_double (dst, meta->name,
2225 meta->nm_value.nm_double);
2226 else if (meta->type == NM_TYPE_BOOLEAN)
2227 plugin_notification_meta_add_boolean (dst, meta->name,
2228 meta->nm_value.nm_boolean);
2229 }
2231 return (0);
2232 } /* int plugin_notification_meta_copy */
2234 int plugin_notification_meta_free (notification_meta_t *n)
2235 {
2236 notification_meta_t *this;
2237 notification_meta_t *next;
2239 if (n == NULL)
2240 {
2241 ERROR ("plugin_notification_meta_free: n == NULL!");
2242 return (-1);
2243 }
2245 this = n;
2246 while (this != NULL)
2247 {
2248 next = this->next;
2250 if (this->type == NM_TYPE_STRING)
2251 {
2252 free ((char *)this->nm_value.nm_string);
2253 this->nm_value.nm_string = NULL;
2254 }
2255 sfree (this);
2257 this = next;
2258 }
2260 return (0);
2261 } /* int plugin_notification_meta_free */
2263 static void plugin_ctx_destructor (void *ctx)
2264 {
2265 sfree (ctx);
2266 } /* void plugin_ctx_destructor */
2268 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2270 static plugin_ctx_t *plugin_ctx_create (void)
2271 {
2272 plugin_ctx_t *ctx;
2274 ctx = malloc (sizeof (*ctx));
2275 if (ctx == NULL) {
2276 char errbuf[1024];
2277 ERROR ("Failed to allocate plugin context: %s",
2278 sstrerror (errno, errbuf, sizeof (errbuf)));
2279 return NULL;
2280 }
2282 *ctx = ctx_init;
2283 assert (plugin_ctx_key_initialized);
2284 pthread_setspecific (plugin_ctx_key, ctx);
2285 DEBUG("Created new plugin context.");
2286 return (ctx);
2287 } /* int plugin_ctx_create */
2289 void plugin_init_ctx (void)
2290 {
2291 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2292 plugin_ctx_key_initialized = 1;
2293 } /* void plugin_init_ctx */
2295 plugin_ctx_t plugin_get_ctx (void)
2296 {
2297 plugin_ctx_t *ctx;
2299 assert (plugin_ctx_key_initialized);
2300 ctx = pthread_getspecific (plugin_ctx_key);
2302 if (ctx == NULL) {
2303 ctx = plugin_ctx_create ();
2304 /* this must no happen -- exit() instead? */
2305 if (ctx == NULL)
2306 return ctx_init;
2307 }
2309 return (*ctx);
2310 } /* plugin_ctx_t plugin_get_ctx */
2312 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2313 {
2314 plugin_ctx_t *c;
2315 plugin_ctx_t old;
2317 assert (plugin_ctx_key_initialized);
2318 c = pthread_getspecific (plugin_ctx_key);
2320 if (c == NULL) {
2321 c = plugin_ctx_create ();
2322 /* this must no happen -- exit() instead? */
2323 if (c == NULL)
2324 return ctx_init;
2325 }
2327 old = *c;
2328 *c = ctx;
2330 return (old);
2331 } /* void plugin_set_ctx */
2333 cdtime_t plugin_get_interval (void)
2334 {
2335 cdtime_t interval;
2337 interval = plugin_get_ctx().interval;
2338 if (interval > 0)
2339 return interval;
2341 return cf_get_default_interval ();
2342 } /* cdtime_t plugin_get_interval */
2344 typedef struct {
2345 plugin_ctx_t ctx;
2346 void *(*start_routine) (void *);
2347 void *arg;
2348 } plugin_thread_t;
2350 static void *plugin_thread_start (void *arg)
2351 {
2352 plugin_thread_t *plugin_thread = arg;
2354 void *(*start_routine) (void *) = plugin_thread->start_routine;
2355 void *plugin_arg = plugin_thread->arg;
2357 plugin_set_ctx (plugin_thread->ctx);
2359 free (plugin_thread);
2361 return start_routine (plugin_arg);
2362 } /* void *plugin_thread_start */
2364 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2365 void *(*start_routine) (void *), void *arg)
2366 {
2367 plugin_thread_t *plugin_thread;
2369 plugin_thread = malloc (sizeof (*plugin_thread));
2370 if (plugin_thread == NULL)
2371 return -1;
2373 plugin_thread->ctx = plugin_get_ctx ();
2374 plugin_thread->start_routine = start_routine;
2375 plugin_thread->arg = arg;
2377 return pthread_create (thread, attr,
2378 plugin_thread_start, plugin_thread);
2379 } /* int plugin_thread_create */
2381 /* vim: set sw=8 ts=8 noet fdm=marker : */