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_avltree.h"
34 #include "utils_llist.h"
35 #include "utils_cache.h"
36 #include "utils_threshold.h"
45 int (*callback) (void);
46 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
48 typedef struct read_func_s read_func_t;
53 static llist_t *list_init;
54 static llist_t *list_read;
55 static llist_t *list_write;
56 static llist_t *list_shutdown;
57 static llist_t *list_log;
58 static llist_t *list_notification;
60 static c_avl_tree_t *data_sets;
62 static char *plugindir = NULL;
64 static int read_loop = 1;
65 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
66 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
67 static pthread_t *read_threads = NULL;
68 static int read_threads_num = 0;
73 static const char *plugin_get_dir (void)
75 if (plugindir == NULL)
81 static int register_callback (llist_t **list, const char *name, void *callback)
87 && ((*list = llist_create ()) == NULL))
90 le = llist_search (*list, name);
97 le = llentry_create (key, callback);
104 llist_append (*list, le);
108 le->value = callback;
112 } /* int register_callback */
114 static int plugin_unregister (llist_t *list, const char *name)
118 e = llist_search (list, name);
123 llist_remove (list, e);
128 } /* int plugin_unregister */
131 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
132 * object, but it will bitch about a shared object not having a
133 * ``module_register'' symbol..
135 static int plugin_load_file (char *file)
138 void (*reg_handle) (void);
140 DEBUG ("file = %s", file);
143 lt_dlerror (); /* clear errors */
145 if ((dlh = lt_dlopen (file)) == NULL)
147 const char *error = lt_dlerror ();
149 ERROR ("lt_dlopen failed: %s", error);
150 fprintf (stderr, "lt_dlopen failed: %s\n", error);
154 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
156 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
157 file, lt_dlerror ());
167 static void *plugin_read_thread (void *args)
174 pthread_mutex_lock (&read_lock);
176 while (read_loop != 0)
178 le = llist_head (list_read);
181 while ((read_loop != 0) && (le != NULL))
183 rf = (read_func_t *) le->value;
185 if (rf->needs_read != TODO)
191 /* We will do this read function */
192 rf->needs_read = ACTIVE;
194 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
195 (unsigned long int) pthread_self (), le->key);
196 pthread_mutex_unlock (&read_lock);
198 status = rf->callback ();
203 if (rf->wait_time < interval_g)
204 rf->wait_time = interval_g;
205 rf->wait_left = rf->wait_time;
206 rf->wait_time = rf->wait_time * 2;
207 if (rf->wait_time > 86400)
208 rf->wait_time = 86400;
210 NOTICE ("read-function of plugin `%s' "
211 "failed. Will suspend it for %i "
212 "seconds.", le->key, rf->wait_left);
217 rf->wait_time = interval_g;
220 pthread_mutex_lock (&read_lock);
222 rf->needs_read = DONE;
224 } /* while (le != NULL) */
226 if ((read_loop != 0) && (done == 0))
228 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
229 (unsigned long int) pthread_self ());
230 pthread_cond_wait (&read_cond, &read_lock);
232 } /* while (read_loop) */
234 pthread_mutex_unlock (&read_lock);
237 } /* void *plugin_read_thread */
239 static void start_threads (int num)
243 if (read_threads != NULL)
246 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
247 if (read_threads == NULL)
249 ERROR ("plugin: start_threads: calloc failed.");
253 read_threads_num = 0;
254 for (i = 0; i < num; i++)
256 if (pthread_create (read_threads + read_threads_num, NULL,
257 plugin_read_thread, NULL) == 0)
263 ERROR ("plugin: start_threads: pthread_create failed.");
267 } /* void start_threads */
269 static void stop_threads (void)
273 pthread_mutex_lock (&read_lock);
275 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
276 pthread_cond_broadcast (&read_cond);
277 pthread_mutex_unlock (&read_lock);
279 for (i = 0; i < read_threads_num; i++)
281 if (pthread_join (read_threads[i], NULL) != 0)
283 ERROR ("plugin: stop_threads: pthread_join failed.");
285 read_threads[i] = (pthread_t) 0;
287 sfree (read_threads);
288 read_threads_num = 0;
289 } /* void stop_threads */
294 void plugin_set_dir (const char *dir)
296 if (plugindir != NULL)
301 else if ((plugindir = strdup (dir)) == NULL)
304 ERROR ("strdup failed: %s",
305 sstrerror (errno, errbuf, sizeof (errbuf)));
310 int plugin_load (const char *type)
314 char filename[BUFSIZE];
315 char typename[BUFSIZE];
321 DEBUG ("type = %s", type);
323 dir = plugin_get_dir ();
326 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
327 * type when matching the filename */
328 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
330 WARNING ("snprintf: truncated: `%s.so'", type);
333 typename_len = strlen (typename);
335 if ((dh = opendir (dir)) == NULL)
338 ERROR ("opendir (%s): %s", dir,
339 sstrerror (errno, errbuf, sizeof (errbuf)));
343 while ((de = readdir (dh)) != NULL)
345 if (strncasecmp (de->d_name, typename, typename_len))
348 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
350 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
354 if (lstat (filename, &statbuf) == -1)
357 WARNING ("stat %s: %s", filename,
358 sstrerror (errno, errbuf, sizeof (errbuf)));
361 else if (!S_ISREG (statbuf.st_mode))
363 /* don't follow symlinks */
367 if (plugin_load_file (filename) == 0)
375 fprintf (stderr, "Unable to load plugin %s.\n", type);
385 * The `register_*' functions follow
387 int plugin_register_config (const char *name,
388 int (*callback) (const char *key, const char *val),
389 const char **keys, int keys_num)
391 cf_register (name, callback, keys, keys_num);
393 } /* int plugin_register_config */
395 int plugin_register_complex_config (const char *type,
396 int (*callback) (oconfig_item_t *))
398 return (cf_register_complex (type, callback));
399 } /* int plugin_register_complex_config */
401 int plugin_register_init (const char *name,
402 int (*callback) (void))
404 return (register_callback (&list_init, name, (void *) callback));
405 } /* plugin_register_init */
407 int plugin_register_read (const char *name,
408 int (*callback) (void))
412 rf = (read_func_t *) malloc (sizeof (read_func_t));
416 ERROR ("plugin_register_read: malloc failed: %s",
417 sstrerror (errno, errbuf, sizeof (errbuf)));
421 memset (rf, '\0', sizeof (read_func_t));
422 rf->wait_time = interval_g;
424 rf->callback = callback;
425 rf->needs_read = DONE;
427 return (register_callback (&list_read, name, (void *) rf));
428 } /* int plugin_register_read */
430 int plugin_register_write (const char *name,
431 int (*callback) (const data_set_t *ds, const value_list_t *vl))
433 return (register_callback (&list_write, name, (void *) callback));
434 } /* int plugin_register_write */
436 int plugin_register_shutdown (char *name,
437 int (*callback) (void))
439 return (register_callback (&list_shutdown, name, (void *) callback));
440 } /* int plugin_register_shutdown */
442 int plugin_register_data_set (const data_set_t *ds)
447 if ((data_sets != NULL)
448 && (c_avl_get (data_sets, ds->type, NULL) == 0))
450 NOTICE ("Replacing DS `%s' with another version.", ds->type);
451 plugin_unregister_data_set (ds->type);
453 else if (data_sets == NULL)
455 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
456 if (data_sets == NULL)
460 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
463 memcpy(ds_copy, ds, sizeof (data_set_t));
465 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
467 if (ds_copy->ds == NULL)
473 for (i = 0; i < ds->ds_num; i++)
474 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
476 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
477 } /* int plugin_register_data_set */
479 int plugin_register_log (char *name,
480 void (*callback) (int priority, const char *msg))
482 return (register_callback (&list_log, name, (void *) callback));
483 } /* int plugin_register_log */
485 int plugin_register_notification (const char *name,
486 int (*callback) (const notification_t *notif))
488 return (register_callback (&list_notification, name, (void *) callback));
489 } /* int plugin_register_log */
491 int plugin_unregister_config (const char *name)
493 cf_unregister (name);
495 } /* int plugin_unregister_config */
497 int plugin_unregister_complex_config (const char *name)
499 cf_unregister_complex (name);
501 } /* int plugin_unregister_complex_config */
503 int plugin_unregister_init (const char *name)
505 return (plugin_unregister (list_init, name));
508 int plugin_unregister_read (const char *name)
512 e = llist_search (list_read, name);
517 llist_remove (list_read, e);
525 int plugin_unregister_write (const char *name)
527 return (plugin_unregister (list_write, name));
530 int plugin_unregister_shutdown (const char *name)
532 return (plugin_unregister (list_shutdown, name));
535 int plugin_unregister_data_set (const char *name)
539 if (data_sets == NULL)
542 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
549 } /* int plugin_unregister_data_set */
551 int plugin_unregister_log (const char *name)
553 return (plugin_unregister (list_log, name));
556 int plugin_unregister_notification (const char *name)
558 return (plugin_unregister (list_notification, name));
561 void plugin_init_all (void)
563 int (*callback) (void);
567 /* Start read-threads */
568 if (list_read != NULL)
572 rt = global_option_get ("ReadThreads");
574 start_threads ((num > 0) ? num : 5);
577 /* Init the value cache */
580 if (list_init == NULL)
583 le = llist_head (list_init);
586 callback = (int (*) (void)) le->value;
587 status = (*callback) ();
591 ERROR ("Initialization of plugin `%s' "
592 "failed with status %i. "
593 "Plugin will be unloaded.",
595 /* FIXME: Unload _all_ functions */
596 plugin_unregister_read (le->key);
601 } /* void plugin_init_all */
603 void plugin_read_all (void)
610 if (list_read == NULL)
613 pthread_mutex_lock (&read_lock);
615 le = llist_head (list_read);
618 rf = (read_func_t *) le->value;
620 if (rf->needs_read != DONE)
626 if (rf->wait_left > 0)
627 rf->wait_left -= interval_g;
629 if (rf->wait_left <= 0)
631 rf->needs_read = TODO;
637 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
638 pthread_cond_broadcast (&read_cond);
639 pthread_mutex_unlock (&read_lock);
640 } /* void plugin_read_all */
642 void plugin_shutdown_all (void)
644 int (*callback) (void);
649 if (list_shutdown == NULL)
652 le = llist_head (list_shutdown);
655 callback = (int (*) (void)) le->value;
657 /* Advance the pointer before calling the callback allows
658 * shutdown functions to unregister themselves. If done the
659 * other way around the memory `le' points to will be freed
660 * after callback returns. */
665 } /* void plugin_shutdown_all */
667 int plugin_dispatch_values (const char *name, value_list_t *vl)
669 int (*callback) (const data_set_t *, const value_list_t *);
673 if ((list_write == NULL) || (data_sets == NULL))
676 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
678 DEBUG ("No such dataset registered: %s", name);
682 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
684 "plugin = %s; plugin_instance = %s; "
685 "type = %s; type_instance = %s;",
686 (unsigned int) vl->time, vl->interval,
688 vl->plugin, vl->plugin_instance,
689 ds->type, vl->type_instance);
692 assert (ds->ds_num == vl->values_len);
694 if (ds->ds_num != vl->values_len)
696 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
697 "(vl->values_len = %i)",
698 ds->type, ds->ds_num, vl->values_len);
703 escape_slashes (vl->host, sizeof (vl->host));
704 escape_slashes (vl->plugin, sizeof (vl->plugin));
705 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
706 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
708 /* Update the value cache */
710 ut_check_threshold (ds, vl);
712 le = llist_head (list_write);
715 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
716 (*callback) (ds, vl);
722 } /* int plugin_dispatch_values */
724 int plugin_dispatch_notification (const notification_t *notif)
726 int (*callback) (const notification_t *);
728 /* Possible TODO: Add flap detection here */
730 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
731 "time = %u; host = %s;",
732 notif->severity, notif->message,
733 (unsigned int) notif->time, notif->host);
735 /* Nobody cares for notifications */
736 if (list_notification == NULL)
739 le = llist_head (list_notification);
742 callback = (int (*) (const notification_t *)) le->value;
749 } /* int plugin_dispatch_notification */
751 void plugin_log (int level, const char *format, ...)
756 void (*callback) (int, const char *);
759 if (list_log == NULL)
763 if (level >= LOG_DEBUG)
767 va_start (ap, format);
768 vsnprintf (msg, 512, format, ap);
772 le = llist_head (list_log);
775 callback = (void (*) (int, const char *)) le->value;
776 (*callback) (level, msg);
780 } /* void plugin_log */
782 const data_set_t *plugin_get_ds (const char *name)
786 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
788 DEBUG ("No such dataset registered: %s", name);
793 } /* data_set_t *plugin_get_ds */