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"
42 int (*callback) (void);
43 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
45 typedef struct read_func_s read_func_t;
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;
68 static const char *plugin_get_dir (void)
70 if (plugindir == NULL)
76 static int register_callback (llist_t **list, const char *name, void *callback)
81 && ((*list = llist_create ()) == NULL))
84 le = llist_search (*list, name);
87 le = llentry_create (name, callback);
91 llist_append (*list, le);
99 } /* int register_callback */
101 static int plugin_unregister (llist_t *list, const char *name)
105 e = llist_search (list, name);
110 llist_remove (list, e);
114 } /* int plugin_unregister */
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..
121 static int plugin_load_file (char *file)
124 void (*reg_handle) (void);
126 DEBUG ("file = %s", file);
129 lt_dlerror (); /* clear errors */
131 if ((dlh = lt_dlopen (file)) == NULL)
133 const char *error = lt_dlerror ();
135 ERROR ("lt_dlopen failed: %s", error);
136 fprintf (stderr, "lt_dlopen failed: %s\n", error);
140 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
142 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
143 file, lt_dlerror ());
153 static void *plugin_read_thread (void *args)
160 pthread_mutex_lock (&read_lock);
162 while (read_loop != 0)
164 le = llist_head (list_read);
167 while ((read_loop != 0) && (le != NULL))
169 rf = (read_func_t *) le->value;
171 if (rf->needs_read != TODO)
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 ();
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);
203 rf->wait_time = interval_g;
206 pthread_mutex_lock (&read_lock);
208 rf->needs_read = DONE;
210 } /* while (le != NULL) */
212 if ((read_loop != 0) && (done == 0))
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);
218 } /* while (read_loop) */
220 pthread_mutex_unlock (&read_lock);
223 } /* void *plugin_read_thread */
225 static void start_threads (int num)
229 if (read_threads != NULL)
232 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
233 if (read_threads == NULL)
235 ERROR ("plugin: start_threads: calloc failed.");
239 read_threads_num = 0;
240 for (i = 0; i < num; i++)
242 if (pthread_create (read_threads + read_threads_num, NULL,
243 plugin_read_thread, NULL) == 0)
249 ERROR ("plugin: start_threads: pthread_create failed.");
253 } /* void start_threads */
255 static void stop_threads (void)
259 pthread_mutex_lock (&read_lock);
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++)
267 if (pthread_join (read_threads[i], NULL) != 0)
269 ERROR ("plugin: stop_threads: pthread_join failed.");
271 read_threads[i] = (pthread_t) 0;
273 sfree (read_threads);
274 read_threads_num = 0;
275 } /* void stop_threads */
280 void plugin_set_dir (const char *dir)
282 if (plugindir != NULL)
287 else if ((plugindir = strdup (dir)) == NULL)
290 ERROR ("strdup failed: %s",
291 sstrerror (errno, errbuf, sizeof (errbuf)));
296 int plugin_load (const char *type)
300 char filename[BUFSIZE];
301 char typename[BUFSIZE];
307 DEBUG ("type = %s", type);
309 dir = plugin_get_dir ();
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)
316 WARNING ("snprintf: truncated: `%s.so'", type);
319 typename_len = strlen (typename);
321 if ((dh = opendir (dir)) == NULL)
324 ERROR ("opendir (%s): %s", dir,
325 sstrerror (errno, errbuf, sizeof (errbuf)));
329 while ((de = readdir (dh)) != NULL)
331 if (strncasecmp (de->d_name, typename, typename_len))
334 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
336 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
340 if (lstat (filename, &statbuf) == -1)
343 WARNING ("stat %s: %s", filename,
344 sstrerror (errno, errbuf, sizeof (errbuf)));
347 else if (!S_ISREG (statbuf.st_mode))
349 /* don't follow symlinks */
353 if (plugin_load_file (filename) == 0)
361 fprintf (stderr, "Unable to load plugin %s.\n", type);
371 * The `register_*' functions follow
373 int plugin_register_config (const char *name,
374 int (*callback) (const char *key, const char *val),
375 const char **keys, int keys_num)
377 cf_register (name, callback, keys, keys_num);
379 } /* int plugin_register_config */
381 int plugin_register_complex_config (const char *type,
382 int (*callback) (oconfig_item_t *))
384 return (cf_register_complex (type, callback));
385 } /* int plugin_register_complex_config */
387 int plugin_register_init (const char *name,
388 int (*callback) (void))
390 return (register_callback (&list_init, name, (void *) callback));
391 } /* plugin_register_init */
393 int plugin_register_read (const char *name,
394 int (*callback) (void))
398 rf = (read_func_t *) malloc (sizeof (read_func_t));
402 ERROR ("plugin_register_read: malloc failed: %s",
403 sstrerror (errno, errbuf, sizeof (errbuf)));
407 memset (rf, '\0', sizeof (read_func_t));
408 rf->wait_time = interval_g;
410 rf->callback = callback;
411 rf->needs_read = DONE;
413 return (register_callback (&list_read, name, (void *) rf));
414 } /* int plugin_register_read */
416 int plugin_register_write (const char *name,
417 int (*callback) (const data_set_t *ds, const value_list_t *vl))
419 return (register_callback (&list_write, name, (void *) callback));
420 } /* int plugin_register_write */
422 int plugin_register_shutdown (char *name,
423 int (*callback) (void))
425 return (register_callback (&list_shutdown, name, (void *) callback));
426 } /* int plugin_register_shutdown */
428 int plugin_register_data_set (const data_set_t *ds)
433 if ((list_data_set != NULL)
434 && (llist_search (list_data_set, ds->type) != NULL))
436 NOTICE ("Replacing DS `%s' with another version.", ds->type);
437 plugin_unregister_data_set (ds->type);
440 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
443 memcpy(ds_copy, ds, sizeof (data_set_t));
445 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
447 if (ds_copy->ds == NULL)
453 for (i = 0; i < ds->ds_num; i++)
454 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
456 return (register_callback (&list_data_set, ds->type, (void *) ds_copy));
457 } /* int plugin_register_data_set */
459 int plugin_register_log (char *name,
460 void (*callback) (int priority, const char *msg))
462 return (register_callback (&list_log, name, (void *) callback));
463 } /* int plugin_register_log */
465 int plugin_unregister_config (const char *name)
467 cf_unregister (name);
469 } /* int plugin_unregister_config */
471 int plugin_unregister_complex_config (const char *name)
473 cf_unregister_complex (name);
475 } /* int plugin_unregister_complex_config */
477 int plugin_unregister_init (const char *name)
479 return (plugin_unregister (list_init, name));
482 int plugin_unregister_read (const char *name)
486 e = llist_search (list_read, name);
491 llist_remove (list_read, e);
498 int plugin_unregister_write (const char *name)
500 return (plugin_unregister (list_write, name));
503 int plugin_unregister_shutdown (const char *name)
505 return (plugin_unregister (list_shutdown, name));
508 int plugin_unregister_data_set (const char *name)
513 if (list_data_set == NULL)
516 e = llist_search (list_data_set, name);
521 llist_remove (list_data_set, e);
522 ds = (data_set_t *) e->value;
529 } /* int plugin_unregister_data_set */
531 int plugin_unregister_log (const char *name)
533 return (plugin_unregister (list_log, name));
536 void plugin_init_all (void)
538 int (*callback) (void);
542 /* Start read-threads */
543 if (list_read != NULL)
547 rt = global_option_get ("ReadThreads");
549 start_threads ((num > 0) ? num : 5);
552 if (list_init == NULL)
555 le = llist_head (list_init);
558 callback = (int (*) (void)) le->value;
559 status = (*callback) ();
563 ERROR ("Initialization of plugin `%s' "
564 "failed with status %i. "
565 "Plugin will be unloaded.",
567 /* FIXME: Unload _all_ functions */
568 plugin_unregister_read (le->key);
573 } /* void plugin_init_all */
575 void plugin_read_all (const int *loop)
580 if (list_read == NULL)
583 pthread_mutex_lock (&read_lock);
585 le = llist_head (list_read);
588 rf = (read_func_t *) le->value;
590 if (rf->needs_read != DONE)
596 if (rf->wait_left > 0)
597 rf->wait_left -= interval_g;
599 if (rf->wait_left <= 0)
601 rf->needs_read = TODO;
607 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
608 pthread_cond_broadcast (&read_cond);
609 pthread_mutex_unlock (&read_lock);
610 } /* void plugin_read_all */
612 void plugin_shutdown_all (void)
614 int (*callback) (void);
619 if (list_shutdown == NULL)
622 le = llist_head (list_shutdown);
625 callback = (int (*) (void)) le->value;
627 /* Advance the pointer before calling the callback allows
628 * shutdown functions to unregister themselves. If done the
629 * other way around the memory `le' points to will be freed
630 * after callback returns. */
635 } /* void plugin_shutdown_all */
637 int plugin_dispatch_values (const char *name, value_list_t *vl)
639 int (*callback) (const data_set_t *, const value_list_t *);
643 if ((list_write == NULL) || (list_data_set == NULL))
646 le = llist_search (list_data_set, name);
649 DEBUG ("No such dataset registered: %s", name);
653 ds = (data_set_t *) le->value;
655 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
657 "plugin = %s; plugin_instance = %s; "
658 "type = %s; type_instance = %s;",
659 (unsigned int) vl->time, vl->interval,
661 vl->plugin, vl->plugin_instance,
662 ds->type, vl->type_instance);
665 assert (ds->ds_num == vl->values_len);
667 if (ds->ds_num != vl->values_len)
669 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
670 "(vl->values_len = %i)",
671 ds->type, ds->ds_num, vl->values_len);
676 escape_slashes (vl->host, sizeof (vl->host));
677 escape_slashes (vl->plugin, sizeof (vl->plugin));
678 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
679 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
681 le = llist_head (list_write);
684 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
685 (*callback) (ds, vl);
691 } /* int plugin_dispatch_values */
693 void plugin_log (int level, const char *format, ...)
698 void (*callback) (int, const char *);
701 if (list_log == NULL)
705 if (level >= LOG_DEBUG)
709 va_start (ap, format);
710 vsnprintf (msg, 512, format, ap);
714 le = llist_head (list_log);
717 callback = (void (*) (int, const char *)) le->value;
718 (*callback) (level, msg);
722 } /* void plugin_log */
724 void plugin_complain (int level, complain_t *c, const char *format, ...)
735 if (c->interval < interval_g)
736 c->interval = interval_g;
740 if (c->interval > 86400)
743 c->delay = c->interval / interval_g;
745 va_start (ap, format);
746 vsnprintf (message, 512, format, ap);
750 plugin_log (level, message);
753 void plugin_relief (int level, complain_t *c, const char *format, ...)
758 if (c->interval == 0)
763 va_start (ap, format);
764 vsnprintf (message, 512, format, ap);
768 plugin_log (level, message);
771 const data_set_t *plugin_get_ds (const char *name)
776 le = llist_search (list_data_set, name);
779 DEBUG ("No such dataset registered: %s", name);
783 ds = (data_set_t *) le->value;
786 } /* data_set_t *plugin_get_ds */