2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2008 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>
20 * Sebastian Harl <sh at tokkee.org>
24 #include "utils_complain.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_cache.h"
38 #include "utils_threshold.h"
39 #include "filter_chain.h"
48 int (*callback) (void);
49 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
51 typedef struct read_func_s read_func_t;
56 static llist_t *list_init;
57 static llist_t *list_read;
58 static llist_t *list_write;
59 static llist_t *list_filter;
60 static llist_t *list_flush;
61 static llist_t *list_shutdown;
62 static llist_t *list_log;
63 static llist_t *list_notification;
65 static c_avl_tree_t *data_sets;
67 static char *plugindir = NULL;
69 static int read_loop = 1;
70 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
71 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
72 static pthread_t *read_threads = NULL;
73 static int read_threads_num = 0;
78 static const char *plugin_get_dir (void)
80 if (plugindir == NULL)
86 static int register_callback (llist_t **list, const char *name, void *callback)
92 && ((*list = llist_create ()) == NULL))
95 le = llist_search (*list, name);
102 le = llentry_create (key, callback);
109 llist_append (*list, le);
113 le->value = callback;
117 } /* int register_callback */
119 static int plugin_unregister (llist_t *list, const char *name)
123 e = llist_search (list, name);
128 llist_remove (list, e);
133 } /* int plugin_unregister */
136 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
137 * object, but it will bitch about a shared object not having a
138 * ``module_register'' symbol..
140 static int plugin_load_file (char *file)
143 void (*reg_handle) (void);
145 DEBUG ("file = %s", file);
148 lt_dlerror (); /* clear errors */
150 if ((dlh = lt_dlopen (file)) == NULL)
152 const char *error = lt_dlerror ();
154 ERROR ("lt_dlopen (%s) failed: %s", file, error);
155 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
159 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
161 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
162 file, lt_dlerror ());
172 static void *plugin_read_thread (void *args)
179 pthread_mutex_lock (&read_lock);
181 while (read_loop != 0)
183 le = llist_head (list_read);
186 while ((read_loop != 0) && (le != NULL))
188 rf = (read_func_t *) le->value;
190 if (rf->needs_read != TODO)
196 /* We will do this read function */
197 rf->needs_read = ACTIVE;
199 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
200 (unsigned long int) pthread_self (), le->key);
201 pthread_mutex_unlock (&read_lock);
203 status = rf->callback ();
208 if (rf->wait_time < interval_g)
209 rf->wait_time = interval_g;
210 rf->wait_left = rf->wait_time;
211 rf->wait_time = rf->wait_time * 2;
212 if (rf->wait_time > 86400)
213 rf->wait_time = 86400;
215 NOTICE ("read-function of plugin `%s' "
216 "failed. Will suspend it for %i "
217 "seconds.", le->key, rf->wait_left);
222 rf->wait_time = interval_g;
225 pthread_mutex_lock (&read_lock);
227 rf->needs_read = DONE;
229 } /* while (le != NULL) */
231 if ((read_loop != 0) && (done == 0))
233 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
234 (unsigned long int) pthread_self ());
235 pthread_cond_wait (&read_cond, &read_lock);
237 } /* while (read_loop) */
239 pthread_mutex_unlock (&read_lock);
243 } /* void *plugin_read_thread */
245 static void start_threads (int num)
249 if (read_threads != NULL)
252 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
253 if (read_threads == NULL)
255 ERROR ("plugin: start_threads: calloc failed.");
259 read_threads_num = 0;
260 for (i = 0; i < num; i++)
262 if (pthread_create (read_threads + read_threads_num, NULL,
263 plugin_read_thread, NULL) == 0)
269 ERROR ("plugin: start_threads: pthread_create failed.");
273 } /* void start_threads */
275 static void stop_threads (void)
279 pthread_mutex_lock (&read_lock);
281 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
282 pthread_cond_broadcast (&read_cond);
283 pthread_mutex_unlock (&read_lock);
285 for (i = 0; i < read_threads_num; i++)
287 if (pthread_join (read_threads[i], NULL) != 0)
289 ERROR ("plugin: stop_threads: pthread_join failed.");
291 read_threads[i] = (pthread_t) 0;
293 sfree (read_threads);
294 read_threads_num = 0;
295 } /* void stop_threads */
300 void plugin_set_dir (const char *dir)
302 if (plugindir != NULL)
307 else if ((plugindir = strdup (dir)) == NULL)
310 ERROR ("strdup failed: %s",
311 sstrerror (errno, errbuf, sizeof (errbuf)));
316 int plugin_load (const char *type)
320 char filename[BUFSIZE] = "";
321 char typename[BUFSIZE];
327 DEBUG ("type = %s", type);
329 dir = plugin_get_dir ();
332 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
333 * type when matching the filename */
334 if (ssnprintf (typename, sizeof (typename),
335 "%s.so", type) >= sizeof (typename))
337 WARNING ("snprintf: truncated: `%s.so'", type);
340 typename_len = strlen (typename);
342 if ((dh = opendir (dir)) == NULL)
345 ERROR ("opendir (%s): %s", dir,
346 sstrerror (errno, errbuf, sizeof (errbuf)));
350 while ((de = readdir (dh)) != NULL)
352 if (strncasecmp (de->d_name, typename, typename_len))
355 if (ssnprintf (filename, sizeof (filename),
356 "%s/%s", dir, de->d_name) >= sizeof (filename))
358 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
362 if (lstat (filename, &statbuf) == -1)
365 WARNING ("stat %s: %s", filename,
366 sstrerror (errno, errbuf, sizeof (errbuf)));
369 else if (!S_ISREG (statbuf.st_mode))
371 /* don't follow symlinks */
375 if (plugin_load_file (filename) == 0)
383 fprintf (stderr, "Unable to load plugin %s.\n", type);
389 if (filename[0] == '\0')
390 fprintf (stderr, "Could not find plugin %s.\n", type);
396 * The `register_*' functions follow
398 int plugin_register_config (const char *name,
399 int (*callback) (const char *key, const char *val),
400 const char **keys, int keys_num)
402 cf_register (name, callback, keys, keys_num);
404 } /* int plugin_register_config */
406 int plugin_register_complex_config (const char *type,
407 int (*callback) (oconfig_item_t *))
409 return (cf_register_complex (type, callback));
410 } /* int plugin_register_complex_config */
412 int plugin_register_init (const char *name,
413 int (*callback) (void))
415 return (register_callback (&list_init, name, (void *) callback));
416 } /* plugin_register_init */
418 int plugin_register_read (const char *name,
419 int (*callback) (void))
423 rf = (read_func_t *) malloc (sizeof (read_func_t));
427 ERROR ("plugin_register_read: malloc failed: %s",
428 sstrerror (errno, errbuf, sizeof (errbuf)));
432 memset (rf, '\0', sizeof (read_func_t));
433 rf->wait_time = interval_g;
435 rf->callback = callback;
436 rf->needs_read = DONE;
438 return (register_callback (&list_read, name, (void *) rf));
439 } /* int plugin_register_read */
441 int plugin_register_write (const char *name,
442 int (*callback) (const data_set_t *ds, const value_list_t *vl))
444 return (register_callback (&list_write, name, (void *) callback));
445 } /* int plugin_register_write */
447 int plugin_register_filter (const char *name,
448 int (*callback) (const data_set_t *ds, value_list_t *vl))
450 return (register_callback (&list_filter, name, (void *) callback));
451 } /* int plugin_register_filter */
453 int plugin_register_flush (const char *name,
454 int (*callback) (const int timeout, const char *identifier))
456 return (register_callback (&list_flush, name, (void *) callback));
457 } /* int plugin_register_flush */
459 int plugin_register_shutdown (char *name,
460 int (*callback) (void))
462 return (register_callback (&list_shutdown, name, (void *) callback));
463 } /* int plugin_register_shutdown */
465 int plugin_register_data_set (const data_set_t *ds)
470 if ((data_sets != NULL)
471 && (c_avl_get (data_sets, ds->type, NULL) == 0))
473 NOTICE ("Replacing DS `%s' with another version.", ds->type);
474 plugin_unregister_data_set (ds->type);
476 else if (data_sets == NULL)
478 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
479 if (data_sets == NULL)
483 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
486 memcpy(ds_copy, ds, sizeof (data_set_t));
488 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
490 if (ds_copy->ds == NULL)
496 for (i = 0; i < ds->ds_num; i++)
497 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
499 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
500 } /* int plugin_register_data_set */
502 int plugin_register_log (char *name,
503 void (*callback) (int priority, const char *msg))
505 return (register_callback (&list_log, name, (void *) callback));
506 } /* int plugin_register_log */
508 int plugin_register_notification (const char *name,
509 int (*callback) (const notification_t *notif))
511 return (register_callback (&list_notification, name, (void *) callback));
512 } /* int plugin_register_log */
514 int plugin_unregister_config (const char *name)
516 cf_unregister (name);
518 } /* int plugin_unregister_config */
520 int plugin_unregister_complex_config (const char *name)
522 cf_unregister_complex (name);
524 } /* int plugin_unregister_complex_config */
526 int plugin_unregister_init (const char *name)
528 return (plugin_unregister (list_init, name));
531 int plugin_unregister_read (const char *name)
535 e = llist_search (list_read, name);
540 llist_remove (list_read, e);
548 int plugin_unregister_write (const char *name)
550 return (plugin_unregister (list_write, name));
553 int plugin_unregister_filter (const char *name)
555 return (plugin_unregister (list_filter, name));
558 int plugin_unregister_flush (const char *name)
560 return (plugin_unregister (list_flush, name));
563 int plugin_unregister_shutdown (const char *name)
565 return (plugin_unregister (list_shutdown, name));
568 int plugin_unregister_data_set (const char *name)
572 if (data_sets == NULL)
575 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
582 } /* int plugin_unregister_data_set */
584 int plugin_unregister_log (const char *name)
586 return (plugin_unregister (list_log, name));
589 int plugin_unregister_notification (const char *name)
591 return (plugin_unregister (list_notification, name));
594 void plugin_init_all (void)
596 int (*callback) (void);
600 /* Init the value cache */
603 if ((list_init == NULL) && (list_read == NULL))
606 /* Calling all init callbacks before checking if read callbacks
607 * are available allows the init callbacks to register the read
609 le = llist_head (list_init);
612 callback = (int (*) (void)) le->value;
613 status = (*callback) ();
617 ERROR ("Initialization of plugin `%s' "
618 "failed with status %i. "
619 "Plugin will be unloaded.",
621 /* Plugins that register read callbacks from the init
622 * callback should take care of appropriate error
623 * handling themselves. */
624 /* FIXME: Unload _all_ functions */
625 plugin_unregister_read (le->key);
631 /* Start read-threads */
632 if (list_read != NULL)
636 rt = global_option_get ("ReadThreads");
638 start_threads ((num > 0) ? num : 5);
640 } /* void plugin_init_all */
642 void plugin_read_all (void)
649 if (list_read == NULL)
652 pthread_mutex_lock (&read_lock);
654 le = llist_head (list_read);
657 rf = (read_func_t *) le->value;
659 if (rf->needs_read != DONE)
665 if (rf->wait_left > 0)
666 rf->wait_left -= interval_g;
668 if (rf->wait_left <= 0)
670 rf->needs_read = TODO;
676 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
677 pthread_cond_broadcast (&read_cond);
678 pthread_mutex_unlock (&read_lock);
679 } /* void plugin_read_all */
681 int plugin_write (const char *plugin, /* {{{ */
682 const data_set_t *ds, const value_list_t *vl)
684 int (*callback) (const data_set_t *ds, const value_list_t *vl);
691 if (list_write == NULL)
696 ds = plugin_get_ds (vl->type);
699 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
709 le = llist_head (list_write);
712 callback = le->value;
713 status = (*callback) (ds, vl);
722 if ((success == 0) && (failure != 0))
727 else /* plugin != NULL */
729 le = llist_head (list_write);
732 if (strcasecmp (plugin, le->key) == 0)
741 callback = le->value;
742 status = (*callback) (ds, vl);
746 } /* }}} int plugin_write */
748 int plugin_flush (const char *plugin, int timeout, const char *identifier)
750 int (*callback) (int timeout, const char *identifier);
753 if (list_flush == NULL)
756 le = llist_head (list_flush);
760 && (strcmp (plugin, le->key) != 0))
766 callback = (int (*) (int, const char *)) le->value;
767 (*callback) (timeout, identifier);
772 } /* int plugin_flush */
774 void plugin_shutdown_all (void)
776 int (*callback) (void);
781 if (list_shutdown == NULL)
784 le = llist_head (list_shutdown);
787 callback = (int (*) (void)) le->value;
789 /* Advance the pointer before calling the callback allows
790 * shutdown functions to unregister themselves. If done the
791 * other way around the memory `le' points to will be freed
792 * after callback returns. */
797 } /* void plugin_shutdown_all */
799 int plugin_dispatch_values (value_list_t *vl)
801 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
805 if ((vl == NULL) || (*vl->type == '\0')) {
806 ERROR ("plugin_dispatch_values: Invalid value list.");
810 if (list_write == NULL)
811 c_complain_once (LOG_WARNING, &no_write_complaint,
812 "plugin_dispatch_values: No write callback has been "
813 "registered. Please load at least one output plugin, "
814 "if you want the collected data to be stored.");
816 if (data_sets == NULL)
818 ERROR ("plugin_dispatch_values: No data sets registered. "
819 "Could the types database be read? Check "
820 "your `TypesDB' setting!");
824 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
826 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
830 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
832 "plugin = %s; plugin_instance = %s; "
833 "type = %s; type_instance = %s;",
834 (unsigned int) vl->time, vl->interval,
836 vl->plugin, vl->plugin_instance,
837 vl->type, vl->type_instance);
840 assert (0 == strcmp (ds->type, vl->type));
842 if (0 != strcmp (ds->type, vl->type))
843 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
848 assert (ds->ds_num == vl->values_len);
850 if (ds->ds_num != vl->values_len)
852 ERROR ("plugin_dispatch_values: ds->type = %s: "
853 "(ds->ds_num = %i) != "
854 "(vl->values_len = %i)",
855 ds->type, ds->ds_num, vl->values_len);
860 escape_slashes (vl->host, sizeof (vl->host));
861 escape_slashes (vl->plugin, sizeof (vl->plugin));
862 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
863 escape_slashes (vl->type, sizeof (vl->type));
864 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
866 /* Update the value cache */
872 } /* int plugin_dispatch_values */
874 int plugin_dispatch_notification (const notification_t *notif)
876 int (*callback) (const notification_t *);
878 /* Possible TODO: Add flap detection here */
880 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
881 "time = %u; host = %s;",
882 notif->severity, notif->message,
883 (unsigned int) notif->time, notif->host);
885 /* Nobody cares for notifications */
886 if (list_notification == NULL)
889 le = llist_head (list_notification);
892 callback = (int (*) (const notification_t *)) le->value;
899 } /* int plugin_dispatch_notification */
901 void plugin_log (int level, const char *format, ...)
906 void (*callback) (int, const char *);
909 if (list_log == NULL)
913 if (level >= LOG_DEBUG)
917 va_start (ap, format);
918 vsnprintf (msg, sizeof (msg), format, ap);
919 msg[sizeof (msg) - 1] = '\0';
922 le = llist_head (list_log);
925 callback = (void (*) (int, const char *)) le->value;
926 (*callback) (level, msg);
930 } /* void plugin_log */
932 const data_set_t *plugin_get_ds (const char *name)
936 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
938 DEBUG ("No such dataset registered: %s", name);
943 } /* data_set_t *plugin_get_ds */
945 static int plugin_notification_meta_add (notification_t *n,
947 enum notification_meta_type_e type,
950 notification_meta_t *meta;
951 notification_meta_t *tail;
953 if ((n == NULL) || (name == NULL) || (value == NULL))
955 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
959 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
962 ERROR ("plugin_notification_meta_add: malloc failed.");
965 memset (meta, 0, sizeof (notification_meta_t));
967 sstrncpy (meta->name, name, sizeof (meta->name));
974 meta->value_string = strdup ((const char *) value);
975 if (meta->value_string == NULL)
977 ERROR ("plugin_notification_meta_add: strdup failed.");
983 case NM_TYPE_SIGNED_INT:
985 meta->value_signed_int = *((int64_t *) value);
988 case NM_TYPE_UNSIGNED_INT:
990 meta->value_unsigned_int = *((uint64_t *) value);
995 meta->value_double = *((double *) value);
998 case NM_TYPE_BOOLEAN:
1000 meta->value_boolean = *((bool *) value);
1005 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1009 } /* switch (type) */
1013 while ((tail != NULL) && (tail->next != NULL))
1022 } /* int plugin_notification_meta_add */
1024 int plugin_notification_meta_add_string (notification_t *n,
1028 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1031 int plugin_notification_meta_add_signed_int (notification_t *n,
1035 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1038 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1042 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1045 int plugin_notification_meta_add_double (notification_t *n,
1049 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1052 int plugin_notification_meta_add_boolean (notification_t *n,
1056 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1059 int plugin_notification_meta_copy (notification_t *dst,
1060 const notification_t *src)
1062 notification_meta_t *meta;
1064 assert (dst != NULL);
1065 assert (src != NULL);
1066 assert (dst != src);
1067 assert ((src->meta == NULL) || (src->meta != dst->meta));
1069 for (meta = src->meta; meta != NULL; meta = meta->next)
1071 if (meta->type == NM_TYPE_STRING)
1072 plugin_notification_meta_add_string (dst, meta->name,
1073 meta->value_string);
1074 else if (meta->type == NM_TYPE_SIGNED_INT)
1075 plugin_notification_meta_add_signed_int (dst, meta->name,
1076 meta->value_signed_int);
1077 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1078 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1079 meta->value_unsigned_int);
1080 else if (meta->type == NM_TYPE_DOUBLE)
1081 plugin_notification_meta_add_double (dst, meta->name,
1082 meta->value_double);
1083 else if (meta->type == NM_TYPE_BOOLEAN)
1084 plugin_notification_meta_add_boolean (dst, meta->name,
1085 meta->value_boolean);
1089 } /* int plugin_notification_meta_copy */
1091 int plugin_notification_meta_free (notification_t *n)
1093 notification_meta_t *this;
1094 notification_meta_t *next;
1098 ERROR ("plugin_notification_meta_free: n == NULL!");
1104 while (this != NULL)
1108 if (this->type == NM_TYPE_STRING)
1110 free ((char *)this->value_string);
1111 this->value_string = NULL;
1119 } /* int plugin_notification_meta_free */