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 #define RF_SIMPLE 0
45 #define RF_COMPLEX 1
46 struct read_func_s
47 {
48 int wait_time;
49 int wait_left;
50 int type;
51 union
52 {
53 int (*cb_simple) (void);
54 plugin_read_cb cb_complex;
55 } callback;
56 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
57 user_data_t udata;
58 };
59 typedef struct read_func_s read_func_t;
61 struct write_func_s
62 {
63 plugin_write_cb callback;
64 user_data_t udata;
65 };
66 typedef struct write_func_s write_func_t;
68 struct flush_func_s
69 {
70 plugin_flush_cb callback;
71 user_data_t udata;
72 };
73 typedef struct flush_func_s flush_func_t;
75 struct log_func_s
76 {
77 plugin_log_cb callback;
78 user_data_t udata;
79 };
80 typedef struct log_func_s log_func_t;
82 /*
83 * Private variables
84 */
85 static llist_t *list_init;
86 static llist_t *list_read;
87 static llist_t *list_write;
88 static llist_t *list_flush;
89 static llist_t *list_shutdown;
90 static llist_t *list_log;
91 static llist_t *list_notification;
93 static fc_chain_t *pre_cache_chain = NULL;
94 static fc_chain_t *post_cache_chain = NULL;
96 static c_avl_tree_t *data_sets;
98 static char *plugindir = NULL;
100 static int read_loop = 1;
101 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
102 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
103 static pthread_t *read_threads = NULL;
104 static int read_threads_num = 0;
106 /*
107 * Static functions
108 */
109 static const char *plugin_get_dir (void)
110 {
111 if (plugindir == NULL)
112 return (PLUGINDIR);
113 else
114 return (plugindir);
115 }
117 static int register_callback (llist_t **list, const char *name, void *callback)
118 {
119 llentry_t *le;
120 char *key;
122 if ((*list == NULL)
123 && ((*list = llist_create ()) == NULL))
124 return (-1);
126 le = llist_search (*list, name);
127 if (le == NULL)
128 {
129 key = strdup (name);
130 if (key == NULL)
131 return (-1);
133 le = llentry_create (key, callback);
134 if (le == NULL)
135 {
136 free (key);
137 return (-1);
138 }
140 llist_append (*list, le);
141 }
142 else
143 {
144 le->value = callback;
145 }
147 return (0);
148 } /* int register_callback */
150 static void plugin_user_data_destroy (user_data_t *ud)
151 {
152 if ((ud != NULL) && (ud->data != NULL) && (ud->free_func != NULL))
153 {
154 ud->free_func (ud->data);
155 ud->data = NULL;
156 ud->free_func = NULL;
157 }
158 } /* void plugin_user_data_destroy */
160 static int plugin_unregister (llist_t *list, const char *name)
161 {
162 llentry_t *e;
164 e = llist_search (list, name);
166 if (e == NULL)
167 return (-1);
169 llist_remove (list, e);
170 free (e->key);
171 llentry_destroy (e);
173 return (0);
174 } /* int plugin_unregister */
176 /*
177 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
178 * object, but it will bitch about a shared object not having a
179 * ``module_register'' symbol..
180 */
181 static int plugin_load_file (char *file)
182 {
183 lt_dlhandle dlh;
184 void (*reg_handle) (void);
186 DEBUG ("file = %s", file);
188 lt_dlinit ();
189 lt_dlerror (); /* clear errors */
191 if ((dlh = lt_dlopen (file)) == NULL)
192 {
193 const char *error = lt_dlerror ();
195 ERROR ("lt_dlopen (%s) failed: %s", file, error);
196 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
197 return (1);
198 }
200 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
201 {
202 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
203 file, lt_dlerror ());
204 lt_dlclose (dlh);
205 return (-1);
206 }
208 (*reg_handle) ();
210 return (0);
211 }
213 static void *plugin_read_thread (void __attribute__((unused)) *args)
214 {
215 llentry_t *le;
216 read_func_t *rf;
217 int status;
218 int done;
220 pthread_mutex_lock (&read_lock);
222 while (read_loop != 0)
223 {
224 le = llist_head (list_read);
225 done = 0;
227 while ((read_loop != 0) && (le != NULL))
228 {
229 rf = (read_func_t *) le->value;
231 if (rf->needs_read != TODO)
232 {
233 le = le->next;
234 continue;
235 }
237 /* We will do this read function */
238 rf->needs_read = ACTIVE;
240 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
241 (unsigned long int) pthread_self (), le->key);
242 pthread_mutex_unlock (&read_lock);
244 if (rf->type == RF_SIMPLE)
245 {
246 status = rf->callback.cb_simple ();
247 }
248 else
249 {
250 assert (rf->type == RF_COMPLEX);
251 status = rf->callback.cb_complex (&rf->udata);
252 }
253 done++;
255 if (status != 0)
256 {
257 if (rf->wait_time < interval_g)
258 rf->wait_time = interval_g;
259 rf->wait_left = rf->wait_time;
260 rf->wait_time = rf->wait_time * 2;
261 if (rf->wait_time > 86400)
262 rf->wait_time = 86400;
264 NOTICE ("read-function of plugin `%s' "
265 "failed. Will suspend it for %i "
266 "seconds.", le->key, rf->wait_left);
267 }
268 else
269 {
270 rf->wait_left = 0;
271 rf->wait_time = interval_g;
272 }
274 pthread_mutex_lock (&read_lock);
276 rf->needs_read = DONE;
277 le = le->next;
278 } /* while (le != NULL) */
280 if ((read_loop != 0) && (done == 0))
281 {
282 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
283 (unsigned long int) pthread_self ());
284 pthread_cond_wait (&read_cond, &read_lock);
285 }
286 } /* while (read_loop) */
288 pthread_mutex_unlock (&read_lock);
290 pthread_exit (NULL);
291 return ((void *) 0);
292 } /* void *plugin_read_thread */
294 static void start_read_threads (int num)
295 {
296 int i;
298 if (read_threads != NULL)
299 return;
301 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
302 if (read_threads == NULL)
303 {
304 ERROR ("plugin: start_read_threads: calloc failed.");
305 return;
306 }
308 read_threads_num = 0;
309 for (i = 0; i < num; i++)
310 {
311 if (pthread_create (read_threads + read_threads_num, NULL,
312 plugin_read_thread, NULL) == 0)
313 {
314 read_threads_num++;
315 }
316 else
317 {
318 ERROR ("plugin: start_read_threads: pthread_create failed.");
319 return;
320 }
321 } /* for (i) */
322 } /* void start_read_threads */
324 static void stop_read_threads (void)
325 {
326 int i;
328 if (read_threads == NULL)
329 return;
331 pthread_mutex_lock (&read_lock);
332 read_loop = 0;
333 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
334 pthread_cond_broadcast (&read_cond);
335 pthread_mutex_unlock (&read_lock);
337 for (i = 0; i < read_threads_num; i++)
338 {
339 if (pthread_join (read_threads[i], NULL) != 0)
340 {
341 ERROR ("plugin: stop_read_threads: pthread_join failed.");
342 }
343 read_threads[i] = (pthread_t) 0;
344 }
345 sfree (read_threads);
346 read_threads_num = 0;
347 } /* void stop_read_threads */
349 static int remove_read_functions (void)
350 {
351 llentry_t *this;
353 if (list_read == NULL)
354 return (0);
356 this = llist_head (list_read);
357 while (this != NULL)
358 {
359 llentry_t *next;
360 read_func_t *rf;
362 next = this->next;
363 rf = (read_func_t *) this->value;
365 free (this->key);
367 plugin_user_data_destroy (&rf->udata);
368 free (rf);
370 this = next;
371 }
373 llist_destroy (list_read);
374 list_read = NULL;
376 return (0);
377 } /* }}} int remove_read_functions */
379 /*
380 * Public functions
381 */
382 void plugin_set_dir (const char *dir)
383 {
384 if (plugindir != NULL)
385 free (plugindir);
387 if (dir == NULL)
388 plugindir = NULL;
389 else if ((plugindir = strdup (dir)) == NULL)
390 {
391 char errbuf[1024];
392 ERROR ("strdup failed: %s",
393 sstrerror (errno, errbuf, sizeof (errbuf)));
394 }
395 }
397 #define BUFSIZE 512
398 int plugin_load (const char *type)
399 {
400 DIR *dh;
401 const char *dir;
402 char filename[BUFSIZE] = "";
403 char typename[BUFSIZE];
404 int typename_len;
405 int ret;
406 struct stat statbuf;
407 struct dirent *de;
408 int status;
410 DEBUG ("type = %s", type);
412 dir = plugin_get_dir ();
413 ret = 1;
415 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
416 * type when matching the filename */
417 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
418 if ((status < 0) || ((size_t) status >= sizeof (typename)))
419 {
420 WARNING ("snprintf: truncated: `%s.so'", type);
421 return (-1);
422 }
423 typename_len = strlen (typename);
425 if ((dh = opendir (dir)) == NULL)
426 {
427 char errbuf[1024];
428 ERROR ("opendir (%s): %s", dir,
429 sstrerror (errno, errbuf, sizeof (errbuf)));
430 return (-1);
431 }
433 while ((de = readdir (dh)) != NULL)
434 {
435 if (strncasecmp (de->d_name, typename, typename_len))
436 continue;
438 status = ssnprintf (filename, sizeof (filename),
439 "%s/%s", dir, de->d_name);
440 if ((status < 0) || ((size_t) status >= sizeof (filename)))
441 {
442 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
443 continue;
444 }
446 if (lstat (filename, &statbuf) == -1)
447 {
448 char errbuf[1024];
449 WARNING ("stat %s: %s", filename,
450 sstrerror (errno, errbuf, sizeof (errbuf)));
451 continue;
452 }
453 else if (!S_ISREG (statbuf.st_mode))
454 {
455 /* don't follow symlinks */
456 continue;
457 }
459 if (plugin_load_file (filename) == 0)
460 {
461 /* success */
462 ret = 0;
463 break;
464 }
465 else
466 {
467 fprintf (stderr, "Unable to load plugin %s.\n", type);
468 }
469 }
471 closedir (dh);
473 if (filename[0] == '\0')
474 fprintf (stderr, "Could not find plugin %s.\n", type);
476 return (ret);
477 }
479 /*
480 * The `register_*' functions follow
481 */
482 int plugin_register_config (const char *name,
483 int (*callback) (const char *key, const char *val),
484 const char **keys, int keys_num)
485 {
486 cf_register (name, callback, keys, keys_num);
487 return (0);
488 } /* int plugin_register_config */
490 int plugin_register_complex_config (const char *type,
491 int (*callback) (oconfig_item_t *))
492 {
493 return (cf_register_complex (type, callback));
494 } /* int plugin_register_complex_config */
496 int plugin_register_init (const char *name,
497 int (*callback) (void))
498 {
499 return (register_callback (&list_init, name, (void *) callback));
500 } /* plugin_register_init */
502 int plugin_register_read (const char *name,
503 int (*callback) (void))
504 {
505 read_func_t *rf;
507 rf = (read_func_t *) malloc (sizeof (read_func_t));
508 if (rf == NULL)
509 {
510 char errbuf[1024];
511 ERROR ("plugin_register_read: malloc failed: %s",
512 sstrerror (errno, errbuf, sizeof (errbuf)));
513 return (-1);
514 }
516 memset (rf, 0, sizeof (read_func_t));
517 rf->wait_time = interval_g;
518 rf->wait_left = 0;
519 rf->type = RF_SIMPLE;
520 rf->callback.cb_simple = callback;
521 rf->needs_read = DONE;
522 rf->udata.data = NULL;
523 rf->udata.free_func = NULL;
525 return (register_callback (&list_read, name, (void *) rf));
526 } /* int plugin_register_read */
528 int plugin_register_complex_read (const char *name,
529 plugin_read_cb callback, user_data_t *user_data)
530 {
531 read_func_t *rf;
533 rf = (read_func_t *) malloc (sizeof (read_func_t));
534 if (rf == NULL)
535 {
536 ERROR ("plugin_register_complex_read: malloc failed.");
537 return (-1);
538 }
540 memset (rf, 0, sizeof (read_func_t));
541 rf->wait_time = interval_g;
542 rf->wait_left = 0;
543 rf->type = RF_COMPLEX;
544 rf->callback.cb_complex = callback;
545 rf->needs_read = DONE;
547 /* Set user data */
548 if (user_data == NULL)
549 {
550 rf->udata.data = NULL;
551 rf->udata.free_func = NULL;
552 }
553 else
554 {
555 rf->udata = *user_data;
556 }
558 return (register_callback (&list_read, name, (void *) rf));
559 } /* int plugin_register_complex_read */
561 int plugin_register_write (const char *name,
562 plugin_write_cb callback, user_data_t *user_data)
563 {
564 write_func_t *wf;
566 wf = (write_func_t *) malloc (sizeof (*wf));
567 if (wf == NULL)
568 {
569 ERROR ("plugin_register_write: malloc failed.");
570 return (-1);
571 }
572 memset (wf, 0, sizeof (*wf));
574 wf->callback = callback;
575 if (user_data == NULL)
576 {
577 wf->udata.data = NULL;
578 wf->udata.free_func = NULL;
579 }
580 else
581 {
582 wf->udata = *user_data;
583 }
585 return (register_callback (&list_write, name, (void *) wf));
586 } /* int plugin_register_write */
588 int plugin_register_flush (const char *name,
589 plugin_flush_cb callback, user_data_t *user_data)
590 {
591 flush_func_t *ff;
593 ff = (flush_func_t *) malloc (sizeof (*ff));
594 if (ff == NULL)
595 {
596 ERROR ("plugin_register_flush: malloc failed.");
597 return (-1);
598 }
599 memset (ff, 0, sizeof (*ff));
601 ff->callback = callback;
602 if (user_data == NULL)
603 {
604 ff->udata.data = NULL;
605 ff->udata.free_func = NULL;
606 }
607 else
608 {
609 ff->udata = *user_data;
610 }
612 return (register_callback (&list_flush, name, (void *) ff));
613 } /* int plugin_register_flush */
615 int plugin_register_shutdown (char *name,
616 int (*callback) (void))
617 {
618 return (register_callback (&list_shutdown, name, (void *) callback));
619 } /* int plugin_register_shutdown */
621 int plugin_register_data_set (const data_set_t *ds)
622 {
623 data_set_t *ds_copy;
624 int i;
626 if ((data_sets != NULL)
627 && (c_avl_get (data_sets, ds->type, NULL) == 0))
628 {
629 NOTICE ("Replacing DS `%s' with another version.", ds->type);
630 plugin_unregister_data_set (ds->type);
631 }
632 else if (data_sets == NULL)
633 {
634 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
635 if (data_sets == NULL)
636 return (-1);
637 }
639 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
640 if (ds_copy == NULL)
641 return (-1);
642 memcpy(ds_copy, ds, sizeof (data_set_t));
644 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
645 * ds->ds_num);
646 if (ds_copy->ds == NULL)
647 {
648 free (ds_copy);
649 return (-1);
650 }
652 for (i = 0; i < ds->ds_num; i++)
653 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
655 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
656 } /* int plugin_register_data_set */
658 int plugin_register_log (const char *name,
659 plugin_log_cb callback, user_data_t *user_data)
660 {
661 log_func_t *lf;
663 lf = (log_func_t *) malloc (sizeof (*lf));
664 if (lf == NULL)
665 {
666 ERROR ("plugin_register_log: malloc failed.");
667 return (-1);
668 }
669 memset (lf, 0, sizeof (*lf));
671 lf->callback = callback;
672 if (user_data == NULL)
673 {
674 lf->udata.data = NULL;
675 lf->udata.free_func = NULL;
676 }
677 else
678 {
679 lf->udata = *user_data;
680 }
682 return (register_callback (&list_log, name, (void *) lf));
683 } /* int plugin_register_log */
685 int plugin_register_notification (const char *name,
686 int (*callback) (const notification_t *notif))
687 {
688 return (register_callback (&list_notification, name, (void *) callback));
689 } /* int plugin_register_log */
691 int plugin_unregister_config (const char *name)
692 {
693 cf_unregister (name);
694 return (0);
695 } /* int plugin_unregister_config */
697 int plugin_unregister_complex_config (const char *name)
698 {
699 cf_unregister_complex (name);
700 return (0);
701 } /* int plugin_unregister_complex_config */
703 int plugin_unregister_init (const char *name)
704 {
705 return (plugin_unregister (list_init, name));
706 }
708 int plugin_unregister_read (const char *name)
709 {
710 llentry_t *e;
711 read_func_t *rf;
713 e = llist_search (list_read, name);
715 if (e == NULL)
716 return (-1);
718 llist_remove (list_read, e);
720 rf = (read_func_t *) e->value;
721 plugin_user_data_destroy (&rf->udata);
722 free (rf);
723 free (e->key);
725 llentry_destroy (e);
727 return (0);
728 }
730 int plugin_unregister_write (const char *name)
731 {
732 llentry_t *e;
733 write_func_t *wf;
735 e = llist_search (list_write, name);
737 if (e == NULL)
738 return (-1);
740 llist_remove (list_write, e);
742 wf = (write_func_t *) e->value;
743 plugin_user_data_destroy (&wf->udata);
744 free (wf);
745 free (e->key);
747 llentry_destroy (e);
749 return (0);
750 }
752 int plugin_unregister_flush (const char *name)
753 {
754 llentry_t *e;
755 flush_func_t *ff;
757 e = llist_search (list_flush, name);
759 if (e == NULL)
760 return (-1);
762 llist_remove (list_flush, e);
764 ff = (flush_func_t *) e->value;
765 plugin_user_data_destroy (&ff->udata);
766 free (ff);
767 free (e->key);
769 llentry_destroy (e);
771 return (0);
772 }
774 int plugin_unregister_shutdown (const char *name)
775 {
776 return (plugin_unregister (list_shutdown, name));
777 }
779 int plugin_unregister_data_set (const char *name)
780 {
781 data_set_t *ds;
783 if (data_sets == NULL)
784 return (-1);
786 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
787 return (-1);
789 sfree (ds->ds);
790 sfree (ds);
792 return (0);
793 } /* int plugin_unregister_data_set */
795 int plugin_unregister_log (const char *name)
796 {
797 llentry_t *e;
798 log_func_t *lf;
800 e = llist_search (list_log, name);
802 if (e == NULL)
803 return (-1);
805 llist_remove (list_log, e);
807 lf = (log_func_t *) e->value;
808 plugin_user_data_destroy (&lf->udata);
809 free (lf);
810 free (e->key);
812 llentry_destroy (e);
814 return (0);
815 }
817 int plugin_unregister_notification (const char *name)
818 {
819 return (plugin_unregister (list_notification, name));
820 }
822 void plugin_init_all (void)
823 {
824 const char *chain_name;
825 int (*callback) (void);
826 llentry_t *le;
827 int status;
829 /* Init the value cache */
830 uc_init ();
832 chain_name = global_option_get ("PreCacheChain");
833 pre_cache_chain = fc_chain_get_by_name (chain_name);
835 chain_name = global_option_get ("PostCacheChain");
836 post_cache_chain = fc_chain_get_by_name (chain_name);
839 if ((list_init == NULL) && (list_read == NULL))
840 return;
842 /* Calling all init callbacks before checking if read callbacks
843 * are available allows the init callbacks to register the read
844 * callback. */
845 le = llist_head (list_init);
846 while (le != NULL)
847 {
848 callback = (int (*) (void)) le->value;
849 status = (*callback) ();
851 if (status != 0)
852 {
853 ERROR ("Initialization of plugin `%s' "
854 "failed with status %i. "
855 "Plugin will be unloaded.",
856 le->key, status);
857 /* Plugins that register read callbacks from the init
858 * callback should take care of appropriate error
859 * handling themselves. */
860 /* FIXME: Unload _all_ functions */
861 plugin_unregister_read (le->key);
862 }
864 le = le->next;
865 }
867 /* Start read-threads */
868 if (list_read != NULL)
869 {
870 const char *rt;
871 int num;
872 rt = global_option_get ("ReadThreads");
873 num = atoi (rt);
874 if (num != -1)
875 start_read_threads ((num > 0) ? num : 5);
876 }
877 } /* void plugin_init_all */
879 void plugin_read_all (void)
880 {
881 llentry_t *le;
882 read_func_t *rf;
884 uc_check_timeout ();
886 if (list_read == NULL)
887 return;
889 pthread_mutex_lock (&read_lock);
891 le = llist_head (list_read);
892 while (le != NULL)
893 {
894 rf = (read_func_t *) le->value;
896 if (rf->needs_read != DONE)
897 {
898 le = le->next;
899 continue;
900 }
902 if (rf->wait_left > 0)
903 rf->wait_left -= interval_g;
905 if (rf->wait_left <= 0)
906 {
907 rf->needs_read = TODO;
908 }
910 le = le->next;
911 }
913 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
914 pthread_cond_broadcast (&read_cond);
915 pthread_mutex_unlock (&read_lock);
916 } /* void plugin_read_all */
918 /* Read function called when the `-T' command line argument is given. */
919 int plugin_read_all_once (void)
920 {
921 llentry_t *le;
922 read_func_t *rf;
923 int status;
924 int return_status = 0;
926 if (list_read == NULL)
927 {
928 NOTICE ("No read-functions are registered.");
929 return (0);
930 }
932 for (le = llist_head (list_read);
933 le != NULL;
934 le = le->next)
935 {
936 rf = (read_func_t *) le->value;
938 if (rf->type == RF_SIMPLE)
939 {
940 status = rf->callback.cb_simple ();
941 }
942 else
943 {
944 assert (rf->type == RF_COMPLEX);
945 status = rf->callback.cb_complex (&rf->udata);
946 }
948 if (status != 0)
949 {
950 NOTICE ("read-function of plugin `%s' failed.",
951 le->key);
952 return_status = -1;
953 }
954 }
956 return (return_status);
957 } /* int plugin_read_all_once */
959 int plugin_write (const char *plugin, /* {{{ */
960 const data_set_t *ds, const value_list_t *vl)
961 {
962 llentry_t *le;
963 int status;
965 if (vl == NULL)
966 return (EINVAL);
968 if (list_write == NULL)
969 return (ENOENT);
971 if (ds == NULL)
972 {
973 ds = plugin_get_ds (vl->type);
974 if (ds == NULL)
975 {
976 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
977 return (ENOENT);
978 }
979 }
981 if (plugin == NULL)
982 {
983 int success = 0;
984 int failure = 0;
986 le = llist_head (list_write);
987 while (le != NULL)
988 {
989 write_func_t *wf = le->value;
991 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
992 status = wf->callback (ds, vl, &wf->udata);
993 if (status != 0)
994 failure++;
995 else
996 success++;
998 le = le->next;
999 }
1001 if ((success == 0) && (failure != 0))
1002 status = -1;
1003 else
1004 status = 0;
1005 }
1006 else /* plugin != NULL */
1007 {
1008 write_func_t *wf;
1009 le = llist_head (list_write);
1010 while (le != NULL)
1011 {
1012 if (strcasecmp (plugin, le->key) == 0)
1013 break;
1015 le = le->next;
1016 }
1018 if (le == NULL)
1019 return (ENOENT);
1021 wf = le->value;
1023 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1024 status = wf->callback (ds, vl, &wf->udata);
1025 }
1027 return (status);
1028 } /* }}} int plugin_write */
1030 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1031 {
1032 llentry_t *le;
1034 if (list_flush == NULL)
1035 return (0);
1037 le = llist_head (list_flush);
1038 while (le != NULL)
1039 {
1040 flush_func_t *ff;
1042 if ((plugin != NULL)
1043 && (strcmp (plugin, le->key) != 0))
1044 {
1045 le = le->next;
1046 continue;
1047 }
1049 ff = (flush_func_t *) le->value;
1051 ff->callback (timeout, identifier, &ff->udata);
1053 le = le->next;
1054 }
1055 return (0);
1056 } /* int plugin_flush */
1058 void plugin_shutdown_all (void)
1059 {
1060 int (*callback) (void);
1061 llentry_t *le;
1063 stop_read_threads ();
1064 remove_read_functions ();
1066 if (list_shutdown == NULL)
1067 return;
1069 le = llist_head (list_shutdown);
1070 while (le != NULL)
1071 {
1072 callback = (int (*) (void)) le->value;
1074 /* Advance the pointer before calling the callback allows
1075 * shutdown functions to unregister themselves. If done the
1076 * other way around the memory `le' points to will be freed
1077 * after callback returns. */
1078 le = le->next;
1080 (*callback) ();
1081 }
1082 } /* void plugin_shutdown_all */
1084 int plugin_dispatch_values (value_list_t *vl)
1085 {
1086 int status;
1087 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1089 data_set_t *ds;
1091 if ((vl == NULL) || (*vl->type == '\0')) {
1092 ERROR ("plugin_dispatch_values: Invalid value list.");
1093 return (-1);
1094 }
1096 if (list_write == NULL)
1097 c_complain_once (LOG_WARNING, &no_write_complaint,
1098 "plugin_dispatch_values: No write callback has been "
1099 "registered. Please load at least one output plugin, "
1100 "if you want the collected data to be stored.");
1102 if (data_sets == NULL)
1103 {
1104 ERROR ("plugin_dispatch_values: No data sets registered. "
1105 "Could the types database be read? Check "
1106 "your `TypesDB' setting!");
1107 return (-1);
1108 }
1110 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1111 {
1112 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1113 return (-1);
1114 }
1116 if (vl->time == 0)
1117 vl->time = time (NULL);
1119 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1120 "host = %s; "
1121 "plugin = %s; plugin_instance = %s; "
1122 "type = %s; type_instance = %s;",
1123 (unsigned int) vl->time, vl->interval,
1124 vl->host,
1125 vl->plugin, vl->plugin_instance,
1126 vl->type, vl->type_instance);
1128 #if COLLECT_DEBUG
1129 assert (0 == strcmp (ds->type, vl->type));
1130 #else
1131 if (0 != strcmp (ds->type, vl->type))
1132 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1133 ds->type, vl->type);
1134 #endif
1136 #if COLLECT_DEBUG
1137 assert (ds->ds_num == vl->values_len);
1138 #else
1139 if (ds->ds_num != vl->values_len)
1140 {
1141 ERROR ("plugin_dispatch_values: ds->type = %s: "
1142 "(ds->ds_num = %i) != "
1143 "(vl->values_len = %i)",
1144 ds->type, ds->ds_num, vl->values_len);
1145 return (-1);
1146 }
1147 #endif
1149 escape_slashes (vl->host, sizeof (vl->host));
1150 escape_slashes (vl->plugin, sizeof (vl->plugin));
1151 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1152 escape_slashes (vl->type, sizeof (vl->type));
1153 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1155 if (pre_cache_chain != NULL)
1156 {
1157 status = fc_process_chain (ds, vl, pre_cache_chain);
1158 if (status < 0)
1159 {
1160 WARNING ("plugin_dispatch_values: Running the "
1161 "pre-cache chain failed with "
1162 "status %i (%#x).",
1163 status, status);
1164 }
1165 else if (status == FC_TARGET_STOP)
1166 return (0);
1167 }
1169 /* Update the value cache */
1170 uc_update (ds, vl);
1172 if (post_cache_chain != NULL)
1173 {
1174 status = fc_process_chain (ds, vl, post_cache_chain);
1175 if (status < 0)
1176 {
1177 WARNING ("plugin_dispatch_values: Running the "
1178 "post-cache chain failed with "
1179 "status %i (%#x).",
1180 status, status);
1181 }
1182 }
1183 else
1184 fc_default_action (ds, vl);
1186 return (0);
1187 } /* int plugin_dispatch_values */
1189 int plugin_dispatch_notification (const notification_t *notif)
1190 {
1191 int (*callback) (const notification_t *);
1192 llentry_t *le;
1193 /* Possible TODO: Add flap detection here */
1195 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1196 "time = %u; host = %s;",
1197 notif->severity, notif->message,
1198 (unsigned int) notif->time, notif->host);
1200 /* Nobody cares for notifications */
1201 if (list_notification == NULL)
1202 return (-1);
1204 le = llist_head (list_notification);
1205 while (le != NULL)
1206 {
1207 callback = (int (*) (const notification_t *)) le->value;
1208 (*callback) (notif);
1210 le = le->next;
1211 }
1213 return (0);
1214 } /* int plugin_dispatch_notification */
1216 void plugin_log (int level, const char *format, ...)
1217 {
1218 char msg[1024];
1219 va_list ap;
1220 llentry_t *le;
1222 if (list_log == NULL)
1223 return;
1225 #if !COLLECT_DEBUG
1226 if (level >= LOG_DEBUG)
1227 return;
1228 #endif
1230 va_start (ap, format);
1231 vsnprintf (msg, sizeof (msg), format, ap);
1232 msg[sizeof (msg) - 1] = '\0';
1233 va_end (ap);
1235 le = llist_head (list_log);
1236 while (le != NULL)
1237 {
1238 log_func_t *lf;
1240 lf = (log_func_t *) le->value;
1242 lf->callback (level, msg, &lf->udata);
1244 le = le->next;
1245 }
1246 } /* void plugin_log */
1248 const data_set_t *plugin_get_ds (const char *name)
1249 {
1250 data_set_t *ds;
1252 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1253 {
1254 DEBUG ("No such dataset registered: %s", name);
1255 return (NULL);
1256 }
1258 return (ds);
1259 } /* data_set_t *plugin_get_ds */
1261 static int plugin_notification_meta_add (notification_t *n,
1262 const char *name,
1263 enum notification_meta_type_e type,
1264 const void *value)
1265 {
1266 notification_meta_t *meta;
1267 notification_meta_t *tail;
1269 if ((n == NULL) || (name == NULL) || (value == NULL))
1270 {
1271 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1272 return (-1);
1273 }
1275 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1276 if (meta == NULL)
1277 {
1278 ERROR ("plugin_notification_meta_add: malloc failed.");
1279 return (-1);
1280 }
1281 memset (meta, 0, sizeof (notification_meta_t));
1283 sstrncpy (meta->name, name, sizeof (meta->name));
1284 meta->type = type;
1286 switch (type)
1287 {
1288 case NM_TYPE_STRING:
1289 {
1290 meta->nm_value.nm_string = strdup ((const char *) value);
1291 if (meta->nm_value.nm_string == NULL)
1292 {
1293 ERROR ("plugin_notification_meta_add: strdup failed.");
1294 sfree (meta);
1295 return (-1);
1296 }
1297 break;
1298 }
1299 case NM_TYPE_SIGNED_INT:
1300 {
1301 meta->nm_value.nm_signed_int = *((int64_t *) value);
1302 break;
1303 }
1304 case NM_TYPE_UNSIGNED_INT:
1305 {
1306 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1307 break;
1308 }
1309 case NM_TYPE_DOUBLE:
1310 {
1311 meta->nm_value.nm_double = *((double *) value);
1312 break;
1313 }
1314 case NM_TYPE_BOOLEAN:
1315 {
1316 meta->nm_value.nm_boolean = *((bool *) value);
1317 break;
1318 }
1319 default:
1320 {
1321 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1322 sfree (meta);
1323 return (-1);
1324 }
1325 } /* switch (type) */
1327 meta->next = NULL;
1328 tail = n->meta;
1329 while ((tail != NULL) && (tail->next != NULL))
1330 tail = tail->next;
1332 if (tail == NULL)
1333 n->meta = meta;
1334 else
1335 tail->next = meta;
1337 return (0);
1338 } /* int plugin_notification_meta_add */
1340 int plugin_notification_meta_add_string (notification_t *n,
1341 const char *name,
1342 const char *value)
1343 {
1344 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1345 }
1347 int plugin_notification_meta_add_signed_int (notification_t *n,
1348 const char *name,
1349 int64_t value)
1350 {
1351 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1352 }
1354 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1355 const char *name,
1356 uint64_t value)
1357 {
1358 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1359 }
1361 int plugin_notification_meta_add_double (notification_t *n,
1362 const char *name,
1363 double value)
1364 {
1365 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1366 }
1368 int plugin_notification_meta_add_boolean (notification_t *n,
1369 const char *name,
1370 bool value)
1371 {
1372 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1373 }
1375 int plugin_notification_meta_copy (notification_t *dst,
1376 const notification_t *src)
1377 {
1378 notification_meta_t *meta;
1380 assert (dst != NULL);
1381 assert (src != NULL);
1382 assert (dst != src);
1383 assert ((src->meta == NULL) || (src->meta != dst->meta));
1385 for (meta = src->meta; meta != NULL; meta = meta->next)
1386 {
1387 if (meta->type == NM_TYPE_STRING)
1388 plugin_notification_meta_add_string (dst, meta->name,
1389 meta->nm_value.nm_string);
1390 else if (meta->type == NM_TYPE_SIGNED_INT)
1391 plugin_notification_meta_add_signed_int (dst, meta->name,
1392 meta->nm_value.nm_signed_int);
1393 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1394 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1395 meta->nm_value.nm_unsigned_int);
1396 else if (meta->type == NM_TYPE_DOUBLE)
1397 plugin_notification_meta_add_double (dst, meta->name,
1398 meta->nm_value.nm_double);
1399 else if (meta->type == NM_TYPE_BOOLEAN)
1400 plugin_notification_meta_add_boolean (dst, meta->name,
1401 meta->nm_value.nm_boolean);
1402 }
1404 return (0);
1405 } /* int plugin_notification_meta_copy */
1407 int plugin_notification_meta_free (notification_meta_t *n)
1408 {
1409 notification_meta_t *this;
1410 notification_meta_t *next;
1412 if (n == NULL)
1413 {
1414 ERROR ("plugin_notification_meta_free: n == NULL!");
1415 return (-1);
1416 }
1418 this = n;
1419 while (this != NULL)
1420 {
1421 next = this->next;
1423 if (this->type == NM_TYPE_STRING)
1424 {
1425 free ((char *)this->nm_value.nm_string);
1426 this->nm_value.nm_string = NULL;
1427 }
1428 sfree (this);
1430 this = next;
1431 }
1433 return (0);
1434 } /* int plugin_notification_meta_free */