1 /**
2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2011 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 /*
73 * Private variables
74 */
75 static llist_t *list_init;
76 static llist_t *list_write;
77 static llist_t *list_flush;
78 static llist_t *list_missing;
79 static llist_t *list_shutdown;
80 static llist_t *list_log;
81 static llist_t *list_notification;
83 static fc_chain_t *pre_cache_chain = NULL;
84 static fc_chain_t *post_cache_chain = NULL;
86 static c_avl_tree_t *data_sets;
88 static char *plugindir = NULL;
90 static c_heap_t *read_heap = NULL;
91 static llist_t *read_list;
92 static int read_loop = 1;
93 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
94 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
95 static pthread_t *read_threads = NULL;
96 static int read_threads_num = 0;
98 static pthread_key_t plugin_ctx_key;
99 static _Bool plugin_ctx_key_initialized = 0;
101 /*
102 * Static functions
103 */
104 static const char *plugin_get_dir (void)
105 {
106 if (plugindir == NULL)
107 return (PLUGINDIR);
108 else
109 return (plugindir);
110 }
112 static void destroy_callback (callback_func_t *cf) /* {{{ */
113 {
114 if (cf == NULL)
115 return;
117 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
118 {
119 cf->cf_udata.free_func (cf->cf_udata.data);
120 cf->cf_udata.data = NULL;
121 cf->cf_udata.free_func = NULL;
122 }
123 sfree (cf);
124 } /* }}} void destroy_callback */
126 static void destroy_all_callbacks (llist_t **list) /* {{{ */
127 {
128 llentry_t *le;
130 if (*list == NULL)
131 return;
133 le = llist_head (*list);
134 while (le != NULL)
135 {
136 llentry_t *le_next;
138 le_next = le->next;
140 sfree (le->key);
141 destroy_callback (le->value);
142 le->value = NULL;
144 le = le_next;
145 }
147 llist_destroy (*list);
148 *list = NULL;
149 } /* }}} void destroy_all_callbacks */
151 static void destroy_read_heap (void) /* {{{ */
152 {
153 if (read_heap == NULL)
154 return;
156 while (42)
157 {
158 callback_func_t *cf;
160 cf = c_heap_get_root (read_heap);
161 if (cf == NULL)
162 break;
164 destroy_callback (cf);
165 }
167 c_heap_destroy (read_heap);
168 read_heap = NULL;
169 } /* }}} void destroy_read_heap */
171 static int register_callback (llist_t **list, /* {{{ */
172 const char *name, callback_func_t *cf)
173 {
174 llentry_t *le;
175 char *key;
177 if (*list == NULL)
178 {
179 *list = llist_create ();
180 if (*list == NULL)
181 {
182 ERROR ("plugin: register_callback: "
183 "llist_create failed.");
184 destroy_callback (cf);
185 return (-1);
186 }
187 }
189 key = strdup (name);
190 if (key == NULL)
191 {
192 ERROR ("plugin: register_callback: strdup failed.");
193 destroy_callback (cf);
194 return (-1);
195 }
197 le = llist_search (*list, name);
198 if (le == NULL)
199 {
200 le = llentry_create (key, cf);
201 if (le == NULL)
202 {
203 ERROR ("plugin: register_callback: "
204 "llentry_create failed.");
205 free (key);
206 destroy_callback (cf);
207 return (-1);
208 }
210 llist_append (*list, le);
211 }
212 else
213 {
214 callback_func_t *old_cf;
216 old_cf = le->value;
217 le->value = cf;
219 WARNING ("plugin: register_callback: "
220 "a callback named `%s' already exists - "
221 "overwriting the old entry!", name);
223 destroy_callback (old_cf);
224 sfree (key);
225 }
227 return (0);
228 } /* }}} int register_callback */
230 static int create_register_callback (llist_t **list, /* {{{ */
231 const char *name, void *callback, user_data_t *ud)
232 {
233 callback_func_t *cf;
235 cf = (callback_func_t *) malloc (sizeof (*cf));
236 if (cf == NULL)
237 {
238 ERROR ("plugin: create_register_callback: malloc failed.");
239 return (-1);
240 }
241 memset (cf, 0, sizeof (*cf));
243 cf->cf_callback = callback;
244 if (ud == NULL)
245 {
246 cf->cf_udata.data = NULL;
247 cf->cf_udata.free_func = NULL;
248 }
249 else
250 {
251 cf->cf_udata = *ud;
252 }
254 cf->cf_ctx = plugin_get_ctx ();
256 return (register_callback (list, name, cf));
257 } /* }}} int create_register_callback */
259 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
260 {
261 llentry_t *e;
263 if (list == NULL)
264 return (-1);
266 e = llist_search (list, name);
267 if (e == NULL)
268 return (-1);
270 llist_remove (list, e);
272 sfree (e->key);
273 destroy_callback (e->value);
275 llentry_destroy (e);
277 return (0);
278 } /* }}} int plugin_unregister */
280 /*
281 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
282 * object, but it will bitch about a shared object not having a
283 * ``module_register'' symbol..
284 */
285 static int plugin_load_file (char *file, uint32_t flags)
286 {
287 lt_dlhandle dlh;
288 void (*reg_handle) (void);
290 lt_dlinit ();
291 lt_dlerror (); /* clear errors */
293 #if LIBTOOL_VERSION == 2
294 if (flags & PLUGIN_FLAGS_GLOBAL) {
295 lt_dladvise advise;
296 lt_dladvise_init(&advise);
297 lt_dladvise_global(&advise);
298 dlh = lt_dlopenadvise(file, advise);
299 lt_dladvise_destroy(&advise);
300 } else {
301 dlh = lt_dlopen (file);
302 }
303 #else /* if LIBTOOL_VERSION == 1 */
304 if (flags & PLUGIN_FLAGS_GLOBAL)
305 WARNING ("plugin_load_file: The global flag is not supported, "
306 "libtool 2 is required for this.");
307 dlh = lt_dlopen (file);
308 #endif
310 if (dlh == NULL)
311 {
312 char errbuf[1024] = "";
314 ssnprintf (errbuf, sizeof (errbuf),
315 "lt_dlopen (\"%s\") failed: %s. "
316 "The most common cause for this problem are "
317 "missing dependencies. Use ldd(1) to check "
318 "the dependencies of the plugin "
319 "/ shared object.",
320 file, lt_dlerror ());
322 ERROR ("%s", errbuf);
323 /* Make sure this is printed to STDERR in any case, but also
324 * make sure it's printed only once. */
325 if (list_log != NULL)
326 fprintf (stderr, "ERROR: %s\n", errbuf);
328 return (1);
329 }
331 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
332 {
333 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
334 file, lt_dlerror ());
335 lt_dlclose (dlh);
336 return (-1);
337 }
339 (*reg_handle) ();
341 return (0);
342 }
344 static _Bool timeout_reached(struct timespec timeout)
345 {
346 struct timeval now;
347 gettimeofday(&now, NULL);
348 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
349 }
351 static void *plugin_read_thread (void __attribute__((unused)) *args)
352 {
353 while (read_loop != 0)
354 {
355 read_func_t *rf;
356 plugin_ctx_t old_ctx;
357 cdtime_t now;
358 int status;
359 int rf_type;
360 int rc;
362 /* Get the read function that needs to be read next.
363 * We don't need to hold "read_lock" for the heap, but we need
364 * to call c_heap_get_root() and pthread_cond_wait() in the
365 * same protected block. */
366 pthread_mutex_lock (&read_lock);
367 rf = c_heap_get_root (read_heap);
368 if (rf == NULL)
369 {
370 pthread_cond_wait (&read_cond, &read_lock);
371 pthread_mutex_unlock (&read_lock);
372 continue;
373 }
374 pthread_mutex_unlock (&read_lock);
376 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
377 {
378 /* this should not happen, because the interval is set
379 * for each plugin when loading it
380 * XXX: issue a warning? */
381 now = cdtime ();
383 CDTIME_T_TO_TIMESPEC (plugin_get_interval (), &rf->rf_interval);
385 rf->rf_effective_interval = rf->rf_interval;
387 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
388 }
390 /* sleep until this entry is due,
391 * using pthread_cond_timedwait */
392 pthread_mutex_lock (&read_lock);
393 /* In pthread_cond_timedwait, spurious wakeups are possible
394 * (and really happen, at least on NetBSD with > 1 CPU), thus
395 * we need to re-evaluate the condition every time
396 * pthread_cond_timedwait returns. */
397 rc = 0;
398 while ((read_loop != 0)
399 && !timeout_reached(rf->rf_next_read)
400 && rc == 0)
401 {
402 rc = pthread_cond_timedwait (&read_cond, &read_lock,
403 &rf->rf_next_read);
404 }
406 /* Must hold `read_lock' when accessing `rf->rf_type'. */
407 rf_type = rf->rf_type;
408 pthread_mutex_unlock (&read_lock);
410 /* Check if we're supposed to stop.. This may have interrupted
411 * the sleep, too. */
412 if (read_loop == 0)
413 {
414 /* Insert `rf' again, so it can be free'd correctly */
415 c_heap_insert (read_heap, rf);
416 break;
417 }
419 /* The entry has been marked for deletion. The linked list
420 * entry has already been removed by `plugin_unregister_read'.
421 * All we have to do here is free the `read_func_t' and
422 * continue. */
423 if (rf_type == RF_REMOVE)
424 {
425 DEBUG ("plugin_read_thread: Destroying the `%s' "
426 "callback.", rf->rf_name);
427 destroy_callback ((callback_func_t *) rf);
428 rf = NULL;
429 continue;
430 }
432 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
434 old_ctx = plugin_set_ctx (rf->rf_ctx);
436 if (rf_type == RF_SIMPLE)
437 {
438 int (*callback) (void);
440 callback = rf->rf_callback;
441 status = (*callback) ();
442 }
443 else
444 {
445 plugin_read_cb callback;
447 assert (rf_type == RF_COMPLEX);
449 callback = rf->rf_callback;
450 status = (*callback) (&rf->rf_udata);
451 }
453 plugin_set_ctx (old_ctx);
455 /* If the function signals failure, we will increase the
456 * intervals in which it will be called. */
457 if (status != 0)
458 {
459 rf->rf_effective_interval.tv_sec *= 2;
460 rf->rf_effective_interval.tv_nsec *= 2;
461 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
463 if (rf->rf_effective_interval.tv_sec >= 86400)
464 {
465 rf->rf_effective_interval.tv_sec = 86400;
466 rf->rf_effective_interval.tv_nsec = 0;
467 }
469 NOTICE ("read-function of plugin `%s' failed. "
470 "Will suspend it for %i seconds.",
471 rf->rf_name,
472 (int) rf->rf_effective_interval.tv_sec);
473 }
474 else
475 {
476 /* Success: Restore the interval, if it was changed. */
477 rf->rf_effective_interval = rf->rf_interval;
478 }
480 /* update the ``next read due'' field */
481 now = cdtime ();
483 DEBUG ("plugin_read_thread: Effective interval of the "
484 "%s plugin is %i.%09i.",
485 rf->rf_name,
486 (int) rf->rf_effective_interval.tv_sec,
487 (int) rf->rf_effective_interval.tv_nsec);
489 /* Calculate the next (absolute) time at which this function
490 * should be called. */
491 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
492 + rf->rf_effective_interval.tv_sec;
493 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
494 + rf->rf_effective_interval.tv_nsec;
495 NORMALIZE_TIMESPEC (rf->rf_next_read);
497 /* Check, if `rf_next_read' is in the past. */
498 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
499 {
500 /* `rf_next_read' is in the past. Insert `now'
501 * so this value doesn't trail off into the
502 * past too much. */
503 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
504 }
506 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
507 rf->rf_name,
508 (int) rf->rf_next_read.tv_sec,
509 (int) rf->rf_next_read.tv_nsec);
511 /* Re-insert this read function into the heap again. */
512 c_heap_insert (read_heap, rf);
513 } /* while (read_loop) */
515 pthread_exit (NULL);
516 return ((void *) 0);
517 } /* void *plugin_read_thread */
519 static void start_read_threads (int num)
520 {
521 int i;
523 if (read_threads != NULL)
524 return;
526 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
527 if (read_threads == NULL)
528 {
529 ERROR ("plugin: start_read_threads: calloc failed.");
530 return;
531 }
533 read_threads_num = 0;
534 for (i = 0; i < num; i++)
535 {
536 if (pthread_create (read_threads + read_threads_num, NULL,
537 plugin_read_thread, NULL) == 0)
538 {
539 read_threads_num++;
540 }
541 else
542 {
543 ERROR ("plugin: start_read_threads: pthread_create failed.");
544 return;
545 }
546 } /* for (i) */
547 } /* void start_read_threads */
549 static void stop_read_threads (void)
550 {
551 int i;
553 if (read_threads == NULL)
554 return;
556 INFO ("collectd: Stopping %i read threads.", read_threads_num);
558 pthread_mutex_lock (&read_lock);
559 read_loop = 0;
560 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
561 pthread_cond_broadcast (&read_cond);
562 pthread_mutex_unlock (&read_lock);
564 for (i = 0; i < read_threads_num; i++)
565 {
566 if (pthread_join (read_threads[i], NULL) != 0)
567 {
568 ERROR ("plugin: stop_read_threads: pthread_join failed.");
569 }
570 read_threads[i] = (pthread_t) 0;
571 }
572 sfree (read_threads);
573 read_threads_num = 0;
574 } /* void stop_read_threads */
576 /*
577 * Public functions
578 */
579 void plugin_set_dir (const char *dir)
580 {
581 if (plugindir != NULL)
582 free (plugindir);
584 if (dir == NULL)
585 plugindir = NULL;
586 else if ((plugindir = strdup (dir)) == NULL)
587 {
588 char errbuf[1024];
589 ERROR ("strdup failed: %s",
590 sstrerror (errno, errbuf, sizeof (errbuf)));
591 }
592 }
594 #define BUFSIZE 512
595 int plugin_load (const char *type, uint32_t flags)
596 {
597 DIR *dh;
598 const char *dir;
599 char filename[BUFSIZE] = "";
600 char typename[BUFSIZE];
601 int typename_len;
602 int ret;
603 struct stat statbuf;
604 struct dirent *de;
605 int status;
607 DEBUG ("type = %s", type);
609 dir = plugin_get_dir ();
610 ret = 1;
612 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
613 * type when matching the filename */
614 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
615 if ((status < 0) || ((size_t) status >= sizeof (typename)))
616 {
617 WARNING ("snprintf: truncated: `%s.so'", type);
618 return (-1);
619 }
620 typename_len = strlen (typename);
622 if ((dh = opendir (dir)) == NULL)
623 {
624 char errbuf[1024];
625 ERROR ("opendir (%s): %s", dir,
626 sstrerror (errno, errbuf, sizeof (errbuf)));
627 return (-1);
628 }
630 while ((de = readdir (dh)) != NULL)
631 {
632 if (strncasecmp (de->d_name, typename, typename_len))
633 continue;
635 status = ssnprintf (filename, sizeof (filename),
636 "%s/%s", dir, de->d_name);
637 if ((status < 0) || ((size_t) status >= sizeof (filename)))
638 {
639 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
640 continue;
641 }
643 if (lstat (filename, &statbuf) == -1)
644 {
645 char errbuf[1024];
646 WARNING ("stat %s: %s", filename,
647 sstrerror (errno, errbuf, sizeof (errbuf)));
648 continue;
649 }
650 else if (!S_ISREG (statbuf.st_mode))
651 {
652 /* don't follow symlinks */
653 WARNING ("stat %s: not a regular file", filename);
654 continue;
655 }
657 if (plugin_load_file (filename, flags) == 0)
658 {
659 /* success */
660 ret = 0;
661 break;
662 }
663 else
664 {
665 fprintf (stderr, "Unable to load plugin %s.\n", type);
666 }
667 }
669 closedir (dh);
671 if (filename[0] == '\0')
672 fprintf (stderr, "Could not find plugin %s.\n", type);
674 return (ret);
675 }
677 /*
678 * The `register_*' functions follow
679 */
680 int plugin_register_config (const char *name,
681 int (*callback) (const char *key, const char *val),
682 const char **keys, int keys_num)
683 {
684 cf_register (name, callback, keys, keys_num);
685 return (0);
686 } /* int plugin_register_config */
688 int plugin_register_complex_config (const char *type,
689 int (*callback) (oconfig_item_t *))
690 {
691 return (cf_register_complex (type, callback));
692 } /* int plugin_register_complex_config */
694 int plugin_register_init (const char *name,
695 int (*callback) (void))
696 {
697 return (create_register_callback (&list_init, name, (void *) callback,
698 /* user_data = */ NULL));
699 } /* plugin_register_init */
701 static int plugin_compare_read_func (const void *arg0, const void *arg1)
702 {
703 const read_func_t *rf0;
704 const read_func_t *rf1;
706 rf0 = arg0;
707 rf1 = arg1;
709 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
710 return (-1);
711 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
712 return (1);
713 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
714 return (-1);
715 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
716 return (1);
717 else
718 return (0);
719 } /* int plugin_compare_read_func */
721 /* Add a read function to both, the heap and a linked list. The linked list if
722 * used to look-up read functions, especially for the remove function. The heap
723 * is used to determine which plugin to read next. */
724 static int plugin_insert_read (read_func_t *rf)
725 {
726 int status;
727 llentry_t *le;
729 cdtime_t now = cdtime ();
730 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
732 pthread_mutex_lock (&read_lock);
734 if (read_list == NULL)
735 {
736 read_list = llist_create ();
737 if (read_list == NULL)
738 {
739 pthread_mutex_unlock (&read_lock);
740 ERROR ("plugin_insert_read: read_list failed.");
741 return (-1);
742 }
743 }
745 if (read_heap == NULL)
746 {
747 read_heap = c_heap_create (plugin_compare_read_func);
748 if (read_heap == NULL)
749 {
750 pthread_mutex_unlock (&read_lock);
751 ERROR ("plugin_insert_read: c_heap_create failed.");
752 return (-1);
753 }
754 }
756 le = llist_search (read_list, rf->rf_name);
757 if (le != NULL)
758 {
759 pthread_mutex_unlock (&read_lock);
760 WARNING ("The read function \"%s\" is already registered. "
761 "Check for duplicate \"LoadPlugin\" lines "
762 "in your configuration!",
763 rf->rf_name);
764 return (EINVAL);
765 }
767 le = llentry_create (rf->rf_name, rf);
768 if (le == NULL)
769 {
770 pthread_mutex_unlock (&read_lock);
771 ERROR ("plugin_insert_read: llentry_create failed.");
772 return (-1);
773 }
775 status = c_heap_insert (read_heap, rf);
776 if (status != 0)
777 {
778 pthread_mutex_unlock (&read_lock);
779 ERROR ("plugin_insert_read: c_heap_insert failed.");
780 llentry_destroy (le);
781 return (-1);
782 }
784 /* This does not fail. */
785 llist_append (read_list, le);
787 /* Wake up all the read threads. */
788 pthread_cond_broadcast (&read_cond);
789 pthread_mutex_unlock (&read_lock);
790 return (0);
791 } /* int plugin_insert_read */
793 static int read_cb_wrapper (user_data_t *ud)
794 {
795 int (*callback) (void);
797 if (ud == NULL)
798 return -1;
800 callback = ud->data;
801 return callback();
802 } /* int read_cb_wrapper */
804 int plugin_register_read (const char *name,
805 int (*callback) (void))
806 {
807 read_func_t *rf;
808 plugin_ctx_t ctx = plugin_get_ctx ();
809 int status;
811 if (ctx.interval != 0) {
812 /* If ctx.interval is not zero (== use the plugin or global
813 * interval), we need to use the "complex" read callback,
814 * because only that allows to specify a different interval.
815 * Wrap the callback using read_cb_wrapper(). */
816 struct timespec interval;
817 user_data_t user_data;
819 user_data.data = callback;
820 user_data.free_func = NULL;
822 CDTIME_T_TO_TIMESPEC (ctx.interval, &interval);
823 return plugin_register_complex_read (/* group = */ NULL,
824 name, read_cb_wrapper, &interval, &user_data);
825 }
827 DEBUG ("plugin_register_read: default_interval = %.3f",
828 CDTIME_T_TO_DOUBLE(plugin_get_interval ()));
830 rf = malloc (sizeof (*rf));
831 if (rf == NULL)
832 {
833 ERROR ("plugin_register_read: malloc failed.");
834 return (ENOMEM);
835 }
837 memset (rf, 0, sizeof (read_func_t));
838 rf->rf_callback = (void *) callback;
839 rf->rf_udata.data = NULL;
840 rf->rf_udata.free_func = NULL;
841 rf->rf_ctx = ctx;
842 rf->rf_group[0] = '\0';
843 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
844 rf->rf_type = RF_SIMPLE;
845 rf->rf_interval.tv_sec = 0;
846 rf->rf_interval.tv_nsec = 0;
847 rf->rf_effective_interval = rf->rf_interval;
849 status = plugin_insert_read (rf);
850 if (status != 0)
851 sfree (rf);
853 return (status);
854 } /* int plugin_register_read */
856 int plugin_register_complex_read (const char *group, const char *name,
857 plugin_read_cb callback,
858 const struct timespec *interval,
859 user_data_t *user_data)
860 {
861 read_func_t *rf;
862 plugin_ctx_t ctx = plugin_get_ctx ();
863 int status;
865 rf = malloc (sizeof (*rf));
866 if (rf == NULL)
867 {
868 ERROR ("plugin_register_complex_read: malloc failed.");
869 return (ENOMEM);
870 }
872 memset (rf, 0, sizeof (read_func_t));
873 rf->rf_callback = (void *) callback;
874 if (group != NULL)
875 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
876 else
877 rf->rf_group[0] = '\0';
878 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
879 rf->rf_type = RF_COMPLEX;
880 if (interval != NULL)
881 {
882 rf->rf_interval = *interval;
883 }
884 else if (ctx.interval != 0)
885 {
886 CDTIME_T_TO_TIMESPEC (ctx.interval, &rf->rf_interval);
887 }
888 rf->rf_effective_interval = rf->rf_interval;
890 DEBUG ("plugin_register_read: interval = %i.%09i",
891 (int) rf->rf_interval.tv_sec,
892 (int) rf->rf_interval.tv_nsec);
894 /* Set user data */
895 if (user_data == NULL)
896 {
897 rf->rf_udata.data = NULL;
898 rf->rf_udata.free_func = NULL;
899 }
900 else
901 {
902 rf->rf_udata = *user_data;
903 }
905 rf->rf_ctx = ctx;
907 status = plugin_insert_read (rf);
908 if (status != 0)
909 sfree (rf);
911 return (status);
912 } /* int plugin_register_complex_read */
914 int plugin_register_write (const char *name,
915 plugin_write_cb callback, user_data_t *ud)
916 {
917 return (create_register_callback (&list_write, name,
918 (void *) callback, ud));
919 } /* int plugin_register_write */
921 int plugin_register_flush (const char *name,
922 plugin_flush_cb callback, user_data_t *ud)
923 {
924 return (create_register_callback (&list_flush, name,
925 (void *) callback, ud));
926 } /* int plugin_register_flush */
928 int plugin_register_missing (const char *name,
929 plugin_missing_cb callback, user_data_t *ud)
930 {
931 return (create_register_callback (&list_missing, name,
932 (void *) callback, ud));
933 } /* int plugin_register_missing */
935 int plugin_register_shutdown (const char *name,
936 int (*callback) (void))
937 {
938 return (create_register_callback (&list_shutdown, name,
939 (void *) callback, /* user_data = */ NULL));
940 } /* int plugin_register_shutdown */
942 int plugin_register_data_set (const data_set_t *ds)
943 {
944 data_set_t *ds_copy;
945 int i;
947 if ((data_sets != NULL)
948 && (c_avl_get (data_sets, ds->type, NULL) == 0))
949 {
950 NOTICE ("Replacing DS `%s' with another version.", ds->type);
951 plugin_unregister_data_set (ds->type);
952 }
953 else if (data_sets == NULL)
954 {
955 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
956 if (data_sets == NULL)
957 return (-1);
958 }
960 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
961 if (ds_copy == NULL)
962 return (-1);
963 memcpy(ds_copy, ds, sizeof (data_set_t));
965 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
966 * ds->ds_num);
967 if (ds_copy->ds == NULL)
968 {
969 free (ds_copy);
970 return (-1);
971 }
973 for (i = 0; i < ds->ds_num; i++)
974 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
976 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
977 } /* int plugin_register_data_set */
979 int plugin_register_log (const char *name,
980 plugin_log_cb callback, user_data_t *ud)
981 {
982 return (create_register_callback (&list_log, name,
983 (void *) callback, ud));
984 } /* int plugin_register_log */
986 int plugin_register_notification (const char *name,
987 plugin_notification_cb callback, user_data_t *ud)
988 {
989 return (create_register_callback (&list_notification, name,
990 (void *) callback, ud));
991 } /* int plugin_register_log */
993 int plugin_unregister_config (const char *name)
994 {
995 cf_unregister (name);
996 return (0);
997 } /* int plugin_unregister_config */
999 int plugin_unregister_complex_config (const char *name)
1000 {
1001 cf_unregister_complex (name);
1002 return (0);
1003 } /* int plugin_unregister_complex_config */
1005 int plugin_unregister_init (const char *name)
1006 {
1007 return (plugin_unregister (list_init, name));
1008 }
1010 int plugin_unregister_read (const char *name) /* {{{ */
1011 {
1012 llentry_t *le;
1013 read_func_t *rf;
1015 if (name == NULL)
1016 return (-ENOENT);
1018 pthread_mutex_lock (&read_lock);
1020 if (read_list == NULL)
1021 {
1022 pthread_mutex_unlock (&read_lock);
1023 return (-ENOENT);
1024 }
1026 le = llist_search (read_list, name);
1027 if (le == NULL)
1028 {
1029 pthread_mutex_unlock (&read_lock);
1030 WARNING ("plugin_unregister_read: No such read function: %s",
1031 name);
1032 return (-ENOENT);
1033 }
1035 llist_remove (read_list, le);
1037 rf = le->value;
1038 assert (rf != NULL);
1039 rf->rf_type = RF_REMOVE;
1041 pthread_mutex_unlock (&read_lock);
1043 llentry_destroy (le);
1045 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1047 return (0);
1048 } /* }}} int plugin_unregister_read */
1050 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1051 {
1052 read_func_t *rf = e->value;
1053 char *group = ud;
1055 return strcmp (rf->rf_group, (const char *)group);
1056 } /* }}} int compare_read_func_group */
1058 int plugin_unregister_read_group (const char *group) /* {{{ */
1059 {
1060 llentry_t *le;
1061 read_func_t *rf;
1063 int found = 0;
1065 if (group == NULL)
1066 return (-ENOENT);
1068 pthread_mutex_lock (&read_lock);
1070 if (read_list == NULL)
1071 {
1072 pthread_mutex_unlock (&read_lock);
1073 return (-ENOENT);
1074 }
1076 while (42)
1077 {
1078 le = llist_search_custom (read_list,
1079 compare_read_func_group, (void *)group);
1081 if (le == NULL)
1082 break;
1084 ++found;
1086 llist_remove (read_list, le);
1088 rf = le->value;
1089 assert (rf != NULL);
1090 rf->rf_type = RF_REMOVE;
1092 llentry_destroy (le);
1094 DEBUG ("plugin_unregister_read_group: "
1095 "Marked `%s' (group `%s') for removal.",
1096 rf->rf_name, group);
1097 }
1099 pthread_mutex_unlock (&read_lock);
1101 if (found == 0)
1102 {
1103 WARNING ("plugin_unregister_read_group: No such "
1104 "group of read function: %s", group);
1105 return (-ENOENT);
1106 }
1108 return (0);
1109 } /* }}} int plugin_unregister_read_group */
1111 int plugin_unregister_write (const char *name)
1112 {
1113 return (plugin_unregister (list_write, name));
1114 }
1116 int plugin_unregister_flush (const char *name)
1117 {
1118 return (plugin_unregister (list_flush, name));
1119 }
1121 int plugin_unregister_missing (const char *name)
1122 {
1123 return (plugin_unregister (list_missing, name));
1124 }
1126 int plugin_unregister_shutdown (const char *name)
1127 {
1128 return (plugin_unregister (list_shutdown, name));
1129 }
1131 int plugin_unregister_data_set (const char *name)
1132 {
1133 data_set_t *ds;
1135 if (data_sets == NULL)
1136 return (-1);
1138 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1139 return (-1);
1141 sfree (ds->ds);
1142 sfree (ds);
1144 return (0);
1145 } /* int plugin_unregister_data_set */
1147 int plugin_unregister_log (const char *name)
1148 {
1149 return (plugin_unregister (list_log, name));
1150 }
1152 int plugin_unregister_notification (const char *name)
1153 {
1154 return (plugin_unregister (list_notification, name));
1155 }
1157 void plugin_init_all (void)
1158 {
1159 const char *chain_name;
1160 llentry_t *le;
1161 int status;
1163 /* Init the value cache */
1164 uc_init ();
1166 chain_name = global_option_get ("PreCacheChain");
1167 pre_cache_chain = fc_chain_get_by_name (chain_name);
1169 chain_name = global_option_get ("PostCacheChain");
1170 post_cache_chain = fc_chain_get_by_name (chain_name);
1173 if ((list_init == NULL) && (read_heap == NULL))
1174 return;
1176 /* Calling all init callbacks before checking if read callbacks
1177 * are available allows the init callbacks to register the read
1178 * callback. */
1179 le = llist_head (list_init);
1180 while (le != NULL)
1181 {
1182 callback_func_t *cf;
1183 plugin_init_cb callback;
1184 plugin_ctx_t old_ctx;
1186 cf = le->value;
1187 old_ctx = plugin_set_ctx (cf->cf_ctx);
1188 callback = cf->cf_callback;
1189 status = (*callback) ();
1190 plugin_set_ctx (old_ctx);
1192 if (status != 0)
1193 {
1194 ERROR ("Initialization of plugin `%s' "
1195 "failed with status %i. "
1196 "Plugin will be unloaded.",
1197 le->key, status);
1198 /* Plugins that register read callbacks from the init
1199 * callback should take care of appropriate error
1200 * handling themselves. */
1201 /* FIXME: Unload _all_ functions */
1202 plugin_unregister_read (le->key);
1203 }
1205 le = le->next;
1206 }
1208 /* Start read-threads */
1209 if (read_heap != NULL)
1210 {
1211 const char *rt;
1212 int num;
1213 rt = global_option_get ("ReadThreads");
1214 num = atoi (rt);
1215 if (num != -1)
1216 start_read_threads ((num > 0) ? num : 5);
1217 }
1218 } /* void plugin_init_all */
1220 /* TODO: Rename this function. */
1221 void plugin_read_all (void)
1222 {
1223 uc_check_timeout ();
1225 return;
1226 } /* void plugin_read_all */
1228 /* Read function called when the `-T' command line argument is given. */
1229 int plugin_read_all_once (void)
1230 {
1231 int status;
1232 int return_status = 0;
1234 if (read_heap == NULL)
1235 {
1236 NOTICE ("No read-functions are registered.");
1237 return (0);
1238 }
1240 while (42)
1241 {
1242 read_func_t *rf;
1243 plugin_ctx_t old_ctx;
1245 rf = c_heap_get_root (read_heap);
1246 if (rf == NULL)
1247 break;
1249 old_ctx = plugin_set_ctx (rf->rf_ctx);
1251 if (rf->rf_type == RF_SIMPLE)
1252 {
1253 int (*callback) (void);
1255 callback = rf->rf_callback;
1256 status = (*callback) ();
1257 }
1258 else
1259 {
1260 plugin_read_cb callback;
1262 callback = rf->rf_callback;
1263 status = (*callback) (&rf->rf_udata);
1264 }
1266 plugin_set_ctx (old_ctx);
1268 if (status != 0)
1269 {
1270 NOTICE ("read-function of plugin `%s' failed.",
1271 rf->rf_name);
1272 return_status = -1;
1273 }
1275 destroy_callback ((void *) rf);
1276 }
1278 return (return_status);
1279 } /* int plugin_read_all_once */
1281 int plugin_write (const char *plugin, /* {{{ */
1282 const data_set_t *ds, const value_list_t *vl)
1283 {
1284 llentry_t *le;
1285 int status;
1287 if (vl == NULL)
1288 return (EINVAL);
1290 if (list_write == NULL)
1291 return (ENOENT);
1293 if (ds == NULL)
1294 {
1295 ds = plugin_get_ds (vl->type);
1296 if (ds == NULL)
1297 {
1298 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1299 return (ENOENT);
1300 }
1301 }
1303 if (plugin == NULL)
1304 {
1305 int success = 0;
1306 int failure = 0;
1308 le = llist_head (list_write);
1309 while (le != NULL)
1310 {
1311 callback_func_t *cf = le->value;
1312 plugin_write_cb callback;
1314 /* do not switch plugin context; rather keep the context (interval)
1315 * information of the calling read plugin */
1317 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1318 callback = cf->cf_callback;
1319 status = (*callback) (ds, vl, &cf->cf_udata);
1320 if (status != 0)
1321 failure++;
1322 else
1323 success++;
1325 le = le->next;
1326 }
1328 if ((success == 0) && (failure != 0))
1329 status = -1;
1330 else
1331 status = 0;
1332 }
1333 else /* plugin != NULL */
1334 {
1335 callback_func_t *cf;
1336 plugin_write_cb callback;
1338 le = llist_head (list_write);
1339 while (le != NULL)
1340 {
1341 if (strcasecmp (plugin, le->key) == 0)
1342 break;
1344 le = le->next;
1345 }
1347 if (le == NULL)
1348 return (ENOENT);
1350 cf = le->value;
1352 /* do not switch plugin context; rather keep the context (interval)
1353 * information of the calling read plugin */
1355 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1356 callback = cf->cf_callback;
1357 status = (*callback) (ds, vl, &cf->cf_udata);
1358 }
1360 return (status);
1361 } /* }}} int plugin_write */
1363 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1364 {
1365 llentry_t *le;
1367 if (list_flush == NULL)
1368 return (0);
1370 le = llist_head (list_flush);
1371 while (le != NULL)
1372 {
1373 callback_func_t *cf;
1374 plugin_flush_cb callback;
1375 plugin_ctx_t old_ctx;
1377 if ((plugin != NULL)
1378 && (strcmp (plugin, le->key) != 0))
1379 {
1380 le = le->next;
1381 continue;
1382 }
1384 cf = le->value;
1385 old_ctx = plugin_set_ctx (cf->cf_ctx);
1386 callback = cf->cf_callback;
1388 (*callback) (timeout, identifier, &cf->cf_udata);
1390 plugin_set_ctx (old_ctx);
1392 le = le->next;
1393 }
1394 return (0);
1395 } /* int plugin_flush */
1397 void plugin_shutdown_all (void)
1398 {
1399 llentry_t *le;
1401 stop_read_threads ();
1403 destroy_all_callbacks (&list_init);
1405 pthread_mutex_lock (&read_lock);
1406 llist_destroy (read_list);
1407 read_list = NULL;
1408 pthread_mutex_unlock (&read_lock);
1410 destroy_read_heap ();
1412 plugin_flush (/* plugin = */ NULL,
1413 /* timeout = */ 0,
1414 /* identifier = */ NULL);
1416 le = NULL;
1417 if (list_shutdown != NULL)
1418 le = llist_head (list_shutdown);
1420 while (le != NULL)
1421 {
1422 callback_func_t *cf;
1423 plugin_shutdown_cb callback;
1424 plugin_ctx_t old_ctx;
1426 cf = le->value;
1427 old_ctx = plugin_set_ctx (cf->cf_ctx);
1428 callback = cf->cf_callback;
1430 /* Advance the pointer before calling the callback allows
1431 * shutdown functions to unregister themselves. If done the
1432 * other way around the memory `le' points to will be freed
1433 * after callback returns. */
1434 le = le->next;
1436 (*callback) ();
1438 plugin_set_ctx (old_ctx);
1439 }
1441 /* Write plugins which use the `user_data' pointer usually need the
1442 * same data available to the flush callback. If this is the case, set
1443 * the free_function to NULL when registering the flush callback and to
1444 * the real free function when registering the write callback. This way
1445 * the data isn't freed twice. */
1446 destroy_all_callbacks (&list_flush);
1447 destroy_all_callbacks (&list_missing);
1448 destroy_all_callbacks (&list_write);
1450 destroy_all_callbacks (&list_notification);
1451 destroy_all_callbacks (&list_shutdown);
1452 destroy_all_callbacks (&list_log);
1453 } /* void plugin_shutdown_all */
1455 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1456 {
1457 llentry_t *le;
1459 if (list_missing == NULL)
1460 return (0);
1462 le = llist_head (list_missing);
1463 while (le != NULL)
1464 {
1465 callback_func_t *cf;
1466 plugin_missing_cb callback;
1467 plugin_ctx_t old_ctx;
1468 int status;
1470 cf = le->value;
1471 old_ctx = plugin_set_ctx (cf->cf_ctx);
1472 callback = cf->cf_callback;
1474 status = (*callback) (vl, &cf->cf_udata);
1475 plugin_set_ctx (old_ctx);
1476 if (status != 0)
1477 {
1478 if (status < 0)
1479 {
1480 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1481 "failed with status %i.",
1482 le->key, status);
1483 return (status);
1484 }
1485 else
1486 {
1487 return (0);
1488 }
1489 }
1491 le = le->next;
1492 }
1493 return (0);
1494 } /* int }}} plugin_dispatch_missing */
1496 int plugin_dispatch_values (value_list_t *vl)
1497 {
1498 int status;
1499 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1501 value_t *saved_values;
1502 int saved_values_len;
1504 data_set_t *ds;
1506 int free_meta_data = 0;
1508 if ((vl == NULL) || (vl->type[0] == 0)
1509 || (vl->values == NULL) || (vl->values_len < 1))
1510 {
1511 ERROR ("plugin_dispatch_values: Invalid value list "
1512 "from plugin %s.", vl->plugin);
1513 return (-1);
1514 }
1516 /* Free meta data only if the calling function didn't specify any. In
1517 * this case matches and targets may add some and the calling function
1518 * may not expect (and therefore free) that data. */
1519 if (vl->meta == NULL)
1520 free_meta_data = 1;
1522 if (list_write == NULL)
1523 c_complain_once (LOG_WARNING, &no_write_complaint,
1524 "plugin_dispatch_values: No write callback has been "
1525 "registered. Please load at least one output plugin, "
1526 "if you want the collected data to be stored.");
1528 if (data_sets == NULL)
1529 {
1530 ERROR ("plugin_dispatch_values: No data sets registered. "
1531 "Could the types database be read? Check "
1532 "your `TypesDB' setting!");
1533 return (-1);
1534 }
1536 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1537 {
1538 char ident[6 * DATA_MAX_NAME_LEN];
1540 FORMAT_VL (ident, sizeof (ident), vl);
1541 INFO ("plugin_dispatch_values: Dataset not found: %s "
1542 "(from \"%s\"), check your types.db!",
1543 vl->type, ident);
1544 return (-1);
1545 }
1547 if (vl->time == 0)
1548 vl->time = cdtime ();
1550 if (vl->interval <= 0)
1551 {
1552 plugin_ctx_t ctx = plugin_get_ctx ();
1554 if (ctx.interval != 0)
1555 vl->interval = ctx.interval;
1556 else
1557 {
1558 char name[6 * DATA_MAX_NAME_LEN];
1559 FORMAT_VL (name, sizeof (name), vl);
1560 ERROR ("plugin_dispatch_values: Unable to determine "
1561 "interval from context for "
1562 "value list \"%s\". "
1563 "This indicates a broken plugin. "
1564 "Please report this problem to the "
1565 "collectd mailing list or at "
1566 "<http://collectd.org/bugs/>.", name);
1567 vl->interval = cf_get_default_interval ();
1568 }
1569 }
1571 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1572 "host = %s; "
1573 "plugin = %s; plugin_instance = %s; "
1574 "type = %s; type_instance = %s;",
1575 CDTIME_T_TO_DOUBLE (vl->time),
1576 CDTIME_T_TO_DOUBLE (vl->interval),
1577 vl->host,
1578 vl->plugin, vl->plugin_instance,
1579 vl->type, vl->type_instance);
1581 #if COLLECT_DEBUG
1582 assert (0 == strcmp (ds->type, vl->type));
1583 #else
1584 if (0 != strcmp (ds->type, vl->type))
1585 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1586 ds->type, vl->type);
1587 #endif
1589 #if COLLECT_DEBUG
1590 assert (ds->ds_num == vl->values_len);
1591 #else
1592 if (ds->ds_num != vl->values_len)
1593 {
1594 ERROR ("plugin_dispatch_values: ds->type = %s: "
1595 "(ds->ds_num = %i) != "
1596 "(vl->values_len = %i)",
1597 ds->type, ds->ds_num, vl->values_len);
1598 return (-1);
1599 }
1600 #endif
1602 escape_slashes (vl->host, sizeof (vl->host));
1603 escape_slashes (vl->plugin, sizeof (vl->plugin));
1604 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1605 escape_slashes (vl->type, sizeof (vl->type));
1606 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1608 /* Copy the values. This way, we can assure `targets' that they get
1609 * dynamically allocated values, which they can free and replace if
1610 * they like. */
1611 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1612 {
1613 saved_values = vl->values;
1614 saved_values_len = vl->values_len;
1616 vl->values = (value_t *) calloc (vl->values_len,
1617 sizeof (*vl->values));
1618 if (vl->values == NULL)
1619 {
1620 ERROR ("plugin_dispatch_values: calloc failed.");
1621 vl->values = saved_values;
1622 return (-1);
1623 }
1624 memcpy (vl->values, saved_values,
1625 vl->values_len * sizeof (*vl->values));
1626 }
1627 else /* if ((pre == NULL) && (post == NULL)) */
1628 {
1629 saved_values = NULL;
1630 saved_values_len = 0;
1631 }
1633 if (pre_cache_chain != NULL)
1634 {
1635 status = fc_process_chain (ds, vl, pre_cache_chain);
1636 if (status < 0)
1637 {
1638 WARNING ("plugin_dispatch_values: Running the "
1639 "pre-cache chain failed with "
1640 "status %i (%#x).",
1641 status, status);
1642 }
1643 else if (status == FC_TARGET_STOP)
1644 {
1645 /* Restore the state of the value_list so that plugins
1646 * don't get confused.. */
1647 if (saved_values != NULL)
1648 {
1649 free (vl->values);
1650 vl->values = saved_values;
1651 vl->values_len = saved_values_len;
1652 }
1653 return (0);
1654 }
1655 }
1657 /* Update the value cache */
1658 uc_update (ds, vl);
1660 if (post_cache_chain != NULL)
1661 {
1662 status = fc_process_chain (ds, vl, post_cache_chain);
1663 if (status < 0)
1664 {
1665 WARNING ("plugin_dispatch_values: Running the "
1666 "post-cache chain failed with "
1667 "status %i (%#x).",
1668 status, status);
1669 }
1670 }
1671 else
1672 fc_default_action (ds, vl);
1674 /* Restore the state of the value_list so that plugins don't get
1675 * confused.. */
1676 if (saved_values != NULL)
1677 {
1678 free (vl->values);
1679 vl->values = saved_values;
1680 vl->values_len = saved_values_len;
1681 }
1683 if ((free_meta_data != 0) && (vl->meta != NULL))
1684 {
1685 meta_data_destroy (vl->meta);
1686 vl->meta = NULL;
1687 }
1689 return (0);
1690 } /* int plugin_dispatch_values */
1692 int plugin_dispatch_values_secure (const value_list_t *vl)
1693 {
1694 value_list_t vl_copy;
1695 int status;
1697 if (vl == NULL)
1698 return EINVAL;
1700 memcpy (&vl_copy, vl, sizeof (vl_copy));
1702 /* Write callbacks must not change the values and meta pointers, so we can
1703 * savely skip copying those and make this more efficient. */
1704 if ((pre_cache_chain == NULL) && (post_cache_chain == NULL))
1705 return (plugin_dispatch_values (&vl_copy));
1707 /* Set pointers to NULL, just to be on the save side. */
1708 vl_copy.values = NULL;
1709 vl_copy.meta = NULL;
1711 vl_copy.values = malloc (sizeof (*vl_copy.values) * vl->values_len);
1712 if (vl_copy.values == NULL)
1713 {
1714 ERROR ("plugin_dispatch_values_secure: malloc failed.");
1715 return (ENOMEM);
1716 }
1717 memcpy (vl_copy.values, vl->values, sizeof (*vl_copy.values) * vl->values_len);
1719 if (vl->meta != NULL)
1720 {
1721 vl_copy.meta = meta_data_clone (vl->meta);
1722 if (vl_copy.meta == NULL)
1723 {
1724 ERROR ("plugin_dispatch_values_secure: meta_data_clone failed.");
1725 free (vl_copy.values);
1726 return (ENOMEM);
1727 }
1728 } /* if (vl->meta) */
1730 status = plugin_dispatch_values (&vl_copy);
1732 meta_data_destroy (vl_copy.meta);
1733 free (vl_copy.values);
1735 return (status);
1736 } /* int plugin_dispatch_values_secure */
1738 int plugin_dispatch_notification (const notification_t *notif)
1739 {
1740 llentry_t *le;
1741 /* Possible TODO: Add flap detection here */
1743 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1744 "time = %.3f; host = %s;",
1745 notif->severity, notif->message,
1746 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1748 /* Nobody cares for notifications */
1749 if (list_notification == NULL)
1750 return (-1);
1752 le = llist_head (list_notification);
1753 while (le != NULL)
1754 {
1755 callback_func_t *cf;
1756 plugin_notification_cb callback;
1757 int status;
1759 /* do not switch plugin context; rather keep the context
1760 * (interval) information of the calling plugin */
1762 cf = le->value;
1763 callback = cf->cf_callback;
1764 status = (*callback) (notif, &cf->cf_udata);
1765 if (status != 0)
1766 {
1767 WARNING ("plugin_dispatch_notification: Notification "
1768 "callback %s returned %i.",
1769 le->key, status);
1770 }
1772 le = le->next;
1773 }
1775 return (0);
1776 } /* int plugin_dispatch_notification */
1778 void plugin_log (int level, const char *format, ...)
1779 {
1780 char msg[1024];
1781 va_list ap;
1782 llentry_t *le;
1784 #if !COLLECT_DEBUG
1785 if (level >= LOG_DEBUG)
1786 return;
1787 #endif
1789 va_start (ap, format);
1790 vsnprintf (msg, sizeof (msg), format, ap);
1791 msg[sizeof (msg) - 1] = '\0';
1792 va_end (ap);
1794 if (list_log == NULL)
1795 {
1796 fprintf (stderr, "%s\n", msg);
1797 return;
1798 }
1800 le = llist_head (list_log);
1801 while (le != NULL)
1802 {
1803 callback_func_t *cf;
1804 plugin_log_cb callback;
1806 cf = le->value;
1807 callback = cf->cf_callback;
1809 /* do not switch plugin context; rather keep the context
1810 * (interval) information of the calling plugin */
1812 (*callback) (level, msg, &cf->cf_udata);
1814 le = le->next;
1815 }
1816 } /* void plugin_log */
1818 int parse_log_severity (const char *severity)
1819 {
1820 int log_level = -1;
1822 if ((0 == strcasecmp (severity, "emerg"))
1823 || (0 == strcasecmp (severity, "alert"))
1824 || (0 == strcasecmp (severity, "crit"))
1825 || (0 == strcasecmp (severity, "err")))
1826 log_level = LOG_ERR;
1827 else if (0 == strcasecmp (severity, "warning"))
1828 log_level = LOG_WARNING;
1829 else if (0 == strcasecmp (severity, "notice"))
1830 log_level = LOG_NOTICE;
1831 else if (0 == strcasecmp (severity, "info"))
1832 log_level = LOG_INFO;
1833 #if COLLECT_DEBUG
1834 else if (0 == strcasecmp (severity, "debug"))
1835 log_level = LOG_DEBUG;
1836 #endif /* COLLECT_DEBUG */
1838 return (log_level);
1839 } /* int parse_log_severity */
1841 int parse_notif_severity (const char *severity)
1842 {
1843 int notif_severity = -1;
1845 if (strcasecmp (severity, "FAILURE") == 0)
1846 notif_severity = NOTIF_FAILURE;
1847 else if (strcmp (severity, "OKAY") == 0)
1848 notif_severity = NOTIF_OKAY;
1849 else if ((strcmp (severity, "WARNING") == 0)
1850 || (strcmp (severity, "WARN") == 0))
1851 notif_severity = NOTIF_WARNING;
1853 return (notif_severity);
1854 } /* int parse_notif_severity */
1856 const data_set_t *plugin_get_ds (const char *name)
1857 {
1858 data_set_t *ds;
1860 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1861 {
1862 DEBUG ("No such dataset registered: %s", name);
1863 return (NULL);
1864 }
1866 return (ds);
1867 } /* data_set_t *plugin_get_ds */
1869 static int plugin_notification_meta_add (notification_t *n,
1870 const char *name,
1871 enum notification_meta_type_e type,
1872 const void *value)
1873 {
1874 notification_meta_t *meta;
1875 notification_meta_t *tail;
1877 if ((n == NULL) || (name == NULL) || (value == NULL))
1878 {
1879 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1880 return (-1);
1881 }
1883 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1884 if (meta == NULL)
1885 {
1886 ERROR ("plugin_notification_meta_add: malloc failed.");
1887 return (-1);
1888 }
1889 memset (meta, 0, sizeof (notification_meta_t));
1891 sstrncpy (meta->name, name, sizeof (meta->name));
1892 meta->type = type;
1894 switch (type)
1895 {
1896 case NM_TYPE_STRING:
1897 {
1898 meta->nm_value.nm_string = strdup ((const char *) value);
1899 if (meta->nm_value.nm_string == NULL)
1900 {
1901 ERROR ("plugin_notification_meta_add: strdup failed.");
1902 sfree (meta);
1903 return (-1);
1904 }
1905 break;
1906 }
1907 case NM_TYPE_SIGNED_INT:
1908 {
1909 meta->nm_value.nm_signed_int = *((int64_t *) value);
1910 break;
1911 }
1912 case NM_TYPE_UNSIGNED_INT:
1913 {
1914 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1915 break;
1916 }
1917 case NM_TYPE_DOUBLE:
1918 {
1919 meta->nm_value.nm_double = *((double *) value);
1920 break;
1921 }
1922 case NM_TYPE_BOOLEAN:
1923 {
1924 meta->nm_value.nm_boolean = *((_Bool *) value);
1925 break;
1926 }
1927 default:
1928 {
1929 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1930 sfree (meta);
1931 return (-1);
1932 }
1933 } /* switch (type) */
1935 meta->next = NULL;
1936 tail = n->meta;
1937 while ((tail != NULL) && (tail->next != NULL))
1938 tail = tail->next;
1940 if (tail == NULL)
1941 n->meta = meta;
1942 else
1943 tail->next = meta;
1945 return (0);
1946 } /* int plugin_notification_meta_add */
1948 int plugin_notification_meta_add_string (notification_t *n,
1949 const char *name,
1950 const char *value)
1951 {
1952 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1953 }
1955 int plugin_notification_meta_add_signed_int (notification_t *n,
1956 const char *name,
1957 int64_t value)
1958 {
1959 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1960 }
1962 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1963 const char *name,
1964 uint64_t value)
1965 {
1966 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1967 }
1969 int plugin_notification_meta_add_double (notification_t *n,
1970 const char *name,
1971 double value)
1972 {
1973 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1974 }
1976 int plugin_notification_meta_add_boolean (notification_t *n,
1977 const char *name,
1978 _Bool value)
1979 {
1980 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1981 }
1983 int plugin_notification_meta_copy (notification_t *dst,
1984 const notification_t *src)
1985 {
1986 notification_meta_t *meta;
1988 assert (dst != NULL);
1989 assert (src != NULL);
1990 assert (dst != src);
1991 assert ((src->meta == NULL) || (src->meta != dst->meta));
1993 for (meta = src->meta; meta != NULL; meta = meta->next)
1994 {
1995 if (meta->type == NM_TYPE_STRING)
1996 plugin_notification_meta_add_string (dst, meta->name,
1997 meta->nm_value.nm_string);
1998 else if (meta->type == NM_TYPE_SIGNED_INT)
1999 plugin_notification_meta_add_signed_int (dst, meta->name,
2000 meta->nm_value.nm_signed_int);
2001 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2002 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2003 meta->nm_value.nm_unsigned_int);
2004 else if (meta->type == NM_TYPE_DOUBLE)
2005 plugin_notification_meta_add_double (dst, meta->name,
2006 meta->nm_value.nm_double);
2007 else if (meta->type == NM_TYPE_BOOLEAN)
2008 plugin_notification_meta_add_boolean (dst, meta->name,
2009 meta->nm_value.nm_boolean);
2010 }
2012 return (0);
2013 } /* int plugin_notification_meta_copy */
2015 int plugin_notification_meta_free (notification_meta_t *n)
2016 {
2017 notification_meta_t *this;
2018 notification_meta_t *next;
2020 if (n == NULL)
2021 {
2022 ERROR ("plugin_notification_meta_free: n == NULL!");
2023 return (-1);
2024 }
2026 this = n;
2027 while (this != NULL)
2028 {
2029 next = this->next;
2031 if (this->type == NM_TYPE_STRING)
2032 {
2033 free ((char *)this->nm_value.nm_string);
2034 this->nm_value.nm_string = NULL;
2035 }
2036 sfree (this);
2038 this = next;
2039 }
2041 return (0);
2042 } /* int plugin_notification_meta_free */
2044 static void plugin_ctx_destructor (void *ctx)
2045 {
2046 sfree (ctx);
2047 } /* void plugin_ctx_destructor */
2049 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2051 static plugin_ctx_t *plugin_ctx_create (void)
2052 {
2053 plugin_ctx_t *ctx;
2055 ctx = malloc (sizeof (*ctx));
2056 if (ctx == NULL) {
2057 char errbuf[1024];
2058 ERROR ("Failed to allocate plugin context: %s",
2059 sstrerror (errno, errbuf, sizeof (errbuf)));
2060 return NULL;
2061 }
2063 *ctx = ctx_init;
2064 assert (plugin_ctx_key_initialized);
2065 pthread_setspecific (plugin_ctx_key, ctx);
2066 DEBUG("Created new plugin context.");
2067 return (ctx);
2068 } /* int plugin_ctx_create */
2070 void plugin_init_ctx (void)
2071 {
2072 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2073 plugin_ctx_key_initialized = 1;
2074 } /* void plugin_init_ctx */
2076 plugin_ctx_t plugin_get_ctx (void)
2077 {
2078 plugin_ctx_t *ctx;
2080 assert (plugin_ctx_key_initialized);
2081 ctx = pthread_getspecific (plugin_ctx_key);
2083 if (ctx == NULL) {
2084 ctx = plugin_ctx_create ();
2085 /* this must no happen -- exit() instead? */
2086 if (ctx == NULL)
2087 return ctx_init;
2088 }
2090 return (*ctx);
2091 } /* plugin_ctx_t plugin_get_ctx */
2093 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2094 {
2095 plugin_ctx_t *c;
2096 plugin_ctx_t old;
2098 assert (plugin_ctx_key_initialized);
2099 c = pthread_getspecific (plugin_ctx_key);
2101 if (c == NULL) {
2102 c = plugin_ctx_create ();
2103 /* this must no happen -- exit() instead? */
2104 if (c == NULL)
2105 return ctx_init;
2106 }
2108 old = *c;
2109 *c = ctx;
2111 return (old);
2112 } /* void plugin_set_ctx */
2114 cdtime_t plugin_get_interval (void)
2115 {
2116 cdtime_t interval;
2118 interval = plugin_get_ctx().interval;
2119 if (interval > 0)
2120 return interval;
2122 return cf_get_default_interval ();
2123 } /* cdtime_t plugin_get_interval */
2125 typedef struct {
2126 plugin_ctx_t ctx;
2127 void *(*start_routine) (void *);
2128 void *arg;
2129 } plugin_thread_t;
2131 static void *plugin_thread_start (void *arg)
2132 {
2133 plugin_thread_t *plugin_thread = arg;
2135 void *(*start_routine) (void *) = plugin_thread->start_routine;
2136 void *plugin_arg = plugin_thread->arg;
2138 plugin_set_ctx (plugin_thread->ctx);
2140 free (plugin_thread);
2142 return start_routine (plugin_arg);
2143 } /* void *plugin_thread_start */
2145 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2146 void *(*start_routine) (void *), void *arg)
2147 {
2148 plugin_thread_t *plugin_thread;
2150 plugin_thread = malloc (sizeof (*plugin_thread));
2151 if (plugin_thread == NULL)
2152 return -1;
2154 plugin_thread->ctx = plugin_get_ctx ();
2155 plugin_thread->start_routine = start_routine;
2156 plugin_thread->arg = arg;
2158 return pthread_create (thread, attr,
2159 plugin_thread_start, plugin_thread);
2160 } /* int plugin_thread_create */
2162 /* vim: set sw=8 ts=8 noet fdm=marker : */