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_head_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: create_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: create_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: create_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 destroy_callback (old_cf);
214 sfree (key);
215 }
217 return (0);
218 } /* }}} int register_callback */
220 static int create_register_callback (llist_t **list, /* {{{ */
221 const char *name, void *callback, user_data_t *ud)
222 {
223 callback_func_t *cf;
225 cf = (callback_func_t *) malloc (sizeof (*cf));
226 if (cf == NULL)
227 {
228 ERROR ("plugin: create_register_callback: malloc failed.");
229 return (-1);
230 }
231 memset (cf, 0, sizeof (*cf));
233 cf->cf_callback = callback;
234 if (ud == NULL)
235 {
236 cf->cf_udata.data = NULL;
237 cf->cf_udata.free_func = NULL;
238 }
239 else
240 {
241 cf->cf_udata = *ud;
242 }
244 return (register_callback (list, name, cf));
245 } /* }}} int create_register_callback */
247 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
248 {
249 llentry_t *e;
251 if (list == NULL)
252 return (-1);
254 e = llist_search (list, name);
255 if (e == NULL)
256 return (-1);
258 llist_remove (list, e);
260 sfree (e->key);
261 destroy_callback (e->value);
263 llentry_destroy (e);
265 return (0);
266 } /* }}} int plugin_unregister */
268 /*
269 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
270 * object, but it will bitch about a shared object not having a
271 * ``module_register'' symbol..
272 */
273 static int plugin_load_file (char *file)
274 {
275 lt_dlhandle dlh;
276 void (*reg_handle) (void);
278 DEBUG ("file = %s", file);
280 lt_dlinit ();
281 lt_dlerror (); /* clear errors */
283 if ((dlh = lt_dlopen (file)) == NULL)
284 {
285 const char *error = lt_dlerror ();
287 ERROR ("lt_dlopen (%s) failed: %s", file, error);
288 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
289 return (1);
290 }
292 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
293 {
294 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
295 file, lt_dlerror ());
296 lt_dlclose (dlh);
297 return (-1);
298 }
300 (*reg_handle) ();
302 return (0);
303 }
305 static _Bool timeout_reached(struct timespec timeout)
306 {
307 struct timeval now;
308 gettimeofday(&now, NULL);
309 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
310 }
312 static void *plugin_read_thread (void __attribute__((unused)) *args)
313 {
314 while (read_loop != 0)
315 {
316 read_func_t *rf;
317 struct timeval now;
318 int status;
319 int rf_type;
320 int rc;
322 /* Get the read function that needs to be read next. */
323 rf = c_head_get_root (read_heap);
324 if (rf == NULL)
325 {
326 struct timespec abstime;
328 gettimeofday (&now, /* timezone = */ NULL);
330 abstime.tv_sec = now.tv_sec + interval_g;
331 abstime.tv_nsec = 1000 * now.tv_usec;
333 pthread_mutex_lock (&read_lock);
334 pthread_cond_timedwait (&read_cond, &read_lock,
335 &abstime);
336 pthread_mutex_unlock (&read_lock);
337 continue;
338 }
340 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
341 {
342 gettimeofday (&now, /* timezone = */ NULL);
344 rf->rf_interval.tv_sec = interval_g;
345 rf->rf_interval.tv_nsec = 0;
347 rf->rf_effective_interval = rf->rf_interval;
349 rf->rf_next_read.tv_sec = now.tv_sec;
350 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
351 }
353 /* sleep until this entry is due,
354 * using pthread_cond_timedwait */
355 pthread_mutex_lock (&read_lock);
356 /* In pthread_cond_timedwait, spurious wakeups are possible
357 * (and really happen, at least on NetBSD with > 1 CPU), thus
358 * we need to re-evaluate the condition every time
359 * pthread_cond_timedwait returns. */
360 rc = 0;
361 while (!timeout_reached(rf->rf_next_read) && rc == 0) {
362 rc = pthread_cond_timedwait (&read_cond, &read_lock,
363 &rf->rf_next_read);
364 }
366 /* Must hold `real_lock' when accessing `rf->rf_type'. */
367 rf_type = rf->rf_type;
368 pthread_mutex_unlock (&read_lock);
370 /* Check if we're supposed to stop.. This may have interrupted
371 * the sleep, too. */
372 if (read_loop == 0)
373 {
374 /* Insert `rf' again, so it can be free'd correctly */
375 c_heap_insert (read_heap, rf);
376 break;
377 }
379 /* The entry has been marked for deletion. The linked list
380 * entry has already been removed by `plugin_unregister_read'.
381 * All we have to do here is free the `read_func_t' and
382 * continue. */
383 if (rf_type == RF_REMOVE)
384 {
385 DEBUG ("plugin_read_thread: Destroying the `%s' "
386 "callback.", rf->rf_name);
387 destroy_callback ((callback_func_t *) rf);
388 rf = NULL;
389 continue;
390 }
392 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
394 if (rf_type == RF_SIMPLE)
395 {
396 int (*callback) (void);
398 callback = rf->rf_callback;
399 status = (*callback) ();
400 }
401 else
402 {
403 plugin_read_cb callback;
405 assert (rf_type == RF_COMPLEX);
407 callback = rf->rf_callback;
408 status = (*callback) (&rf->rf_udata);
409 }
411 /* If the function signals failure, we will increase the
412 * intervals in which it will be called. */
413 if (status != 0)
414 {
415 rf->rf_effective_interval.tv_sec *= 2;
416 rf->rf_effective_interval.tv_nsec *= 2;
417 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
419 if (rf->rf_effective_interval.tv_sec >= 86400)
420 {
421 rf->rf_effective_interval.tv_sec = 86400;
422 rf->rf_effective_interval.tv_nsec = 0;
423 }
425 NOTICE ("read-function of plugin `%s' failed. "
426 "Will suspend it for %i seconds.",
427 rf->rf_name,
428 (int) rf->rf_effective_interval.tv_sec);
429 }
430 else
431 {
432 /* Success: Restore the interval, if it was changed. */
433 rf->rf_effective_interval = rf->rf_interval;
434 }
436 /* update the ``next read due'' field */
437 gettimeofday (&now, /* timezone = */ NULL);
439 DEBUG ("plugin_read_thread: Effective interval of the "
440 "%s plugin is %i.%09i.",
441 rf->rf_name,
442 (int) rf->rf_effective_interval.tv_sec,
443 (int) rf->rf_effective_interval.tv_nsec);
445 /* Calculate the next (absolute) time at which this function
446 * should be called. */
447 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
448 + rf->rf_effective_interval.tv_sec;
449 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
450 + rf->rf_effective_interval.tv_nsec;
451 NORMALIZE_TIMESPEC (rf->rf_next_read);
453 /* Check, if `rf_next_read' is in the past. */
454 if ((rf->rf_next_read.tv_sec < now.tv_sec)
455 || ((rf->rf_next_read.tv_sec == now.tv_sec)
456 && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
457 {
458 /* `rf_next_read' is in the past. Insert `now'
459 * so this value doesn't trail off into the
460 * past too much. */
461 rf->rf_next_read.tv_sec = now.tv_sec;
462 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
463 }
465 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
466 rf->rf_name,
467 (int) rf->rf_next_read.tv_sec,
468 (int) rf->rf_next_read.tv_nsec);
470 /* Re-insert this read function into the heap again. */
471 c_heap_insert (read_heap, rf);
472 } /* while (read_loop) */
474 pthread_exit (NULL);
475 return ((void *) 0);
476 } /* void *plugin_read_thread */
478 static void start_read_threads (int num)
479 {
480 int i;
482 if (read_threads != NULL)
483 return;
485 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
486 if (read_threads == NULL)
487 {
488 ERROR ("plugin: start_read_threads: calloc failed.");
489 return;
490 }
492 read_threads_num = 0;
493 for (i = 0; i < num; i++)
494 {
495 if (pthread_create (read_threads + read_threads_num, NULL,
496 plugin_read_thread, NULL) == 0)
497 {
498 read_threads_num++;
499 }
500 else
501 {
502 ERROR ("plugin: start_read_threads: pthread_create failed.");
503 return;
504 }
505 } /* for (i) */
506 } /* void start_read_threads */
508 static void stop_read_threads (void)
509 {
510 int i;
512 if (read_threads == NULL)
513 return;
515 INFO ("collectd: Stopping %i read threads.", read_threads_num);
517 pthread_mutex_lock (&read_lock);
518 read_loop = 0;
519 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
520 pthread_cond_broadcast (&read_cond);
521 pthread_mutex_unlock (&read_lock);
523 for (i = 0; i < read_threads_num; i++)
524 {
525 if (pthread_join (read_threads[i], NULL) != 0)
526 {
527 ERROR ("plugin: stop_read_threads: pthread_join failed.");
528 }
529 read_threads[i] = (pthread_t) 0;
530 }
531 sfree (read_threads);
532 read_threads_num = 0;
533 } /* void stop_read_threads */
535 /*
536 * Public functions
537 */
538 void plugin_set_dir (const char *dir)
539 {
540 if (plugindir != NULL)
541 free (plugindir);
543 if (dir == NULL)
544 plugindir = NULL;
545 else if ((plugindir = strdup (dir)) == NULL)
546 {
547 char errbuf[1024];
548 ERROR ("strdup failed: %s",
549 sstrerror (errno, errbuf, sizeof (errbuf)));
550 }
551 }
553 #define BUFSIZE 512
554 int plugin_load (const char *type)
555 {
556 DIR *dh;
557 const char *dir;
558 char filename[BUFSIZE] = "";
559 char typename[BUFSIZE];
560 int typename_len;
561 int ret;
562 struct stat statbuf;
563 struct dirent *de;
564 int status;
566 DEBUG ("type = %s", type);
568 dir = plugin_get_dir ();
569 ret = 1;
571 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
572 * type when matching the filename */
573 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
574 if ((status < 0) || ((size_t) status >= sizeof (typename)))
575 {
576 WARNING ("snprintf: truncated: `%s.so'", type);
577 return (-1);
578 }
579 typename_len = strlen (typename);
581 if ((dh = opendir (dir)) == NULL)
582 {
583 char errbuf[1024];
584 ERROR ("opendir (%s): %s", dir,
585 sstrerror (errno, errbuf, sizeof (errbuf)));
586 return (-1);
587 }
589 while ((de = readdir (dh)) != NULL)
590 {
591 if (strncasecmp (de->d_name, typename, typename_len))
592 continue;
594 status = ssnprintf (filename, sizeof (filename),
595 "%s/%s", dir, de->d_name);
596 if ((status < 0) || ((size_t) status >= sizeof (filename)))
597 {
598 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
599 continue;
600 }
602 if (lstat (filename, &statbuf) == -1)
603 {
604 char errbuf[1024];
605 WARNING ("stat %s: %s", filename,
606 sstrerror (errno, errbuf, sizeof (errbuf)));
607 continue;
608 }
609 else if (!S_ISREG (statbuf.st_mode))
610 {
611 /* don't follow symlinks */
612 WARNING ("stat %s: not a regular file", filename);
613 continue;
614 }
616 if (plugin_load_file (filename) == 0)
617 {
618 /* success */
619 ret = 0;
620 break;
621 }
622 else
623 {
624 fprintf (stderr, "Unable to load plugin %s.\n", type);
625 }
626 }
628 closedir (dh);
630 if (filename[0] == '\0')
631 fprintf (stderr, "Could not find plugin %s.\n", type);
633 return (ret);
634 }
636 /*
637 * The `register_*' functions follow
638 */
639 int plugin_register_config (const char *name,
640 int (*callback) (const char *key, const char *val),
641 const char **keys, int keys_num)
642 {
643 cf_register (name, callback, keys, keys_num);
644 return (0);
645 } /* int plugin_register_config */
647 int plugin_register_complex_config (const char *type,
648 int (*callback) (oconfig_item_t *))
649 {
650 return (cf_register_complex (type, callback));
651 } /* int plugin_register_complex_config */
653 int plugin_register_init (const char *name,
654 int (*callback) (void))
655 {
656 return (create_register_callback (&list_init, name, (void *) callback,
657 /* user_data = */ NULL));
658 } /* plugin_register_init */
660 static int plugin_compare_read_func (const void *arg0, const void *arg1)
661 {
662 const read_func_t *rf0;
663 const read_func_t *rf1;
665 rf0 = arg0;
666 rf1 = arg1;
668 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
669 return (-1);
670 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
671 return (1);
672 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
673 return (-1);
674 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
675 return (1);
676 else
677 return (0);
678 } /* int plugin_compare_read_func */
680 /* Add a read function to both, the heap and a linked list. The linked list if
681 * used to look-up read functions, especially for the remove function. The heap
682 * is used to determine which plugin to read next. */
683 static int plugin_insert_read (read_func_t *rf)
684 {
685 int status;
686 llentry_t *le;
688 pthread_mutex_lock (&read_lock);
690 if (read_list == NULL)
691 {
692 read_list = llist_create ();
693 if (read_list == NULL)
694 {
695 pthread_mutex_unlock (&read_lock);
696 ERROR ("plugin_insert_read: read_list failed.");
697 return (-1);
698 }
699 }
701 if (read_heap == NULL)
702 {
703 read_heap = c_heap_create (plugin_compare_read_func);
704 if (read_heap == NULL)
705 {
706 pthread_mutex_unlock (&read_lock);
707 ERROR ("plugin_insert_read: c_heap_create failed.");
708 return (-1);
709 }
710 }
712 le = llentry_create (rf->rf_name, rf);
713 if (le == NULL)
714 {
715 pthread_mutex_unlock (&read_lock);
716 ERROR ("plugin_insert_read: llentry_create failed.");
717 return (-1);
718 }
720 status = c_heap_insert (read_heap, rf);
721 if (status != 0)
722 {
723 pthread_mutex_unlock (&read_lock);
724 ERROR ("plugin_insert_read: c_heap_insert failed.");
725 llentry_destroy (le);
726 return (-1);
727 }
729 /* This does not fail. */
730 llist_append (read_list, le);
732 pthread_mutex_unlock (&read_lock);
733 return (0);
734 } /* int plugin_insert_read */
736 int plugin_register_read (const char *name,
737 int (*callback) (void))
738 {
739 read_func_t *rf;
741 rf = (read_func_t *) malloc (sizeof (read_func_t));
742 if (rf == NULL)
743 {
744 char errbuf[1024];
745 ERROR ("plugin_register_read: malloc failed: %s",
746 sstrerror (errno, errbuf, sizeof (errbuf)));
747 return (-1);
748 }
750 memset (rf, 0, sizeof (read_func_t));
751 rf->rf_callback = (void *) callback;
752 rf->rf_udata.data = NULL;
753 rf->rf_udata.free_func = NULL;
754 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
755 rf->rf_type = RF_SIMPLE;
756 rf->rf_interval.tv_sec = 0;
757 rf->rf_interval.tv_nsec = 0;
758 rf->rf_effective_interval = rf->rf_interval;
760 return (plugin_insert_read (rf));
761 } /* int plugin_register_read */
763 int plugin_register_complex_read (const char *name,
764 plugin_read_cb callback,
765 const struct timespec *interval,
766 user_data_t *user_data)
767 {
768 read_func_t *rf;
770 rf = (read_func_t *) malloc (sizeof (read_func_t));
771 if (rf == NULL)
772 {
773 ERROR ("plugin_register_complex_read: malloc failed.");
774 return (-1);
775 }
777 memset (rf, 0, sizeof (read_func_t));
778 rf->rf_callback = (void *) callback;
779 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
780 rf->rf_type = RF_COMPLEX;
781 if (interval != NULL)
782 {
783 rf->rf_interval = *interval;
784 }
785 rf->rf_effective_interval = rf->rf_interval;
787 /* Set user data */
788 if (user_data == NULL)
789 {
790 rf->rf_udata.data = NULL;
791 rf->rf_udata.free_func = NULL;
792 }
793 else
794 {
795 rf->rf_udata = *user_data;
796 }
798 return (plugin_insert_read (rf));
799 } /* int plugin_register_complex_read */
801 int plugin_register_write (const char *name,
802 plugin_write_cb callback, user_data_t *ud)
803 {
804 return (create_register_callback (&list_write, name,
805 (void *) callback, ud));
806 } /* int plugin_register_write */
808 int plugin_register_flush (const char *name,
809 plugin_flush_cb callback, user_data_t *ud)
810 {
811 return (create_register_callback (&list_flush, name,
812 (void *) callback, ud));
813 } /* int plugin_register_flush */
815 int plugin_register_shutdown (char *name,
816 int (*callback) (void))
817 {
818 return (create_register_callback (&list_shutdown, name,
819 (void *) callback, /* user_data = */ NULL));
820 } /* int plugin_register_shutdown */
822 int plugin_register_data_set (const data_set_t *ds)
823 {
824 data_set_t *ds_copy;
825 int i;
827 if ((data_sets != NULL)
828 && (c_avl_get (data_sets, ds->type, NULL) == 0))
829 {
830 NOTICE ("Replacing DS `%s' with another version.", ds->type);
831 plugin_unregister_data_set (ds->type);
832 }
833 else if (data_sets == NULL)
834 {
835 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
836 if (data_sets == NULL)
837 return (-1);
838 }
840 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
841 if (ds_copy == NULL)
842 return (-1);
843 memcpy(ds_copy, ds, sizeof (data_set_t));
845 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
846 * ds->ds_num);
847 if (ds_copy->ds == NULL)
848 {
849 free (ds_copy);
850 return (-1);
851 }
853 for (i = 0; i < ds->ds_num; i++)
854 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
856 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
857 } /* int plugin_register_data_set */
859 int plugin_register_log (const char *name,
860 plugin_log_cb callback, user_data_t *ud)
861 {
862 return (create_register_callback (&list_log, name,
863 (void *) callback, ud));
864 } /* int plugin_register_log */
866 int plugin_register_notification (const char *name,
867 plugin_notification_cb callback, user_data_t *ud)
868 {
869 return (create_register_callback (&list_notification, name,
870 (void *) callback, ud));
871 } /* int plugin_register_log */
873 int plugin_unregister_config (const char *name)
874 {
875 cf_unregister (name);
876 return (0);
877 } /* int plugin_unregister_config */
879 int plugin_unregister_complex_config (const char *name)
880 {
881 cf_unregister_complex (name);
882 return (0);
883 } /* int plugin_unregister_complex_config */
885 int plugin_unregister_init (const char *name)
886 {
887 return (plugin_unregister (list_init, name));
888 }
890 int plugin_unregister_read (const char *name) /* {{{ */
891 {
892 llentry_t *le;
893 read_func_t *rf;
895 if (name == NULL)
896 return (-ENOENT);
898 pthread_mutex_lock (&read_lock);
900 if (read_list == NULL)
901 {
902 pthread_mutex_unlock (&read_lock);
903 return (-ENOENT);
904 }
906 le = llist_search (read_list, name);
907 if (le == NULL)
908 {
909 pthread_mutex_unlock (&read_lock);
910 WARNING ("plugin_unregister_read: No such read function: %s",
911 name);
912 return (-ENOENT);
913 }
915 llist_remove (read_list, le);
917 rf = le->value;
918 assert (rf != NULL);
919 rf->rf_type = RF_REMOVE;
921 pthread_mutex_unlock (&read_lock);
923 llentry_destroy (le);
925 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
927 return (0);
928 } /* }}} int plugin_unregister_read */
930 int plugin_unregister_write (const char *name)
931 {
932 return (plugin_unregister (list_write, name));
933 }
935 int plugin_unregister_flush (const char *name)
936 {
937 return (plugin_unregister (list_flush, name));
938 }
940 int plugin_unregister_shutdown (const char *name)
941 {
942 return (plugin_unregister (list_shutdown, name));
943 }
945 int plugin_unregister_data_set (const char *name)
946 {
947 data_set_t *ds;
949 if (data_sets == NULL)
950 return (-1);
952 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
953 return (-1);
955 sfree (ds->ds);
956 sfree (ds);
958 return (0);
959 } /* int plugin_unregister_data_set */
961 int plugin_unregister_log (const char *name)
962 {
963 return (plugin_unregister (list_log, name));
964 }
966 int plugin_unregister_notification (const char *name)
967 {
968 return (plugin_unregister (list_notification, name));
969 }
971 void plugin_init_all (void)
972 {
973 const char *chain_name;
974 llentry_t *le;
975 int status;
977 /* Init the value cache */
978 uc_init ();
980 chain_name = global_option_get ("PreCacheChain");
981 pre_cache_chain = fc_chain_get_by_name (chain_name);
983 chain_name = global_option_get ("PostCacheChain");
984 post_cache_chain = fc_chain_get_by_name (chain_name);
987 if ((list_init == NULL) && (read_heap == NULL))
988 return;
990 /* Calling all init callbacks before checking if read callbacks
991 * are available allows the init callbacks to register the read
992 * callback. */
993 le = llist_head (list_init);
994 while (le != NULL)
995 {
996 callback_func_t *cf;
997 plugin_init_cb callback;
999 cf = le->value;
1000 callback = cf->cf_callback;
1001 status = (*callback) ();
1003 if (status != 0)
1004 {
1005 ERROR ("Initialization of plugin `%s' "
1006 "failed with status %i. "
1007 "Plugin will be unloaded.",
1008 le->key, status);
1009 /* Plugins that register read callbacks from the init
1010 * callback should take care of appropriate error
1011 * handling themselves. */
1012 /* FIXME: Unload _all_ functions */
1013 plugin_unregister_read (le->key);
1014 }
1016 le = le->next;
1017 }
1019 /* Start read-threads */
1020 if (read_heap != NULL)
1021 {
1022 const char *rt;
1023 int num;
1024 rt = global_option_get ("ReadThreads");
1025 num = atoi (rt);
1026 if (num != -1)
1027 start_read_threads ((num > 0) ? num : 5);
1028 }
1029 } /* void plugin_init_all */
1031 /* TODO: Rename this function. */
1032 void plugin_read_all (void)
1033 {
1034 uc_check_timeout ();
1036 return;
1037 } /* void plugin_read_all */
1039 /* Read function called when the `-T' command line argument is given. */
1040 int plugin_read_all_once (void)
1041 {
1042 int status;
1043 int return_status = 0;
1045 if (read_heap == NULL)
1046 {
1047 NOTICE ("No read-functions are registered.");
1048 return (0);
1049 }
1051 while (42)
1052 {
1053 read_func_t *rf;
1055 rf = c_head_get_root (read_heap);
1056 if (rf == NULL)
1057 break;
1059 if (rf->rf_type == RF_SIMPLE)
1060 {
1061 int (*callback) (void);
1063 callback = rf->rf_callback;
1064 status = (*callback) ();
1065 }
1066 else
1067 {
1068 plugin_read_cb callback;
1070 callback = rf->rf_callback;
1071 status = (*callback) (&rf->rf_udata);
1072 }
1074 if (status != 0)
1075 {
1076 NOTICE ("read-function of plugin `%s' failed.",
1077 rf->rf_name);
1078 return_status = -1;
1079 }
1081 destroy_callback ((void *) rf);
1082 }
1084 return (return_status);
1085 } /* int plugin_read_all_once */
1087 int plugin_write (const char *plugin, /* {{{ */
1088 const data_set_t *ds, const value_list_t *vl)
1089 {
1090 llentry_t *le;
1091 int status;
1093 if (vl == NULL)
1094 return (EINVAL);
1096 if (list_write == NULL)
1097 return (ENOENT);
1099 if (ds == NULL)
1100 {
1101 ds = plugin_get_ds (vl->type);
1102 if (ds == NULL)
1103 {
1104 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1105 return (ENOENT);
1106 }
1107 }
1109 if (plugin == NULL)
1110 {
1111 int success = 0;
1112 int failure = 0;
1114 le = llist_head (list_write);
1115 while (le != NULL)
1116 {
1117 callback_func_t *cf = le->value;
1118 plugin_write_cb callback;
1120 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1121 callback = cf->cf_callback;
1122 status = (*callback) (ds, vl, &cf->cf_udata);
1123 if (status != 0)
1124 failure++;
1125 else
1126 success++;
1128 le = le->next;
1129 }
1131 if ((success == 0) && (failure != 0))
1132 status = -1;
1133 else
1134 status = 0;
1135 }
1136 else /* plugin != NULL */
1137 {
1138 callback_func_t *cf;
1139 plugin_write_cb callback;
1141 le = llist_head (list_write);
1142 while (le != NULL)
1143 {
1144 if (strcasecmp (plugin, le->key) == 0)
1145 break;
1147 le = le->next;
1148 }
1150 if (le == NULL)
1151 return (ENOENT);
1153 cf = le->value;
1155 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1156 callback = cf->cf_callback;
1157 status = (*callback) (ds, vl, &cf->cf_udata);
1158 }
1160 return (status);
1161 } /* }}} int plugin_write */
1163 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1164 {
1165 llentry_t *le;
1167 if (list_flush == NULL)
1168 return (0);
1170 le = llist_head (list_flush);
1171 while (le != NULL)
1172 {
1173 callback_func_t *cf;
1174 plugin_flush_cb callback;
1176 if ((plugin != NULL)
1177 && (strcmp (plugin, le->key) != 0))
1178 {
1179 le = le->next;
1180 continue;
1181 }
1183 cf = le->value;
1184 callback = cf->cf_callback;
1186 (*callback) (timeout, identifier, &cf->cf_udata);
1188 le = le->next;
1189 }
1190 return (0);
1191 } /* int plugin_flush */
1193 void plugin_shutdown_all (void)
1194 {
1195 llentry_t *le;
1197 stop_read_threads ();
1199 destroy_all_callbacks (&list_init);
1201 pthread_mutex_lock (&read_lock);
1202 llist_destroy (read_list);
1203 read_list = NULL;
1204 pthread_mutex_unlock (&read_lock);
1206 destroy_read_heap ();
1208 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1209 /* identifier = */ NULL);
1211 le = NULL;
1212 if (list_shutdown != NULL)
1213 le = llist_head (list_shutdown);
1215 while (le != NULL)
1216 {
1217 callback_func_t *cf;
1218 plugin_shutdown_cb callback;
1220 cf = le->value;
1221 callback = cf->cf_callback;
1223 /* Advance the pointer before calling the callback allows
1224 * shutdown functions to unregister themselves. If done the
1225 * other way around the memory `le' points to will be freed
1226 * after callback returns. */
1227 le = le->next;
1229 (*callback) ();
1230 }
1232 /* Write plugins which use the `user_data' pointer usually need the
1233 * same data available to the flush callback. If this is the case, set
1234 * the free_function to NULL when registering the flush callback and to
1235 * the real free function when registering the write callback. This way
1236 * the data isn't freed twice. */
1237 destroy_all_callbacks (&list_flush);
1238 destroy_all_callbacks (&list_write);
1240 destroy_all_callbacks (&list_notification);
1241 destroy_all_callbacks (&list_shutdown);
1242 destroy_all_callbacks (&list_log);
1243 } /* void plugin_shutdown_all */
1245 int plugin_dispatch_values (value_list_t *vl)
1246 {
1247 int status;
1248 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1250 value_t *saved_values;
1251 int saved_values_len;
1253 data_set_t *ds;
1255 int free_meta_data = 0;
1257 if ((vl == NULL) || (vl->type[0] == 0)
1258 || (vl->values == NULL) || (vl->values_len < 1))
1259 {
1260 ERROR ("plugin_dispatch_values: Invalid value list.");
1261 return (-1);
1262 }
1264 /* Free meta data only if the calling function didn't specify any. In
1265 * this case matches and targets may add some and the calling function
1266 * may not expect (and therefore free) that data. */
1267 if (vl->meta == NULL)
1268 free_meta_data = 1;
1270 if (list_write == NULL)
1271 c_complain_once (LOG_WARNING, &no_write_complaint,
1272 "plugin_dispatch_values: No write callback has been "
1273 "registered. Please load at least one output plugin, "
1274 "if you want the collected data to be stored.");
1276 if (data_sets == NULL)
1277 {
1278 ERROR ("plugin_dispatch_values: No data sets registered. "
1279 "Could the types database be read? Check "
1280 "your `TypesDB' setting!");
1281 return (-1);
1282 }
1284 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1285 {
1286 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1287 return (-1);
1288 }
1290 if (vl->time == 0)
1291 vl->time = time (NULL);
1293 if (vl->interval <= 0)
1294 vl->interval = interval_g;
1296 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1297 "host = %s; "
1298 "plugin = %s; plugin_instance = %s; "
1299 "type = %s; type_instance = %s;",
1300 (unsigned int) vl->time, vl->interval,
1301 vl->host,
1302 vl->plugin, vl->plugin_instance,
1303 vl->type, vl->type_instance);
1305 #if COLLECT_DEBUG
1306 assert (0 == strcmp (ds->type, vl->type));
1307 #else
1308 if (0 != strcmp (ds->type, vl->type))
1309 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1310 ds->type, vl->type);
1311 #endif
1313 #if COLLECT_DEBUG
1314 assert (ds->ds_num == vl->values_len);
1315 #else
1316 if (ds->ds_num != vl->values_len)
1317 {
1318 ERROR ("plugin_dispatch_values: ds->type = %s: "
1319 "(ds->ds_num = %i) != "
1320 "(vl->values_len = %i)",
1321 ds->type, ds->ds_num, vl->values_len);
1322 return (-1);
1323 }
1324 #endif
1326 escape_slashes (vl->host, sizeof (vl->host));
1327 escape_slashes (vl->plugin, sizeof (vl->plugin));
1328 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1329 escape_slashes (vl->type, sizeof (vl->type));
1330 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1332 /* Copy the values. This way, we can assure `targets' that they get
1333 * dynamically allocated values, which they can free and replace if
1334 * they like. */
1335 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1336 {
1337 saved_values = vl->values;
1338 saved_values_len = vl->values_len;
1340 vl->values = (value_t *) calloc (vl->values_len,
1341 sizeof (*vl->values));
1342 if (vl->values == NULL)
1343 {
1344 ERROR ("plugin_dispatch_values: calloc failed.");
1345 vl->values = saved_values;
1346 return (-1);
1347 }
1348 memcpy (vl->values, saved_values,
1349 vl->values_len * sizeof (*vl->values));
1350 }
1351 else /* if ((pre == NULL) && (post == NULL)) */
1352 {
1353 saved_values = NULL;
1354 saved_values_len = 0;
1355 }
1357 if (pre_cache_chain != NULL)
1358 {
1359 status = fc_process_chain (ds, vl, pre_cache_chain);
1360 if (status < 0)
1361 {
1362 WARNING ("plugin_dispatch_values: Running the "
1363 "pre-cache chain failed with "
1364 "status %i (%#x).",
1365 status, status);
1366 }
1367 else if (status == FC_TARGET_STOP)
1368 {
1369 /* Restore the state of the value_list so that plugins
1370 * don't get confused.. */
1371 if (saved_values != NULL)
1372 {
1373 free (vl->values);
1374 vl->values = saved_values;
1375 vl->values_len = saved_values_len;
1376 }
1377 return (0);
1378 }
1379 }
1381 /* Update the value cache */
1382 uc_update (ds, vl);
1384 /* Initiate threshold checking */
1385 ut_check_threshold (ds, vl);
1387 if (post_cache_chain != NULL)
1388 {
1389 status = fc_process_chain (ds, vl, post_cache_chain);
1390 if (status < 0)
1391 {
1392 WARNING ("plugin_dispatch_values: Running the "
1393 "post-cache chain failed with "
1394 "status %i (%#x).",
1395 status, status);
1396 }
1397 }
1398 else
1399 fc_default_action (ds, vl);
1401 /* Restore the state of the value_list so that plugins don't get
1402 * confused.. */
1403 if (saved_values != NULL)
1404 {
1405 free (vl->values);
1406 vl->values = saved_values;
1407 vl->values_len = saved_values_len;
1408 }
1410 if ((free_meta_data != 0) && (vl->meta != NULL))
1411 {
1412 meta_data_destroy (vl->meta);
1413 vl->meta = NULL;
1414 }
1416 return (0);
1417 } /* int plugin_dispatch_values */
1419 int plugin_dispatch_notification (const notification_t *notif)
1420 {
1421 llentry_t *le;
1422 /* Possible TODO: Add flap detection here */
1424 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1425 "time = %u; host = %s;",
1426 notif->severity, notif->message,
1427 (unsigned int) notif->time, notif->host);
1429 /* Nobody cares for notifications */
1430 if (list_notification == NULL)
1431 return (-1);
1433 le = llist_head (list_notification);
1434 while (le != NULL)
1435 {
1436 callback_func_t *cf;
1437 plugin_notification_cb callback;
1438 int status;
1440 cf = le->value;
1441 callback = cf->cf_callback;
1442 status = (*callback) (notif, &cf->cf_udata);
1443 if (status != 0)
1444 {
1445 WARNING ("plugin_dispatch_notification: Notification "
1446 "callback %s returned %i.",
1447 le->key, status);
1448 }
1450 le = le->next;
1451 }
1453 return (0);
1454 } /* int plugin_dispatch_notification */
1456 void plugin_log (int level, const char *format, ...)
1457 {
1458 char msg[1024];
1459 va_list ap;
1460 llentry_t *le;
1462 if (list_log == NULL)
1463 {
1464 va_start (ap, format);
1465 vfprintf (stderr, format, ap);
1466 va_end (ap);
1467 return;
1468 }
1470 #if !COLLECT_DEBUG
1471 if (level >= LOG_DEBUG)
1472 return;
1473 #endif
1475 va_start (ap, format);
1476 vsnprintf (msg, sizeof (msg), format, ap);
1477 msg[sizeof (msg) - 1] = '\0';
1478 va_end (ap);
1480 le = llist_head (list_log);
1481 while (le != NULL)
1482 {
1483 callback_func_t *cf;
1484 plugin_log_cb callback;
1486 cf = le->value;
1487 callback = cf->cf_callback;
1489 (*callback) (level, msg, &cf->cf_udata);
1491 le = le->next;
1492 }
1493 } /* void plugin_log */
1495 const data_set_t *plugin_get_ds (const char *name)
1496 {
1497 data_set_t *ds;
1499 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1500 {
1501 DEBUG ("No such dataset registered: %s", name);
1502 return (NULL);
1503 }
1505 return (ds);
1506 } /* data_set_t *plugin_get_ds */
1508 static int plugin_notification_meta_add (notification_t *n,
1509 const char *name,
1510 enum notification_meta_type_e type,
1511 const void *value)
1512 {
1513 notification_meta_t *meta;
1514 notification_meta_t *tail;
1516 if ((n == NULL) || (name == NULL) || (value == NULL))
1517 {
1518 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1519 return (-1);
1520 }
1522 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1523 if (meta == NULL)
1524 {
1525 ERROR ("plugin_notification_meta_add: malloc failed.");
1526 return (-1);
1527 }
1528 memset (meta, 0, sizeof (notification_meta_t));
1530 sstrncpy (meta->name, name, sizeof (meta->name));
1531 meta->type = type;
1533 switch (type)
1534 {
1535 case NM_TYPE_STRING:
1536 {
1537 meta->nm_value.nm_string = strdup ((const char *) value);
1538 if (meta->nm_value.nm_string == NULL)
1539 {
1540 ERROR ("plugin_notification_meta_add: strdup failed.");
1541 sfree (meta);
1542 return (-1);
1543 }
1544 break;
1545 }
1546 case NM_TYPE_SIGNED_INT:
1547 {
1548 meta->nm_value.nm_signed_int = *((int64_t *) value);
1549 break;
1550 }
1551 case NM_TYPE_UNSIGNED_INT:
1552 {
1553 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1554 break;
1555 }
1556 case NM_TYPE_DOUBLE:
1557 {
1558 meta->nm_value.nm_double = *((double *) value);
1559 break;
1560 }
1561 case NM_TYPE_BOOLEAN:
1562 {
1563 meta->nm_value.nm_boolean = *((bool *) value);
1564 break;
1565 }
1566 default:
1567 {
1568 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1569 sfree (meta);
1570 return (-1);
1571 }
1572 } /* switch (type) */
1574 meta->next = NULL;
1575 tail = n->meta;
1576 while ((tail != NULL) && (tail->next != NULL))
1577 tail = tail->next;
1579 if (tail == NULL)
1580 n->meta = meta;
1581 else
1582 tail->next = meta;
1584 return (0);
1585 } /* int plugin_notification_meta_add */
1587 int plugin_notification_meta_add_string (notification_t *n,
1588 const char *name,
1589 const char *value)
1590 {
1591 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1592 }
1594 int plugin_notification_meta_add_signed_int (notification_t *n,
1595 const char *name,
1596 int64_t value)
1597 {
1598 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1599 }
1601 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1602 const char *name,
1603 uint64_t value)
1604 {
1605 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1606 }
1608 int plugin_notification_meta_add_double (notification_t *n,
1609 const char *name,
1610 double value)
1611 {
1612 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1613 }
1615 int plugin_notification_meta_add_boolean (notification_t *n,
1616 const char *name,
1617 bool value)
1618 {
1619 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1620 }
1622 int plugin_notification_meta_copy (notification_t *dst,
1623 const notification_t *src)
1624 {
1625 notification_meta_t *meta;
1627 assert (dst != NULL);
1628 assert (src != NULL);
1629 assert (dst != src);
1630 assert ((src->meta == NULL) || (src->meta != dst->meta));
1632 for (meta = src->meta; meta != NULL; meta = meta->next)
1633 {
1634 if (meta->type == NM_TYPE_STRING)
1635 plugin_notification_meta_add_string (dst, meta->name,
1636 meta->nm_value.nm_string);
1637 else if (meta->type == NM_TYPE_SIGNED_INT)
1638 plugin_notification_meta_add_signed_int (dst, meta->name,
1639 meta->nm_value.nm_signed_int);
1640 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1641 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1642 meta->nm_value.nm_unsigned_int);
1643 else if (meta->type == NM_TYPE_DOUBLE)
1644 plugin_notification_meta_add_double (dst, meta->name,
1645 meta->nm_value.nm_double);
1646 else if (meta->type == NM_TYPE_BOOLEAN)
1647 plugin_notification_meta_add_boolean (dst, meta->name,
1648 meta->nm_value.nm_boolean);
1649 }
1651 return (0);
1652 } /* int plugin_notification_meta_copy */
1654 int plugin_notification_meta_free (notification_meta_t *n)
1655 {
1656 notification_meta_t *this;
1657 notification_meta_t *next;
1659 if (n == NULL)
1660 {
1661 ERROR ("plugin_notification_meta_free: n == NULL!");
1662 return (-1);
1663 }
1665 this = n;
1666 while (this != NULL)
1667 {
1668 next = this->next;
1670 if (this->type == NM_TYPE_STRING)
1671 {
1672 free ((char *)this->nm_value.nm_string);
1673 this->nm_value.nm_string = NULL;
1674 }
1675 sfree (this);
1677 this = next;
1678 }
1680 return (0);
1681 } /* int plugin_notification_meta_free */
1683 /* vim: set sw=8 ts=8 noet fdm=marker : */