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;
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;
70 static const char *plugin_get_dir (void)
72 if (plugindir == NULL)
78 static int register_callback (llist_t **list, const char *name, void *callback)
83 && ((*list = llist_create ()) == NULL))
86 le = llist_search (*list, name);
89 le = llentry_create (name, callback);
93 llist_append (*list, le);
101 } /* int register_callback */
103 static int plugin_unregister (llist_t *list, const char *name)
107 e = llist_search (list, name);
112 llist_remove (list, e);
116 } /* int plugin_unregister */
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..
123 static int plugin_load_file (char *file)
126 void (*reg_handle) (void);
128 DEBUG ("file = %s", file);
131 lt_dlerror (); /* clear errors */
133 if ((dlh = lt_dlopen (file)) == NULL)
135 const char *error = lt_dlerror ();
137 ERROR ("lt_dlopen failed: %s", error);
138 fprintf (stderr, "lt_dlopen failed: %s\n", error);
142 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
144 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
145 file, lt_dlerror ());
155 static void *plugin_read_thread (void *args)
162 pthread_mutex_lock (&read_lock);
164 while (read_loop != 0)
166 le = llist_head (list_read);
169 while ((read_loop != 0) && (le != NULL))
171 rf = (read_func_t *) le->value;
173 if (rf->needs_read != TODO)
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 ();
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);
205 rf->wait_time = interval_g;
208 pthread_mutex_lock (&read_lock);
210 rf->needs_read = DONE;
212 } /* while (le != NULL) */
214 if ((read_loop != 0) && (done == 0))
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);
220 } /* while (read_loop) */
222 pthread_mutex_unlock (&read_lock);
225 } /* void *plugin_read_thread */
227 static void start_threads (int num)
231 if (read_threads != NULL)
234 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
235 if (read_threads == NULL)
237 ERROR ("plugin: start_threads: calloc failed.");
241 read_threads_num = 0;
242 for (i = 0; i < num; i++)
244 if (pthread_create (read_threads + read_threads_num, NULL,
245 plugin_read_thread, NULL) == 0)
251 ERROR ("plugin: start_threads: pthread_create failed.");
255 } /* void start_threads */
257 static void stop_threads (void)
261 pthread_mutex_lock (&read_lock);
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++)
269 if (pthread_join (read_threads[i], NULL) != 0)
271 ERROR ("plugin: stop_threads: pthread_join failed.");
273 read_threads[i] = (pthread_t) 0;
275 sfree (read_threads);
276 read_threads_num = 0;
277 } /* void stop_threads */
282 void plugin_set_dir (const char *dir)
284 if (plugindir != NULL)
289 else if ((plugindir = strdup (dir)) == NULL)
292 ERROR ("strdup failed: %s",
293 sstrerror (errno, errbuf, sizeof (errbuf)));
298 int plugin_load (const char *type)
302 char filename[BUFSIZE];
303 char typename[BUFSIZE];
309 DEBUG ("type = %s", type);
311 dir = plugin_get_dir ();
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)
318 WARNING ("snprintf: truncated: `%s.so'", type);
321 typename_len = strlen (typename);
323 if ((dh = opendir (dir)) == NULL)
326 ERROR ("opendir (%s): %s", dir,
327 sstrerror (errno, errbuf, sizeof (errbuf)));
331 while ((de = readdir (dh)) != NULL)
333 if (strncasecmp (de->d_name, typename, typename_len))
336 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
338 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
342 if (lstat (filename, &statbuf) == -1)
345 WARNING ("stat %s: %s", filename,
346 sstrerror (errno, errbuf, sizeof (errbuf)));
349 else if (!S_ISREG (statbuf.st_mode))
351 /* don't follow symlinks */
355 if (plugin_load_file (filename) == 0)
363 fprintf (stderr, "Unable to load plugin %s.\n", type);
373 * The `register_*' functions follow
375 int plugin_register_config (const char *name,
376 int (*callback) (const char *key, const char *val),
377 const char **keys, int keys_num)
379 cf_register (name, callback, keys, keys_num);
381 } /* int plugin_register_config */
383 int plugin_register_complex_config (const char *type,
384 int (*callback) (oconfig_item_t *))
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))
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))
400 rf = (read_func_t *) malloc (sizeof (read_func_t));
404 ERROR ("plugin_register_read: malloc failed: %s",
405 sstrerror (errno, errbuf, sizeof (errbuf)));
409 memset (rf, '\0', sizeof (read_func_t));
410 rf->wait_time = interval_g;
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))
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))
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)
435 if ((list_data_set != NULL)
436 && (llist_search (list_data_set, ds->type) != NULL))
438 NOTICE ("Replacing DS `%s' with another version.", ds->type);
439 plugin_unregister_data_set (ds->type);
442 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
445 memcpy(ds_copy, ds, sizeof (data_set_t));
447 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
449 if (ds_copy->ds == NULL)
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))
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))
470 return (register_callback (&list_log, name, (void *) callback));
471 } /* int plugin_register_log */
473 int plugin_unregister_config (const char *name)
475 cf_unregister (name);
477 } /* int plugin_unregister_config */
479 int plugin_unregister_complex_config (const char *name)
481 cf_unregister_complex (name);
483 } /* int plugin_unregister_complex_config */
485 int plugin_unregister_init (const char *name)
487 return (plugin_unregister (list_init, name));
490 int plugin_unregister_read (const char *name)
494 e = llist_search (list_read, name);
499 llist_remove (list_read, e);
506 int plugin_unregister_write (const char *name)
508 return (plugin_unregister (list_write, name));
511 int plugin_unregister_shutdown (const char *name)
513 return (plugin_unregister (list_shutdown, name));
516 int plugin_unregister_data_set (const char *name)
521 if (list_data_set == NULL)
524 e = llist_search (list_data_set, name);
529 llist_remove (list_data_set, e);
530 ds = (data_set_t *) e->value;
537 } /* int plugin_unregister_data_set */
539 int plugin_unregister_log (const char *name)
541 return (plugin_unregister (list_log, name));
544 int plugin_unregister_notification (const char *name)
546 return (plugin_unregister (list_notification, name));
549 void plugin_init_all (void)
551 int (*callback) (void);
555 /* Start read-threads */
556 if (list_read != NULL)
560 rt = global_option_get ("ReadThreads");
562 start_threads ((num > 0) ? num : 5);
565 /* Init the value cache */
568 if (list_init == NULL)
571 le = llist_head (list_init);
574 callback = (int (*) (void)) le->value;
575 status = (*callback) ();
579 ERROR ("Initialization of plugin `%s' "
580 "failed with status %i. "
581 "Plugin will be unloaded.",
583 /* FIXME: Unload _all_ functions */
584 plugin_unregister_read (le->key);
589 } /* void plugin_init_all */
591 void plugin_read_all (const int *loop)
596 if (list_read == NULL)
599 pthread_mutex_lock (&read_lock);
601 le = llist_head (list_read);
604 rf = (read_func_t *) le->value;
606 if (rf->needs_read != DONE)
612 if (rf->wait_left > 0)
613 rf->wait_left -= interval_g;
615 if (rf->wait_left <= 0)
617 rf->needs_read = TODO;
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)
630 int (*callback) (void);
635 if (list_shutdown == NULL)
638 le = llist_head (list_shutdown);
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. */
651 } /* void plugin_shutdown_all */
653 int plugin_dispatch_values (const char *name, value_list_t *vl)
655 int (*callback) (const data_set_t *, const value_list_t *);
659 if ((list_write == NULL) || (list_data_set == NULL))
662 le = llist_search (list_data_set, name);
665 DEBUG ("No such dataset registered: %s", name);
669 ds = (data_set_t *) le->value;
671 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
673 "plugin = %s; plugin_instance = %s; "
674 "type = %s; type_instance = %s;",
675 (unsigned int) vl->time, vl->interval,
677 vl->plugin, vl->plugin_instance,
678 ds->type, vl->type_instance);
681 assert (ds->ds_num == vl->values_len);
683 if (ds->ds_num != vl->values_len)
685 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
686 "(vl->values_len = %i)",
687 ds->type, ds->ds_num, vl->values_len);
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 */
700 le = llist_head (list_write);
703 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
704 (*callback) (ds, vl);
710 } /* int plugin_dispatch_values */
712 int plugin_dispatch_notification (const notification_t *notif)
714 int (*callback) (const notification_t *);
716 /* Possible TODO: Add flap detection here */
718 /* Nobody cares for notifications */
719 if (list_notification == NULL)
722 le = llist_head (list_notification);
725 callback = (int (*) (const notification_t *)) le->value;
732 } /* int plugin_dispatch_notification */
734 void plugin_log (int level, const char *format, ...)
739 void (*callback) (int, const char *);
742 if (list_log == NULL)
746 if (level >= LOG_DEBUG)
750 va_start (ap, format);
751 vsnprintf (msg, 512, format, ap);
755 le = llist_head (list_log);
758 callback = (void (*) (int, const char *)) le->value;
759 (*callback) (level, msg);
763 } /* void plugin_log */
765 void plugin_complain (int level, complain_t *c, const char *format, ...)
776 if (c->interval < interval_g)
777 c->interval = interval_g;
781 if (c->interval > 86400)
784 c->delay = c->interval / interval_g;
786 va_start (ap, format);
787 vsnprintf (message, 512, format, ap);
791 plugin_log (level, message);
794 void plugin_relief (int level, complain_t *c, const char *format, ...)
799 if (c->interval == 0)
804 va_start (ap, format);
805 vsnprintf (message, 512, format, ap);
809 plugin_log (level, message);
812 const data_set_t *plugin_get_ds (const char *name)
817 le = llist_search (list_data_set, name);
820 DEBUG ("No such dataset registered: %s", name);
824 ds = (data_set_t *) le->value;
827 } /* data_set_t *plugin_get_ds */