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_heap.h"
38 #include "utils_cache.h"
39 #include "utils_threshold.h"
40 #include "filter_chain.h"
42 /*
43 * Private structures
44 */
45 struct callback_func_s
46 {
47 void *cf_callback;
48 user_data_t cf_udata;
49 };
50 typedef struct callback_func_s callback_func_t;
52 #define RF_SIMPLE 0
53 #define RF_COMPLEX 1
54 #define RF_REMOVE 65535
55 struct read_func_s
56 {
57 /* `read_func_t' "inherits" from `callback_func_t'.
58 * The `rf_super' member MUST be the first one in this structure! */
59 #define rf_callback rf_super.cf_callback
60 #define rf_udata rf_super.cf_udata
61 callback_func_t rf_super;
62 char rf_name[DATA_MAX_NAME_LEN];
63 int rf_type;
64 struct timespec rf_interval;
65 struct timespec rf_effective_interval;
66 struct timespec rf_next_read;
67 };
68 typedef struct read_func_s read_func_t;
70 /*
71 * Private variables
72 */
73 static llist_t *list_init;
74 static llist_t *list_write;
75 static llist_t *list_flush;
76 static llist_t *list_shutdown;
77 static llist_t *list_log;
78 static llist_t *list_notification;
80 static fc_chain_t *pre_cache_chain = NULL;
81 static fc_chain_t *post_cache_chain = NULL;
83 static c_avl_tree_t *data_sets;
85 static char *plugindir = NULL;
87 static c_heap_t *read_heap = NULL;
88 static llist_t *read_list;
89 static int read_loop = 1;
90 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
91 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
92 static pthread_t *read_threads = NULL;
93 static int read_threads_num = 0;
95 /*
96 * Static functions
97 */
98 static const char *plugin_get_dir (void)
99 {
100 if (plugindir == NULL)
101 return (PLUGINDIR);
102 else
103 return (plugindir);
104 }
106 static void destroy_callback (callback_func_t *cf) /* {{{ */
107 {
108 if (cf == NULL)
109 return;
111 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
112 {
113 cf->cf_udata.free_func (cf->cf_udata.data);
114 cf->cf_udata.data = NULL;
115 cf->cf_udata.free_func = NULL;
116 }
117 sfree (cf);
118 } /* }}} void destroy_callback */
120 static void destroy_all_callbacks (llist_t **list) /* {{{ */
121 {
122 llentry_t *le;
124 if (*list == NULL)
125 return;
127 le = llist_head (*list);
128 while (le != NULL)
129 {
130 llentry_t *le_next;
132 le_next = le->next;
134 sfree (le->key);
135 destroy_callback (le->value);
136 le->value = NULL;
138 le = le_next;
139 }
141 llist_destroy (*list);
142 *list = NULL;
143 } /* }}} void destroy_all_callbacks */
145 static void destroy_read_heap (void) /* {{{ */
146 {
147 if (read_heap == NULL)
148 return;
150 while (42)
151 {
152 callback_func_t *cf;
154 cf = c_heap_get_root (read_heap);
155 if (cf == NULL)
156 break;
158 destroy_callback (cf);
159 }
161 c_heap_destroy (read_heap);
162 read_heap = NULL;
163 } /* }}} void destroy_read_heap */
165 static int register_callback (llist_t **list, /* {{{ */
166 const char *name, callback_func_t *cf)
167 {
168 llentry_t *le;
169 char *key;
171 if (*list == NULL)
172 {
173 *list = llist_create ();
174 if (*list == NULL)
175 {
176 ERROR ("plugin: register_callback: "
177 "llist_create failed.");
178 destroy_callback (cf);
179 return (-1);
180 }
181 }
183 key = strdup (name);
184 if (key == NULL)
185 {
186 ERROR ("plugin: register_callback: strdup failed.");
187 destroy_callback (cf);
188 return (-1);
189 }
191 le = llist_search (*list, name);
192 if (le == NULL)
193 {
194 le = llentry_create (key, cf);
195 if (le == NULL)
196 {
197 ERROR ("plugin: register_callback: "
198 "llentry_create failed.");
199 free (key);
200 destroy_callback (cf);
201 return (-1);
202 }
204 llist_append (*list, le);
205 }
206 else
207 {
208 callback_func_t *old_cf;
210 old_cf = le->value;
211 le->value = cf;
213 WARNING ("plugin: register_callback: "
214 "a callback named `%s' already exists - "
215 "overwriting the old entry!", name);
217 destroy_callback (old_cf);
218 sfree (key);
219 }
221 return (0);
222 } /* }}} int register_callback */
224 static int create_register_callback (llist_t **list, /* {{{ */
225 const char *name, void *callback, user_data_t *ud)
226 {
227 callback_func_t *cf;
229 cf = (callback_func_t *) malloc (sizeof (*cf));
230 if (cf == NULL)
231 {
232 ERROR ("plugin: create_register_callback: malloc failed.");
233 return (-1);
234 }
235 memset (cf, 0, sizeof (*cf));
237 cf->cf_callback = callback;
238 if (ud == NULL)
239 {
240 cf->cf_udata.data = NULL;
241 cf->cf_udata.free_func = NULL;
242 }
243 else
244 {
245 cf->cf_udata = *ud;
246 }
248 return (register_callback (list, name, cf));
249 } /* }}} int create_register_callback */
251 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
252 {
253 llentry_t *e;
255 if (list == NULL)
256 return (-1);
258 e = llist_search (list, name);
259 if (e == NULL)
260 return (-1);
262 llist_remove (list, e);
264 sfree (e->key);
265 destroy_callback (e->value);
267 llentry_destroy (e);
269 return (0);
270 } /* }}} int plugin_unregister */
272 /*
273 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
274 * object, but it will bitch about a shared object not having a
275 * ``module_register'' symbol..
276 */
277 static int plugin_load_file (char *file, uint32_t flags)
278 {
279 lt_dlhandle dlh;
280 void (*reg_handle) (void);
282 DEBUG ("file = %s", file);
284 lt_dlinit ();
285 lt_dlerror (); /* clear errors */
287 #if LIBTOOL_VERSION == 2
288 if (flags & PLUGIN_FLAGS_GLOBAL) {
289 lt_dladvise advise;
290 lt_dladvise_init(&advise);
291 lt_dladvise_global(&advise);
292 dlh = lt_dlopenadvise(file, advise);
293 lt_dladvise_destroy(&advise);
294 } else {
295 dlh = lt_dlopen (file);
296 }
297 #else /* if LIBTOOL_VERSION == 1 */
298 if (flags & PLUGIN_FLAGS_GLOBAL)
299 ERROR ("plugin_load_file: The global flag is not supported, "
300 "libtool 2 is required for this.");
301 dlh = lt_dlopen (file);
302 #endif
304 if (dlh == NULL)
305 {
306 const char *error = lt_dlerror ();
308 ERROR ("lt_dlopen (%s) failed: %s", file, error);
309 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
310 return (1);
311 }
313 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
314 {
315 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
316 file, lt_dlerror ());
317 lt_dlclose (dlh);
318 return (-1);
319 }
321 (*reg_handle) ();
323 return (0);
324 }
326 static void *plugin_read_thread (void __attribute__((unused)) *args)
327 {
328 while (read_loop != 0)
329 {
330 read_func_t *rf;
331 struct timeval now;
332 int status;
333 int rf_type;
335 /* Get the read function that needs to be read next. */
336 rf = c_heap_get_root (read_heap);
337 if (rf == NULL)
338 {
339 struct timespec abstime;
341 gettimeofday (&now, /* timezone = */ NULL);
343 abstime.tv_sec = now.tv_sec + interval_g;
344 abstime.tv_nsec = 1000 * now.tv_usec;
346 pthread_mutex_lock (&read_lock);
347 pthread_cond_timedwait (&read_cond, &read_lock,
348 &abstime);
349 pthread_mutex_unlock (&read_lock);
350 continue;
351 }
353 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
354 {
355 gettimeofday (&now, /* timezone = */ NULL);
357 rf->rf_interval.tv_sec = interval_g;
358 rf->rf_interval.tv_nsec = 0;
360 rf->rf_effective_interval = rf->rf_interval;
362 rf->rf_next_read.tv_sec = now.tv_sec;
363 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
364 }
366 /* sleep until this entry is due,
367 * using pthread_cond_timedwait */
368 pthread_mutex_lock (&read_lock);
369 pthread_cond_timedwait (&read_cond, &read_lock,
370 &rf->rf_next_read);
371 /* Must hold `real_lock' when accessing `rf->rf_type'. */
372 rf_type = rf->rf_type;
373 pthread_mutex_unlock (&read_lock);
375 /* Check if we're supposed to stop.. This may have interrupted
376 * the sleep, too. */
377 if (read_loop == 0)
378 {
379 /* Insert `rf' again, so it can be free'd correctly */
380 c_heap_insert (read_heap, rf);
381 break;
382 }
384 /* The entry has been marked for deletion. The linked list
385 * entry has already been removed by `plugin_unregister_read'.
386 * All we have to do here is free the `read_func_t' and
387 * continue. */
388 if (rf_type == RF_REMOVE)
389 {
390 DEBUG ("plugin_read_thread: Destroying the `%s' "
391 "callback.", rf->rf_name);
392 destroy_callback ((callback_func_t *) rf);
393 rf = NULL;
394 continue;
395 }
397 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
399 if (rf_type == RF_SIMPLE)
400 {
401 int (*callback) (void);
403 callback = rf->rf_callback;
404 status = (*callback) ();
405 }
406 else
407 {
408 plugin_read_cb callback;
410 assert (rf_type == RF_COMPLEX);
412 callback = rf->rf_callback;
413 status = (*callback) (&rf->rf_udata);
414 }
416 /* If the function signals failure, we will increase the
417 * intervals in which it will be called. */
418 if (status != 0)
419 {
420 rf->rf_effective_interval.tv_sec *= 2;
421 rf->rf_effective_interval.tv_nsec *= 2;
422 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
424 if (rf->rf_effective_interval.tv_sec >= 86400)
425 {
426 rf->rf_effective_interval.tv_sec = 86400;
427 rf->rf_effective_interval.tv_nsec = 0;
428 }
430 NOTICE ("read-function of plugin `%s' failed. "
431 "Will suspend it for %i seconds.",
432 rf->rf_name,
433 (int) rf->rf_effective_interval.tv_sec);
434 }
435 else
436 {
437 /* Success: Restore the interval, if it was changed. */
438 rf->rf_effective_interval = rf->rf_interval;
439 }
441 /* update the ``next read due'' field */
442 gettimeofday (&now, /* timezone = */ NULL);
444 DEBUG ("plugin_read_thread: Effective interval of the "
445 "%s plugin is %i.%09i.",
446 rf->rf_name,
447 (int) rf->rf_effective_interval.tv_sec,
448 (int) rf->rf_effective_interval.tv_nsec);
450 /* Calculate the next (absolute) time at which this function
451 * should be called. */
452 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
453 + rf->rf_effective_interval.tv_sec;
454 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
455 + rf->rf_effective_interval.tv_nsec;
456 NORMALIZE_TIMESPEC (rf->rf_next_read);
458 /* Check, if `rf_next_read' is in the past. */
459 if ((rf->rf_next_read.tv_sec < now.tv_sec)
460 || ((rf->rf_next_read.tv_sec == now.tv_sec)
461 && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
462 {
463 /* `rf_next_read' is in the past. Insert `now'
464 * so this value doesn't trail off into the
465 * past too much. */
466 rf->rf_next_read.tv_sec = now.tv_sec;
467 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
468 }
470 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
471 rf->rf_name,
472 (int) rf->rf_next_read.tv_sec,
473 (int) rf->rf_next_read.tv_nsec);
475 /* Re-insert this read function into the heap again. */
476 c_heap_insert (read_heap, rf);
477 } /* while (read_loop) */
479 pthread_exit (NULL);
480 return ((void *) 0);
481 } /* void *plugin_read_thread */
483 static void start_read_threads (int num)
484 {
485 int i;
487 if (read_threads != NULL)
488 return;
490 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
491 if (read_threads == NULL)
492 {
493 ERROR ("plugin: start_read_threads: calloc failed.");
494 return;
495 }
497 read_threads_num = 0;
498 for (i = 0; i < num; i++)
499 {
500 if (pthread_create (read_threads + read_threads_num, NULL,
501 plugin_read_thread, NULL) == 0)
502 {
503 read_threads_num++;
504 }
505 else
506 {
507 ERROR ("plugin: start_read_threads: pthread_create failed.");
508 return;
509 }
510 } /* for (i) */
511 } /* void start_read_threads */
513 static void stop_read_threads (void)
514 {
515 int i;
517 if (read_threads == NULL)
518 return;
520 INFO ("collectd: Stopping %i read threads.", read_threads_num);
522 pthread_mutex_lock (&read_lock);
523 read_loop = 0;
524 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
525 pthread_cond_broadcast (&read_cond);
526 pthread_mutex_unlock (&read_lock);
528 for (i = 0; i < read_threads_num; i++)
529 {
530 if (pthread_join (read_threads[i], NULL) != 0)
531 {
532 ERROR ("plugin: stop_read_threads: pthread_join failed.");
533 }
534 read_threads[i] = (pthread_t) 0;
535 }
536 sfree (read_threads);
537 read_threads_num = 0;
538 } /* void stop_read_threads */
540 /*
541 * Public functions
542 */
543 void plugin_set_dir (const char *dir)
544 {
545 if (plugindir != NULL)
546 free (plugindir);
548 if (dir == NULL)
549 plugindir = NULL;
550 else if ((plugindir = strdup (dir)) == NULL)
551 {
552 char errbuf[1024];
553 ERROR ("strdup failed: %s",
554 sstrerror (errno, errbuf, sizeof (errbuf)));
555 }
556 }
558 #define BUFSIZE 512
559 int plugin_load (const char *type, uint32_t flags)
560 {
561 DIR *dh;
562 const char *dir;
563 char filename[BUFSIZE] = "";
564 char typename[BUFSIZE];
565 int typename_len;
566 int ret;
567 struct stat statbuf;
568 struct dirent *de;
569 int status;
571 DEBUG ("type = %s", type);
573 dir = plugin_get_dir ();
574 ret = 1;
576 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
577 * type when matching the filename */
578 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
579 if ((status < 0) || ((size_t) status >= sizeof (typename)))
580 {
581 WARNING ("snprintf: truncated: `%s.so'", type);
582 return (-1);
583 }
584 typename_len = strlen (typename);
586 if ((dh = opendir (dir)) == NULL)
587 {
588 char errbuf[1024];
589 ERROR ("opendir (%s): %s", dir,
590 sstrerror (errno, errbuf, sizeof (errbuf)));
591 return (-1);
592 }
594 while ((de = readdir (dh)) != NULL)
595 {
596 if (strncasecmp (de->d_name, typename, typename_len))
597 continue;
599 status = ssnprintf (filename, sizeof (filename),
600 "%s/%s", dir, de->d_name);
601 if ((status < 0) || ((size_t) status >= sizeof (filename)))
602 {
603 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
604 continue;
605 }
607 if (lstat (filename, &statbuf) == -1)
608 {
609 char errbuf[1024];
610 WARNING ("stat %s: %s", filename,
611 sstrerror (errno, errbuf, sizeof (errbuf)));
612 continue;
613 }
614 else if (!S_ISREG (statbuf.st_mode))
615 {
616 /* don't follow symlinks */
617 WARNING ("stat %s: not a regular file", filename);
618 continue;
619 }
621 if (plugin_load_file (filename, flags) == 0)
622 {
623 /* success */
624 ret = 0;
625 break;
626 }
627 else
628 {
629 fprintf (stderr, "Unable to load plugin %s.\n", type);
630 }
631 }
633 closedir (dh);
635 if (filename[0] == '\0')
636 fprintf (stderr, "Could not find plugin %s.\n", type);
638 return (ret);
639 }
641 /*
642 * The `register_*' functions follow
643 */
644 int plugin_register_config (const char *name,
645 int (*callback) (const char *key, const char *val),
646 const char **keys, int keys_num)
647 {
648 cf_register (name, callback, keys, keys_num);
649 return (0);
650 } /* int plugin_register_config */
652 int plugin_register_complex_config (const char *type,
653 int (*callback) (oconfig_item_t *))
654 {
655 return (cf_register_complex (type, callback));
656 } /* int plugin_register_complex_config */
658 int plugin_register_init (const char *name,
659 int (*callback) (void))
660 {
661 return (create_register_callback (&list_init, name, (void *) callback,
662 /* user_data = */ NULL));
663 } /* plugin_register_init */
665 static int plugin_compare_read_func (const void *arg0, const void *arg1)
666 {
667 const read_func_t *rf0;
668 const read_func_t *rf1;
670 rf0 = arg0;
671 rf1 = arg1;
673 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
674 return (-1);
675 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
676 return (1);
677 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
678 return (-1);
679 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
680 return (1);
681 else
682 return (0);
683 } /* int plugin_compare_read_func */
685 /* Add a read function to both, the heap and a linked list. The linked list if
686 * used to look-up read functions, especially for the remove function. The heap
687 * is used to determine which plugin to read next. */
688 static int plugin_insert_read (read_func_t *rf)
689 {
690 int status;
691 llentry_t *le;
693 pthread_mutex_lock (&read_lock);
695 if (read_list == NULL)
696 {
697 read_list = llist_create ();
698 if (read_list == NULL)
699 {
700 pthread_mutex_unlock (&read_lock);
701 ERROR ("plugin_insert_read: read_list failed.");
702 return (-1);
703 }
704 }
706 if (read_heap == NULL)
707 {
708 read_heap = c_heap_create (plugin_compare_read_func);
709 if (read_heap == NULL)
710 {
711 pthread_mutex_unlock (&read_lock);
712 ERROR ("plugin_insert_read: c_heap_create failed.");
713 return (-1);
714 }
715 }
717 le = llentry_create (rf->rf_name, rf);
718 if (le == NULL)
719 {
720 pthread_mutex_unlock (&read_lock);
721 ERROR ("plugin_insert_read: llentry_create failed.");
722 return (-1);
723 }
725 status = c_heap_insert (read_heap, rf);
726 if (status != 0)
727 {
728 pthread_mutex_unlock (&read_lock);
729 ERROR ("plugin_insert_read: c_heap_insert failed.");
730 llentry_destroy (le);
731 return (-1);
732 }
734 /* This does not fail. */
735 llist_append (read_list, le);
737 pthread_mutex_unlock (&read_lock);
738 return (0);
739 } /* int plugin_insert_read */
741 int plugin_register_read (const char *name,
742 int (*callback) (void))
743 {
744 read_func_t *rf;
746 rf = (read_func_t *) malloc (sizeof (read_func_t));
747 if (rf == NULL)
748 {
749 char errbuf[1024];
750 ERROR ("plugin_register_read: malloc failed: %s",
751 sstrerror (errno, errbuf, sizeof (errbuf)));
752 return (-1);
753 }
755 memset (rf, 0, sizeof (read_func_t));
756 rf->rf_callback = (void *) callback;
757 rf->rf_udata.data = NULL;
758 rf->rf_udata.free_func = NULL;
759 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
760 rf->rf_type = RF_SIMPLE;
761 rf->rf_interval.tv_sec = 0;
762 rf->rf_interval.tv_nsec = 0;
763 rf->rf_effective_interval = rf->rf_interval;
765 return (plugin_insert_read (rf));
766 } /* int plugin_register_read */
768 int plugin_register_complex_read (const char *name,
769 plugin_read_cb callback,
770 const struct timespec *interval,
771 user_data_t *user_data)
772 {
773 read_func_t *rf;
775 rf = (read_func_t *) malloc (sizeof (read_func_t));
776 if (rf == NULL)
777 {
778 ERROR ("plugin_register_complex_read: malloc failed.");
779 return (-1);
780 }
782 memset (rf, 0, sizeof (read_func_t));
783 rf->rf_callback = (void *) callback;
784 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
785 rf->rf_type = RF_COMPLEX;
786 if (interval != NULL)
787 {
788 rf->rf_interval = *interval;
789 }
790 rf->rf_effective_interval = rf->rf_interval;
792 /* Set user data */
793 if (user_data == NULL)
794 {
795 rf->rf_udata.data = NULL;
796 rf->rf_udata.free_func = NULL;
797 }
798 else
799 {
800 rf->rf_udata = *user_data;
801 }
803 return (plugin_insert_read (rf));
804 } /* int plugin_register_complex_read */
806 int plugin_register_write (const char *name,
807 plugin_write_cb callback, user_data_t *ud)
808 {
809 return (create_register_callback (&list_write, name,
810 (void *) callback, ud));
811 } /* int plugin_register_write */
813 int plugin_register_flush (const char *name,
814 plugin_flush_cb callback, user_data_t *ud)
815 {
816 return (create_register_callback (&list_flush, name,
817 (void *) callback, ud));
818 } /* int plugin_register_flush */
820 int plugin_register_shutdown (char *name,
821 int (*callback) (void))
822 {
823 return (create_register_callback (&list_shutdown, name,
824 (void *) callback, /* user_data = */ NULL));
825 } /* int plugin_register_shutdown */
827 int plugin_register_data_set (const data_set_t *ds)
828 {
829 data_set_t *ds_copy;
830 int i;
832 if ((data_sets != NULL)
833 && (c_avl_get (data_sets, ds->type, NULL) == 0))
834 {
835 NOTICE ("Replacing DS `%s' with another version.", ds->type);
836 plugin_unregister_data_set (ds->type);
837 }
838 else if (data_sets == NULL)
839 {
840 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
841 if (data_sets == NULL)
842 return (-1);
843 }
845 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
846 if (ds_copy == NULL)
847 return (-1);
848 memcpy(ds_copy, ds, sizeof (data_set_t));
850 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
851 * ds->ds_num);
852 if (ds_copy->ds == NULL)
853 {
854 free (ds_copy);
855 return (-1);
856 }
858 for (i = 0; i < ds->ds_num; i++)
859 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
861 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
862 } /* int plugin_register_data_set */
864 int plugin_register_log (const char *name,
865 plugin_log_cb callback, user_data_t *ud)
866 {
867 return (create_register_callback (&list_log, name,
868 (void *) callback, ud));
869 } /* int plugin_register_log */
871 int plugin_register_notification (const char *name,
872 plugin_notification_cb callback, user_data_t *ud)
873 {
874 return (create_register_callback (&list_notification, name,
875 (void *) callback, ud));
876 } /* int plugin_register_log */
878 int plugin_unregister_config (const char *name)
879 {
880 cf_unregister (name);
881 return (0);
882 } /* int plugin_unregister_config */
884 int plugin_unregister_complex_config (const char *name)
885 {
886 cf_unregister_complex (name);
887 return (0);
888 } /* int plugin_unregister_complex_config */
890 int plugin_unregister_init (const char *name)
891 {
892 return (plugin_unregister (list_init, name));
893 }
895 int plugin_unregister_read (const char *name) /* {{{ */
896 {
897 llentry_t *le;
898 read_func_t *rf;
900 if (name == NULL)
901 return (-ENOENT);
903 pthread_mutex_lock (&read_lock);
905 if (read_list == NULL)
906 {
907 pthread_mutex_unlock (&read_lock);
908 return (-ENOENT);
909 }
911 le = llist_search (read_list, name);
912 if (le == NULL)
913 {
914 pthread_mutex_unlock (&read_lock);
915 WARNING ("plugin_unregister_read: No such read function: %s",
916 name);
917 return (-ENOENT);
918 }
920 llist_remove (read_list, le);
922 rf = le->value;
923 assert (rf != NULL);
924 rf->rf_type = RF_REMOVE;
926 pthread_mutex_unlock (&read_lock);
928 llentry_destroy (le);
930 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
932 return (0);
933 } /* }}} int plugin_unregister_read */
935 int plugin_unregister_write (const char *name)
936 {
937 return (plugin_unregister (list_write, name));
938 }
940 int plugin_unregister_flush (const char *name)
941 {
942 return (plugin_unregister (list_flush, name));
943 }
945 int plugin_unregister_shutdown (const char *name)
946 {
947 return (plugin_unregister (list_shutdown, name));
948 }
950 int plugin_unregister_data_set (const char *name)
951 {
952 data_set_t *ds;
954 if (data_sets == NULL)
955 return (-1);
957 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
958 return (-1);
960 sfree (ds->ds);
961 sfree (ds);
963 return (0);
964 } /* int plugin_unregister_data_set */
966 int plugin_unregister_log (const char *name)
967 {
968 return (plugin_unregister (list_log, name));
969 }
971 int plugin_unregister_notification (const char *name)
972 {
973 return (plugin_unregister (list_notification, name));
974 }
976 void plugin_init_all (void)
977 {
978 const char *chain_name;
979 llentry_t *le;
980 int status;
982 /* Init the value cache */
983 uc_init ();
985 chain_name = global_option_get ("PreCacheChain");
986 pre_cache_chain = fc_chain_get_by_name (chain_name);
988 chain_name = global_option_get ("PostCacheChain");
989 post_cache_chain = fc_chain_get_by_name (chain_name);
992 if ((list_init == NULL) && (read_heap == NULL))
993 return;
995 /* Calling all init callbacks before checking if read callbacks
996 * are available allows the init callbacks to register the read
997 * callback. */
998 le = llist_head (list_init);
999 while (le != NULL)
1000 {
1001 callback_func_t *cf;
1002 plugin_init_cb callback;
1004 cf = le->value;
1005 callback = cf->cf_callback;
1006 status = (*callback) ();
1008 if (status != 0)
1009 {
1010 ERROR ("Initialization of plugin `%s' "
1011 "failed with status %i. "
1012 "Plugin will be unloaded.",
1013 le->key, status);
1014 /* Plugins that register read callbacks from the init
1015 * callback should take care of appropriate error
1016 * handling themselves. */
1017 /* FIXME: Unload _all_ functions */
1018 plugin_unregister_read (le->key);
1019 }
1021 le = le->next;
1022 }
1024 /* Start read-threads */
1025 if (read_heap != NULL)
1026 {
1027 const char *rt;
1028 int num;
1029 rt = global_option_get ("ReadThreads");
1030 num = atoi (rt);
1031 if (num != -1)
1032 start_read_threads ((num > 0) ? num : 5);
1033 }
1034 } /* void plugin_init_all */
1036 /* TODO: Rename this function. */
1037 void plugin_read_all (void)
1038 {
1039 uc_check_timeout ();
1041 return;
1042 } /* void plugin_read_all */
1044 /* Read function called when the `-T' command line argument is given. */
1045 int plugin_read_all_once (void)
1046 {
1047 int status;
1048 int return_status = 0;
1050 if (read_heap == NULL)
1051 {
1052 NOTICE ("No read-functions are registered.");
1053 return (0);
1054 }
1056 while (42)
1057 {
1058 read_func_t *rf;
1060 rf = c_heap_get_root (read_heap);
1061 if (rf == NULL)
1062 break;
1064 if (rf->rf_type == RF_SIMPLE)
1065 {
1066 int (*callback) (void);
1068 callback = rf->rf_callback;
1069 status = (*callback) ();
1070 }
1071 else
1072 {
1073 plugin_read_cb callback;
1075 callback = rf->rf_callback;
1076 status = (*callback) (&rf->rf_udata);
1077 }
1079 if (status != 0)
1080 {
1081 NOTICE ("read-function of plugin `%s' failed.",
1082 rf->rf_name);
1083 return_status = -1;
1084 }
1086 destroy_callback ((void *) rf);
1087 }
1089 return (return_status);
1090 } /* int plugin_read_all_once */
1092 int plugin_write (const char *plugin, /* {{{ */
1093 const data_set_t *ds, const value_list_t *vl)
1094 {
1095 llentry_t *le;
1096 int status;
1098 if (vl == NULL)
1099 return (EINVAL);
1101 if (list_write == NULL)
1102 return (ENOENT);
1104 if (ds == NULL)
1105 {
1106 ds = plugin_get_ds (vl->type);
1107 if (ds == NULL)
1108 {
1109 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1110 return (ENOENT);
1111 }
1112 }
1114 if (plugin == NULL)
1115 {
1116 int success = 0;
1117 int failure = 0;
1119 le = llist_head (list_write);
1120 while (le != NULL)
1121 {
1122 callback_func_t *cf = le->value;
1123 plugin_write_cb callback;
1125 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1126 callback = cf->cf_callback;
1127 status = (*callback) (ds, vl, &cf->cf_udata);
1128 if (status != 0)
1129 failure++;
1130 else
1131 success++;
1133 le = le->next;
1134 }
1136 if ((success == 0) && (failure != 0))
1137 status = -1;
1138 else
1139 status = 0;
1140 }
1141 else /* plugin != NULL */
1142 {
1143 callback_func_t *cf;
1144 plugin_write_cb callback;
1146 le = llist_head (list_write);
1147 while (le != NULL)
1148 {
1149 if (strcasecmp (plugin, le->key) == 0)
1150 break;
1152 le = le->next;
1153 }
1155 if (le == NULL)
1156 return (ENOENT);
1158 cf = le->value;
1160 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1161 callback = cf->cf_callback;
1162 status = (*callback) (ds, vl, &cf->cf_udata);
1163 }
1165 return (status);
1166 } /* }}} int plugin_write */
1168 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1169 {
1170 llentry_t *le;
1172 if (list_flush == NULL)
1173 return (0);
1175 le = llist_head (list_flush);
1176 while (le != NULL)
1177 {
1178 callback_func_t *cf;
1179 plugin_flush_cb callback;
1181 if ((plugin != NULL)
1182 && (strcmp (plugin, le->key) != 0))
1183 {
1184 le = le->next;
1185 continue;
1186 }
1188 cf = le->value;
1189 callback = cf->cf_callback;
1191 (*callback) (timeout, identifier, &cf->cf_udata);
1193 le = le->next;
1194 }
1195 return (0);
1196 } /* int plugin_flush */
1198 void plugin_shutdown_all (void)
1199 {
1200 llentry_t *le;
1202 stop_read_threads ();
1204 destroy_all_callbacks (&list_init);
1206 pthread_mutex_lock (&read_lock);
1207 llist_destroy (read_list);
1208 read_list = NULL;
1209 pthread_mutex_unlock (&read_lock);
1211 destroy_read_heap ();
1213 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1214 /* identifier = */ NULL);
1216 le = NULL;
1217 if (list_shutdown != NULL)
1218 le = llist_head (list_shutdown);
1220 while (le != NULL)
1221 {
1222 callback_func_t *cf;
1223 plugin_shutdown_cb callback;
1225 cf = le->value;
1226 callback = cf->cf_callback;
1228 /* Advance the pointer before calling the callback allows
1229 * shutdown functions to unregister themselves. If done the
1230 * other way around the memory `le' points to will be freed
1231 * after callback returns. */
1232 le = le->next;
1234 (*callback) ();
1235 }
1237 /* Write plugins which use the `user_data' pointer usually need the
1238 * same data available to the flush callback. If this is the case, set
1239 * the free_function to NULL when registering the flush callback and to
1240 * the real free function when registering the write callback. This way
1241 * the data isn't freed twice. */
1242 destroy_all_callbacks (&list_flush);
1243 destroy_all_callbacks (&list_write);
1245 destroy_all_callbacks (&list_notification);
1246 destroy_all_callbacks (&list_shutdown);
1247 destroy_all_callbacks (&list_log);
1248 } /* void plugin_shutdown_all */
1250 int plugin_dispatch_values (value_list_t *vl)
1251 {
1252 int status;
1253 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1255 value_t *saved_values;
1256 int saved_values_len;
1258 data_set_t *ds;
1260 int free_meta_data = 0;
1262 if ((vl == NULL) || (vl->type[0] == 0)
1263 || (vl->values == NULL) || (vl->values_len < 1))
1264 {
1265 ERROR ("plugin_dispatch_values: Invalid value list.");
1266 return (-1);
1267 }
1269 /* Free meta data only if the calling function didn't specify any. In
1270 * this case matches and targets may add some and the calling function
1271 * may not expect (and therefore free) that data. */
1272 if (vl->meta == NULL)
1273 free_meta_data = 1;
1275 if (list_write == NULL)
1276 c_complain_once (LOG_WARNING, &no_write_complaint,
1277 "plugin_dispatch_values: No write callback has been "
1278 "registered. Please load at least one output plugin, "
1279 "if you want the collected data to be stored.");
1281 if (data_sets == NULL)
1282 {
1283 ERROR ("plugin_dispatch_values: No data sets registered. "
1284 "Could the types database be read? Check "
1285 "your `TypesDB' setting!");
1286 return (-1);
1287 }
1289 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1290 {
1291 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1292 return (-1);
1293 }
1295 if (vl->time == 0)
1296 vl->time = time (NULL);
1298 if (vl->interval <= 0)
1299 vl->interval = interval_g;
1301 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1302 "host = %s; "
1303 "plugin = %s; plugin_instance = %s; "
1304 "type = %s; type_instance = %s;",
1305 (unsigned int) vl->time, vl->interval,
1306 vl->host,
1307 vl->plugin, vl->plugin_instance,
1308 vl->type, vl->type_instance);
1310 #if COLLECT_DEBUG
1311 assert (0 == strcmp (ds->type, vl->type));
1312 #else
1313 if (0 != strcmp (ds->type, vl->type))
1314 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1315 ds->type, vl->type);
1316 #endif
1318 #if COLLECT_DEBUG
1319 assert (ds->ds_num == vl->values_len);
1320 #else
1321 if (ds->ds_num != vl->values_len)
1322 {
1323 ERROR ("plugin_dispatch_values: ds->type = %s: "
1324 "(ds->ds_num = %i) != "
1325 "(vl->values_len = %i)",
1326 ds->type, ds->ds_num, vl->values_len);
1327 return (-1);
1328 }
1329 #endif
1331 escape_slashes (vl->host, sizeof (vl->host));
1332 escape_slashes (vl->plugin, sizeof (vl->plugin));
1333 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1334 escape_slashes (vl->type, sizeof (vl->type));
1335 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1337 /* Copy the values. This way, we can assure `targets' that they get
1338 * dynamically allocated values, which they can free and replace if
1339 * they like. */
1340 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1341 {
1342 saved_values = vl->values;
1343 saved_values_len = vl->values_len;
1345 vl->values = (value_t *) calloc (vl->values_len,
1346 sizeof (*vl->values));
1347 if (vl->values == NULL)
1348 {
1349 ERROR ("plugin_dispatch_values: calloc failed.");
1350 vl->values = saved_values;
1351 return (-1);
1352 }
1353 memcpy (vl->values, saved_values,
1354 vl->values_len * sizeof (*vl->values));
1355 }
1356 else /* if ((pre == NULL) && (post == NULL)) */
1357 {
1358 saved_values = NULL;
1359 saved_values_len = 0;
1360 }
1362 if (pre_cache_chain != NULL)
1363 {
1364 status = fc_process_chain (ds, vl, pre_cache_chain);
1365 if (status < 0)
1366 {
1367 WARNING ("plugin_dispatch_values: Running the "
1368 "pre-cache chain failed with "
1369 "status %i (%#x).",
1370 status, status);
1371 }
1372 else if (status == FC_TARGET_STOP)
1373 {
1374 /* Restore the state of the value_list so that plugins
1375 * don't get confused.. */
1376 if (saved_values != NULL)
1377 {
1378 free (vl->values);
1379 vl->values = saved_values;
1380 vl->values_len = saved_values_len;
1381 }
1382 return (0);
1383 }
1384 }
1386 /* Update the value cache */
1387 uc_update (ds, vl);
1389 /* Initiate threshold checking */
1390 ut_check_threshold (ds, vl);
1392 if (post_cache_chain != NULL)
1393 {
1394 status = fc_process_chain (ds, vl, post_cache_chain);
1395 if (status < 0)
1396 {
1397 WARNING ("plugin_dispatch_values: Running the "
1398 "post-cache chain failed with "
1399 "status %i (%#x).",
1400 status, status);
1401 }
1402 }
1403 else
1404 fc_default_action (ds, vl);
1406 /* Restore the state of the value_list so that plugins don't get
1407 * confused.. */
1408 if (saved_values != NULL)
1409 {
1410 free (vl->values);
1411 vl->values = saved_values;
1412 vl->values_len = saved_values_len;
1413 }
1415 if ((free_meta_data != 0) && (vl->meta != NULL))
1416 {
1417 meta_data_destroy (vl->meta);
1418 vl->meta = NULL;
1419 }
1421 return (0);
1422 } /* int plugin_dispatch_values */
1424 int plugin_dispatch_notification (const notification_t *notif)
1425 {
1426 llentry_t *le;
1427 /* Possible TODO: Add flap detection here */
1429 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1430 "time = %u; host = %s;",
1431 notif->severity, notif->message,
1432 (unsigned int) notif->time, notif->host);
1434 /* Nobody cares for notifications */
1435 if (list_notification == NULL)
1436 return (-1);
1438 le = llist_head (list_notification);
1439 while (le != NULL)
1440 {
1441 callback_func_t *cf;
1442 plugin_notification_cb callback;
1443 int status;
1445 cf = le->value;
1446 callback = cf->cf_callback;
1447 status = (*callback) (notif, &cf->cf_udata);
1448 if (status != 0)
1449 {
1450 WARNING ("plugin_dispatch_notification: Notification "
1451 "callback %s returned %i.",
1452 le->key, status);
1453 }
1455 le = le->next;
1456 }
1458 return (0);
1459 } /* int plugin_dispatch_notification */
1461 void plugin_log (int level, const char *format, ...)
1462 {
1463 char msg[1024];
1464 va_list ap;
1465 llentry_t *le;
1467 if (list_log == NULL)
1468 {
1469 va_start (ap, format);
1470 vfprintf (stderr, format, ap);
1471 va_end (ap);
1472 return;
1473 }
1475 #if !COLLECT_DEBUG
1476 if (level >= LOG_DEBUG)
1477 return;
1478 #endif
1480 va_start (ap, format);
1481 vsnprintf (msg, sizeof (msg), format, ap);
1482 msg[sizeof (msg) - 1] = '\0';
1483 va_end (ap);
1485 le = llist_head (list_log);
1486 while (le != NULL)
1487 {
1488 callback_func_t *cf;
1489 plugin_log_cb callback;
1491 cf = le->value;
1492 callback = cf->cf_callback;
1494 (*callback) (level, msg, &cf->cf_udata);
1496 le = le->next;
1497 }
1498 } /* void plugin_log */
1500 const data_set_t *plugin_get_ds (const char *name)
1501 {
1502 data_set_t *ds;
1504 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1505 {
1506 DEBUG ("No such dataset registered: %s", name);
1507 return (NULL);
1508 }
1510 return (ds);
1511 } /* data_set_t *plugin_get_ds */
1513 static int plugin_notification_meta_add (notification_t *n,
1514 const char *name,
1515 enum notification_meta_type_e type,
1516 const void *value)
1517 {
1518 notification_meta_t *meta;
1519 notification_meta_t *tail;
1521 if ((n == NULL) || (name == NULL) || (value == NULL))
1522 {
1523 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1524 return (-1);
1525 }
1527 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1528 if (meta == NULL)
1529 {
1530 ERROR ("plugin_notification_meta_add: malloc failed.");
1531 return (-1);
1532 }
1533 memset (meta, 0, sizeof (notification_meta_t));
1535 sstrncpy (meta->name, name, sizeof (meta->name));
1536 meta->type = type;
1538 switch (type)
1539 {
1540 case NM_TYPE_STRING:
1541 {
1542 meta->nm_value.nm_string = strdup ((const char *) value);
1543 if (meta->nm_value.nm_string == NULL)
1544 {
1545 ERROR ("plugin_notification_meta_add: strdup failed.");
1546 sfree (meta);
1547 return (-1);
1548 }
1549 break;
1550 }
1551 case NM_TYPE_SIGNED_INT:
1552 {
1553 meta->nm_value.nm_signed_int = *((int64_t *) value);
1554 break;
1555 }
1556 case NM_TYPE_UNSIGNED_INT:
1557 {
1558 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1559 break;
1560 }
1561 case NM_TYPE_DOUBLE:
1562 {
1563 meta->nm_value.nm_double = *((double *) value);
1564 break;
1565 }
1566 case NM_TYPE_BOOLEAN:
1567 {
1568 meta->nm_value.nm_boolean = *((bool *) value);
1569 break;
1570 }
1571 default:
1572 {
1573 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1574 sfree (meta);
1575 return (-1);
1576 }
1577 } /* switch (type) */
1579 meta->next = NULL;
1580 tail = n->meta;
1581 while ((tail != NULL) && (tail->next != NULL))
1582 tail = tail->next;
1584 if (tail == NULL)
1585 n->meta = meta;
1586 else
1587 tail->next = meta;
1589 return (0);
1590 } /* int plugin_notification_meta_add */
1592 int plugin_notification_meta_add_string (notification_t *n,
1593 const char *name,
1594 const char *value)
1595 {
1596 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1597 }
1599 int plugin_notification_meta_add_signed_int (notification_t *n,
1600 const char *name,
1601 int64_t value)
1602 {
1603 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1604 }
1606 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1607 const char *name,
1608 uint64_t value)
1609 {
1610 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1611 }
1613 int plugin_notification_meta_add_double (notification_t *n,
1614 const char *name,
1615 double value)
1616 {
1617 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1618 }
1620 int plugin_notification_meta_add_boolean (notification_t *n,
1621 const char *name,
1622 bool value)
1623 {
1624 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1625 }
1627 int plugin_notification_meta_copy (notification_t *dst,
1628 const notification_t *src)
1629 {
1630 notification_meta_t *meta;
1632 assert (dst != NULL);
1633 assert (src != NULL);
1634 assert (dst != src);
1635 assert ((src->meta == NULL) || (src->meta != dst->meta));
1637 for (meta = src->meta; meta != NULL; meta = meta->next)
1638 {
1639 if (meta->type == NM_TYPE_STRING)
1640 plugin_notification_meta_add_string (dst, meta->name,
1641 meta->nm_value.nm_string);
1642 else if (meta->type == NM_TYPE_SIGNED_INT)
1643 plugin_notification_meta_add_signed_int (dst, meta->name,
1644 meta->nm_value.nm_signed_int);
1645 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1646 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1647 meta->nm_value.nm_unsigned_int);
1648 else if (meta->type == NM_TYPE_DOUBLE)
1649 plugin_notification_meta_add_double (dst, meta->name,
1650 meta->nm_value.nm_double);
1651 else if (meta->type == NM_TYPE_BOOLEAN)
1652 plugin_notification_meta_add_boolean (dst, meta->name,
1653 meta->nm_value.nm_boolean);
1654 }
1656 return (0);
1657 } /* int plugin_notification_meta_copy */
1659 int plugin_notification_meta_free (notification_meta_t *n)
1660 {
1661 notification_meta_t *this;
1662 notification_meta_t *next;
1664 if (n == NULL)
1665 {
1666 ERROR ("plugin_notification_meta_free: n == NULL!");
1667 return (-1);
1668 }
1670 this = n;
1671 while (this != NULL)
1672 {
1673 next = this->next;
1675 if (this->type == NM_TYPE_STRING)
1676 {
1677 free ((char *)this->nm_value.nm_string);
1678 this->nm_value.nm_string = NULL;
1679 }
1680 sfree (this);
1682 this = next;
1683 }
1685 return (0);
1686 } /* int plugin_notification_meta_free */
1688 /* vim: set sw=8 ts=8 noet fdm=marker : */