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 */
283 if ((dlh = lt_dlopen (file)) == NULL)
285 const char *error = lt_dlerror ();
287 ERROR ("lt_dlopen (%s) failed: %s", file, error);
288 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
292 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
294 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
295 file, lt_dlerror ());
305 static void *plugin_read_thread (void __attribute__((unused)) *args)
307 while (read_loop != 0)
314 /* Get the read function that needs to be read next. */
315 rf = c_head_get_root (read_heap);
318 struct timespec abstime;
320 gettimeofday (&now, /* timezone = */ NULL);
322 abstime.tv_sec = now.tv_sec + interval_g;
323 abstime.tv_nsec = 1000 * now.tv_usec;
325 pthread_mutex_lock (&read_lock);
326 pthread_cond_timedwait (&read_cond, &read_lock,
328 pthread_mutex_unlock (&read_lock);
332 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
334 gettimeofday (&now, /* timezone = */ NULL);
336 rf->rf_interval.tv_sec = interval_g;
337 rf->rf_interval.tv_nsec = 0;
339 rf->rf_effective_interval = rf->rf_interval;
341 rf->rf_next_read.tv_sec = now.tv_sec;
342 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
345 /* sleep until this entry is due,
346 * using pthread_cond_timedwait */
347 pthread_mutex_lock (&read_lock);
348 pthread_cond_timedwait (&read_cond, &read_lock,
350 /* Must hold `real_lock' when accessing `rf->rf_type'. */
351 rf_type = rf->rf_type;
352 pthread_mutex_unlock (&read_lock);
354 /* Check if we're supposed to stop.. This may have interrupted
358 /* Insert `rf' again, so it can be free'd correctly */
359 c_heap_insert (read_heap, rf);
363 /* The entry has been marked for deletion. The linked list
364 * entry has already been removed by `plugin_unregister_read'.
365 * All we have to do here is free the `read_func_t' and
367 if (rf_type == RF_REMOVE)
369 DEBUG ("plugin_read_thread: Destroying the `%s' "
370 "callback.", rf->rf_name);
371 destroy_callback ((callback_func_t *) rf);
376 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
378 if (rf_type == RF_SIMPLE)
380 int (*callback) (void);
382 callback = rf->rf_callback;
383 status = (*callback) ();
387 plugin_read_cb callback;
389 assert (rf_type == RF_COMPLEX);
391 callback = rf->rf_callback;
392 status = (*callback) (&rf->rf_udata);
395 /* If the function signals failure, we will increase the
396 * intervals in which it will be called. */
399 rf->rf_effective_interval.tv_sec *= 2;
400 rf->rf_effective_interval.tv_nsec *= 2;
401 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
403 if (rf->rf_effective_interval.tv_sec >= 86400)
405 rf->rf_effective_interval.tv_sec = 86400;
406 rf->rf_effective_interval.tv_nsec = 0;
409 NOTICE ("read-function of plugin `%s' failed. "
410 "Will suspend it for %i seconds.",
412 (int) rf->rf_effective_interval.tv_sec);
416 /* Success: Restore the interval, if it was changed. */
417 rf->rf_effective_interval = rf->rf_interval;
420 /* update the ``next read due'' field */
421 gettimeofday (&now, /* timezone = */ NULL);
423 DEBUG ("plugin_read_thread: Effective interval of the "
424 "%s plugin is %i.%09i.",
426 (int) rf->rf_effective_interval.tv_sec,
427 (int) rf->rf_effective_interval.tv_nsec);
429 /* Calculate the next (absolute) time at which this function
430 * should be called. */
431 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
432 + rf->rf_effective_interval.tv_sec;
433 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
434 + rf->rf_effective_interval.tv_nsec;
435 NORMALIZE_TIMESPEC (rf->rf_next_read);
437 /* Check, if `rf_next_read' is in the past. */
438 if ((rf->rf_next_read.tv_sec < now.tv_sec)
439 || ((rf->rf_next_read.tv_sec == now.tv_sec)
440 && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
442 /* `rf_next_read' is in the past. Insert `now'
443 * so this value doesn't trail off into the
445 rf->rf_next_read.tv_sec = now.tv_sec;
446 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
449 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
451 (int) rf->rf_next_read.tv_sec,
452 (int) rf->rf_next_read.tv_nsec);
454 /* Re-insert this read function into the heap again. */
455 c_heap_insert (read_heap, rf);
456 } /* while (read_loop) */
460 } /* void *plugin_read_thread */
462 static void start_read_threads (int num)
466 if (read_threads != NULL)
469 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
470 if (read_threads == NULL)
472 ERROR ("plugin: start_read_threads: calloc failed.");
476 read_threads_num = 0;
477 for (i = 0; i < num; i++)
479 if (pthread_create (read_threads + read_threads_num, NULL,
480 plugin_read_thread, NULL) == 0)
486 ERROR ("plugin: start_read_threads: pthread_create failed.");
490 } /* void start_read_threads */
492 static void stop_read_threads (void)
496 if (read_threads == NULL)
499 INFO ("collectd: Stopping %i read threads.", read_threads_num);
501 pthread_mutex_lock (&read_lock);
503 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
504 pthread_cond_broadcast (&read_cond);
505 pthread_mutex_unlock (&read_lock);
507 for (i = 0; i < read_threads_num; i++)
509 if (pthread_join (read_threads[i], NULL) != 0)
511 ERROR ("plugin: stop_read_threads: pthread_join failed.");
513 read_threads[i] = (pthread_t) 0;
515 sfree (read_threads);
516 read_threads_num = 0;
517 } /* void stop_read_threads */
522 void plugin_set_dir (const char *dir)
524 if (plugindir != NULL)
529 else if ((plugindir = strdup (dir)) == NULL)
532 ERROR ("strdup failed: %s",
533 sstrerror (errno, errbuf, sizeof (errbuf)));
538 int plugin_load (const char *type)
542 char filename[BUFSIZE] = "";
543 char typename[BUFSIZE];
550 DEBUG ("type = %s", type);
552 dir = plugin_get_dir ();
555 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
556 * type when matching the filename */
557 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
558 if ((status < 0) || ((size_t) status >= sizeof (typename)))
560 WARNING ("snprintf: truncated: `%s.so'", type);
563 typename_len = strlen (typename);
565 if ((dh = opendir (dir)) == NULL)
568 ERROR ("opendir (%s): %s", dir,
569 sstrerror (errno, errbuf, sizeof (errbuf)));
573 while ((de = readdir (dh)) != NULL)
575 if (strncasecmp (de->d_name, typename, typename_len))
578 status = ssnprintf (filename, sizeof (filename),
579 "%s/%s", dir, de->d_name);
580 if ((status < 0) || ((size_t) status >= sizeof (filename)))
582 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
586 if (lstat (filename, &statbuf) == -1)
589 WARNING ("stat %s: %s", filename,
590 sstrerror (errno, errbuf, sizeof (errbuf)));
593 else if (!S_ISREG (statbuf.st_mode))
595 /* don't follow symlinks */
596 WARNING ("stat %s: not a regular file", filename);
600 if (plugin_load_file (filename) == 0)
608 fprintf (stderr, "Unable to load plugin %s.\n", type);
614 if (filename[0] == '\0')
615 fprintf (stderr, "Could not find plugin %s.\n", type);
621 * The `register_*' functions follow
623 int plugin_register_config (const char *name,
624 int (*callback) (const char *key, const char *val),
625 const char **keys, int keys_num)
627 cf_register (name, callback, keys, keys_num);
629 } /* int plugin_register_config */
631 int plugin_register_complex_config (const char *type,
632 int (*callback) (oconfig_item_t *))
634 return (cf_register_complex (type, callback));
635 } /* int plugin_register_complex_config */
637 int plugin_register_init (const char *name,
638 int (*callback) (void))
640 return (create_register_callback (&list_init, name, (void *) callback,
641 /* user_data = */ NULL));
642 } /* plugin_register_init */
644 static int plugin_compare_read_func (const void *arg0, const void *arg1)
646 const read_func_t *rf0;
647 const read_func_t *rf1;
652 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
654 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
656 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
658 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
662 } /* int plugin_compare_read_func */
664 /* Add a read function to both, the heap and a linked list. The linked list if
665 * used to look-up read functions, especially for the remove function. The heap
666 * is used to determine which plugin to read next. */
667 static int plugin_insert_read (read_func_t *rf)
672 pthread_mutex_lock (&read_lock);
674 if (read_list == NULL)
676 read_list = llist_create ();
677 if (read_list == NULL)
679 pthread_mutex_unlock (&read_lock);
680 ERROR ("plugin_insert_read: read_list failed.");
685 if (read_heap == NULL)
687 read_heap = c_heap_create (plugin_compare_read_func);
688 if (read_heap == NULL)
690 pthread_mutex_unlock (&read_lock);
691 ERROR ("plugin_insert_read: c_heap_create failed.");
696 le = llentry_create (rf->rf_name, rf);
699 pthread_mutex_unlock (&read_lock);
700 ERROR ("plugin_insert_read: llentry_create failed.");
704 status = c_heap_insert (read_heap, rf);
707 pthread_mutex_unlock (&read_lock);
708 ERROR ("plugin_insert_read: c_heap_insert failed.");
709 llentry_destroy (le);
713 /* This does not fail. */
714 llist_append (read_list, le);
716 pthread_mutex_unlock (&read_lock);
718 } /* int plugin_insert_read */
720 int plugin_register_read (const char *name,
721 int (*callback) (void))
725 rf = (read_func_t *) malloc (sizeof (read_func_t));
729 ERROR ("plugin_register_read: malloc failed: %s",
730 sstrerror (errno, errbuf, sizeof (errbuf)));
734 memset (rf, 0, sizeof (read_func_t));
735 rf->rf_callback = (void *) callback;
736 rf->rf_udata.data = NULL;
737 rf->rf_udata.free_func = NULL;
738 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
739 rf->rf_type = RF_SIMPLE;
740 rf->rf_interval.tv_sec = 0;
741 rf->rf_interval.tv_nsec = 0;
742 rf->rf_effective_interval = rf->rf_interval;
744 return (plugin_insert_read (rf));
745 } /* int plugin_register_read */
747 int plugin_register_complex_read (const char *name,
748 plugin_read_cb callback,
749 const struct timespec *interval,
750 user_data_t *user_data)
754 rf = (read_func_t *) malloc (sizeof (read_func_t));
757 ERROR ("plugin_register_complex_read: malloc failed.");
761 memset (rf, 0, sizeof (read_func_t));
762 rf->rf_callback = (void *) callback;
763 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
764 rf->rf_type = RF_COMPLEX;
765 if (interval != NULL)
767 rf->rf_interval = *interval;
769 rf->rf_effective_interval = rf->rf_interval;
772 if (user_data == NULL)
774 rf->rf_udata.data = NULL;
775 rf->rf_udata.free_func = NULL;
779 rf->rf_udata = *user_data;
782 return (plugin_insert_read (rf));
783 } /* int plugin_register_complex_read */
785 int plugin_register_write (const char *name,
786 plugin_write_cb callback, user_data_t *ud)
788 return (create_register_callback (&list_write, name,
789 (void *) callback, ud));
790 } /* int plugin_register_write */
792 int plugin_register_flush (const char *name,
793 plugin_flush_cb callback, user_data_t *ud)
795 return (create_register_callback (&list_flush, name,
796 (void *) callback, ud));
797 } /* int plugin_register_flush */
799 int plugin_register_shutdown (char *name,
800 int (*callback) (void))
802 return (create_register_callback (&list_shutdown, name,
803 (void *) callback, /* user_data = */ NULL));
804 } /* int plugin_register_shutdown */
806 int plugin_register_data_set (const data_set_t *ds)
811 if ((data_sets != NULL)
812 && (c_avl_get (data_sets, ds->type, NULL) == 0))
814 NOTICE ("Replacing DS `%s' with another version.", ds->type);
815 plugin_unregister_data_set (ds->type);
817 else if (data_sets == NULL)
819 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
820 if (data_sets == NULL)
824 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
827 memcpy(ds_copy, ds, sizeof (data_set_t));
829 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
831 if (ds_copy->ds == NULL)
837 for (i = 0; i < ds->ds_num; i++)
838 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
840 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
841 } /* int plugin_register_data_set */
843 int plugin_register_log (const char *name,
844 plugin_log_cb callback, user_data_t *ud)
846 return (create_register_callback (&list_log, name,
847 (void *) callback, ud));
848 } /* int plugin_register_log */
850 int plugin_register_notification (const char *name,
851 plugin_notification_cb callback, user_data_t *ud)
853 return (create_register_callback (&list_notification, name,
854 (void *) callback, ud));
855 } /* int plugin_register_log */
857 int plugin_unregister_config (const char *name)
859 cf_unregister (name);
861 } /* int plugin_unregister_config */
863 int plugin_unregister_complex_config (const char *name)
865 cf_unregister_complex (name);
867 } /* int plugin_unregister_complex_config */
869 int plugin_unregister_init (const char *name)
871 return (plugin_unregister (list_init, name));
874 int plugin_unregister_read (const char *name) /* {{{ */
882 pthread_mutex_lock (&read_lock);
884 if (read_list == NULL)
886 pthread_mutex_unlock (&read_lock);
890 le = llist_search (read_list, name);
893 pthread_mutex_unlock (&read_lock);
894 WARNING ("plugin_unregister_read: No such read function: %s",
899 llist_remove (read_list, le);
903 rf->rf_type = RF_REMOVE;
905 pthread_mutex_unlock (&read_lock);
907 llentry_destroy (le);
909 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
912 } /* }}} int plugin_unregister_read */
914 int plugin_unregister_write (const char *name)
916 return (plugin_unregister (list_write, name));
919 int plugin_unregister_flush (const char *name)
921 return (plugin_unregister (list_flush, name));
924 int plugin_unregister_shutdown (const char *name)
926 return (plugin_unregister (list_shutdown, name));
929 int plugin_unregister_data_set (const char *name)
933 if (data_sets == NULL)
936 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
943 } /* int plugin_unregister_data_set */
945 int plugin_unregister_log (const char *name)
947 return (plugin_unregister (list_log, name));
950 int plugin_unregister_notification (const char *name)
952 return (plugin_unregister (list_notification, name));
955 void plugin_init_all (void)
957 const char *chain_name;
961 /* Init the value cache */
964 chain_name = global_option_get ("PreCacheChain");
965 pre_cache_chain = fc_chain_get_by_name (chain_name);
967 chain_name = global_option_get ("PostCacheChain");
968 post_cache_chain = fc_chain_get_by_name (chain_name);
971 if ((list_init == NULL) && (read_heap == NULL))
974 /* Calling all init callbacks before checking if read callbacks
975 * are available allows the init callbacks to register the read
977 le = llist_head (list_init);
981 plugin_init_cb callback;
984 callback = cf->cf_callback;
985 status = (*callback) ();
989 ERROR ("Initialization of plugin `%s' "
990 "failed with status %i. "
991 "Plugin will be unloaded.",
993 /* Plugins that register read callbacks from the init
994 * callback should take care of appropriate error
995 * handling themselves. */
996 /* FIXME: Unload _all_ functions */
997 plugin_unregister_read (le->key);
1003 /* Start read-threads */
1004 if (read_heap != NULL)
1008 rt = global_option_get ("ReadThreads");
1011 start_read_threads ((num > 0) ? num : 5);
1013 } /* void plugin_init_all */
1015 /* TODO: Rename this function. */
1016 void plugin_read_all (void)
1018 uc_check_timeout ();
1021 } /* void plugin_read_all */
1023 /* Read function called when the `-T' command line argument is given. */
1024 int plugin_read_all_once (void)
1027 int return_status = 0;
1029 if (read_heap == NULL)
1031 NOTICE ("No read-functions are registered.");
1039 rf = c_head_get_root (read_heap);
1043 if (rf->rf_type == RF_SIMPLE)
1045 int (*callback) (void);
1047 callback = rf->rf_callback;
1048 status = (*callback) ();
1052 plugin_read_cb callback;
1054 callback = rf->rf_callback;
1055 status = (*callback) (&rf->rf_udata);
1060 NOTICE ("read-function of plugin `%s' failed.",
1065 destroy_callback ((void *) rf);
1068 return (return_status);
1069 } /* int plugin_read_all_once */
1071 int plugin_write (const char *plugin, /* {{{ */
1072 const data_set_t *ds, const value_list_t *vl)
1080 if (list_write == NULL)
1085 ds = plugin_get_ds (vl->type);
1088 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1098 le = llist_head (list_write);
1101 callback_func_t *cf = le->value;
1102 plugin_write_cb callback;
1104 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1105 callback = cf->cf_callback;
1106 status = (*callback) (ds, vl, &cf->cf_udata);
1115 if ((success == 0) && (failure != 0))
1120 else /* plugin != NULL */
1122 callback_func_t *cf;
1123 plugin_write_cb callback;
1125 le = llist_head (list_write);
1128 if (strcasecmp (plugin, le->key) == 0)
1139 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1140 callback = cf->cf_callback;
1141 status = (*callback) (ds, vl, &cf->cf_udata);
1145 } /* }}} int plugin_write */
1147 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1151 if (list_flush == NULL)
1154 le = llist_head (list_flush);
1157 callback_func_t *cf;
1158 plugin_flush_cb callback;
1160 if ((plugin != NULL)
1161 && (strcmp (plugin, le->key) != 0))
1168 callback = cf->cf_callback;
1170 (*callback) (timeout, identifier, &cf->cf_udata);
1175 } /* int plugin_flush */
1177 void plugin_shutdown_all (void)
1181 stop_read_threads ();
1183 destroy_all_callbacks (&list_init);
1185 pthread_mutex_lock (&read_lock);
1186 llist_destroy (read_list);
1188 pthread_mutex_unlock (&read_lock);
1190 destroy_read_heap ();
1192 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1193 /* identifier = */ NULL);
1196 if (list_shutdown != NULL)
1197 le = llist_head (list_shutdown);
1201 callback_func_t *cf;
1202 plugin_shutdown_cb callback;
1205 callback = cf->cf_callback;
1207 /* Advance the pointer before calling the callback allows
1208 * shutdown functions to unregister themselves. If done the
1209 * other way around the memory `le' points to will be freed
1210 * after callback returns. */
1216 /* Write plugins which use the `user_data' pointer usually need the
1217 * same data available to the flush callback. If this is the case, set
1218 * the free_function to NULL when registering the flush callback and to
1219 * the real free function when registering the write callback. This way
1220 * the data isn't freed twice. */
1221 destroy_all_callbacks (&list_flush);
1222 destroy_all_callbacks (&list_write);
1224 destroy_all_callbacks (&list_notification);
1225 destroy_all_callbacks (&list_shutdown);
1226 destroy_all_callbacks (&list_log);
1227 } /* void plugin_shutdown_all */
1229 int plugin_dispatch_values (value_list_t *vl)
1232 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1234 value_t *saved_values;
1235 int saved_values_len;
1239 int free_meta_data = 0;
1241 if ((vl == NULL) || (vl->type[0] == 0)
1242 || (vl->values == NULL) || (vl->values_len < 1))
1244 ERROR ("plugin_dispatch_values: Invalid value list.");
1248 /* Free meta data only if the calling function didn't specify any. In
1249 * this case matches and targets may add some and the calling function
1250 * may not expect (and therefore free) that data. */
1251 if (vl->meta == NULL)
1254 if (list_write == NULL)
1255 c_complain_once (LOG_WARNING, &no_write_complaint,
1256 "plugin_dispatch_values: No write callback has been "
1257 "registered. Please load at least one output plugin, "
1258 "if you want the collected data to be stored.");
1260 if (data_sets == NULL)
1262 ERROR ("plugin_dispatch_values: No data sets registered. "
1263 "Could the types database be read? Check "
1264 "your `TypesDB' setting!");
1268 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1270 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1275 vl->time = time (NULL);
1277 if (vl->interval <= 0)
1278 vl->interval = interval_g;
1280 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1282 "plugin = %s; plugin_instance = %s; "
1283 "type = %s; type_instance = %s;",
1284 (unsigned int) vl->time, vl->interval,
1286 vl->plugin, vl->plugin_instance,
1287 vl->type, vl->type_instance);
1290 assert (0 == strcmp (ds->type, vl->type));
1292 if (0 != strcmp (ds->type, vl->type))
1293 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1294 ds->type, vl->type);
1298 assert (ds->ds_num == vl->values_len);
1300 if (ds->ds_num != vl->values_len)
1302 ERROR ("plugin_dispatch_values: ds->type = %s: "
1303 "(ds->ds_num = %i) != "
1304 "(vl->values_len = %i)",
1305 ds->type, ds->ds_num, vl->values_len);
1310 escape_slashes (vl->host, sizeof (vl->host));
1311 escape_slashes (vl->plugin, sizeof (vl->plugin));
1312 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1313 escape_slashes (vl->type, sizeof (vl->type));
1314 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1316 /* Copy the values. This way, we can assure `targets' that they get
1317 * dynamically allocated values, which they can free and replace if
1319 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1321 saved_values = vl->values;
1322 saved_values_len = vl->values_len;
1324 vl->values = (value_t *) calloc (vl->values_len,
1325 sizeof (*vl->values));
1326 if (vl->values == NULL)
1328 ERROR ("plugin_dispatch_values: calloc failed.");
1329 vl->values = saved_values;
1332 memcpy (vl->values, saved_values,
1333 vl->values_len * sizeof (*vl->values));
1335 else /* if ((pre == NULL) && (post == NULL)) */
1337 saved_values = NULL;
1338 saved_values_len = 0;
1341 if (pre_cache_chain != NULL)
1343 status = fc_process_chain (ds, vl, pre_cache_chain);
1346 WARNING ("plugin_dispatch_values: Running the "
1347 "pre-cache chain failed with "
1351 else if (status == FC_TARGET_STOP)
1353 /* Restore the state of the value_list so that plugins
1354 * don't get confused.. */
1355 if (saved_values != NULL)
1358 vl->values = saved_values;
1359 vl->values_len = saved_values_len;
1365 /* Update the value cache */
1368 /* Initiate threshold checking */
1369 ut_check_threshold (ds, vl);
1371 if (post_cache_chain != NULL)
1373 status = fc_process_chain (ds, vl, post_cache_chain);
1376 WARNING ("plugin_dispatch_values: Running the "
1377 "post-cache chain failed with "
1383 fc_default_action (ds, vl);
1385 /* Restore the state of the value_list so that plugins don't get
1387 if (saved_values != NULL)
1390 vl->values = saved_values;
1391 vl->values_len = saved_values_len;
1394 if ((free_meta_data != 0) && (vl->meta != NULL))
1396 meta_data_destroy (vl->meta);
1401 } /* int plugin_dispatch_values */
1403 int plugin_dispatch_notification (const notification_t *notif)
1406 /* Possible TODO: Add flap detection here */
1408 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1409 "time = %u; host = %s;",
1410 notif->severity, notif->message,
1411 (unsigned int) notif->time, notif->host);
1413 /* Nobody cares for notifications */
1414 if (list_notification == NULL)
1417 le = llist_head (list_notification);
1420 callback_func_t *cf;
1421 plugin_notification_cb callback;
1425 callback = cf->cf_callback;
1426 status = (*callback) (notif, &cf->cf_udata);
1429 WARNING ("plugin_dispatch_notification: Notification "
1430 "callback %s returned %i.",
1438 } /* int plugin_dispatch_notification */
1440 void plugin_log (int level, const char *format, ...)
1446 if (list_log == NULL)
1450 if (level >= LOG_DEBUG)
1454 va_start (ap, format);
1455 vsnprintf (msg, sizeof (msg), format, ap);
1456 msg[sizeof (msg) - 1] = '\0';
1459 le = llist_head (list_log);
1462 callback_func_t *cf;
1463 plugin_log_cb callback;
1466 callback = cf->cf_callback;
1468 (*callback) (level, msg, &cf->cf_udata);
1472 } /* void plugin_log */
1474 const data_set_t *plugin_get_ds (const char *name)
1478 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1480 DEBUG ("No such dataset registered: %s", name);
1485 } /* data_set_t *plugin_get_ds */
1487 static int plugin_notification_meta_add (notification_t *n,
1489 enum notification_meta_type_e type,
1492 notification_meta_t *meta;
1493 notification_meta_t *tail;
1495 if ((n == NULL) || (name == NULL) || (value == NULL))
1497 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1501 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1504 ERROR ("plugin_notification_meta_add: malloc failed.");
1507 memset (meta, 0, sizeof (notification_meta_t));
1509 sstrncpy (meta->name, name, sizeof (meta->name));
1514 case NM_TYPE_STRING:
1516 meta->nm_value.nm_string = strdup ((const char *) value);
1517 if (meta->nm_value.nm_string == NULL)
1519 ERROR ("plugin_notification_meta_add: strdup failed.");
1525 case NM_TYPE_SIGNED_INT:
1527 meta->nm_value.nm_signed_int = *((int64_t *) value);
1530 case NM_TYPE_UNSIGNED_INT:
1532 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1535 case NM_TYPE_DOUBLE:
1537 meta->nm_value.nm_double = *((double *) value);
1540 case NM_TYPE_BOOLEAN:
1542 meta->nm_value.nm_boolean = *((bool *) value);
1547 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1551 } /* switch (type) */
1555 while ((tail != NULL) && (tail->next != NULL))
1564 } /* int plugin_notification_meta_add */
1566 int plugin_notification_meta_add_string (notification_t *n,
1570 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1573 int plugin_notification_meta_add_signed_int (notification_t *n,
1577 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1580 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1584 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1587 int plugin_notification_meta_add_double (notification_t *n,
1591 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1594 int plugin_notification_meta_add_boolean (notification_t *n,
1598 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1601 int plugin_notification_meta_copy (notification_t *dst,
1602 const notification_t *src)
1604 notification_meta_t *meta;
1606 assert (dst != NULL);
1607 assert (src != NULL);
1608 assert (dst != src);
1609 assert ((src->meta == NULL) || (src->meta != dst->meta));
1611 for (meta = src->meta; meta != NULL; meta = meta->next)
1613 if (meta->type == NM_TYPE_STRING)
1614 plugin_notification_meta_add_string (dst, meta->name,
1615 meta->nm_value.nm_string);
1616 else if (meta->type == NM_TYPE_SIGNED_INT)
1617 plugin_notification_meta_add_signed_int (dst, meta->name,
1618 meta->nm_value.nm_signed_int);
1619 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1620 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1621 meta->nm_value.nm_unsigned_int);
1622 else if (meta->type == NM_TYPE_DOUBLE)
1623 plugin_notification_meta_add_double (dst, meta->name,
1624 meta->nm_value.nm_double);
1625 else if (meta->type == NM_TYPE_BOOLEAN)
1626 plugin_notification_meta_add_boolean (dst, meta->name,
1627 meta->nm_value.nm_boolean);
1631 } /* int plugin_notification_meta_copy */
1633 int plugin_notification_meta_free (notification_meta_t *n)
1635 notification_meta_t *this;
1636 notification_meta_t *next;
1640 ERROR ("plugin_notification_meta_free: n == NULL!");
1645 while (this != NULL)
1649 if (this->type == NM_TYPE_STRING)
1651 free ((char *)this->nm_value.nm_string);
1652 this->nm_value.nm_string = NULL;
1660 } /* int plugin_notification_meta_free */
1662 /* vim: set sw=8 ts=8 noet fdm=marker : */