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 void *plugin_read_thread (void __attribute__((unused)) *args)
306 {
307 while (read_loop != 0)
308 {
309 read_func_t *rf;
310 struct timeval now;
311 int status;
312 int rf_type;
314 /* Get the read function that needs to be read next. */
315 rf = c_head_get_root (read_heap);
316 if (rf == NULL)
317 {
318 struct timespec abstime;
320 gettimeofday (&now, /* timezone = */ NULL);
322 abstime.tv_sec = now.tv_sec + interval_g;
323 abstime.tv_nsec = 1000 * now.tv_usec;
325 pthread_mutex_lock (&read_lock);
326 pthread_cond_timedwait (&read_cond, &read_lock,
327 &abstime);
328 pthread_mutex_unlock (&read_lock);
329 continue;
330 }
332 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
333 {
334 gettimeofday (&now, /* timezone = */ NULL);
336 rf->rf_interval.tv_sec = interval_g;
337 rf->rf_interval.tv_nsec = 0;
339 rf->rf_effective_interval = rf->rf_interval;
341 rf->rf_next_read.tv_sec = now.tv_sec;
342 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
343 }
345 /* sleep until this entry is due,
346 * using pthread_cond_timedwait */
347 pthread_mutex_lock (&read_lock);
348 pthread_cond_timedwait (&read_cond, &read_lock,
349 &rf->rf_next_read);
350 /* Must hold `real_lock' when accessing `rf->rf_type'. */
351 rf_type = rf->rf_type;
352 pthread_mutex_unlock (&read_lock);
354 /* Check if we're supposed to stop.. This may have interrupted
355 * the sleep, too. */
356 if (read_loop == 0)
357 {
358 /* Insert `rf' again, so it can be free'd correctly */
359 c_heap_insert (read_heap, rf);
360 break;
361 }
363 /* The entry has been marked for deletion. The linked list
364 * entry has already been removed by `plugin_unregister_read'.
365 * All we have to do here is free the `read_func_t' and
366 * continue. */
367 if (rf_type == RF_REMOVE)
368 {
369 DEBUG ("plugin_read_thread: Destroying the `%s' "
370 "callback.", rf->rf_name);
371 destroy_callback ((callback_func_t *) rf);
372 rf = NULL;
373 continue;
374 }
376 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
378 if (rf_type == RF_SIMPLE)
379 {
380 int (*callback) (void);
382 callback = rf->rf_callback;
383 status = (*callback) ();
384 }
385 else
386 {
387 plugin_read_cb callback;
389 assert (rf_type == RF_COMPLEX);
391 callback = rf->rf_callback;
392 status = (*callback) (&rf->rf_udata);
393 }
395 /* If the function signals failure, we will increase the
396 * intervals in which it will be called. */
397 if (status != 0)
398 {
399 rf->rf_effective_interval.tv_sec *= 2;
400 rf->rf_effective_interval.tv_nsec *= 2;
401 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
403 if (rf->rf_effective_interval.tv_sec >= 86400)
404 {
405 rf->rf_effective_interval.tv_sec = 86400;
406 rf->rf_effective_interval.tv_nsec = 0;
407 }
409 NOTICE ("read-function of plugin `%s' failed. "
410 "Will suspend it for %i seconds.",
411 rf->rf_name,
412 (int) rf->rf_effective_interval.tv_sec);
413 }
414 else
415 {
416 /* Success: Restore the interval, if it was changed. */
417 rf->rf_effective_interval = rf->rf_interval;
418 }
420 /* update the ``next read due'' field */
421 gettimeofday (&now, /* timezone = */ NULL);
423 DEBUG ("plugin_read_thread: Effective interval of the "
424 "%s plugin is %i.%09i.",
425 rf->rf_name,
426 (int) rf->rf_effective_interval.tv_sec,
427 (int) rf->rf_effective_interval.tv_nsec);
429 /* Calculate the next (absolute) time at which this function
430 * should be called. */
431 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
432 + rf->rf_effective_interval.tv_sec;
433 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
434 + rf->rf_effective_interval.tv_nsec;
435 NORMALIZE_TIMESPEC (rf->rf_next_read);
437 /* Check, if `rf_next_read' is in the past. */
438 if ((rf->rf_next_read.tv_sec < now.tv_sec)
439 || ((rf->rf_next_read.tv_sec == now.tv_sec)
440 && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
441 {
442 /* `rf_next_read' is in the past. Insert `now'
443 * so this value doesn't trail off into the
444 * past too much. */
445 rf->rf_next_read.tv_sec = now.tv_sec;
446 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
447 }
449 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
450 rf->rf_name,
451 (int) rf->rf_next_read.tv_sec,
452 (int) rf->rf_next_read.tv_nsec);
454 /* Re-insert this read function into the heap again. */
455 c_heap_insert (read_heap, rf);
456 } /* while (read_loop) */
458 pthread_exit (NULL);
459 return ((void *) 0);
460 } /* void *plugin_read_thread */
462 static void start_read_threads (int num)
463 {
464 int i;
466 if (read_threads != NULL)
467 return;
469 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
470 if (read_threads == NULL)
471 {
472 ERROR ("plugin: start_read_threads: calloc failed.");
473 return;
474 }
476 read_threads_num = 0;
477 for (i = 0; i < num; i++)
478 {
479 if (pthread_create (read_threads + read_threads_num, NULL,
480 plugin_read_thread, NULL) == 0)
481 {
482 read_threads_num++;
483 }
484 else
485 {
486 ERROR ("plugin: start_read_threads: pthread_create failed.");
487 return;
488 }
489 } /* for (i) */
490 } /* void start_read_threads */
492 static void stop_read_threads (void)
493 {
494 int i;
496 if (read_threads == NULL)
497 return;
499 INFO ("collectd: Stopping %i read threads.", read_threads_num);
501 pthread_mutex_lock (&read_lock);
502 read_loop = 0;
503 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
504 pthread_cond_broadcast (&read_cond);
505 pthread_mutex_unlock (&read_lock);
507 for (i = 0; i < read_threads_num; i++)
508 {
509 if (pthread_join (read_threads[i], NULL) != 0)
510 {
511 ERROR ("plugin: stop_read_threads: pthread_join failed.");
512 }
513 read_threads[i] = (pthread_t) 0;
514 }
515 sfree (read_threads);
516 read_threads_num = 0;
517 } /* void stop_read_threads */
519 /*
520 * Public functions
521 */
522 void plugin_set_dir (const char *dir)
523 {
524 if (plugindir != NULL)
525 free (plugindir);
527 if (dir == NULL)
528 plugindir = NULL;
529 else if ((plugindir = strdup (dir)) == NULL)
530 {
531 char errbuf[1024];
532 ERROR ("strdup failed: %s",
533 sstrerror (errno, errbuf, sizeof (errbuf)));
534 }
535 }
537 #define BUFSIZE 512
538 int plugin_load (const char *type)
539 {
540 DIR *dh;
541 const char *dir;
542 char filename[BUFSIZE] = "";
543 char typename[BUFSIZE];
544 int typename_len;
545 int ret;
546 struct stat statbuf;
547 struct dirent *de;
548 int status;
550 DEBUG ("type = %s", type);
552 dir = plugin_get_dir ();
553 ret = 1;
555 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
556 * type when matching the filename */
557 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
558 if ((status < 0) || ((size_t) status >= sizeof (typename)))
559 {
560 WARNING ("snprintf: truncated: `%s.so'", type);
561 return (-1);
562 }
563 typename_len = strlen (typename);
565 if ((dh = opendir (dir)) == NULL)
566 {
567 char errbuf[1024];
568 ERROR ("opendir (%s): %s", dir,
569 sstrerror (errno, errbuf, sizeof (errbuf)));
570 return (-1);
571 }
573 while ((de = readdir (dh)) != NULL)
574 {
575 if (strncasecmp (de->d_name, typename, typename_len))
576 continue;
578 status = ssnprintf (filename, sizeof (filename),
579 "%s/%s", dir, de->d_name);
580 if ((status < 0) || ((size_t) status >= sizeof (filename)))
581 {
582 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
583 continue;
584 }
586 if (lstat (filename, &statbuf) == -1)
587 {
588 char errbuf[1024];
589 WARNING ("stat %s: %s", filename,
590 sstrerror (errno, errbuf, sizeof (errbuf)));
591 continue;
592 }
593 else if (!S_ISREG (statbuf.st_mode))
594 {
595 /* don't follow symlinks */
596 WARNING ("stat %s: not a regular file", filename);
597 continue;
598 }
600 if (plugin_load_file (filename) == 0)
601 {
602 /* success */
603 ret = 0;
604 break;
605 }
606 else
607 {
608 fprintf (stderr, "Unable to load plugin %s.\n", type);
609 }
610 }
612 closedir (dh);
614 if (filename[0] == '\0')
615 fprintf (stderr, "Could not find plugin %s.\n", type);
617 return (ret);
618 }
620 /*
621 * The `register_*' functions follow
622 */
623 int plugin_register_config (const char *name,
624 int (*callback) (const char *key, const char *val),
625 const char **keys, int keys_num)
626 {
627 cf_register (name, callback, keys, keys_num);
628 return (0);
629 } /* int plugin_register_config */
631 int plugin_register_complex_config (const char *type,
632 int (*callback) (oconfig_item_t *))
633 {
634 return (cf_register_complex (type, callback));
635 } /* int plugin_register_complex_config */
637 int plugin_register_init (const char *name,
638 int (*callback) (void))
639 {
640 return (create_register_callback (&list_init, name, (void *) callback,
641 /* user_data = */ NULL));
642 } /* plugin_register_init */
644 static int plugin_compare_read_func (const void *arg0, const void *arg1)
645 {
646 const read_func_t *rf0;
647 const read_func_t *rf1;
649 rf0 = arg0;
650 rf1 = arg1;
652 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
653 return (-1);
654 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
655 return (1);
656 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
657 return (-1);
658 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
659 return (1);
660 else
661 return (0);
662 } /* int plugin_compare_read_func */
664 /* Add a read function to both, the heap and a linked list. The linked list if
665 * used to look-up read functions, especially for the remove function. The heap
666 * is used to determine which plugin to read next. */
667 static int plugin_insert_read (read_func_t *rf)
668 {
669 int status;
670 llentry_t *le;
672 pthread_mutex_lock (&read_lock);
674 if (read_list == NULL)
675 {
676 read_list = llist_create ();
677 if (read_list == NULL)
678 {
679 pthread_mutex_unlock (&read_lock);
680 ERROR ("plugin_insert_read: read_list failed.");
681 return (-1);
682 }
683 }
685 if (read_heap == NULL)
686 {
687 read_heap = c_heap_create (plugin_compare_read_func);
688 if (read_heap == NULL)
689 {
690 pthread_mutex_unlock (&read_lock);
691 ERROR ("plugin_insert_read: c_heap_create failed.");
692 return (-1);
693 }
694 }
696 le = llentry_create (rf->rf_name, rf);
697 if (le == NULL)
698 {
699 pthread_mutex_unlock (&read_lock);
700 ERROR ("plugin_insert_read: llentry_create failed.");
701 return (-1);
702 }
704 status = c_heap_insert (read_heap, rf);
705 if (status != 0)
706 {
707 pthread_mutex_unlock (&read_lock);
708 ERROR ("plugin_insert_read: c_heap_insert failed.");
709 llentry_destroy (le);
710 return (-1);
711 }
713 /* This does not fail. */
714 llist_append (read_list, le);
716 pthread_mutex_unlock (&read_lock);
717 return (0);
718 } /* int plugin_insert_read */
720 int plugin_register_read (const char *name,
721 int (*callback) (void))
722 {
723 read_func_t *rf;
725 rf = (read_func_t *) malloc (sizeof (read_func_t));
726 if (rf == NULL)
727 {
728 char errbuf[1024];
729 ERROR ("plugin_register_read: malloc failed: %s",
730 sstrerror (errno, errbuf, sizeof (errbuf)));
731 return (-1);
732 }
734 memset (rf, 0, sizeof (read_func_t));
735 rf->rf_callback = (void *) callback;
736 rf->rf_udata.data = NULL;
737 rf->rf_udata.free_func = NULL;
738 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
739 rf->rf_type = RF_SIMPLE;
740 rf->rf_interval.tv_sec = 0;
741 rf->rf_interval.tv_nsec = 0;
742 rf->rf_effective_interval = rf->rf_interval;
744 return (plugin_insert_read (rf));
745 } /* int plugin_register_read */
747 int plugin_register_complex_read (const char *name,
748 plugin_read_cb callback,
749 const struct timespec *interval,
750 user_data_t *user_data)
751 {
752 read_func_t *rf;
754 rf = (read_func_t *) malloc (sizeof (read_func_t));
755 if (rf == NULL)
756 {
757 ERROR ("plugin_register_complex_read: malloc failed.");
758 return (-1);
759 }
761 memset (rf, 0, sizeof (read_func_t));
762 rf->rf_callback = (void *) callback;
763 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
764 rf->rf_type = RF_COMPLEX;
765 if (interval != NULL)
766 {
767 rf->rf_interval = *interval;
768 }
769 rf->rf_effective_interval = rf->rf_interval;
771 /* Set user data */
772 if (user_data == NULL)
773 {
774 rf->rf_udata.data = NULL;
775 rf->rf_udata.free_func = NULL;
776 }
777 else
778 {
779 rf->rf_udata = *user_data;
780 }
782 return (plugin_insert_read (rf));
783 } /* int plugin_register_complex_read */
785 int plugin_register_write (const char *name,
786 plugin_write_cb callback, user_data_t *ud)
787 {
788 return (create_register_callback (&list_write, name,
789 (void *) callback, ud));
790 } /* int plugin_register_write */
792 int plugin_register_flush (const char *name,
793 plugin_flush_cb callback, user_data_t *ud)
794 {
795 return (create_register_callback (&list_flush, name,
796 (void *) callback, ud));
797 } /* int plugin_register_flush */
799 int plugin_register_shutdown (char *name,
800 int (*callback) (void))
801 {
802 return (create_register_callback (&list_shutdown, name,
803 (void *) callback, /* user_data = */ NULL));
804 } /* int plugin_register_shutdown */
806 int plugin_register_data_set (const data_set_t *ds)
807 {
808 data_set_t *ds_copy;
809 int i;
811 if ((data_sets != NULL)
812 && (c_avl_get (data_sets, ds->type, NULL) == 0))
813 {
814 NOTICE ("Replacing DS `%s' with another version.", ds->type);
815 plugin_unregister_data_set (ds->type);
816 }
817 else if (data_sets == NULL)
818 {
819 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
820 if (data_sets == NULL)
821 return (-1);
822 }
824 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
825 if (ds_copy == NULL)
826 return (-1);
827 memcpy(ds_copy, ds, sizeof (data_set_t));
829 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
830 * ds->ds_num);
831 if (ds_copy->ds == NULL)
832 {
833 free (ds_copy);
834 return (-1);
835 }
837 for (i = 0; i < ds->ds_num; i++)
838 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
840 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
841 } /* int plugin_register_data_set */
843 int plugin_register_log (const char *name,
844 plugin_log_cb callback, user_data_t *ud)
845 {
846 return (create_register_callback (&list_log, name,
847 (void *) callback, ud));
848 } /* int plugin_register_log */
850 int plugin_register_notification (const char *name,
851 plugin_notification_cb callback, user_data_t *ud)
852 {
853 return (create_register_callback (&list_notification, name,
854 (void *) callback, ud));
855 } /* int plugin_register_log */
857 int plugin_unregister_config (const char *name)
858 {
859 cf_unregister (name);
860 return (0);
861 } /* int plugin_unregister_config */
863 int plugin_unregister_complex_config (const char *name)
864 {
865 cf_unregister_complex (name);
866 return (0);
867 } /* int plugin_unregister_complex_config */
869 int plugin_unregister_init (const char *name)
870 {
871 return (plugin_unregister (list_init, name));
872 }
874 int plugin_unregister_read (const char *name) /* {{{ */
875 {
876 llentry_t *le;
877 read_func_t *rf;
879 if (name == NULL)
880 return (-ENOENT);
882 pthread_mutex_lock (&read_lock);
884 if (read_list == NULL)
885 {
886 pthread_mutex_unlock (&read_lock);
887 return (-ENOENT);
888 }
890 le = llist_search (read_list, name);
891 llist_remove (read_list, le);
893 rf = le->value;
894 assert (rf != NULL);
895 rf->rf_type = RF_REMOVE;
897 pthread_mutex_unlock (&read_lock);
899 llentry_destroy (le);
901 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
903 return (0);
904 } /* }}} int plugin_unregister_read */
906 int plugin_unregister_write (const char *name)
907 {
908 return (plugin_unregister (list_write, name));
909 }
911 int plugin_unregister_flush (const char *name)
912 {
913 return (plugin_unregister (list_flush, name));
914 }
916 int plugin_unregister_shutdown (const char *name)
917 {
918 return (plugin_unregister (list_shutdown, name));
919 }
921 int plugin_unregister_data_set (const char *name)
922 {
923 data_set_t *ds;
925 if (data_sets == NULL)
926 return (-1);
928 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
929 return (-1);
931 sfree (ds->ds);
932 sfree (ds);
934 return (0);
935 } /* int plugin_unregister_data_set */
937 int plugin_unregister_log (const char *name)
938 {
939 return (plugin_unregister (list_log, name));
940 }
942 int plugin_unregister_notification (const char *name)
943 {
944 return (plugin_unregister (list_notification, name));
945 }
947 void plugin_init_all (void)
948 {
949 const char *chain_name;
950 llentry_t *le;
951 int status;
953 /* Init the value cache */
954 uc_init ();
956 chain_name = global_option_get ("PreCacheChain");
957 pre_cache_chain = fc_chain_get_by_name (chain_name);
959 chain_name = global_option_get ("PostCacheChain");
960 post_cache_chain = fc_chain_get_by_name (chain_name);
963 if ((list_init == NULL) && (read_heap == NULL))
964 return;
966 /* Calling all init callbacks before checking if read callbacks
967 * are available allows the init callbacks to register the read
968 * callback. */
969 le = llist_head (list_init);
970 while (le != NULL)
971 {
972 callback_func_t *cf;
973 plugin_init_cb callback;
975 cf = le->value;
976 callback = cf->cf_callback;
977 status = (*callback) ();
979 if (status != 0)
980 {
981 ERROR ("Initialization of plugin `%s' "
982 "failed with status %i. "
983 "Plugin will be unloaded.",
984 le->key, status);
985 /* Plugins that register read callbacks from the init
986 * callback should take care of appropriate error
987 * handling themselves. */
988 /* FIXME: Unload _all_ functions */
989 plugin_unregister_read (le->key);
990 }
992 le = le->next;
993 }
995 /* Start read-threads */
996 if (read_heap != NULL)
997 {
998 const char *rt;
999 int num;
1000 rt = global_option_get ("ReadThreads");
1001 num = atoi (rt);
1002 if (num != -1)
1003 start_read_threads ((num > 0) ? num : 5);
1004 }
1005 } /* void plugin_init_all */
1007 /* TODO: Rename this function. */
1008 void plugin_read_all (void)
1009 {
1010 uc_check_timeout ();
1012 return;
1013 } /* void plugin_read_all */
1015 /* Read function called when the `-T' command line argument is given. */
1016 int plugin_read_all_once (void)
1017 {
1018 int status;
1019 int return_status = 0;
1021 if (read_heap == NULL)
1022 {
1023 NOTICE ("No read-functions are registered.");
1024 return (0);
1025 }
1027 while (42)
1028 {
1029 read_func_t *rf;
1031 rf = c_head_get_root (read_heap);
1032 if (rf == NULL)
1033 break;
1035 if (rf->rf_type == RF_SIMPLE)
1036 {
1037 int (*callback) (void);
1039 callback = rf->rf_callback;
1040 status = (*callback) ();
1041 }
1042 else
1043 {
1044 plugin_read_cb callback;
1046 callback = rf->rf_callback;
1047 status = (*callback) (&rf->rf_udata);
1048 }
1050 if (status != 0)
1051 {
1052 NOTICE ("read-function of plugin `%s' failed.",
1053 rf->rf_name);
1054 return_status = -1;
1055 }
1057 destroy_callback ((void *) rf);
1058 }
1060 return (return_status);
1061 } /* int plugin_read_all_once */
1063 int plugin_write (const char *plugin, /* {{{ */
1064 const data_set_t *ds, const value_list_t *vl)
1065 {
1066 llentry_t *le;
1067 int status;
1069 if (vl == NULL)
1070 return (EINVAL);
1072 if (list_write == NULL)
1073 return (ENOENT);
1075 if (ds == NULL)
1076 {
1077 ds = plugin_get_ds (vl->type);
1078 if (ds == NULL)
1079 {
1080 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1081 return (ENOENT);
1082 }
1083 }
1085 if (plugin == NULL)
1086 {
1087 int success = 0;
1088 int failure = 0;
1090 le = llist_head (list_write);
1091 while (le != NULL)
1092 {
1093 callback_func_t *cf = le->value;
1094 plugin_write_cb callback;
1096 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1097 callback = cf->cf_callback;
1098 status = (*callback) (ds, vl, &cf->cf_udata);
1099 if (status != 0)
1100 failure++;
1101 else
1102 success++;
1104 le = le->next;
1105 }
1107 if ((success == 0) && (failure != 0))
1108 status = -1;
1109 else
1110 status = 0;
1111 }
1112 else /* plugin != NULL */
1113 {
1114 callback_func_t *cf;
1115 plugin_write_cb callback;
1117 le = llist_head (list_write);
1118 while (le != NULL)
1119 {
1120 if (strcasecmp (plugin, le->key) == 0)
1121 break;
1123 le = le->next;
1124 }
1126 if (le == NULL)
1127 return (ENOENT);
1129 cf = le->value;
1131 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1132 callback = cf->cf_callback;
1133 status = (*callback) (ds, vl, &cf->cf_udata);
1134 }
1136 return (status);
1137 } /* }}} int plugin_write */
1139 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1140 {
1141 llentry_t *le;
1143 if (list_flush == NULL)
1144 return (0);
1146 le = llist_head (list_flush);
1147 while (le != NULL)
1148 {
1149 callback_func_t *cf;
1150 plugin_flush_cb callback;
1152 if ((plugin != NULL)
1153 && (strcmp (plugin, le->key) != 0))
1154 {
1155 le = le->next;
1156 continue;
1157 }
1159 cf = le->value;
1160 callback = cf->cf_callback;
1162 (*callback) (timeout, identifier, &cf->cf_udata);
1164 le = le->next;
1165 }
1166 return (0);
1167 } /* int plugin_flush */
1169 void plugin_shutdown_all (void)
1170 {
1171 llentry_t *le;
1173 stop_read_threads ();
1175 destroy_all_callbacks (&list_init);
1177 pthread_mutex_lock (&read_lock);
1178 llist_destroy (read_list);
1179 read_list = NULL;
1180 pthread_mutex_unlock (&read_lock);
1182 destroy_read_heap ();
1184 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1185 /* identifier = */ NULL);
1187 le = NULL;
1188 if (list_shutdown != NULL)
1189 le = llist_head (list_shutdown);
1191 while (le != NULL)
1192 {
1193 callback_func_t *cf;
1194 plugin_shutdown_cb callback;
1196 cf = le->value;
1197 callback = cf->cf_callback;
1199 /* Advance the pointer before calling the callback allows
1200 * shutdown functions to unregister themselves. If done the
1201 * other way around the memory `le' points to will be freed
1202 * after callback returns. */
1203 le = le->next;
1205 (*callback) ();
1206 }
1208 destroy_all_callbacks (&list_write);
1209 destroy_all_callbacks (&list_flush);
1210 destroy_all_callbacks (&list_notification);
1211 destroy_all_callbacks (&list_shutdown);
1212 destroy_all_callbacks (&list_log);
1213 } /* void plugin_shutdown_all */
1215 int plugin_dispatch_values (value_list_t *vl)
1216 {
1217 int status;
1218 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1220 value_t *saved_values;
1221 int saved_values_len;
1223 data_set_t *ds;
1225 int free_meta_data = 0;
1227 if ((vl == NULL) || (vl->type[0] == 0)
1228 || (vl->values == NULL) || (vl->values_len < 1))
1229 {
1230 ERROR ("plugin_dispatch_values: Invalid value list.");
1231 return (-1);
1232 }
1234 /* Free meta data only if the calling function didn't specify any. In
1235 * this case matches and targets may add some and the calling function
1236 * may not expect (and therefore free) that data. */
1237 if (vl->meta == NULL)
1238 free_meta_data = 1;
1240 if (list_write == NULL)
1241 c_complain_once (LOG_WARNING, &no_write_complaint,
1242 "plugin_dispatch_values: No write callback has been "
1243 "registered. Please load at least one output plugin, "
1244 "if you want the collected data to be stored.");
1246 if (data_sets == NULL)
1247 {
1248 ERROR ("plugin_dispatch_values: No data sets registered. "
1249 "Could the types database be read? Check "
1250 "your `TypesDB' setting!");
1251 return (-1);
1252 }
1254 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1255 {
1256 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1257 return (-1);
1258 }
1260 if (vl->time == 0)
1261 vl->time = time (NULL);
1263 if (vl->interval <= 0)
1264 vl->interval = interval_g;
1266 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1267 "host = %s; "
1268 "plugin = %s; plugin_instance = %s; "
1269 "type = %s; type_instance = %s;",
1270 (unsigned int) vl->time, vl->interval,
1271 vl->host,
1272 vl->plugin, vl->plugin_instance,
1273 vl->type, vl->type_instance);
1275 #if COLLECT_DEBUG
1276 assert (0 == strcmp (ds->type, vl->type));
1277 #else
1278 if (0 != strcmp (ds->type, vl->type))
1279 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1280 ds->type, vl->type);
1281 #endif
1283 #if COLLECT_DEBUG
1284 assert (ds->ds_num == vl->values_len);
1285 #else
1286 if (ds->ds_num != vl->values_len)
1287 {
1288 ERROR ("plugin_dispatch_values: ds->type = %s: "
1289 "(ds->ds_num = %i) != "
1290 "(vl->values_len = %i)",
1291 ds->type, ds->ds_num, vl->values_len);
1292 return (-1);
1293 }
1294 #endif
1296 escape_slashes (vl->host, sizeof (vl->host));
1297 escape_slashes (vl->plugin, sizeof (vl->plugin));
1298 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1299 escape_slashes (vl->type, sizeof (vl->type));
1300 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1302 /* Copy the values. This way, we can assure `targets' that they get
1303 * dynamically allocated values, which they can free and replace if
1304 * they like. */
1305 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1306 {
1307 saved_values = vl->values;
1308 saved_values_len = vl->values_len;
1310 vl->values = (value_t *) calloc (vl->values_len,
1311 sizeof (*vl->values));
1312 if (vl->values == NULL)
1313 {
1314 ERROR ("plugin_dispatch_values: calloc failed.");
1315 vl->values = saved_values;
1316 return (-1);
1317 }
1318 memcpy (vl->values, saved_values,
1319 vl->values_len * sizeof (*vl->values));
1320 }
1321 else /* if ((pre == NULL) && (post == NULL)) */
1322 {
1323 saved_values = NULL;
1324 saved_values_len = 0;
1325 }
1327 if (pre_cache_chain != NULL)
1328 {
1329 status = fc_process_chain (ds, vl, pre_cache_chain);
1330 if (status < 0)
1331 {
1332 WARNING ("plugin_dispatch_values: Running the "
1333 "pre-cache chain failed with "
1334 "status %i (%#x).",
1335 status, status);
1336 }
1337 else if (status == FC_TARGET_STOP)
1338 {
1339 /* Restore the state of the value_list so that plugins
1340 * don't get confused.. */
1341 if (saved_values != NULL)
1342 {
1343 free (vl->values);
1344 vl->values = saved_values;
1345 vl->values_len = saved_values_len;
1346 }
1347 return (0);
1348 }
1349 }
1351 /* Update the value cache */
1352 uc_update (ds, vl);
1354 /* Initiate threshold checking */
1355 ut_check_threshold (ds, vl);
1357 if (post_cache_chain != NULL)
1358 {
1359 status = fc_process_chain (ds, vl, post_cache_chain);
1360 if (status < 0)
1361 {
1362 WARNING ("plugin_dispatch_values: Running the "
1363 "post-cache chain failed with "
1364 "status %i (%#x).",
1365 status, status);
1366 }
1367 }
1368 else
1369 fc_default_action (ds, vl);
1371 /* Restore the state of the value_list so that plugins don't get
1372 * confused.. */
1373 if (saved_values != NULL)
1374 {
1375 free (vl->values);
1376 vl->values = saved_values;
1377 vl->values_len = saved_values_len;
1378 }
1380 if ((free_meta_data != 0) && (vl->meta != NULL))
1381 {
1382 meta_data_destroy (vl->meta);
1383 vl->meta = NULL;
1384 }
1386 return (0);
1387 } /* int plugin_dispatch_values */
1389 int plugin_dispatch_notification (const notification_t *notif)
1390 {
1391 llentry_t *le;
1392 /* Possible TODO: Add flap detection here */
1394 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1395 "time = %u; host = %s;",
1396 notif->severity, notif->message,
1397 (unsigned int) notif->time, notif->host);
1399 /* Nobody cares for notifications */
1400 if (list_notification == NULL)
1401 return (-1);
1403 le = llist_head (list_notification);
1404 while (le != NULL)
1405 {
1406 callback_func_t *cf;
1407 plugin_notification_cb callback;
1408 int status;
1410 cf = le->value;
1411 callback = cf->cf_callback;
1412 status = (*callback) (notif, &cf->cf_udata);
1413 if (status != 0)
1414 {
1415 WARNING ("plugin_dispatch_notification: Notification "
1416 "callback %s returned %i.",
1417 le->key, status);
1418 }
1420 le = le->next;
1421 }
1423 return (0);
1424 } /* int plugin_dispatch_notification */
1426 void plugin_log (int level, const char *format, ...)
1427 {
1428 char msg[1024];
1429 va_list ap;
1430 llentry_t *le;
1432 if (list_log == NULL)
1433 return;
1435 #if !COLLECT_DEBUG
1436 if (level >= LOG_DEBUG)
1437 return;
1438 #endif
1440 va_start (ap, format);
1441 vsnprintf (msg, sizeof (msg), format, ap);
1442 msg[sizeof (msg) - 1] = '\0';
1443 va_end (ap);
1445 le = llist_head (list_log);
1446 while (le != NULL)
1447 {
1448 callback_func_t *cf;
1449 plugin_log_cb callback;
1451 cf = le->value;
1452 callback = cf->cf_callback;
1454 (*callback) (level, msg, &cf->cf_udata);
1456 le = le->next;
1457 }
1458 } /* void plugin_log */
1460 const data_set_t *plugin_get_ds (const char *name)
1461 {
1462 data_set_t *ds;
1464 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1465 {
1466 DEBUG ("No such dataset registered: %s", name);
1467 return (NULL);
1468 }
1470 return (ds);
1471 } /* data_set_t *plugin_get_ds */
1473 static int plugin_notification_meta_add (notification_t *n,
1474 const char *name,
1475 enum notification_meta_type_e type,
1476 const void *value)
1477 {
1478 notification_meta_t *meta;
1479 notification_meta_t *tail;
1481 if ((n == NULL) || (name == NULL) || (value == NULL))
1482 {
1483 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1484 return (-1);
1485 }
1487 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1488 if (meta == NULL)
1489 {
1490 ERROR ("plugin_notification_meta_add: malloc failed.");
1491 return (-1);
1492 }
1493 memset (meta, 0, sizeof (notification_meta_t));
1495 sstrncpy (meta->name, name, sizeof (meta->name));
1496 meta->type = type;
1498 switch (type)
1499 {
1500 case NM_TYPE_STRING:
1501 {
1502 meta->nm_value.nm_string = strdup ((const char *) value);
1503 if (meta->nm_value.nm_string == NULL)
1504 {
1505 ERROR ("plugin_notification_meta_add: strdup failed.");
1506 sfree (meta);
1507 return (-1);
1508 }
1509 break;
1510 }
1511 case NM_TYPE_SIGNED_INT:
1512 {
1513 meta->nm_value.nm_signed_int = *((int64_t *) value);
1514 break;
1515 }
1516 case NM_TYPE_UNSIGNED_INT:
1517 {
1518 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1519 break;
1520 }
1521 case NM_TYPE_DOUBLE:
1522 {
1523 meta->nm_value.nm_double = *((double *) value);
1524 break;
1525 }
1526 case NM_TYPE_BOOLEAN:
1527 {
1528 meta->nm_value.nm_boolean = *((bool *) value);
1529 break;
1530 }
1531 default:
1532 {
1533 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1534 sfree (meta);
1535 return (-1);
1536 }
1537 } /* switch (type) */
1539 meta->next = NULL;
1540 tail = n->meta;
1541 while ((tail != NULL) && (tail->next != NULL))
1542 tail = tail->next;
1544 if (tail == NULL)
1545 n->meta = meta;
1546 else
1547 tail->next = meta;
1549 return (0);
1550 } /* int plugin_notification_meta_add */
1552 int plugin_notification_meta_add_string (notification_t *n,
1553 const char *name,
1554 const char *value)
1555 {
1556 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1557 }
1559 int plugin_notification_meta_add_signed_int (notification_t *n,
1560 const char *name,
1561 int64_t value)
1562 {
1563 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1564 }
1566 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1567 const char *name,
1568 uint64_t value)
1569 {
1570 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1571 }
1573 int plugin_notification_meta_add_double (notification_t *n,
1574 const char *name,
1575 double value)
1576 {
1577 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1578 }
1580 int plugin_notification_meta_add_boolean (notification_t *n,
1581 const char *name,
1582 bool value)
1583 {
1584 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1585 }
1587 int plugin_notification_meta_copy (notification_t *dst,
1588 const notification_t *src)
1589 {
1590 notification_meta_t *meta;
1592 assert (dst != NULL);
1593 assert (src != NULL);
1594 assert (dst != src);
1595 assert ((src->meta == NULL) || (src->meta != dst->meta));
1597 for (meta = src->meta; meta != NULL; meta = meta->next)
1598 {
1599 if (meta->type == NM_TYPE_STRING)
1600 plugin_notification_meta_add_string (dst, meta->name,
1601 meta->nm_value.nm_string);
1602 else if (meta->type == NM_TYPE_SIGNED_INT)
1603 plugin_notification_meta_add_signed_int (dst, meta->name,
1604 meta->nm_value.nm_signed_int);
1605 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1606 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1607 meta->nm_value.nm_unsigned_int);
1608 else if (meta->type == NM_TYPE_DOUBLE)
1609 plugin_notification_meta_add_double (dst, meta->name,
1610 meta->nm_value.nm_double);
1611 else if (meta->type == NM_TYPE_BOOLEAN)
1612 plugin_notification_meta_add_boolean (dst, meta->name,
1613 meta->nm_value.nm_boolean);
1614 }
1616 return (0);
1617 } /* int plugin_notification_meta_copy */
1619 int plugin_notification_meta_free (notification_meta_t *n)
1620 {
1621 notification_meta_t *this;
1622 notification_meta_t *next;
1624 if (n == NULL)
1625 {
1626 ERROR ("plugin_notification_meta_free: n == NULL!");
1627 return (-1);
1628 }
1630 this = n;
1631 while (this != NULL)
1632 {
1633 next = this->next;
1635 if (this->type == NM_TYPE_STRING)
1636 {
1637 free ((char *)this->nm_value.nm_string);
1638 this->nm_value.nm_string = NULL;
1639 }
1640 sfree (this);
1642 this = next;
1643 }
1645 return (0);
1646 } /* int plugin_notification_meta_free */
1648 /* vim: set sw=8 ts=8 noet fdm=marker : */