2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2009 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 fc_chain_t *pre_cache_chain = NULL;
66 static fc_chain_t *post_cache_chain = NULL;
68 static c_avl_tree_t *data_sets;
70 static char *plugindir = NULL;
72 static int read_loop = 1;
73 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
75 static pthread_t *read_threads = NULL;
76 static int read_threads_num = 0;
81 static const char *plugin_get_dir (void)
83 if (plugindir == NULL)
89 static int register_callback (llist_t **list, const char *name, void *callback)
95 && ((*list = llist_create ()) == NULL))
98 le = llist_search (*list, name);
105 le = llentry_create (key, callback);
112 llist_append (*list, le);
116 le->value = callback;
120 } /* int register_callback */
122 static int plugin_unregister (llist_t *list, const char *name)
126 e = llist_search (list, name);
131 llist_remove (list, e);
136 } /* int plugin_unregister */
139 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
140 * object, but it will bitch about a shared object not having a
141 * ``module_register'' symbol..
143 static int plugin_load_file (char *file)
146 void (*reg_handle) (void);
148 DEBUG ("file = %s", file);
151 lt_dlerror (); /* clear errors */
153 if ((dlh = lt_dlopen (file)) == NULL)
155 const char *error = lt_dlerror ();
157 ERROR ("lt_dlopen (%s) failed: %s", file, error);
158 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
162 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
164 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
165 file, lt_dlerror ());
175 static void *plugin_read_thread (void *args)
182 pthread_mutex_lock (&read_lock);
184 while (read_loop != 0)
186 le = llist_head (list_read);
189 while ((read_loop != 0) && (le != NULL))
191 rf = (read_func_t *) le->value;
193 if (rf->needs_read != TODO)
199 /* We will do this read function */
200 rf->needs_read = ACTIVE;
202 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
203 (unsigned long int) pthread_self (), le->key);
204 pthread_mutex_unlock (&read_lock);
206 status = rf->callback ();
211 if (rf->wait_time < interval_g)
212 rf->wait_time = interval_g;
213 rf->wait_left = rf->wait_time;
214 rf->wait_time = rf->wait_time * 2;
215 if (rf->wait_time > 86400)
216 rf->wait_time = 86400;
218 NOTICE ("read-function of plugin `%s' "
219 "failed. Will suspend it for %i "
220 "seconds.", le->key, rf->wait_left);
225 rf->wait_time = interval_g;
228 pthread_mutex_lock (&read_lock);
230 rf->needs_read = DONE;
232 } /* while (le != NULL) */
234 if ((read_loop != 0) && (done == 0))
236 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
237 (unsigned long int) pthread_self ());
238 pthread_cond_wait (&read_cond, &read_lock);
240 } /* while (read_loop) */
242 pthread_mutex_unlock (&read_lock);
246 } /* void *plugin_read_thread */
248 static void start_threads (int num)
252 if (read_threads != NULL)
255 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
256 if (read_threads == NULL)
258 ERROR ("plugin: start_threads: calloc failed.");
262 read_threads_num = 0;
263 for (i = 0; i < num; i++)
265 if (pthread_create (read_threads + read_threads_num, NULL,
266 plugin_read_thread, NULL) == 0)
272 ERROR ("plugin: start_threads: pthread_create failed.");
276 } /* void start_threads */
278 static void stop_threads (void)
282 if (read_threads == NULL)
285 pthread_mutex_lock (&read_lock);
287 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
288 pthread_cond_broadcast (&read_cond);
289 pthread_mutex_unlock (&read_lock);
291 for (i = 0; i < read_threads_num; i++)
293 if (pthread_join (read_threads[i], NULL) != 0)
295 ERROR ("plugin: stop_threads: pthread_join failed.");
297 read_threads[i] = (pthread_t) 0;
299 sfree (read_threads);
300 read_threads_num = 0;
301 } /* void stop_threads */
306 void plugin_set_dir (const char *dir)
308 if (plugindir != NULL)
313 else if ((plugindir = strdup (dir)) == NULL)
316 ERROR ("strdup failed: %s",
317 sstrerror (errno, errbuf, sizeof (errbuf)));
322 int plugin_load (const char *type)
326 char filename[BUFSIZE] = "";
327 char typename[BUFSIZE];
333 DEBUG ("type = %s", type);
335 dir = plugin_get_dir ();
338 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
339 * type when matching the filename */
340 if (ssnprintf (typename, sizeof (typename),
341 "%s.so", type) >= sizeof (typename))
343 WARNING ("snprintf: truncated: `%s.so'", type);
346 typename_len = strlen (typename);
348 if ((dh = opendir (dir)) == NULL)
351 ERROR ("opendir (%s): %s", dir,
352 sstrerror (errno, errbuf, sizeof (errbuf)));
356 while ((de = readdir (dh)) != NULL)
358 if (strncasecmp (de->d_name, typename, typename_len))
361 if (ssnprintf (filename, sizeof (filename),
362 "%s/%s", dir, de->d_name) >= sizeof (filename))
364 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
368 if (lstat (filename, &statbuf) == -1)
371 WARNING ("stat %s: %s", filename,
372 sstrerror (errno, errbuf, sizeof (errbuf)));
375 else if (!S_ISREG (statbuf.st_mode))
377 /* don't follow symlinks */
381 if (plugin_load_file (filename) == 0)
389 fprintf (stderr, "Unable to load plugin %s.\n", type);
395 if (filename[0] == '\0')
396 fprintf (stderr, "Could not find plugin %s.\n", type);
402 * The `register_*' functions follow
404 int plugin_register_config (const char *name,
405 int (*callback) (const char *key, const char *val),
406 const char **keys, int keys_num)
408 cf_register (name, callback, keys, keys_num);
410 } /* int plugin_register_config */
412 int plugin_register_complex_config (const char *type,
413 int (*callback) (oconfig_item_t *))
415 return (cf_register_complex (type, callback));
416 } /* int plugin_register_complex_config */
418 int plugin_register_init (const char *name,
419 int (*callback) (void))
421 return (register_callback (&list_init, name, (void *) callback));
422 } /* plugin_register_init */
424 int plugin_register_read (const char *name,
425 int (*callback) (void))
429 rf = (read_func_t *) malloc (sizeof (read_func_t));
433 ERROR ("plugin_register_read: malloc failed: %s",
434 sstrerror (errno, errbuf, sizeof (errbuf)));
438 memset (rf, '\0', sizeof (read_func_t));
439 rf->wait_time = interval_g;
441 rf->callback = callback;
442 rf->needs_read = DONE;
444 return (register_callback (&list_read, name, (void *) rf));
445 } /* int plugin_register_read */
447 int plugin_register_write (const char *name,
448 int (*callback) (const data_set_t *ds, const value_list_t *vl))
450 return (register_callback (&list_write, name, (void *) callback));
451 } /* int plugin_register_write */
453 int plugin_register_filter (const char *name,
454 int (*callback) (const data_set_t *ds, value_list_t *vl))
456 return (register_callback (&list_filter, name, (void *) callback));
457 } /* int plugin_register_filter */
459 int plugin_register_flush (const char *name,
460 int (*callback) (const int timeout, const char *identifier))
462 return (register_callback (&list_flush, name, (void *) callback));
463 } /* int plugin_register_flush */
465 int plugin_register_shutdown (char *name,
466 int (*callback) (void))
468 return (register_callback (&list_shutdown, name, (void *) callback));
469 } /* int plugin_register_shutdown */
471 int plugin_register_data_set (const data_set_t *ds)
476 if ((data_sets != NULL)
477 && (c_avl_get (data_sets, ds->type, NULL) == 0))
479 NOTICE ("Replacing DS `%s' with another version.", ds->type);
480 plugin_unregister_data_set (ds->type);
482 else if (data_sets == NULL)
484 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
485 if (data_sets == NULL)
489 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
492 memcpy(ds_copy, ds, sizeof (data_set_t));
494 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
496 if (ds_copy->ds == NULL)
502 for (i = 0; i < ds->ds_num; i++)
503 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
505 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
506 } /* int plugin_register_data_set */
508 int plugin_register_log (char *name,
509 void (*callback) (int priority, const char *msg))
511 return (register_callback (&list_log, name, (void *) callback));
512 } /* int plugin_register_log */
514 int plugin_register_notification (const char *name,
515 int (*callback) (const notification_t *notif))
517 return (register_callback (&list_notification, name, (void *) callback));
518 } /* int plugin_register_log */
520 int plugin_unregister_config (const char *name)
522 cf_unregister (name);
524 } /* int plugin_unregister_config */
526 int plugin_unregister_complex_config (const char *name)
528 cf_unregister_complex (name);
530 } /* int plugin_unregister_complex_config */
532 int plugin_unregister_init (const char *name)
534 return (plugin_unregister (list_init, name));
537 int plugin_unregister_read (const char *name)
541 e = llist_search (list_read, name);
546 llist_remove (list_read, e);
554 int plugin_unregister_write (const char *name)
556 return (plugin_unregister (list_write, name));
559 int plugin_unregister_filter (const char *name)
561 return (plugin_unregister (list_filter, name));
564 int plugin_unregister_flush (const char *name)
566 return (plugin_unregister (list_flush, name));
569 int plugin_unregister_shutdown (const char *name)
571 return (plugin_unregister (list_shutdown, name));
574 int plugin_unregister_data_set (const char *name)
578 if (data_sets == NULL)
581 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
588 } /* int plugin_unregister_data_set */
590 int plugin_unregister_log (const char *name)
592 return (plugin_unregister (list_log, name));
595 int plugin_unregister_notification (const char *name)
597 return (plugin_unregister (list_notification, name));
600 void plugin_init_all (void)
602 const char *chain_name;
603 int (*callback) (void);
607 /* Init the value cache */
610 chain_name = global_option_get ("PreCacheChain");
611 pre_cache_chain = fc_chain_get_by_name (chain_name);
613 chain_name = global_option_get ("PostCacheChain");
614 post_cache_chain = fc_chain_get_by_name (chain_name);
617 if ((list_init == NULL) && (list_read == NULL))
620 /* Calling all init callbacks before checking if read callbacks
621 * are available allows the init callbacks to register the read
623 le = llist_head (list_init);
626 callback = (int (*) (void)) le->value;
627 status = (*callback) ();
631 ERROR ("Initialization of plugin `%s' "
632 "failed with status %i. "
633 "Plugin will be unloaded.",
635 /* Plugins that register read callbacks from the init
636 * callback should take care of appropriate error
637 * handling themselves. */
638 /* FIXME: Unload _all_ functions */
639 plugin_unregister_read (le->key);
645 /* Start read-threads */
646 if (list_read != NULL)
650 rt = global_option_get ("ReadThreads");
653 start_threads ((num > 0) ? num : 5);
655 } /* void plugin_init_all */
657 void plugin_read_all (void)
664 if (list_read == NULL)
667 pthread_mutex_lock (&read_lock);
669 le = llist_head (list_read);
672 rf = (read_func_t *) le->value;
674 if (rf->needs_read != DONE)
680 if (rf->wait_left > 0)
681 rf->wait_left -= interval_g;
683 if (rf->wait_left <= 0)
685 rf->needs_read = TODO;
691 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
692 pthread_cond_broadcast (&read_cond);
693 pthread_mutex_unlock (&read_lock);
694 } /* void plugin_read_all */
696 /* Read function called when the `-T' command line argument is given. */
697 int plugin_read_all_once (void)
702 int return_status = 0;
704 if (list_read == NULL)
706 NOTICE ("No read-functions are registered.");
710 for (le = llist_head (list_read);
714 rf = (read_func_t *) le->value;
715 status = rf->callback ();
718 NOTICE ("read-function of plugin `%s' failed.",
724 return (return_status);
725 } /* int plugin_read_all_once */
727 int plugin_write (const char *plugin, /* {{{ */
728 const data_set_t *ds, const value_list_t *vl)
730 int (*callback) (const data_set_t *ds, const value_list_t *vl);
737 if (list_write == NULL)
742 ds = plugin_get_ds (vl->type);
745 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
755 le = llist_head (list_write);
758 callback = le->value;
759 status = (*callback) (ds, vl);
768 if ((success == 0) && (failure != 0))
773 else /* plugin != NULL */
775 le = llist_head (list_write);
778 if (strcasecmp (plugin, le->key) == 0)
787 callback = le->value;
788 status = (*callback) (ds, vl);
792 } /* }}} int plugin_write */
794 int plugin_flush (const char *plugin, int timeout, const char *identifier)
796 int (*callback) (int timeout, const char *identifier);
799 if (list_flush == NULL)
802 le = llist_head (list_flush);
806 && (strcmp (plugin, le->key) != 0))
812 callback = (int (*) (int, const char *)) le->value;
813 (*callback) (timeout, identifier);
818 } /* int plugin_flush */
820 void plugin_shutdown_all (void)
822 int (*callback) (void);
827 if (list_shutdown == NULL)
830 le = llist_head (list_shutdown);
833 callback = (int (*) (void)) le->value;
835 /* Advance the pointer before calling the callback allows
836 * shutdown functions to unregister themselves. If done the
837 * other way around the memory `le' points to will be freed
838 * after callback returns. */
843 } /* void plugin_shutdown_all */
845 int plugin_dispatch_values (value_list_t *vl)
848 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
852 if ((vl == NULL) || (*vl->type == '\0')) {
853 ERROR ("plugin_dispatch_values: Invalid value list.");
857 if (list_write == NULL)
858 c_complain_once (LOG_WARNING, &no_write_complaint,
859 "plugin_dispatch_values: No write callback has been "
860 "registered. Please load at least one output plugin, "
861 "if you want the collected data to be stored.");
863 if (data_sets == NULL)
865 ERROR ("plugin_dispatch_values: No data sets registered. "
866 "Could the types database be read? Check "
867 "your `TypesDB' setting!");
871 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
873 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
878 vl->time = time (NULL);
880 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
882 "plugin = %s; plugin_instance = %s; "
883 "type = %s; type_instance = %s;",
884 (unsigned int) vl->time, vl->interval,
886 vl->plugin, vl->plugin_instance,
887 vl->type, vl->type_instance);
890 assert (0 == strcmp (ds->type, vl->type));
892 if (0 != strcmp (ds->type, vl->type))
893 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
898 assert (ds->ds_num == vl->values_len);
900 if (ds->ds_num != vl->values_len)
902 ERROR ("plugin_dispatch_values: ds->type = %s: "
903 "(ds->ds_num = %i) != "
904 "(vl->values_len = %i)",
905 ds->type, ds->ds_num, vl->values_len);
910 escape_slashes (vl->host, sizeof (vl->host));
911 escape_slashes (vl->plugin, sizeof (vl->plugin));
912 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
913 escape_slashes (vl->type, sizeof (vl->type));
914 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
916 if (pre_cache_chain != NULL)
918 status = fc_process_chain (ds, vl, pre_cache_chain);
921 WARNING ("plugin_dispatch_values: Running the "
922 "pre-cache chain failed with "
926 else if (status == FC_TARGET_STOP)
930 /* Update the value cache */
933 if (post_cache_chain != NULL)
935 status = fc_process_chain (ds, vl, post_cache_chain);
938 WARNING ("plugin_dispatch_values: Running the "
939 "post-cache chain failed with "
945 fc_default_action (ds, vl);
948 } /* int plugin_dispatch_values */
950 int plugin_dispatch_notification (const notification_t *notif)
952 int (*callback) (const notification_t *);
954 /* Possible TODO: Add flap detection here */
956 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
957 "time = %u; host = %s;",
958 notif->severity, notif->message,
959 (unsigned int) notif->time, notif->host);
961 /* Nobody cares for notifications */
962 if (list_notification == NULL)
965 le = llist_head (list_notification);
968 callback = (int (*) (const notification_t *)) le->value;
975 } /* int plugin_dispatch_notification */
977 void plugin_log (int level, const char *format, ...)
982 void (*callback) (int, const char *);
985 if (list_log == NULL)
989 if (level >= LOG_DEBUG)
993 va_start (ap, format);
994 vsnprintf (msg, sizeof (msg), format, ap);
995 msg[sizeof (msg) - 1] = '\0';
998 le = llist_head (list_log);
1001 callback = (void (*) (int, const char *)) le->value;
1002 (*callback) (level, msg);
1006 } /* void plugin_log */
1008 const data_set_t *plugin_get_ds (const char *name)
1012 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1014 DEBUG ("No such dataset registered: %s", name);
1019 } /* data_set_t *plugin_get_ds */
1021 static int plugin_notification_meta_add (notification_t *n,
1023 enum notification_meta_type_e type,
1026 notification_meta_t *meta;
1027 notification_meta_t *tail;
1029 if ((n == NULL) || (name == NULL) || (value == NULL))
1031 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1035 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1038 ERROR ("plugin_notification_meta_add: malloc failed.");
1041 memset (meta, 0, sizeof (notification_meta_t));
1043 sstrncpy (meta->name, name, sizeof (meta->name));
1048 case NM_TYPE_STRING:
1050 meta->nm_value.nm_string = strdup ((const char *) value);
1051 if (meta->nm_value.nm_string == NULL)
1053 ERROR ("plugin_notification_meta_add: strdup failed.");
1059 case NM_TYPE_SIGNED_INT:
1061 meta->nm_value.nm_signed_int = *((int64_t *) value);
1064 case NM_TYPE_UNSIGNED_INT:
1066 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1069 case NM_TYPE_DOUBLE:
1071 meta->nm_value.nm_double = *((double *) value);
1074 case NM_TYPE_BOOLEAN:
1076 meta->nm_value.nm_boolean = *((bool *) value);
1081 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1085 } /* switch (type) */
1089 while ((tail != NULL) && (tail->next != NULL))
1098 } /* int plugin_notification_meta_add */
1100 int plugin_notification_meta_add_string (notification_t *n,
1104 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1107 int plugin_notification_meta_add_signed_int (notification_t *n,
1111 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1114 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1118 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1121 int plugin_notification_meta_add_double (notification_t *n,
1125 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1128 int plugin_notification_meta_add_boolean (notification_t *n,
1132 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1135 int plugin_notification_meta_copy (notification_t *dst,
1136 const notification_t *src)
1138 notification_meta_t *meta;
1140 assert (dst != NULL);
1141 assert (src != NULL);
1142 assert (dst != src);
1143 assert ((src->meta == NULL) || (src->meta != dst->meta));
1145 for (meta = src->meta; meta != NULL; meta = meta->next)
1147 if (meta->type == NM_TYPE_STRING)
1148 plugin_notification_meta_add_string (dst, meta->name,
1149 meta->nm_value.nm_string);
1150 else if (meta->type == NM_TYPE_SIGNED_INT)
1151 plugin_notification_meta_add_signed_int (dst, meta->name,
1152 meta->nm_value.nm_signed_int);
1153 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1154 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1155 meta->nm_value.nm_unsigned_int);
1156 else if (meta->type == NM_TYPE_DOUBLE)
1157 plugin_notification_meta_add_double (dst, meta->name,
1158 meta->nm_value.nm_double);
1159 else if (meta->type == NM_TYPE_BOOLEAN)
1160 plugin_notification_meta_add_boolean (dst, meta->name,
1161 meta->nm_value.nm_boolean);
1165 } /* int plugin_notification_meta_copy */
1167 int plugin_notification_meta_free (notification_t *n)
1169 notification_meta_t *this;
1170 notification_meta_t *next;
1174 ERROR ("plugin_notification_meta_free: n == NULL!");
1180 while (this != NULL)
1184 if (this->type == NM_TYPE_STRING)
1186 free ((char *)this->nm_value.nm_string);
1187 this->nm_value.nm_string = NULL;
1195 } /* int plugin_notification_meta_free */