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_flush;
57 static llist_t *list_shutdown;
58 static llist_t *list_log;
59 static llist_t *list_notification;
61 static c_avl_tree_t *data_sets;
63 static char *plugindir = NULL;
65 static int read_loop = 1;
66 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
67 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
68 static pthread_t *read_threads = NULL;
69 static int read_threads_num = 0;
74 static const char *plugin_get_dir (void)
76 if (plugindir == NULL)
82 static int register_callback (llist_t **list, const char *name, void *callback)
88 && ((*list = llist_create ()) == NULL))
91 le = llist_search (*list, name);
98 le = llentry_create (key, callback);
105 llist_append (*list, le);
109 le->value = callback;
113 } /* int register_callback */
115 static int plugin_unregister (llist_t *list, const char *name)
119 e = llist_search (list, name);
124 llist_remove (list, e);
129 } /* int plugin_unregister */
132 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
133 * object, but it will bitch about a shared object not having a
134 * ``module_register'' symbol..
136 static int plugin_load_file (char *file)
139 void (*reg_handle) (void);
141 DEBUG ("file = %s", file);
144 lt_dlerror (); /* clear errors */
146 if ((dlh = lt_dlopen (file)) == NULL)
148 const char *error = lt_dlerror ();
150 ERROR ("lt_dlopen failed: %s", error);
151 fprintf (stderr, "lt_dlopen failed: %s\n", error);
155 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
157 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
158 file, lt_dlerror ());
168 static void *plugin_read_thread (void *args)
175 pthread_mutex_lock (&read_lock);
177 while (read_loop != 0)
179 le = llist_head (list_read);
182 while ((read_loop != 0) && (le != NULL))
184 rf = (read_func_t *) le->value;
186 if (rf->needs_read != TODO)
192 /* We will do this read function */
193 rf->needs_read = ACTIVE;
195 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
196 (unsigned long int) pthread_self (), le->key);
197 pthread_mutex_unlock (&read_lock);
199 status = rf->callback ();
204 if (rf->wait_time < interval_g)
205 rf->wait_time = interval_g;
206 rf->wait_left = rf->wait_time;
207 rf->wait_time = rf->wait_time * 2;
208 if (rf->wait_time > 86400)
209 rf->wait_time = 86400;
211 NOTICE ("read-function of plugin `%s' "
212 "failed. Will suspend it for %i "
213 "seconds.", le->key, rf->wait_left);
218 rf->wait_time = interval_g;
221 pthread_mutex_lock (&read_lock);
223 rf->needs_read = DONE;
225 } /* while (le != NULL) */
227 if ((read_loop != 0) && (done == 0))
229 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
230 (unsigned long int) pthread_self ());
231 pthread_cond_wait (&read_cond, &read_lock);
233 } /* while (read_loop) */
235 pthread_mutex_unlock (&read_lock);
238 } /* void *plugin_read_thread */
240 static void start_threads (int num)
244 if (read_threads != NULL)
247 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
248 if (read_threads == NULL)
250 ERROR ("plugin: start_threads: calloc failed.");
254 read_threads_num = 0;
255 for (i = 0; i < num; i++)
257 if (pthread_create (read_threads + read_threads_num, NULL,
258 plugin_read_thread, NULL) == 0)
264 ERROR ("plugin: start_threads: pthread_create failed.");
268 } /* void start_threads */
270 static void stop_threads (void)
274 pthread_mutex_lock (&read_lock);
276 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
277 pthread_cond_broadcast (&read_cond);
278 pthread_mutex_unlock (&read_lock);
280 for (i = 0; i < read_threads_num; i++)
282 if (pthread_join (read_threads[i], NULL) != 0)
284 ERROR ("plugin: stop_threads: pthread_join failed.");
286 read_threads[i] = (pthread_t) 0;
288 sfree (read_threads);
289 read_threads_num = 0;
290 } /* void stop_threads */
295 void plugin_set_dir (const char *dir)
297 if (plugindir != NULL)
302 else if ((plugindir = strdup (dir)) == NULL)
305 ERROR ("strdup failed: %s",
306 sstrerror (errno, errbuf, sizeof (errbuf)));
311 int plugin_load (const char *type)
315 char filename[BUFSIZE];
316 char typename[BUFSIZE];
322 DEBUG ("type = %s", type);
324 dir = plugin_get_dir ();
327 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
328 * type when matching the filename */
329 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
331 WARNING ("snprintf: truncated: `%s.so'", type);
334 typename_len = strlen (typename);
336 if ((dh = opendir (dir)) == NULL)
339 ERROR ("opendir (%s): %s", dir,
340 sstrerror (errno, errbuf, sizeof (errbuf)));
344 while ((de = readdir (dh)) != NULL)
346 if (strncasecmp (de->d_name, typename, typename_len))
349 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
351 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
355 if (lstat (filename, &statbuf) == -1)
358 WARNING ("stat %s: %s", filename,
359 sstrerror (errno, errbuf, sizeof (errbuf)));
362 else if (!S_ISREG (statbuf.st_mode))
364 /* don't follow symlinks */
368 if (plugin_load_file (filename) == 0)
376 fprintf (stderr, "Unable to load plugin %s.\n", type);
386 * The `register_*' functions follow
388 int plugin_register_config (const char *name,
389 int (*callback) (const char *key, const char *val),
390 const char **keys, int keys_num)
392 cf_register (name, callback, keys, keys_num);
394 } /* int plugin_register_config */
396 int plugin_register_complex_config (const char *type,
397 int (*callback) (oconfig_item_t *))
399 return (cf_register_complex (type, callback));
400 } /* int plugin_register_complex_config */
402 int plugin_register_init (const char *name,
403 int (*callback) (void))
405 return (register_callback (&list_init, name, (void *) callback));
406 } /* plugin_register_init */
408 int plugin_register_read (const char *name,
409 int (*callback) (void))
413 rf = (read_func_t *) malloc (sizeof (read_func_t));
417 ERROR ("plugin_register_read: malloc failed: %s",
418 sstrerror (errno, errbuf, sizeof (errbuf)));
422 memset (rf, '\0', sizeof (read_func_t));
423 rf->wait_time = interval_g;
425 rf->callback = callback;
426 rf->needs_read = DONE;
428 return (register_callback (&list_read, name, (void *) rf));
429 } /* int plugin_register_read */
431 int plugin_register_write (const char *name,
432 int (*callback) (const data_set_t *ds, const value_list_t *vl))
434 return (register_callback (&list_write, name, (void *) callback));
435 } /* int plugin_register_write */
437 int plugin_register_flush (const char *name, int (*callback) (const int))
439 return (register_callback (&list_flush, name, (void *) callback));
440 } /* int plugin_register_flush */
442 int plugin_register_shutdown (char *name,
443 int (*callback) (void))
445 return (register_callback (&list_shutdown, name, (void *) callback));
446 } /* int plugin_register_shutdown */
448 int plugin_register_data_set (const data_set_t *ds)
453 if ((data_sets != NULL)
454 && (c_avl_get (data_sets, ds->type, NULL) == 0))
456 NOTICE ("Replacing DS `%s' with another version.", ds->type);
457 plugin_unregister_data_set (ds->type);
459 else if (data_sets == NULL)
461 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
462 if (data_sets == NULL)
466 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
469 memcpy(ds_copy, ds, sizeof (data_set_t));
471 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
473 if (ds_copy->ds == NULL)
479 for (i = 0; i < ds->ds_num; i++)
480 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
482 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
483 } /* int plugin_register_data_set */
485 int plugin_register_log (char *name,
486 void (*callback) (int priority, const char *msg))
488 return (register_callback (&list_log, name, (void *) callback));
489 } /* int plugin_register_log */
491 int plugin_register_notification (const char *name,
492 int (*callback) (const notification_t *notif))
494 return (register_callback (&list_notification, name, (void *) callback));
495 } /* int plugin_register_log */
497 int plugin_unregister_config (const char *name)
499 cf_unregister (name);
501 } /* int plugin_unregister_config */
503 int plugin_unregister_complex_config (const char *name)
505 cf_unregister_complex (name);
507 } /* int plugin_unregister_complex_config */
509 int plugin_unregister_init (const char *name)
511 return (plugin_unregister (list_init, name));
514 int plugin_unregister_read (const char *name)
518 e = llist_search (list_read, name);
523 llist_remove (list_read, e);
531 int plugin_unregister_write (const char *name)
533 return (plugin_unregister (list_write, name));
536 int plugin_unregister_flush (const char *name)
538 return (plugin_unregister (list_flush, name));
541 int plugin_unregister_shutdown (const char *name)
543 return (plugin_unregister (list_shutdown, name));
546 int plugin_unregister_data_set (const char *name)
550 if (data_sets == NULL)
553 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
560 } /* int plugin_unregister_data_set */
562 int plugin_unregister_log (const char *name)
564 return (plugin_unregister (list_log, name));
567 int plugin_unregister_notification (const char *name)
569 return (plugin_unregister (list_notification, name));
572 void plugin_init_all (void)
574 int (*callback) (void);
578 /* Start read-threads */
579 if (list_read != NULL)
583 rt = global_option_get ("ReadThreads");
585 start_threads ((num > 0) ? num : 5);
588 /* Init the value cache */
591 if (list_init == NULL)
594 le = llist_head (list_init);
597 callback = (int (*) (void)) le->value;
598 status = (*callback) ();
602 ERROR ("Initialization of plugin `%s' "
603 "failed with status %i. "
604 "Plugin will be unloaded.",
606 /* FIXME: Unload _all_ functions */
607 plugin_unregister_read (le->key);
612 } /* void plugin_init_all */
614 void plugin_read_all (void)
621 if (list_read == NULL)
624 pthread_mutex_lock (&read_lock);
626 le = llist_head (list_read);
629 rf = (read_func_t *) le->value;
631 if (rf->needs_read != DONE)
637 if (rf->wait_left > 0)
638 rf->wait_left -= interval_g;
640 if (rf->wait_left <= 0)
642 rf->needs_read = TODO;
648 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
649 pthread_cond_broadcast (&read_cond);
650 pthread_mutex_unlock (&read_lock);
651 } /* void plugin_read_all */
653 void plugin_flush_all (int timeout)
655 int (*callback) (int);
658 if (list_flush == NULL)
661 le = llist_head (list_flush);
664 callback = (int (*) (int)) le->value;
667 (*callback) (timeout);
669 } /* void plugin_flush_all */
671 void plugin_shutdown_all (void)
673 int (*callback) (void);
678 if (list_shutdown == NULL)
681 le = llist_head (list_shutdown);
684 callback = (int (*) (void)) le->value;
686 /* Advance the pointer before calling the callback allows
687 * shutdown functions to unregister themselves. If done the
688 * other way around the memory `le' points to will be freed
689 * after callback returns. */
694 } /* void plugin_shutdown_all */
696 int plugin_dispatch_values (const char *name, value_list_t *vl)
698 int (*callback) (const data_set_t *, const value_list_t *);
702 if ((list_write == NULL) || (data_sets == NULL))
705 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
707 DEBUG ("No such dataset registered: %s", name);
711 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
713 "plugin = %s; plugin_instance = %s; "
714 "type = %s; type_instance = %s;",
715 (unsigned int) vl->time, vl->interval,
717 vl->plugin, vl->plugin_instance,
718 ds->type, vl->type_instance);
721 assert (ds->ds_num == vl->values_len);
723 if (ds->ds_num != vl->values_len)
725 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
726 "(vl->values_len = %i)",
727 ds->type, ds->ds_num, vl->values_len);
732 escape_slashes (vl->host, sizeof (vl->host));
733 escape_slashes (vl->plugin, sizeof (vl->plugin));
734 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
735 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
737 /* Update the value cache */
739 ut_check_threshold (ds, vl);
741 le = llist_head (list_write);
744 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
745 (*callback) (ds, vl);
751 } /* int plugin_dispatch_values */
753 int plugin_dispatch_notification (const notification_t *notif)
755 int (*callback) (const notification_t *);
757 /* Possible TODO: Add flap detection here */
759 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
760 "time = %u; host = %s;",
761 notif->severity, notif->message,
762 (unsigned int) notif->time, notif->host);
764 /* Nobody cares for notifications */
765 if (list_notification == NULL)
768 le = llist_head (list_notification);
771 callback = (int (*) (const notification_t *)) le->value;
778 } /* int plugin_dispatch_notification */
780 void plugin_log (int level, const char *format, ...)
785 void (*callback) (int, const char *);
788 if (list_log == NULL)
792 if (level >= LOG_DEBUG)
796 va_start (ap, format);
797 vsnprintf (msg, 512, format, ap);
801 le = llist_head (list_log);
804 callback = (void (*) (int, const char *)) le->value;
805 (*callback) (level, msg);
809 } /* void plugin_log */
811 const data_set_t *plugin_get_ds (const char *name)
815 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
817 DEBUG ("No such dataset registered: %s", name);
822 } /* data_set_t *plugin_get_ds */