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);
139 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
141 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
142 file, lt_dlerror ());
152 static void *plugin_read_thread (void *args)
159 pthread_mutex_lock (&read_lock);
161 while (read_loop != 0)
163 le = llist_head (list_read);
166 while ((read_loop != 0) && (le != NULL))
168 rf = (read_func_t *) le->value;
170 if (rf->needs_read != TODO)
176 /* We will do this read function */
177 rf->needs_read = ACTIVE;
179 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
180 (unsigned long int) pthread_self (), le->key);
181 pthread_mutex_unlock (&read_lock);
183 status = rf->callback ();
188 if (rf->wait_time < interval_g)
189 rf->wait_time = interval_g;
190 rf->wait_left = rf->wait_time;
191 rf->wait_time = rf->wait_time * 2;
192 if (rf->wait_time > 86400)
193 rf->wait_time = 86400;
195 NOTICE ("read-function of plugin `%s' "
196 "failed. Will suspend it for %i "
197 "seconds.", le->key, rf->wait_left);
202 rf->wait_time = interval_g;
205 pthread_mutex_lock (&read_lock);
207 rf->needs_read = DONE;
209 } /* while (le != NULL) */
211 if ((read_loop != 0) && (done == 0))
213 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
214 (unsigned long int) pthread_self ());
215 pthread_cond_wait (&read_cond, &read_lock);
217 } /* while (read_loop) */
219 pthread_mutex_unlock (&read_lock);
222 } /* void *plugin_read_thread */
224 static void start_threads (int num)
228 if (read_threads != NULL)
231 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
232 if (read_threads == NULL)
234 ERROR ("plugin: start_threads: calloc failed.");
238 read_threads_num = 0;
239 for (i = 0; i < num; i++)
241 if (pthread_create (read_threads + read_threads_num, NULL,
242 plugin_read_thread, NULL) == 0)
248 ERROR ("plugin: start_threads: pthread_create failed.");
252 } /* void start_threads */
254 static void stop_threads (void)
258 pthread_mutex_lock (&read_lock);
260 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
261 pthread_cond_broadcast (&read_cond);
262 pthread_mutex_unlock (&read_lock);
264 for (i = 0; i < read_threads_num; i++)
266 if (pthread_join (read_threads[i], NULL) != 0)
268 ERROR ("plugin: stop_threads: pthread_join failed.");
270 read_threads[i] = (pthread_t) 0;
272 sfree (read_threads);
273 read_threads_num = 0;
274 } /* void stop_threads */
279 void plugin_set_dir (const char *dir)
281 if (plugindir != NULL)
286 else if ((plugindir = strdup (dir)) == NULL)
289 ERROR ("strdup failed: %s",
290 sstrerror (errno, errbuf, sizeof (errbuf)));
295 int plugin_load (const char *type)
299 char filename[BUFSIZE];
300 char typename[BUFSIZE];
306 DEBUG ("type = %s", type);
308 dir = plugin_get_dir ();
311 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
312 * type when matching the filename */
313 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
315 WARNING ("snprintf: truncated: `%s.so'", type);
318 typename_len = strlen (typename);
320 if ((dh = opendir (dir)) == NULL)
323 ERROR ("opendir (%s): %s", dir,
324 sstrerror (errno, errbuf, sizeof (errbuf)));
328 while ((de = readdir (dh)) != NULL)
330 if (strncasecmp (de->d_name, typename, typename_len))
333 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
335 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
339 if (lstat (filename, &statbuf) == -1)
342 WARNING ("stat %s: %s", filename,
343 sstrerror (errno, errbuf, sizeof (errbuf)));
346 else if (!S_ISREG (statbuf.st_mode))
348 /* don't follow symlinks */
352 if (plugin_load_file (filename) == 0)
366 * The `register_*' functions follow
368 int plugin_register_config (const char *name,
369 int (*callback) (const char *key, const char *val),
370 const char **keys, int keys_num)
372 cf_register (name, callback, keys, keys_num);
374 } /* int plugin_register_config */
376 int plugin_register_complex_config (const char *type,
377 int (*callback) (oconfig_item_t *))
379 return (cf_register_complex (type, callback));
380 } /* int plugin_register_complex_config */
382 int plugin_register_init (const char *name,
383 int (*callback) (void))
385 return (register_callback (&list_init, name, (void *) callback));
386 } /* plugin_register_init */
388 int plugin_register_read (const char *name,
389 int (*callback) (void))
393 rf = (read_func_t *) malloc (sizeof (read_func_t));
397 ERROR ("plugin_register_read: malloc failed: %s",
398 sstrerror (errno, errbuf, sizeof (errbuf)));
402 memset (rf, '\0', sizeof (read_func_t));
403 rf->wait_time = interval_g;
405 rf->callback = callback;
406 rf->needs_read = DONE;
408 return (register_callback (&list_read, name, (void *) rf));
409 } /* int plugin_register_read */
411 int plugin_register_write (const char *name,
412 int (*callback) (const data_set_t *ds, const value_list_t *vl))
414 return (register_callback (&list_write, name, (void *) callback));
415 } /* int plugin_register_write */
417 int plugin_register_shutdown (char *name,
418 int (*callback) (void))
420 return (register_callback (&list_shutdown, name, (void *) callback));
421 } /* int plugin_register_shutdown */
423 int plugin_register_data_set (const data_set_t *ds)
428 if ((list_data_set != NULL)
429 && (llist_search (list_data_set, ds->type) != NULL))
431 NOTICE ("Replacing DS `%s' with another version.", ds->type);
432 plugin_unregister_data_set (ds->type);
435 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
438 memcpy(ds_copy, ds, sizeof (data_set_t));
440 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
442 if (ds_copy->ds == NULL)
448 for (i = 0; i < ds->ds_num; i++)
449 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
451 return (register_callback (&list_data_set, ds->type, (void *) ds_copy));
452 } /* int plugin_register_data_set */
454 int plugin_register_log (char *name,
455 void (*callback) (int priority, const char *msg))
457 return (register_callback (&list_log, name, (void *) callback));
458 } /* int plugin_register_log */
460 int plugin_unregister_config (const char *name)
462 cf_unregister (name);
464 } /* int plugin_unregister_config */
466 int plugin_unregister_complex_config (const char *name)
468 cf_unregister_complex (name);
470 } /* int plugin_unregister_complex_config */
472 int plugin_unregister_init (const char *name)
474 return (plugin_unregister (list_init, name));
477 int plugin_unregister_read (const char *name)
481 e = llist_search (list_read, name);
486 llist_remove (list_read, e);
493 int plugin_unregister_write (const char *name)
495 return (plugin_unregister (list_write, name));
498 int plugin_unregister_shutdown (const char *name)
500 return (plugin_unregister (list_shutdown, name));
503 int plugin_unregister_data_set (const char *name)
508 if (list_data_set == NULL)
511 e = llist_search (list_data_set, name);
516 llist_remove (list_data_set, e);
517 ds = (data_set_t *) e->value;
524 } /* int plugin_unregister_data_set */
526 int plugin_unregister_log (const char *name)
528 return (plugin_unregister (list_log, name));
531 void plugin_init_all (void)
533 int (*callback) (void);
537 /* Start read-threads */
538 if (list_read != NULL)
542 rt = global_option_get ("ReadThreads");
544 start_threads ((num > 0) ? num : 5);
547 if (list_init == NULL)
550 le = llist_head (list_init);
553 callback = (int (*) (void)) le->value;
554 status = (*callback) ();
558 ERROR ("Initialization of plugin `%s' "
559 "failed with status %i. "
560 "Plugin will be unloaded.",
562 /* FIXME: Unload _all_ functions */
563 plugin_unregister_read (le->key);
568 } /* void plugin_init_all */
570 void plugin_read_all (const int *loop)
575 if (list_read == NULL)
578 pthread_mutex_lock (&read_lock);
580 le = llist_head (list_read);
583 rf = (read_func_t *) le->value;
585 if (rf->needs_read != DONE)
591 if (rf->wait_left > 0)
592 rf->wait_left -= interval_g;
594 if (rf->wait_left <= 0)
596 rf->needs_read = TODO;
602 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
603 pthread_cond_broadcast (&read_cond);
604 pthread_mutex_unlock (&read_lock);
605 } /* void plugin_read_all */
607 void plugin_shutdown_all (void)
609 int (*callback) (void);
614 if (list_shutdown == NULL)
617 le = llist_head (list_shutdown);
620 callback = (int (*) (void)) le->value;
622 /* Advance the pointer before calling the callback allows
623 * shutdown functions to unregister themselves. If done the
624 * other way around the memory `le' points to will be freed
625 * after callback returns. */
630 } /* void plugin_shutdown_all */
632 int plugin_dispatch_values (const char *name, value_list_t *vl)
634 int (*callback) (const data_set_t *, const value_list_t *);
638 if ((list_write == NULL) || (list_data_set == NULL))
641 le = llist_search (list_data_set, name);
644 DEBUG ("No such dataset registered: %s", name);
648 ds = (data_set_t *) le->value;
650 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
652 "plugin = %s; plugin_instance = %s; "
653 "type = %s; type_instance = %s;",
654 (unsigned int) vl->time, vl->interval,
656 vl->plugin, vl->plugin_instance,
657 ds->type, vl->type_instance);
660 assert (ds->ds_num == vl->values_len);
662 if (ds->ds_num != vl->values_len)
664 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
665 "(vl->values_len = %i)",
666 ds->type, ds->ds_num, vl->values_len);
671 escape_slashes (vl->host, sizeof (vl->host));
672 escape_slashes (vl->plugin, sizeof (vl->plugin));
673 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
674 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
676 le = llist_head (list_write);
679 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
680 (*callback) (ds, vl);
686 } /* int plugin_dispatch_values */
688 void plugin_log (int level, const char *format, ...)
693 void (*callback) (int, const char *);
696 if (list_log == NULL)
700 if (level >= LOG_DEBUG)
704 va_start (ap, format);
705 vsnprintf (msg, 512, format, ap);
709 le = llist_head (list_log);
712 callback = (void (*) (int, const char *)) le->value;
713 (*callback) (level, msg);
717 } /* void plugin_log */
719 void plugin_complain (int level, complain_t *c, const char *format, ...)
730 if (c->interval < interval_g)
731 c->interval = interval_g;
735 if (c->interval > 86400)
738 c->delay = c->interval / interval_g;
740 va_start (ap, format);
741 vsnprintf (message, 512, format, ap);
745 plugin_log (level, message);
748 void plugin_relief (int level, complain_t *c, const char *format, ...)
753 if (c->interval == 0)
758 va_start (ap, format);
759 vsnprintf (message, 512, format, ap);
763 plugin_log (level, message);
766 const data_set_t *plugin_get_ds (const char *name)
771 le = llist_search (list_data_set, name);
774 DEBUG ("No such dataset registered: %s", name);
778 ds = (data_set_t *) le->value;
781 } /* data_set_t *plugin_get_ds */