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"
35 /*
36 * Private structures
37 */
38 struct read_func_s
39 {
40 int wait_time;
41 int wait_left;
42 int (*callback) (void);
43 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
44 };
45 typedef struct read_func_s read_func_t;
47 /*
48 * Private variables
49 */
50 static llist_t *list_init;
51 static llist_t *list_read;
52 static llist_t *list_write;
53 static llist_t *list_shutdown;
54 static llist_t *list_data_set;
55 static llist_t *list_log;
57 static char *plugindir = NULL;
59 static int read_loop = 1;
60 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
61 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
62 static pthread_t *read_threads = NULL;
63 static int read_threads_num = 0;
65 /*
66 * Static functions
67 */
68 static const char *plugin_get_dir (void)
69 {
70 if (plugindir == NULL)
71 return (PLUGINDIR);
72 else
73 return (plugindir);
74 }
76 static int register_callback (llist_t **list, const char *name, void *callback)
77 {
78 llentry_t *le;
80 if ((*list == NULL)
81 && ((*list = llist_create ()) == NULL))
82 return (-1);
84 le = llist_search (*list, name);
85 if (le == NULL)
86 {
87 le = llentry_create (name, callback);
88 if (le == NULL)
89 return (-1);
91 llist_append (*list, le);
92 }
93 else
94 {
95 le->value = callback;
96 }
98 return (0);
99 } /* int register_callback */
101 static int plugin_unregister (llist_t *list, const char *name)
102 {
103 llentry_t *e;
105 e = llist_search (list, name);
107 if (e == NULL)
108 return (-1);
110 llist_remove (list, e);
111 llentry_destroy (e);
113 return (0);
114 } /* int plugin_unregister */
116 /*
117 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
118 * object, but it will bitch about a shared object not having a
119 * ``module_register'' symbol..
120 */
121 static int plugin_load_file (char *file)
122 {
123 lt_dlhandle dlh;
124 void (*reg_handle) (void);
126 DEBUG ("file = %s", file);
128 lt_dlinit ();
129 lt_dlerror (); /* clear errors */
131 if ((dlh = lt_dlopen (file)) == NULL)
132 {
133 const char *error = lt_dlerror ();
135 ERROR ("lt_dlopen failed: %s", error);
136 fprintf (stderr, "lt_dlopen failed: %s\n", error);
137 return (1);
138 }
140 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
141 {
142 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
143 file, lt_dlerror ());
144 lt_dlclose (dlh);
145 return (-1);
146 }
148 (*reg_handle) ();
150 return (0);
151 }
153 static void *plugin_read_thread (void *args)
154 {
155 llentry_t *le;
156 read_func_t *rf;
157 int status;
158 int done;
160 pthread_mutex_lock (&read_lock);
162 while (read_loop != 0)
163 {
164 le = llist_head (list_read);
165 done = 0;
167 while ((read_loop != 0) && (le != NULL))
168 {
169 rf = (read_func_t *) le->value;
171 if (rf->needs_read != TODO)
172 {
173 le = le->next;
174 continue;
175 }
177 /* We will do this read function */
178 rf->needs_read = ACTIVE;
180 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
181 (unsigned long int) pthread_self (), le->key);
182 pthread_mutex_unlock (&read_lock);
184 status = rf->callback ();
185 done++;
187 if (status != 0)
188 {
189 if (rf->wait_time < interval_g)
190 rf->wait_time = interval_g;
191 rf->wait_left = rf->wait_time;
192 rf->wait_time = rf->wait_time * 2;
193 if (rf->wait_time > 86400)
194 rf->wait_time = 86400;
196 NOTICE ("read-function of plugin `%s' "
197 "failed. Will suspend it for %i "
198 "seconds.", le->key, rf->wait_left);
199 }
200 else
201 {
202 rf->wait_left = 0;
203 rf->wait_time = interval_g;
204 }
206 pthread_mutex_lock (&read_lock);
208 rf->needs_read = DONE;
209 le = le->next;
210 } /* while (le != NULL) */
212 if ((read_loop != 0) && (done == 0))
213 {
214 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
215 (unsigned long int) pthread_self ());
216 pthread_cond_wait (&read_cond, &read_lock);
217 }
218 } /* while (read_loop) */
220 pthread_mutex_unlock (&read_lock);
222 pthread_exit (NULL);
223 } /* void *plugin_read_thread */
225 static void start_threads (int num)
226 {
227 int i;
229 if (read_threads != NULL)
230 return;
232 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
233 if (read_threads == NULL)
234 {
235 ERROR ("plugin: start_threads: calloc failed.");
236 return;
237 }
239 read_threads_num = 0;
240 for (i = 0; i < num; i++)
241 {
242 if (pthread_create (read_threads + read_threads_num, NULL,
243 plugin_read_thread, NULL) == 0)
244 {
245 read_threads_num++;
246 }
247 else
248 {
249 ERROR ("plugin: start_threads: pthread_create failed.");
250 return;
251 }
252 } /* for (i) */
253 } /* void start_threads */
255 static void stop_threads (void)
256 {
257 int i;
259 pthread_mutex_lock (&read_lock);
260 read_loop = 0;
261 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
262 pthread_cond_broadcast (&read_cond);
263 pthread_mutex_unlock (&read_lock);
265 for (i = 0; i < read_threads_num; i++)
266 {
267 if (pthread_join (read_threads[i], NULL) != 0)
268 {
269 ERROR ("plugin: stop_threads: pthread_join failed.");
270 }
271 read_threads[i] = (pthread_t) 0;
272 }
273 sfree (read_threads);
274 read_threads_num = 0;
275 } /* void stop_threads */
277 /*
278 * Public functions
279 */
280 void plugin_set_dir (const char *dir)
281 {
282 if (plugindir != NULL)
283 free (plugindir);
285 if (dir == NULL)
286 plugindir = NULL;
287 else if ((plugindir = strdup (dir)) == NULL)
288 {
289 char errbuf[1024];
290 ERROR ("strdup failed: %s",
291 sstrerror (errno, errbuf, sizeof (errbuf)));
292 }
293 }
295 #define BUFSIZE 512
296 int plugin_load (const char *type)
297 {
298 DIR *dh;
299 const char *dir;
300 char filename[BUFSIZE] = "";
301 char typename[BUFSIZE];
302 int typename_len;
303 int ret;
304 struct stat statbuf;
305 struct dirent *de;
307 DEBUG ("type = %s", type);
309 dir = plugin_get_dir ();
310 ret = 1;
312 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
313 * type when matching the filename */
314 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
315 {
316 WARNING ("snprintf: truncated: `%s.so'", type);
317 return (-1);
318 }
319 typename_len = strlen (typename);
321 if ((dh = opendir (dir)) == NULL)
322 {
323 char errbuf[1024];
324 ERROR ("opendir (%s): %s", dir,
325 sstrerror (errno, errbuf, sizeof (errbuf)));
326 return (-1);
327 }
329 while ((de = readdir (dh)) != NULL)
330 {
331 if (strncasecmp (de->d_name, typename, typename_len))
332 continue;
334 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
335 {
336 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
337 continue;
338 }
340 if (lstat (filename, &statbuf) == -1)
341 {
342 char errbuf[1024];
343 WARNING ("stat %s: %s", filename,
344 sstrerror (errno, errbuf, sizeof (errbuf)));
345 continue;
346 }
347 else if (!S_ISREG (statbuf.st_mode))
348 {
349 /* don't follow symlinks */
350 continue;
351 }
353 if (plugin_load_file (filename) == 0)
354 {
355 /* success */
356 ret = 0;
357 break;
358 }
359 else
360 {
361 fprintf (stderr, "Unable to load plugin %s.\n", type);
362 }
363 }
365 closedir (dh);
367 if (filename[0] == '\0')
368 fprintf (stderr, "Could not find plugin %s.\n", type);
370 return (ret);
371 }
373 /*
374 * The `register_*' functions follow
375 */
376 int plugin_register_config (const char *name,
377 int (*callback) (const char *key, const char *val),
378 const char **keys, int keys_num)
379 {
380 cf_register (name, callback, keys, keys_num);
381 return (0);
382 } /* int plugin_register_config */
384 int plugin_register_complex_config (const char *type,
385 int (*callback) (oconfig_item_t *))
386 {
387 return (cf_register_complex (type, callback));
388 } /* int plugin_register_complex_config */
390 int plugin_register_init (const char *name,
391 int (*callback) (void))
392 {
393 return (register_callback (&list_init, name, (void *) callback));
394 } /* plugin_register_init */
396 int plugin_register_read (const char *name,
397 int (*callback) (void))
398 {
399 read_func_t *rf;
401 rf = (read_func_t *) malloc (sizeof (read_func_t));
402 if (rf == NULL)
403 {
404 char errbuf[1024];
405 ERROR ("plugin_register_read: malloc failed: %s",
406 sstrerror (errno, errbuf, sizeof (errbuf)));
407 return (-1);
408 }
410 memset (rf, '\0', sizeof (read_func_t));
411 rf->wait_time = interval_g;
412 rf->wait_left = 0;
413 rf->callback = callback;
414 rf->needs_read = DONE;
416 return (register_callback (&list_read, name, (void *) rf));
417 } /* int plugin_register_read */
419 int plugin_register_write (const char *name,
420 int (*callback) (const data_set_t *ds, const value_list_t *vl))
421 {
422 return (register_callback (&list_write, name, (void *) callback));
423 } /* int plugin_register_write */
425 int plugin_register_shutdown (char *name,
426 int (*callback) (void))
427 {
428 return (register_callback (&list_shutdown, name, (void *) callback));
429 } /* int plugin_register_shutdown */
431 int plugin_register_data_set (const data_set_t *ds)
432 {
433 data_set_t *ds_copy;
434 int i;
436 if ((list_data_set != NULL)
437 && (llist_search (list_data_set, ds->type) != NULL))
438 {
439 NOTICE ("Replacing DS `%s' with another version.", ds->type);
440 plugin_unregister_data_set (ds->type);
441 }
443 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
444 if (ds_copy == NULL)
445 return (-1);
446 memcpy(ds_copy, ds, sizeof (data_set_t));
448 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
449 * ds->ds_num);
450 if (ds_copy->ds == NULL)
451 {
452 free (ds_copy);
453 return (-1);
454 }
456 for (i = 0; i < ds->ds_num; i++)
457 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
459 return (register_callback (&list_data_set, ds->type, (void *) ds_copy));
460 } /* int plugin_register_data_set */
462 int plugin_register_log (char *name,
463 void (*callback) (int priority, const char *msg))
464 {
465 return (register_callback (&list_log, name, (void *) callback));
466 } /* int plugin_register_log */
468 int plugin_unregister_config (const char *name)
469 {
470 cf_unregister (name);
471 return (0);
472 } /* int plugin_unregister_config */
474 int plugin_unregister_complex_config (const char *name)
475 {
476 cf_unregister_complex (name);
477 return (0);
478 } /* int plugin_unregister_complex_config */
480 int plugin_unregister_init (const char *name)
481 {
482 return (plugin_unregister (list_init, name));
483 }
485 int plugin_unregister_read (const char *name)
486 {
487 llentry_t *e;
489 e = llist_search (list_read, name);
491 if (e == NULL)
492 return (-1);
494 llist_remove (list_read, e);
495 free (e->value);
496 llentry_destroy (e);
498 return (0);
499 }
501 int plugin_unregister_write (const char *name)
502 {
503 return (plugin_unregister (list_write, name));
504 }
506 int plugin_unregister_shutdown (const char *name)
507 {
508 return (plugin_unregister (list_shutdown, name));
509 }
511 int plugin_unregister_data_set (const char *name)
512 {
513 llentry_t *e;
514 data_set_t *ds;
516 if (list_data_set == NULL)
517 return (-1);
519 e = llist_search (list_data_set, name);
521 if (e == NULL)
522 return (-1);
524 llist_remove (list_data_set, e);
525 ds = (data_set_t *) e->value;
526 llentry_destroy (e);
528 sfree (ds->ds);
529 sfree (ds);
531 return (0);
532 } /* int plugin_unregister_data_set */
534 int plugin_unregister_log (const char *name)
535 {
536 return (plugin_unregister (list_log, name));
537 }
539 void plugin_init_all (void)
540 {
541 int (*callback) (void);
542 llentry_t *le;
543 int status;
545 /* Start read-threads */
546 if (list_read != NULL)
547 {
548 const char *rt;
549 int num;
550 rt = global_option_get ("ReadThreads");
551 num = atoi (rt);
552 start_threads ((num > 0) ? num : 5);
553 }
555 if (list_init == NULL)
556 return;
558 le = llist_head (list_init);
559 while (le != NULL)
560 {
561 callback = (int (*) (void)) le->value;
562 status = (*callback) ();
564 if (status != 0)
565 {
566 ERROR ("Initialization of plugin `%s' "
567 "failed with status %i. "
568 "Plugin will be unloaded.",
569 le->key, status);
570 /* FIXME: Unload _all_ functions */
571 plugin_unregister_read (le->key);
572 }
574 le = le->next;
575 }
576 } /* void plugin_init_all */
578 void plugin_read_all (const int *loop)
579 {
580 llentry_t *le;
581 read_func_t *rf;
583 if (list_read == NULL)
584 return;
586 pthread_mutex_lock (&read_lock);
588 le = llist_head (list_read);
589 while (le != NULL)
590 {
591 rf = (read_func_t *) le->value;
593 if (rf->needs_read != DONE)
594 {
595 le = le->next;
596 continue;
597 }
599 if (rf->wait_left > 0)
600 rf->wait_left -= interval_g;
602 if (rf->wait_left <= 0)
603 {
604 rf->needs_read = TODO;
605 }
607 le = le->next;
608 }
610 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
611 pthread_cond_broadcast (&read_cond);
612 pthread_mutex_unlock (&read_lock);
613 } /* void plugin_read_all */
615 void plugin_shutdown_all (void)
616 {
617 int (*callback) (void);
618 llentry_t *le;
620 stop_threads ();
622 if (list_shutdown == NULL)
623 return;
625 le = llist_head (list_shutdown);
626 while (le != NULL)
627 {
628 callback = (int (*) (void)) le->value;
630 /* Advance the pointer before calling the callback allows
631 * shutdown functions to unregister themselves. If done the
632 * other way around the memory `le' points to will be freed
633 * after callback returns. */
634 le = le->next;
636 (*callback) ();
637 }
638 } /* void plugin_shutdown_all */
640 int plugin_dispatch_values (const char *name, value_list_t *vl)
641 {
642 int (*callback) (const data_set_t *, const value_list_t *);
643 data_set_t *ds;
644 llentry_t *le;
646 if (list_write == NULL)
647 {
648 ERROR ("plugin_dispatch_values: No write callback has been "
649 "registered. Please load at least one plugin "
650 "that provides a write function.");
651 return (-1);
652 }
654 if (list_data_set == NULL)
655 {
656 ERROR ("plugin_dispatch_values: No data sets registered. "
657 "Could the types database be read? Check "
658 "your `TypesDB' setting!");
659 return (-1);
660 }
662 le = llist_search (list_data_set, name);
663 if (le == NULL)
664 {
665 INFO ("plugin_dispatch_values: Dataset not found: %s", name);
666 return (-1);
667 }
669 ds = (data_set_t *) le->value;
671 DEBUG ("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_dispatch_values: ds->type = %s: "
686 "(ds->ds_num = %i) != "
687 "(vl->values_len = %i)",
688 ds->type, ds->ds_num, vl->values_len);
689 return (-1);
690 }
691 #endif
693 escape_slashes (vl->host, sizeof (vl->host));
694 escape_slashes (vl->plugin, sizeof (vl->plugin));
695 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
696 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
698 le = llist_head (list_write);
699 while (le != NULL)
700 {
701 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
702 (*callback) (ds, vl);
704 le = le->next;
705 }
707 return (0);
708 } /* int plugin_dispatch_values */
710 void plugin_log (int level, const char *format, ...)
711 {
712 char msg[512];
713 va_list ap;
715 void (*callback) (int, const char *);
716 llentry_t *le;
718 if (list_log == NULL)
719 return;
721 #if !COLLECT_DEBUG
722 if (level >= LOG_DEBUG)
723 return;
724 #endif
726 va_start (ap, format);
727 vsnprintf (msg, 512, format, ap);
728 msg[511] = '\0';
729 va_end (ap);
731 le = llist_head (list_log);
732 while (le != NULL)
733 {
734 callback = (void (*) (int, const char *)) le->value;
735 (*callback) (level, msg);
737 le = le->next;
738 }
739 } /* void plugin_log */
741 void plugin_complain (int level, complain_t *c, const char *format, ...)
742 {
743 char message[512];
744 va_list ap;
746 if (c->delay > 0)
747 {
748 c->delay--;
749 return;
750 }
752 if (c->interval < interval_g)
753 c->interval = interval_g;
754 else
755 c->interval *= 2;
757 if (c->interval > 86400)
758 c->interval = 86400;
760 c->delay = c->interval / interval_g;
762 va_start (ap, format);
763 vsnprintf (message, 512, format, ap);
764 message[511] = '\0';
765 va_end (ap);
767 plugin_log (level, message);
768 }
770 void plugin_relief (int level, complain_t *c, const char *format, ...)
771 {
772 char message[512];
773 va_list ap;
775 if (c->interval == 0)
776 return;
778 c->interval = 0;
780 va_start (ap, format);
781 vsnprintf (message, 512, format, ap);
782 message[511] = '\0';
783 va_end (ap);
785 plugin_log (level, message);
786 }
788 const data_set_t *plugin_get_ds (const char *name)
789 {
790 data_set_t *ds;
791 llentry_t *le;
793 le = llist_search (list_data_set, name);
794 if (le == NULL)
795 {
796 DEBUG ("No such dataset registered: %s", name);
797 return (NULL);
798 }
800 ds = (data_set_t *) le->value;
802 return (ds);
803 } /* data_set_t *plugin_get_ds */