2 * collectd - src/plugin.c
3 * Copyright (C) 2005,2006 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
32 #include "configfile.h"
33 #include "utils_llist.h"
34 #include "utils_cache.h"
43 int (*callback) (void);
44 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
46 typedef struct read_func_s read_func_t;
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;
58 static char *plugindir = NULL;
60 static int read_loop = 1;
61 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
62 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
63 static pthread_t *read_threads = NULL;
64 static int read_threads_num = 0;
69 static const char *plugin_get_dir (void)
71 if (plugindir == NULL)
77 static int register_callback (llist_t **list, const char *name, void *callback)
82 && ((*list = llist_create ()) == NULL))
85 le = llist_search (*list, name);
88 le = llentry_create (name, callback);
92 llist_append (*list, le);
100 } /* int register_callback */
102 static int plugin_unregister (llist_t *list, const char *name)
106 e = llist_search (list, name);
111 llist_remove (list, e);
115 } /* int plugin_unregister */
118 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
119 * object, but it will bitch about a shared object not having a
120 * ``module_register'' symbol..
122 static int plugin_load_file (char *file)
125 void (*reg_handle) (void);
127 DEBUG ("file = %s", file);
130 lt_dlerror (); /* clear errors */
132 if ((dlh = lt_dlopen (file)) == NULL)
134 const char *error = lt_dlerror ();
136 ERROR ("lt_dlopen failed: %s", error);
137 fprintf (stderr, "lt_dlopen failed: %s\n", error);
141 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
143 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
144 file, lt_dlerror ());
154 static void *plugin_read_thread (void *args)
161 pthread_mutex_lock (&read_lock);
163 while (read_loop != 0)
165 le = llist_head (list_read);
168 while ((read_loop != 0) && (le != NULL))
170 rf = (read_func_t *) le->value;
172 if (rf->needs_read != TODO)
178 /* We will do this read function */
179 rf->needs_read = ACTIVE;
181 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
182 (unsigned long int) pthread_self (), le->key);
183 pthread_mutex_unlock (&read_lock);
185 status = rf->callback ();
190 if (rf->wait_time < interval_g)
191 rf->wait_time = interval_g;
192 rf->wait_left = rf->wait_time;
193 rf->wait_time = rf->wait_time * 2;
194 if (rf->wait_time > 86400)
195 rf->wait_time = 86400;
197 NOTICE ("read-function of plugin `%s' "
198 "failed. Will suspend it for %i "
199 "seconds.", le->key, rf->wait_left);
204 rf->wait_time = interval_g;
207 pthread_mutex_lock (&read_lock);
209 rf->needs_read = DONE;
211 } /* while (le != NULL) */
213 if ((read_loop != 0) && (done == 0))
215 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
216 (unsigned long int) pthread_self ());
217 pthread_cond_wait (&read_cond, &read_lock);
219 } /* while (read_loop) */
221 pthread_mutex_unlock (&read_lock);
224 } /* void *plugin_read_thread */
226 static void start_threads (int num)
230 if (read_threads != NULL)
233 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
234 if (read_threads == NULL)
236 ERROR ("plugin: start_threads: calloc failed.");
240 read_threads_num = 0;
241 for (i = 0; i < num; i++)
243 if (pthread_create (read_threads + read_threads_num, NULL,
244 plugin_read_thread, NULL) == 0)
250 ERROR ("plugin: start_threads: pthread_create failed.");
254 } /* void start_threads */
256 static void stop_threads (void)
260 pthread_mutex_lock (&read_lock);
262 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
263 pthread_cond_broadcast (&read_cond);
264 pthread_mutex_unlock (&read_lock);
266 for (i = 0; i < read_threads_num; i++)
268 if (pthread_join (read_threads[i], NULL) != 0)
270 ERROR ("plugin: stop_threads: pthread_join failed.");
272 read_threads[i] = (pthread_t) 0;
274 sfree (read_threads);
275 read_threads_num = 0;
276 } /* void stop_threads */
281 void plugin_set_dir (const char *dir)
283 if (plugindir != NULL)
288 else if ((plugindir = strdup (dir)) == NULL)
291 ERROR ("strdup failed: %s",
292 sstrerror (errno, errbuf, sizeof (errbuf)));
297 int plugin_load (const char *type)
301 char filename[BUFSIZE];
302 char typename[BUFSIZE];
308 DEBUG ("type = %s", type);
310 dir = plugin_get_dir ();
313 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
314 * type when matching the filename */
315 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
317 WARNING ("snprintf: truncated: `%s.so'", type);
320 typename_len = strlen (typename);
322 if ((dh = opendir (dir)) == NULL)
325 ERROR ("opendir (%s): %s", dir,
326 sstrerror (errno, errbuf, sizeof (errbuf)));
330 while ((de = readdir (dh)) != NULL)
332 if (strncasecmp (de->d_name, typename, typename_len))
335 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
337 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
341 if (lstat (filename, &statbuf) == -1)
344 WARNING ("stat %s: %s", filename,
345 sstrerror (errno, errbuf, sizeof (errbuf)));
348 else if (!S_ISREG (statbuf.st_mode))
350 /* don't follow symlinks */
354 if (plugin_load_file (filename) == 0)
362 fprintf (stderr, "Unable to load plugin %s.\n", type);
372 * The `register_*' functions follow
374 int plugin_register_config (const char *name,
375 int (*callback) (const char *key, const char *val),
376 const char **keys, int keys_num)
378 cf_register (name, callback, keys, keys_num);
380 } /* int plugin_register_config */
382 int plugin_register_complex_config (const char *type,
383 int (*callback) (oconfig_item_t *))
385 return (cf_register_complex (type, callback));
386 } /* int plugin_register_complex_config */
388 int plugin_register_init (const char *name,
389 int (*callback) (void))
391 return (register_callback (&list_init, name, (void *) callback));
392 } /* plugin_register_init */
394 int plugin_register_read (const char *name,
395 int (*callback) (void))
399 rf = (read_func_t *) malloc (sizeof (read_func_t));
403 ERROR ("plugin_register_read: malloc failed: %s",
404 sstrerror (errno, errbuf, sizeof (errbuf)));
408 memset (rf, '\0', sizeof (read_func_t));
409 rf->wait_time = interval_g;
411 rf->callback = callback;
412 rf->needs_read = DONE;
414 return (register_callback (&list_read, name, (void *) rf));
415 } /* int plugin_register_read */
417 int plugin_register_write (const char *name,
418 int (*callback) (const data_set_t *ds, const value_list_t *vl))
420 return (register_callback (&list_write, name, (void *) callback));
421 } /* int plugin_register_write */
423 int plugin_register_shutdown (char *name,
424 int (*callback) (void))
426 return (register_callback (&list_shutdown, name, (void *) callback));
427 } /* int plugin_register_shutdown */
429 int plugin_register_data_set (const data_set_t *ds)
434 if ((list_data_set != NULL)
435 && (llist_search (list_data_set, ds->type) != NULL))
437 NOTICE ("Replacing DS `%s' with another version.", ds->type);
438 plugin_unregister_data_set (ds->type);
441 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
444 memcpy(ds_copy, ds, sizeof (data_set_t));
446 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
448 if (ds_copy->ds == NULL)
454 for (i = 0; i < ds->ds_num; i++)
455 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
457 return (register_callback (&list_data_set, ds->type, (void *) ds_copy));
458 } /* int plugin_register_data_set */
460 int plugin_register_log (char *name,
461 void (*callback) (int priority, const char *msg))
463 return (register_callback (&list_log, name, (void *) callback));
464 } /* int plugin_register_log */
466 int plugin_unregister_config (const char *name)
468 cf_unregister (name);
470 } /* int plugin_unregister_config */
472 int plugin_unregister_complex_config (const char *name)
474 cf_unregister_complex (name);
476 } /* int plugin_unregister_complex_config */
478 int plugin_unregister_init (const char *name)
480 return (plugin_unregister (list_init, name));
483 int plugin_unregister_read (const char *name)
487 e = llist_search (list_read, name);
492 llist_remove (list_read, e);
499 int plugin_unregister_write (const char *name)
501 return (plugin_unregister (list_write, name));
504 int plugin_unregister_shutdown (const char *name)
506 return (plugin_unregister (list_shutdown, name));
509 int plugin_unregister_data_set (const char *name)
514 if (list_data_set == NULL)
517 e = llist_search (list_data_set, name);
522 llist_remove (list_data_set, e);
523 ds = (data_set_t *) e->value;
530 } /* int plugin_unregister_data_set */
532 int plugin_unregister_log (const char *name)
534 return (plugin_unregister (list_log, name));
537 void plugin_init_all (void)
539 int (*callback) (void);
543 /* Start read-threads */
544 if (list_read != NULL)
548 rt = global_option_get ("ReadThreads");
550 start_threads ((num > 0) ? num : 5);
553 /* Init the value cache */
556 if (list_init == NULL)
559 le = llist_head (list_init);
562 callback = (int (*) (void)) le->value;
563 status = (*callback) ();
567 ERROR ("Initialization of plugin `%s' "
568 "failed with status %i. "
569 "Plugin will be unloaded.",
571 /* FIXME: Unload _all_ functions */
572 plugin_unregister_read (le->key);
577 } /* void plugin_init_all */
579 void plugin_read_all (const int *loop)
584 if (list_read == NULL)
587 pthread_mutex_lock (&read_lock);
589 le = llist_head (list_read);
592 rf = (read_func_t *) le->value;
594 if (rf->needs_read != DONE)
600 if (rf->wait_left > 0)
601 rf->wait_left -= interval_g;
603 if (rf->wait_left <= 0)
605 rf->needs_read = TODO;
611 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
612 pthread_cond_broadcast (&read_cond);
613 pthread_mutex_unlock (&read_lock);
614 } /* void plugin_read_all */
616 void plugin_shutdown_all (void)
618 int (*callback) (void);
623 if (list_shutdown == NULL)
626 le = llist_head (list_shutdown);
629 callback = (int (*) (void)) le->value;
631 /* Advance the pointer before calling the callback allows
632 * shutdown functions to unregister themselves. If done the
633 * other way around the memory `le' points to will be freed
634 * after callback returns. */
639 } /* void plugin_shutdown_all */
641 int plugin_dispatch_values (const char *name, value_list_t *vl)
643 int (*callback) (const data_set_t *, const value_list_t *);
647 if ((list_write == NULL) || (list_data_set == NULL))
650 le = llist_search (list_data_set, name);
653 DEBUG ("No such dataset registered: %s", name);
657 ds = (data_set_t *) le->value;
659 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
661 "plugin = %s; plugin_instance = %s; "
662 "type = %s; type_instance = %s;",
663 (unsigned int) vl->time, vl->interval,
665 vl->plugin, vl->plugin_instance,
666 ds->type, vl->type_instance);
669 assert (ds->ds_num == vl->values_len);
671 if (ds->ds_num != vl->values_len)
673 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
674 "(vl->values_len = %i)",
675 ds->type, ds->ds_num, vl->values_len);
680 escape_slashes (vl->host, sizeof (vl->host));
681 escape_slashes (vl->plugin, sizeof (vl->plugin));
682 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
683 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
685 /* Update the value cache */
688 le = llist_head (list_write);
691 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
692 (*callback) (ds, vl);
698 } /* int plugin_dispatch_values */
700 void plugin_log (int level, const char *format, ...)
705 void (*callback) (int, const char *);
708 if (list_log == NULL)
712 if (level >= LOG_DEBUG)
716 va_start (ap, format);
717 vsnprintf (msg, 512, format, ap);
721 le = llist_head (list_log);
724 callback = (void (*) (int, const char *)) le->value;
725 (*callback) (level, msg);
729 } /* void plugin_log */
731 void plugin_complain (int level, complain_t *c, const char *format, ...)
742 if (c->interval < interval_g)
743 c->interval = interval_g;
747 if (c->interval > 86400)
750 c->delay = c->interval / interval_g;
752 va_start (ap, format);
753 vsnprintf (message, 512, format, ap);
757 plugin_log (level, message);
760 void plugin_relief (int level, complain_t *c, const char *format, ...)
765 if (c->interval == 0)
770 va_start (ap, format);
771 vsnprintf (message, 512, format, ap);
775 plugin_log (level, message);
778 const data_set_t *plugin_get_ds (const char *name)
783 le = llist_search (list_data_set, name);
786 DEBUG ("No such dataset registered: %s", name);
790 ds = (data_set_t *) le->value;
793 } /* data_set_t *plugin_get_ds */