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 callback_func_t *cf;
201 cf = (callback_func_t *) malloc (sizeof (*cf));
202 if (cf == NULL)
203 {
204 ERROR ("plugin: create_register_callback: malloc failed.");
205 return (-1);
206 }
207 memset (cf, 0, sizeof (*cf));
209 cf->cf_callback = callback;
210 if (ud == NULL)
211 {
212 cf->cf_udata.data = NULL;
213 cf->cf_udata.free_func = NULL;
214 }
215 else
216 {
217 cf->cf_udata = *ud;
218 }
220 return (register_callback (list, name, cf));
221 } /* }}} int create_register_callback */
223 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
224 {
225 llentry_t *e;
227 if (list == NULL)
228 return (-1);
230 e = llist_search (list, name);
231 if (e == NULL)
232 return (-1);
234 llist_remove (list, e);
236 sfree (e->key);
237 destroy_callback (e->value);
239 llentry_destroy (e);
241 return (0);
242 } /* }}} int plugin_unregister */
244 /*
245 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
246 * object, but it will bitch about a shared object not having a
247 * ``module_register'' symbol..
248 */
249 static int plugin_load_file (char *file)
250 {
251 lt_dlhandle dlh;
252 void (*reg_handle) (void);
254 DEBUG ("file = %s", file);
256 lt_dlinit ();
257 lt_dlerror (); /* clear errors */
259 if ((dlh = lt_dlopen (file)) == NULL)
260 {
261 const char *error = lt_dlerror ();
263 ERROR ("lt_dlopen (%s) failed: %s", file, error);
264 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
265 return (1);
266 }
268 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
269 {
270 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
271 file, lt_dlerror ());
272 lt_dlclose (dlh);
273 return (-1);
274 }
276 (*reg_handle) ();
278 return (0);
279 }
281 static void *plugin_read_thread (void __attribute__((unused)) *args)
282 {
283 llentry_t *le;
284 read_func_t *rf;
285 int status;
286 int done;
288 pthread_mutex_lock (&read_lock);
290 while (read_loop != 0)
291 {
292 le = llist_head (list_read);
293 done = 0;
295 while ((read_loop != 0) && (le != NULL))
296 {
297 rf = (read_func_t *) le->value;
299 if (rf->rf_needs_read != TODO)
300 {
301 le = le->next;
302 continue;
303 }
305 /* We will do this read function */
306 rf->rf_needs_read = ACTIVE;
308 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
309 (unsigned long int) pthread_self (), le->key);
310 pthread_mutex_unlock (&read_lock);
312 if (rf->rf_type == RF_SIMPLE)
313 {
314 int (*callback) (void);
316 callback = rf->rf_callback;
317 status = (*callback) ();
318 }
319 else
320 {
321 plugin_read_cb callback;
323 callback = rf->rf_callback;
324 status = (*callback) (&rf->rf_udata);
325 }
327 done++;
329 if (status != 0)
330 {
331 if (rf->rf_wait_time < interval_g)
332 rf->rf_wait_time = interval_g;
333 rf->rf_wait_left = rf->rf_wait_time;
334 rf->rf_wait_time = rf->rf_wait_time * 2;
335 if (rf->rf_wait_time > 86400)
336 rf->rf_wait_time = 86400;
338 NOTICE ("read-function of plugin `%s' "
339 "failed. Will suspend it for %i "
340 "seconds.", le->key, rf->rf_wait_left);
341 }
342 else
343 {
344 rf->rf_wait_left = 0;
345 rf->rf_wait_time = interval_g;
346 }
348 pthread_mutex_lock (&read_lock);
350 rf->rf_needs_read = DONE;
351 le = le->next;
352 } /* while (le != NULL) */
354 if ((read_loop != 0) && (done == 0))
355 {
356 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
357 (unsigned long int) pthread_self ());
358 pthread_cond_wait (&read_cond, &read_lock);
359 }
360 } /* while (read_loop) */
362 pthread_mutex_unlock (&read_lock);
364 pthread_exit (NULL);
365 return ((void *) 0);
366 } /* void *plugin_read_thread */
368 static void start_read_threads (int num)
369 {
370 int i;
372 if (read_threads != NULL)
373 return;
375 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
376 if (read_threads == NULL)
377 {
378 ERROR ("plugin: start_read_threads: calloc failed.");
379 return;
380 }
382 read_threads_num = 0;
383 for (i = 0; i < num; i++)
384 {
385 if (pthread_create (read_threads + read_threads_num, NULL,
386 plugin_read_thread, NULL) == 0)
387 {
388 read_threads_num++;
389 }
390 else
391 {
392 ERROR ("plugin: start_read_threads: pthread_create failed.");
393 return;
394 }
395 } /* for (i) */
396 } /* void start_read_threads */
398 static void stop_read_threads (void)
399 {
400 int i;
402 if (read_threads == NULL)
403 return;
405 INFO ("collectd: Stopping %i read threads.", read_threads_num);
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 value_t *saved_values;
1040 int saved_values_len;
1042 data_set_t *ds;
1044 if ((vl == NULL) || (vl->type[0] == 0)
1045 || (vl->values == NULL) || (vl->values_len < 1))
1046 {
1047 ERROR ("plugin_dispatch_values: Invalid value list.");
1048 return (-1);
1049 }
1051 if (list_write == NULL)
1052 c_complain_once (LOG_WARNING, &no_write_complaint,
1053 "plugin_dispatch_values: No write callback has been "
1054 "registered. Please load at least one output plugin, "
1055 "if you want the collected data to be stored.");
1057 if (data_sets == NULL)
1058 {
1059 ERROR ("plugin_dispatch_values: No data sets registered. "
1060 "Could the types database be read? Check "
1061 "your `TypesDB' setting!");
1062 return (-1);
1063 }
1065 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1066 {
1067 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1068 return (-1);
1069 }
1071 if (vl->time == 0)
1072 vl->time = time (NULL);
1074 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1075 "host = %s; "
1076 "plugin = %s; plugin_instance = %s; "
1077 "type = %s; type_instance = %s;",
1078 (unsigned int) vl->time, vl->interval,
1079 vl->host,
1080 vl->plugin, vl->plugin_instance,
1081 vl->type, vl->type_instance);
1083 #if COLLECT_DEBUG
1084 assert (0 == strcmp (ds->type, vl->type));
1085 #else
1086 if (0 != strcmp (ds->type, vl->type))
1087 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1088 ds->type, vl->type);
1089 #endif
1091 #if COLLECT_DEBUG
1092 assert (ds->ds_num == vl->values_len);
1093 #else
1094 if (ds->ds_num != vl->values_len)
1095 {
1096 ERROR ("plugin_dispatch_values: ds->type = %s: "
1097 "(ds->ds_num = %i) != "
1098 "(vl->values_len = %i)",
1099 ds->type, ds->ds_num, vl->values_len);
1100 return (-1);
1101 }
1102 #endif
1104 escape_slashes (vl->host, sizeof (vl->host));
1105 escape_slashes (vl->plugin, sizeof (vl->plugin));
1106 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1107 escape_slashes (vl->type, sizeof (vl->type));
1108 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1110 /* Copy the values. This way, we can assure `targets' that they get
1111 * dynamically allocated values, which they can free and replace if
1112 * they like. */
1113 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1114 {
1115 saved_values = vl->values;
1116 saved_values_len = vl->values_len;
1118 vl->values = (value_t *) calloc (vl->values_len,
1119 sizeof (*vl->values));
1120 if (vl->values == NULL)
1121 {
1122 ERROR ("plugin_dispatch_values: calloc failed.");
1123 vl->values = saved_values;
1124 return (-1);
1125 }
1126 memcpy (vl->values, saved_values,
1127 vl->values_len * sizeof (*vl->values));
1128 }
1129 else /* if ((pre == NULL) && (post == NULL)) */
1130 {
1131 saved_values = NULL;
1132 saved_values_len = 0;
1133 }
1135 if (pre_cache_chain != NULL)
1136 {
1137 status = fc_process_chain (ds, vl, pre_cache_chain);
1138 if (status < 0)
1139 {
1140 WARNING ("plugin_dispatch_values: Running the "
1141 "pre-cache chain failed with "
1142 "status %i (%#x).",
1143 status, status);
1144 }
1145 else if (status == FC_TARGET_STOP)
1146 {
1147 /* Restore the state of the value_list so that plugins
1148 * don't get confused.. */
1149 if (saved_values != NULL)
1150 {
1151 free (vl->values);
1152 vl->values = saved_values;
1153 vl->values_len = saved_values_len;
1154 }
1155 return (0);
1156 }
1157 }
1159 /* Update the value cache */
1160 uc_update (ds, vl);
1162 if (post_cache_chain != NULL)
1163 {
1164 status = fc_process_chain (ds, vl, post_cache_chain);
1165 if (status < 0)
1166 {
1167 WARNING ("plugin_dispatch_values: Running the "
1168 "post-cache chain failed with "
1169 "status %i (%#x).",
1170 status, status);
1171 }
1172 }
1173 else
1174 fc_default_action (ds, vl);
1176 /* Restore the state of the value_list so that plugins don't get
1177 * confused.. */
1178 if (saved_values != NULL)
1179 {
1180 free (vl->values);
1181 vl->values = saved_values;
1182 vl->values_len = saved_values_len;
1183 }
1185 return (0);
1186 } /* int plugin_dispatch_values */
1188 int plugin_dispatch_notification (const notification_t *notif)
1189 {
1190 llentry_t *le;
1191 /* Possible TODO: Add flap detection here */
1193 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1194 "time = %u; host = %s;",
1195 notif->severity, notif->message,
1196 (unsigned int) notif->time, notif->host);
1198 /* Nobody cares for notifications */
1199 if (list_notification == NULL)
1200 return (-1);
1202 le = llist_head (list_notification);
1203 while (le != NULL)
1204 {
1205 callback_func_t *cf;
1206 plugin_notification_cb callback;
1207 int status;
1209 cf = le->value;
1210 callback = cf->cf_callback;
1211 status = (*callback) (notif, &cf->cf_udata);
1212 if (status != 0)
1213 {
1214 WARNING ("plugin_dispatch_notification: Notification "
1215 "callback %s returned %i.",
1216 le->key, status);
1217 }
1219 le = le->next;
1220 }
1222 return (0);
1223 } /* int plugin_dispatch_notification */
1225 void plugin_log (int level, const char *format, ...)
1226 {
1227 char msg[1024];
1228 va_list ap;
1229 llentry_t *le;
1231 if (list_log == NULL)
1232 return;
1234 #if !COLLECT_DEBUG
1235 if (level >= LOG_DEBUG)
1236 return;
1237 #endif
1239 va_start (ap, format);
1240 vsnprintf (msg, sizeof (msg), format, ap);
1241 msg[sizeof (msg) - 1] = '\0';
1242 va_end (ap);
1244 le = llist_head (list_log);
1245 while (le != NULL)
1246 {
1247 callback_func_t *cf;
1248 plugin_log_cb callback;
1250 cf = le->value;
1251 callback = cf->cf_callback;
1253 (*callback) (level, msg, &cf->cf_udata);
1255 le = le->next;
1256 }
1257 } /* void plugin_log */
1259 const data_set_t *plugin_get_ds (const char *name)
1260 {
1261 data_set_t *ds;
1263 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1264 {
1265 DEBUG ("No such dataset registered: %s", name);
1266 return (NULL);
1267 }
1269 return (ds);
1270 } /* data_set_t *plugin_get_ds */
1272 static int plugin_notification_meta_add (notification_t *n,
1273 const char *name,
1274 enum notification_meta_type_e type,
1275 const void *value)
1276 {
1277 notification_meta_t *meta;
1278 notification_meta_t *tail;
1280 if ((n == NULL) || (name == NULL) || (value == NULL))
1281 {
1282 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1283 return (-1);
1284 }
1286 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1287 if (meta == NULL)
1288 {
1289 ERROR ("plugin_notification_meta_add: malloc failed.");
1290 return (-1);
1291 }
1292 memset (meta, 0, sizeof (notification_meta_t));
1294 sstrncpy (meta->name, name, sizeof (meta->name));
1295 meta->type = type;
1297 switch (type)
1298 {
1299 case NM_TYPE_STRING:
1300 {
1301 meta->nm_value.nm_string = strdup ((const char *) value);
1302 if (meta->nm_value.nm_string == NULL)
1303 {
1304 ERROR ("plugin_notification_meta_add: strdup failed.");
1305 sfree (meta);
1306 return (-1);
1307 }
1308 break;
1309 }
1310 case NM_TYPE_SIGNED_INT:
1311 {
1312 meta->nm_value.nm_signed_int = *((int64_t *) value);
1313 break;
1314 }
1315 case NM_TYPE_UNSIGNED_INT:
1316 {
1317 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1318 break;
1319 }
1320 case NM_TYPE_DOUBLE:
1321 {
1322 meta->nm_value.nm_double = *((double *) value);
1323 break;
1324 }
1325 case NM_TYPE_BOOLEAN:
1326 {
1327 meta->nm_value.nm_boolean = *((bool *) value);
1328 break;
1329 }
1330 default:
1331 {
1332 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1333 sfree (meta);
1334 return (-1);
1335 }
1336 } /* switch (type) */
1338 meta->next = NULL;
1339 tail = n->meta;
1340 while ((tail != NULL) && (tail->next != NULL))
1341 tail = tail->next;
1343 if (tail == NULL)
1344 n->meta = meta;
1345 else
1346 tail->next = meta;
1348 return (0);
1349 } /* int plugin_notification_meta_add */
1351 int plugin_notification_meta_add_string (notification_t *n,
1352 const char *name,
1353 const char *value)
1354 {
1355 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1356 }
1358 int plugin_notification_meta_add_signed_int (notification_t *n,
1359 const char *name,
1360 int64_t value)
1361 {
1362 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1363 }
1365 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1366 const char *name,
1367 uint64_t value)
1368 {
1369 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1370 }
1372 int plugin_notification_meta_add_double (notification_t *n,
1373 const char *name,
1374 double value)
1375 {
1376 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1377 }
1379 int plugin_notification_meta_add_boolean (notification_t *n,
1380 const char *name,
1381 bool value)
1382 {
1383 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1384 }
1386 int plugin_notification_meta_copy (notification_t *dst,
1387 const notification_t *src)
1388 {
1389 notification_meta_t *meta;
1391 assert (dst != NULL);
1392 assert (src != NULL);
1393 assert (dst != src);
1394 assert ((src->meta == NULL) || (src->meta != dst->meta));
1396 for (meta = src->meta; meta != NULL; meta = meta->next)
1397 {
1398 if (meta->type == NM_TYPE_STRING)
1399 plugin_notification_meta_add_string (dst, meta->name,
1400 meta->nm_value.nm_string);
1401 else if (meta->type == NM_TYPE_SIGNED_INT)
1402 plugin_notification_meta_add_signed_int (dst, meta->name,
1403 meta->nm_value.nm_signed_int);
1404 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1405 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1406 meta->nm_value.nm_unsigned_int);
1407 else if (meta->type == NM_TYPE_DOUBLE)
1408 plugin_notification_meta_add_double (dst, meta->name,
1409 meta->nm_value.nm_double);
1410 else if (meta->type == NM_TYPE_BOOLEAN)
1411 plugin_notification_meta_add_boolean (dst, meta->name,
1412 meta->nm_value.nm_boolean);
1413 }
1415 return (0);
1416 } /* int plugin_notification_meta_copy */
1418 int plugin_notification_meta_free (notification_meta_t *n)
1419 {
1420 notification_meta_t *this;
1421 notification_meta_t *next;
1423 if (n == NULL)
1424 {
1425 ERROR ("plugin_notification_meta_free: n == NULL!");
1426 return (-1);
1427 }
1429 this = n;
1430 while (this != NULL)
1431 {
1432 next = this->next;
1434 if (this->type == NM_TYPE_STRING)
1435 {
1436 free ((char *)this->nm_value.nm_string);
1437 this->nm_value.nm_string = NULL;
1438 }
1439 sfree (this);
1441 this = next;
1442 }
1444 return (0);
1445 } /* int plugin_notification_meta_free */
1447 /* vim: set sw=8 ts=8 noet fdm=marker : */