856ec1a01f89fbddadf744b81b7b31d5d202345b
1 /**
2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 * Sebastian Harl <sh at tokkee.org>
21 **/
23 #include "collectd.h"
24 #include "utils_complain.h"
26 #include <ltdl.h>
28 #if HAVE_PTHREAD_H
29 # include <pthread.h>
30 #endif
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_cache.h"
38 #include "utils_threshold.h"
39 #include "filter_chain.h"
41 /*
42 * Private structures
43 */
44 struct callback_func_s
45 {
46 void *cf_callback;
47 user_data_t cf_udata;
48 };
49 typedef struct callback_func_s callback_func_t;
51 #define RF_SIMPLE 0
52 #define RF_COMPLEX 1
53 struct read_func_s
54 {
55 /* `read_func_t' "inherits" from `callback_func_t'.
56 * The `rf_super' member MUST be the first one in this structure! */
57 #define rf_callback rf_super.cf_callback
58 #define rf_udata rf_super.cf_udata
59 callback_func_t rf_super;
60 int rf_type;
61 int rf_wait_time;
62 int rf_wait_left;
63 enum { DONE = 0, TODO = 1, ACTIVE = 2 } rf_needs_read;
64 };
65 typedef struct read_func_s read_func_t;
67 /*
68 * Private variables
69 */
70 static llist_t *list_init;
71 static llist_t *list_read;
72 static llist_t *list_write;
73 static llist_t *list_flush;
74 static llist_t *list_shutdown;
75 static llist_t *list_log;
76 static llist_t *list_notification;
78 static fc_chain_t *pre_cache_chain = NULL;
79 static fc_chain_t *post_cache_chain = NULL;
81 static c_avl_tree_t *data_sets;
83 static char *plugindir = NULL;
85 static int read_loop = 1;
86 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
87 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
88 static pthread_t *read_threads = NULL;
89 static int read_threads_num = 0;
91 /*
92 * Static functions
93 */
94 static const char *plugin_get_dir (void)
95 {
96 if (plugindir == NULL)
97 return (PLUGINDIR);
98 else
99 return (plugindir);
100 }
102 static void destroy_callback (callback_func_t *cf) /* {{{ */
103 {
104 if (cf == NULL)
105 return;
107 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
108 {
109 cf->cf_udata.free_func (cf->cf_udata.data);
110 cf->cf_udata.data = NULL;
111 cf->cf_udata.free_func = NULL;
112 }
113 sfree (cf);
114 } /* }}} void destroy_callback */
116 static void destroy_all_callbacks (llist_t **list) /* {{{ */
117 {
118 llentry_t *le;
120 if (*list == NULL)
121 return;
123 le = llist_head (*list);
124 while (le != NULL)
125 {
126 llentry_t *le_next;
128 le_next = le->next;
130 sfree (le->key);
131 destroy_callback (le->value);
132 le->value = NULL;
134 le = le_next;
135 }
137 llist_destroy (*list);
138 *list = NULL;
139 } /* }}} void destroy_all_callbacks */
141 static int register_callback (llist_t **list, /* {{{ */
142 const char *name, callback_func_t *cf)
143 {
144 llentry_t *le;
145 char *key;
147 if (*list == NULL)
148 {
149 *list = llist_create ();
150 if (*list == NULL)
151 {
152 ERROR ("plugin: create_register_callback: "
153 "llist_create failed.");
154 destroy_callback (cf);
155 return (-1);
156 }
157 }
159 key = strdup (name);
160 if (key == NULL)
161 {
162 ERROR ("plugin: create_register_callback: strdup failed.");
163 destroy_callback (cf);
164 return (-1);
165 }
167 le = llist_search (*list, name);
168 if (le == NULL)
169 {
170 le = llentry_create (key, cf);
171 if (le == NULL)
172 {
173 ERROR ("plugin: create_register_callback: "
174 "llentry_create failed.");
175 free (key);
176 destroy_callback (cf);
177 return (-1);
178 }
180 llist_append (*list, le);
181 }
182 else
183 {
184 callback_func_t *old_cf;
186 old_cf = le->value;
187 le->value = cf;
189 destroy_callback (old_cf);
190 sfree (key);
191 }
193 return (0);
194 } /* }}} int register_callback */
196 static int create_register_callback (llist_t **list, /* {{{ */
197 const char *name, void *callback, user_data_t *ud)
198 {
199 char *key;
200 callback_func_t *cf;
202 cf = (callback_func_t *) malloc (sizeof (*cf));
203 if (cf == NULL)
204 {
205 ERROR ("plugin: create_register_callback: malloc failed.");
206 sfree (key);
207 return (-1);
208 }
209 memset (cf, 0, sizeof (*cf));
211 cf->cf_callback = callback;
212 if (ud == NULL)
213 {
214 cf->cf_udata.data = NULL;
215 cf->cf_udata.free_func = NULL;
216 }
217 else
218 {
219 cf->cf_udata = *ud;
220 }
222 return (register_callback (list, name, cf));
223 } /* }}} int create_register_callback */
225 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
226 {
227 llentry_t *e;
229 if (list == NULL)
230 return (-1);
232 e = llist_search (list, name);
233 if (e == NULL)
234 return (-1);
236 llist_remove (list, e);
238 sfree (e->key);
239 destroy_callback (e->value);
241 llentry_destroy (e);
243 return (0);
244 } /* }}} int plugin_unregister */
246 /*
247 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
248 * object, but it will bitch about a shared object not having a
249 * ``module_register'' symbol..
250 */
251 static int plugin_load_file (char *file)
252 {
253 lt_dlhandle dlh;
254 void (*reg_handle) (void);
256 DEBUG ("file = %s", file);
258 lt_dlinit ();
259 lt_dlerror (); /* clear errors */
261 if ((dlh = lt_dlopen (file)) == NULL)
262 {
263 const char *error = lt_dlerror ();
265 ERROR ("lt_dlopen (%s) failed: %s", file, error);
266 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
267 return (1);
268 }
270 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
271 {
272 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
273 file, lt_dlerror ());
274 lt_dlclose (dlh);
275 return (-1);
276 }
278 (*reg_handle) ();
280 return (0);
281 }
283 static void *plugin_read_thread (void __attribute__((unused)) *args)
284 {
285 llentry_t *le;
286 read_func_t *rf;
287 int status;
288 int done;
290 pthread_mutex_lock (&read_lock);
292 while (read_loop != 0)
293 {
294 le = llist_head (list_read);
295 done = 0;
297 while ((read_loop != 0) && (le != NULL))
298 {
299 rf = (read_func_t *) le->value;
301 if (rf->rf_needs_read != TODO)
302 {
303 le = le->next;
304 continue;
305 }
307 /* We will do this read function */
308 rf->rf_needs_read = ACTIVE;
310 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
311 (unsigned long int) pthread_self (), le->key);
312 pthread_mutex_unlock (&read_lock);
314 if (rf->rf_type == RF_SIMPLE)
315 {
316 int (*callback) (void);
318 callback = rf->rf_callback;
319 status = (*callback) ();
320 }
321 else
322 {
323 plugin_read_cb callback;
325 callback = rf->rf_callback;
326 status = (*callback) (&rf->rf_udata);
327 }
329 done++;
331 if (status != 0)
332 {
333 if (rf->rf_wait_time < interval_g)
334 rf->rf_wait_time = interval_g;
335 rf->rf_wait_left = rf->rf_wait_time;
336 rf->rf_wait_time = rf->rf_wait_time * 2;
337 if (rf->rf_wait_time > 86400)
338 rf->rf_wait_time = 86400;
340 NOTICE ("read-function of plugin `%s' "
341 "failed. Will suspend it for %i "
342 "seconds.", le->key, rf->rf_wait_left);
343 }
344 else
345 {
346 rf->rf_wait_left = 0;
347 rf->rf_wait_time = interval_g;
348 }
350 pthread_mutex_lock (&read_lock);
352 rf->rf_needs_read = DONE;
353 le = le->next;
354 } /* while (le != NULL) */
356 if ((read_loop != 0) && (done == 0))
357 {
358 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
359 (unsigned long int) pthread_self ());
360 pthread_cond_wait (&read_cond, &read_lock);
361 }
362 } /* while (read_loop) */
364 pthread_mutex_unlock (&read_lock);
366 pthread_exit (NULL);
367 return ((void *) 0);
368 } /* void *plugin_read_thread */
370 static void start_read_threads (int num)
371 {
372 int i;
374 if (read_threads != NULL)
375 return;
377 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
378 if (read_threads == NULL)
379 {
380 ERROR ("plugin: start_read_threads: calloc failed.");
381 return;
382 }
384 read_threads_num = 0;
385 for (i = 0; i < num; i++)
386 {
387 if (pthread_create (read_threads + read_threads_num, NULL,
388 plugin_read_thread, NULL) == 0)
389 {
390 read_threads_num++;
391 }
392 else
393 {
394 ERROR ("plugin: start_read_threads: pthread_create failed.");
395 return;
396 }
397 } /* for (i) */
398 } /* void start_read_threads */
400 static void stop_read_threads (void)
401 {
402 int i;
404 if (read_threads == NULL)
405 return;
407 pthread_mutex_lock (&read_lock);
408 read_loop = 0;
409 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
410 pthread_cond_broadcast (&read_cond);
411 pthread_mutex_unlock (&read_lock);
413 for (i = 0; i < read_threads_num; i++)
414 {
415 if (pthread_join (read_threads[i], NULL) != 0)
416 {
417 ERROR ("plugin: stop_read_threads: pthread_join failed.");
418 }
419 read_threads[i] = (pthread_t) 0;
420 }
421 sfree (read_threads);
422 read_threads_num = 0;
423 } /* void stop_read_threads */
425 /*
426 * Public functions
427 */
428 void plugin_set_dir (const char *dir)
429 {
430 if (plugindir != NULL)
431 free (plugindir);
433 if (dir == NULL)
434 plugindir = NULL;
435 else if ((plugindir = strdup (dir)) == NULL)
436 {
437 char errbuf[1024];
438 ERROR ("strdup failed: %s",
439 sstrerror (errno, errbuf, sizeof (errbuf)));
440 }
441 }
443 #define BUFSIZE 512
444 int plugin_load (const char *type)
445 {
446 DIR *dh;
447 const char *dir;
448 char filename[BUFSIZE] = "";
449 char typename[BUFSIZE];
450 int typename_len;
451 int ret;
452 struct stat statbuf;
453 struct dirent *de;
454 int status;
456 DEBUG ("type = %s", type);
458 dir = plugin_get_dir ();
459 ret = 1;
461 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
462 * type when matching the filename */
463 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
464 if ((status < 0) || ((size_t) status >= sizeof (typename)))
465 {
466 WARNING ("snprintf: truncated: `%s.so'", type);
467 return (-1);
468 }
469 typename_len = strlen (typename);
471 if ((dh = opendir (dir)) == NULL)
472 {
473 char errbuf[1024];
474 ERROR ("opendir (%s): %s", dir,
475 sstrerror (errno, errbuf, sizeof (errbuf)));
476 return (-1);
477 }
479 while ((de = readdir (dh)) != NULL)
480 {
481 if (strncasecmp (de->d_name, typename, typename_len))
482 continue;
484 status = ssnprintf (filename, sizeof (filename),
485 "%s/%s", dir, de->d_name);
486 if ((status < 0) || ((size_t) status >= sizeof (filename)))
487 {
488 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
489 continue;
490 }
492 if (lstat (filename, &statbuf) == -1)
493 {
494 char errbuf[1024];
495 WARNING ("stat %s: %s", filename,
496 sstrerror (errno, errbuf, sizeof (errbuf)));
497 continue;
498 }
499 else if (!S_ISREG (statbuf.st_mode))
500 {
501 /* don't follow symlinks */
502 continue;
503 }
505 if (plugin_load_file (filename) == 0)
506 {
507 /* success */
508 ret = 0;
509 break;
510 }
511 else
512 {
513 fprintf (stderr, "Unable to load plugin %s.\n", type);
514 }
515 }
517 closedir (dh);
519 if (filename[0] == '\0')
520 fprintf (stderr, "Could not find plugin %s.\n", type);
522 return (ret);
523 }
525 /*
526 * The `register_*' functions follow
527 */
528 int plugin_register_config (const char *name,
529 int (*callback) (const char *key, const char *val),
530 const char **keys, int keys_num)
531 {
532 cf_register (name, callback, keys, keys_num);
533 return (0);
534 } /* int plugin_register_config */
536 int plugin_register_complex_config (const char *type,
537 int (*callback) (oconfig_item_t *))
538 {
539 return (cf_register_complex (type, callback));
540 } /* int plugin_register_complex_config */
542 int plugin_register_init (const char *name,
543 int (*callback) (void))
544 {
545 return (create_register_callback (&list_init, name, (void *) callback,
546 /* user_data = */ NULL));
547 } /* plugin_register_init */
549 int plugin_register_read (const char *name,
550 int (*callback) (void))
551 {
552 read_func_t *rf;
554 rf = (read_func_t *) malloc (sizeof (read_func_t));
555 if (rf == NULL)
556 {
557 char errbuf[1024];
558 ERROR ("plugin_register_read: malloc failed: %s",
559 sstrerror (errno, errbuf, sizeof (errbuf)));
560 return (-1);
561 }
563 memset (rf, 0, sizeof (read_func_t));
564 rf->rf_callback = (void *) callback;
565 rf->rf_udata.data = NULL;
566 rf->rf_udata.free_func = NULL;
567 rf->rf_wait_time = interval_g;
568 rf->rf_wait_left = 0;
569 rf->rf_type = RF_SIMPLE;
570 rf->rf_needs_read = DONE;
572 return (register_callback (&list_read, name, (callback_func_t *) rf));
573 } /* int plugin_register_read */
575 int plugin_register_complex_read (const char *name,
576 plugin_read_cb callback, user_data_t *user_data)
577 {
578 read_func_t *rf;
580 rf = (read_func_t *) malloc (sizeof (read_func_t));
581 if (rf == NULL)
582 {
583 ERROR ("plugin_register_complex_read: malloc failed.");
584 return (-1);
585 }
587 memset (rf, 0, sizeof (read_func_t));
588 rf->rf_callback = (void *) callback;
589 rf->rf_wait_time = interval_g;
590 rf->rf_wait_left = 0;
591 rf->rf_type = RF_COMPLEX;
592 rf->rf_needs_read = DONE;
594 /* Set user data */
595 if (user_data == NULL)
596 {
597 rf->rf_udata.data = NULL;
598 rf->rf_udata.free_func = NULL;
599 }
600 else
601 {
602 rf->rf_udata = *user_data;
603 }
605 return (register_callback (&list_read, name, (callback_func_t *) rf));
606 } /* int plugin_register_complex_read */
608 int plugin_register_write (const char *name,
609 plugin_write_cb callback, user_data_t *ud)
610 {
611 return (create_register_callback (&list_write, name,
612 (void *) callback, ud));
613 } /* int plugin_register_write */
615 int plugin_register_flush (const char *name,
616 plugin_flush_cb callback, user_data_t *ud)
617 {
618 return (create_register_callback (&list_flush, name,
619 (void *) callback, ud));
620 } /* int plugin_register_flush */
622 int plugin_register_shutdown (char *name,
623 int (*callback) (void))
624 {
625 return (create_register_callback (&list_shutdown, name,
626 (void *) callback, /* user_data = */ NULL));
627 } /* int plugin_register_shutdown */
629 int plugin_register_data_set (const data_set_t *ds)
630 {
631 data_set_t *ds_copy;
632 int i;
634 if ((data_sets != NULL)
635 && (c_avl_get (data_sets, ds->type, NULL) == 0))
636 {
637 NOTICE ("Replacing DS `%s' with another version.", ds->type);
638 plugin_unregister_data_set (ds->type);
639 }
640 else if (data_sets == NULL)
641 {
642 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
643 if (data_sets == NULL)
644 return (-1);
645 }
647 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
648 if (ds_copy == NULL)
649 return (-1);
650 memcpy(ds_copy, ds, sizeof (data_set_t));
652 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
653 * ds->ds_num);
654 if (ds_copy->ds == NULL)
655 {
656 free (ds_copy);
657 return (-1);
658 }
660 for (i = 0; i < ds->ds_num; i++)
661 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
663 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
664 } /* int plugin_register_data_set */
666 int plugin_register_log (const char *name,
667 plugin_log_cb callback, user_data_t *ud)
668 {
669 return (create_register_callback (&list_log, name,
670 (void *) callback, ud));
671 } /* int plugin_register_log */
673 int plugin_register_notification (const char *name,
674 plugin_notification_cb callback, user_data_t *ud)
675 {
676 return (create_register_callback (&list_notification, name,
677 (void *) callback, ud));
678 } /* int plugin_register_log */
680 int plugin_unregister_config (const char *name)
681 {
682 cf_unregister (name);
683 return (0);
684 } /* int plugin_unregister_config */
686 int plugin_unregister_complex_config (const char *name)
687 {
688 cf_unregister_complex (name);
689 return (0);
690 } /* int plugin_unregister_complex_config */
692 int plugin_unregister_init (const char *name)
693 {
694 return (plugin_unregister (list_init, name));
695 }
697 int plugin_unregister_read (const char *name)
698 {
699 return (plugin_unregister (list_read, name));
700 }
702 int plugin_unregister_write (const char *name)
703 {
704 return (plugin_unregister (list_write, name));
705 }
707 int plugin_unregister_flush (const char *name)
708 {
709 return (plugin_unregister (list_flush, name));
710 }
712 int plugin_unregister_shutdown (const char *name)
713 {
714 return (plugin_unregister (list_shutdown, name));
715 }
717 int plugin_unregister_data_set (const char *name)
718 {
719 data_set_t *ds;
721 if (data_sets == NULL)
722 return (-1);
724 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
725 return (-1);
727 sfree (ds->ds);
728 sfree (ds);
730 return (0);
731 } /* int plugin_unregister_data_set */
733 int plugin_unregister_log (const char *name)
734 {
735 return (plugin_unregister (list_log, name));
736 }
738 int plugin_unregister_notification (const char *name)
739 {
740 return (plugin_unregister (list_notification, name));
741 }
743 void plugin_init_all (void)
744 {
745 const char *chain_name;
746 llentry_t *le;
747 int status;
749 /* Init the value cache */
750 uc_init ();
752 chain_name = global_option_get ("PreCacheChain");
753 pre_cache_chain = fc_chain_get_by_name (chain_name);
755 chain_name = global_option_get ("PostCacheChain");
756 post_cache_chain = fc_chain_get_by_name (chain_name);
759 if ((list_init == NULL) && (list_read == NULL))
760 return;
762 /* Calling all init callbacks before checking if read callbacks
763 * are available allows the init callbacks to register the read
764 * callback. */
765 le = llist_head (list_init);
766 while (le != NULL)
767 {
768 callback_func_t *cf;
769 plugin_init_cb callback;
771 cf = le->value;
772 callback = cf->cf_callback;
773 status = (*callback) ();
775 if (status != 0)
776 {
777 ERROR ("Initialization of plugin `%s' "
778 "failed with status %i. "
779 "Plugin will be unloaded.",
780 le->key, status);
781 /* Plugins that register read callbacks from the init
782 * callback should take care of appropriate error
783 * handling themselves. */
784 /* FIXME: Unload _all_ functions */
785 plugin_unregister_read (le->key);
786 }
788 le = le->next;
789 }
791 /* Start read-threads */
792 if (list_read != NULL)
793 {
794 const char *rt;
795 int num;
796 rt = global_option_get ("ReadThreads");
797 num = atoi (rt);
798 if (num != -1)
799 start_read_threads ((num > 0) ? num : 5);
800 }
801 } /* void plugin_init_all */
803 void plugin_read_all (void)
804 {
805 llentry_t *le;
806 read_func_t *rf;
808 uc_check_timeout ();
810 if (list_read == NULL)
811 return;
813 pthread_mutex_lock (&read_lock);
815 le = llist_head (list_read);
816 while (le != NULL)
817 {
818 rf = (read_func_t *) le->value;
820 if (rf->rf_needs_read != DONE)
821 {
822 le = le->next;
823 continue;
824 }
826 if (rf->rf_wait_left > 0)
827 rf->rf_wait_left -= interval_g;
829 if (rf->rf_wait_left <= 0)
830 {
831 rf->rf_needs_read = TODO;
832 }
834 le = le->next;
835 }
837 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
838 pthread_cond_broadcast (&read_cond);
839 pthread_mutex_unlock (&read_lock);
840 } /* void plugin_read_all */
842 /* Read function called when the `-T' command line argument is given. */
843 int plugin_read_all_once (void)
844 {
845 llentry_t *le;
846 read_func_t *rf;
847 int status;
848 int return_status = 0;
850 if (list_read == NULL)
851 {
852 NOTICE ("No read-functions are registered.");
853 return (0);
854 }
856 for (le = llist_head (list_read);
857 le != NULL;
858 le = le->next)
859 {
860 rf = (read_func_t *) le->value;
862 if (rf->rf_type == RF_SIMPLE)
863 {
864 int (*callback) (void);
866 callback = rf->rf_callback;
867 status = (*callback) ();
868 }
869 else
870 {
871 plugin_read_cb callback;
873 callback = rf->rf_callback;
874 status = (*callback) (&rf->rf_udata);
875 }
877 if (status != 0)
878 {
879 NOTICE ("read-function of plugin `%s' failed.",
880 le->key);
881 return_status = -1;
882 }
883 }
885 return (return_status);
886 } /* int plugin_read_all_once */
888 int plugin_write (const char *plugin, /* {{{ */
889 const data_set_t *ds, const value_list_t *vl)
890 {
891 llentry_t *le;
892 int status;
894 if (vl == NULL)
895 return (EINVAL);
897 if (list_write == NULL)
898 return (ENOENT);
900 if (ds == NULL)
901 {
902 ds = plugin_get_ds (vl->type);
903 if (ds == NULL)
904 {
905 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
906 return (ENOENT);
907 }
908 }
910 if (plugin == NULL)
911 {
912 int success = 0;
913 int failure = 0;
915 le = llist_head (list_write);
916 while (le != NULL)
917 {
918 callback_func_t *cf = le->value;
919 plugin_write_cb callback;
921 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
922 callback = cf->cf_callback;
923 status = (*callback) (ds, vl, &cf->cf_udata);
924 if (status != 0)
925 failure++;
926 else
927 success++;
929 le = le->next;
930 }
932 if ((success == 0) && (failure != 0))
933 status = -1;
934 else
935 status = 0;
936 }
937 else /* plugin != NULL */
938 {
939 callback_func_t *cf;
940 plugin_write_cb callback;
942 le = llist_head (list_write);
943 while (le != NULL)
944 {
945 if (strcasecmp (plugin, le->key) == 0)
946 break;
948 le = le->next;
949 }
951 if (le == NULL)
952 return (ENOENT);
954 cf = le->value;
956 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
957 callback = cf->cf_callback;
958 status = (*callback) (ds, vl, &cf->cf_udata);
959 }
961 return (status);
962 } /* }}} int plugin_write */
964 int plugin_flush (const char *plugin, int timeout, const char *identifier)
965 {
966 llentry_t *le;
968 if (list_flush == NULL)
969 return (0);
971 le = llist_head (list_flush);
972 while (le != NULL)
973 {
974 callback_func_t *cf;
975 plugin_flush_cb callback;
977 if ((plugin != NULL)
978 && (strcmp (plugin, le->key) != 0))
979 {
980 le = le->next;
981 continue;
982 }
984 cf = le->value;
985 callback = cf->cf_callback;
987 (*callback) (timeout, identifier, &cf->cf_udata);
989 le = le->next;
990 }
991 return (0);
992 } /* int plugin_flush */
994 void plugin_shutdown_all (void)
995 {
996 llentry_t *le;
998 stop_read_threads ();
1000 destroy_all_callbacks (&list_init);
1001 destroy_all_callbacks (&list_read);
1003 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1004 /* identifier = */ NULL);
1006 le = NULL;
1007 if (list_shutdown != NULL)
1008 le = llist_head (list_shutdown);
1010 while (le != NULL)
1011 {
1012 callback_func_t *cf;
1013 plugin_shutdown_cb callback;
1015 cf = le->value;
1016 callback = cf->cf_callback;
1018 /* Advance the pointer before calling the callback allows
1019 * shutdown functions to unregister themselves. If done the
1020 * other way around the memory `le' points to will be freed
1021 * after callback returns. */
1022 le = le->next;
1024 (*callback) ();
1025 }
1027 destroy_all_callbacks (&list_write);
1028 destroy_all_callbacks (&list_flush);
1029 destroy_all_callbacks (&list_notification);
1030 destroy_all_callbacks (&list_shutdown);
1031 destroy_all_callbacks (&list_log);
1032 } /* void plugin_shutdown_all */
1034 int plugin_dispatch_values (value_list_t *vl)
1035 {
1036 int status;
1037 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1039 data_set_t *ds;
1041 if ((vl == NULL) || (*vl->type == '\0')) {
1042 ERROR ("plugin_dispatch_values: Invalid value list.");
1043 return (-1);
1044 }
1046 if (list_write == NULL)
1047 c_complain_once (LOG_WARNING, &no_write_complaint,
1048 "plugin_dispatch_values: No write callback has been "
1049 "registered. Please load at least one output plugin, "
1050 "if you want the collected data to be stored.");
1052 if (data_sets == NULL)
1053 {
1054 ERROR ("plugin_dispatch_values: No data sets registered. "
1055 "Could the types database be read? Check "
1056 "your `TypesDB' setting!");
1057 return (-1);
1058 }
1060 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1061 {
1062 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1063 return (-1);
1064 }
1066 if (vl->time == 0)
1067 vl->time = time (NULL);
1069 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1070 "host = %s; "
1071 "plugin = %s; plugin_instance = %s; "
1072 "type = %s; type_instance = %s;",
1073 (unsigned int) vl->time, vl->interval,
1074 vl->host,
1075 vl->plugin, vl->plugin_instance,
1076 vl->type, vl->type_instance);
1078 #if COLLECT_DEBUG
1079 assert (0 == strcmp (ds->type, vl->type));
1080 #else
1081 if (0 != strcmp (ds->type, vl->type))
1082 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1083 ds->type, vl->type);
1084 #endif
1086 #if COLLECT_DEBUG
1087 assert (ds->ds_num == vl->values_len);
1088 #else
1089 if (ds->ds_num != vl->values_len)
1090 {
1091 ERROR ("plugin_dispatch_values: ds->type = %s: "
1092 "(ds->ds_num = %i) != "
1093 "(vl->values_len = %i)",
1094 ds->type, ds->ds_num, vl->values_len);
1095 return (-1);
1096 }
1097 #endif
1099 escape_slashes (vl->host, sizeof (vl->host));
1100 escape_slashes (vl->plugin, sizeof (vl->plugin));
1101 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1102 escape_slashes (vl->type, sizeof (vl->type));
1103 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1105 if (pre_cache_chain != NULL)
1106 {
1107 status = fc_process_chain (ds, vl, pre_cache_chain);
1108 if (status < 0)
1109 {
1110 WARNING ("plugin_dispatch_values: Running the "
1111 "pre-cache chain failed with "
1112 "status %i (%#x).",
1113 status, status);
1114 }
1115 else if (status == FC_TARGET_STOP)
1116 return (0);
1117 }
1119 /* Update the value cache */
1120 uc_update (ds, vl);
1122 if (post_cache_chain != NULL)
1123 {
1124 status = fc_process_chain (ds, vl, post_cache_chain);
1125 if (status < 0)
1126 {
1127 WARNING ("plugin_dispatch_values: Running the "
1128 "post-cache chain failed with "
1129 "status %i (%#x).",
1130 status, status);
1131 }
1132 }
1133 else
1134 fc_default_action (ds, vl);
1136 return (0);
1137 } /* int plugin_dispatch_values */
1139 int plugin_dispatch_notification (const notification_t *notif)
1140 {
1141 llentry_t *le;
1142 /* Possible TODO: Add flap detection here */
1144 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1145 "time = %u; host = %s;",
1146 notif->severity, notif->message,
1147 (unsigned int) notif->time, notif->host);
1149 /* Nobody cares for notifications */
1150 if (list_notification == NULL)
1151 return (-1);
1153 le = llist_head (list_notification);
1154 while (le != NULL)
1155 {
1156 callback_func_t *cf;
1157 plugin_notification_cb callback;
1158 int status;
1160 cf = le->value;
1161 callback = cf->cf_callback;
1162 status = (*callback) (notif, &cf->cf_udata);
1163 if (status != 0)
1164 {
1165 WARNING ("plugin_dispatch_notification: Notification "
1166 "callback %s returned %i.",
1167 le->key, status);
1168 }
1170 le = le->next;
1171 }
1173 return (0);
1174 } /* int plugin_dispatch_notification */
1176 void plugin_log (int level, const char *format, ...)
1177 {
1178 char msg[1024];
1179 va_list ap;
1180 llentry_t *le;
1182 if (list_log == NULL)
1183 return;
1185 #if !COLLECT_DEBUG
1186 if (level >= LOG_DEBUG)
1187 return;
1188 #endif
1190 va_start (ap, format);
1191 vsnprintf (msg, sizeof (msg), format, ap);
1192 msg[sizeof (msg) - 1] = '\0';
1193 va_end (ap);
1195 le = llist_head (list_log);
1196 while (le != NULL)
1197 {
1198 callback_func_t *cf;
1199 plugin_log_cb callback;
1201 cf = le->value;
1202 callback = cf->cf_callback;
1204 (*callback) (level, msg, &cf->cf_udata);
1206 le = le->next;
1207 }
1208 } /* void plugin_log */
1210 const data_set_t *plugin_get_ds (const char *name)
1211 {
1212 data_set_t *ds;
1214 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1215 {
1216 DEBUG ("No such dataset registered: %s", name);
1217 return (NULL);
1218 }
1220 return (ds);
1221 } /* data_set_t *plugin_get_ds */
1223 static int plugin_notification_meta_add (notification_t *n,
1224 const char *name,
1225 enum notification_meta_type_e type,
1226 const void *value)
1227 {
1228 notification_meta_t *meta;
1229 notification_meta_t *tail;
1231 if ((n == NULL) || (name == NULL) || (value == NULL))
1232 {
1233 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1234 return (-1);
1235 }
1237 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1238 if (meta == NULL)
1239 {
1240 ERROR ("plugin_notification_meta_add: malloc failed.");
1241 return (-1);
1242 }
1243 memset (meta, 0, sizeof (notification_meta_t));
1245 sstrncpy (meta->name, name, sizeof (meta->name));
1246 meta->type = type;
1248 switch (type)
1249 {
1250 case NM_TYPE_STRING:
1251 {
1252 meta->nm_value.nm_string = strdup ((const char *) value);
1253 if (meta->nm_value.nm_string == NULL)
1254 {
1255 ERROR ("plugin_notification_meta_add: strdup failed.");
1256 sfree (meta);
1257 return (-1);
1258 }
1259 break;
1260 }
1261 case NM_TYPE_SIGNED_INT:
1262 {
1263 meta->nm_value.nm_signed_int = *((int64_t *) value);
1264 break;
1265 }
1266 case NM_TYPE_UNSIGNED_INT:
1267 {
1268 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1269 break;
1270 }
1271 case NM_TYPE_DOUBLE:
1272 {
1273 meta->nm_value.nm_double = *((double *) value);
1274 break;
1275 }
1276 case NM_TYPE_BOOLEAN:
1277 {
1278 meta->nm_value.nm_boolean = *((bool *) value);
1279 break;
1280 }
1281 default:
1282 {
1283 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1284 sfree (meta);
1285 return (-1);
1286 }
1287 } /* switch (type) */
1289 meta->next = NULL;
1290 tail = n->meta;
1291 while ((tail != NULL) && (tail->next != NULL))
1292 tail = tail->next;
1294 if (tail == NULL)
1295 n->meta = meta;
1296 else
1297 tail->next = meta;
1299 return (0);
1300 } /* int plugin_notification_meta_add */
1302 int plugin_notification_meta_add_string (notification_t *n,
1303 const char *name,
1304 const char *value)
1305 {
1306 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1307 }
1309 int plugin_notification_meta_add_signed_int (notification_t *n,
1310 const char *name,
1311 int64_t value)
1312 {
1313 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1314 }
1316 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1317 const char *name,
1318 uint64_t value)
1319 {
1320 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1321 }
1323 int plugin_notification_meta_add_double (notification_t *n,
1324 const char *name,
1325 double value)
1326 {
1327 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1328 }
1330 int plugin_notification_meta_add_boolean (notification_t *n,
1331 const char *name,
1332 bool value)
1333 {
1334 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1335 }
1337 int plugin_notification_meta_copy (notification_t *dst,
1338 const notification_t *src)
1339 {
1340 notification_meta_t *meta;
1342 assert (dst != NULL);
1343 assert (src != NULL);
1344 assert (dst != src);
1345 assert ((src->meta == NULL) || (src->meta != dst->meta));
1347 for (meta = src->meta; meta != NULL; meta = meta->next)
1348 {
1349 if (meta->type == NM_TYPE_STRING)
1350 plugin_notification_meta_add_string (dst, meta->name,
1351 meta->nm_value.nm_string);
1352 else if (meta->type == NM_TYPE_SIGNED_INT)
1353 plugin_notification_meta_add_signed_int (dst, meta->name,
1354 meta->nm_value.nm_signed_int);
1355 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1356 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1357 meta->nm_value.nm_unsigned_int);
1358 else if (meta->type == NM_TYPE_DOUBLE)
1359 plugin_notification_meta_add_double (dst, meta->name,
1360 meta->nm_value.nm_double);
1361 else if (meta->type == NM_TYPE_BOOLEAN)
1362 plugin_notification_meta_add_boolean (dst, meta->name,
1363 meta->nm_value.nm_boolean);
1364 }
1366 return (0);
1367 } /* int plugin_notification_meta_copy */
1369 int plugin_notification_meta_free (notification_meta_t *n)
1370 {
1371 notification_meta_t *this;
1372 notification_meta_t *next;
1374 if (n == NULL)
1375 {
1376 ERROR ("plugin_notification_meta_free: n == NULL!");
1377 return (-1);
1378 }
1380 this = n;
1381 while (this != NULL)
1382 {
1383 next = this->next;
1385 if (this->type == NM_TYPE_STRING)
1386 {
1387 free ((char *)this->nm_value.nm_string);
1388 this->nm_value.nm_string = NULL;
1389 }
1390 sfree (this);
1392 this = next;
1393 }
1395 return (0);
1396 } /* int plugin_notification_meta_free */
1398 /* vim: set sw=8 ts=8 noet fdm=marker : */