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 /* XXX BUG FIXME */
284 if (strstr(file, "python") != NULL) {
285 lt_dladvise advise;
286 lt_dladvise_init(&advise);
287 lt_dladvise_global(&advise);
288 dlh = lt_dlopenadvise(file, advise);
289 lt_dladvise_destroy(&advise);
290 } else {
291 dlh = lt_dlopen (file);
292 }
293 if (dlh == NULL)
294 {
295 const char *error = lt_dlerror ();
297 ERROR ("lt_dlopen (%s) failed: %s", file, error);
298 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
299 return (1);
300 }
302 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
303 {
304 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
305 file, lt_dlerror ());
306 lt_dlclose (dlh);
307 return (-1);
308 }
310 (*reg_handle) ();
312 return (0);
313 }
315 static void *plugin_read_thread (void __attribute__((unused)) *args)
316 {
317 while (read_loop != 0)
318 {
319 read_func_t *rf;
320 struct timeval now;
321 int status;
322 int rf_type;
324 /* Get the read function that needs to be read next. */
325 rf = c_head_get_root (read_heap);
326 if (rf == NULL)
327 {
328 struct timespec abstime;
330 gettimeofday (&now, /* timezone = */ NULL);
332 abstime.tv_sec = now.tv_sec + interval_g;
333 abstime.tv_nsec = 1000 * now.tv_usec;
335 pthread_mutex_lock (&read_lock);
336 pthread_cond_timedwait (&read_cond, &read_lock,
337 &abstime);
338 pthread_mutex_unlock (&read_lock);
339 continue;
340 }
342 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
343 {
344 gettimeofday (&now, /* timezone = */ NULL);
346 rf->rf_interval.tv_sec = interval_g;
347 rf->rf_interval.tv_nsec = 0;
349 rf->rf_effective_interval = rf->rf_interval;
351 rf->rf_next_read.tv_sec = now.tv_sec;
352 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
353 }
355 /* sleep until this entry is due,
356 * using pthread_cond_timedwait */
357 pthread_mutex_lock (&read_lock);
358 pthread_cond_timedwait (&read_cond, &read_lock,
359 &rf->rf_next_read);
360 /* Must hold `real_lock' when accessing `rf->rf_type'. */
361 rf_type = rf->rf_type;
362 pthread_mutex_unlock (&read_lock);
364 /* Check if we're supposed to stop.. This may have interrupted
365 * the sleep, too. */
366 if (read_loop == 0)
367 {
368 /* Insert `rf' again, so it can be free'd correctly */
369 c_heap_insert (read_heap, rf);
370 break;
371 }
373 /* The entry has been marked for deletion. The linked list
374 * entry has already been removed by `plugin_unregister_read'.
375 * All we have to do here is free the `read_func_t' and
376 * continue. */
377 if (rf_type == RF_REMOVE)
378 {
379 DEBUG ("plugin_read_thread: Destroying the `%s' "
380 "callback.", rf->rf_name);
381 destroy_callback ((callback_func_t *) rf);
382 rf = NULL;
383 continue;
384 }
386 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
388 if (rf_type == RF_SIMPLE)
389 {
390 int (*callback) (void);
392 callback = rf->rf_callback;
393 status = (*callback) ();
394 }
395 else
396 {
397 plugin_read_cb callback;
399 assert (rf_type == RF_COMPLEX);
401 callback = rf->rf_callback;
402 status = (*callback) (&rf->rf_udata);
403 }
405 /* If the function signals failure, we will increase the
406 * intervals in which it will be called. */
407 if (status != 0)
408 {
409 rf->rf_effective_interval.tv_sec *= 2;
410 rf->rf_effective_interval.tv_nsec *= 2;
411 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
413 if (rf->rf_effective_interval.tv_sec >= 86400)
414 {
415 rf->rf_effective_interval.tv_sec = 86400;
416 rf->rf_effective_interval.tv_nsec = 0;
417 }
419 NOTICE ("read-function of plugin `%s' failed. "
420 "Will suspend it for %i seconds.",
421 rf->rf_name,
422 (int) rf->rf_effective_interval.tv_sec);
423 }
424 else
425 {
426 /* Success: Restore the interval, if it was changed. */
427 rf->rf_effective_interval = rf->rf_interval;
428 }
430 /* update the ``next read due'' field */
431 gettimeofday (&now, /* timezone = */ NULL);
433 DEBUG ("plugin_read_thread: Effective interval of the "
434 "%s plugin is %i.%09i.",
435 rf->rf_name,
436 (int) rf->rf_effective_interval.tv_sec,
437 (int) rf->rf_effective_interval.tv_nsec);
439 /* Calculate the next (absolute) time at which this function
440 * should be called. */
441 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
442 + rf->rf_effective_interval.tv_sec;
443 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
444 + rf->rf_effective_interval.tv_nsec;
445 NORMALIZE_TIMESPEC (rf->rf_next_read);
447 /* Check, if `rf_next_read' is in the past. */
448 if ((rf->rf_next_read.tv_sec < now.tv_sec)
449 || ((rf->rf_next_read.tv_sec == now.tv_sec)
450 && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
451 {
452 /* `rf_next_read' is in the past. Insert `now'
453 * so this value doesn't trail off into the
454 * past too much. */
455 rf->rf_next_read.tv_sec = now.tv_sec;
456 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
457 }
459 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
460 rf->rf_name,
461 (int) rf->rf_next_read.tv_sec,
462 (int) rf->rf_next_read.tv_nsec);
464 /* Re-insert this read function into the heap again. */
465 c_heap_insert (read_heap, rf);
466 } /* while (read_loop) */
468 pthread_exit (NULL);
469 return ((void *) 0);
470 } /* void *plugin_read_thread */
472 static void start_read_threads (int num)
473 {
474 int i;
476 if (read_threads != NULL)
477 return;
479 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
480 if (read_threads == NULL)
481 {
482 ERROR ("plugin: start_read_threads: calloc failed.");
483 return;
484 }
486 read_threads_num = 0;
487 for (i = 0; i < num; i++)
488 {
489 if (pthread_create (read_threads + read_threads_num, NULL,
490 plugin_read_thread, NULL) == 0)
491 {
492 read_threads_num++;
493 }
494 else
495 {
496 ERROR ("plugin: start_read_threads: pthread_create failed.");
497 return;
498 }
499 } /* for (i) */
500 } /* void start_read_threads */
502 static void stop_read_threads (void)
503 {
504 int i;
506 if (read_threads == NULL)
507 return;
509 INFO ("collectd: Stopping %i read threads.", read_threads_num);
511 pthread_mutex_lock (&read_lock);
512 read_loop = 0;
513 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
514 pthread_cond_broadcast (&read_cond);
515 pthread_mutex_unlock (&read_lock);
517 for (i = 0; i < read_threads_num; i++)
518 {
519 if (pthread_join (read_threads[i], NULL) != 0)
520 {
521 ERROR ("plugin: stop_read_threads: pthread_join failed.");
522 }
523 read_threads[i] = (pthread_t) 0;
524 }
525 sfree (read_threads);
526 read_threads_num = 0;
527 } /* void stop_read_threads */
529 /*
530 * Public functions
531 */
532 void plugin_set_dir (const char *dir)
533 {
534 if (plugindir != NULL)
535 free (plugindir);
537 if (dir == NULL)
538 plugindir = NULL;
539 else if ((plugindir = strdup (dir)) == NULL)
540 {
541 char errbuf[1024];
542 ERROR ("strdup failed: %s",
543 sstrerror (errno, errbuf, sizeof (errbuf)));
544 }
545 }
547 #define BUFSIZE 512
548 int plugin_load (const char *type)
549 {
550 DIR *dh;
551 const char *dir;
552 char filename[BUFSIZE] = "";
553 char typename[BUFSIZE];
554 int typename_len;
555 int ret;
556 struct stat statbuf;
557 struct dirent *de;
558 int status;
560 DEBUG ("type = %s", type);
562 dir = plugin_get_dir ();
563 ret = 1;
565 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
566 * type when matching the filename */
567 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
568 if ((status < 0) || ((size_t) status >= sizeof (typename)))
569 {
570 WARNING ("snprintf: truncated: `%s.so'", type);
571 return (-1);
572 }
573 typename_len = strlen (typename);
575 if ((dh = opendir (dir)) == NULL)
576 {
577 char errbuf[1024];
578 ERROR ("opendir (%s): %s", dir,
579 sstrerror (errno, errbuf, sizeof (errbuf)));
580 return (-1);
581 }
583 while ((de = readdir (dh)) != NULL)
584 {
585 if (strncasecmp (de->d_name, typename, typename_len))
586 continue;
588 status = ssnprintf (filename, sizeof (filename),
589 "%s/%s", dir, de->d_name);
590 if ((status < 0) || ((size_t) status >= sizeof (filename)))
591 {
592 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
593 continue;
594 }
596 if (lstat (filename, &statbuf) == -1)
597 {
598 char errbuf[1024];
599 WARNING ("stat %s: %s", filename,
600 sstrerror (errno, errbuf, sizeof (errbuf)));
601 continue;
602 }
603 else if (!S_ISREG (statbuf.st_mode))
604 {
605 /* don't follow symlinks */
606 WARNING ("stat %s: not a regular file", filename);
607 continue;
608 }
610 if (plugin_load_file (filename) == 0)
611 {
612 /* success */
613 ret = 0;
614 break;
615 }
616 else
617 {
618 fprintf (stderr, "Unable to load plugin %s.\n", type);
619 }
620 }
622 closedir (dh);
624 if (filename[0] == '\0')
625 fprintf (stderr, "Could not find plugin %s.\n", type);
627 return (ret);
628 }
630 /*
631 * The `register_*' functions follow
632 */
633 int plugin_register_config (const char *name,
634 int (*callback) (const char *key, const char *val),
635 const char **keys, int keys_num)
636 {
637 cf_register (name, callback, keys, keys_num);
638 return (0);
639 } /* int plugin_register_config */
641 int plugin_register_complex_config (const char *type,
642 int (*callback) (oconfig_item_t *))
643 {
644 return (cf_register_complex (type, callback));
645 } /* int plugin_register_complex_config */
647 int plugin_register_init (const char *name,
648 int (*callback) (void))
649 {
650 return (create_register_callback (&list_init, name, (void *) callback,
651 /* user_data = */ NULL));
652 } /* plugin_register_init */
654 static int plugin_compare_read_func (const void *arg0, const void *arg1)
655 {
656 const read_func_t *rf0;
657 const read_func_t *rf1;
659 rf0 = arg0;
660 rf1 = arg1;
662 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
663 return (-1);
664 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
665 return (1);
666 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
667 return (-1);
668 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
669 return (1);
670 else
671 return (0);
672 } /* int plugin_compare_read_func */
674 /* Add a read function to both, the heap and a linked list. The linked list if
675 * used to look-up read functions, especially for the remove function. The heap
676 * is used to determine which plugin to read next. */
677 static int plugin_insert_read (read_func_t *rf)
678 {
679 int status;
680 llentry_t *le;
682 pthread_mutex_lock (&read_lock);
684 if (read_list == NULL)
685 {
686 read_list = llist_create ();
687 if (read_list == NULL)
688 {
689 pthread_mutex_unlock (&read_lock);
690 ERROR ("plugin_insert_read: read_list failed.");
691 return (-1);
692 }
693 }
695 if (read_heap == NULL)
696 {
697 read_heap = c_heap_create (plugin_compare_read_func);
698 if (read_heap == NULL)
699 {
700 pthread_mutex_unlock (&read_lock);
701 ERROR ("plugin_insert_read: c_heap_create failed.");
702 return (-1);
703 }
704 }
706 le = llentry_create (rf->rf_name, rf);
707 if (le == NULL)
708 {
709 pthread_mutex_unlock (&read_lock);
710 ERROR ("plugin_insert_read: llentry_create failed.");
711 return (-1);
712 }
714 status = c_heap_insert (read_heap, rf);
715 if (status != 0)
716 {
717 pthread_mutex_unlock (&read_lock);
718 ERROR ("plugin_insert_read: c_heap_insert failed.");
719 llentry_destroy (le);
720 return (-1);
721 }
723 /* This does not fail. */
724 llist_append (read_list, le);
726 pthread_mutex_unlock (&read_lock);
727 return (0);
728 } /* int plugin_insert_read */
730 int plugin_register_read (const char *name,
731 int (*callback) (void))
732 {
733 read_func_t *rf;
735 rf = (read_func_t *) malloc (sizeof (read_func_t));
736 if (rf == NULL)
737 {
738 char errbuf[1024];
739 ERROR ("plugin_register_read: malloc failed: %s",
740 sstrerror (errno, errbuf, sizeof (errbuf)));
741 return (-1);
742 }
744 memset (rf, 0, sizeof (read_func_t));
745 rf->rf_callback = (void *) callback;
746 rf->rf_udata.data = NULL;
747 rf->rf_udata.free_func = NULL;
748 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
749 rf->rf_type = RF_SIMPLE;
750 rf->rf_interval.tv_sec = 0;
751 rf->rf_interval.tv_nsec = 0;
752 rf->rf_effective_interval = rf->rf_interval;
754 return (plugin_insert_read (rf));
755 } /* int plugin_register_read */
757 int plugin_register_complex_read (const char *name,
758 plugin_read_cb callback,
759 const struct timespec *interval,
760 user_data_t *user_data)
761 {
762 read_func_t *rf;
764 rf = (read_func_t *) malloc (sizeof (read_func_t));
765 if (rf == NULL)
766 {
767 ERROR ("plugin_register_complex_read: malloc failed.");
768 return (-1);
769 }
771 memset (rf, 0, sizeof (read_func_t));
772 rf->rf_callback = (void *) callback;
773 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
774 rf->rf_type = RF_COMPLEX;
775 if (interval != NULL)
776 {
777 rf->rf_interval = *interval;
778 }
779 rf->rf_effective_interval = rf->rf_interval;
781 /* Set user data */
782 if (user_data == NULL)
783 {
784 rf->rf_udata.data = NULL;
785 rf->rf_udata.free_func = NULL;
786 }
787 else
788 {
789 rf->rf_udata = *user_data;
790 }
792 return (plugin_insert_read (rf));
793 } /* int plugin_register_complex_read */
795 int plugin_register_write (const char *name,
796 plugin_write_cb callback, user_data_t *ud)
797 {
798 return (create_register_callback (&list_write, name,
799 (void *) callback, ud));
800 } /* int plugin_register_write */
802 int plugin_register_flush (const char *name,
803 plugin_flush_cb callback, user_data_t *ud)
804 {
805 return (create_register_callback (&list_flush, name,
806 (void *) callback, ud));
807 } /* int plugin_register_flush */
809 int plugin_register_shutdown (char *name,
810 int (*callback) (void))
811 {
812 return (create_register_callback (&list_shutdown, name,
813 (void *) callback, /* user_data = */ NULL));
814 } /* int plugin_register_shutdown */
816 int plugin_register_data_set (const data_set_t *ds)
817 {
818 data_set_t *ds_copy;
819 int i;
821 if ((data_sets != NULL)
822 && (c_avl_get (data_sets, ds->type, NULL) == 0))
823 {
824 NOTICE ("Replacing DS `%s' with another version.", ds->type);
825 plugin_unregister_data_set (ds->type);
826 }
827 else if (data_sets == NULL)
828 {
829 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
830 if (data_sets == NULL)
831 return (-1);
832 }
834 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
835 if (ds_copy == NULL)
836 return (-1);
837 memcpy(ds_copy, ds, sizeof (data_set_t));
839 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
840 * ds->ds_num);
841 if (ds_copy->ds == NULL)
842 {
843 free (ds_copy);
844 return (-1);
845 }
847 for (i = 0; i < ds->ds_num; i++)
848 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
850 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
851 } /* int plugin_register_data_set */
853 int plugin_register_log (const char *name,
854 plugin_log_cb callback, user_data_t *ud)
855 {
856 return (create_register_callback (&list_log, name,
857 (void *) callback, ud));
858 } /* int plugin_register_log */
860 int plugin_register_notification (const char *name,
861 plugin_notification_cb callback, user_data_t *ud)
862 {
863 return (create_register_callback (&list_notification, name,
864 (void *) callback, ud));
865 } /* int plugin_register_log */
867 int plugin_unregister_config (const char *name)
868 {
869 cf_unregister (name);
870 return (0);
871 } /* int plugin_unregister_config */
873 int plugin_unregister_complex_config (const char *name)
874 {
875 cf_unregister_complex (name);
876 return (0);
877 } /* int plugin_unregister_complex_config */
879 int plugin_unregister_init (const char *name)
880 {
881 return (plugin_unregister (list_init, name));
882 }
884 int plugin_unregister_read (const char *name) /* {{{ */
885 {
886 llentry_t *le;
887 read_func_t *rf;
889 if (name == NULL)
890 return (-ENOENT);
892 pthread_mutex_lock (&read_lock);
894 if (read_list == NULL)
895 {
896 pthread_mutex_unlock (&read_lock);
897 return (-ENOENT);
898 }
900 le = llist_search (read_list, name);
901 if (le == NULL)
902 {
903 pthread_mutex_unlock (&read_lock);
904 WARNING ("plugin_unregister_read: No such read function: %s",
905 name);
906 return (-ENOENT);
907 }
909 llist_remove (read_list, le);
911 rf = le->value;
912 assert (rf != NULL);
913 rf->rf_type = RF_REMOVE;
915 pthread_mutex_unlock (&read_lock);
917 llentry_destroy (le);
919 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
921 return (0);
922 } /* }}} int plugin_unregister_read */
924 int plugin_unregister_write (const char *name)
925 {
926 return (plugin_unregister (list_write, name));
927 }
929 int plugin_unregister_flush (const char *name)
930 {
931 return (plugin_unregister (list_flush, name));
932 }
934 int plugin_unregister_shutdown (const char *name)
935 {
936 return (plugin_unregister (list_shutdown, name));
937 }
939 int plugin_unregister_data_set (const char *name)
940 {
941 data_set_t *ds;
943 if (data_sets == NULL)
944 return (-1);
946 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
947 return (-1);
949 sfree (ds->ds);
950 sfree (ds);
952 return (0);
953 } /* int plugin_unregister_data_set */
955 int plugin_unregister_log (const char *name)
956 {
957 return (plugin_unregister (list_log, name));
958 }
960 int plugin_unregister_notification (const char *name)
961 {
962 return (plugin_unregister (list_notification, name));
963 }
965 void plugin_init_all (void)
966 {
967 const char *chain_name;
968 llentry_t *le;
969 int status;
971 /* Init the value cache */
972 uc_init ();
974 chain_name = global_option_get ("PreCacheChain");
975 pre_cache_chain = fc_chain_get_by_name (chain_name);
977 chain_name = global_option_get ("PostCacheChain");
978 post_cache_chain = fc_chain_get_by_name (chain_name);
981 if ((list_init == NULL) && (read_heap == NULL))
982 return;
984 /* Calling all init callbacks before checking if read callbacks
985 * are available allows the init callbacks to register the read
986 * callback. */
987 le = llist_head (list_init);
988 while (le != NULL)
989 {
990 callback_func_t *cf;
991 plugin_init_cb callback;
993 cf = le->value;
994 callback = cf->cf_callback;
995 status = (*callback) ();
997 if (status != 0)
998 {
999 ERROR ("Initialization of plugin `%s' "
1000 "failed with status %i. "
1001 "Plugin will be unloaded.",
1002 le->key, status);
1003 /* Plugins that register read callbacks from the init
1004 * callback should take care of appropriate error
1005 * handling themselves. */
1006 /* FIXME: Unload _all_ functions */
1007 plugin_unregister_read (le->key);
1008 }
1010 le = le->next;
1011 }
1013 /* Start read-threads */
1014 if (read_heap != NULL)
1015 {
1016 const char *rt;
1017 int num;
1018 rt = global_option_get ("ReadThreads");
1019 num = atoi (rt);
1020 if (num != -1)
1021 start_read_threads ((num > 0) ? num : 5);
1022 }
1023 } /* void plugin_init_all */
1025 /* TODO: Rename this function. */
1026 void plugin_read_all (void)
1027 {
1028 uc_check_timeout ();
1030 return;
1031 } /* void plugin_read_all */
1033 /* Read function called when the `-T' command line argument is given. */
1034 int plugin_read_all_once (void)
1035 {
1036 int status;
1037 int return_status = 0;
1039 if (read_heap == NULL)
1040 {
1041 NOTICE ("No read-functions are registered.");
1042 return (0);
1043 }
1045 while (42)
1046 {
1047 read_func_t *rf;
1049 rf = c_head_get_root (read_heap);
1050 if (rf == NULL)
1051 break;
1053 if (rf->rf_type == RF_SIMPLE)
1054 {
1055 int (*callback) (void);
1057 callback = rf->rf_callback;
1058 status = (*callback) ();
1059 }
1060 else
1061 {
1062 plugin_read_cb callback;
1064 callback = rf->rf_callback;
1065 status = (*callback) (&rf->rf_udata);
1066 }
1068 if (status != 0)
1069 {
1070 NOTICE ("read-function of plugin `%s' failed.",
1071 rf->rf_name);
1072 return_status = -1;
1073 }
1075 destroy_callback ((void *) rf);
1076 }
1078 return (return_status);
1079 } /* int plugin_read_all_once */
1081 int plugin_write (const char *plugin, /* {{{ */
1082 const data_set_t *ds, const value_list_t *vl)
1083 {
1084 llentry_t *le;
1085 int status;
1087 if (vl == NULL)
1088 return (EINVAL);
1090 if (list_write == NULL)
1091 return (ENOENT);
1093 if (ds == NULL)
1094 {
1095 ds = plugin_get_ds (vl->type);
1096 if (ds == NULL)
1097 {
1098 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1099 return (ENOENT);
1100 }
1101 }
1103 if (plugin == NULL)
1104 {
1105 int success = 0;
1106 int failure = 0;
1108 le = llist_head (list_write);
1109 while (le != NULL)
1110 {
1111 callback_func_t *cf = le->value;
1112 plugin_write_cb callback;
1114 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1115 callback = cf->cf_callback;
1116 status = (*callback) (ds, vl, &cf->cf_udata);
1117 if (status != 0)
1118 failure++;
1119 else
1120 success++;
1122 le = le->next;
1123 }
1125 if ((success == 0) && (failure != 0))
1126 status = -1;
1127 else
1128 status = 0;
1129 }
1130 else /* plugin != NULL */
1131 {
1132 callback_func_t *cf;
1133 plugin_write_cb callback;
1135 le = llist_head (list_write);
1136 while (le != NULL)
1137 {
1138 if (strcasecmp (plugin, le->key) == 0)
1139 break;
1141 le = le->next;
1142 }
1144 if (le == NULL)
1145 return (ENOENT);
1147 cf = le->value;
1149 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1150 callback = cf->cf_callback;
1151 status = (*callback) (ds, vl, &cf->cf_udata);
1152 }
1154 return (status);
1155 } /* }}} int plugin_write */
1157 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1158 {
1159 llentry_t *le;
1161 if (list_flush == NULL)
1162 return (0);
1164 le = llist_head (list_flush);
1165 while (le != NULL)
1166 {
1167 callback_func_t *cf;
1168 plugin_flush_cb callback;
1170 if ((plugin != NULL)
1171 && (strcmp (plugin, le->key) != 0))
1172 {
1173 le = le->next;
1174 continue;
1175 }
1177 cf = le->value;
1178 callback = cf->cf_callback;
1180 (*callback) (timeout, identifier, &cf->cf_udata);
1182 le = le->next;
1183 }
1184 return (0);
1185 } /* int plugin_flush */
1187 void plugin_shutdown_all (void)
1188 {
1189 llentry_t *le;
1191 stop_read_threads ();
1193 destroy_all_callbacks (&list_init);
1195 pthread_mutex_lock (&read_lock);
1196 llist_destroy (read_list);
1197 read_list = NULL;
1198 pthread_mutex_unlock (&read_lock);
1200 destroy_read_heap ();
1202 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1203 /* identifier = */ NULL);
1205 le = NULL;
1206 if (list_shutdown != NULL)
1207 le = llist_head (list_shutdown);
1209 while (le != NULL)
1210 {
1211 callback_func_t *cf;
1212 plugin_shutdown_cb callback;
1214 cf = le->value;
1215 callback = cf->cf_callback;
1217 /* Advance the pointer before calling the callback allows
1218 * shutdown functions to unregister themselves. If done the
1219 * other way around the memory `le' points to will be freed
1220 * after callback returns. */
1221 le = le->next;
1223 (*callback) ();
1224 }
1226 /* Write plugins which use the `user_data' pointer usually need the
1227 * same data available to the flush callback. If this is the case, set
1228 * the free_function to NULL when registering the flush callback and to
1229 * the real free function when registering the write callback. This way
1230 * the data isn't freed twice. */
1231 destroy_all_callbacks (&list_flush);
1232 destroy_all_callbacks (&list_write);
1234 destroy_all_callbacks (&list_notification);
1235 destroy_all_callbacks (&list_shutdown);
1236 destroy_all_callbacks (&list_log);
1237 } /* void plugin_shutdown_all */
1239 int plugin_dispatch_values (value_list_t *vl)
1240 {
1241 int status;
1242 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1244 value_t *saved_values;
1245 int saved_values_len;
1247 data_set_t *ds;
1249 int free_meta_data = 0;
1251 if ((vl == NULL) || (vl->type[0] == 0)
1252 || (vl->values == NULL) || (vl->values_len < 1))
1253 {
1254 ERROR ("plugin_dispatch_values: Invalid value list.");
1255 return (-1);
1256 }
1258 /* Free meta data only if the calling function didn't specify any. In
1259 * this case matches and targets may add some and the calling function
1260 * may not expect (and therefore free) that data. */
1261 if (vl->meta == NULL)
1262 free_meta_data = 1;
1264 if (list_write == NULL)
1265 c_complain_once (LOG_WARNING, &no_write_complaint,
1266 "plugin_dispatch_values: No write callback has been "
1267 "registered. Please load at least one output plugin, "
1268 "if you want the collected data to be stored.");
1270 if (data_sets == NULL)
1271 {
1272 ERROR ("plugin_dispatch_values: No data sets registered. "
1273 "Could the types database be read? Check "
1274 "your `TypesDB' setting!");
1275 return (-1);
1276 }
1278 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1279 {
1280 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1281 return (-1);
1282 }
1284 if (vl->time == 0)
1285 vl->time = time (NULL);
1287 if (vl->interval <= 0)
1288 vl->interval = interval_g;
1290 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1291 "host = %s; "
1292 "plugin = %s; plugin_instance = %s; "
1293 "type = %s; type_instance = %s;",
1294 (unsigned int) vl->time, vl->interval,
1295 vl->host,
1296 vl->plugin, vl->plugin_instance,
1297 vl->type, vl->type_instance);
1299 #if COLLECT_DEBUG
1300 assert (0 == strcmp (ds->type, vl->type));
1301 #else
1302 if (0 != strcmp (ds->type, vl->type))
1303 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1304 ds->type, vl->type);
1305 #endif
1307 #if COLLECT_DEBUG
1308 assert (ds->ds_num == vl->values_len);
1309 #else
1310 if (ds->ds_num != vl->values_len)
1311 {
1312 ERROR ("plugin_dispatch_values: ds->type = %s: "
1313 "(ds->ds_num = %i) != "
1314 "(vl->values_len = %i)",
1315 ds->type, ds->ds_num, vl->values_len);
1316 return (-1);
1317 }
1318 #endif
1320 escape_slashes (vl->host, sizeof (vl->host));
1321 escape_slashes (vl->plugin, sizeof (vl->plugin));
1322 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1323 escape_slashes (vl->type, sizeof (vl->type));
1324 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1326 /* Copy the values. This way, we can assure `targets' that they get
1327 * dynamically allocated values, which they can free and replace if
1328 * they like. */
1329 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1330 {
1331 saved_values = vl->values;
1332 saved_values_len = vl->values_len;
1334 vl->values = (value_t *) calloc (vl->values_len,
1335 sizeof (*vl->values));
1336 if (vl->values == NULL)
1337 {
1338 ERROR ("plugin_dispatch_values: calloc failed.");
1339 vl->values = saved_values;
1340 return (-1);
1341 }
1342 memcpy (vl->values, saved_values,
1343 vl->values_len * sizeof (*vl->values));
1344 }
1345 else /* if ((pre == NULL) && (post == NULL)) */
1346 {
1347 saved_values = NULL;
1348 saved_values_len = 0;
1349 }
1351 if (pre_cache_chain != NULL)
1352 {
1353 status = fc_process_chain (ds, vl, pre_cache_chain);
1354 if (status < 0)
1355 {
1356 WARNING ("plugin_dispatch_values: Running the "
1357 "pre-cache chain failed with "
1358 "status %i (%#x).",
1359 status, status);
1360 }
1361 else if (status == FC_TARGET_STOP)
1362 {
1363 /* Restore the state of the value_list so that plugins
1364 * don't get confused.. */
1365 if (saved_values != NULL)
1366 {
1367 free (vl->values);
1368 vl->values = saved_values;
1369 vl->values_len = saved_values_len;
1370 }
1371 return (0);
1372 }
1373 }
1375 /* Update the value cache */
1376 uc_update (ds, vl);
1378 /* Initiate threshold checking */
1379 ut_check_threshold (ds, vl);
1381 if (post_cache_chain != NULL)
1382 {
1383 status = fc_process_chain (ds, vl, post_cache_chain);
1384 if (status < 0)
1385 {
1386 WARNING ("plugin_dispatch_values: Running the "
1387 "post-cache chain failed with "
1388 "status %i (%#x).",
1389 status, status);
1390 }
1391 }
1392 else
1393 fc_default_action (ds, vl);
1395 /* Restore the state of the value_list so that plugins don't get
1396 * confused.. */
1397 if (saved_values != NULL)
1398 {
1399 free (vl->values);
1400 vl->values = saved_values;
1401 vl->values_len = saved_values_len;
1402 }
1404 if ((free_meta_data != 0) && (vl->meta != NULL))
1405 {
1406 meta_data_destroy (vl->meta);
1407 vl->meta = NULL;
1408 }
1410 return (0);
1411 } /* int plugin_dispatch_values */
1413 int plugin_dispatch_notification (const notification_t *notif)
1414 {
1415 llentry_t *le;
1416 /* Possible TODO: Add flap detection here */
1418 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1419 "time = %u; host = %s;",
1420 notif->severity, notif->message,
1421 (unsigned int) notif->time, notif->host);
1423 /* Nobody cares for notifications */
1424 if (list_notification == NULL)
1425 return (-1);
1427 le = llist_head (list_notification);
1428 while (le != NULL)
1429 {
1430 callback_func_t *cf;
1431 plugin_notification_cb callback;
1432 int status;
1434 cf = le->value;
1435 callback = cf->cf_callback;
1436 status = (*callback) (notif, &cf->cf_udata);
1437 if (status != 0)
1438 {
1439 WARNING ("plugin_dispatch_notification: Notification "
1440 "callback %s returned %i.",
1441 le->key, status);
1442 }
1444 le = le->next;
1445 }
1447 return (0);
1448 } /* int plugin_dispatch_notification */
1450 void plugin_log (int level, const char *format, ...)
1451 {
1452 char msg[1024];
1453 va_list ap;
1454 llentry_t *le;
1456 if (list_log == NULL)
1457 return;
1459 #if !COLLECT_DEBUG
1460 if (level >= LOG_DEBUG)
1461 return;
1462 #endif
1464 va_start (ap, format);
1465 vsnprintf (msg, sizeof (msg), format, ap);
1466 msg[sizeof (msg) - 1] = '\0';
1467 va_end (ap);
1469 le = llist_head (list_log);
1470 while (le != NULL)
1471 {
1472 callback_func_t *cf;
1473 plugin_log_cb callback;
1475 cf = le->value;
1476 callback = cf->cf_callback;
1478 (*callback) (level, msg, &cf->cf_udata);
1480 le = le->next;
1481 }
1482 } /* void plugin_log */
1484 const data_set_t *plugin_get_ds (const char *name)
1485 {
1486 data_set_t *ds;
1488 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1489 {
1490 DEBUG ("No such dataset registered: %s", name);
1491 return (NULL);
1492 }
1494 return (ds);
1495 } /* data_set_t *plugin_get_ds */
1497 static int plugin_notification_meta_add (notification_t *n,
1498 const char *name,
1499 enum notification_meta_type_e type,
1500 const void *value)
1501 {
1502 notification_meta_t *meta;
1503 notification_meta_t *tail;
1505 if ((n == NULL) || (name == NULL) || (value == NULL))
1506 {
1507 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1508 return (-1);
1509 }
1511 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1512 if (meta == NULL)
1513 {
1514 ERROR ("plugin_notification_meta_add: malloc failed.");
1515 return (-1);
1516 }
1517 memset (meta, 0, sizeof (notification_meta_t));
1519 sstrncpy (meta->name, name, sizeof (meta->name));
1520 meta->type = type;
1522 switch (type)
1523 {
1524 case NM_TYPE_STRING:
1525 {
1526 meta->nm_value.nm_string = strdup ((const char *) value);
1527 if (meta->nm_value.nm_string == NULL)
1528 {
1529 ERROR ("plugin_notification_meta_add: strdup failed.");
1530 sfree (meta);
1531 return (-1);
1532 }
1533 break;
1534 }
1535 case NM_TYPE_SIGNED_INT:
1536 {
1537 meta->nm_value.nm_signed_int = *((int64_t *) value);
1538 break;
1539 }
1540 case NM_TYPE_UNSIGNED_INT:
1541 {
1542 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1543 break;
1544 }
1545 case NM_TYPE_DOUBLE:
1546 {
1547 meta->nm_value.nm_double = *((double *) value);
1548 break;
1549 }
1550 case NM_TYPE_BOOLEAN:
1551 {
1552 meta->nm_value.nm_boolean = *((bool *) value);
1553 break;
1554 }
1555 default:
1556 {
1557 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1558 sfree (meta);
1559 return (-1);
1560 }
1561 } /* switch (type) */
1563 meta->next = NULL;
1564 tail = n->meta;
1565 while ((tail != NULL) && (tail->next != NULL))
1566 tail = tail->next;
1568 if (tail == NULL)
1569 n->meta = meta;
1570 else
1571 tail->next = meta;
1573 return (0);
1574 } /* int plugin_notification_meta_add */
1576 int plugin_notification_meta_add_string (notification_t *n,
1577 const char *name,
1578 const char *value)
1579 {
1580 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1581 }
1583 int plugin_notification_meta_add_signed_int (notification_t *n,
1584 const char *name,
1585 int64_t value)
1586 {
1587 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1588 }
1590 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1591 const char *name,
1592 uint64_t value)
1593 {
1594 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1595 }
1597 int plugin_notification_meta_add_double (notification_t *n,
1598 const char *name,
1599 double value)
1600 {
1601 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1602 }
1604 int plugin_notification_meta_add_boolean (notification_t *n,
1605 const char *name,
1606 bool value)
1607 {
1608 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1609 }
1611 int plugin_notification_meta_copy (notification_t *dst,
1612 const notification_t *src)
1613 {
1614 notification_meta_t *meta;
1616 assert (dst != NULL);
1617 assert (src != NULL);
1618 assert (dst != src);
1619 assert ((src->meta == NULL) || (src->meta != dst->meta));
1621 for (meta = src->meta; meta != NULL; meta = meta->next)
1622 {
1623 if (meta->type == NM_TYPE_STRING)
1624 plugin_notification_meta_add_string (dst, meta->name,
1625 meta->nm_value.nm_string);
1626 else if (meta->type == NM_TYPE_SIGNED_INT)
1627 plugin_notification_meta_add_signed_int (dst, meta->name,
1628 meta->nm_value.nm_signed_int);
1629 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1630 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1631 meta->nm_value.nm_unsigned_int);
1632 else if (meta->type == NM_TYPE_DOUBLE)
1633 plugin_notification_meta_add_double (dst, meta->name,
1634 meta->nm_value.nm_double);
1635 else if (meta->type == NM_TYPE_BOOLEAN)
1636 plugin_notification_meta_add_boolean (dst, meta->name,
1637 meta->nm_value.nm_boolean);
1638 }
1640 return (0);
1641 } /* int plugin_notification_meta_copy */
1643 int plugin_notification_meta_free (notification_meta_t *n)
1644 {
1645 notification_meta_t *this;
1646 notification_meta_t *next;
1648 if (n == NULL)
1649 {
1650 ERROR ("plugin_notification_meta_free: n == NULL!");
1651 return (-1);
1652 }
1654 this = n;
1655 while (this != NULL)
1656 {
1657 next = this->next;
1659 if (this->type == NM_TYPE_STRING)
1660 {
1661 free ((char *)this->nm_value.nm_string);
1662 this->nm_value.nm_string = NULL;
1663 }
1664 sfree (this);
1666 this = next;
1667 }
1669 return (0);
1670 } /* int plugin_notification_meta_free */
1672 /* vim: set sw=8 ts=8 noet fdm=marker : */