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"
47 int (*callback) (void);
48 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
50 typedef struct read_func_s read_func_t;
55 static llist_t *list_init;
56 static llist_t *list_read;
57 static llist_t *list_write;
58 static llist_t *list_filter;
59 static llist_t *list_flush;
60 static llist_t *list_shutdown;
61 static llist_t *list_log;
62 static llist_t *list_notification;
64 static c_avl_tree_t *data_sets;
66 static char *plugindir = NULL;
68 static int read_loop = 1;
69 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
70 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
71 static pthread_t *read_threads = NULL;
72 static int read_threads_num = 0;
77 static const char *plugin_get_dir (void)
79 if (plugindir == NULL)
85 static int register_callback (llist_t **list, const char *name, void *callback)
91 && ((*list = llist_create ()) == NULL))
94 le = llist_search (*list, name);
101 le = llentry_create (key, callback);
108 llist_append (*list, le);
112 le->value = callback;
116 } /* int register_callback */
118 static int plugin_unregister (llist_t *list, const char *name)
122 e = llist_search (list, name);
127 llist_remove (list, e);
132 } /* int plugin_unregister */
135 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
136 * object, but it will bitch about a shared object not having a
137 * ``module_register'' symbol..
139 static int plugin_load_file (char *file)
142 void (*reg_handle) (void);
144 DEBUG ("file = %s", file);
147 lt_dlerror (); /* clear errors */
149 if ((dlh = lt_dlopen (file)) == NULL)
151 const char *error = lt_dlerror ();
153 ERROR ("lt_dlopen (%s) failed: %s", file, error);
154 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
158 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
160 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
161 file, lt_dlerror ());
171 static void *plugin_read_thread (void *args)
178 pthread_mutex_lock (&read_lock);
180 while (read_loop != 0)
182 le = llist_head (list_read);
185 while ((read_loop != 0) && (le != NULL))
187 rf = (read_func_t *) le->value;
189 if (rf->needs_read != TODO)
195 /* We will do this read function */
196 rf->needs_read = ACTIVE;
198 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
199 (unsigned long int) pthread_self (), le->key);
200 pthread_mutex_unlock (&read_lock);
202 status = rf->callback ();
207 if (rf->wait_time < interval_g)
208 rf->wait_time = interval_g;
209 rf->wait_left = rf->wait_time;
210 rf->wait_time = rf->wait_time * 2;
211 if (rf->wait_time > 86400)
212 rf->wait_time = 86400;
214 NOTICE ("read-function of plugin `%s' "
215 "failed. Will suspend it for %i "
216 "seconds.", le->key, rf->wait_left);
221 rf->wait_time = interval_g;
224 pthread_mutex_lock (&read_lock);
226 rf->needs_read = DONE;
228 } /* while (le != NULL) */
230 if ((read_loop != 0) && (done == 0))
232 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
233 (unsigned long int) pthread_self ());
234 pthread_cond_wait (&read_cond, &read_lock);
236 } /* while (read_loop) */
238 pthread_mutex_unlock (&read_lock);
242 } /* void *plugin_read_thread */
244 static void start_threads (int num)
248 if (read_threads != NULL)
251 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
252 if (read_threads == NULL)
254 ERROR ("plugin: start_threads: calloc failed.");
258 read_threads_num = 0;
259 for (i = 0; i < num; i++)
261 if (pthread_create (read_threads + read_threads_num, NULL,
262 plugin_read_thread, NULL) == 0)
268 ERROR ("plugin: start_threads: pthread_create failed.");
272 } /* void start_threads */
274 static void stop_threads (void)
278 pthread_mutex_lock (&read_lock);
280 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
281 pthread_cond_broadcast (&read_cond);
282 pthread_mutex_unlock (&read_lock);
284 for (i = 0; i < read_threads_num; i++)
286 if (pthread_join (read_threads[i], NULL) != 0)
288 ERROR ("plugin: stop_threads: pthread_join failed.");
290 read_threads[i] = (pthread_t) 0;
292 sfree (read_threads);
293 read_threads_num = 0;
294 } /* void stop_threads */
299 void plugin_set_dir (const char *dir)
301 if (plugindir != NULL)
306 else if ((plugindir = strdup (dir)) == NULL)
309 ERROR ("strdup failed: %s",
310 sstrerror (errno, errbuf, sizeof (errbuf)));
315 int plugin_load (const char *type)
319 char filename[BUFSIZE] = "";
320 char typename[BUFSIZE];
326 DEBUG ("type = %s", type);
328 dir = plugin_get_dir ();
331 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
332 * type when matching the filename */
333 if (ssnprintf (typename, sizeof (typename),
334 "%s.so", type) >= sizeof (typename))
336 WARNING ("snprintf: truncated: `%s.so'", type);
339 typename_len = strlen (typename);
341 if ((dh = opendir (dir)) == NULL)
344 ERROR ("opendir (%s): %s", dir,
345 sstrerror (errno, errbuf, sizeof (errbuf)));
349 while ((de = readdir (dh)) != NULL)
351 if (strncasecmp (de->d_name, typename, typename_len))
354 if (ssnprintf (filename, sizeof (filename),
355 "%s/%s", dir, de->d_name) >= sizeof (filename))
357 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
361 if (lstat (filename, &statbuf) == -1)
364 WARNING ("stat %s: %s", filename,
365 sstrerror (errno, errbuf, sizeof (errbuf)));
368 else if (!S_ISREG (statbuf.st_mode))
370 /* don't follow symlinks */
374 if (plugin_load_file (filename) == 0)
382 fprintf (stderr, "Unable to load plugin %s.\n", type);
388 if (filename[0] == '\0')
389 fprintf (stderr, "Could not find plugin %s.\n", type);
395 * The `register_*' functions follow
397 int plugin_register_config (const char *name,
398 int (*callback) (const char *key, const char *val),
399 const char **keys, int keys_num)
401 cf_register (name, callback, keys, keys_num);
403 } /* int plugin_register_config */
405 int plugin_register_complex_config (const char *type,
406 int (*callback) (oconfig_item_t *))
408 return (cf_register_complex (type, callback));
409 } /* int plugin_register_complex_config */
411 int plugin_register_init (const char *name,
412 int (*callback) (void))
414 return (register_callback (&list_init, name, (void *) callback));
415 } /* plugin_register_init */
417 int plugin_register_read (const char *name,
418 int (*callback) (void))
422 rf = (read_func_t *) malloc (sizeof (read_func_t));
426 ERROR ("plugin_register_read: malloc failed: %s",
427 sstrerror (errno, errbuf, sizeof (errbuf)));
431 memset (rf, '\0', sizeof (read_func_t));
432 rf->wait_time = interval_g;
434 rf->callback = callback;
435 rf->needs_read = DONE;
437 return (register_callback (&list_read, name, (void *) rf));
438 } /* int plugin_register_read */
440 int plugin_register_write (const char *name,
441 int (*callback) (const data_set_t *ds, const value_list_t *vl))
443 return (register_callback (&list_write, name, (void *) callback));
444 } /* int plugin_register_write */
446 int plugin_register_filter (const char *name,
447 int (*callback) (const data_set_t *ds, value_list_t *vl))
449 return (register_callback (&list_filter, name, (void *) callback));
450 } /* int plugin_register_filter */
452 int plugin_register_flush (const char *name,
453 int (*callback) (const int timeout, const char *identifier))
455 return (register_callback (&list_flush, name, (void *) callback));
456 } /* int plugin_register_flush */
458 int plugin_register_shutdown (char *name,
459 int (*callback) (void))
461 return (register_callback (&list_shutdown, name, (void *) callback));
462 } /* int plugin_register_shutdown */
464 int plugin_register_data_set (const data_set_t *ds)
469 if ((data_sets != NULL)
470 && (c_avl_get (data_sets, ds->type, NULL) == 0))
472 NOTICE ("Replacing DS `%s' with another version.", ds->type);
473 plugin_unregister_data_set (ds->type);
475 else if (data_sets == NULL)
477 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
478 if (data_sets == NULL)
482 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
485 memcpy(ds_copy, ds, sizeof (data_set_t));
487 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
489 if (ds_copy->ds == NULL)
495 for (i = 0; i < ds->ds_num; i++)
496 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
498 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
499 } /* int plugin_register_data_set */
501 int plugin_register_log (char *name,
502 void (*callback) (int priority, const char *msg))
504 return (register_callback (&list_log, name, (void *) callback));
505 } /* int plugin_register_log */
507 int plugin_register_notification (const char *name,
508 int (*callback) (const notification_t *notif))
510 return (register_callback (&list_notification, name, (void *) callback));
511 } /* int plugin_register_log */
513 int plugin_unregister_config (const char *name)
515 cf_unregister (name);
517 } /* int plugin_unregister_config */
519 int plugin_unregister_complex_config (const char *name)
521 cf_unregister_complex (name);
523 } /* int plugin_unregister_complex_config */
525 int plugin_unregister_init (const char *name)
527 return (plugin_unregister (list_init, name));
530 int plugin_unregister_read (const char *name)
534 e = llist_search (list_read, name);
539 llist_remove (list_read, e);
547 int plugin_unregister_write (const char *name)
549 return (plugin_unregister (list_write, name));
552 int plugin_unregister_filter (const char *name)
554 return (plugin_unregister (list_filter, name));
557 int plugin_unregister_flush (const char *name)
559 return (plugin_unregister (list_flush, name));
562 int plugin_unregister_shutdown (const char *name)
564 return (plugin_unregister (list_shutdown, name));
567 int plugin_unregister_data_set (const char *name)
571 if (data_sets == NULL)
574 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
581 } /* int plugin_unregister_data_set */
583 int plugin_unregister_log (const char *name)
585 return (plugin_unregister (list_log, name));
588 int plugin_unregister_notification (const char *name)
590 return (plugin_unregister (list_notification, name));
593 void plugin_init_all (void)
595 int (*callback) (void);
599 /* Init the value cache */
602 if ((list_init == NULL) && (list_read == NULL))
605 /* Calling all init callbacks before checking if read callbacks
606 * are available allows the init callbacks to register the read
608 le = llist_head (list_init);
611 callback = (int (*) (void)) le->value;
612 status = (*callback) ();
616 ERROR ("Initialization of plugin `%s' "
617 "failed with status %i. "
618 "Plugin will be unloaded.",
620 /* Plugins that register read callbacks from the init
621 * callback should take care of appropriate error
622 * handling themselves. */
623 /* FIXME: Unload _all_ functions */
624 plugin_unregister_read (le->key);
630 /* Start read-threads */
631 if (list_read != NULL)
635 rt = global_option_get ("ReadThreads");
637 start_threads ((num > 0) ? num : 5);
639 } /* void plugin_init_all */
641 void plugin_read_all (void)
648 if (list_read == NULL)
651 pthread_mutex_lock (&read_lock);
653 le = llist_head (list_read);
656 rf = (read_func_t *) le->value;
658 if (rf->needs_read != DONE)
664 if (rf->wait_left > 0)
665 rf->wait_left -= interval_g;
667 if (rf->wait_left <= 0)
669 rf->needs_read = TODO;
675 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
676 pthread_cond_broadcast (&read_cond);
677 pthread_mutex_unlock (&read_lock);
678 } /* void plugin_read_all */
680 int plugin_flush (const char *plugin, int timeout, const char *identifier)
682 int (*callback) (int timeout, const char *identifier);
685 if (list_flush == NULL)
688 le = llist_head (list_flush);
692 && (strcmp (plugin, le->key) != 0))
698 callback = (int (*) (int, const char *)) le->value;
699 (*callback) (timeout, identifier);
704 } /* int plugin_flush */
706 void plugin_shutdown_all (void)
708 int (*callback) (void);
713 if (list_shutdown == NULL)
716 le = llist_head (list_shutdown);
719 callback = (int (*) (void)) le->value;
721 /* Advance the pointer before calling the callback allows
722 * shutdown functions to unregister themselves. If done the
723 * other way around the memory `le' points to will be freed
724 * after callback returns. */
729 } /* void plugin_shutdown_all */
731 int plugin_dispatch_values (value_list_t *vl)
733 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
740 if ((vl == NULL) || (*vl->type == '\0')) {
741 ERROR ("plugin_dispatch_values: Invalid value list.");
745 if (list_write == NULL)
746 c_complain_once (LOG_WARNING, &no_write_complaint,
747 "plugin_dispatch_values: No write callback has been "
748 "registered. Please load at least one output plugin, "
749 "if you want the collected data to be stored.");
751 if (data_sets == NULL)
753 ERROR ("plugin_dispatch_values: No data sets registered. "
754 "Could the types database be read? Check "
755 "your `TypesDB' setting!");
759 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
761 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
765 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
767 "plugin = %s; plugin_instance = %s; "
768 "type = %s; type_instance = %s;",
769 (unsigned int) vl->time, vl->interval,
771 vl->plugin, vl->plugin_instance,
772 vl->type, vl->type_instance);
775 assert (0 == strcmp (ds->type, vl->type));
777 if (0 != strcmp (ds->type, vl->type))
778 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
783 assert (ds->ds_num == vl->values_len);
785 if (ds->ds_num != vl->values_len)
787 ERROR ("plugin_dispatch_values: ds->type = %s: "
788 "(ds->ds_num = %i) != "
789 "(vl->values_len = %i)",
790 ds->type, ds->ds_num, vl->values_len);
795 escape_slashes (vl->host, sizeof (vl->host));
796 escape_slashes (vl->plugin, sizeof (vl->plugin));
797 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
798 escape_slashes (vl->type, sizeof (vl->type));
799 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
801 le = llist_head (list_filter);
804 int (*filter_callback) (const data_set_t *, value_list_t *) =
805 (int (*) (const data_set_t *, value_list_t *)) le->value;
807 filter |= (*filter_callback) (ds, vl);
809 if (filter == FILTER_IGNORE)
815 /* Update the value cache */
818 if ((filter & FILTER_NOTHRESHOLD_CHECK) == 0)
819 ut_check_threshold (ds, vl);
821 if (filter & FILTER_NOWRITE)
824 le = llist_head (list_write);
827 int (*write_callback) (const data_set_t *, const value_list_t *) =
828 (int (*) (const data_set_t *, const value_list_t *)) le->value;
830 (*write_callback) (ds, vl);
836 } /* int plugin_dispatch_values */
838 int plugin_dispatch_notification (const notification_t *notif)
840 int (*callback) (const notification_t *);
842 /* Possible TODO: Add flap detection here */
844 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
845 "time = %u; host = %s;",
846 notif->severity, notif->message,
847 (unsigned int) notif->time, notif->host);
849 /* Nobody cares for notifications */
850 if (list_notification == NULL)
853 le = llist_head (list_notification);
856 callback = (int (*) (const notification_t *)) le->value;
863 } /* int plugin_dispatch_notification */
865 void plugin_log (int level, const char *format, ...)
870 void (*callback) (int, const char *);
873 if (list_log == NULL)
877 if (level >= LOG_DEBUG)
881 va_start (ap, format);
882 vsnprintf (msg, sizeof (msg), format, ap);
883 msg[sizeof (msg) - 1] = '\0';
886 le = llist_head (list_log);
889 callback = (void (*) (int, const char *)) le->value;
890 (*callback) (level, msg);
894 } /* void plugin_log */
896 const data_set_t *plugin_get_ds (const char *name)
900 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
902 DEBUG ("No such dataset registered: %s", name);
907 } /* data_set_t *plugin_get_ds */
909 static int plugin_notification_meta_add (notification_t *n,
911 enum notification_meta_type_e type,
914 notification_meta_t *meta;
915 notification_meta_t *tail;
917 if ((n == NULL) || (name == NULL) || (value == NULL))
919 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
923 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
926 ERROR ("plugin_notification_meta_add: malloc failed.");
929 memset (meta, 0, sizeof (notification_meta_t));
931 sstrncpy (meta->name, name, sizeof (meta->name));
938 meta->value_string = strdup ((const char *) value);
939 if (meta->value_string == NULL)
941 ERROR ("plugin_notification_meta_add: strdup failed.");
947 case NM_TYPE_SIGNED_INT:
949 meta->value_signed_int = *((int64_t *) value);
952 case NM_TYPE_UNSIGNED_INT:
954 meta->value_unsigned_int = *((uint64_t *) value);
959 meta->value_double = *((double *) value);
962 case NM_TYPE_BOOLEAN:
964 meta->value_boolean = *((bool *) value);
969 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
973 } /* switch (type) */
977 while ((tail != NULL) && (tail->next != NULL))
986 } /* int plugin_notification_meta_add */
988 int plugin_notification_meta_add_string (notification_t *n,
992 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
995 int plugin_notification_meta_add_signed_int (notification_t *n,
999 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1002 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1006 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1009 int plugin_notification_meta_add_double (notification_t *n,
1013 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1016 int plugin_notification_meta_add_boolean (notification_t *n,
1020 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1023 int plugin_notification_meta_copy (notification_t *dst,
1024 const notification_t *src)
1026 notification_meta_t *meta;
1028 assert (dst != NULL);
1029 assert (src != NULL);
1030 assert (dst != src);
1031 assert ((src->meta == NULL) || (src->meta != dst->meta));
1033 for (meta = src->meta; meta != NULL; meta = meta->next)
1035 if (meta->type == NM_TYPE_STRING)
1036 plugin_notification_meta_add_string (dst, meta->name,
1037 meta->value_string);
1038 else if (meta->type == NM_TYPE_SIGNED_INT)
1039 plugin_notification_meta_add_signed_int (dst, meta->name,
1040 meta->value_signed_int);
1041 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1042 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1043 meta->value_unsigned_int);
1044 else if (meta->type == NM_TYPE_DOUBLE)
1045 plugin_notification_meta_add_double (dst, meta->name,
1046 meta->value_double);
1047 else if (meta->type == NM_TYPE_BOOLEAN)
1048 plugin_notification_meta_add_boolean (dst, meta->name,
1049 meta->value_boolean);
1053 } /* int plugin_notification_meta_copy */
1055 int plugin_notification_meta_free (notification_t *n)
1057 notification_meta_t *this;
1058 notification_meta_t *next;
1062 ERROR ("plugin_notification_meta_free: n == NULL!");
1068 while (this != NULL)
1072 if (this->type == NM_TYPE_STRING)
1074 free ((char *)this->value_string);
1075 this->value_string = NULL;
1083 } /* int plugin_notification_meta_free */