6fb75206a686cecebafc78b91389901d892e1f75
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 if (le == NULL)
892 {
893 pthread_mutex_unlock (&read_lock);
894 WARNING ("plugin_unregister_read: No such read function: %s",
895 name);
896 return (-ENOENT);
897 }
899 llist_remove (read_list, le);
901 rf = le->value;
902 assert (rf != NULL);
903 rf->rf_type = RF_REMOVE;
905 pthread_mutex_unlock (&read_lock);
907 llentry_destroy (le);
909 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
911 return (0);
912 } /* }}} int plugin_unregister_read */
914 int plugin_unregister_write (const char *name)
915 {
916 return (plugin_unregister (list_write, name));
917 }
919 int plugin_unregister_flush (const char *name)
920 {
921 return (plugin_unregister (list_flush, name));
922 }
924 int plugin_unregister_shutdown (const char *name)
925 {
926 return (plugin_unregister (list_shutdown, name));
927 }
929 int plugin_unregister_data_set (const char *name)
930 {
931 data_set_t *ds;
933 if (data_sets == NULL)
934 return (-1);
936 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
937 return (-1);
939 sfree (ds->ds);
940 sfree (ds);
942 return (0);
943 } /* int plugin_unregister_data_set */
945 int plugin_unregister_log (const char *name)
946 {
947 return (plugin_unregister (list_log, name));
948 }
950 int plugin_unregister_notification (const char *name)
951 {
952 return (plugin_unregister (list_notification, name));
953 }
955 void plugin_init_all (void)
956 {
957 const char *chain_name;
958 llentry_t *le;
959 int status;
961 /* Init the value cache */
962 uc_init ();
964 chain_name = global_option_get ("PreCacheChain");
965 pre_cache_chain = fc_chain_get_by_name (chain_name);
967 chain_name = global_option_get ("PostCacheChain");
968 post_cache_chain = fc_chain_get_by_name (chain_name);
971 if ((list_init == NULL) && (read_heap == NULL))
972 return;
974 /* Calling all init callbacks before checking if read callbacks
975 * are available allows the init callbacks to register the read
976 * callback. */
977 le = llist_head (list_init);
978 while (le != NULL)
979 {
980 callback_func_t *cf;
981 plugin_init_cb callback;
983 cf = le->value;
984 callback = cf->cf_callback;
985 status = (*callback) ();
987 if (status != 0)
988 {
989 ERROR ("Initialization of plugin `%s' "
990 "failed with status %i. "
991 "Plugin will be unloaded.",
992 le->key, status);
993 /* Plugins that register read callbacks from the init
994 * callback should take care of appropriate error
995 * handling themselves. */
996 /* FIXME: Unload _all_ functions */
997 plugin_unregister_read (le->key);
998 }
1000 le = le->next;
1001 }
1003 /* Start read-threads */
1004 if (read_heap != NULL)
1005 {
1006 const char *rt;
1007 int num;
1008 rt = global_option_get ("ReadThreads");
1009 num = atoi (rt);
1010 if (num != -1)
1011 start_read_threads ((num > 0) ? num : 5);
1012 }
1013 } /* void plugin_init_all */
1015 /* TODO: Rename this function. */
1016 void plugin_read_all (void)
1017 {
1018 uc_check_timeout ();
1020 return;
1021 } /* void plugin_read_all */
1023 /* Read function called when the `-T' command line argument is given. */
1024 int plugin_read_all_once (void)
1025 {
1026 int status;
1027 int return_status = 0;
1029 if (read_heap == NULL)
1030 {
1031 NOTICE ("No read-functions are registered.");
1032 return (0);
1033 }
1035 while (42)
1036 {
1037 read_func_t *rf;
1039 rf = c_head_get_root (read_heap);
1040 if (rf == NULL)
1041 break;
1043 if (rf->rf_type == RF_SIMPLE)
1044 {
1045 int (*callback) (void);
1047 callback = rf->rf_callback;
1048 status = (*callback) ();
1049 }
1050 else
1051 {
1052 plugin_read_cb callback;
1054 callback = rf->rf_callback;
1055 status = (*callback) (&rf->rf_udata);
1056 }
1058 if (status != 0)
1059 {
1060 NOTICE ("read-function of plugin `%s' failed.",
1061 rf->rf_name);
1062 return_status = -1;
1063 }
1065 destroy_callback ((void *) rf);
1066 }
1068 return (return_status);
1069 } /* int plugin_read_all_once */
1071 int plugin_write (const char *plugin, /* {{{ */
1072 const data_set_t *ds, const value_list_t *vl)
1073 {
1074 llentry_t *le;
1075 int status;
1077 if (vl == NULL)
1078 return (EINVAL);
1080 if (list_write == NULL)
1081 return (ENOENT);
1083 if (ds == NULL)
1084 {
1085 ds = plugin_get_ds (vl->type);
1086 if (ds == NULL)
1087 {
1088 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1089 return (ENOENT);
1090 }
1091 }
1093 if (plugin == NULL)
1094 {
1095 int success = 0;
1096 int failure = 0;
1098 le = llist_head (list_write);
1099 while (le != NULL)
1100 {
1101 callback_func_t *cf = le->value;
1102 plugin_write_cb callback;
1104 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1105 callback = cf->cf_callback;
1106 status = (*callback) (ds, vl, &cf->cf_udata);
1107 if (status != 0)
1108 failure++;
1109 else
1110 success++;
1112 le = le->next;
1113 }
1115 if ((success == 0) && (failure != 0))
1116 status = -1;
1117 else
1118 status = 0;
1119 }
1120 else /* plugin != NULL */
1121 {
1122 callback_func_t *cf;
1123 plugin_write_cb callback;
1125 le = llist_head (list_write);
1126 while (le != NULL)
1127 {
1128 if (strcasecmp (plugin, le->key) == 0)
1129 break;
1131 le = le->next;
1132 }
1134 if (le == NULL)
1135 return (ENOENT);
1137 cf = le->value;
1139 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1140 callback = cf->cf_callback;
1141 status = (*callback) (ds, vl, &cf->cf_udata);
1142 }
1144 return (status);
1145 } /* }}} int plugin_write */
1147 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1148 {
1149 llentry_t *le;
1151 if (list_flush == NULL)
1152 return (0);
1154 le = llist_head (list_flush);
1155 while (le != NULL)
1156 {
1157 callback_func_t *cf;
1158 plugin_flush_cb callback;
1160 if ((plugin != NULL)
1161 && (strcmp (plugin, le->key) != 0))
1162 {
1163 le = le->next;
1164 continue;
1165 }
1167 cf = le->value;
1168 callback = cf->cf_callback;
1170 (*callback) (timeout, identifier, &cf->cf_udata);
1172 le = le->next;
1173 }
1174 return (0);
1175 } /* int plugin_flush */
1177 void plugin_shutdown_all (void)
1178 {
1179 llentry_t *le;
1181 stop_read_threads ();
1183 destroy_all_callbacks (&list_init);
1185 pthread_mutex_lock (&read_lock);
1186 llist_destroy (read_list);
1187 read_list = NULL;
1188 pthread_mutex_unlock (&read_lock);
1190 destroy_read_heap ();
1192 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1193 /* identifier = */ NULL);
1195 le = NULL;
1196 if (list_shutdown != NULL)
1197 le = llist_head (list_shutdown);
1199 while (le != NULL)
1200 {
1201 callback_func_t *cf;
1202 plugin_shutdown_cb callback;
1204 cf = le->value;
1205 callback = cf->cf_callback;
1207 /* Advance the pointer before calling the callback allows
1208 * shutdown functions to unregister themselves. If done the
1209 * other way around the memory `le' points to will be freed
1210 * after callback returns. */
1211 le = le->next;
1213 (*callback) ();
1214 }
1216 destroy_all_callbacks (&list_write);
1217 destroy_all_callbacks (&list_flush);
1218 destroy_all_callbacks (&list_notification);
1219 destroy_all_callbacks (&list_shutdown);
1220 destroy_all_callbacks (&list_log);
1221 } /* void plugin_shutdown_all */
1223 int plugin_dispatch_values (value_list_t *vl)
1224 {
1225 int status;
1226 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1228 value_t *saved_values;
1229 int saved_values_len;
1231 data_set_t *ds;
1233 if ((vl == NULL) || (vl->type[0] == 0)
1234 || (vl->values == NULL) || (vl->values_len < 1))
1235 {
1236 ERROR ("plugin_dispatch_values: Invalid value list.");
1237 return (-1);
1238 }
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 return (0);
1381 } /* int plugin_dispatch_values */
1383 int plugin_dispatch_notification (const notification_t *notif)
1384 {
1385 llentry_t *le;
1386 /* Possible TODO: Add flap detection here */
1388 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1389 "time = %u; host = %s;",
1390 notif->severity, notif->message,
1391 (unsigned int) notif->time, notif->host);
1393 /* Nobody cares for notifications */
1394 if (list_notification == NULL)
1395 return (-1);
1397 le = llist_head (list_notification);
1398 while (le != NULL)
1399 {
1400 callback_func_t *cf;
1401 plugin_notification_cb callback;
1402 int status;
1404 cf = le->value;
1405 callback = cf->cf_callback;
1406 status = (*callback) (notif, &cf->cf_udata);
1407 if (status != 0)
1408 {
1409 WARNING ("plugin_dispatch_notification: Notification "
1410 "callback %s returned %i.",
1411 le->key, status);
1412 }
1414 le = le->next;
1415 }
1417 return (0);
1418 } /* int plugin_dispatch_notification */
1420 void plugin_log (int level, const char *format, ...)
1421 {
1422 char msg[1024];
1423 va_list ap;
1424 llentry_t *le;
1426 if (list_log == NULL)
1427 return;
1429 #if !COLLECT_DEBUG
1430 if (level >= LOG_DEBUG)
1431 return;
1432 #endif
1434 va_start (ap, format);
1435 vsnprintf (msg, sizeof (msg), format, ap);
1436 msg[sizeof (msg) - 1] = '\0';
1437 va_end (ap);
1439 le = llist_head (list_log);
1440 while (le != NULL)
1441 {
1442 callback_func_t *cf;
1443 plugin_log_cb callback;
1445 cf = le->value;
1446 callback = cf->cf_callback;
1448 (*callback) (level, msg, &cf->cf_udata);
1450 le = le->next;
1451 }
1452 } /* void plugin_log */
1454 const data_set_t *plugin_get_ds (const char *name)
1455 {
1456 data_set_t *ds;
1458 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1459 {
1460 DEBUG ("No such dataset registered: %s", name);
1461 return (NULL);
1462 }
1464 return (ds);
1465 } /* data_set_t *plugin_get_ds */
1467 static int plugin_notification_meta_add (notification_t *n,
1468 const char *name,
1469 enum notification_meta_type_e type,
1470 const void *value)
1471 {
1472 notification_meta_t *meta;
1473 notification_meta_t *tail;
1475 if ((n == NULL) || (name == NULL) || (value == NULL))
1476 {
1477 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1478 return (-1);
1479 }
1481 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1482 if (meta == NULL)
1483 {
1484 ERROR ("plugin_notification_meta_add: malloc failed.");
1485 return (-1);
1486 }
1487 memset (meta, 0, sizeof (notification_meta_t));
1489 sstrncpy (meta->name, name, sizeof (meta->name));
1490 meta->type = type;
1492 switch (type)
1493 {
1494 case NM_TYPE_STRING:
1495 {
1496 meta->nm_value.nm_string = strdup ((const char *) value);
1497 if (meta->nm_value.nm_string == NULL)
1498 {
1499 ERROR ("plugin_notification_meta_add: strdup failed.");
1500 sfree (meta);
1501 return (-1);
1502 }
1503 break;
1504 }
1505 case NM_TYPE_SIGNED_INT:
1506 {
1507 meta->nm_value.nm_signed_int = *((int64_t *) value);
1508 break;
1509 }
1510 case NM_TYPE_UNSIGNED_INT:
1511 {
1512 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1513 break;
1514 }
1515 case NM_TYPE_DOUBLE:
1516 {
1517 meta->nm_value.nm_double = *((double *) value);
1518 break;
1519 }
1520 case NM_TYPE_BOOLEAN:
1521 {
1522 meta->nm_value.nm_boolean = *((bool *) value);
1523 break;
1524 }
1525 default:
1526 {
1527 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1528 sfree (meta);
1529 return (-1);
1530 }
1531 } /* switch (type) */
1533 meta->next = NULL;
1534 tail = n->meta;
1535 while ((tail != NULL) && (tail->next != NULL))
1536 tail = tail->next;
1538 if (tail == NULL)
1539 n->meta = meta;
1540 else
1541 tail->next = meta;
1543 return (0);
1544 } /* int plugin_notification_meta_add */
1546 int plugin_notification_meta_add_string (notification_t *n,
1547 const char *name,
1548 const char *value)
1549 {
1550 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1551 }
1553 int plugin_notification_meta_add_signed_int (notification_t *n,
1554 const char *name,
1555 int64_t value)
1556 {
1557 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1558 }
1560 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1561 const char *name,
1562 uint64_t value)
1563 {
1564 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1565 }
1567 int plugin_notification_meta_add_double (notification_t *n,
1568 const char *name,
1569 double value)
1570 {
1571 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1572 }
1574 int plugin_notification_meta_add_boolean (notification_t *n,
1575 const char *name,
1576 bool value)
1577 {
1578 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1579 }
1581 int plugin_notification_meta_copy (notification_t *dst,
1582 const notification_t *src)
1583 {
1584 notification_meta_t *meta;
1586 assert (dst != NULL);
1587 assert (src != NULL);
1588 assert (dst != src);
1589 assert ((src->meta == NULL) || (src->meta != dst->meta));
1591 for (meta = src->meta; meta != NULL; meta = meta->next)
1592 {
1593 if (meta->type == NM_TYPE_STRING)
1594 plugin_notification_meta_add_string (dst, meta->name,
1595 meta->nm_value.nm_string);
1596 else if (meta->type == NM_TYPE_SIGNED_INT)
1597 plugin_notification_meta_add_signed_int (dst, meta->name,
1598 meta->nm_value.nm_signed_int);
1599 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1600 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1601 meta->nm_value.nm_unsigned_int);
1602 else if (meta->type == NM_TYPE_DOUBLE)
1603 plugin_notification_meta_add_double (dst, meta->name,
1604 meta->nm_value.nm_double);
1605 else if (meta->type == NM_TYPE_BOOLEAN)
1606 plugin_notification_meta_add_boolean (dst, meta->name,
1607 meta->nm_value.nm_boolean);
1608 }
1610 return (0);
1611 } /* int plugin_notification_meta_copy */
1613 int plugin_notification_meta_free (notification_meta_t *n)
1614 {
1615 notification_meta_t *this;
1616 notification_meta_t *next;
1618 if (n == NULL)
1619 {
1620 ERROR ("plugin_notification_meta_free: n == NULL!");
1621 return (-1);
1622 }
1624 this = n;
1625 while (this != NULL)
1626 {
1627 next = this->next;
1629 if (this->type == NM_TYPE_STRING)
1630 {
1631 free ((char *)this->nm_value.nm_string);
1632 this->nm_value.nm_string = NULL;
1633 }
1634 sfree (this);
1636 this = next;
1637 }
1639 return (0);
1640 } /* int plugin_notification_meta_free */
1642 /* vim: set sw=8 ts=8 noet fdm=marker : */