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 if (read_threads == NULL)
282 pthread_mutex_lock (&read_lock);
284 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
285 pthread_cond_broadcast (&read_cond);
286 pthread_mutex_unlock (&read_lock);
288 for (i = 0; i < read_threads_num; i++)
290 if (pthread_join (read_threads[i], NULL) != 0)
292 ERROR ("plugin: stop_threads: pthread_join failed.");
294 read_threads[i] = (pthread_t) 0;
296 sfree (read_threads);
297 read_threads_num = 0;
298 } /* void stop_threads */
303 void plugin_set_dir (const char *dir)
305 if (plugindir != NULL)
310 else if ((plugindir = strdup (dir)) == NULL)
313 ERROR ("strdup failed: %s",
314 sstrerror (errno, errbuf, sizeof (errbuf)));
319 int plugin_load (const char *type)
323 char filename[BUFSIZE] = "";
324 char typename[BUFSIZE];
330 DEBUG ("type = %s", type);
332 dir = plugin_get_dir ();
335 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
336 * type when matching the filename */
337 if (ssnprintf (typename, sizeof (typename),
338 "%s.so", type) >= sizeof (typename))
340 WARNING ("snprintf: truncated: `%s.so'", type);
343 typename_len = strlen (typename);
345 if ((dh = opendir (dir)) == NULL)
348 ERROR ("opendir (%s): %s", dir,
349 sstrerror (errno, errbuf, sizeof (errbuf)));
353 while ((de = readdir (dh)) != NULL)
355 if (strncasecmp (de->d_name, typename, typename_len))
358 if (ssnprintf (filename, sizeof (filename),
359 "%s/%s", dir, de->d_name) >= sizeof (filename))
361 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
365 if (lstat (filename, &statbuf) == -1)
368 WARNING ("stat %s: %s", filename,
369 sstrerror (errno, errbuf, sizeof (errbuf)));
372 else if (!S_ISREG (statbuf.st_mode))
374 /* don't follow symlinks */
378 if (plugin_load_file (filename) == 0)
386 fprintf (stderr, "Unable to load plugin %s.\n", type);
392 if (filename[0] == '\0')
393 fprintf (stderr, "Could not find plugin %s.\n", type);
399 * The `register_*' functions follow
401 int plugin_register_config (const char *name,
402 int (*callback) (const char *key, const char *val),
403 const char **keys, int keys_num)
405 cf_register (name, callback, keys, keys_num);
407 } /* int plugin_register_config */
409 int plugin_register_complex_config (const char *type,
410 int (*callback) (oconfig_item_t *))
412 return (cf_register_complex (type, callback));
413 } /* int plugin_register_complex_config */
415 int plugin_register_init (const char *name,
416 int (*callback) (void))
418 return (register_callback (&list_init, name, (void *) callback));
419 } /* plugin_register_init */
421 int plugin_register_read (const char *name,
422 int (*callback) (void))
426 rf = (read_func_t *) malloc (sizeof (read_func_t));
430 ERROR ("plugin_register_read: malloc failed: %s",
431 sstrerror (errno, errbuf, sizeof (errbuf)));
435 memset (rf, '\0', sizeof (read_func_t));
436 rf->wait_time = interval_g;
438 rf->callback = callback;
439 rf->needs_read = DONE;
441 return (register_callback (&list_read, name, (void *) rf));
442 } /* int plugin_register_read */
444 int plugin_register_write (const char *name,
445 int (*callback) (const data_set_t *ds, const value_list_t *vl))
447 return (register_callback (&list_write, name, (void *) callback));
448 } /* int plugin_register_write */
450 int plugin_register_filter (const char *name,
451 int (*callback) (const data_set_t *ds, value_list_t *vl))
453 return (register_callback (&list_filter, name, (void *) callback));
454 } /* int plugin_register_filter */
456 int plugin_register_flush (const char *name,
457 int (*callback) (const int timeout, const char *identifier))
459 return (register_callback (&list_flush, name, (void *) callback));
460 } /* int plugin_register_flush */
462 int plugin_register_shutdown (char *name,
463 int (*callback) (void))
465 return (register_callback (&list_shutdown, name, (void *) callback));
466 } /* int plugin_register_shutdown */
468 int plugin_register_data_set (const data_set_t *ds)
473 if ((data_sets != NULL)
474 && (c_avl_get (data_sets, ds->type, NULL) == 0))
476 NOTICE ("Replacing DS `%s' with another version.", ds->type);
477 plugin_unregister_data_set (ds->type);
479 else if (data_sets == NULL)
481 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
482 if (data_sets == NULL)
486 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
489 memcpy(ds_copy, ds, sizeof (data_set_t));
491 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
493 if (ds_copy->ds == NULL)
499 for (i = 0; i < ds->ds_num; i++)
500 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
502 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
503 } /* int plugin_register_data_set */
505 int plugin_register_log (char *name,
506 void (*callback) (int priority, const char *msg))
508 return (register_callback (&list_log, name, (void *) callback));
509 } /* int plugin_register_log */
511 int plugin_register_notification (const char *name,
512 int (*callback) (const notification_t *notif))
514 return (register_callback (&list_notification, name, (void *) callback));
515 } /* int plugin_register_log */
517 int plugin_unregister_config (const char *name)
519 cf_unregister (name);
521 } /* int plugin_unregister_config */
523 int plugin_unregister_complex_config (const char *name)
525 cf_unregister_complex (name);
527 } /* int plugin_unregister_complex_config */
529 int plugin_unregister_init (const char *name)
531 return (plugin_unregister (list_init, name));
534 int plugin_unregister_read (const char *name)
538 e = llist_search (list_read, name);
543 llist_remove (list_read, e);
551 int plugin_unregister_write (const char *name)
553 return (plugin_unregister (list_write, name));
556 int plugin_unregister_filter (const char *name)
558 return (plugin_unregister (list_filter, name));
561 int plugin_unregister_flush (const char *name)
563 return (plugin_unregister (list_flush, name));
566 int plugin_unregister_shutdown (const char *name)
568 return (plugin_unregister (list_shutdown, name));
571 int plugin_unregister_data_set (const char *name)
575 if (data_sets == NULL)
578 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
585 } /* int plugin_unregister_data_set */
587 int plugin_unregister_log (const char *name)
589 return (plugin_unregister (list_log, name));
592 int plugin_unregister_notification (const char *name)
594 return (plugin_unregister (list_notification, name));
597 void plugin_init_all (void)
599 int (*callback) (void);
603 /* Init the value cache */
606 if ((list_init == NULL) && (list_read == NULL))
609 /* Calling all init callbacks before checking if read callbacks
610 * are available allows the init callbacks to register the read
612 le = llist_head (list_init);
615 callback = (int (*) (void)) le->value;
616 status = (*callback) ();
620 ERROR ("Initialization of plugin `%s' "
621 "failed with status %i. "
622 "Plugin will be unloaded.",
624 /* Plugins that register read callbacks from the init
625 * callback should take care of appropriate error
626 * handling themselves. */
627 /* FIXME: Unload _all_ functions */
628 plugin_unregister_read (le->key);
634 /* Start read-threads */
635 if (list_read != NULL)
639 rt = global_option_get ("ReadThreads");
642 start_threads ((num > 0) ? num : 5);
644 } /* void plugin_init_all */
646 void plugin_read_all (void)
653 if (list_read == NULL)
656 pthread_mutex_lock (&read_lock);
658 le = llist_head (list_read);
661 rf = (read_func_t *) le->value;
663 if (rf->needs_read != DONE)
669 if (rf->wait_left > 0)
670 rf->wait_left -= interval_g;
672 if (rf->wait_left <= 0)
674 rf->needs_read = TODO;
680 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
681 pthread_cond_broadcast (&read_cond);
682 pthread_mutex_unlock (&read_lock);
683 } /* void plugin_read_all */
685 /* Read function called when the `-T' command line argument is given. */
686 int plugin_read_all_once (void)
691 int return_status = 0;
693 if (list_read == NULL)
695 NOTICE ("No read-functions are registered.");
699 for (le = llist_head (list_read);
703 rf = (read_func_t *) le->value;
704 status = rf->callback ();
707 NOTICE ("read-function of plugin `%s' failed.",
713 return (return_status);
714 } /* int plugin_read_all_once */
716 int plugin_write (const char *plugin, /* {{{ */
717 const data_set_t *ds, const value_list_t *vl)
719 int (*callback) (const data_set_t *ds, const value_list_t *vl);
726 if (list_write == NULL)
731 ds = plugin_get_ds (vl->type);
734 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
744 le = llist_head (list_write);
747 callback = le->value;
748 status = (*callback) (ds, vl);
757 if ((success == 0) && (failure != 0))
762 else /* plugin != NULL */
764 le = llist_head (list_write);
767 if (strcasecmp (plugin, le->key) == 0)
776 callback = le->value;
777 status = (*callback) (ds, vl);
781 } /* }}} int plugin_write */
783 int plugin_flush (const char *plugin, int timeout, const char *identifier)
785 int (*callback) (int timeout, const char *identifier);
788 if (list_flush == NULL)
791 le = llist_head (list_flush);
795 && (strcmp (plugin, le->key) != 0))
801 callback = (int (*) (int, const char *)) le->value;
802 (*callback) (timeout, identifier);
807 } /* int plugin_flush */
809 void plugin_shutdown_all (void)
811 int (*callback) (void);
816 if (list_shutdown == NULL)
819 le = llist_head (list_shutdown);
822 callback = (int (*) (void)) le->value;
824 /* Advance the pointer before calling the callback allows
825 * shutdown functions to unregister themselves. If done the
826 * other way around the memory `le' points to will be freed
827 * after callback returns. */
832 } /* void plugin_shutdown_all */
834 int plugin_dispatch_values (value_list_t *vl)
836 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
840 if ((vl == NULL) || (*vl->type == '\0')) {
841 ERROR ("plugin_dispatch_values: Invalid value list.");
845 if (list_write == NULL)
846 c_complain_once (LOG_WARNING, &no_write_complaint,
847 "plugin_dispatch_values: No write callback has been "
848 "registered. Please load at least one output plugin, "
849 "if you want the collected data to be stored.");
851 if (data_sets == NULL)
853 ERROR ("plugin_dispatch_values: No data sets registered. "
854 "Could the types database be read? Check "
855 "your `TypesDB' setting!");
859 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
861 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
865 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
867 "plugin = %s; plugin_instance = %s; "
868 "type = %s; type_instance = %s;",
869 (unsigned int) vl->time, vl->interval,
871 vl->plugin, vl->plugin_instance,
872 vl->type, vl->type_instance);
875 assert (0 == strcmp (ds->type, vl->type));
877 if (0 != strcmp (ds->type, vl->type))
878 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
883 assert (ds->ds_num == vl->values_len);
885 if (ds->ds_num != vl->values_len)
887 ERROR ("plugin_dispatch_values: ds->type = %s: "
888 "(ds->ds_num = %i) != "
889 "(vl->values_len = %i)",
890 ds->type, ds->ds_num, vl->values_len);
895 escape_slashes (vl->host, sizeof (vl->host));
896 escape_slashes (vl->plugin, sizeof (vl->plugin));
897 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
898 escape_slashes (vl->type, sizeof (vl->type));
899 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
901 /* Update the value cache */
907 } /* int plugin_dispatch_values */
909 int plugin_dispatch_notification (const notification_t *notif)
911 int (*callback) (const notification_t *);
913 /* Possible TODO: Add flap detection here */
915 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
916 "time = %u; host = %s;",
917 notif->severity, notif->message,
918 (unsigned int) notif->time, notif->host);
920 /* Nobody cares for notifications */
921 if (list_notification == NULL)
924 le = llist_head (list_notification);
927 callback = (int (*) (const notification_t *)) le->value;
934 } /* int plugin_dispatch_notification */
936 void plugin_log (int level, const char *format, ...)
941 void (*callback) (int, const char *);
944 if (list_log == NULL)
948 if (level >= LOG_DEBUG)
952 va_start (ap, format);
953 vsnprintf (msg, sizeof (msg), format, ap);
954 msg[sizeof (msg) - 1] = '\0';
957 le = llist_head (list_log);
960 callback = (void (*) (int, const char *)) le->value;
961 (*callback) (level, msg);
965 } /* void plugin_log */
967 const data_set_t *plugin_get_ds (const char *name)
971 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
973 DEBUG ("No such dataset registered: %s", name);
978 } /* data_set_t *plugin_get_ds */
980 static int plugin_notification_meta_add (notification_t *n,
982 enum notification_meta_type_e type,
985 notification_meta_t *meta;
986 notification_meta_t *tail;
988 if ((n == NULL) || (name == NULL) || (value == NULL))
990 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
994 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
997 ERROR ("plugin_notification_meta_add: malloc failed.");
1000 memset (meta, 0, sizeof (notification_meta_t));
1002 sstrncpy (meta->name, name, sizeof (meta->name));
1007 case NM_TYPE_STRING:
1009 meta->nm_value.nm_string = strdup ((const char *) value);
1010 if (meta->nm_value.nm_string == NULL)
1012 ERROR ("plugin_notification_meta_add: strdup failed.");
1018 case NM_TYPE_SIGNED_INT:
1020 meta->nm_value.nm_signed_int = *((int64_t *) value);
1023 case NM_TYPE_UNSIGNED_INT:
1025 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1028 case NM_TYPE_DOUBLE:
1030 meta->nm_value.nm_double = *((double *) value);
1033 case NM_TYPE_BOOLEAN:
1035 meta->nm_value.nm_boolean = *((bool *) value);
1040 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1044 } /* switch (type) */
1048 while ((tail != NULL) && (tail->next != NULL))
1057 } /* int plugin_notification_meta_add */
1059 int plugin_notification_meta_add_string (notification_t *n,
1063 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1066 int plugin_notification_meta_add_signed_int (notification_t *n,
1070 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1073 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1077 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1080 int plugin_notification_meta_add_double (notification_t *n,
1084 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1087 int plugin_notification_meta_add_boolean (notification_t *n,
1091 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1094 int plugin_notification_meta_copy (notification_t *dst,
1095 const notification_t *src)
1097 notification_meta_t *meta;
1099 assert (dst != NULL);
1100 assert (src != NULL);
1101 assert (dst != src);
1102 assert ((src->meta == NULL) || (src->meta != dst->meta));
1104 for (meta = src->meta; meta != NULL; meta = meta->next)
1106 if (meta->type == NM_TYPE_STRING)
1107 plugin_notification_meta_add_string (dst, meta->name,
1108 meta->nm_value.nm_string);
1109 else if (meta->type == NM_TYPE_SIGNED_INT)
1110 plugin_notification_meta_add_signed_int (dst, meta->name,
1111 meta->nm_value.nm_signed_int);
1112 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1113 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1114 meta->nm_value.nm_unsigned_int);
1115 else if (meta->type == NM_TYPE_DOUBLE)
1116 plugin_notification_meta_add_double (dst, meta->name,
1117 meta->nm_value.nm_double);
1118 else if (meta->type == NM_TYPE_BOOLEAN)
1119 plugin_notification_meta_add_boolean (dst, meta->name,
1120 meta->nm_value.nm_boolean);
1124 } /* int plugin_notification_meta_copy */
1126 int plugin_notification_meta_free (notification_t *n)
1128 notification_meta_t *this;
1129 notification_meta_t *next;
1133 ERROR ("plugin_notification_meta_free: n == NULL!");
1139 while (this != NULL)
1143 if (this->type == NM_TYPE_STRING)
1145 free ((char *)this->nm_value.nm_string);
1146 this->nm_value.nm_string = NULL;
1154 } /* int plugin_notification_meta_free */