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);
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);
382 if (filename[0] == '\0')
383 fprintf (stderr, "Could not find plugin %s.\n", type);
389 * The `register_*' functions follow
391 int plugin_register_config (const char *name,
392 int (*callback) (const char *key, const char *val),
393 const char **keys, int keys_num)
395 cf_register (name, callback, keys, keys_num);
397 } /* int plugin_register_config */
399 int plugin_register_complex_config (const char *type,
400 int (*callback) (oconfig_item_t *))
402 return (cf_register_complex (type, callback));
403 } /* int plugin_register_complex_config */
405 int plugin_register_init (const char *name,
406 int (*callback) (void))
408 return (register_callback (&list_init, name, (void *) callback));
409 } /* plugin_register_init */
411 int plugin_register_read (const char *name,
412 int (*callback) (void))
416 rf = (read_func_t *) malloc (sizeof (read_func_t));
420 ERROR ("plugin_register_read: malloc failed: %s",
421 sstrerror (errno, errbuf, sizeof (errbuf)));
425 memset (rf, '\0', sizeof (read_func_t));
426 rf->wait_time = interval_g;
428 rf->callback = callback;
429 rf->needs_read = DONE;
431 return (register_callback (&list_read, name, (void *) rf));
432 } /* int plugin_register_read */
434 int plugin_register_write (const char *name,
435 int (*callback) (const data_set_t *ds, const value_list_t *vl))
437 return (register_callback (&list_write, name, (void *) callback));
438 } /* int plugin_register_write */
440 int plugin_register_shutdown (char *name,
441 int (*callback) (void))
443 return (register_callback (&list_shutdown, name, (void *) callback));
444 } /* int plugin_register_shutdown */
446 int plugin_register_data_set (const data_set_t *ds)
451 if ((data_sets != NULL)
452 && (c_avl_get (data_sets, ds->type, NULL) == 0))
454 NOTICE ("Replacing DS `%s' with another version.", ds->type);
455 plugin_unregister_data_set (ds->type);
457 else if (data_sets == NULL)
459 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
460 if (data_sets == NULL)
464 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
467 memcpy(ds_copy, ds, sizeof (data_set_t));
469 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
471 if (ds_copy->ds == NULL)
477 for (i = 0; i < ds->ds_num; i++)
478 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
480 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
481 } /* int plugin_register_data_set */
483 int plugin_register_log (char *name,
484 void (*callback) (int priority, const char *msg))
486 return (register_callback (&list_log, name, (void *) callback));
487 } /* int plugin_register_log */
489 int plugin_register_notification (const char *name,
490 int (*callback) (const notification_t *notif))
492 return (register_callback (&list_notification, name, (void *) callback));
493 } /* int plugin_register_log */
495 int plugin_unregister_config (const char *name)
497 cf_unregister (name);
499 } /* int plugin_unregister_config */
501 int plugin_unregister_complex_config (const char *name)
503 cf_unregister_complex (name);
505 } /* int plugin_unregister_complex_config */
507 int plugin_unregister_init (const char *name)
509 return (plugin_unregister (list_init, name));
512 int plugin_unregister_read (const char *name)
516 e = llist_search (list_read, name);
521 llist_remove (list_read, e);
529 int plugin_unregister_write (const char *name)
531 return (plugin_unregister (list_write, name));
534 int plugin_unregister_shutdown (const char *name)
536 return (plugin_unregister (list_shutdown, name));
539 int plugin_unregister_data_set (const char *name)
543 if (data_sets == NULL)
546 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
553 } /* int plugin_unregister_data_set */
555 int plugin_unregister_log (const char *name)
557 return (plugin_unregister (list_log, name));
560 int plugin_unregister_notification (const char *name)
562 return (plugin_unregister (list_notification, name));
565 void plugin_init_all (void)
567 int (*callback) (void);
571 /* Start read-threads */
572 if (list_read != NULL)
576 rt = global_option_get ("ReadThreads");
578 start_threads ((num > 0) ? num : 5);
581 /* Init the value cache */
584 if (list_init == NULL)
587 le = llist_head (list_init);
590 callback = (int (*) (void)) le->value;
591 status = (*callback) ();
595 ERROR ("Initialization of plugin `%s' "
596 "failed with status %i. "
597 "Plugin will be unloaded.",
599 /* FIXME: Unload _all_ functions */
600 plugin_unregister_read (le->key);
605 } /* void plugin_init_all */
607 void plugin_read_all (void)
614 if (list_read == NULL)
617 pthread_mutex_lock (&read_lock);
619 le = llist_head (list_read);
622 rf = (read_func_t *) le->value;
624 if (rf->needs_read != DONE)
630 if (rf->wait_left > 0)
631 rf->wait_left -= interval_g;
633 if (rf->wait_left <= 0)
635 rf->needs_read = TODO;
641 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
642 pthread_cond_broadcast (&read_cond);
643 pthread_mutex_unlock (&read_lock);
644 } /* void plugin_read_all */
646 void plugin_shutdown_all (void)
648 int (*callback) (void);
653 if (list_shutdown == NULL)
656 le = llist_head (list_shutdown);
659 callback = (int (*) (void)) le->value;
661 /* Advance the pointer before calling the callback allows
662 * shutdown functions to unregister themselves. If done the
663 * other way around the memory `le' points to will be freed
664 * after callback returns. */
669 } /* void plugin_shutdown_all */
671 int plugin_dispatch_values (const char *name, value_list_t *vl)
673 int (*callback) (const data_set_t *, const value_list_t *);
677 if (list_write == NULL)
679 ERROR ("plugin_dispatch_values: No write callback has been "
680 "registered. Please load at least one plugin "
681 "that provides a write function.");
685 if (data_sets == NULL)
687 ERROR ("plugin_dispatch_values: No data sets registered. "
688 "Could the types database be read? Check "
689 "your `TypesDB' setting!");
693 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
695 INFO ("plugin_dispatch_values: Dataset not found: %s", name);
699 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
701 "plugin = %s; plugin_instance = %s; "
702 "type = %s; type_instance = %s;",
703 (unsigned int) vl->time, vl->interval,
705 vl->plugin, vl->plugin_instance,
706 ds->type, vl->type_instance);
709 assert (ds->ds_num == vl->values_len);
711 if (ds->ds_num != vl->values_len)
713 ERROR ("plugin_dispatch_values: ds->type = %s: "
714 "(ds->ds_num = %i) != "
715 "(vl->values_len = %i)",
716 ds->type, ds->ds_num, vl->values_len);
721 escape_slashes (vl->host, sizeof (vl->host));
722 escape_slashes (vl->plugin, sizeof (vl->plugin));
723 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
724 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
726 /* Update the value cache */
728 ut_check_threshold (ds, vl);
730 le = llist_head (list_write);
733 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
734 (*callback) (ds, vl);
740 } /* int plugin_dispatch_values */
742 int plugin_dispatch_notification (const notification_t *notif)
744 int (*callback) (const notification_t *);
746 /* Possible TODO: Add flap detection here */
748 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
749 "time = %u; host = %s;",
750 notif->severity, notif->message,
751 (unsigned int) notif->time, notif->host);
753 /* Nobody cares for notifications */
754 if (list_notification == NULL)
757 le = llist_head (list_notification);
760 callback = (int (*) (const notification_t *)) le->value;
767 } /* int plugin_dispatch_notification */
769 void plugin_log (int level, const char *format, ...)
774 void (*callback) (int, const char *);
777 if (list_log == NULL)
781 if (level >= LOG_DEBUG)
785 va_start (ap, format);
786 vsnprintf (msg, 512, format, ap);
790 le = llist_head (list_log);
793 callback = (void (*) (int, const char *)) le->value;
794 (*callback) (level, msg);
798 } /* void plugin_log */
800 const data_set_t *plugin_get_ds (const char *name)
804 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
806 DEBUG ("No such dataset registered: %s", name);
811 } /* data_set_t *plugin_get_ds */