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 write_queue_t *next;
78 };
80 /*
81 * Private variables
82 */
83 static llist_t *list_init;
84 static llist_t *list_write;
85 static llist_t *list_flush;
86 static llist_t *list_missing;
87 static llist_t *list_shutdown;
88 static llist_t *list_log;
89 static llist_t *list_notification;
91 static fc_chain_t *pre_cache_chain = NULL;
92 static fc_chain_t *post_cache_chain = NULL;
94 static c_avl_tree_t *data_sets;
96 static char *plugindir = NULL;
98 static c_heap_t *read_heap = NULL;
99 static llist_t *read_list;
100 static int read_loop = 1;
101 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
102 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
103 static pthread_t *read_threads = NULL;
104 static int read_threads_num = 0;
106 static write_queue_t *write_queue_head;
107 static write_queue_t *write_queue_tail;
108 static _Bool write_loop = 1;
109 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
110 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
111 static pthread_t *write_threads = NULL;
112 static size_t write_threads_num = 0;
114 static pthread_key_t plugin_ctx_key;
115 static _Bool plugin_ctx_key_initialized = 0;
117 /*
118 * Static functions
119 */
120 static int plugin_dispatch_values_internal (value_list_t *vl);
122 static const char *plugin_get_dir (void)
123 {
124 if (plugindir == NULL)
125 return (PLUGINDIR);
126 else
127 return (plugindir);
128 }
130 static void destroy_callback (callback_func_t *cf) /* {{{ */
131 {
132 if (cf == NULL)
133 return;
135 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
136 {
137 cf->cf_udata.free_func (cf->cf_udata.data);
138 cf->cf_udata.data = NULL;
139 cf->cf_udata.free_func = NULL;
140 }
141 sfree (cf);
142 } /* }}} void destroy_callback */
144 static void destroy_all_callbacks (llist_t **list) /* {{{ */
145 {
146 llentry_t *le;
148 if (*list == NULL)
149 return;
151 le = llist_head (*list);
152 while (le != NULL)
153 {
154 llentry_t *le_next;
156 le_next = le->next;
158 sfree (le->key);
159 destroy_callback (le->value);
160 le->value = NULL;
162 le = le_next;
163 }
165 llist_destroy (*list);
166 *list = NULL;
167 } /* }}} void destroy_all_callbacks */
169 static void destroy_read_heap (void) /* {{{ */
170 {
171 if (read_heap == NULL)
172 return;
174 while (42)
175 {
176 callback_func_t *cf;
178 cf = c_heap_get_root (read_heap);
179 if (cf == NULL)
180 break;
182 destroy_callback (cf);
183 }
185 c_heap_destroy (read_heap);
186 read_heap = NULL;
187 } /* }}} void destroy_read_heap */
189 static int register_callback (llist_t **list, /* {{{ */
190 const char *name, callback_func_t *cf)
191 {
192 llentry_t *le;
193 char *key;
195 if (*list == NULL)
196 {
197 *list = llist_create ();
198 if (*list == NULL)
199 {
200 ERROR ("plugin: register_callback: "
201 "llist_create failed.");
202 destroy_callback (cf);
203 return (-1);
204 }
205 }
207 key = strdup (name);
208 if (key == NULL)
209 {
210 ERROR ("plugin: register_callback: strdup failed.");
211 destroy_callback (cf);
212 return (-1);
213 }
215 le = llist_search (*list, name);
216 if (le == NULL)
217 {
218 le = llentry_create (key, cf);
219 if (le == NULL)
220 {
221 ERROR ("plugin: register_callback: "
222 "llentry_create failed.");
223 free (key);
224 destroy_callback (cf);
225 return (-1);
226 }
228 llist_append (*list, le);
229 }
230 else
231 {
232 callback_func_t *old_cf;
234 old_cf = le->value;
235 le->value = cf;
237 WARNING ("plugin: register_callback: "
238 "a callback named `%s' already exists - "
239 "overwriting the old entry!", name);
241 destroy_callback (old_cf);
242 sfree (key);
243 }
245 return (0);
246 } /* }}} int register_callback */
248 static int create_register_callback (llist_t **list, /* {{{ */
249 const char *name, void *callback, user_data_t *ud)
250 {
251 callback_func_t *cf;
253 cf = (callback_func_t *) malloc (sizeof (*cf));
254 if (cf == NULL)
255 {
256 ERROR ("plugin: create_register_callback: malloc failed.");
257 return (-1);
258 }
259 memset (cf, 0, sizeof (*cf));
261 cf->cf_callback = callback;
262 if (ud == NULL)
263 {
264 cf->cf_udata.data = NULL;
265 cf->cf_udata.free_func = NULL;
266 }
267 else
268 {
269 cf->cf_udata = *ud;
270 }
272 cf->cf_ctx = plugin_get_ctx ();
274 return (register_callback (list, name, cf));
275 } /* }}} int create_register_callback */
277 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
278 {
279 llentry_t *e;
281 if (list == NULL)
282 return (-1);
284 e = llist_search (list, name);
285 if (e == NULL)
286 return (-1);
288 llist_remove (list, e);
290 sfree (e->key);
291 destroy_callback (e->value);
293 llentry_destroy (e);
295 return (0);
296 } /* }}} int plugin_unregister */
298 /*
299 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
300 * object, but it will bitch about a shared object not having a
301 * ``module_register'' symbol..
302 */
303 static int plugin_load_file (char *file, uint32_t flags)
304 {
305 lt_dlhandle dlh;
306 void (*reg_handle) (void);
308 lt_dlinit ();
309 lt_dlerror (); /* clear errors */
311 #if LIBTOOL_VERSION == 2
312 if (flags & PLUGIN_FLAGS_GLOBAL) {
313 lt_dladvise advise;
314 lt_dladvise_init(&advise);
315 lt_dladvise_global(&advise);
316 dlh = lt_dlopenadvise(file, advise);
317 lt_dladvise_destroy(&advise);
318 } else {
319 dlh = lt_dlopen (file);
320 }
321 #else /* if LIBTOOL_VERSION == 1 */
322 if (flags & PLUGIN_FLAGS_GLOBAL)
323 WARNING ("plugin_load_file: The global flag is not supported, "
324 "libtool 2 is required for this.");
325 dlh = lt_dlopen (file);
326 #endif
328 if (dlh == NULL)
329 {
330 char errbuf[1024] = "";
332 ssnprintf (errbuf, sizeof (errbuf),
333 "lt_dlopen (\"%s\") failed: %s. "
334 "The most common cause for this problem are "
335 "missing dependencies. Use ldd(1) to check "
336 "the dependencies of the plugin "
337 "/ shared object.",
338 file, lt_dlerror ());
340 ERROR ("%s", errbuf);
341 /* Make sure this is printed to STDERR in any case, but also
342 * make sure it's printed only once. */
343 if (list_log != NULL)
344 fprintf (stderr, "ERROR: %s\n", errbuf);
346 return (1);
347 }
349 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
350 {
351 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
352 file, lt_dlerror ());
353 lt_dlclose (dlh);
354 return (-1);
355 }
357 (*reg_handle) ();
359 return (0);
360 }
362 static _Bool timeout_reached(struct timespec timeout)
363 {
364 struct timeval now;
365 gettimeofday(&now, NULL);
366 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
367 }
369 static void *plugin_read_thread (void __attribute__((unused)) *args)
370 {
371 while (read_loop != 0)
372 {
373 read_func_t *rf;
374 plugin_ctx_t old_ctx;
375 cdtime_t now;
376 int status;
377 int rf_type;
378 int rc;
380 /* Get the read function that needs to be read next.
381 * We don't need to hold "read_lock" for the heap, but we need
382 * to call c_heap_get_root() and pthread_cond_wait() in the
383 * same protected block. */
384 pthread_mutex_lock (&read_lock);
385 rf = c_heap_get_root (read_heap);
386 if (rf == NULL)
387 {
388 pthread_cond_wait (&read_cond, &read_lock);
389 pthread_mutex_unlock (&read_lock);
390 continue;
391 }
392 pthread_mutex_unlock (&read_lock);
394 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
395 {
396 /* this should not happen, because the interval is set
397 * for each plugin when loading it
398 * XXX: issue a warning? */
399 now = cdtime ();
401 CDTIME_T_TO_TIMESPEC (plugin_get_interval (), &rf->rf_interval);
403 rf->rf_effective_interval = rf->rf_interval;
405 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
406 }
408 /* sleep until this entry is due,
409 * using pthread_cond_timedwait */
410 pthread_mutex_lock (&read_lock);
411 /* In pthread_cond_timedwait, spurious wakeups are possible
412 * (and really happen, at least on NetBSD with > 1 CPU), thus
413 * we need to re-evaluate the condition every time
414 * pthread_cond_timedwait returns. */
415 rc = 0;
416 while ((read_loop != 0)
417 && !timeout_reached(rf->rf_next_read)
418 && rc == 0)
419 {
420 rc = pthread_cond_timedwait (&read_cond, &read_lock,
421 &rf->rf_next_read);
422 }
424 /* Must hold `read_lock' when accessing `rf->rf_type'. */
425 rf_type = rf->rf_type;
426 pthread_mutex_unlock (&read_lock);
428 /* Check if we're supposed to stop.. This may have interrupted
429 * the sleep, too. */
430 if (read_loop == 0)
431 {
432 /* Insert `rf' again, so it can be free'd correctly */
433 c_heap_insert (read_heap, rf);
434 break;
435 }
437 /* The entry has been marked for deletion. The linked list
438 * entry has already been removed by `plugin_unregister_read'.
439 * All we have to do here is free the `read_func_t' and
440 * continue. */
441 if (rf_type == RF_REMOVE)
442 {
443 DEBUG ("plugin_read_thread: Destroying the `%s' "
444 "callback.", rf->rf_name);
445 destroy_callback ((callback_func_t *) rf);
446 rf = NULL;
447 continue;
448 }
450 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
452 old_ctx = plugin_set_ctx (rf->rf_ctx);
454 if (rf_type == RF_SIMPLE)
455 {
456 int (*callback) (void);
458 callback = rf->rf_callback;
459 status = (*callback) ();
460 }
461 else
462 {
463 plugin_read_cb callback;
465 assert (rf_type == RF_COMPLEX);
467 callback = rf->rf_callback;
468 status = (*callback) (&rf->rf_udata);
469 }
471 plugin_set_ctx (old_ctx);
473 /* If the function signals failure, we will increase the
474 * intervals in which it will be called. */
475 if (status != 0)
476 {
477 rf->rf_effective_interval.tv_sec *= 2;
478 rf->rf_effective_interval.tv_nsec *= 2;
479 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
481 if (rf->rf_effective_interval.tv_sec >= 86400)
482 {
483 rf->rf_effective_interval.tv_sec = 86400;
484 rf->rf_effective_interval.tv_nsec = 0;
485 }
487 NOTICE ("read-function of plugin `%s' failed. "
488 "Will suspend it for %i seconds.",
489 rf->rf_name,
490 (int) rf->rf_effective_interval.tv_sec);
491 }
492 else
493 {
494 /* Success: Restore the interval, if it was changed. */
495 rf->rf_effective_interval = rf->rf_interval;
496 }
498 /* update the ``next read due'' field */
499 now = cdtime ();
501 DEBUG ("plugin_read_thread: Effective interval of the "
502 "%s plugin is %i.%09i.",
503 rf->rf_name,
504 (int) rf->rf_effective_interval.tv_sec,
505 (int) rf->rf_effective_interval.tv_nsec);
507 /* Calculate the next (absolute) time at which this function
508 * should be called. */
509 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
510 + rf->rf_effective_interval.tv_sec;
511 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
512 + rf->rf_effective_interval.tv_nsec;
513 NORMALIZE_TIMESPEC (rf->rf_next_read);
515 /* Check, if `rf_next_read' is in the past. */
516 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
517 {
518 /* `rf_next_read' is in the past. Insert `now'
519 * so this value doesn't trail off into the
520 * past too much. */
521 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
522 }
524 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
525 rf->rf_name,
526 (int) rf->rf_next_read.tv_sec,
527 (int) rf->rf_next_read.tv_nsec);
529 /* Re-insert this read function into the heap again. */
530 c_heap_insert (read_heap, rf);
531 } /* while (read_loop) */
533 pthread_exit (NULL);
534 return ((void *) 0);
535 } /* void *plugin_read_thread */
537 static void start_read_threads (int num)
538 {
539 int i;
541 if (read_threads != NULL)
542 return;
544 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
545 if (read_threads == NULL)
546 {
547 ERROR ("plugin: start_read_threads: calloc failed.");
548 return;
549 }
551 read_threads_num = 0;
552 for (i = 0; i < num; i++)
553 {
554 if (pthread_create (read_threads + read_threads_num, NULL,
555 plugin_read_thread, NULL) == 0)
556 {
557 read_threads_num++;
558 }
559 else
560 {
561 ERROR ("plugin: start_read_threads: pthread_create failed.");
562 return;
563 }
564 } /* for (i) */
565 } /* void start_read_threads */
567 static void stop_read_threads (void)
568 {
569 int i;
571 if (read_threads == NULL)
572 return;
574 INFO ("collectd: Stopping %i read threads.", read_threads_num);
576 pthread_mutex_lock (&read_lock);
577 read_loop = 0;
578 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
579 pthread_cond_broadcast (&read_cond);
580 pthread_mutex_unlock (&read_lock);
582 for (i = 0; i < read_threads_num; i++)
583 {
584 if (pthread_join (read_threads[i], NULL) != 0)
585 {
586 ERROR ("plugin: stop_read_threads: pthread_join failed.");
587 }
588 read_threads[i] = (pthread_t) 0;
589 }
590 sfree (read_threads);
591 read_threads_num = 0;
592 } /* void stop_read_threads */
594 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
595 {
596 if (vl == NULL)
597 return;
599 meta_data_destroy (vl->meta);
600 sfree (vl->values);
601 sfree (vl);
602 } /* }}} void plugin_value_list_free */
604 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
605 {
606 value_list_t *vl;
608 if (vl_orig == NULL)
609 return (NULL);
611 vl = malloc (sizeof (*vl));
612 if (vl == NULL)
613 return (NULL);
614 memcpy (vl, vl_orig, sizeof (*vl));
616 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
617 if (vl->values == NULL)
618 {
619 plugin_value_list_free (vl);
620 return (NULL);
621 }
622 memcpy (vl->values, vl_orig->values,
623 vl_orig->values_len * sizeof (*vl->values));
625 vl->meta = meta_data_clone (vl->meta);
626 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
627 {
628 plugin_value_list_free (vl);
629 return (NULL);
630 }
632 return (vl);
633 } /* }}} value_list_t *plugin_value_list_clone */
635 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
636 {
637 write_queue_t *q;
639 q = malloc (sizeof (*q));
640 if (q == NULL)
641 return (ENOMEM);
642 q->next = NULL;
644 q->vl = plugin_value_list_clone (vl);
645 if (q->vl == NULL)
646 {
647 sfree (q);
648 return (ENOMEM);
649 }
651 pthread_mutex_lock (&write_lock);
653 if (write_queue_tail == NULL)
654 {
655 write_queue_head = q;
656 write_queue_tail = q;
657 }
658 else
659 {
660 write_queue_tail->next = q;
661 write_queue_tail = q;
662 }
664 pthread_cond_signal (&write_cond);
665 pthread_mutex_unlock (&write_lock);
667 return (0);
668 } /* }}} int plugin_write_enqueue */
670 static value_list_t *plugin_write_dequeue (void) /* {{{ */
671 {
672 write_queue_t *q;
673 value_list_t *vl;
675 pthread_mutex_lock (&write_lock);
677 while (write_loop && (write_queue_head == NULL))
678 pthread_cond_wait (&write_cond, &write_lock);
680 if (write_queue_head == NULL)
681 {
682 pthread_mutex_unlock (&write_lock);
683 return (NULL);
684 }
686 q = write_queue_head;
687 write_queue_head = q->next;
688 if (write_queue_head == NULL)
689 write_queue_tail = NULL;
691 pthread_mutex_unlock (&write_lock);
693 vl = q->vl;
694 sfree (q);
695 return (vl);
696 } /* }}} value_list_t *plugin_write_dequeue */
698 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
699 {
700 while (write_loop)
701 {
702 value_list_t *vl = plugin_write_dequeue ();
703 if (vl == NULL)
704 continue;
706 plugin_dispatch_values_internal (vl);
708 plugin_value_list_free (vl);
709 }
711 pthread_exit (NULL);
712 return ((void *) 0);
713 } /* }}} void *plugin_write_thread */
715 static void start_write_threads (size_t num) /* {{{ */
716 {
717 size_t i;
719 if (write_threads != NULL)
720 return;
722 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
723 if (write_threads == NULL)
724 {
725 ERROR ("plugin: start_write_threads: calloc failed.");
726 return;
727 }
729 write_threads_num = 0;
730 for (i = 0; i < num; i++)
731 {
732 int status;
734 status = pthread_create (write_threads + write_threads_num,
735 /* attr = */ NULL,
736 plugin_write_thread,
737 /* arg = */ NULL);
738 if (status != 0)
739 {
740 char errbuf[1024];
741 ERROR ("plugin: start_write_threads: pthread_create failed "
742 "with status %i (%s).", status,
743 sstrerror (status, errbuf, sizeof (errbuf)));
744 return;
745 }
747 write_threads_num++;
748 } /* for (i) */
749 } /* }}} void start_write_threads */
751 static void stop_write_threads (void) /* {{{ */
752 {
753 write_queue_t *q;
754 int i;
756 if (write_threads == NULL)
757 return;
759 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
761 pthread_mutex_lock (&write_lock);
762 write_loop = 0;
763 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
764 pthread_cond_broadcast (&write_cond);
765 pthread_mutex_unlock (&write_lock);
767 for (i = 0; i < write_threads_num; i++)
768 {
769 if (pthread_join (write_threads[i], NULL) != 0)
770 {
771 ERROR ("plugin: stop_write_threads: pthread_join failed.");
772 }
773 write_threads[i] = (pthread_t) 0;
774 }
775 sfree (write_threads);
776 write_threads_num = 0;
778 pthread_mutex_lock (&write_lock);
779 i = 0;
780 for (q = write_queue_head; q != NULL; q = q->next)
781 {
782 plugin_value_list_free (q->vl);
783 sfree (q);
784 i++;
785 }
786 write_queue_head = NULL;
787 write_queue_tail = NULL;
788 pthread_mutex_unlock (&write_lock);
790 if (i > 0)
791 {
792 WARNING ("plugin: %i value list%s left after shutting down "
793 "the write threads.",
794 i, (i == 1) ? " was" : "s were");
795 }
796 } /* }}} void stop_write_threads */
798 /*
799 * Public functions
800 */
801 void plugin_set_dir (const char *dir)
802 {
803 if (plugindir != NULL)
804 free (plugindir);
806 if (dir == NULL)
807 plugindir = NULL;
808 else if ((plugindir = strdup (dir)) == NULL)
809 {
810 char errbuf[1024];
811 ERROR ("strdup failed: %s",
812 sstrerror (errno, errbuf, sizeof (errbuf)));
813 }
814 }
816 #define BUFSIZE 512
817 int plugin_load (const char *type, uint32_t flags)
818 {
819 DIR *dh;
820 const char *dir;
821 char filename[BUFSIZE] = "";
822 char typename[BUFSIZE];
823 int typename_len;
824 int ret;
825 struct stat statbuf;
826 struct dirent *de;
827 int status;
829 DEBUG ("type = %s", type);
831 dir = plugin_get_dir ();
832 ret = 1;
834 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
835 * type when matching the filename */
836 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
837 if ((status < 0) || ((size_t) status >= sizeof (typename)))
838 {
839 WARNING ("snprintf: truncated: `%s.so'", type);
840 return (-1);
841 }
842 typename_len = strlen (typename);
844 if ((dh = opendir (dir)) == NULL)
845 {
846 char errbuf[1024];
847 ERROR ("opendir (%s): %s", dir,
848 sstrerror (errno, errbuf, sizeof (errbuf)));
849 return (-1);
850 }
852 while ((de = readdir (dh)) != NULL)
853 {
854 if (strncasecmp (de->d_name, typename, typename_len))
855 continue;
857 status = ssnprintf (filename, sizeof (filename),
858 "%s/%s", dir, de->d_name);
859 if ((status < 0) || ((size_t) status >= sizeof (filename)))
860 {
861 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
862 continue;
863 }
865 if (lstat (filename, &statbuf) == -1)
866 {
867 char errbuf[1024];
868 WARNING ("stat %s: %s", filename,
869 sstrerror (errno, errbuf, sizeof (errbuf)));
870 continue;
871 }
872 else if (!S_ISREG (statbuf.st_mode))
873 {
874 /* don't follow symlinks */
875 WARNING ("stat %s: not a regular file", filename);
876 continue;
877 }
879 if (plugin_load_file (filename, flags) == 0)
880 {
881 /* success */
882 ret = 0;
883 break;
884 }
885 else
886 {
887 fprintf (stderr, "Unable to load plugin %s.\n", type);
888 }
889 }
891 closedir (dh);
893 if (filename[0] == '\0')
894 fprintf (stderr, "Could not find plugin %s.\n", type);
896 return (ret);
897 }
899 /*
900 * The `register_*' functions follow
901 */
902 int plugin_register_config (const char *name,
903 int (*callback) (const char *key, const char *val),
904 const char **keys, int keys_num)
905 {
906 cf_register (name, callback, keys, keys_num);
907 return (0);
908 } /* int plugin_register_config */
910 int plugin_register_complex_config (const char *type,
911 int (*callback) (oconfig_item_t *))
912 {
913 return (cf_register_complex (type, callback));
914 } /* int plugin_register_complex_config */
916 int plugin_register_init (const char *name,
917 int (*callback) (void))
918 {
919 return (create_register_callback (&list_init, name, (void *) callback,
920 /* user_data = */ NULL));
921 } /* plugin_register_init */
923 static int plugin_compare_read_func (const void *arg0, const void *arg1)
924 {
925 const read_func_t *rf0;
926 const read_func_t *rf1;
928 rf0 = arg0;
929 rf1 = arg1;
931 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
932 return (-1);
933 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
934 return (1);
935 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
936 return (-1);
937 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
938 return (1);
939 else
940 return (0);
941 } /* int plugin_compare_read_func */
943 /* Add a read function to both, the heap and a linked list. The linked list if
944 * used to look-up read functions, especially for the remove function. The heap
945 * is used to determine which plugin to read next. */
946 static int plugin_insert_read (read_func_t *rf)
947 {
948 int status;
949 llentry_t *le;
951 pthread_mutex_lock (&read_lock);
953 if (read_list == NULL)
954 {
955 read_list = llist_create ();
956 if (read_list == NULL)
957 {
958 pthread_mutex_unlock (&read_lock);
959 ERROR ("plugin_insert_read: read_list failed.");
960 return (-1);
961 }
962 }
964 if (read_heap == NULL)
965 {
966 read_heap = c_heap_create (plugin_compare_read_func);
967 if (read_heap == NULL)
968 {
969 pthread_mutex_unlock (&read_lock);
970 ERROR ("plugin_insert_read: c_heap_create failed.");
971 return (-1);
972 }
973 }
975 le = llist_search (read_list, rf->rf_name);
976 if (le != NULL)
977 {
978 pthread_mutex_unlock (&read_lock);
979 WARNING ("The read function \"%s\" is already registered. "
980 "Check for duplicate \"LoadPlugin\" lines "
981 "in your configuration!",
982 rf->rf_name);
983 return (EINVAL);
984 }
986 le = llentry_create (rf->rf_name, rf);
987 if (le == NULL)
988 {
989 pthread_mutex_unlock (&read_lock);
990 ERROR ("plugin_insert_read: llentry_create failed.");
991 return (-1);
992 }
994 status = c_heap_insert (read_heap, rf);
995 if (status != 0)
996 {
997 pthread_mutex_unlock (&read_lock);
998 ERROR ("plugin_insert_read: c_heap_insert failed.");
999 llentry_destroy (le);
1000 return (-1);
1001 }
1003 /* This does not fail. */
1004 llist_append (read_list, le);
1006 /* Wake up all the read threads. */
1007 pthread_cond_broadcast (&read_cond);
1008 pthread_mutex_unlock (&read_lock);
1009 return (0);
1010 } /* int plugin_insert_read */
1012 static int read_cb_wrapper (user_data_t *ud)
1013 {
1014 int (*callback) (void);
1016 if (ud == NULL)
1017 return -1;
1019 callback = ud->data;
1020 return callback();
1021 } /* int read_cb_wrapper */
1023 int plugin_register_read (const char *name,
1024 int (*callback) (void))
1025 {
1026 read_func_t *rf;
1027 plugin_ctx_t ctx = plugin_get_ctx ();
1028 int status;
1030 if (ctx.interval != 0) {
1031 /* If ctx.interval is not zero (== use the plugin or global
1032 * interval), we need to use the "complex" read callback,
1033 * because only that allows to specify a different interval.
1034 * Wrap the callback using read_cb_wrapper(). */
1035 struct timespec interval;
1036 user_data_t user_data;
1038 user_data.data = callback;
1039 user_data.free_func = NULL;
1041 CDTIME_T_TO_TIMESPEC (ctx.interval, &interval);
1042 return plugin_register_complex_read (/* group = */ NULL,
1043 name, read_cb_wrapper, &interval, &user_data);
1044 }
1046 DEBUG ("plugin_register_read: default_interval = %.3f",
1047 CDTIME_T_TO_DOUBLE(plugin_get_interval ()));
1049 rf = malloc (sizeof (*rf));
1050 if (rf == NULL)
1051 {
1052 ERROR ("plugin_register_read: malloc failed.");
1053 return (ENOMEM);
1054 }
1056 memset (rf, 0, sizeof (read_func_t));
1057 rf->rf_callback = (void *) callback;
1058 rf->rf_udata.data = NULL;
1059 rf->rf_udata.free_func = NULL;
1060 rf->rf_ctx = ctx;
1061 rf->rf_group[0] = '\0';
1062 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1063 rf->rf_type = RF_SIMPLE;
1064 rf->rf_interval.tv_sec = 0;
1065 rf->rf_interval.tv_nsec = 0;
1066 rf->rf_effective_interval = rf->rf_interval;
1068 status = plugin_insert_read (rf);
1069 if (status != 0)
1070 sfree (rf);
1072 return (status);
1073 } /* int plugin_register_read */
1075 int plugin_register_complex_read (const char *group, const char *name,
1076 plugin_read_cb callback,
1077 const struct timespec *interval,
1078 user_data_t *user_data)
1079 {
1080 read_func_t *rf;
1081 plugin_ctx_t ctx = plugin_get_ctx ();
1082 int status;
1084 rf = malloc (sizeof (*rf));
1085 if (rf == NULL)
1086 {
1087 ERROR ("plugin_register_complex_read: malloc failed.");
1088 return (ENOMEM);
1089 }
1091 memset (rf, 0, sizeof (read_func_t));
1092 rf->rf_callback = (void *) callback;
1093 if (group != NULL)
1094 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1095 else
1096 rf->rf_group[0] = '\0';
1097 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1098 rf->rf_type = RF_COMPLEX;
1099 if (interval != NULL)
1100 {
1101 rf->rf_interval = *interval;
1102 }
1103 else if (ctx.interval != 0)
1104 {
1105 CDTIME_T_TO_TIMESPEC (ctx.interval, &rf->rf_interval);
1106 }
1107 rf->rf_effective_interval = rf->rf_interval;
1109 DEBUG ("plugin_register_read: interval = %i.%09i",
1110 (int) rf->rf_interval.tv_sec,
1111 (int) rf->rf_interval.tv_nsec);
1113 /* Set user data */
1114 if (user_data == NULL)
1115 {
1116 rf->rf_udata.data = NULL;
1117 rf->rf_udata.free_func = NULL;
1118 }
1119 else
1120 {
1121 rf->rf_udata = *user_data;
1122 }
1124 rf->rf_ctx = ctx;
1126 status = plugin_insert_read (rf);
1127 if (status != 0)
1128 sfree (rf);
1130 return (status);
1131 } /* int plugin_register_complex_read */
1133 int plugin_register_write (const char *name,
1134 plugin_write_cb callback, user_data_t *ud)
1135 {
1136 return (create_register_callback (&list_write, name,
1137 (void *) callback, ud));
1138 } /* int plugin_register_write */
1140 int plugin_register_flush (const char *name,
1141 plugin_flush_cb callback, user_data_t *ud)
1142 {
1143 return (create_register_callback (&list_flush, name,
1144 (void *) callback, ud));
1145 } /* int plugin_register_flush */
1147 int plugin_register_missing (const char *name,
1148 plugin_missing_cb callback, user_data_t *ud)
1149 {
1150 return (create_register_callback (&list_missing, name,
1151 (void *) callback, ud));
1152 } /* int plugin_register_missing */
1154 int plugin_register_shutdown (const char *name,
1155 int (*callback) (void))
1156 {
1157 return (create_register_callback (&list_shutdown, name,
1158 (void *) callback, /* user_data = */ NULL));
1159 } /* int plugin_register_shutdown */
1161 int plugin_register_data_set (const data_set_t *ds)
1162 {
1163 data_set_t *ds_copy;
1164 int i;
1166 if ((data_sets != NULL)
1167 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1168 {
1169 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1170 plugin_unregister_data_set (ds->type);
1171 }
1172 else if (data_sets == NULL)
1173 {
1174 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1175 if (data_sets == NULL)
1176 return (-1);
1177 }
1179 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1180 if (ds_copy == NULL)
1181 return (-1);
1182 memcpy(ds_copy, ds, sizeof (data_set_t));
1184 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1185 * ds->ds_num);
1186 if (ds_copy->ds == NULL)
1187 {
1188 free (ds_copy);
1189 return (-1);
1190 }
1192 for (i = 0; i < ds->ds_num; i++)
1193 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1195 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1196 } /* int plugin_register_data_set */
1198 int plugin_register_log (const char *name,
1199 plugin_log_cb callback, user_data_t *ud)
1200 {
1201 return (create_register_callback (&list_log, name,
1202 (void *) callback, ud));
1203 } /* int plugin_register_log */
1205 int plugin_register_notification (const char *name,
1206 plugin_notification_cb callback, user_data_t *ud)
1207 {
1208 return (create_register_callback (&list_notification, name,
1209 (void *) callback, ud));
1210 } /* int plugin_register_log */
1212 int plugin_unregister_config (const char *name)
1213 {
1214 cf_unregister (name);
1215 return (0);
1216 } /* int plugin_unregister_config */
1218 int plugin_unregister_complex_config (const char *name)
1219 {
1220 cf_unregister_complex (name);
1221 return (0);
1222 } /* int plugin_unregister_complex_config */
1224 int plugin_unregister_init (const char *name)
1225 {
1226 return (plugin_unregister (list_init, name));
1227 }
1229 int plugin_unregister_read (const char *name) /* {{{ */
1230 {
1231 llentry_t *le;
1232 read_func_t *rf;
1234 if (name == NULL)
1235 return (-ENOENT);
1237 pthread_mutex_lock (&read_lock);
1239 if (read_list == NULL)
1240 {
1241 pthread_mutex_unlock (&read_lock);
1242 return (-ENOENT);
1243 }
1245 le = llist_search (read_list, name);
1246 if (le == NULL)
1247 {
1248 pthread_mutex_unlock (&read_lock);
1249 WARNING ("plugin_unregister_read: No such read function: %s",
1250 name);
1251 return (-ENOENT);
1252 }
1254 llist_remove (read_list, le);
1256 rf = le->value;
1257 assert (rf != NULL);
1258 rf->rf_type = RF_REMOVE;
1260 pthread_mutex_unlock (&read_lock);
1262 llentry_destroy (le);
1264 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1266 return (0);
1267 } /* }}} int plugin_unregister_read */
1269 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1270 {
1271 read_func_t *rf = e->value;
1272 char *group = ud;
1274 return strcmp (rf->rf_group, (const char *)group);
1275 } /* }}} int compare_read_func_group */
1277 int plugin_unregister_read_group (const char *group) /* {{{ */
1278 {
1279 llentry_t *le;
1280 read_func_t *rf;
1282 int found = 0;
1284 if (group == NULL)
1285 return (-ENOENT);
1287 pthread_mutex_lock (&read_lock);
1289 if (read_list == NULL)
1290 {
1291 pthread_mutex_unlock (&read_lock);
1292 return (-ENOENT);
1293 }
1295 while (42)
1296 {
1297 le = llist_search_custom (read_list,
1298 compare_read_func_group, (void *)group);
1300 if (le == NULL)
1301 break;
1303 ++found;
1305 llist_remove (read_list, le);
1307 rf = le->value;
1308 assert (rf != NULL);
1309 rf->rf_type = RF_REMOVE;
1311 llentry_destroy (le);
1313 DEBUG ("plugin_unregister_read_group: "
1314 "Marked `%s' (group `%s') for removal.",
1315 rf->rf_name, group);
1316 }
1318 pthread_mutex_unlock (&read_lock);
1320 if (found == 0)
1321 {
1322 WARNING ("plugin_unregister_read_group: No such "
1323 "group of read function: %s", group);
1324 return (-ENOENT);
1325 }
1327 return (0);
1328 } /* }}} int plugin_unregister_read_group */
1330 int plugin_unregister_write (const char *name)
1331 {
1332 return (plugin_unregister (list_write, name));
1333 }
1335 int plugin_unregister_flush (const char *name)
1336 {
1337 return (plugin_unregister (list_flush, name));
1338 }
1340 int plugin_unregister_missing (const char *name)
1341 {
1342 return (plugin_unregister (list_missing, name));
1343 }
1345 int plugin_unregister_shutdown (const char *name)
1346 {
1347 return (plugin_unregister (list_shutdown, name));
1348 }
1350 int plugin_unregister_data_set (const char *name)
1351 {
1352 data_set_t *ds;
1354 if (data_sets == NULL)
1355 return (-1);
1357 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1358 return (-1);
1360 sfree (ds->ds);
1361 sfree (ds);
1363 return (0);
1364 } /* int plugin_unregister_data_set */
1366 int plugin_unregister_log (const char *name)
1367 {
1368 return (plugin_unregister (list_log, name));
1369 }
1371 int plugin_unregister_notification (const char *name)
1372 {
1373 return (plugin_unregister (list_notification, name));
1374 }
1376 void plugin_init_all (void)
1377 {
1378 const char *chain_name;
1379 llentry_t *le;
1380 int status;
1382 /* Init the value cache */
1383 uc_init ();
1385 chain_name = global_option_get ("PreCacheChain");
1386 pre_cache_chain = fc_chain_get_by_name (chain_name);
1388 chain_name = global_option_get ("PostCacheChain");
1389 post_cache_chain = fc_chain_get_by_name (chain_name);
1391 {
1392 char const *tmp = global_option_get ("WriteThreads");
1393 int num = atoi (tmp);
1395 if (num < 1)
1396 num = 5;
1398 start_write_threads ((size_t) num);
1399 }
1401 if ((list_init == NULL) && (read_heap == NULL))
1402 return;
1404 /* Calling all init callbacks before checking if read callbacks
1405 * are available allows the init callbacks to register the read
1406 * callback. */
1407 le = llist_head (list_init);
1408 while (le != NULL)
1409 {
1410 callback_func_t *cf;
1411 plugin_init_cb callback;
1412 plugin_ctx_t old_ctx;
1414 cf = le->value;
1415 old_ctx = plugin_set_ctx (cf->cf_ctx);
1416 callback = cf->cf_callback;
1417 status = (*callback) ();
1418 plugin_set_ctx (old_ctx);
1420 if (status != 0)
1421 {
1422 ERROR ("Initialization of plugin `%s' "
1423 "failed with status %i. "
1424 "Plugin will be unloaded.",
1425 le->key, status);
1426 /* Plugins that register read callbacks from the init
1427 * callback should take care of appropriate error
1428 * handling themselves. */
1429 /* FIXME: Unload _all_ functions */
1430 plugin_unregister_read (le->key);
1431 }
1433 le = le->next;
1434 }
1436 /* Start read-threads */
1437 if (read_heap != NULL)
1438 {
1439 const char *rt;
1440 int num;
1441 rt = global_option_get ("ReadThreads");
1442 num = atoi (rt);
1443 if (num != -1)
1444 start_read_threads ((num > 0) ? num : 5);
1445 }
1446 } /* void plugin_init_all */
1448 /* TODO: Rename this function. */
1449 void plugin_read_all (void)
1450 {
1451 uc_check_timeout ();
1453 return;
1454 } /* void plugin_read_all */
1456 /* Read function called when the `-T' command line argument is given. */
1457 int plugin_read_all_once (void)
1458 {
1459 int status;
1460 int return_status = 0;
1462 if (read_heap == NULL)
1463 {
1464 NOTICE ("No read-functions are registered.");
1465 return (0);
1466 }
1468 while (42)
1469 {
1470 read_func_t *rf;
1471 plugin_ctx_t old_ctx;
1473 rf = c_heap_get_root (read_heap);
1474 if (rf == NULL)
1475 break;
1477 old_ctx = plugin_set_ctx (rf->rf_ctx);
1479 if (rf->rf_type == RF_SIMPLE)
1480 {
1481 int (*callback) (void);
1483 callback = rf->rf_callback;
1484 status = (*callback) ();
1485 }
1486 else
1487 {
1488 plugin_read_cb callback;
1490 callback = rf->rf_callback;
1491 status = (*callback) (&rf->rf_udata);
1492 }
1494 plugin_set_ctx (old_ctx);
1496 if (status != 0)
1497 {
1498 NOTICE ("read-function of plugin `%s' failed.",
1499 rf->rf_name);
1500 return_status = -1;
1501 }
1503 destroy_callback ((void *) rf);
1504 }
1506 return (return_status);
1507 } /* int plugin_read_all_once */
1509 int plugin_write (const char *plugin, /* {{{ */
1510 const data_set_t *ds, const value_list_t *vl)
1511 {
1512 llentry_t *le;
1513 int status;
1515 if (vl == NULL)
1516 return (EINVAL);
1518 if (list_write == NULL)
1519 return (ENOENT);
1521 if (ds == NULL)
1522 {
1523 ds = plugin_get_ds (vl->type);
1524 if (ds == NULL)
1525 {
1526 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1527 return (ENOENT);
1528 }
1529 }
1531 if (plugin == NULL)
1532 {
1533 int success = 0;
1534 int failure = 0;
1536 le = llist_head (list_write);
1537 while (le != NULL)
1538 {
1539 callback_func_t *cf = le->value;
1540 plugin_write_cb callback;
1542 /* do not switch plugin context; rather keep the context (interval)
1543 * information of the calling read plugin */
1545 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1546 callback = cf->cf_callback;
1547 status = (*callback) (ds, vl, &cf->cf_udata);
1548 if (status != 0)
1549 failure++;
1550 else
1551 success++;
1553 le = le->next;
1554 }
1556 if ((success == 0) && (failure != 0))
1557 status = -1;
1558 else
1559 status = 0;
1560 }
1561 else /* plugin != NULL */
1562 {
1563 callback_func_t *cf;
1564 plugin_write_cb callback;
1566 le = llist_head (list_write);
1567 while (le != NULL)
1568 {
1569 if (strcasecmp (plugin, le->key) == 0)
1570 break;
1572 le = le->next;
1573 }
1575 if (le == NULL)
1576 return (ENOENT);
1578 cf = le->value;
1580 /* do not switch plugin context; rather keep the context (interval)
1581 * information of the calling read plugin */
1583 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1584 callback = cf->cf_callback;
1585 status = (*callback) (ds, vl, &cf->cf_udata);
1586 }
1588 return (status);
1589 } /* }}} int plugin_write */
1591 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1592 {
1593 llentry_t *le;
1595 if (list_flush == NULL)
1596 return (0);
1598 le = llist_head (list_flush);
1599 while (le != NULL)
1600 {
1601 callback_func_t *cf;
1602 plugin_flush_cb callback;
1603 plugin_ctx_t old_ctx;
1605 if ((plugin != NULL)
1606 && (strcmp (plugin, le->key) != 0))
1607 {
1608 le = le->next;
1609 continue;
1610 }
1612 cf = le->value;
1613 old_ctx = plugin_set_ctx (cf->cf_ctx);
1614 callback = cf->cf_callback;
1616 (*callback) (timeout, identifier, &cf->cf_udata);
1618 plugin_set_ctx (old_ctx);
1620 le = le->next;
1621 }
1622 return (0);
1623 } /* int plugin_flush */
1625 void plugin_shutdown_all (void)
1626 {
1627 llentry_t *le;
1629 stop_read_threads ();
1631 destroy_all_callbacks (&list_init);
1633 pthread_mutex_lock (&read_lock);
1634 llist_destroy (read_list);
1635 read_list = NULL;
1636 pthread_mutex_unlock (&read_lock);
1638 destroy_read_heap ();
1640 plugin_flush (/* plugin = */ NULL,
1641 /* timeout = */ 0,
1642 /* identifier = */ NULL);
1644 le = NULL;
1645 if (list_shutdown != NULL)
1646 le = llist_head (list_shutdown);
1648 while (le != NULL)
1649 {
1650 callback_func_t *cf;
1651 plugin_shutdown_cb callback;
1652 plugin_ctx_t old_ctx;
1654 cf = le->value;
1655 old_ctx = plugin_set_ctx (cf->cf_ctx);
1656 callback = cf->cf_callback;
1658 /* Advance the pointer before calling the callback allows
1659 * shutdown functions to unregister themselves. If done the
1660 * other way around the memory `le' points to will be freed
1661 * after callback returns. */
1662 le = le->next;
1664 (*callback) ();
1666 plugin_set_ctx (old_ctx);
1667 }
1669 stop_write_threads ();
1671 /* Write plugins which use the `user_data' pointer usually need the
1672 * same data available to the flush callback. If this is the case, set
1673 * the free_function to NULL when registering the flush callback and to
1674 * the real free function when registering the write callback. This way
1675 * the data isn't freed twice. */
1676 destroy_all_callbacks (&list_flush);
1677 destroy_all_callbacks (&list_missing);
1678 destroy_all_callbacks (&list_write);
1680 destroy_all_callbacks (&list_notification);
1681 destroy_all_callbacks (&list_shutdown);
1682 destroy_all_callbacks (&list_log);
1683 } /* void plugin_shutdown_all */
1685 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1686 {
1687 llentry_t *le;
1689 if (list_missing == NULL)
1690 return (0);
1692 le = llist_head (list_missing);
1693 while (le != NULL)
1694 {
1695 callback_func_t *cf;
1696 plugin_missing_cb callback;
1697 plugin_ctx_t old_ctx;
1698 int status;
1700 cf = le->value;
1701 old_ctx = plugin_set_ctx (cf->cf_ctx);
1702 callback = cf->cf_callback;
1704 status = (*callback) (vl, &cf->cf_udata);
1705 plugin_set_ctx (old_ctx);
1706 if (status != 0)
1707 {
1708 if (status < 0)
1709 {
1710 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1711 "failed with status %i.",
1712 le->key, status);
1713 return (status);
1714 }
1715 else
1716 {
1717 return (0);
1718 }
1719 }
1721 le = le->next;
1722 }
1723 return (0);
1724 } /* int }}} plugin_dispatch_missing */
1726 static int plugin_dispatch_values_internal (value_list_t *vl)
1727 {
1728 int status;
1729 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1731 value_t *saved_values;
1732 int saved_values_len;
1734 data_set_t *ds;
1736 int free_meta_data = 0;
1738 if ((vl == NULL) || (vl->type[0] == 0)
1739 || (vl->values == NULL) || (vl->values_len < 1))
1740 {
1741 ERROR ("plugin_dispatch_values: Invalid value list "
1742 "from plugin %s.", vl->plugin);
1743 return (-1);
1744 }
1746 /* Free meta data only if the calling function didn't specify any. In
1747 * this case matches and targets may add some and the calling function
1748 * may not expect (and therefore free) that data. */
1749 if (vl->meta == NULL)
1750 free_meta_data = 1;
1752 if (list_write == NULL)
1753 c_complain_once (LOG_WARNING, &no_write_complaint,
1754 "plugin_dispatch_values: No write callback has been "
1755 "registered. Please load at least one output plugin, "
1756 "if you want the collected data to be stored.");
1758 if (data_sets == NULL)
1759 {
1760 ERROR ("plugin_dispatch_values: No data sets registered. "
1761 "Could the types database be read? Check "
1762 "your `TypesDB' setting!");
1763 return (-1);
1764 }
1766 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1767 {
1768 char ident[6 * DATA_MAX_NAME_LEN];
1770 FORMAT_VL (ident, sizeof (ident), vl);
1771 INFO ("plugin_dispatch_values: Dataset not found: %s "
1772 "(from \"%s\"), check your types.db!",
1773 vl->type, ident);
1774 return (-1);
1775 }
1777 if (vl->time == 0)
1778 vl->time = cdtime ();
1780 if (vl->interval <= 0)
1781 {
1782 plugin_ctx_t ctx = plugin_get_ctx ();
1784 if (ctx.interval != 0)
1785 vl->interval = ctx.interval;
1786 else
1787 {
1788 char name[6 * DATA_MAX_NAME_LEN];
1789 FORMAT_VL (name, sizeof (name), vl);
1790 ERROR ("plugin_dispatch_values: Unable to determine "
1791 "interval from context for "
1792 "value list \"%s\". "
1793 "This indicates a broken plugin. "
1794 "Please report this problem to the "
1795 "collectd mailing list or at "
1796 "<http://collectd.org/bugs/>.", name);
1797 vl->interval = cf_get_default_interval ();
1798 }
1799 }
1801 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1802 "host = %s; "
1803 "plugin = %s; plugin_instance = %s; "
1804 "type = %s; type_instance = %s;",
1805 CDTIME_T_TO_DOUBLE (vl->time),
1806 CDTIME_T_TO_DOUBLE (vl->interval),
1807 vl->host,
1808 vl->plugin, vl->plugin_instance,
1809 vl->type, vl->type_instance);
1811 #if COLLECT_DEBUG
1812 assert (0 == strcmp (ds->type, vl->type));
1813 #else
1814 if (0 != strcmp (ds->type, vl->type))
1815 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1816 ds->type, vl->type);
1817 #endif
1819 #if COLLECT_DEBUG
1820 assert (ds->ds_num == vl->values_len);
1821 #else
1822 if (ds->ds_num != vl->values_len)
1823 {
1824 ERROR ("plugin_dispatch_values: ds->type = %s: "
1825 "(ds->ds_num = %i) != "
1826 "(vl->values_len = %i)",
1827 ds->type, ds->ds_num, vl->values_len);
1828 return (-1);
1829 }
1830 #endif
1832 escape_slashes (vl->host, sizeof (vl->host));
1833 escape_slashes (vl->plugin, sizeof (vl->plugin));
1834 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1835 escape_slashes (vl->type, sizeof (vl->type));
1836 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1838 /* Copy the values. This way, we can assure `targets' that they get
1839 * dynamically allocated values, which they can free and replace if
1840 * they like. */
1841 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1842 {
1843 saved_values = vl->values;
1844 saved_values_len = vl->values_len;
1846 vl->values = (value_t *) calloc (vl->values_len,
1847 sizeof (*vl->values));
1848 if (vl->values == NULL)
1849 {
1850 ERROR ("plugin_dispatch_values: calloc failed.");
1851 vl->values = saved_values;
1852 return (-1);
1853 }
1854 memcpy (vl->values, saved_values,
1855 vl->values_len * sizeof (*vl->values));
1856 }
1857 else /* if ((pre == NULL) && (post == NULL)) */
1858 {
1859 saved_values = NULL;
1860 saved_values_len = 0;
1861 }
1863 if (pre_cache_chain != NULL)
1864 {
1865 status = fc_process_chain (ds, vl, pre_cache_chain);
1866 if (status < 0)
1867 {
1868 WARNING ("plugin_dispatch_values: Running the "
1869 "pre-cache chain failed with "
1870 "status %i (%#x).",
1871 status, status);
1872 }
1873 else if (status == FC_TARGET_STOP)
1874 {
1875 /* Restore the state of the value_list so that plugins
1876 * don't get confused.. */
1877 if (saved_values != NULL)
1878 {
1879 free (vl->values);
1880 vl->values = saved_values;
1881 vl->values_len = saved_values_len;
1882 }
1883 return (0);
1884 }
1885 }
1887 /* Update the value cache */
1888 uc_update (ds, vl);
1890 if (post_cache_chain != NULL)
1891 {
1892 status = fc_process_chain (ds, vl, post_cache_chain);
1893 if (status < 0)
1894 {
1895 WARNING ("plugin_dispatch_values: Running the "
1896 "post-cache chain failed with "
1897 "status %i (%#x).",
1898 status, status);
1899 }
1900 }
1901 else
1902 fc_default_action (ds, vl);
1904 /* Restore the state of the value_list so that plugins don't get
1905 * confused.. */
1906 if (saved_values != NULL)
1907 {
1908 free (vl->values);
1909 vl->values = saved_values;
1910 vl->values_len = saved_values_len;
1911 }
1913 if ((free_meta_data != 0) && (vl->meta != NULL))
1914 {
1915 meta_data_destroy (vl->meta);
1916 vl->meta = NULL;
1917 }
1919 return (0);
1920 } /* int plugin_dispatch_values_internal */
1922 int plugin_dispatch_values (value_list_t const *vl)
1923 {
1924 int status;
1926 status = plugin_write_enqueue (vl);
1927 if (status != 0)
1928 {
1929 char errbuf[1024];
1930 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
1931 "with status %i (%s).", status,
1932 sstrerror (status, errbuf, sizeof (errbuf)));
1933 return (status);
1934 }
1936 return (0);
1937 }
1939 int plugin_dispatch_notification (const notification_t *notif)
1940 {
1941 llentry_t *le;
1942 /* Possible TODO: Add flap detection here */
1944 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1945 "time = %.3f; host = %s;",
1946 notif->severity, notif->message,
1947 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1949 /* Nobody cares for notifications */
1950 if (list_notification == NULL)
1951 return (-1);
1953 le = llist_head (list_notification);
1954 while (le != NULL)
1955 {
1956 callback_func_t *cf;
1957 plugin_notification_cb callback;
1958 int status;
1960 /* do not switch plugin context; rather keep the context
1961 * (interval) information of the calling plugin */
1963 cf = le->value;
1964 callback = cf->cf_callback;
1965 status = (*callback) (notif, &cf->cf_udata);
1966 if (status != 0)
1967 {
1968 WARNING ("plugin_dispatch_notification: Notification "
1969 "callback %s returned %i.",
1970 le->key, status);
1971 }
1973 le = le->next;
1974 }
1976 return (0);
1977 } /* int plugin_dispatch_notification */
1979 void plugin_log (int level, const char *format, ...)
1980 {
1981 char msg[1024];
1982 va_list ap;
1983 llentry_t *le;
1985 #if !COLLECT_DEBUG
1986 if (level >= LOG_DEBUG)
1987 return;
1988 #endif
1990 va_start (ap, format);
1991 vsnprintf (msg, sizeof (msg), format, ap);
1992 msg[sizeof (msg) - 1] = '\0';
1993 va_end (ap);
1995 if (list_log == NULL)
1996 {
1997 fprintf (stderr, "%s\n", msg);
1998 return;
1999 }
2001 le = llist_head (list_log);
2002 while (le != NULL)
2003 {
2004 callback_func_t *cf;
2005 plugin_log_cb callback;
2007 cf = le->value;
2008 callback = cf->cf_callback;
2010 /* do not switch plugin context; rather keep the context
2011 * (interval) information of the calling plugin */
2013 (*callback) (level, msg, &cf->cf_udata);
2015 le = le->next;
2016 }
2017 } /* void plugin_log */
2019 int parse_log_severity (const char *severity)
2020 {
2021 int log_level = -1;
2023 if ((0 == strcasecmp (severity, "emerg"))
2024 || (0 == strcasecmp (severity, "alert"))
2025 || (0 == strcasecmp (severity, "crit"))
2026 || (0 == strcasecmp (severity, "err")))
2027 log_level = LOG_ERR;
2028 else if (0 == strcasecmp (severity, "warning"))
2029 log_level = LOG_WARNING;
2030 else if (0 == strcasecmp (severity, "notice"))
2031 log_level = LOG_NOTICE;
2032 else if (0 == strcasecmp (severity, "info"))
2033 log_level = LOG_INFO;
2034 #if COLLECT_DEBUG
2035 else if (0 == strcasecmp (severity, "debug"))
2036 log_level = LOG_DEBUG;
2037 #endif /* COLLECT_DEBUG */
2039 return (log_level);
2040 } /* int parse_log_severity */
2042 int parse_notif_severity (const char *severity)
2043 {
2044 int notif_severity = -1;
2046 if (strcasecmp (severity, "FAILURE") == 0)
2047 notif_severity = NOTIF_FAILURE;
2048 else if (strcmp (severity, "OKAY") == 0)
2049 notif_severity = NOTIF_OKAY;
2050 else if ((strcmp (severity, "WARNING") == 0)
2051 || (strcmp (severity, "WARN") == 0))
2052 notif_severity = NOTIF_WARNING;
2054 return (notif_severity);
2055 } /* int parse_notif_severity */
2057 const data_set_t *plugin_get_ds (const char *name)
2058 {
2059 data_set_t *ds;
2061 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2062 {
2063 DEBUG ("No such dataset registered: %s", name);
2064 return (NULL);
2065 }
2067 return (ds);
2068 } /* data_set_t *plugin_get_ds */
2070 static int plugin_notification_meta_add (notification_t *n,
2071 const char *name,
2072 enum notification_meta_type_e type,
2073 const void *value)
2074 {
2075 notification_meta_t *meta;
2076 notification_meta_t *tail;
2078 if ((n == NULL) || (name == NULL) || (value == NULL))
2079 {
2080 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2081 return (-1);
2082 }
2084 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2085 if (meta == NULL)
2086 {
2087 ERROR ("plugin_notification_meta_add: malloc failed.");
2088 return (-1);
2089 }
2090 memset (meta, 0, sizeof (notification_meta_t));
2092 sstrncpy (meta->name, name, sizeof (meta->name));
2093 meta->type = type;
2095 switch (type)
2096 {
2097 case NM_TYPE_STRING:
2098 {
2099 meta->nm_value.nm_string = strdup ((const char *) value);
2100 if (meta->nm_value.nm_string == NULL)
2101 {
2102 ERROR ("plugin_notification_meta_add: strdup failed.");
2103 sfree (meta);
2104 return (-1);
2105 }
2106 break;
2107 }
2108 case NM_TYPE_SIGNED_INT:
2109 {
2110 meta->nm_value.nm_signed_int = *((int64_t *) value);
2111 break;
2112 }
2113 case NM_TYPE_UNSIGNED_INT:
2114 {
2115 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2116 break;
2117 }
2118 case NM_TYPE_DOUBLE:
2119 {
2120 meta->nm_value.nm_double = *((double *) value);
2121 break;
2122 }
2123 case NM_TYPE_BOOLEAN:
2124 {
2125 meta->nm_value.nm_boolean = *((_Bool *) value);
2126 break;
2127 }
2128 default:
2129 {
2130 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2131 sfree (meta);
2132 return (-1);
2133 }
2134 } /* switch (type) */
2136 meta->next = NULL;
2137 tail = n->meta;
2138 while ((tail != NULL) && (tail->next != NULL))
2139 tail = tail->next;
2141 if (tail == NULL)
2142 n->meta = meta;
2143 else
2144 tail->next = meta;
2146 return (0);
2147 } /* int plugin_notification_meta_add */
2149 int plugin_notification_meta_add_string (notification_t *n,
2150 const char *name,
2151 const char *value)
2152 {
2153 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2154 }
2156 int plugin_notification_meta_add_signed_int (notification_t *n,
2157 const char *name,
2158 int64_t value)
2159 {
2160 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2161 }
2163 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2164 const char *name,
2165 uint64_t value)
2166 {
2167 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2168 }
2170 int plugin_notification_meta_add_double (notification_t *n,
2171 const char *name,
2172 double value)
2173 {
2174 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2175 }
2177 int plugin_notification_meta_add_boolean (notification_t *n,
2178 const char *name,
2179 _Bool value)
2180 {
2181 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2182 }
2184 int plugin_notification_meta_copy (notification_t *dst,
2185 const notification_t *src)
2186 {
2187 notification_meta_t *meta;
2189 assert (dst != NULL);
2190 assert (src != NULL);
2191 assert (dst != src);
2192 assert ((src->meta == NULL) || (src->meta != dst->meta));
2194 for (meta = src->meta; meta != NULL; meta = meta->next)
2195 {
2196 if (meta->type == NM_TYPE_STRING)
2197 plugin_notification_meta_add_string (dst, meta->name,
2198 meta->nm_value.nm_string);
2199 else if (meta->type == NM_TYPE_SIGNED_INT)
2200 plugin_notification_meta_add_signed_int (dst, meta->name,
2201 meta->nm_value.nm_signed_int);
2202 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2203 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2204 meta->nm_value.nm_unsigned_int);
2205 else if (meta->type == NM_TYPE_DOUBLE)
2206 plugin_notification_meta_add_double (dst, meta->name,
2207 meta->nm_value.nm_double);
2208 else if (meta->type == NM_TYPE_BOOLEAN)
2209 plugin_notification_meta_add_boolean (dst, meta->name,
2210 meta->nm_value.nm_boolean);
2211 }
2213 return (0);
2214 } /* int plugin_notification_meta_copy */
2216 int plugin_notification_meta_free (notification_meta_t *n)
2217 {
2218 notification_meta_t *this;
2219 notification_meta_t *next;
2221 if (n == NULL)
2222 {
2223 ERROR ("plugin_notification_meta_free: n == NULL!");
2224 return (-1);
2225 }
2227 this = n;
2228 while (this != NULL)
2229 {
2230 next = this->next;
2232 if (this->type == NM_TYPE_STRING)
2233 {
2234 free ((char *)this->nm_value.nm_string);
2235 this->nm_value.nm_string = NULL;
2236 }
2237 sfree (this);
2239 this = next;
2240 }
2242 return (0);
2243 } /* int plugin_notification_meta_free */
2245 static void plugin_ctx_destructor (void *ctx)
2246 {
2247 sfree (ctx);
2248 } /* void plugin_ctx_destructor */
2250 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2252 static plugin_ctx_t *plugin_ctx_create (void)
2253 {
2254 plugin_ctx_t *ctx;
2256 ctx = malloc (sizeof (*ctx));
2257 if (ctx == NULL) {
2258 char errbuf[1024];
2259 ERROR ("Failed to allocate plugin context: %s",
2260 sstrerror (errno, errbuf, sizeof (errbuf)));
2261 return NULL;
2262 }
2264 *ctx = ctx_init;
2265 assert (plugin_ctx_key_initialized);
2266 pthread_setspecific (plugin_ctx_key, ctx);
2267 DEBUG("Created new plugin context.");
2268 return (ctx);
2269 } /* int plugin_ctx_create */
2271 void plugin_init_ctx (void)
2272 {
2273 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2274 plugin_ctx_key_initialized = 1;
2275 } /* void plugin_init_ctx */
2277 plugin_ctx_t plugin_get_ctx (void)
2278 {
2279 plugin_ctx_t *ctx;
2281 assert (plugin_ctx_key_initialized);
2282 ctx = pthread_getspecific (plugin_ctx_key);
2284 if (ctx == NULL) {
2285 ctx = plugin_ctx_create ();
2286 /* this must no happen -- exit() instead? */
2287 if (ctx == NULL)
2288 return ctx_init;
2289 }
2291 return (*ctx);
2292 } /* plugin_ctx_t plugin_get_ctx */
2294 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2295 {
2296 plugin_ctx_t *c;
2297 plugin_ctx_t old;
2299 assert (plugin_ctx_key_initialized);
2300 c = pthread_getspecific (plugin_ctx_key);
2302 if (c == NULL) {
2303 c = plugin_ctx_create ();
2304 /* this must no happen -- exit() instead? */
2305 if (c == NULL)
2306 return ctx_init;
2307 }
2309 old = *c;
2310 *c = ctx;
2312 return (old);
2313 } /* void plugin_set_ctx */
2315 cdtime_t plugin_get_interval (void)
2316 {
2317 cdtime_t interval;
2319 interval = plugin_get_ctx().interval;
2320 if (interval > 0)
2321 return interval;
2323 return cf_get_default_interval ();
2324 } /* cdtime_t plugin_get_interval */
2326 typedef struct {
2327 plugin_ctx_t ctx;
2328 void *(*start_routine) (void *);
2329 void *arg;
2330 } plugin_thread_t;
2332 static void *plugin_thread_start (void *arg)
2333 {
2334 plugin_thread_t *plugin_thread = arg;
2336 void *(*start_routine) (void *) = plugin_thread->start_routine;
2337 void *plugin_arg = plugin_thread->arg;
2339 plugin_set_ctx (plugin_thread->ctx);
2341 free (plugin_thread);
2343 return start_routine (plugin_arg);
2344 } /* void *plugin_thread_start */
2346 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2347 void *(*start_routine) (void *), void *arg)
2348 {
2349 plugin_thread_t *plugin_thread;
2351 plugin_thread = malloc (sizeof (*plugin_thread));
2352 if (plugin_thread == NULL)
2353 return -1;
2355 plugin_thread->ctx = plugin_get_ctx ();
2356 plugin_thread->start_routine = start_routine;
2357 plugin_thread->arg = arg;
2359 return pthread_create (thread, attr,
2360 plugin_thread_start, plugin_thread);
2361 } /* int plugin_thread_create */
2363 /* vim: set sw=8 ts=8 noet fdm=marker : */