dfab52c3e4fcb68f15eae50e55aa76095fe7d1f9
1 /**
2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2008 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"
25 #include <ltdl.h>
27 #if HAVE_PTHREAD_H
28 # include <pthread.h>
29 #endif
31 #include "common.h"
32 #include "plugin.h"
33 #include "configfile.h"
34 #include "utils_avltree.h"
35 #include "utils_llist.h"
36 #include "utils_cache.h"
37 #include "utils_threshold.h"
39 /*
40 * Private structures
41 */
42 struct read_func_s
43 {
44 int wait_time;
45 int wait_left;
46 int (*callback) (void);
47 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
48 };
49 typedef struct read_func_s read_func_t;
51 /*
52 * Private variables
53 */
54 static llist_t *list_init;
55 static llist_t *list_read;
56 static llist_t *list_write;
57 static llist_t *list_flush;
58 static llist_t *list_shutdown;
59 static llist_t *list_log;
60 static llist_t *list_notification;
62 static c_avl_tree_t *data_sets;
64 static char *plugindir = NULL;
66 static int read_loop = 1;
67 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
68 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
69 static pthread_t *read_threads = NULL;
70 static int read_threads_num = 0;
72 /*
73 * Static functions
74 */
75 static const char *plugin_get_dir (void)
76 {
77 if (plugindir == NULL)
78 return (PLUGINDIR);
79 else
80 return (plugindir);
81 }
83 static int register_callback (llist_t **list, const char *name, void *callback)
84 {
85 llentry_t *le;
86 char *key;
88 if ((*list == NULL)
89 && ((*list = llist_create ()) == NULL))
90 return (-1);
92 le = llist_search (*list, name);
93 if (le == NULL)
94 {
95 key = strdup (name);
96 if (key == NULL)
97 return (-1);
99 le = llentry_create (key, callback);
100 if (le == NULL)
101 {
102 free (key);
103 return (-1);
104 }
106 llist_append (*list, le);
107 }
108 else
109 {
110 le->value = callback;
111 }
113 return (0);
114 } /* int register_callback */
116 static int plugin_unregister (llist_t *list, const char *name)
117 {
118 llentry_t *e;
120 e = llist_search (list, name);
122 if (e == NULL)
123 return (-1);
125 llist_remove (list, e);
126 free (e->key);
127 llentry_destroy (e);
129 return (0);
130 } /* int plugin_unregister */
132 /*
133 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
134 * object, but it will bitch about a shared object not having a
135 * ``module_register'' symbol..
136 */
137 static int plugin_load_file (char *file)
138 {
139 lt_dlhandle dlh;
140 void (*reg_handle) (void);
142 DEBUG ("file = %s", file);
144 lt_dlinit ();
145 lt_dlerror (); /* clear errors */
147 if ((dlh = lt_dlopen (file)) == NULL)
148 {
149 const char *error = lt_dlerror ();
151 ERROR ("lt_dlopen failed: %s", error);
152 fprintf (stderr, "lt_dlopen failed: %s\n", error);
153 return (1);
154 }
156 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
157 {
158 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
159 file, lt_dlerror ());
160 lt_dlclose (dlh);
161 return (-1);
162 }
164 (*reg_handle) ();
166 return (0);
167 }
169 static void *plugin_read_thread (void *args)
170 {
171 llentry_t *le;
172 read_func_t *rf;
173 int status;
174 int done;
176 pthread_mutex_lock (&read_lock);
178 while (read_loop != 0)
179 {
180 le = llist_head (list_read);
181 done = 0;
183 while ((read_loop != 0) && (le != NULL))
184 {
185 rf = (read_func_t *) le->value;
187 if (rf->needs_read != TODO)
188 {
189 le = le->next;
190 continue;
191 }
193 /* We will do this read function */
194 rf->needs_read = ACTIVE;
196 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
197 (unsigned long int) pthread_self (), le->key);
198 pthread_mutex_unlock (&read_lock);
200 status = rf->callback ();
201 done++;
203 if (status != 0)
204 {
205 if (rf->wait_time < interval_g)
206 rf->wait_time = interval_g;
207 rf->wait_left = rf->wait_time;
208 rf->wait_time = rf->wait_time * 2;
209 if (rf->wait_time > 86400)
210 rf->wait_time = 86400;
212 NOTICE ("read-function of plugin `%s' "
213 "failed. Will suspend it for %i "
214 "seconds.", le->key, rf->wait_left);
215 }
216 else
217 {
218 rf->wait_left = 0;
219 rf->wait_time = interval_g;
220 }
222 pthread_mutex_lock (&read_lock);
224 rf->needs_read = DONE;
225 le = le->next;
226 } /* while (le != NULL) */
228 if ((read_loop != 0) && (done == 0))
229 {
230 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
231 (unsigned long int) pthread_self ());
232 pthread_cond_wait (&read_cond, &read_lock);
233 }
234 } /* while (read_loop) */
236 pthread_mutex_unlock (&read_lock);
238 pthread_exit (NULL);
239 return ((void *) 0);
240 } /* void *plugin_read_thread */
242 static void start_threads (int num)
243 {
244 int i;
246 if (read_threads != NULL)
247 return;
249 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
250 if (read_threads == NULL)
251 {
252 ERROR ("plugin: start_threads: calloc failed.");
253 return;
254 }
256 read_threads_num = 0;
257 for (i = 0; i < num; i++)
258 {
259 if (pthread_create (read_threads + read_threads_num, NULL,
260 plugin_read_thread, NULL) == 0)
261 {
262 read_threads_num++;
263 }
264 else
265 {
266 ERROR ("plugin: start_threads: pthread_create failed.");
267 return;
268 }
269 } /* for (i) */
270 } /* void start_threads */
272 static void stop_threads (void)
273 {
274 int i;
276 pthread_mutex_lock (&read_lock);
277 read_loop = 0;
278 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
279 pthread_cond_broadcast (&read_cond);
280 pthread_mutex_unlock (&read_lock);
282 for (i = 0; i < read_threads_num; i++)
283 {
284 if (pthread_join (read_threads[i], NULL) != 0)
285 {
286 ERROR ("plugin: stop_threads: pthread_join failed.");
287 }
288 read_threads[i] = (pthread_t) 0;
289 }
290 sfree (read_threads);
291 read_threads_num = 0;
292 } /* void stop_threads */
294 /*
295 * Public functions
296 */
297 void plugin_set_dir (const char *dir)
298 {
299 if (plugindir != NULL)
300 free (plugindir);
302 if (dir == NULL)
303 plugindir = NULL;
304 else if ((plugindir = strdup (dir)) == NULL)
305 {
306 char errbuf[1024];
307 ERROR ("strdup failed: %s",
308 sstrerror (errno, errbuf, sizeof (errbuf)));
309 }
310 }
312 #define BUFSIZE 512
313 int plugin_load (const char *type)
314 {
315 DIR *dh;
316 const char *dir;
317 char filename[BUFSIZE] = "";
318 char typename[BUFSIZE];
319 int typename_len;
320 int ret;
321 struct stat statbuf;
322 struct dirent *de;
324 DEBUG ("type = %s", type);
326 dir = plugin_get_dir ();
327 ret = 1;
329 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
330 * type when matching the filename */
331 if (ssnprintf (typename, sizeof (typename),
332 "%s.so", type) >= sizeof (typename))
333 {
334 WARNING ("snprintf: truncated: `%s.so'", type);
335 return (-1);
336 }
337 typename_len = strlen (typename);
339 if ((dh = opendir (dir)) == NULL)
340 {
341 char errbuf[1024];
342 ERROR ("opendir (%s): %s", dir,
343 sstrerror (errno, errbuf, sizeof (errbuf)));
344 return (-1);
345 }
347 while ((de = readdir (dh)) != NULL)
348 {
349 if (strncasecmp (de->d_name, typename, typename_len))
350 continue;
352 if (ssnprintf (filename, sizeof (filename),
353 "%s/%s", dir, de->d_name) >= sizeof (filename))
354 {
355 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
356 continue;
357 }
359 if (lstat (filename, &statbuf) == -1)
360 {
361 char errbuf[1024];
362 WARNING ("stat %s: %s", filename,
363 sstrerror (errno, errbuf, sizeof (errbuf)));
364 continue;
365 }
366 else if (!S_ISREG (statbuf.st_mode))
367 {
368 /* don't follow symlinks */
369 continue;
370 }
372 if (plugin_load_file (filename) == 0)
373 {
374 /* success */
375 ret = 0;
376 break;
377 }
378 else
379 {
380 fprintf (stderr, "Unable to load plugin %s.\n", type);
381 }
382 }
384 closedir (dh);
386 if (filename[0] == '\0')
387 fprintf (stderr, "Could not find plugin %s.\n", type);
389 return (ret);
390 }
392 /*
393 * The `register_*' functions follow
394 */
395 int plugin_register_config (const char *name,
396 int (*callback) (const char *key, const char *val),
397 const char **keys, int keys_num)
398 {
399 cf_register (name, callback, keys, keys_num);
400 return (0);
401 } /* int plugin_register_config */
403 int plugin_register_complex_config (const char *type,
404 int (*callback) (oconfig_item_t *))
405 {
406 return (cf_register_complex (type, callback));
407 } /* int plugin_register_complex_config */
409 int plugin_register_init (const char *name,
410 int (*callback) (void))
411 {
412 return (register_callback (&list_init, name, (void *) callback));
413 } /* plugin_register_init */
415 int plugin_register_read (const char *name,
416 int (*callback) (void))
417 {
418 read_func_t *rf;
420 rf = (read_func_t *) malloc (sizeof (read_func_t));
421 if (rf == NULL)
422 {
423 char errbuf[1024];
424 ERROR ("plugin_register_read: malloc failed: %s",
425 sstrerror (errno, errbuf, sizeof (errbuf)));
426 return (-1);
427 }
429 memset (rf, '\0', sizeof (read_func_t));
430 rf->wait_time = interval_g;
431 rf->wait_left = 0;
432 rf->callback = callback;
433 rf->needs_read = DONE;
435 return (register_callback (&list_read, name, (void *) rf));
436 } /* int plugin_register_read */
438 int plugin_register_write (const char *name,
439 int (*callback) (const data_set_t *ds, const value_list_t *vl))
440 {
441 return (register_callback (&list_write, name, (void *) callback));
442 } /* int plugin_register_write */
444 int plugin_register_flush (const char *name, int (*callback) (const int))
445 {
446 return (register_callback (&list_flush, name, (void *) callback));
447 } /* int plugin_register_flush */
449 int plugin_register_shutdown (char *name,
450 int (*callback) (void))
451 {
452 return (register_callback (&list_shutdown, name, (void *) callback));
453 } /* int plugin_register_shutdown */
455 int plugin_register_data_set (const data_set_t *ds)
456 {
457 data_set_t *ds_copy;
458 int i;
460 if ((data_sets != NULL)
461 && (c_avl_get (data_sets, ds->type, NULL) == 0))
462 {
463 NOTICE ("Replacing DS `%s' with another version.", ds->type);
464 plugin_unregister_data_set (ds->type);
465 }
466 else if (data_sets == NULL)
467 {
468 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
469 if (data_sets == NULL)
470 return (-1);
471 }
473 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
474 if (ds_copy == NULL)
475 return (-1);
476 memcpy(ds_copy, ds, sizeof (data_set_t));
478 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
479 * ds->ds_num);
480 if (ds_copy->ds == NULL)
481 {
482 free (ds_copy);
483 return (-1);
484 }
486 for (i = 0; i < ds->ds_num; i++)
487 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
489 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
490 } /* int plugin_register_data_set */
492 int plugin_register_log (char *name,
493 void (*callback) (int priority, const char *msg))
494 {
495 return (register_callback (&list_log, name, (void *) callback));
496 } /* int plugin_register_log */
498 int plugin_register_notification (const char *name,
499 int (*callback) (const notification_t *notif))
500 {
501 return (register_callback (&list_notification, name, (void *) callback));
502 } /* int plugin_register_log */
504 int plugin_unregister_config (const char *name)
505 {
506 cf_unregister (name);
507 return (0);
508 } /* int plugin_unregister_config */
510 int plugin_unregister_complex_config (const char *name)
511 {
512 cf_unregister_complex (name);
513 return (0);
514 } /* int plugin_unregister_complex_config */
516 int plugin_unregister_init (const char *name)
517 {
518 return (plugin_unregister (list_init, name));
519 }
521 int plugin_unregister_read (const char *name)
522 {
523 llentry_t *e;
525 e = llist_search (list_read, name);
527 if (e == NULL)
528 return (-1);
530 llist_remove (list_read, e);
531 free (e->value);
532 free (e->key);
533 llentry_destroy (e);
535 return (0);
536 }
538 int plugin_unregister_write (const char *name)
539 {
540 return (plugin_unregister (list_write, name));
541 }
543 int plugin_unregister_flush (const char *name)
544 {
545 return (plugin_unregister (list_flush, name));
546 }
548 int plugin_unregister_shutdown (const char *name)
549 {
550 return (plugin_unregister (list_shutdown, name));
551 }
553 int plugin_unregister_data_set (const char *name)
554 {
555 data_set_t *ds;
557 if (data_sets == NULL)
558 return (-1);
560 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
561 return (-1);
563 sfree (ds->ds);
564 sfree (ds);
566 return (0);
567 } /* int plugin_unregister_data_set */
569 int plugin_unregister_log (const char *name)
570 {
571 return (plugin_unregister (list_log, name));
572 }
574 int plugin_unregister_notification (const char *name)
575 {
576 return (plugin_unregister (list_notification, name));
577 }
579 void plugin_init_all (void)
580 {
581 int (*callback) (void);
582 llentry_t *le;
583 int status;
585 /* Start read-threads */
586 if (list_read != NULL)
587 {
588 const char *rt;
589 int num;
590 rt = global_option_get ("ReadThreads");
591 num = atoi (rt);
592 start_threads ((num > 0) ? num : 5);
593 }
595 /* Init the value cache */
596 uc_init ();
598 if (list_init == NULL)
599 return;
601 le = llist_head (list_init);
602 while (le != NULL)
603 {
604 callback = (int (*) (void)) le->value;
605 status = (*callback) ();
607 if (status != 0)
608 {
609 ERROR ("Initialization of plugin `%s' "
610 "failed with status %i. "
611 "Plugin will be unloaded.",
612 le->key, status);
613 /* FIXME: Unload _all_ functions */
614 plugin_unregister_read (le->key);
615 }
617 le = le->next;
618 }
619 } /* void plugin_init_all */
621 void plugin_read_all (void)
622 {
623 llentry_t *le;
624 read_func_t *rf;
626 uc_check_timeout ();
628 if (list_read == NULL)
629 return;
631 pthread_mutex_lock (&read_lock);
633 le = llist_head (list_read);
634 while (le != NULL)
635 {
636 rf = (read_func_t *) le->value;
638 if (rf->needs_read != DONE)
639 {
640 le = le->next;
641 continue;
642 }
644 if (rf->wait_left > 0)
645 rf->wait_left -= interval_g;
647 if (rf->wait_left <= 0)
648 {
649 rf->needs_read = TODO;
650 }
652 le = le->next;
653 }
655 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
656 pthread_cond_broadcast (&read_cond);
657 pthread_mutex_unlock (&read_lock);
658 } /* void plugin_read_all */
660 int plugin_flush_one (int timeout, const char *name)
661 {
662 int (*callback) (int);
663 llentry_t *le;
664 int status;
666 if (list_flush == NULL)
667 return (-1);
669 le = llist_search (list_flush, name);
670 if (le == NULL)
671 return (-1);
672 callback = (int (*) (int)) le->value;
674 status = (*callback) (timeout);
676 return (status);
677 } /* int plugin_flush_ont */
679 void plugin_flush_all (int timeout)
680 {
681 int (*callback) (int);
682 llentry_t *le;
684 if (list_flush == NULL)
685 return;
687 le = llist_head (list_flush);
688 while (le != NULL)
689 {
690 callback = (int (*) (int)) le->value;
691 le = le->next;
693 (*callback) (timeout);
694 }
695 } /* void plugin_flush_all */
697 void plugin_shutdown_all (void)
698 {
699 int (*callback) (void);
700 llentry_t *le;
702 stop_threads ();
704 if (list_shutdown == NULL)
705 return;
707 le = llist_head (list_shutdown);
708 while (le != NULL)
709 {
710 callback = (int (*) (void)) le->value;
712 /* Advance the pointer before calling the callback allows
713 * shutdown functions to unregister themselves. If done the
714 * other way around the memory `le' points to will be freed
715 * after callback returns. */
716 le = le->next;
718 (*callback) ();
719 }
720 } /* void plugin_shutdown_all */
722 int plugin_dispatch_values (value_list_t *vl)
723 {
724 int (*callback) (const data_set_t *, const value_list_t *);
725 data_set_t *ds;
726 llentry_t *le;
728 if ((vl == NULL) || (*vl->type == '\0')) {
729 ERROR ("plugin_dispatch_values: Invalid value list.");
730 return (-1);
731 }
733 if (list_write == NULL)
734 {
735 ERROR ("plugin_dispatch_values: No write callback has been "
736 "registered. Please load at least one plugin "
737 "that provides a write function.");
738 return (-1);
739 }
741 if (data_sets == NULL)
742 {
743 ERROR ("plugin_dispatch_values: No data sets registered. "
744 "Could the types database be read? Check "
745 "your `TypesDB' setting!");
746 return (-1);
747 }
749 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
750 {
751 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
752 return (-1);
753 }
755 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
756 "host = %s; "
757 "plugin = %s; plugin_instance = %s; "
758 "type = %s; type_instance = %s;",
759 (unsigned int) vl->time, vl->interval,
760 vl->host,
761 vl->plugin, vl->plugin_instance,
762 vl->type, vl->type_instance);
764 #if COLLECT_DEBUG
765 assert (0 == strcmp (ds->type, vl->type));
766 #else
767 if (0 != strcmp (ds->type, vl->type))
768 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
769 ds->type, vl->type);
770 #endif
772 #if COLLECT_DEBUG
773 assert (ds->ds_num == vl->values_len);
774 #else
775 if (ds->ds_num != vl->values_len)
776 {
777 ERROR ("plugin_dispatch_values: ds->type = %s: "
778 "(ds->ds_num = %i) != "
779 "(vl->values_len = %i)",
780 ds->type, ds->ds_num, vl->values_len);
781 return (-1);
782 }
783 #endif
785 escape_slashes (vl->host, sizeof (vl->host));
786 escape_slashes (vl->plugin, sizeof (vl->plugin));
787 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
788 escape_slashes (vl->type, sizeof (vl->type));
789 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
791 /* Update the value cache */
792 uc_update (ds, vl);
793 ut_check_threshold (ds, vl);
795 le = llist_head (list_write);
796 while (le != NULL)
797 {
798 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
799 (*callback) (ds, vl);
801 le = le->next;
802 }
804 return (0);
805 } /* int plugin_dispatch_values */
807 int plugin_dispatch_notification (const notification_t *notif)
808 {
809 int (*callback) (const notification_t *);
810 llentry_t *le;
811 /* Possible TODO: Add flap detection here */
813 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
814 "time = %u; host = %s;",
815 notif->severity, notif->message,
816 (unsigned int) notif->time, notif->host);
818 /* Nobody cares for notifications */
819 if (list_notification == NULL)
820 return (-1);
822 le = llist_head (list_notification);
823 while (le != NULL)
824 {
825 callback = (int (*) (const notification_t *)) le->value;
826 (*callback) (notif);
828 le = le->next;
829 }
831 return (0);
832 } /* int plugin_dispatch_notification */
834 void plugin_log (int level, const char *format, ...)
835 {
836 char msg[512];
837 va_list ap;
839 void (*callback) (int, const char *);
840 llentry_t *le;
842 if (list_log == NULL)
843 return;
845 #if !COLLECT_DEBUG
846 if (level >= LOG_DEBUG)
847 return;
848 #endif
850 va_start (ap, format);
851 vsnprintf (msg, 512, format, ap);
852 msg[511] = '\0';
853 va_end (ap);
855 le = llist_head (list_log);
856 while (le != NULL)
857 {
858 callback = (void (*) (int, const char *)) le->value;
859 (*callback) (level, msg);
861 le = le->next;
862 }
863 } /* void plugin_log */
865 const data_set_t *plugin_get_ds (const char *name)
866 {
867 data_set_t *ds;
869 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
870 {
871 DEBUG ("No such dataset registered: %s", name);
872 return (NULL);
873 }
875 return (ds);
876 } /* data_set_t *plugin_get_ds */