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"
35 #include "utils_threshold.h"
44 int (*callback) (void);
45 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
47 typedef struct read_func_s read_func_t;
52 static llist_t *list_init;
53 static llist_t *list_read;
54 static llist_t *list_write;
55 static llist_t *list_shutdown;
56 static llist_t *list_data_set;
57 static llist_t *list_log;
58 static llist_t *list_notification;
60 static char *plugindir = NULL;
62 static int read_loop = 1;
63 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
64 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
65 static pthread_t *read_threads = NULL;
66 static int read_threads_num = 0;
71 static const char *plugin_get_dir (void)
73 if (plugindir == NULL)
79 static int register_callback (llist_t **list, const char *name, void *callback)
85 && ((*list = llist_create ()) == NULL))
88 le = llist_search (*list, name);
95 le = llentry_create (key, callback);
102 llist_append (*list, le);
106 le->value = callback;
110 } /* int register_callback */
112 static int plugin_unregister (llist_t *list, const char *name)
116 e = llist_search (list, name);
121 llist_remove (list, e);
126 } /* int plugin_unregister */
129 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
130 * object, but it will bitch about a shared object not having a
131 * ``module_register'' symbol..
133 static int plugin_load_file (char *file)
136 void (*reg_handle) (void);
138 DEBUG ("file = %s", file);
141 lt_dlerror (); /* clear errors */
143 if ((dlh = lt_dlopen (file)) == NULL)
145 const char *error = lt_dlerror ();
147 ERROR ("lt_dlopen failed: %s", error);
148 fprintf (stderr, "lt_dlopen failed: %s\n", error);
152 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
154 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
155 file, lt_dlerror ());
165 static void *plugin_read_thread (void *args)
172 pthread_mutex_lock (&read_lock);
174 while (read_loop != 0)
176 le = llist_head (list_read);
179 while ((read_loop != 0) && (le != NULL))
181 rf = (read_func_t *) le->value;
183 if (rf->needs_read != TODO)
189 /* We will do this read function */
190 rf->needs_read = ACTIVE;
192 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
193 (unsigned long int) pthread_self (), le->key);
194 pthread_mutex_unlock (&read_lock);
196 status = rf->callback ();
201 if (rf->wait_time < interval_g)
202 rf->wait_time = interval_g;
203 rf->wait_left = rf->wait_time;
204 rf->wait_time = rf->wait_time * 2;
205 if (rf->wait_time > 86400)
206 rf->wait_time = 86400;
208 NOTICE ("read-function of plugin `%s' "
209 "failed. Will suspend it for %i "
210 "seconds.", le->key, rf->wait_left);
215 rf->wait_time = interval_g;
218 pthread_mutex_lock (&read_lock);
220 rf->needs_read = DONE;
222 } /* while (le != NULL) */
224 if ((read_loop != 0) && (done == 0))
226 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
227 (unsigned long int) pthread_self ());
228 pthread_cond_wait (&read_cond, &read_lock);
230 } /* while (read_loop) */
232 pthread_mutex_unlock (&read_lock);
235 } /* void *plugin_read_thread */
237 static void start_threads (int num)
241 if (read_threads != NULL)
244 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
245 if (read_threads == NULL)
247 ERROR ("plugin: start_threads: calloc failed.");
251 read_threads_num = 0;
252 for (i = 0; i < num; i++)
254 if (pthread_create (read_threads + read_threads_num, NULL,
255 plugin_read_thread, NULL) == 0)
261 ERROR ("plugin: start_threads: pthread_create failed.");
265 } /* void start_threads */
267 static void stop_threads (void)
271 pthread_mutex_lock (&read_lock);
273 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
274 pthread_cond_broadcast (&read_cond);
275 pthread_mutex_unlock (&read_lock);
277 for (i = 0; i < read_threads_num; i++)
279 if (pthread_join (read_threads[i], NULL) != 0)
281 ERROR ("plugin: stop_threads: pthread_join failed.");
283 read_threads[i] = (pthread_t) 0;
285 sfree (read_threads);
286 read_threads_num = 0;
287 } /* void stop_threads */
292 void plugin_set_dir (const char *dir)
294 if (plugindir != NULL)
299 else if ((plugindir = strdup (dir)) == NULL)
302 ERROR ("strdup failed: %s",
303 sstrerror (errno, errbuf, sizeof (errbuf)));
308 int plugin_load (const char *type)
312 char filename[BUFSIZE];
313 char typename[BUFSIZE];
319 DEBUG ("type = %s", type);
321 dir = plugin_get_dir ();
324 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
325 * type when matching the filename */
326 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
328 WARNING ("snprintf: truncated: `%s.so'", type);
331 typename_len = strlen (typename);
333 if ((dh = opendir (dir)) == NULL)
336 ERROR ("opendir (%s): %s", dir,
337 sstrerror (errno, errbuf, sizeof (errbuf)));
341 while ((de = readdir (dh)) != NULL)
343 if (strncasecmp (de->d_name, typename, typename_len))
346 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
348 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
352 if (lstat (filename, &statbuf) == -1)
355 WARNING ("stat %s: %s", filename,
356 sstrerror (errno, errbuf, sizeof (errbuf)));
359 else if (!S_ISREG (statbuf.st_mode))
361 /* don't follow symlinks */
365 if (plugin_load_file (filename) == 0)
373 fprintf (stderr, "Unable to load plugin %s.\n", type);
383 * The `register_*' functions follow
385 int plugin_register_config (const char *name,
386 int (*callback) (const char *key, const char *val),
387 const char **keys, int keys_num)
389 cf_register (name, callback, keys, keys_num);
391 } /* int plugin_register_config */
393 int plugin_register_complex_config (const char *type,
394 int (*callback) (oconfig_item_t *))
396 return (cf_register_complex (type, callback));
397 } /* int plugin_register_complex_config */
399 int plugin_register_init (const char *name,
400 int (*callback) (void))
402 return (register_callback (&list_init, name, (void *) callback));
403 } /* plugin_register_init */
405 int plugin_register_read (const char *name,
406 int (*callback) (void))
410 rf = (read_func_t *) malloc (sizeof (read_func_t));
414 ERROR ("plugin_register_read: malloc failed: %s",
415 sstrerror (errno, errbuf, sizeof (errbuf)));
419 memset (rf, '\0', sizeof (read_func_t));
420 rf->wait_time = interval_g;
422 rf->callback = callback;
423 rf->needs_read = DONE;
425 return (register_callback (&list_read, name, (void *) rf));
426 } /* int plugin_register_read */
428 int plugin_register_write (const char *name,
429 int (*callback) (const data_set_t *ds, const value_list_t *vl))
431 return (register_callback (&list_write, name, (void *) callback));
432 } /* int plugin_register_write */
434 int plugin_register_shutdown (char *name,
435 int (*callback) (void))
437 return (register_callback (&list_shutdown, name, (void *) callback));
438 } /* int plugin_register_shutdown */
440 int plugin_register_data_set (const data_set_t *ds)
445 if ((list_data_set != NULL)
446 && (llist_search (list_data_set, ds->type) != NULL))
448 NOTICE ("Replacing DS `%s' with another version.", ds->type);
449 plugin_unregister_data_set (ds->type);
452 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
455 memcpy(ds_copy, ds, sizeof (data_set_t));
457 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
459 if (ds_copy->ds == NULL)
465 for (i = 0; i < ds->ds_num; i++)
466 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
468 return (register_callback (&list_data_set, ds->type, (void *) ds_copy));
469 } /* int plugin_register_data_set */
471 int plugin_register_log (char *name,
472 void (*callback) (int priority, const char *msg))
474 return (register_callback (&list_log, name, (void *) callback));
475 } /* int plugin_register_log */
477 int plugin_register_notification (const char *name,
478 int (*callback) (const notification_t *notif))
480 return (register_callback (&list_notification, name, (void *) callback));
481 } /* int plugin_register_log */
483 int plugin_unregister_config (const char *name)
485 cf_unregister (name);
487 } /* int plugin_unregister_config */
489 int plugin_unregister_complex_config (const char *name)
491 cf_unregister_complex (name);
493 } /* int plugin_unregister_complex_config */
495 int plugin_unregister_init (const char *name)
497 return (plugin_unregister (list_init, name));
500 int plugin_unregister_read (const char *name)
504 e = llist_search (list_read, name);
509 llist_remove (list_read, e);
517 int plugin_unregister_write (const char *name)
519 return (plugin_unregister (list_write, name));
522 int plugin_unregister_shutdown (const char *name)
524 return (plugin_unregister (list_shutdown, name));
527 int plugin_unregister_data_set (const char *name)
532 if (list_data_set == NULL)
535 e = llist_search (list_data_set, name);
540 llist_remove (list_data_set, e);
541 ds = (data_set_t *) e->value;
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) || (list_data_set == NULL))
676 le = llist_search (list_data_set, name);
679 DEBUG ("No such dataset registered: %s", name);
683 ds = (data_set_t *) le->value;
685 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
687 "plugin = %s; plugin_instance = %s; "
688 "type = %s; type_instance = %s;",
689 (unsigned int) vl->time, vl->interval,
691 vl->plugin, vl->plugin_instance,
692 ds->type, vl->type_instance);
695 assert (ds->ds_num == vl->values_len);
697 if (ds->ds_num != vl->values_len)
699 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
700 "(vl->values_len = %i)",
701 ds->type, ds->ds_num, vl->values_len);
706 escape_slashes (vl->host, sizeof (vl->host));
707 escape_slashes (vl->plugin, sizeof (vl->plugin));
708 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
709 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
711 /* Update the value cache */
713 ut_check_threshold (ds, vl);
715 le = llist_head (list_write);
718 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
719 (*callback) (ds, vl);
725 } /* int plugin_dispatch_values */
727 int plugin_dispatch_notification (const notification_t *notif)
729 int (*callback) (const notification_t *);
731 /* Possible TODO: Add flap detection here */
733 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
734 "time = %u; host = %s;",
735 notif->severity, notif->message,
736 (unsigned int) notif->time, notif->host);
738 /* Nobody cares for notifications */
739 if (list_notification == NULL)
742 le = llist_head (list_notification);
745 callback = (int (*) (const notification_t *)) le->value;
752 } /* int plugin_dispatch_notification */
754 void plugin_log (int level, const char *format, ...)
759 void (*callback) (int, const char *);
762 if (list_log == NULL)
766 if (level >= LOG_DEBUG)
770 va_start (ap, format);
771 vsnprintf (msg, 512, format, ap);
775 le = llist_head (list_log);
778 callback = (void (*) (int, const char *)) le->value;
779 (*callback) (level, msg);
783 } /* void plugin_log */
785 const data_set_t *plugin_get_ds (const char *name)
790 le = llist_search (list_data_set, name);
793 DEBUG ("No such dataset registered: %s", name);
797 ds = (data_set_t *) le->value;
800 } /* data_set_t *plugin_get_ds */