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_head_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: create_register_callback: "
177 "llist_create failed.");
178 destroy_callback (cf);
186 ERROR ("plugin: create_register_callback: strdup failed.");
187 destroy_callback (cf);
191 le = llist_search (*list, name);
194 le = llentry_create (key, cf);
197 ERROR ("plugin: create_register_callback: "
198 "llentry_create failed.");
200 destroy_callback (cf);
204 llist_append (*list, le);
208 callback_func_t *old_cf;
213 destroy_callback (old_cf);
218 } /* }}} int register_callback */
220 static int create_register_callback (llist_t **list, /* {{{ */
221 const char *name, void *callback, user_data_t *ud)
225 cf = (callback_func_t *) malloc (sizeof (*cf));
228 ERROR ("plugin: create_register_callback: malloc failed.");
231 memset (cf, 0, sizeof (*cf));
233 cf->cf_callback = callback;
236 cf->cf_udata.data = NULL;
237 cf->cf_udata.free_func = NULL;
244 return (register_callback (list, name, cf));
245 } /* }}} int create_register_callback */
247 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
254 e = llist_search (list, name);
258 llist_remove (list, e);
261 destroy_callback (e->value);
266 } /* }}} int plugin_unregister */
269 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
270 * object, but it will bitch about a shared object not having a
271 * ``module_register'' symbol..
273 static int plugin_load_file (char *file)
276 void (*reg_handle) (void);
278 DEBUG ("file = %s", file);
281 lt_dlerror (); /* clear errors */
284 if (strstr(file, "python") != NULL) {
286 lt_dladvise_init(&advise);
287 lt_dladvise_global(&advise);
288 dlh = lt_dlopenadvise(file, advise);
289 lt_dladvise_destroy(&advise);
291 dlh = lt_dlopen (file);
295 const char *error = lt_dlerror ();
297 ERROR ("lt_dlopen (%s) failed: %s", file, error);
298 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
302 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
304 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
305 file, lt_dlerror ());
315 static void *plugin_read_thread (void __attribute__((unused)) *args)
317 while (read_loop != 0)
324 /* Get the read function that needs to be read next. */
325 rf = c_head_get_root (read_heap);
328 struct timespec abstime;
330 gettimeofday (&now, /* timezone = */ NULL);
332 abstime.tv_sec = now.tv_sec + interval_g;
333 abstime.tv_nsec = 1000 * now.tv_usec;
335 pthread_mutex_lock (&read_lock);
336 pthread_cond_timedwait (&read_cond, &read_lock,
338 pthread_mutex_unlock (&read_lock);
342 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
344 gettimeofday (&now, /* timezone = */ NULL);
346 rf->rf_interval.tv_sec = interval_g;
347 rf->rf_interval.tv_nsec = 0;
349 rf->rf_effective_interval = rf->rf_interval;
351 rf->rf_next_read.tv_sec = now.tv_sec;
352 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
355 /* sleep until this entry is due,
356 * using pthread_cond_timedwait */
357 pthread_mutex_lock (&read_lock);
358 pthread_cond_timedwait (&read_cond, &read_lock,
360 /* Must hold `real_lock' when accessing `rf->rf_type'. */
361 rf_type = rf->rf_type;
362 pthread_mutex_unlock (&read_lock);
364 /* Check if we're supposed to stop.. This may have interrupted
368 /* Insert `rf' again, so it can be free'd correctly */
369 c_heap_insert (read_heap, rf);
373 /* The entry has been marked for deletion. The linked list
374 * entry has already been removed by `plugin_unregister_read'.
375 * All we have to do here is free the `read_func_t' and
377 if (rf_type == RF_REMOVE)
379 DEBUG ("plugin_read_thread: Destroying the `%s' "
380 "callback.", rf->rf_name);
381 destroy_callback ((callback_func_t *) rf);
386 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
388 if (rf_type == RF_SIMPLE)
390 int (*callback) (void);
392 callback = rf->rf_callback;
393 status = (*callback) ();
397 plugin_read_cb callback;
399 assert (rf_type == RF_COMPLEX);
401 callback = rf->rf_callback;
402 status = (*callback) (&rf->rf_udata);
405 /* If the function signals failure, we will increase the
406 * intervals in which it will be called. */
409 rf->rf_effective_interval.tv_sec *= 2;
410 rf->rf_effective_interval.tv_nsec *= 2;
411 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
413 if (rf->rf_effective_interval.tv_sec >= 86400)
415 rf->rf_effective_interval.tv_sec = 86400;
416 rf->rf_effective_interval.tv_nsec = 0;
419 NOTICE ("read-function of plugin `%s' failed. "
420 "Will suspend it for %i seconds.",
422 (int) rf->rf_effective_interval.tv_sec);
426 /* Success: Restore the interval, if it was changed. */
427 rf->rf_effective_interval = rf->rf_interval;
430 /* update the ``next read due'' field */
431 gettimeofday (&now, /* timezone = */ NULL);
433 DEBUG ("plugin_read_thread: Effective interval of the "
434 "%s plugin is %i.%09i.",
436 (int) rf->rf_effective_interval.tv_sec,
437 (int) rf->rf_effective_interval.tv_nsec);
439 /* Calculate the next (absolute) time at which this function
440 * should be called. */
441 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
442 + rf->rf_effective_interval.tv_sec;
443 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
444 + rf->rf_effective_interval.tv_nsec;
445 NORMALIZE_TIMESPEC (rf->rf_next_read);
447 /* Check, if `rf_next_read' is in the past. */
448 if ((rf->rf_next_read.tv_sec < now.tv_sec)
449 || ((rf->rf_next_read.tv_sec == now.tv_sec)
450 && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
452 /* `rf_next_read' is in the past. Insert `now'
453 * so this value doesn't trail off into the
455 rf->rf_next_read.tv_sec = now.tv_sec;
456 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
459 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
461 (int) rf->rf_next_read.tv_sec,
462 (int) rf->rf_next_read.tv_nsec);
464 /* Re-insert this read function into the heap again. */
465 c_heap_insert (read_heap, rf);
466 } /* while (read_loop) */
470 } /* void *plugin_read_thread */
472 static void start_read_threads (int num)
476 if (read_threads != NULL)
479 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
480 if (read_threads == NULL)
482 ERROR ("plugin: start_read_threads: calloc failed.");
486 read_threads_num = 0;
487 for (i = 0; i < num; i++)
489 if (pthread_create (read_threads + read_threads_num, NULL,
490 plugin_read_thread, NULL) == 0)
496 ERROR ("plugin: start_read_threads: pthread_create failed.");
500 } /* void start_read_threads */
502 static void stop_read_threads (void)
506 if (read_threads == NULL)
509 INFO ("collectd: Stopping %i read threads.", read_threads_num);
511 pthread_mutex_lock (&read_lock);
513 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
514 pthread_cond_broadcast (&read_cond);
515 pthread_mutex_unlock (&read_lock);
517 for (i = 0; i < read_threads_num; i++)
519 if (pthread_join (read_threads[i], NULL) != 0)
521 ERROR ("plugin: stop_read_threads: pthread_join failed.");
523 read_threads[i] = (pthread_t) 0;
525 sfree (read_threads);
526 read_threads_num = 0;
527 } /* void stop_read_threads */
532 void plugin_set_dir (const char *dir)
534 if (plugindir != NULL)
539 else if ((plugindir = strdup (dir)) == NULL)
542 ERROR ("strdup failed: %s",
543 sstrerror (errno, errbuf, sizeof (errbuf)));
548 int plugin_load (const char *type)
552 char filename[BUFSIZE] = "";
553 char typename[BUFSIZE];
560 DEBUG ("type = %s", type);
562 dir = plugin_get_dir ();
565 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
566 * type when matching the filename */
567 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
568 if ((status < 0) || ((size_t) status >= sizeof (typename)))
570 WARNING ("snprintf: truncated: `%s.so'", type);
573 typename_len = strlen (typename);
575 if ((dh = opendir (dir)) == NULL)
578 ERROR ("opendir (%s): %s", dir,
579 sstrerror (errno, errbuf, sizeof (errbuf)));
583 while ((de = readdir (dh)) != NULL)
585 if (strncasecmp (de->d_name, typename, typename_len))
588 status = ssnprintf (filename, sizeof (filename),
589 "%s/%s", dir, de->d_name);
590 if ((status < 0) || ((size_t) status >= sizeof (filename)))
592 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
596 if (lstat (filename, &statbuf) == -1)
599 WARNING ("stat %s: %s", filename,
600 sstrerror (errno, errbuf, sizeof (errbuf)));
603 else if (!S_ISREG (statbuf.st_mode))
605 /* don't follow symlinks */
606 WARNING ("stat %s: not a regular file", filename);
610 if (plugin_load_file (filename) == 0)
618 fprintf (stderr, "Unable to load plugin %s.\n", type);
624 if (filename[0] == '\0')
625 fprintf (stderr, "Could not find plugin %s.\n", type);
631 * The `register_*' functions follow
633 int plugin_register_config (const char *name,
634 int (*callback) (const char *key, const char *val),
635 const char **keys, int keys_num)
637 cf_register (name, callback, keys, keys_num);
639 } /* int plugin_register_config */
641 int plugin_register_complex_config (const char *type,
642 int (*callback) (oconfig_item_t *))
644 return (cf_register_complex (type, callback));
645 } /* int plugin_register_complex_config */
647 int plugin_register_init (const char *name,
648 int (*callback) (void))
650 return (create_register_callback (&list_init, name, (void *) callback,
651 /* user_data = */ NULL));
652 } /* plugin_register_init */
654 static int plugin_compare_read_func (const void *arg0, const void *arg1)
656 const read_func_t *rf0;
657 const read_func_t *rf1;
662 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
664 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
666 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
668 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
672 } /* int plugin_compare_read_func */
674 /* Add a read function to both, the heap and a linked list. The linked list if
675 * used to look-up read functions, especially for the remove function. The heap
676 * is used to determine which plugin to read next. */
677 static int plugin_insert_read (read_func_t *rf)
682 pthread_mutex_lock (&read_lock);
684 if (read_list == NULL)
686 read_list = llist_create ();
687 if (read_list == NULL)
689 pthread_mutex_unlock (&read_lock);
690 ERROR ("plugin_insert_read: read_list failed.");
695 if (read_heap == NULL)
697 read_heap = c_heap_create (plugin_compare_read_func);
698 if (read_heap == NULL)
700 pthread_mutex_unlock (&read_lock);
701 ERROR ("plugin_insert_read: c_heap_create failed.");
706 le = llentry_create (rf->rf_name, rf);
709 pthread_mutex_unlock (&read_lock);
710 ERROR ("plugin_insert_read: llentry_create failed.");
714 status = c_heap_insert (read_heap, rf);
717 pthread_mutex_unlock (&read_lock);
718 ERROR ("plugin_insert_read: c_heap_insert failed.");
719 llentry_destroy (le);
723 /* This does not fail. */
724 llist_append (read_list, le);
726 pthread_mutex_unlock (&read_lock);
728 } /* int plugin_insert_read */
730 int plugin_register_read (const char *name,
731 int (*callback) (void))
735 rf = (read_func_t *) malloc (sizeof (read_func_t));
739 ERROR ("plugin_register_read: malloc failed: %s",
740 sstrerror (errno, errbuf, sizeof (errbuf)));
744 memset (rf, 0, sizeof (read_func_t));
745 rf->rf_callback = (void *) callback;
746 rf->rf_udata.data = NULL;
747 rf->rf_udata.free_func = NULL;
748 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
749 rf->rf_type = RF_SIMPLE;
750 rf->rf_interval.tv_sec = 0;
751 rf->rf_interval.tv_nsec = 0;
752 rf->rf_effective_interval = rf->rf_interval;
754 return (plugin_insert_read (rf));
755 } /* int plugin_register_read */
757 int plugin_register_complex_read (const char *name,
758 plugin_read_cb callback,
759 const struct timespec *interval,
760 user_data_t *user_data)
764 rf = (read_func_t *) malloc (sizeof (read_func_t));
767 ERROR ("plugin_register_complex_read: malloc failed.");
771 memset (rf, 0, sizeof (read_func_t));
772 rf->rf_callback = (void *) callback;
773 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
774 rf->rf_type = RF_COMPLEX;
775 if (interval != NULL)
777 rf->rf_interval = *interval;
779 rf->rf_effective_interval = rf->rf_interval;
782 if (user_data == NULL)
784 rf->rf_udata.data = NULL;
785 rf->rf_udata.free_func = NULL;
789 rf->rf_udata = *user_data;
792 return (plugin_insert_read (rf));
793 } /* int plugin_register_complex_read */
795 int plugin_register_write (const char *name,
796 plugin_write_cb callback, user_data_t *ud)
798 return (create_register_callback (&list_write, name,
799 (void *) callback, ud));
800 } /* int plugin_register_write */
802 int plugin_register_flush (const char *name,
803 plugin_flush_cb callback, user_data_t *ud)
805 return (create_register_callback (&list_flush, name,
806 (void *) callback, ud));
807 } /* int plugin_register_flush */
809 int plugin_register_shutdown (char *name,
810 int (*callback) (void))
812 return (create_register_callback (&list_shutdown, name,
813 (void *) callback, /* user_data = */ NULL));
814 } /* int plugin_register_shutdown */
816 int plugin_register_data_set (const data_set_t *ds)
821 if ((data_sets != NULL)
822 && (c_avl_get (data_sets, ds->type, NULL) == 0))
824 NOTICE ("Replacing DS `%s' with another version.", ds->type);
825 plugin_unregister_data_set (ds->type);
827 else if (data_sets == NULL)
829 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
830 if (data_sets == NULL)
834 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
837 memcpy(ds_copy, ds, sizeof (data_set_t));
839 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
841 if (ds_copy->ds == NULL)
847 for (i = 0; i < ds->ds_num; i++)
848 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
850 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
851 } /* int plugin_register_data_set */
853 int plugin_register_log (const char *name,
854 plugin_log_cb callback, user_data_t *ud)
856 return (create_register_callback (&list_log, name,
857 (void *) callback, ud));
858 } /* int plugin_register_log */
860 int plugin_register_notification (const char *name,
861 plugin_notification_cb callback, user_data_t *ud)
863 return (create_register_callback (&list_notification, name,
864 (void *) callback, ud));
865 } /* int plugin_register_log */
867 int plugin_unregister_config (const char *name)
869 cf_unregister (name);
871 } /* int plugin_unregister_config */
873 int plugin_unregister_complex_config (const char *name)
875 cf_unregister_complex (name);
877 } /* int plugin_unregister_complex_config */
879 int plugin_unregister_init (const char *name)
881 return (plugin_unregister (list_init, name));
884 int plugin_unregister_read (const char *name) /* {{{ */
892 pthread_mutex_lock (&read_lock);
894 if (read_list == NULL)
896 pthread_mutex_unlock (&read_lock);
900 le = llist_search (read_list, name);
903 pthread_mutex_unlock (&read_lock);
904 WARNING ("plugin_unregister_read: No such read function: %s",
909 llist_remove (read_list, le);
913 rf->rf_type = RF_REMOVE;
915 pthread_mutex_unlock (&read_lock);
917 llentry_destroy (le);
919 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
922 } /* }}} int plugin_unregister_read */
924 int plugin_unregister_write (const char *name)
926 return (plugin_unregister (list_write, name));
929 int plugin_unregister_flush (const char *name)
931 return (plugin_unregister (list_flush, name));
934 int plugin_unregister_shutdown (const char *name)
936 return (plugin_unregister (list_shutdown, name));
939 int plugin_unregister_data_set (const char *name)
943 if (data_sets == NULL)
946 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
953 } /* int plugin_unregister_data_set */
955 int plugin_unregister_log (const char *name)
957 return (plugin_unregister (list_log, name));
960 int plugin_unregister_notification (const char *name)
962 return (plugin_unregister (list_notification, name));
965 void plugin_init_all (void)
967 const char *chain_name;
971 /* Init the value cache */
974 chain_name = global_option_get ("PreCacheChain");
975 pre_cache_chain = fc_chain_get_by_name (chain_name);
977 chain_name = global_option_get ("PostCacheChain");
978 post_cache_chain = fc_chain_get_by_name (chain_name);
981 if ((list_init == NULL) && (read_heap == NULL))
984 /* Calling all init callbacks before checking if read callbacks
985 * are available allows the init callbacks to register the read
987 le = llist_head (list_init);
991 plugin_init_cb callback;
994 callback = cf->cf_callback;
995 status = (*callback) ();
999 ERROR ("Initialization of plugin `%s' "
1000 "failed with status %i. "
1001 "Plugin will be unloaded.",
1003 /* Plugins that register read callbacks from the init
1004 * callback should take care of appropriate error
1005 * handling themselves. */
1006 /* FIXME: Unload _all_ functions */
1007 plugin_unregister_read (le->key);
1013 /* Start read-threads */
1014 if (read_heap != NULL)
1018 rt = global_option_get ("ReadThreads");
1021 start_read_threads ((num > 0) ? num : 5);
1023 } /* void plugin_init_all */
1025 /* TODO: Rename this function. */
1026 void plugin_read_all (void)
1028 uc_check_timeout ();
1031 } /* void plugin_read_all */
1033 /* Read function called when the `-T' command line argument is given. */
1034 int plugin_read_all_once (void)
1037 int return_status = 0;
1039 if (read_heap == NULL)
1041 NOTICE ("No read-functions are registered.");
1049 rf = c_head_get_root (read_heap);
1053 if (rf->rf_type == RF_SIMPLE)
1055 int (*callback) (void);
1057 callback = rf->rf_callback;
1058 status = (*callback) ();
1062 plugin_read_cb callback;
1064 callback = rf->rf_callback;
1065 status = (*callback) (&rf->rf_udata);
1070 NOTICE ("read-function of plugin `%s' failed.",
1075 destroy_callback ((void *) rf);
1078 return (return_status);
1079 } /* int plugin_read_all_once */
1081 int plugin_write (const char *plugin, /* {{{ */
1082 const data_set_t *ds, const value_list_t *vl)
1090 if (list_write == NULL)
1095 ds = plugin_get_ds (vl->type);
1098 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1108 le = llist_head (list_write);
1111 callback_func_t *cf = le->value;
1112 plugin_write_cb callback;
1114 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1115 callback = cf->cf_callback;
1116 status = (*callback) (ds, vl, &cf->cf_udata);
1125 if ((success == 0) && (failure != 0))
1130 else /* plugin != NULL */
1132 callback_func_t *cf;
1133 plugin_write_cb callback;
1135 le = llist_head (list_write);
1138 if (strcasecmp (plugin, le->key) == 0)
1149 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1150 callback = cf->cf_callback;
1151 status = (*callback) (ds, vl, &cf->cf_udata);
1155 } /* }}} int plugin_write */
1157 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1161 if (list_flush == NULL)
1164 le = llist_head (list_flush);
1167 callback_func_t *cf;
1168 plugin_flush_cb callback;
1170 if ((plugin != NULL)
1171 && (strcmp (plugin, le->key) != 0))
1178 callback = cf->cf_callback;
1180 (*callback) (timeout, identifier, &cf->cf_udata);
1185 } /* int plugin_flush */
1187 void plugin_shutdown_all (void)
1191 stop_read_threads ();
1193 destroy_all_callbacks (&list_init);
1195 pthread_mutex_lock (&read_lock);
1196 llist_destroy (read_list);
1198 pthread_mutex_unlock (&read_lock);
1200 destroy_read_heap ();
1202 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1203 /* identifier = */ NULL);
1206 if (list_shutdown != NULL)
1207 le = llist_head (list_shutdown);
1211 callback_func_t *cf;
1212 plugin_shutdown_cb callback;
1215 callback = cf->cf_callback;
1217 /* Advance the pointer before calling the callback allows
1218 * shutdown functions to unregister themselves. If done the
1219 * other way around the memory `le' points to will be freed
1220 * after callback returns. */
1226 /* Write plugins which use the `user_data' pointer usually need the
1227 * same data available to the flush callback. If this is the case, set
1228 * the free_function to NULL when registering the flush callback and to
1229 * the real free function when registering the write callback. This way
1230 * the data isn't freed twice. */
1231 destroy_all_callbacks (&list_flush);
1232 destroy_all_callbacks (&list_write);
1234 destroy_all_callbacks (&list_notification);
1235 destroy_all_callbacks (&list_shutdown);
1236 destroy_all_callbacks (&list_log);
1237 } /* void plugin_shutdown_all */
1239 int plugin_dispatch_values (value_list_t *vl)
1242 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1244 value_t *saved_values;
1245 int saved_values_len;
1249 int free_meta_data = 0;
1251 if ((vl == NULL) || (vl->type[0] == 0)
1252 || (vl->values == NULL) || (vl->values_len < 1))
1254 ERROR ("plugin_dispatch_values: Invalid value list.");
1258 /* Free meta data only if the calling function didn't specify any. In
1259 * this case matches and targets may add some and the calling function
1260 * may not expect (and therefore free) that data. */
1261 if (vl->meta == NULL)
1264 if (list_write == NULL)
1265 c_complain_once (LOG_WARNING, &no_write_complaint,
1266 "plugin_dispatch_values: No write callback has been "
1267 "registered. Please load at least one output plugin, "
1268 "if you want the collected data to be stored.");
1270 if (data_sets == NULL)
1272 ERROR ("plugin_dispatch_values: No data sets registered. "
1273 "Could the types database be read? Check "
1274 "your `TypesDB' setting!");
1278 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1280 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1285 vl->time = time (NULL);
1287 if (vl->interval <= 0)
1288 vl->interval = interval_g;
1290 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1292 "plugin = %s; plugin_instance = %s; "
1293 "type = %s; type_instance = %s;",
1294 (unsigned int) vl->time, vl->interval,
1296 vl->plugin, vl->plugin_instance,
1297 vl->type, vl->type_instance);
1300 assert (0 == strcmp (ds->type, vl->type));
1302 if (0 != strcmp (ds->type, vl->type))
1303 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1304 ds->type, vl->type);
1308 assert (ds->ds_num == vl->values_len);
1310 if (ds->ds_num != vl->values_len)
1312 ERROR ("plugin_dispatch_values: ds->type = %s: "
1313 "(ds->ds_num = %i) != "
1314 "(vl->values_len = %i)",
1315 ds->type, ds->ds_num, vl->values_len);
1320 escape_slashes (vl->host, sizeof (vl->host));
1321 escape_slashes (vl->plugin, sizeof (vl->plugin));
1322 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1323 escape_slashes (vl->type, sizeof (vl->type));
1324 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1326 /* Copy the values. This way, we can assure `targets' that they get
1327 * dynamically allocated values, which they can free and replace if
1329 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1331 saved_values = vl->values;
1332 saved_values_len = vl->values_len;
1334 vl->values = (value_t *) calloc (vl->values_len,
1335 sizeof (*vl->values));
1336 if (vl->values == NULL)
1338 ERROR ("plugin_dispatch_values: calloc failed.");
1339 vl->values = saved_values;
1342 memcpy (vl->values, saved_values,
1343 vl->values_len * sizeof (*vl->values));
1345 else /* if ((pre == NULL) && (post == NULL)) */
1347 saved_values = NULL;
1348 saved_values_len = 0;
1351 if (pre_cache_chain != NULL)
1353 status = fc_process_chain (ds, vl, pre_cache_chain);
1356 WARNING ("plugin_dispatch_values: Running the "
1357 "pre-cache chain failed with "
1361 else if (status == FC_TARGET_STOP)
1363 /* Restore the state of the value_list so that plugins
1364 * don't get confused.. */
1365 if (saved_values != NULL)
1368 vl->values = saved_values;
1369 vl->values_len = saved_values_len;
1375 /* Update the value cache */
1378 /* Initiate threshold checking */
1379 ut_check_threshold (ds, vl);
1381 if (post_cache_chain != NULL)
1383 status = fc_process_chain (ds, vl, post_cache_chain);
1386 WARNING ("plugin_dispatch_values: Running the "
1387 "post-cache chain failed with "
1393 fc_default_action (ds, vl);
1395 /* Restore the state of the value_list so that plugins don't get
1397 if (saved_values != NULL)
1400 vl->values = saved_values;
1401 vl->values_len = saved_values_len;
1404 if ((free_meta_data != 0) && (vl->meta != NULL))
1406 meta_data_destroy (vl->meta);
1411 } /* int plugin_dispatch_values */
1413 int plugin_dispatch_notification (const notification_t *notif)
1416 /* Possible TODO: Add flap detection here */
1418 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1419 "time = %u; host = %s;",
1420 notif->severity, notif->message,
1421 (unsigned int) notif->time, notif->host);
1423 /* Nobody cares for notifications */
1424 if (list_notification == NULL)
1427 le = llist_head (list_notification);
1430 callback_func_t *cf;
1431 plugin_notification_cb callback;
1435 callback = cf->cf_callback;
1436 status = (*callback) (notif, &cf->cf_udata);
1439 WARNING ("plugin_dispatch_notification: Notification "
1440 "callback %s returned %i.",
1448 } /* int plugin_dispatch_notification */
1450 void plugin_log (int level, const char *format, ...)
1456 if (list_log == NULL)
1460 if (level >= LOG_DEBUG)
1464 va_start (ap, format);
1465 vsnprintf (msg, sizeof (msg), format, ap);
1466 msg[sizeof (msg) - 1] = '\0';
1469 le = llist_head (list_log);
1472 callback_func_t *cf;
1473 plugin_log_cb callback;
1476 callback = cf->cf_callback;
1478 (*callback) (level, msg, &cf->cf_udata);
1482 } /* void plugin_log */
1484 const data_set_t *plugin_get_ds (const char *name)
1488 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1490 DEBUG ("No such dataset registered: %s", name);
1495 } /* data_set_t *plugin_get_ds */
1497 static int plugin_notification_meta_add (notification_t *n,
1499 enum notification_meta_type_e type,
1502 notification_meta_t *meta;
1503 notification_meta_t *tail;
1505 if ((n == NULL) || (name == NULL) || (value == NULL))
1507 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1511 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1514 ERROR ("plugin_notification_meta_add: malloc failed.");
1517 memset (meta, 0, sizeof (notification_meta_t));
1519 sstrncpy (meta->name, name, sizeof (meta->name));
1524 case NM_TYPE_STRING:
1526 meta->nm_value.nm_string = strdup ((const char *) value);
1527 if (meta->nm_value.nm_string == NULL)
1529 ERROR ("plugin_notification_meta_add: strdup failed.");
1535 case NM_TYPE_SIGNED_INT:
1537 meta->nm_value.nm_signed_int = *((int64_t *) value);
1540 case NM_TYPE_UNSIGNED_INT:
1542 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1545 case NM_TYPE_DOUBLE:
1547 meta->nm_value.nm_double = *((double *) value);
1550 case NM_TYPE_BOOLEAN:
1552 meta->nm_value.nm_boolean = *((bool *) value);
1557 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1561 } /* switch (type) */
1565 while ((tail != NULL) && (tail->next != NULL))
1574 } /* int plugin_notification_meta_add */
1576 int plugin_notification_meta_add_string (notification_t *n,
1580 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1583 int plugin_notification_meta_add_signed_int (notification_t *n,
1587 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1590 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1594 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1597 int plugin_notification_meta_add_double (notification_t *n,
1601 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1604 int plugin_notification_meta_add_boolean (notification_t *n,
1608 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1611 int plugin_notification_meta_copy (notification_t *dst,
1612 const notification_t *src)
1614 notification_meta_t *meta;
1616 assert (dst != NULL);
1617 assert (src != NULL);
1618 assert (dst != src);
1619 assert ((src->meta == NULL) || (src->meta != dst->meta));
1621 for (meta = src->meta; meta != NULL; meta = meta->next)
1623 if (meta->type == NM_TYPE_STRING)
1624 plugin_notification_meta_add_string (dst, meta->name,
1625 meta->nm_value.nm_string);
1626 else if (meta->type == NM_TYPE_SIGNED_INT)
1627 plugin_notification_meta_add_signed_int (dst, meta->name,
1628 meta->nm_value.nm_signed_int);
1629 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1630 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1631 meta->nm_value.nm_unsigned_int);
1632 else if (meta->type == NM_TYPE_DOUBLE)
1633 plugin_notification_meta_add_double (dst, meta->name,
1634 meta->nm_value.nm_double);
1635 else if (meta->type == NM_TYPE_BOOLEAN)
1636 plugin_notification_meta_add_boolean (dst, meta->name,
1637 meta->nm_value.nm_boolean);
1641 } /* int plugin_notification_meta_copy */
1643 int plugin_notification_meta_free (notification_meta_t *n)
1645 notification_meta_t *this;
1646 notification_meta_t *next;
1650 ERROR ("plugin_notification_meta_free: n == NULL!");
1655 while (this != NULL)
1659 if (this->type == NM_TYPE_STRING)
1661 free ((char *)this->nm_value.nm_string);
1662 this->nm_value.nm_string = NULL;
1670 } /* int plugin_notification_meta_free */
1672 /* vim: set sw=8 ts=8 noet fdm=marker : */