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 read_func_s
45 {
46 int wait_time;
47 int wait_left;
48 int (*callback) (void);
49 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
50 };
51 typedef struct read_func_s read_func_t;
53 struct dispatch_queue_s;
54 typedef struct dispatch_queue_s dispatch_queue_t;
55 struct dispatch_queue_s
56 {
57 value_list_t *vl;
58 dispatch_queue_t *next;
59 };
61 /*
62 * Private variables
63 */
64 static llist_t *list_init;
65 static llist_t *list_read;
66 static llist_t *list_write;
67 static llist_t *list_flush;
68 static llist_t *list_shutdown;
69 static llist_t *list_log;
70 static llist_t *list_notification;
72 static fc_chain_t *pre_cache_chain = NULL;
73 static fc_chain_t *post_cache_chain = NULL;
75 static c_avl_tree_t *data_sets;
77 static char *plugindir = NULL;
79 static int read_loop = 0;
80 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
81 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
82 static pthread_t *read_threads = NULL;
83 static size_t read_threads_num = 0;
85 static int dispatch_loop = 0;
86 static pthread_mutex_t dispatch_lock = PTHREAD_MUTEX_INITIALIZER;
87 static pthread_cond_t dispatch_cond = PTHREAD_COND_INITIALIZER;
88 static pthread_t *dispatch_threads = NULL;
89 static size_t dispatch_threads_num = 0;
90 static dispatch_queue_t *dispatch_head;
91 static dispatch_queue_t *dispatch_tail;
93 /*
94 * Static functions
95 */
96 static const char *plugin_get_dir (void)
97 {
98 if (plugindir == NULL)
99 return (PLUGINDIR);
100 else
101 return (plugindir);
102 }
104 static int register_callback (llist_t **list, const char *name, void *callback)
105 {
106 llentry_t *le;
107 char *key;
109 if ((*list == NULL)
110 && ((*list = llist_create ()) == NULL))
111 return (-1);
113 le = llist_search (*list, name);
114 if (le == NULL)
115 {
116 key = strdup (name);
117 if (key == NULL)
118 return (-1);
120 le = llentry_create (key, callback);
121 if (le == NULL)
122 {
123 free (key);
124 return (-1);
125 }
127 llist_append (*list, le);
128 }
129 else
130 {
131 le->value = callback;
132 }
134 return (0);
135 } /* int register_callback */
137 static int plugin_unregister (llist_t *list, const char *name)
138 {
139 llentry_t *e;
141 e = llist_search (list, name);
143 if (e == NULL)
144 return (-1);
146 llist_remove (list, e);
147 free (e->key);
148 llentry_destroy (e);
150 return (0);
151 } /* int plugin_unregister */
153 /*
154 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
155 * object, but it will bitch about a shared object not having a
156 * ``module_register'' symbol..
157 */
158 static int plugin_load_file (char *file)
159 {
160 lt_dlhandle dlh;
161 void (*reg_handle) (void);
163 DEBUG ("file = %s", file);
165 lt_dlinit ();
166 lt_dlerror (); /* clear errors */
168 if ((dlh = lt_dlopen (file)) == NULL)
169 {
170 const char *error = lt_dlerror ();
172 ERROR ("lt_dlopen (%s) failed: %s", file, error);
173 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
174 return (1);
175 }
177 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
178 {
179 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
180 file, lt_dlerror ());
181 lt_dlclose (dlh);
182 return (-1);
183 }
185 (*reg_handle) ();
187 return (0);
188 }
190 static void *plugin_read_thread (void __attribute__((unused)) *args)
191 {
192 llentry_t *le;
193 read_func_t *rf;
194 int status;
195 int done;
197 pthread_mutex_lock (&read_lock);
199 while (read_loop != 0)
200 {
201 le = llist_head (list_read);
202 done = 0;
204 while ((read_loop != 0) && (le != NULL))
205 {
206 rf = (read_func_t *) le->value;
208 if (rf->needs_read != TODO)
209 {
210 le = le->next;
211 continue;
212 }
214 /* We will do this read function */
215 rf->needs_read = ACTIVE;
217 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
218 (unsigned long int) pthread_self (), le->key);
219 pthread_mutex_unlock (&read_lock);
221 status = rf->callback ();
222 done++;
224 if (status != 0)
225 {
226 if (rf->wait_time < interval_g)
227 rf->wait_time = interval_g;
228 rf->wait_left = rf->wait_time;
229 rf->wait_time = rf->wait_time * 2;
230 if (rf->wait_time > 86400)
231 rf->wait_time = 86400;
233 NOTICE ("read-function of plugin `%s' "
234 "failed. Will suspend it for %i "
235 "seconds.", le->key, rf->wait_left);
236 }
237 else
238 {
239 rf->wait_left = 0;
240 rf->wait_time = interval_g;
241 }
243 pthread_mutex_lock (&read_lock);
245 rf->needs_read = DONE;
246 le = le->next;
247 } /* while (le != NULL) */
249 if ((read_loop != 0) && (done == 0))
250 {
251 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
252 (unsigned long int) pthread_self ());
253 pthread_cond_wait (&read_cond, &read_lock);
254 }
255 } /* while (read_loop) */
257 pthread_mutex_unlock (&read_lock);
259 pthread_exit (NULL);
260 return ((void *) 0);
261 } /* void *plugin_read_thread */
263 static void *plugin_dispatch_thread (void *arg)
264 {
265 pthread_mutex_lock (&dispatch_lock);
267 while ((dispatch_loop != 0) || (dispatch_head != NULL))
268 {
269 dispatch_queue_t *qi;
271 if (dispatch_head == NULL)
272 pthread_cond_wait (&dispatch_cond, &dispatch_lock);
274 if (dispatch_head == NULL)
275 continue;
277 qi = dispatch_head;
279 if (dispatch_head == dispatch_tail)
280 {
281 dispatch_head = NULL;
282 dispatch_tail = NULL;
283 }
284 else
285 {
286 dispatch_head = qi->next;
287 }
289 pthread_mutex_unlock (&dispatch_lock);
291 plugin_dispatch_values (qi->vl);
292 sfree (qi->vl->values);
293 sfree (qi->vl);
294 sfree (qi);
296 pthread_mutex_lock (&dispatch_lock);
297 } /* while */
299 pthread_mutex_unlock (&dispatch_lock);
301 return ((void *) 0);
302 } /* void *plugin_dispatch_thread */
304 static int plugin_start_threads (size_t num,
305 pthread_t **tlist, size_t *tlist_num,
306 void *(*thread_main) (void *))
307 {
308 size_t i;
309 pthread_t *new_tlist;
310 size_t new_tlist_num;
312 if (*tlist != NULL)
313 return (-1);
315 new_tlist = (pthread_t *) calloc (num, sizeof (*new_tlist));
316 if (new_tlist == NULL)
317 {
318 ERROR ("plugin: plugin_start_threads: calloc failed.");
319 return (-1);
320 }
322 new_tlist_num = 0;
323 for (i = 0; i < num; i++)
324 {
325 int status;
327 status = pthread_create (new_tlist + new_tlist_num,
328 /* attr = */ NULL, thread_main,
329 /* arg = */ (void *) 0);
330 if (status != 0)
331 {
332 ERROR ("plugin: plugin_start_threads: "
333 "pthread_create failed.");
334 continue;
335 }
337 new_tlist_num++;
338 } /* for (i) */
340 if (new_tlist_num < 1)
341 {
342 ERROR ("plugin: plugin_start_threads: "
343 "Creating threads failed.");
344 sfree (new_tlist);
345 return (-1);
346 }
348 *tlist = new_tlist;
349 *tlist_num = new_tlist_num;
351 return (0);
352 } /* int plugin_start_threads */
354 static int plugin_stop_threads (int *loop,
355 pthread_mutex_t *lock, pthread_cond_t *cond,
356 pthread_t **ret_tlist, size_t *ret_tlist_len)
357 {
358 pthread_t *tlist;
359 size_t tlist_len;
360 size_t i;
362 tlist = *ret_tlist;
363 tlist_len = *ret_tlist_len;
365 if (tlist == NULL)
366 return (0);
368 DEBUG ("plugin_stop_threads: Stopping %zu threads.", tlist_len);
370 pthread_mutex_lock (lock);
372 *loop = 0;
374 pthread_cond_broadcast (cond);
375 pthread_mutex_unlock (lock);
377 for (i = 0; i < tlist_len; i++)
378 {
379 int status;
381 status = pthread_join (tlist[i], NULL);
382 if (status != 0)
383 {
384 ERROR ("plugin_stop_threads: pthread_join failed.");
385 }
386 }
388 sfree (tlist);
390 *ret_tlist = NULL;
391 *ret_tlist_len = 0;
393 return (0);
394 } /* int plugin_stop_threads */
396 /*
397 * Public functions
398 */
399 void plugin_set_dir (const char *dir)
400 {
401 if (plugindir != NULL)
402 free (plugindir);
404 if (dir == NULL)
405 plugindir = NULL;
406 else if ((plugindir = strdup (dir)) == NULL)
407 {
408 char errbuf[1024];
409 ERROR ("strdup failed: %s",
410 sstrerror (errno, errbuf, sizeof (errbuf)));
411 }
412 }
414 #define BUFSIZE 512
415 int plugin_load (const char *type)
416 {
417 DIR *dh;
418 const char *dir;
419 char filename[BUFSIZE] = "";
420 char typename[BUFSIZE];
421 int typename_len;
422 int ret;
423 struct stat statbuf;
424 struct dirent *de;
425 int status;
427 DEBUG ("type = %s", type);
429 dir = plugin_get_dir ();
430 ret = 1;
432 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
433 * type when matching the filename */
434 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
435 if ((status < 0) || ((size_t) status >= sizeof (typename)))
436 {
437 WARNING ("snprintf: truncated: `%s.so'", type);
438 return (-1);
439 }
440 typename_len = strlen (typename);
442 if ((dh = opendir (dir)) == NULL)
443 {
444 char errbuf[1024];
445 ERROR ("opendir (%s): %s", dir,
446 sstrerror (errno, errbuf, sizeof (errbuf)));
447 return (-1);
448 }
450 while ((de = readdir (dh)) != NULL)
451 {
452 if (strncasecmp (de->d_name, typename, typename_len))
453 continue;
455 status = ssnprintf (filename, sizeof (filename),
456 "%s/%s", dir, de->d_name);
457 if ((status < 0) || ((size_t) status >= sizeof (filename)))
458 {
459 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
460 continue;
461 }
463 if (lstat (filename, &statbuf) == -1)
464 {
465 char errbuf[1024];
466 WARNING ("stat %s: %s", filename,
467 sstrerror (errno, errbuf, sizeof (errbuf)));
468 continue;
469 }
470 else if (!S_ISREG (statbuf.st_mode))
471 {
472 /* don't follow symlinks */
473 continue;
474 }
476 if (plugin_load_file (filename) == 0)
477 {
478 /* success */
479 ret = 0;
480 break;
481 }
482 else
483 {
484 fprintf (stderr, "Unable to load plugin %s.\n", type);
485 }
486 }
488 closedir (dh);
490 if (filename[0] == '\0')
491 fprintf (stderr, "Could not find plugin %s.\n", type);
493 return (ret);
494 }
496 /*
497 * The `register_*' functions follow
498 */
499 int plugin_register_config (const char *name,
500 int (*callback) (const char *key, const char *val),
501 const char **keys, int keys_num)
502 {
503 cf_register (name, callback, keys, keys_num);
504 return (0);
505 } /* int plugin_register_config */
507 int plugin_register_complex_config (const char *type,
508 int (*callback) (oconfig_item_t *))
509 {
510 return (cf_register_complex (type, callback));
511 } /* int plugin_register_complex_config */
513 int plugin_register_init (const char *name,
514 int (*callback) (void))
515 {
516 return (register_callback (&list_init, name, (void *) callback));
517 } /* plugin_register_init */
519 int plugin_register_read (const char *name,
520 int (*callback) (void))
521 {
522 read_func_t *rf;
524 rf = (read_func_t *) malloc (sizeof (read_func_t));
525 if (rf == NULL)
526 {
527 char errbuf[1024];
528 ERROR ("plugin_register_read: malloc failed: %s",
529 sstrerror (errno, errbuf, sizeof (errbuf)));
530 return (-1);
531 }
533 memset (rf, '\0', sizeof (read_func_t));
534 rf->wait_time = interval_g;
535 rf->wait_left = 0;
536 rf->callback = callback;
537 rf->needs_read = DONE;
539 return (register_callback (&list_read, name, (void *) rf));
540 } /* int plugin_register_read */
542 int plugin_register_write (const char *name,
543 int (*callback) (const data_set_t *ds, const value_list_t *vl))
544 {
545 return (register_callback (&list_write, name, (void *) callback));
546 } /* int plugin_register_write */
548 int plugin_register_flush (const char *name,
549 int (*callback) (const int timeout, const char *identifier))
550 {
551 return (register_callback (&list_flush, name, (void *) callback));
552 } /* int plugin_register_flush */
554 int plugin_register_shutdown (char *name,
555 int (*callback) (void))
556 {
557 return (register_callback (&list_shutdown, name, (void *) callback));
558 } /* int plugin_register_shutdown */
560 int plugin_register_data_set (const data_set_t *ds)
561 {
562 data_set_t *ds_copy;
563 int i;
565 if ((data_sets != NULL)
566 && (c_avl_get (data_sets, ds->type, NULL) == 0))
567 {
568 NOTICE ("Replacing DS `%s' with another version.", ds->type);
569 plugin_unregister_data_set (ds->type);
570 }
571 else if (data_sets == NULL)
572 {
573 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
574 if (data_sets == NULL)
575 return (-1);
576 }
578 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
579 if (ds_copy == NULL)
580 return (-1);
581 memcpy(ds_copy, ds, sizeof (data_set_t));
583 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
584 * ds->ds_num);
585 if (ds_copy->ds == NULL)
586 {
587 free (ds_copy);
588 return (-1);
589 }
591 for (i = 0; i < ds->ds_num; i++)
592 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
594 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
595 } /* int plugin_register_data_set */
597 int plugin_register_log (char *name,
598 void (*callback) (int priority, const char *msg))
599 {
600 return (register_callback (&list_log, name, (void *) callback));
601 } /* int plugin_register_log */
603 int plugin_register_notification (const char *name,
604 int (*callback) (const notification_t *notif))
605 {
606 return (register_callback (&list_notification, name, (void *) callback));
607 } /* int plugin_register_log */
609 int plugin_unregister_config (const char *name)
610 {
611 cf_unregister (name);
612 return (0);
613 } /* int plugin_unregister_config */
615 int plugin_unregister_complex_config (const char *name)
616 {
617 cf_unregister_complex (name);
618 return (0);
619 } /* int plugin_unregister_complex_config */
621 int plugin_unregister_init (const char *name)
622 {
623 return (plugin_unregister (list_init, name));
624 }
626 int plugin_unregister_read (const char *name)
627 {
628 llentry_t *e;
630 e = llist_search (list_read, name);
632 if (e == NULL)
633 return (-1);
635 llist_remove (list_read, e);
636 free (e->value);
637 free (e->key);
638 llentry_destroy (e);
640 return (0);
641 }
643 int plugin_unregister_write (const char *name)
644 {
645 return (plugin_unregister (list_write, name));
646 }
648 int plugin_unregister_flush (const char *name)
649 {
650 return (plugin_unregister (list_flush, name));
651 }
653 int plugin_unregister_shutdown (const char *name)
654 {
655 return (plugin_unregister (list_shutdown, name));
656 }
658 int plugin_unregister_data_set (const char *name)
659 {
660 data_set_t *ds;
662 if (data_sets == NULL)
663 return (-1);
665 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
666 return (-1);
668 sfree (ds->ds);
669 sfree (ds);
671 return (0);
672 } /* int plugin_unregister_data_set */
674 int plugin_unregister_log (const char *name)
675 {
676 return (plugin_unregister (list_log, name));
677 }
679 int plugin_unregister_notification (const char *name)
680 {
681 return (plugin_unregister (list_notification, name));
682 }
684 void plugin_init_all (void)
685 {
686 const char *chain_name;
687 int (*callback) (void);
688 llentry_t *le;
689 int status;
691 /* Init the value cache */
692 uc_init ();
694 chain_name = global_option_get ("PreCacheChain");
695 pre_cache_chain = fc_chain_get_by_name (chain_name);
697 chain_name = global_option_get ("PostCacheChain");
698 post_cache_chain = fc_chain_get_by_name (chain_name);
701 if ((list_init == NULL) && (list_read == NULL))
702 return;
704 /* Calling all init callbacks before checking if read callbacks
705 * are available allows the init callbacks to register the read
706 * callback. */
707 le = llist_head (list_init);
708 while (le != NULL)
709 {
710 callback = (int (*) (void)) le->value;
711 status = (*callback) ();
713 if (status != 0)
714 {
715 ERROR ("Initialization of plugin `%s' "
716 "failed with status %i. "
717 "Plugin will be unloaded.",
718 le->key, status);
719 /* Plugins that register read callbacks from the init
720 * callback should take care of appropriate error
721 * handling themselves. */
722 /* FIXME: Unload _all_ functions */
723 plugin_unregister_read (le->key);
724 }
726 le = le->next;
727 }
729 /* Start read-threads */
730 if (list_read != NULL)
731 {
732 const char *rt;
733 int num;
734 rt = global_option_get ("ReadThreads");
735 num = atoi (rt);
736 if (num != -1)
737 {
738 read_loop = 1;
739 plugin_start_threads ((num > 0) ? ((size_t ) num) : 5,
740 &read_threads, &read_threads_num,
741 plugin_read_thread);
742 }
743 }
745 dispatch_loop = 1;
746 plugin_start_threads (/* num = */ 1, /* FIXME: Make this number configurable */
747 &dispatch_threads, &dispatch_threads_num,
748 plugin_dispatch_thread);
749 } /* void plugin_init_all */
751 void plugin_read_all (void)
752 {
753 llentry_t *le;
754 read_func_t *rf;
756 uc_check_timeout ();
758 if (list_read == NULL)
759 return;
761 pthread_mutex_lock (&read_lock);
763 le = llist_head (list_read);
764 while (le != NULL)
765 {
766 rf = (read_func_t *) le->value;
768 if (rf->needs_read != DONE)
769 {
770 le = le->next;
771 continue;
772 }
774 if (rf->wait_left > 0)
775 rf->wait_left -= interval_g;
777 if (rf->wait_left <= 0)
778 {
779 rf->needs_read = TODO;
780 }
782 le = le->next;
783 }
785 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
786 pthread_cond_broadcast (&read_cond);
787 pthread_mutex_unlock (&read_lock);
788 } /* void plugin_read_all */
790 /* Read function called when the `-T' command line argument is given. */
791 int plugin_read_all_once (void)
792 {
793 llentry_t *le;
794 read_func_t *rf;
795 int status;
796 int return_status = 0;
798 if (list_read == NULL)
799 {
800 NOTICE ("No read-functions are registered.");
801 return (0);
802 }
804 for (le = llist_head (list_read);
805 le != NULL;
806 le = le->next)
807 {
808 rf = (read_func_t *) le->value;
809 status = rf->callback ();
810 if (status != 0)
811 {
812 NOTICE ("read-function of plugin `%s' failed.",
813 le->key);
814 return_status = -1;
815 }
816 }
818 return (return_status);
819 } /* int plugin_read_all_once */
821 int plugin_write (const char *plugin, /* {{{ */
822 const data_set_t *ds, const value_list_t *vl)
823 {
824 int (*callback) (const data_set_t *ds, const value_list_t *vl);
825 llentry_t *le;
826 int status;
828 if (vl == NULL)
829 return (EINVAL);
831 if (list_write == NULL)
832 return (ENOENT);
834 if (ds == NULL)
835 {
836 ds = plugin_get_ds (vl->type);
837 if (ds == NULL)
838 {
839 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
840 return (ENOENT);
841 }
842 }
844 if (plugin == NULL)
845 {
846 int success = 0;
847 int failure = 0;
849 le = llist_head (list_write);
850 while (le != NULL)
851 {
852 callback = le->value;
853 status = (*callback) (ds, vl);
854 if (status != 0)
855 failure++;
856 else
857 success++;
859 le = le->next;
860 }
862 if ((success == 0) && (failure != 0))
863 status = -1;
864 else
865 status = 0;
866 }
867 else /* plugin != NULL */
868 {
869 le = llist_head (list_write);
870 while (le != NULL)
871 {
872 if (strcasecmp (plugin, le->key) == 0)
873 break;
875 le = le->next;
876 }
878 if (le == NULL)
879 return (ENOENT);
881 callback = le->value;
882 status = (*callback) (ds, vl);
883 }
885 return (status);
886 } /* }}} int plugin_write */
888 int plugin_flush (const char *plugin, int timeout, const char *identifier)
889 {
890 int (*callback) (int timeout, const char *identifier);
891 llentry_t *le;
893 if (list_flush == NULL)
894 return (0);
896 le = llist_head (list_flush);
897 while (le != NULL)
898 {
899 if ((plugin != NULL)
900 && (strcmp (plugin, le->key) != 0))
901 {
902 le = le->next;
903 continue;
904 }
906 callback = (int (*) (int, const char *)) le->value;
907 (*callback) (timeout, identifier);
909 le = le->next;
910 }
911 return (0);
912 } /* int plugin_flush */
914 void plugin_shutdown_all (void)
915 {
916 int (*callback) (void);
917 llentry_t *le;
919 plugin_stop_threads (&read_loop, &read_lock, &read_cond,
920 &read_threads, &read_threads_num);
922 plugin_stop_threads (&dispatch_loop, &dispatch_lock, &dispatch_cond,
923 &dispatch_threads, &dispatch_threads_num);
925 if (list_shutdown == NULL)
926 return;
928 le = llist_head (list_shutdown);
929 while (le != NULL)
930 {
931 callback = (int (*) (void)) le->value;
933 /* Advance the pointer before calling the callback allows
934 * shutdown functions to unregister themselves. If done the
935 * other way around the memory `le' points to will be freed
936 * after callback returns. */
937 le = le->next;
939 (*callback) ();
940 }
941 } /* void plugin_shutdown_all */
943 int plugin_dispatch_values (value_list_t *vl)
944 {
945 int status;
946 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
948 data_set_t *ds;
950 if ((vl == NULL) || (*vl->type == '\0')) {
951 ERROR ("plugin_dispatch_values: Invalid value list.");
952 return (-1);
953 }
955 if (list_write == NULL)
956 c_complain_once (LOG_WARNING, &no_write_complaint,
957 "plugin_dispatch_values: No write callback has been "
958 "registered. Please load at least one output plugin, "
959 "if you want the collected data to be stored.");
961 if (data_sets == NULL)
962 {
963 ERROR ("plugin_dispatch_values: No data sets registered. "
964 "Could the types database be read? Check "
965 "your `TypesDB' setting!");
966 return (-1);
967 }
969 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
970 {
971 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
972 return (-1);
973 }
975 if (vl->time == 0)
976 vl->time = time (NULL);
978 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
979 "host = %s; "
980 "plugin = %s; plugin_instance = %s; "
981 "type = %s; type_instance = %s;",
982 (unsigned int) vl->time, vl->interval,
983 vl->host,
984 vl->plugin, vl->plugin_instance,
985 vl->type, vl->type_instance);
987 #if COLLECT_DEBUG
988 assert (0 == strcmp (ds->type, vl->type));
989 #else
990 if (0 != strcmp (ds->type, vl->type))
991 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
992 ds->type, vl->type);
993 #endif
995 #if COLLECT_DEBUG
996 assert (ds->ds_num == vl->values_len);
997 #else
998 if (ds->ds_num != vl->values_len)
999 {
1000 ERROR ("plugin_dispatch_values: ds->type = %s: "
1001 "(ds->ds_num = %i) != "
1002 "(vl->values_len = %i)",
1003 ds->type, ds->ds_num, vl->values_len);
1004 return (-1);
1005 }
1006 #endif
1008 escape_slashes (vl->host, sizeof (vl->host));
1009 escape_slashes (vl->plugin, sizeof (vl->plugin));
1010 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1011 escape_slashes (vl->type, sizeof (vl->type));
1012 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1014 if (pre_cache_chain != NULL)
1015 {
1016 status = fc_process_chain (ds, vl, pre_cache_chain);
1017 if (status < 0)
1018 {
1019 WARNING ("plugin_dispatch_values: Running the "
1020 "pre-cache chain failed with "
1021 "status %i (%#x).",
1022 status, status);
1023 }
1024 else if (status == FC_TARGET_STOP)
1025 return (0);
1026 }
1028 /* Update the value cache */
1029 uc_update (ds, vl);
1031 if (post_cache_chain != NULL)
1032 {
1033 status = fc_process_chain (ds, vl, post_cache_chain);
1034 if (status < 0)
1035 {
1036 WARNING ("plugin_dispatch_values: Running the "
1037 "post-cache chain failed with "
1038 "status %i (%#x).",
1039 status, status);
1040 }
1041 }
1042 else
1043 fc_default_action (ds, vl);
1045 return (0);
1046 } /* int plugin_dispatch_values */
1048 int plugin_dispatch_values_async (const value_list_t *vl)
1049 {
1050 dispatch_queue_t *qi;
1051 int i;
1053 if (vl == NULL)
1054 return (-EINVAL);
1056 if (dispatch_threads_num < 1)
1057 {
1058 ERROR ("plugin_dispatch_values_async: "
1059 "No dispatch threads have been started!");
1060 #ifdef ENOTCONN
1061 return (-ENOTCONN);
1062 #else
1063 return (-1);
1064 #endif
1065 }
1067 qi = (dispatch_queue_t *) malloc (sizeof (*qi));
1068 if (qi == NULL)
1069 {
1070 ERROR ("plugin_dispatch_values_async: malloc failed.");
1071 return (-ENOMEM);
1072 }
1073 memset (qi, 0, sizeof (*qi));
1074 qi->next = NULL;
1076 qi->vl = (value_list_t *) malloc (sizeof (value_list_t));
1077 if (qi->vl == NULL)
1078 {
1079 ERROR ("plugin_dispatch_values_async: malloc failed.");
1080 sfree (qi);
1081 return (-ENOMEM);
1082 }
1083 memcpy (qi->vl, vl, sizeof (value_list_t));
1084 qi->vl->values = NULL;
1086 qi->vl->values = (value_t *) calloc (qi->vl->values_len,
1087 sizeof (value_t));
1088 if (qi->vl->values == NULL)
1089 {
1090 ERROR ("plugin_dispatch_values_async: malloc failed.");
1091 sfree (qi->vl);
1092 sfree (qi);
1093 return (-ENOMEM);
1094 }
1096 for (i = 0; i < vl->values_len; i++)
1097 qi->vl->values[i] = vl->values[i];
1099 pthread_mutex_lock (&dispatch_lock);
1101 if (dispatch_tail == NULL)
1102 {
1103 dispatch_head = qi;
1104 dispatch_tail = qi;
1105 }
1106 else
1107 {
1108 dispatch_tail->next = qi;
1109 dispatch_tail = qi;
1110 }
1112 pthread_cond_signal (&dispatch_cond);
1113 pthread_mutex_unlock (&dispatch_lock);
1115 return (0);
1116 } /* int plugin_dispatch_values_async */
1118 int plugin_dispatch_notification (const notification_t *notif)
1119 {
1120 int (*callback) (const notification_t *);
1121 llentry_t *le;
1122 /* Possible TODO: Add flap detection here */
1124 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1125 "time = %u; host = %s;",
1126 notif->severity, notif->message,
1127 (unsigned int) notif->time, notif->host);
1129 /* Nobody cares for notifications */
1130 if (list_notification == NULL)
1131 return (-1);
1133 le = llist_head (list_notification);
1134 while (le != NULL)
1135 {
1136 callback = (int (*) (const notification_t *)) le->value;
1137 (*callback) (notif);
1139 le = le->next;
1140 }
1142 return (0);
1143 } /* int plugin_dispatch_notification */
1145 void plugin_log (int level, const char *format, ...)
1146 {
1147 char msg[1024];
1148 va_list ap;
1150 void (*callback) (int, const char *);
1151 llentry_t *le;
1153 if (list_log == NULL)
1154 return;
1156 #if !COLLECT_DEBUG
1157 if (level >= LOG_DEBUG)
1158 return;
1159 #endif
1161 va_start (ap, format);
1162 vsnprintf (msg, sizeof (msg), format, ap);
1163 msg[sizeof (msg) - 1] = '\0';
1164 va_end (ap);
1166 le = llist_head (list_log);
1167 while (le != NULL)
1168 {
1169 callback = (void (*) (int, const char *)) le->value;
1170 (*callback) (level, msg);
1172 le = le->next;
1173 }
1174 } /* void plugin_log */
1176 const data_set_t *plugin_get_ds (const char *name)
1177 {
1178 data_set_t *ds;
1180 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1181 {
1182 DEBUG ("No such dataset registered: %s", name);
1183 return (NULL);
1184 }
1186 return (ds);
1187 } /* data_set_t *plugin_get_ds */
1189 static int plugin_notification_meta_add (notification_t *n,
1190 const char *name,
1191 enum notification_meta_type_e type,
1192 const void *value)
1193 {
1194 notification_meta_t *meta;
1195 notification_meta_t *tail;
1197 if ((n == NULL) || (name == NULL) || (value == NULL))
1198 {
1199 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1200 return (-1);
1201 }
1203 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1204 if (meta == NULL)
1205 {
1206 ERROR ("plugin_notification_meta_add: malloc failed.");
1207 return (-1);
1208 }
1209 memset (meta, 0, sizeof (notification_meta_t));
1211 sstrncpy (meta->name, name, sizeof (meta->name));
1212 meta->type = type;
1214 switch (type)
1215 {
1216 case NM_TYPE_STRING:
1217 {
1218 meta->nm_value.nm_string = strdup ((const char *) value);
1219 if (meta->nm_value.nm_string == NULL)
1220 {
1221 ERROR ("plugin_notification_meta_add: strdup failed.");
1222 sfree (meta);
1223 return (-1);
1224 }
1225 break;
1226 }
1227 case NM_TYPE_SIGNED_INT:
1228 {
1229 meta->nm_value.nm_signed_int = *((int64_t *) value);
1230 break;
1231 }
1232 case NM_TYPE_UNSIGNED_INT:
1233 {
1234 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1235 break;
1236 }
1237 case NM_TYPE_DOUBLE:
1238 {
1239 meta->nm_value.nm_double = *((double *) value);
1240 break;
1241 }
1242 case NM_TYPE_BOOLEAN:
1243 {
1244 meta->nm_value.nm_boolean = *((bool *) value);
1245 break;
1246 }
1247 default:
1248 {
1249 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1250 sfree (meta);
1251 return (-1);
1252 }
1253 } /* switch (type) */
1255 meta->next = NULL;
1256 tail = n->meta;
1257 while ((tail != NULL) && (tail->next != NULL))
1258 tail = tail->next;
1260 if (tail == NULL)
1261 n->meta = meta;
1262 else
1263 tail->next = meta;
1265 return (0);
1266 } /* int plugin_notification_meta_add */
1268 int plugin_notification_meta_add_string (notification_t *n,
1269 const char *name,
1270 const char *value)
1271 {
1272 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1273 }
1275 int plugin_notification_meta_add_signed_int (notification_t *n,
1276 const char *name,
1277 int64_t value)
1278 {
1279 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1280 }
1282 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1283 const char *name,
1284 uint64_t value)
1285 {
1286 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1287 }
1289 int plugin_notification_meta_add_double (notification_t *n,
1290 const char *name,
1291 double value)
1292 {
1293 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1294 }
1296 int plugin_notification_meta_add_boolean (notification_t *n,
1297 const char *name,
1298 bool value)
1299 {
1300 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1301 }
1303 int plugin_notification_meta_copy (notification_t *dst,
1304 const notification_t *src)
1305 {
1306 notification_meta_t *meta;
1308 assert (dst != NULL);
1309 assert (src != NULL);
1310 assert (dst != src);
1311 assert ((src->meta == NULL) || (src->meta != dst->meta));
1313 for (meta = src->meta; meta != NULL; meta = meta->next)
1314 {
1315 if (meta->type == NM_TYPE_STRING)
1316 plugin_notification_meta_add_string (dst, meta->name,
1317 meta->nm_value.nm_string);
1318 else if (meta->type == NM_TYPE_SIGNED_INT)
1319 plugin_notification_meta_add_signed_int (dst, meta->name,
1320 meta->nm_value.nm_signed_int);
1321 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1322 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1323 meta->nm_value.nm_unsigned_int);
1324 else if (meta->type == NM_TYPE_DOUBLE)
1325 plugin_notification_meta_add_double (dst, meta->name,
1326 meta->nm_value.nm_double);
1327 else if (meta->type == NM_TYPE_BOOLEAN)
1328 plugin_notification_meta_add_boolean (dst, meta->name,
1329 meta->nm_value.nm_boolean);
1330 }
1332 return (0);
1333 } /* int plugin_notification_meta_copy */
1335 int plugin_notification_meta_free (notification_meta_t *n)
1336 {
1337 notification_meta_t *this;
1338 notification_meta_t *next;
1340 if (n == NULL)
1341 {
1342 ERROR ("plugin_notification_meta_free: n == NULL!");
1343 return (-1);
1344 }
1346 this = n;
1347 while (this != NULL)
1348 {
1349 next = this->next;
1351 if (this->type == NM_TYPE_STRING)
1352 {
1353 free ((char *)this->nm_value.nm_string);
1354 this->nm_value.nm_string = NULL;
1355 }
1356 sfree (this);
1358 this = next;
1359 }
1361 return (0);
1362 } /* int plugin_notification_meta_free */