1 /**
2 * collectd - src/plugin.c
3 * Copyright (C) 2005,2006 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 **/
22 #include "collectd.h"
24 #include <ltdl.h>
26 #if HAVE_PTHREAD_H
27 # include <pthread.h>
28 #endif
30 #include "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
33 #include "utils_llist.h"
34 #include "utils_cache.h"
36 /*
37 * Private structures
38 */
39 struct read_func_s
40 {
41 int wait_time;
42 int wait_left;
43 int (*callback) (void);
44 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
45 };
46 typedef struct read_func_s read_func_t;
48 /*
49 * Private variables
50 */
51 static llist_t *list_init;
52 static llist_t *list_read;
53 static llist_t *list_write;
54 static llist_t *list_shutdown;
55 static llist_t *list_data_set;
56 static llist_t *list_log;
57 static llist_t *list_notification;
59 static char *plugindir = NULL;
61 static int read_loop = 1;
62 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
63 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
64 static pthread_t *read_threads = NULL;
65 static int read_threads_num = 0;
67 /*
68 * Static functions
69 */
70 static const char *plugin_get_dir (void)
71 {
72 if (plugindir == NULL)
73 return (PLUGINDIR);
74 else
75 return (plugindir);
76 }
78 static int register_callback (llist_t **list, const char *name, void *callback)
79 {
80 llentry_t *le;
82 if ((*list == NULL)
83 && ((*list = llist_create ()) == NULL))
84 return (-1);
86 le = llist_search (*list, name);
87 if (le == NULL)
88 {
89 le = llentry_create (name, callback);
90 if (le == NULL)
91 return (-1);
93 llist_append (*list, le);
94 }
95 else
96 {
97 le->value = callback;
98 }
100 return (0);
101 } /* int register_callback */
103 static int plugin_unregister (llist_t *list, const char *name)
104 {
105 llentry_t *e;
107 e = llist_search (list, name);
109 if (e == NULL)
110 return (-1);
112 llist_remove (list, e);
113 llentry_destroy (e);
115 return (0);
116 } /* int plugin_unregister */
118 /*
119 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
120 * object, but it will bitch about a shared object not having a
121 * ``module_register'' symbol..
122 */
123 static int plugin_load_file (char *file)
124 {
125 lt_dlhandle dlh;
126 void (*reg_handle) (void);
128 DEBUG ("file = %s", file);
130 lt_dlinit ();
131 lt_dlerror (); /* clear errors */
133 if ((dlh = lt_dlopen (file)) == NULL)
134 {
135 const char *error = lt_dlerror ();
137 ERROR ("lt_dlopen failed: %s", error);
138 fprintf (stderr, "lt_dlopen failed: %s\n", error);
139 return (1);
140 }
142 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
143 {
144 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
145 file, lt_dlerror ());
146 lt_dlclose (dlh);
147 return (-1);
148 }
150 (*reg_handle) ();
152 return (0);
153 }
155 static void *plugin_read_thread (void *args)
156 {
157 llentry_t *le;
158 read_func_t *rf;
159 int status;
160 int done;
162 pthread_mutex_lock (&read_lock);
164 while (read_loop != 0)
165 {
166 le = llist_head (list_read);
167 done = 0;
169 while ((read_loop != 0) && (le != NULL))
170 {
171 rf = (read_func_t *) le->value;
173 if (rf->needs_read != TODO)
174 {
175 le = le->next;
176 continue;
177 }
179 /* We will do this read function */
180 rf->needs_read = ACTIVE;
182 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
183 (unsigned long int) pthread_self (), le->key);
184 pthread_mutex_unlock (&read_lock);
186 status = rf->callback ();
187 done++;
189 if (status != 0)
190 {
191 if (rf->wait_time < interval_g)
192 rf->wait_time = interval_g;
193 rf->wait_left = rf->wait_time;
194 rf->wait_time = rf->wait_time * 2;
195 if (rf->wait_time > 86400)
196 rf->wait_time = 86400;
198 NOTICE ("read-function of plugin `%s' "
199 "failed. Will suspend it for %i "
200 "seconds.", le->key, rf->wait_left);
201 }
202 else
203 {
204 rf->wait_left = 0;
205 rf->wait_time = interval_g;
206 }
208 pthread_mutex_lock (&read_lock);
210 rf->needs_read = DONE;
211 le = le->next;
212 } /* while (le != NULL) */
214 if ((read_loop != 0) && (done == 0))
215 {
216 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
217 (unsigned long int) pthread_self ());
218 pthread_cond_wait (&read_cond, &read_lock);
219 }
220 } /* while (read_loop) */
222 pthread_mutex_unlock (&read_lock);
224 pthread_exit (NULL);
225 } /* void *plugin_read_thread */
227 static void start_threads (int num)
228 {
229 int i;
231 if (read_threads != NULL)
232 return;
234 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
235 if (read_threads == NULL)
236 {
237 ERROR ("plugin: start_threads: calloc failed.");
238 return;
239 }
241 read_threads_num = 0;
242 for (i = 0; i < num; i++)
243 {
244 if (pthread_create (read_threads + read_threads_num, NULL,
245 plugin_read_thread, NULL) == 0)
246 {
247 read_threads_num++;
248 }
249 else
250 {
251 ERROR ("plugin: start_threads: pthread_create failed.");
252 return;
253 }
254 } /* for (i) */
255 } /* void start_threads */
257 static void stop_threads (void)
258 {
259 int i;
261 pthread_mutex_lock (&read_lock);
262 read_loop = 0;
263 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
264 pthread_cond_broadcast (&read_cond);
265 pthread_mutex_unlock (&read_lock);
267 for (i = 0; i < read_threads_num; i++)
268 {
269 if (pthread_join (read_threads[i], NULL) != 0)
270 {
271 ERROR ("plugin: stop_threads: pthread_join failed.");
272 }
273 read_threads[i] = (pthread_t) 0;
274 }
275 sfree (read_threads);
276 read_threads_num = 0;
277 } /* void stop_threads */
279 /*
280 * Public functions
281 */
282 void plugin_set_dir (const char *dir)
283 {
284 if (plugindir != NULL)
285 free (plugindir);
287 if (dir == NULL)
288 plugindir = NULL;
289 else if ((plugindir = strdup (dir)) == NULL)
290 {
291 char errbuf[1024];
292 ERROR ("strdup failed: %s",
293 sstrerror (errno, errbuf, sizeof (errbuf)));
294 }
295 }
297 #define BUFSIZE 512
298 int plugin_load (const char *type)
299 {
300 DIR *dh;
301 const char *dir;
302 char filename[BUFSIZE];
303 char typename[BUFSIZE];
304 int typename_len;
305 int ret;
306 struct stat statbuf;
307 struct dirent *de;
309 DEBUG ("type = %s", type);
311 dir = plugin_get_dir ();
312 ret = 1;
314 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
315 * type when matching the filename */
316 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
317 {
318 WARNING ("snprintf: truncated: `%s.so'", type);
319 return (-1);
320 }
321 typename_len = strlen (typename);
323 if ((dh = opendir (dir)) == NULL)
324 {
325 char errbuf[1024];
326 ERROR ("opendir (%s): %s", dir,
327 sstrerror (errno, errbuf, sizeof (errbuf)));
328 return (-1);
329 }
331 while ((de = readdir (dh)) != NULL)
332 {
333 if (strncasecmp (de->d_name, typename, typename_len))
334 continue;
336 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
337 {
338 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
339 continue;
340 }
342 if (lstat (filename, &statbuf) == -1)
343 {
344 char errbuf[1024];
345 WARNING ("stat %s: %s", filename,
346 sstrerror (errno, errbuf, sizeof (errbuf)));
347 continue;
348 }
349 else if (!S_ISREG (statbuf.st_mode))
350 {
351 /* don't follow symlinks */
352 continue;
353 }
355 if (plugin_load_file (filename) == 0)
356 {
357 /* success */
358 ret = 0;
359 break;
360 }
361 else
362 {
363 fprintf (stderr, "Unable to load plugin %s.\n", type);
364 }
365 }
367 closedir (dh);
369 return (ret);
370 }
372 /*
373 * The `register_*' functions follow
374 */
375 int plugin_register_config (const char *name,
376 int (*callback) (const char *key, const char *val),
377 const char **keys, int keys_num)
378 {
379 cf_register (name, callback, keys, keys_num);
380 return (0);
381 } /* int plugin_register_config */
383 int plugin_register_complex_config (const char *type,
384 int (*callback) (oconfig_item_t *))
385 {
386 return (cf_register_complex (type, callback));
387 } /* int plugin_register_complex_config */
389 int plugin_register_init (const char *name,
390 int (*callback) (void))
391 {
392 return (register_callback (&list_init, name, (void *) callback));
393 } /* plugin_register_init */
395 int plugin_register_read (const char *name,
396 int (*callback) (void))
397 {
398 read_func_t *rf;
400 rf = (read_func_t *) malloc (sizeof (read_func_t));
401 if (rf == NULL)
402 {
403 char errbuf[1024];
404 ERROR ("plugin_register_read: malloc failed: %s",
405 sstrerror (errno, errbuf, sizeof (errbuf)));
406 return (-1);
407 }
409 memset (rf, '\0', sizeof (read_func_t));
410 rf->wait_time = interval_g;
411 rf->wait_left = 0;
412 rf->callback = callback;
413 rf->needs_read = DONE;
415 return (register_callback (&list_read, name, (void *) rf));
416 } /* int plugin_register_read */
418 int plugin_register_write (const char *name,
419 int (*callback) (const data_set_t *ds, const value_list_t *vl))
420 {
421 return (register_callback (&list_write, name, (void *) callback));
422 } /* int plugin_register_write */
424 int plugin_register_shutdown (char *name,
425 int (*callback) (void))
426 {
427 return (register_callback (&list_shutdown, name, (void *) callback));
428 } /* int plugin_register_shutdown */
430 int plugin_register_data_set (const data_set_t *ds)
431 {
432 data_set_t *ds_copy;
433 int i;
435 if ((list_data_set != NULL)
436 && (llist_search (list_data_set, ds->type) != NULL))
437 {
438 NOTICE ("Replacing DS `%s' with another version.", ds->type);
439 plugin_unregister_data_set (ds->type);
440 }
442 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
443 if (ds_copy == NULL)
444 return (-1);
445 memcpy(ds_copy, ds, sizeof (data_set_t));
447 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
448 * ds->ds_num);
449 if (ds_copy->ds == NULL)
450 {
451 free (ds_copy);
452 return (-1);
453 }
455 for (i = 0; i < ds->ds_num; i++)
456 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
458 return (register_callback (&list_data_set, ds->type, (void *) ds_copy));
459 } /* int plugin_register_data_set */
461 int plugin_register_log (char *name,
462 void (*callback) (int priority, const char *msg))
463 {
464 return (register_callback (&list_log, name, (void *) callback));
465 } /* int plugin_register_log */
467 int plugin_register_notification (const char *name,
468 int (*callback) (const notification_t *notif))
469 {
470 return (register_callback (&list_log, name, (void *) callback));
471 } /* int plugin_register_log */
473 int plugin_unregister_config (const char *name)
474 {
475 cf_unregister (name);
476 return (0);
477 } /* int plugin_unregister_config */
479 int plugin_unregister_complex_config (const char *name)
480 {
481 cf_unregister_complex (name);
482 return (0);
483 } /* int plugin_unregister_complex_config */
485 int plugin_unregister_init (const char *name)
486 {
487 return (plugin_unregister (list_init, name));
488 }
490 int plugin_unregister_read (const char *name)
491 {
492 llentry_t *e;
494 e = llist_search (list_read, name);
496 if (e == NULL)
497 return (-1);
499 llist_remove (list_read, e);
500 free (e->value);
501 llentry_destroy (e);
503 return (0);
504 }
506 int plugin_unregister_write (const char *name)
507 {
508 return (plugin_unregister (list_write, name));
509 }
511 int plugin_unregister_shutdown (const char *name)
512 {
513 return (plugin_unregister (list_shutdown, name));
514 }
516 int plugin_unregister_data_set (const char *name)
517 {
518 llentry_t *e;
519 data_set_t *ds;
521 if (list_data_set == NULL)
522 return (-1);
524 e = llist_search (list_data_set, name);
526 if (e == NULL)
527 return (-1);
529 llist_remove (list_data_set, e);
530 ds = (data_set_t *) e->value;
531 llentry_destroy (e);
533 sfree (ds->ds);
534 sfree (ds);
536 return (0);
537 } /* int plugin_unregister_data_set */
539 int plugin_unregister_log (const char *name)
540 {
541 return (plugin_unregister (list_log, name));
542 }
544 int plugin_unregister_notification (const char *name)
545 {
546 return (plugin_unregister (list_notification, name));
547 }
549 void plugin_init_all (void)
550 {
551 int (*callback) (void);
552 llentry_t *le;
553 int status;
555 /* Start read-threads */
556 if (list_read != NULL)
557 {
558 const char *rt;
559 int num;
560 rt = global_option_get ("ReadThreads");
561 num = atoi (rt);
562 start_threads ((num > 0) ? num : 5);
563 }
565 /* Init the value cache */
566 uc_init ();
568 if (list_init == NULL)
569 return;
571 le = llist_head (list_init);
572 while (le != NULL)
573 {
574 callback = (int (*) (void)) le->value;
575 status = (*callback) ();
577 if (status != 0)
578 {
579 ERROR ("Initialization of plugin `%s' "
580 "failed with status %i. "
581 "Plugin will be unloaded.",
582 le->key, status);
583 /* FIXME: Unload _all_ functions */
584 plugin_unregister_read (le->key);
585 }
587 le = le->next;
588 }
589 } /* void plugin_init_all */
591 void plugin_read_all (const int *loop)
592 {
593 llentry_t *le;
594 read_func_t *rf;
596 if (list_read == NULL)
597 return;
599 pthread_mutex_lock (&read_lock);
601 le = llist_head (list_read);
602 while (le != NULL)
603 {
604 rf = (read_func_t *) le->value;
606 if (rf->needs_read != DONE)
607 {
608 le = le->next;
609 continue;
610 }
612 if (rf->wait_left > 0)
613 rf->wait_left -= interval_g;
615 if (rf->wait_left <= 0)
616 {
617 rf->needs_read = TODO;
618 }
620 le = le->next;
621 }
623 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
624 pthread_cond_broadcast (&read_cond);
625 pthread_mutex_unlock (&read_lock);
626 } /* void plugin_read_all */
628 void plugin_shutdown_all (void)
629 {
630 int (*callback) (void);
631 llentry_t *le;
633 stop_threads ();
635 if (list_shutdown == NULL)
636 return;
638 le = llist_head (list_shutdown);
639 while (le != NULL)
640 {
641 callback = (int (*) (void)) le->value;
643 /* Advance the pointer before calling the callback allows
644 * shutdown functions to unregister themselves. If done the
645 * other way around the memory `le' points to will be freed
646 * after callback returns. */
647 le = le->next;
649 (*callback) ();
650 }
651 } /* void plugin_shutdown_all */
653 int plugin_dispatch_values (const char *name, value_list_t *vl)
654 {
655 int (*callback) (const data_set_t *, const value_list_t *);
656 data_set_t *ds;
657 llentry_t *le;
659 if ((list_write == NULL) || (list_data_set == NULL))
660 return (-1);
662 le = llist_search (list_data_set, name);
663 if (le == NULL)
664 {
665 DEBUG ("No such dataset registered: %s", name);
666 return (-1);
667 }
669 ds = (data_set_t *) le->value;
671 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
672 "host = %s; "
673 "plugin = %s; plugin_instance = %s; "
674 "type = %s; type_instance = %s;",
675 (unsigned int) vl->time, vl->interval,
676 vl->host,
677 vl->plugin, vl->plugin_instance,
678 ds->type, vl->type_instance);
680 #if COLLECT_DEBUG
681 assert (ds->ds_num == vl->values_len);
682 #else
683 if (ds->ds_num != vl->values_len)
684 {
685 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
686 "(vl->values_len = %i)",
687 ds->type, ds->ds_num, vl->values_len);
688 return (-1);
689 }
690 #endif
692 escape_slashes (vl->host, sizeof (vl->host));
693 escape_slashes (vl->plugin, sizeof (vl->plugin));
694 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
695 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
697 /* Update the value cache */
698 uc_update (ds, vl);
700 le = llist_head (list_write);
701 while (le != NULL)
702 {
703 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
704 (*callback) (ds, vl);
706 le = le->next;
707 }
709 return (0);
710 } /* int plugin_dispatch_values */
712 int plugin_dispatch_notification (const notification_t *notif)
713 {
714 int (*callback) (const notification_t *);
715 llentry_t *le;
716 /* Possible TODO: Add flap detection here */
718 /* Nobody cares for notifications */
719 if (list_notification == NULL)
720 return (-1);
722 le = llist_head (list_notification);
723 while (le != NULL)
724 {
725 callback = (int (*) (const notification_t *)) le->value;
726 (*callback) (notif);
728 le = le->next;
729 }
731 return (0);
732 } /* int plugin_dispatch_notification */
734 void plugin_log (int level, const char *format, ...)
735 {
736 char msg[512];
737 va_list ap;
739 void (*callback) (int, const char *);
740 llentry_t *le;
742 if (list_log == NULL)
743 return;
745 #if !COLLECT_DEBUG
746 if (level >= LOG_DEBUG)
747 return;
748 #endif
750 va_start (ap, format);
751 vsnprintf (msg, 512, format, ap);
752 msg[511] = '\0';
753 va_end (ap);
755 le = llist_head (list_log);
756 while (le != NULL)
757 {
758 callback = (void (*) (int, const char *)) le->value;
759 (*callback) (level, msg);
761 le = le->next;
762 }
763 } /* void plugin_log */
765 void plugin_complain (int level, complain_t *c, const char *format, ...)
766 {
767 char message[512];
768 va_list ap;
770 if (c->delay > 0)
771 {
772 c->delay--;
773 return;
774 }
776 if (c->interval < interval_g)
777 c->interval = interval_g;
778 else
779 c->interval *= 2;
781 if (c->interval > 86400)
782 c->interval = 86400;
784 c->delay = c->interval / interval_g;
786 va_start (ap, format);
787 vsnprintf (message, 512, format, ap);
788 message[511] = '\0';
789 va_end (ap);
791 plugin_log (level, message);
792 }
794 void plugin_relief (int level, complain_t *c, const char *format, ...)
795 {
796 char message[512];
797 va_list ap;
799 if (c->interval == 0)
800 return;
802 c->interval = 0;
804 va_start (ap, format);
805 vsnprintf (message, 512, format, ap);
806 message[511] = '\0';
807 va_end (ap);
809 plugin_log (level, message);
810 }
812 const data_set_t *plugin_get_ds (const char *name)
813 {
814 data_set_t *ds;
815 llentry_t *le;
817 le = llist_search (list_data_set, name);
818 if (le == NULL)
819 {
820 DEBUG ("No such dataset registered: %s", name);
821 return (NULL);
822 }
824 ds = (data_set_t *) le->value;
826 return (ds);
827 } /* data_set_t *plugin_get_ds */