2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2009 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
20 * Sebastian Harl <sh at tokkee.org>
24 #include "utils_complain.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 "utils_threshold.h"
40 #include "filter_chain.h"
45 struct callback_func_s
50 typedef struct callback_func_s callback_func_t;
54 #define RF_REMOVE 65535
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 callback_func_t rf_super;
62 char rf_name[DATA_MAX_NAME_LEN];
64 struct timespec rf_interval;
65 struct timespec rf_effective_interval;
66 struct timespec rf_next_read;
68 typedef struct read_func_s read_func_t;
73 static llist_t *list_init;
74 static llist_t *list_write;
75 static llist_t *list_flush;
76 static llist_t *list_shutdown;
77 static llist_t *list_log;
78 static llist_t *list_notification;
80 static fc_chain_t *pre_cache_chain = NULL;
81 static fc_chain_t *post_cache_chain = NULL;
83 static c_avl_tree_t *data_sets;
85 static char *plugindir = NULL;
87 static c_heap_t *read_heap = NULL;
88 static llist_t *read_list;
89 static int read_loop = 1;
90 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
91 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
92 static pthread_t *read_threads = NULL;
93 static int read_threads_num = 0;
98 static const char *plugin_get_dir (void)
100 if (plugindir == NULL)
106 static void destroy_callback (callback_func_t *cf) /* {{{ */
111 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
113 cf->cf_udata.free_func (cf->cf_udata.data);
114 cf->cf_udata.data = NULL;
115 cf->cf_udata.free_func = NULL;
118 } /* }}} void destroy_callback */
120 static void destroy_all_callbacks (llist_t **list) /* {{{ */
127 le = llist_head (*list);
135 destroy_callback (le->value);
141 llist_destroy (*list);
143 } /* }}} void destroy_all_callbacks */
145 static void destroy_read_heap (void) /* {{{ */
147 if (read_heap == NULL)
154 cf = c_heap_get_root (read_heap);
158 destroy_callback (cf);
161 c_heap_destroy (read_heap);
163 } /* }}} void destroy_read_heap */
165 static int register_callback (llist_t **list, /* {{{ */
166 const char *name, callback_func_t *cf)
173 *list = llist_create ();
176 ERROR ("plugin: register_callback: "
177 "llist_create failed.");
178 destroy_callback (cf);
186 ERROR ("plugin: register_callback: strdup failed.");
187 destroy_callback (cf);
191 le = llist_search (*list, name);
194 le = llentry_create (key, cf);
197 ERROR ("plugin: register_callback: "
198 "llentry_create failed.");
200 destroy_callback (cf);
204 llist_append (*list, le);
208 callback_func_t *old_cf;
213 WARNING ("plugin: register_callback: "
214 "a callback named `%s' already exists - "
215 "overwriting the old entry!", name);
217 destroy_callback (old_cf);
222 } /* }}} int register_callback */
224 static int create_register_callback (llist_t **list, /* {{{ */
225 const char *name, void *callback, user_data_t *ud)
229 cf = (callback_func_t *) malloc (sizeof (*cf));
232 ERROR ("plugin: create_register_callback: malloc failed.");
235 memset (cf, 0, sizeof (*cf));
237 cf->cf_callback = callback;
240 cf->cf_udata.data = NULL;
241 cf->cf_udata.free_func = NULL;
248 return (register_callback (list, name, cf));
249 } /* }}} int create_register_callback */
251 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
258 e = llist_search (list, name);
262 llist_remove (list, e);
265 destroy_callback (e->value);
270 } /* }}} int plugin_unregister */
273 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
274 * object, but it will bitch about a shared object not having a
275 * ``module_register'' symbol..
277 static int plugin_load_file (char *file, uint32_t flags)
280 void (*reg_handle) (void);
282 DEBUG ("file = %s", file);
285 lt_dlerror (); /* clear errors */
287 #if LIBTOOL_VERSION == 2
288 if (flags & PLUGIN_FLAGS_GLOBAL) {
290 lt_dladvise_init(&advise);
291 lt_dladvise_global(&advise);
292 dlh = lt_dlopenadvise(file, advise);
293 lt_dladvise_destroy(&advise);
295 dlh = lt_dlopen (file);
297 #else /* if LIBTOOL_VERSION == 1 */
298 if (flags & PLUGIN_FLAGS_GLOBAL)
299 ERROR ("plugin_load_file: The global flag is not supported, "
300 "libtool 2 is required for this.");
301 dlh = lt_dlopen (file);
306 const char *error = lt_dlerror ();
308 ERROR ("lt_dlopen (%s) failed: %s", file, error);
309 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
313 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
315 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
316 file, lt_dlerror ());
326 static _Bool timeout_reached(struct timespec timeout)
329 gettimeofday(&now, NULL);
330 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
333 static void *plugin_read_thread (void __attribute__((unused)) *args)
335 while (read_loop != 0)
343 /* Get the read function that needs to be read next. */
344 rf = c_heap_get_root (read_heap);
347 struct timespec abstime;
349 gettimeofday (&now, /* timezone = */ NULL);
351 abstime.tv_sec = now.tv_sec + interval_g;
352 abstime.tv_nsec = 1000 * now.tv_usec;
354 pthread_mutex_lock (&read_lock);
355 pthread_cond_timedwait (&read_cond, &read_lock,
357 pthread_mutex_unlock (&read_lock);
361 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
363 gettimeofday (&now, /* timezone = */ NULL);
365 rf->rf_interval.tv_sec = interval_g;
366 rf->rf_interval.tv_nsec = 0;
368 rf->rf_effective_interval = rf->rf_interval;
370 rf->rf_next_read.tv_sec = now.tv_sec;
371 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
374 /* sleep until this entry is due,
375 * using pthread_cond_timedwait */
376 pthread_mutex_lock (&read_lock);
377 /* In pthread_cond_timedwait, spurious wakeups are possible
378 * (and really happen, at least on NetBSD with > 1 CPU), thus
379 * we need to re-evaluate the condition every time
380 * pthread_cond_timedwait returns. */
382 while (!timeout_reached(rf->rf_next_read) && rc == 0) {
383 rc = pthread_cond_timedwait (&read_cond, &read_lock,
387 /* Must hold `real_lock' when accessing `rf->rf_type'. */
388 rf_type = rf->rf_type;
389 pthread_mutex_unlock (&read_lock);
391 /* Check if we're supposed to stop.. This may have interrupted
395 /* Insert `rf' again, so it can be free'd correctly */
396 c_heap_insert (read_heap, rf);
400 /* The entry has been marked for deletion. The linked list
401 * entry has already been removed by `plugin_unregister_read'.
402 * All we have to do here is free the `read_func_t' and
404 if (rf_type == RF_REMOVE)
406 DEBUG ("plugin_read_thread: Destroying the `%s' "
407 "callback.", rf->rf_name);
408 destroy_callback ((callback_func_t *) rf);
413 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
415 if (rf_type == RF_SIMPLE)
417 int (*callback) (void);
419 callback = rf->rf_callback;
420 status = (*callback) ();
424 plugin_read_cb callback;
426 assert (rf_type == RF_COMPLEX);
428 callback = rf->rf_callback;
429 status = (*callback) (&rf->rf_udata);
432 /* If the function signals failure, we will increase the
433 * intervals in which it will be called. */
436 rf->rf_effective_interval.tv_sec *= 2;
437 rf->rf_effective_interval.tv_nsec *= 2;
438 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
440 if (rf->rf_effective_interval.tv_sec >= 86400)
442 rf->rf_effective_interval.tv_sec = 86400;
443 rf->rf_effective_interval.tv_nsec = 0;
446 NOTICE ("read-function of plugin `%s' failed. "
447 "Will suspend it for %i seconds.",
449 (int) rf->rf_effective_interval.tv_sec);
453 /* Success: Restore the interval, if it was changed. */
454 rf->rf_effective_interval = rf->rf_interval;
457 /* update the ``next read due'' field */
458 gettimeofday (&now, /* timezone = */ NULL);
460 DEBUG ("plugin_read_thread: Effective interval of the "
461 "%s plugin is %i.%09i.",
463 (int) rf->rf_effective_interval.tv_sec,
464 (int) rf->rf_effective_interval.tv_nsec);
466 /* Calculate the next (absolute) time at which this function
467 * should be called. */
468 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
469 + rf->rf_effective_interval.tv_sec;
470 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
471 + rf->rf_effective_interval.tv_nsec;
472 NORMALIZE_TIMESPEC (rf->rf_next_read);
474 /* Check, if `rf_next_read' is in the past. */
475 if ((rf->rf_next_read.tv_sec < now.tv_sec)
476 || ((rf->rf_next_read.tv_sec == now.tv_sec)
477 && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
479 /* `rf_next_read' is in the past. Insert `now'
480 * so this value doesn't trail off into the
482 rf->rf_next_read.tv_sec = now.tv_sec;
483 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
486 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
488 (int) rf->rf_next_read.tv_sec,
489 (int) rf->rf_next_read.tv_nsec);
491 /* Re-insert this read function into the heap again. */
492 c_heap_insert (read_heap, rf);
493 } /* while (read_loop) */
497 } /* void *plugin_read_thread */
499 static void start_read_threads (int num)
503 if (read_threads != NULL)
506 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
507 if (read_threads == NULL)
509 ERROR ("plugin: start_read_threads: calloc failed.");
513 read_threads_num = 0;
514 for (i = 0; i < num; i++)
516 if (pthread_create (read_threads + read_threads_num, NULL,
517 plugin_read_thread, NULL) == 0)
523 ERROR ("plugin: start_read_threads: pthread_create failed.");
527 } /* void start_read_threads */
529 static void stop_read_threads (void)
533 if (read_threads == NULL)
536 INFO ("collectd: Stopping %i read threads.", read_threads_num);
538 pthread_mutex_lock (&read_lock);
540 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
541 pthread_cond_broadcast (&read_cond);
542 pthread_mutex_unlock (&read_lock);
544 for (i = 0; i < read_threads_num; i++)
546 if (pthread_join (read_threads[i], NULL) != 0)
548 ERROR ("plugin: stop_read_threads: pthread_join failed.");
550 read_threads[i] = (pthread_t) 0;
552 sfree (read_threads);
553 read_threads_num = 0;
554 } /* void stop_read_threads */
559 void plugin_set_dir (const char *dir)
561 if (plugindir != NULL)
566 else if ((plugindir = strdup (dir)) == NULL)
569 ERROR ("strdup failed: %s",
570 sstrerror (errno, errbuf, sizeof (errbuf)));
575 int plugin_load (const char *type, uint32_t flags)
579 char filename[BUFSIZE] = "";
580 char typename[BUFSIZE];
587 DEBUG ("type = %s", type);
589 dir = plugin_get_dir ();
592 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
593 * type when matching the filename */
594 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
595 if ((status < 0) || ((size_t) status >= sizeof (typename)))
597 WARNING ("snprintf: truncated: `%s.so'", type);
600 typename_len = strlen (typename);
602 if ((dh = opendir (dir)) == NULL)
605 ERROR ("opendir (%s): %s", dir,
606 sstrerror (errno, errbuf, sizeof (errbuf)));
610 while ((de = readdir (dh)) != NULL)
612 if (strncasecmp (de->d_name, typename, typename_len))
615 status = ssnprintf (filename, sizeof (filename),
616 "%s/%s", dir, de->d_name);
617 if ((status < 0) || ((size_t) status >= sizeof (filename)))
619 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
623 if (lstat (filename, &statbuf) == -1)
626 WARNING ("stat %s: %s", filename,
627 sstrerror (errno, errbuf, sizeof (errbuf)));
630 else if (!S_ISREG (statbuf.st_mode))
632 /* don't follow symlinks */
633 WARNING ("stat %s: not a regular file", filename);
637 if (plugin_load_file (filename, flags) == 0)
645 fprintf (stderr, "Unable to load plugin %s.\n", type);
651 if (filename[0] == '\0')
652 fprintf (stderr, "Could not find plugin %s.\n", type);
658 * The `register_*' functions follow
660 int plugin_register_config (const char *name,
661 int (*callback) (const char *key, const char *val),
662 const char **keys, int keys_num)
664 cf_register (name, callback, keys, keys_num);
666 } /* int plugin_register_config */
668 int plugin_register_complex_config (const char *type,
669 int (*callback) (oconfig_item_t *))
671 return (cf_register_complex (type, callback));
672 } /* int plugin_register_complex_config */
674 int plugin_register_init (const char *name,
675 int (*callback) (void))
677 return (create_register_callback (&list_init, name, (void *) callback,
678 /* user_data = */ NULL));
679 } /* plugin_register_init */
681 static int plugin_compare_read_func (const void *arg0, const void *arg1)
683 const read_func_t *rf0;
684 const read_func_t *rf1;
689 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
691 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
693 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
695 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
699 } /* int plugin_compare_read_func */
701 /* Add a read function to both, the heap and a linked list. The linked list if
702 * used to look-up read functions, especially for the remove function. The heap
703 * is used to determine which plugin to read next. */
704 static int plugin_insert_read (read_func_t *rf)
709 pthread_mutex_lock (&read_lock);
711 if (read_list == NULL)
713 read_list = llist_create ();
714 if (read_list == NULL)
716 pthread_mutex_unlock (&read_lock);
717 ERROR ("plugin_insert_read: read_list failed.");
722 if (read_heap == NULL)
724 read_heap = c_heap_create (plugin_compare_read_func);
725 if (read_heap == NULL)
727 pthread_mutex_unlock (&read_lock);
728 ERROR ("plugin_insert_read: c_heap_create failed.");
733 le = llentry_create (rf->rf_name, rf);
736 pthread_mutex_unlock (&read_lock);
737 ERROR ("plugin_insert_read: llentry_create failed.");
741 status = c_heap_insert (read_heap, rf);
744 pthread_mutex_unlock (&read_lock);
745 ERROR ("plugin_insert_read: c_heap_insert failed.");
746 llentry_destroy (le);
750 /* This does not fail. */
751 llist_append (read_list, le);
753 pthread_mutex_unlock (&read_lock);
755 } /* int plugin_insert_read */
757 int plugin_register_read (const char *name,
758 int (*callback) (void))
762 rf = (read_func_t *) malloc (sizeof (read_func_t));
766 ERROR ("plugin_register_read: malloc failed: %s",
767 sstrerror (errno, errbuf, sizeof (errbuf)));
771 memset (rf, 0, sizeof (read_func_t));
772 rf->rf_callback = (void *) callback;
773 rf->rf_udata.data = NULL;
774 rf->rf_udata.free_func = NULL;
775 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
776 rf->rf_type = RF_SIMPLE;
777 rf->rf_interval.tv_sec = 0;
778 rf->rf_interval.tv_nsec = 0;
779 rf->rf_effective_interval = rf->rf_interval;
781 return (plugin_insert_read (rf));
782 } /* int plugin_register_read */
784 int plugin_register_complex_read (const char *name,
785 plugin_read_cb callback,
786 const struct timespec *interval,
787 user_data_t *user_data)
791 rf = (read_func_t *) malloc (sizeof (read_func_t));
794 ERROR ("plugin_register_complex_read: malloc failed.");
798 memset (rf, 0, sizeof (read_func_t));
799 rf->rf_callback = (void *) callback;
800 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
801 rf->rf_type = RF_COMPLEX;
802 if (interval != NULL)
804 rf->rf_interval = *interval;
806 rf->rf_effective_interval = rf->rf_interval;
809 if (user_data == NULL)
811 rf->rf_udata.data = NULL;
812 rf->rf_udata.free_func = NULL;
816 rf->rf_udata = *user_data;
819 return (plugin_insert_read (rf));
820 } /* int plugin_register_complex_read */
822 int plugin_register_write (const char *name,
823 plugin_write_cb callback, user_data_t *ud)
825 return (create_register_callback (&list_write, name,
826 (void *) callback, ud));
827 } /* int plugin_register_write */
829 int plugin_register_flush (const char *name,
830 plugin_flush_cb callback, user_data_t *ud)
832 return (create_register_callback (&list_flush, name,
833 (void *) callback, ud));
834 } /* int plugin_register_flush */
836 int plugin_register_shutdown (char *name,
837 int (*callback) (void))
839 return (create_register_callback (&list_shutdown, name,
840 (void *) callback, /* user_data = */ NULL));
841 } /* int plugin_register_shutdown */
843 int plugin_register_data_set (const data_set_t *ds)
848 if ((data_sets != NULL)
849 && (c_avl_get (data_sets, ds->type, NULL) == 0))
851 NOTICE ("Replacing DS `%s' with another version.", ds->type);
852 plugin_unregister_data_set (ds->type);
854 else if (data_sets == NULL)
856 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
857 if (data_sets == NULL)
861 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
864 memcpy(ds_copy, ds, sizeof (data_set_t));
866 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
868 if (ds_copy->ds == NULL)
874 for (i = 0; i < ds->ds_num; i++)
875 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
877 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
878 } /* int plugin_register_data_set */
880 int plugin_register_log (const char *name,
881 plugin_log_cb callback, user_data_t *ud)
883 return (create_register_callback (&list_log, name,
884 (void *) callback, ud));
885 } /* int plugin_register_log */
887 int plugin_register_notification (const char *name,
888 plugin_notification_cb callback, user_data_t *ud)
890 return (create_register_callback (&list_notification, name,
891 (void *) callback, ud));
892 } /* int plugin_register_log */
894 int plugin_unregister_config (const char *name)
896 cf_unregister (name);
898 } /* int plugin_unregister_config */
900 int plugin_unregister_complex_config (const char *name)
902 cf_unregister_complex (name);
904 } /* int plugin_unregister_complex_config */
906 int plugin_unregister_init (const char *name)
908 return (plugin_unregister (list_init, name));
911 int plugin_unregister_read (const char *name) /* {{{ */
919 pthread_mutex_lock (&read_lock);
921 if (read_list == NULL)
923 pthread_mutex_unlock (&read_lock);
927 le = llist_search (read_list, name);
930 pthread_mutex_unlock (&read_lock);
931 WARNING ("plugin_unregister_read: No such read function: %s",
936 llist_remove (read_list, le);
940 rf->rf_type = RF_REMOVE;
942 pthread_mutex_unlock (&read_lock);
944 llentry_destroy (le);
946 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
949 } /* }}} int plugin_unregister_read */
951 int plugin_unregister_write (const char *name)
953 return (plugin_unregister (list_write, name));
956 int plugin_unregister_flush (const char *name)
958 return (plugin_unregister (list_flush, name));
961 int plugin_unregister_shutdown (const char *name)
963 return (plugin_unregister (list_shutdown, name));
966 int plugin_unregister_data_set (const char *name)
970 if (data_sets == NULL)
973 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
980 } /* int plugin_unregister_data_set */
982 int plugin_unregister_log (const char *name)
984 return (plugin_unregister (list_log, name));
987 int plugin_unregister_notification (const char *name)
989 return (plugin_unregister (list_notification, name));
992 void plugin_init_all (void)
994 const char *chain_name;
998 /* Init the value cache */
1001 chain_name = global_option_get ("PreCacheChain");
1002 pre_cache_chain = fc_chain_get_by_name (chain_name);
1004 chain_name = global_option_get ("PostCacheChain");
1005 post_cache_chain = fc_chain_get_by_name (chain_name);
1008 if ((list_init == NULL) && (read_heap == NULL))
1011 /* Calling all init callbacks before checking if read callbacks
1012 * are available allows the init callbacks to register the read
1014 le = llist_head (list_init);
1017 callback_func_t *cf;
1018 plugin_init_cb callback;
1021 callback = cf->cf_callback;
1022 status = (*callback) ();
1026 ERROR ("Initialization of plugin `%s' "
1027 "failed with status %i. "
1028 "Plugin will be unloaded.",
1030 /* Plugins that register read callbacks from the init
1031 * callback should take care of appropriate error
1032 * handling themselves. */
1033 /* FIXME: Unload _all_ functions */
1034 plugin_unregister_read (le->key);
1040 /* Start read-threads */
1041 if (read_heap != NULL)
1045 rt = global_option_get ("ReadThreads");
1048 start_read_threads ((num > 0) ? num : 5);
1050 } /* void plugin_init_all */
1052 /* TODO: Rename this function. */
1053 void plugin_read_all (void)
1055 uc_check_timeout ();
1058 } /* void plugin_read_all */
1060 /* Read function called when the `-T' command line argument is given. */
1061 int plugin_read_all_once (void)
1064 int return_status = 0;
1066 if (read_heap == NULL)
1068 NOTICE ("No read-functions are registered.");
1076 rf = c_heap_get_root (read_heap);
1080 if (rf->rf_type == RF_SIMPLE)
1082 int (*callback) (void);
1084 callback = rf->rf_callback;
1085 status = (*callback) ();
1089 plugin_read_cb callback;
1091 callback = rf->rf_callback;
1092 status = (*callback) (&rf->rf_udata);
1097 NOTICE ("read-function of plugin `%s' failed.",
1102 destroy_callback ((void *) rf);
1105 return (return_status);
1106 } /* int plugin_read_all_once */
1108 int plugin_write (const char *plugin, /* {{{ */
1109 const data_set_t *ds, const value_list_t *vl)
1117 if (list_write == NULL)
1122 ds = plugin_get_ds (vl->type);
1125 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1135 le = llist_head (list_write);
1138 callback_func_t *cf = le->value;
1139 plugin_write_cb callback;
1141 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1142 callback = cf->cf_callback;
1143 status = (*callback) (ds, vl, &cf->cf_udata);
1152 if ((success == 0) && (failure != 0))
1157 else /* plugin != NULL */
1159 callback_func_t *cf;
1160 plugin_write_cb callback;
1162 le = llist_head (list_write);
1165 if (strcasecmp (plugin, le->key) == 0)
1176 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1177 callback = cf->cf_callback;
1178 status = (*callback) (ds, vl, &cf->cf_udata);
1182 } /* }}} int plugin_write */
1184 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1188 if (list_flush == NULL)
1191 le = llist_head (list_flush);
1194 callback_func_t *cf;
1195 plugin_flush_cb callback;
1197 if ((plugin != NULL)
1198 && (strcmp (plugin, le->key) != 0))
1205 callback = cf->cf_callback;
1207 (*callback) (timeout, identifier, &cf->cf_udata);
1212 } /* int plugin_flush */
1214 void plugin_shutdown_all (void)
1218 stop_read_threads ();
1220 destroy_all_callbacks (&list_init);
1222 pthread_mutex_lock (&read_lock);
1223 llist_destroy (read_list);
1225 pthread_mutex_unlock (&read_lock);
1227 destroy_read_heap ();
1229 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1230 /* identifier = */ NULL);
1233 if (list_shutdown != NULL)
1234 le = llist_head (list_shutdown);
1238 callback_func_t *cf;
1239 plugin_shutdown_cb callback;
1242 callback = cf->cf_callback;
1244 /* Advance the pointer before calling the callback allows
1245 * shutdown functions to unregister themselves. If done the
1246 * other way around the memory `le' points to will be freed
1247 * after callback returns. */
1253 /* Write plugins which use the `user_data' pointer usually need the
1254 * same data available to the flush callback. If this is the case, set
1255 * the free_function to NULL when registering the flush callback and to
1256 * the real free function when registering the write callback. This way
1257 * the data isn't freed twice. */
1258 destroy_all_callbacks (&list_flush);
1259 destroy_all_callbacks (&list_write);
1261 destroy_all_callbacks (&list_notification);
1262 destroy_all_callbacks (&list_shutdown);
1263 destroy_all_callbacks (&list_log);
1264 } /* void plugin_shutdown_all */
1266 int plugin_dispatch_values (value_list_t *vl)
1269 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1271 value_t *saved_values;
1272 int saved_values_len;
1276 int free_meta_data = 0;
1278 if ((vl == NULL) || (vl->type[0] == 0)
1279 || (vl->values == NULL) || (vl->values_len < 1))
1281 ERROR ("plugin_dispatch_values: Invalid value list.");
1285 /* Free meta data only if the calling function didn't specify any. In
1286 * this case matches and targets may add some and the calling function
1287 * may not expect (and therefore free) that data. */
1288 if (vl->meta == NULL)
1291 if (list_write == NULL)
1292 c_complain_once (LOG_WARNING, &no_write_complaint,
1293 "plugin_dispatch_values: No write callback has been "
1294 "registered. Please load at least one output plugin, "
1295 "if you want the collected data to be stored.");
1297 if (data_sets == NULL)
1299 ERROR ("plugin_dispatch_values: No data sets registered. "
1300 "Could the types database be read? Check "
1301 "your `TypesDB' setting!");
1305 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1307 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1312 vl->time = time (NULL);
1314 if (vl->interval <= 0)
1315 vl->interval = interval_g;
1317 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1319 "plugin = %s; plugin_instance = %s; "
1320 "type = %s; type_instance = %s;",
1321 (unsigned int) vl->time, vl->interval,
1323 vl->plugin, vl->plugin_instance,
1324 vl->type, vl->type_instance);
1327 assert (0 == strcmp (ds->type, vl->type));
1329 if (0 != strcmp (ds->type, vl->type))
1330 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1331 ds->type, vl->type);
1335 assert (ds->ds_num == vl->values_len);
1337 if (ds->ds_num != vl->values_len)
1339 ERROR ("plugin_dispatch_values: ds->type = %s: "
1340 "(ds->ds_num = %i) != "
1341 "(vl->values_len = %i)",
1342 ds->type, ds->ds_num, vl->values_len);
1347 escape_slashes (vl->host, sizeof (vl->host));
1348 escape_slashes (vl->plugin, sizeof (vl->plugin));
1349 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1350 escape_slashes (vl->type, sizeof (vl->type));
1351 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1353 /* Copy the values. This way, we can assure `targets' that they get
1354 * dynamically allocated values, which they can free and replace if
1356 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1358 saved_values = vl->values;
1359 saved_values_len = vl->values_len;
1361 vl->values = (value_t *) calloc (vl->values_len,
1362 sizeof (*vl->values));
1363 if (vl->values == NULL)
1365 ERROR ("plugin_dispatch_values: calloc failed.");
1366 vl->values = saved_values;
1369 memcpy (vl->values, saved_values,
1370 vl->values_len * sizeof (*vl->values));
1372 else /* if ((pre == NULL) && (post == NULL)) */
1374 saved_values = NULL;
1375 saved_values_len = 0;
1378 if (pre_cache_chain != NULL)
1380 status = fc_process_chain (ds, vl, pre_cache_chain);
1383 WARNING ("plugin_dispatch_values: Running the "
1384 "pre-cache chain failed with "
1388 else if (status == FC_TARGET_STOP)
1390 /* Restore the state of the value_list so that plugins
1391 * don't get confused.. */
1392 if (saved_values != NULL)
1395 vl->values = saved_values;
1396 vl->values_len = saved_values_len;
1402 /* Update the value cache */
1405 /* Initiate threshold checking */
1406 ut_check_threshold (ds, vl);
1408 if (post_cache_chain != NULL)
1410 status = fc_process_chain (ds, vl, post_cache_chain);
1413 WARNING ("plugin_dispatch_values: Running the "
1414 "post-cache chain failed with "
1420 fc_default_action (ds, vl);
1422 /* Restore the state of the value_list so that plugins don't get
1424 if (saved_values != NULL)
1427 vl->values = saved_values;
1428 vl->values_len = saved_values_len;
1431 if ((free_meta_data != 0) && (vl->meta != NULL))
1433 meta_data_destroy (vl->meta);
1438 } /* int plugin_dispatch_values */
1440 int plugin_dispatch_notification (const notification_t *notif)
1443 /* Possible TODO: Add flap detection here */
1445 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1446 "time = %u; host = %s;",
1447 notif->severity, notif->message,
1448 (unsigned int) notif->time, notif->host);
1450 /* Nobody cares for notifications */
1451 if (list_notification == NULL)
1454 le = llist_head (list_notification);
1457 callback_func_t *cf;
1458 plugin_notification_cb callback;
1462 callback = cf->cf_callback;
1463 status = (*callback) (notif, &cf->cf_udata);
1466 WARNING ("plugin_dispatch_notification: Notification "
1467 "callback %s returned %i.",
1475 } /* int plugin_dispatch_notification */
1477 void plugin_log (int level, const char *format, ...)
1483 if (list_log == NULL)
1485 va_start (ap, format);
1486 vfprintf (stderr, format, ap);
1492 if (level >= LOG_DEBUG)
1496 va_start (ap, format);
1497 vsnprintf (msg, sizeof (msg), format, ap);
1498 msg[sizeof (msg) - 1] = '\0';
1501 le = llist_head (list_log);
1504 callback_func_t *cf;
1505 plugin_log_cb callback;
1508 callback = cf->cf_callback;
1510 (*callback) (level, msg, &cf->cf_udata);
1514 } /* void plugin_log */
1516 const data_set_t *plugin_get_ds (const char *name)
1520 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1522 DEBUG ("No such dataset registered: %s", name);
1527 } /* data_set_t *plugin_get_ds */
1529 static int plugin_notification_meta_add (notification_t *n,
1531 enum notification_meta_type_e type,
1534 notification_meta_t *meta;
1535 notification_meta_t *tail;
1537 if ((n == NULL) || (name == NULL) || (value == NULL))
1539 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1543 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1546 ERROR ("plugin_notification_meta_add: malloc failed.");
1549 memset (meta, 0, sizeof (notification_meta_t));
1551 sstrncpy (meta->name, name, sizeof (meta->name));
1556 case NM_TYPE_STRING:
1558 meta->nm_value.nm_string = strdup ((const char *) value);
1559 if (meta->nm_value.nm_string == NULL)
1561 ERROR ("plugin_notification_meta_add: strdup failed.");
1567 case NM_TYPE_SIGNED_INT:
1569 meta->nm_value.nm_signed_int = *((int64_t *) value);
1572 case NM_TYPE_UNSIGNED_INT:
1574 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1577 case NM_TYPE_DOUBLE:
1579 meta->nm_value.nm_double = *((double *) value);
1582 case NM_TYPE_BOOLEAN:
1584 meta->nm_value.nm_boolean = *((bool *) value);
1589 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1593 } /* switch (type) */
1597 while ((tail != NULL) && (tail->next != NULL))
1606 } /* int plugin_notification_meta_add */
1608 int plugin_notification_meta_add_string (notification_t *n,
1612 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1615 int plugin_notification_meta_add_signed_int (notification_t *n,
1619 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1622 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1626 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1629 int plugin_notification_meta_add_double (notification_t *n,
1633 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1636 int plugin_notification_meta_add_boolean (notification_t *n,
1640 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1643 int plugin_notification_meta_copy (notification_t *dst,
1644 const notification_t *src)
1646 notification_meta_t *meta;
1648 assert (dst != NULL);
1649 assert (src != NULL);
1650 assert (dst != src);
1651 assert ((src->meta == NULL) || (src->meta != dst->meta));
1653 for (meta = src->meta; meta != NULL; meta = meta->next)
1655 if (meta->type == NM_TYPE_STRING)
1656 plugin_notification_meta_add_string (dst, meta->name,
1657 meta->nm_value.nm_string);
1658 else if (meta->type == NM_TYPE_SIGNED_INT)
1659 plugin_notification_meta_add_signed_int (dst, meta->name,
1660 meta->nm_value.nm_signed_int);
1661 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1662 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1663 meta->nm_value.nm_unsigned_int);
1664 else if (meta->type == NM_TYPE_DOUBLE)
1665 plugin_notification_meta_add_double (dst, meta->name,
1666 meta->nm_value.nm_double);
1667 else if (meta->type == NM_TYPE_BOOLEAN)
1668 plugin_notification_meta_add_boolean (dst, meta->name,
1669 meta->nm_value.nm_boolean);
1673 } /* int plugin_notification_meta_copy */
1675 int plugin_notification_meta_free (notification_meta_t *n)
1677 notification_meta_t *this;
1678 notification_meta_t *next;
1682 ERROR ("plugin_notification_meta_free: n == NULL!");
1687 while (this != NULL)
1691 if (this->type == NM_TYPE_STRING)
1693 free ((char *)this->nm_value.nm_string);
1694 this->nm_value.nm_string = NULL;
1702 } /* int plugin_notification_meta_free */
1704 /* vim: set sw=8 ts=8 noet fdm=marker : */