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_flush;
59 static llist_t *list_shutdown;
60 static llist_t *list_log;
61 static llist_t *list_notification;
63 static c_avl_tree_t *data_sets;
65 static char *plugindir = NULL;
67 static int read_loop = 1;
68 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
69 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
70 static pthread_t *read_threads = NULL;
71 static int read_threads_num = 0;
76 static const char *plugin_get_dir (void)
78 if (plugindir == NULL)
84 static int register_callback (llist_t **list, const char *name, void *callback)
90 && ((*list = llist_create ()) == NULL))
93 le = llist_search (*list, name);
100 le = llentry_create (key, callback);
107 llist_append (*list, le);
111 le->value = callback;
115 } /* int register_callback */
117 static int plugin_unregister (llist_t *list, const char *name)
121 e = llist_search (list, name);
126 llist_remove (list, e);
131 } /* int plugin_unregister */
134 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
135 * object, but it will bitch about a shared object not having a
136 * ``module_register'' symbol..
138 static int plugin_load_file (char *file)
141 void (*reg_handle) (void);
143 DEBUG ("file = %s", file);
146 lt_dlerror (); /* clear errors */
148 if ((dlh = lt_dlopen (file)) == NULL)
150 const char *error = lt_dlerror ();
152 ERROR ("lt_dlopen (%s) failed: %s", file, error);
153 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
157 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
159 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
160 file, lt_dlerror ());
170 static void *plugin_read_thread (void *args)
177 pthread_mutex_lock (&read_lock);
179 while (read_loop != 0)
181 le = llist_head (list_read);
184 while ((read_loop != 0) && (le != NULL))
186 rf = (read_func_t *) le->value;
188 if (rf->needs_read != TODO)
194 /* We will do this read function */
195 rf->needs_read = ACTIVE;
197 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
198 (unsigned long int) pthread_self (), le->key);
199 pthread_mutex_unlock (&read_lock);
201 status = rf->callback ();
206 if (rf->wait_time < interval_g)
207 rf->wait_time = interval_g;
208 rf->wait_left = rf->wait_time;
209 rf->wait_time = rf->wait_time * 2;
210 if (rf->wait_time > 86400)
211 rf->wait_time = 86400;
213 NOTICE ("read-function of plugin `%s' "
214 "failed. Will suspend it for %i "
215 "seconds.", le->key, rf->wait_left);
220 rf->wait_time = interval_g;
223 pthread_mutex_lock (&read_lock);
225 rf->needs_read = DONE;
227 } /* while (le != NULL) */
229 if ((read_loop != 0) && (done == 0))
231 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
232 (unsigned long int) pthread_self ());
233 pthread_cond_wait (&read_cond, &read_lock);
235 } /* while (read_loop) */
237 pthread_mutex_unlock (&read_lock);
241 } /* void *plugin_read_thread */
243 static void start_threads (int num)
247 if (read_threads != NULL)
250 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
251 if (read_threads == NULL)
253 ERROR ("plugin: start_threads: calloc failed.");
257 read_threads_num = 0;
258 for (i = 0; i < num; i++)
260 if (pthread_create (read_threads + read_threads_num, NULL,
261 plugin_read_thread, NULL) == 0)
267 ERROR ("plugin: start_threads: pthread_create failed.");
271 } /* void start_threads */
273 static void stop_threads (void)
277 pthread_mutex_lock (&read_lock);
279 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
280 pthread_cond_broadcast (&read_cond);
281 pthread_mutex_unlock (&read_lock);
283 for (i = 0; i < read_threads_num; i++)
285 if (pthread_join (read_threads[i], NULL) != 0)
287 ERROR ("plugin: stop_threads: pthread_join failed.");
289 read_threads[i] = (pthread_t) 0;
291 sfree (read_threads);
292 read_threads_num = 0;
293 } /* void stop_threads */
298 void plugin_set_dir (const char *dir)
300 if (plugindir != NULL)
305 else if ((plugindir = strdup (dir)) == NULL)
308 ERROR ("strdup failed: %s",
309 sstrerror (errno, errbuf, sizeof (errbuf)));
314 int plugin_load (const char *type)
318 char filename[BUFSIZE] = "";
319 char typename[BUFSIZE];
325 DEBUG ("type = %s", type);
327 dir = plugin_get_dir ();
330 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
331 * type when matching the filename */
332 if (ssnprintf (typename, sizeof (typename),
333 "%s.so", type) >= sizeof (typename))
335 WARNING ("snprintf: truncated: `%s.so'", type);
338 typename_len = strlen (typename);
340 if ((dh = opendir (dir)) == NULL)
343 ERROR ("opendir (%s): %s", dir,
344 sstrerror (errno, errbuf, sizeof (errbuf)));
348 while ((de = readdir (dh)) != NULL)
350 if (strncasecmp (de->d_name, typename, typename_len))
353 if (ssnprintf (filename, sizeof (filename),
354 "%s/%s", dir, de->d_name) >= sizeof (filename))
356 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
360 if (lstat (filename, &statbuf) == -1)
363 WARNING ("stat %s: %s", filename,
364 sstrerror (errno, errbuf, sizeof (errbuf)));
367 else if (!S_ISREG (statbuf.st_mode))
369 /* don't follow symlinks */
370 WARNING ("stat %s: not a regular file", filename);
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_flush (const char *name,
447 int (*callback) (const int timeout, const char *identifier))
449 return (register_callback (&list_flush, name, (void *) callback));
450 } /* int plugin_register_flush */
452 int plugin_register_shutdown (char *name,
453 int (*callback) (void))
455 return (register_callback (&list_shutdown, name, (void *) callback));
456 } /* int plugin_register_shutdown */
458 int plugin_register_data_set (const data_set_t *ds)
463 if ((data_sets != NULL)
464 && (c_avl_get (data_sets, ds->type, NULL) == 0))
466 NOTICE ("Replacing DS `%s' with another version.", ds->type);
467 plugin_unregister_data_set (ds->type);
469 else if (data_sets == NULL)
471 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
472 if (data_sets == NULL)
476 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
479 memcpy(ds_copy, ds, sizeof (data_set_t));
481 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
483 if (ds_copy->ds == NULL)
489 for (i = 0; i < ds->ds_num; i++)
490 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
492 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
493 } /* int plugin_register_data_set */
495 int plugin_register_log (char *name,
496 void (*callback) (int priority, const char *msg))
498 return (register_callback (&list_log, name, (void *) callback));
499 } /* int plugin_register_log */
501 int plugin_register_notification (const char *name,
502 int (*callback) (const notification_t *notif))
504 return (register_callback (&list_notification, name, (void *) callback));
505 } /* int plugin_register_log */
507 int plugin_unregister_config (const char *name)
509 cf_unregister (name);
511 } /* int plugin_unregister_config */
513 int plugin_unregister_complex_config (const char *name)
515 cf_unregister_complex (name);
517 } /* int plugin_unregister_complex_config */
519 int plugin_unregister_init (const char *name)
521 return (plugin_unregister (list_init, name));
524 int plugin_unregister_read (const char *name)
528 e = llist_search (list_read, name);
533 llist_remove (list_read, e);
541 int plugin_unregister_write (const char *name)
543 return (plugin_unregister (list_write, name));
546 int plugin_unregister_flush (const char *name)
548 return (plugin_unregister (list_flush, name));
551 int plugin_unregister_shutdown (const char *name)
553 return (plugin_unregister (list_shutdown, name));
556 int plugin_unregister_data_set (const char *name)
560 if (data_sets == NULL)
563 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
570 } /* int plugin_unregister_data_set */
572 int plugin_unregister_log (const char *name)
574 return (plugin_unregister (list_log, name));
577 int plugin_unregister_notification (const char *name)
579 return (plugin_unregister (list_notification, name));
582 void plugin_init_all (void)
584 int (*callback) (void);
588 /* Init the value cache */
591 if ((list_init == NULL) && (list_read == NULL))
594 /* Calling all init callbacks before checking if read callbacks
595 * are available allows the init callbacks to register the read
597 le = llist_head (list_init);
600 callback = (int (*) (void)) le->value;
601 status = (*callback) ();
605 ERROR ("Initialization of plugin `%s' "
606 "failed with status %i. "
607 "Plugin will be unloaded.",
609 /* Plugins that register read callbacks from the init
610 * callback should take care of appropriate error
611 * handling themselves. */
612 /* FIXME: Unload _all_ functions */
613 plugin_unregister_read (le->key);
619 /* Start read-threads */
620 if (list_read != NULL)
624 rt = global_option_get ("ReadThreads");
626 start_threads ((num > 0) ? num : 5);
628 } /* void plugin_init_all */
630 void plugin_read_all (void)
637 if (list_read == NULL)
640 pthread_mutex_lock (&read_lock);
642 le = llist_head (list_read);
645 rf = (read_func_t *) le->value;
647 if (rf->needs_read != DONE)
653 if (rf->wait_left > 0)
654 rf->wait_left -= interval_g;
656 if (rf->wait_left <= 0)
658 rf->needs_read = TODO;
664 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
665 pthread_cond_broadcast (&read_cond);
666 pthread_mutex_unlock (&read_lock);
667 } /* void plugin_read_all */
669 int plugin_flush (const char *plugin, int timeout, const char *identifier)
671 int (*callback) (int timeout, const char *identifier);
674 if (list_flush == NULL)
677 le = llist_head (list_flush);
681 && (strcmp (plugin, le->key) != 0))
687 callback = (int (*) (int, const char *)) le->value;
688 (*callback) (timeout, identifier);
693 } /* int plugin_flush */
695 void plugin_shutdown_all (void)
697 int (*callback) (void);
702 if (list_shutdown == NULL)
705 le = llist_head (list_shutdown);
708 callback = (int (*) (void)) le->value;
710 /* Advance the pointer before calling the callback allows
711 * shutdown functions to unregister themselves. If done the
712 * other way around the memory `le' points to will be freed
713 * after callback returns. */
718 } /* void plugin_shutdown_all */
720 int plugin_dispatch_values (value_list_t *vl)
722 static c_complain_t no_write_complaint = C_COMPLAIN_INIT;
724 int (*callback) (const data_set_t *, const value_list_t *);
728 if ((vl == NULL) || (*vl->type == '\0')) {
729 ERROR ("plugin_dispatch_values: Invalid value list.");
733 if (list_write == NULL)
734 c_complain_once (LOG_WARNING, &no_write_complaint,
735 "plugin_dispatch_values: No write callback has been "
736 "registered. Please load at least one output plugin, "
737 "if you want the collected data to be stored.");
739 if (data_sets == NULL)
741 ERROR ("plugin_dispatch_values: No data sets registered. "
742 "Could the types database be read? Check "
743 "your `TypesDB' setting!");
747 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
749 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
753 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
755 "plugin = %s; plugin_instance = %s; "
756 "type = %s; type_instance = %s;",
757 (unsigned int) vl->time, vl->interval,
759 vl->plugin, vl->plugin_instance,
760 vl->type, vl->type_instance);
763 assert (0 == strcmp (ds->type, vl->type));
765 if (0 != strcmp (ds->type, vl->type))
766 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
771 assert (ds->ds_num == vl->values_len);
773 if (ds->ds_num != vl->values_len)
775 ERROR ("plugin_dispatch_values: ds->type = %s: "
776 "(ds->ds_num = %i) != "
777 "(vl->values_len = %i)",
778 ds->type, ds->ds_num, vl->values_len);
783 escape_slashes (vl->host, sizeof (vl->host));
784 escape_slashes (vl->plugin, sizeof (vl->plugin));
785 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
786 escape_slashes (vl->type, sizeof (vl->type));
787 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
789 /* Update the value cache */
791 ut_check_threshold (ds, vl);
793 le = llist_head (list_write);
796 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
797 (*callback) (ds, vl);
803 } /* int plugin_dispatch_values */
805 int plugin_dispatch_notification (const notification_t *notif)
807 int (*callback) (const notification_t *);
809 /* Possible TODO: Add flap detection here */
811 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
812 "time = %u; host = %s;",
813 notif->severity, notif->message,
814 (unsigned int) notif->time, notif->host);
816 /* Nobody cares for notifications */
817 if (list_notification == NULL)
820 le = llist_head (list_notification);
823 callback = (int (*) (const notification_t *)) le->value;
830 } /* int plugin_dispatch_notification */
832 void plugin_log (int level, const char *format, ...)
837 void (*callback) (int, const char *);
840 if (list_log == NULL)
844 if (level >= LOG_DEBUG)
848 va_start (ap, format);
849 vsnprintf (msg, sizeof (msg), format, ap);
850 msg[sizeof (msg) - 1] = '\0';
853 le = llist_head (list_log);
856 callback = (void (*) (int, const char *)) le->value;
857 (*callback) (level, msg);
861 } /* void plugin_log */
863 const data_set_t *plugin_get_ds (const char *name)
867 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
869 DEBUG ("No such dataset registered: %s", name);
874 } /* data_set_t *plugin_get_ds */
876 static int plugin_notification_meta_add (notification_t *n,
878 enum notification_meta_type_e type,
881 notification_meta_t *meta;
882 notification_meta_t *tail;
884 if ((n == NULL) || (name == NULL) || (value == NULL))
886 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
890 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
893 ERROR ("plugin_notification_meta_add: malloc failed.");
896 memset (meta, 0, sizeof (notification_meta_t));
898 sstrncpy (meta->name, name, sizeof (meta->name));
905 meta->nm_value.nm_string = strdup ((const char *) value);
906 if (meta->nm_value.nm_string == NULL)
908 ERROR ("plugin_notification_meta_add: strdup failed.");
914 case NM_TYPE_SIGNED_INT:
916 meta->nm_value.nm_signed_int = *((int64_t *) value);
919 case NM_TYPE_UNSIGNED_INT:
921 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
926 meta->nm_value.nm_double = *((double *) value);
929 case NM_TYPE_BOOLEAN:
931 meta->nm_value.nm_boolean = *((bool *) value);
936 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
940 } /* switch (type) */
944 while ((tail != NULL) && (tail->next != NULL))
953 } /* int plugin_notification_meta_add */
955 int plugin_notification_meta_add_string (notification_t *n,
959 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
962 int plugin_notification_meta_add_signed_int (notification_t *n,
966 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
969 int plugin_notification_meta_add_unsigned_int (notification_t *n,
973 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
976 int plugin_notification_meta_add_double (notification_t *n,
980 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
983 int plugin_notification_meta_add_boolean (notification_t *n,
987 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
990 int plugin_notification_meta_copy (notification_t *dst,
991 const notification_t *src)
993 notification_meta_t *meta;
995 assert (dst != NULL);
996 assert (src != NULL);
998 assert ((src->meta == NULL) || (src->meta != dst->meta));
1000 for (meta = src->meta; meta != NULL; meta = meta->next)
1002 if (meta->type == NM_TYPE_STRING)
1003 plugin_notification_meta_add_string (dst, meta->name,
1004 meta->nm_value.nm_string);
1005 else if (meta->type == NM_TYPE_SIGNED_INT)
1006 plugin_notification_meta_add_signed_int (dst, meta->name,
1007 meta->nm_value.nm_signed_int);
1008 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1009 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1010 meta->nm_value.nm_unsigned_int);
1011 else if (meta->type == NM_TYPE_DOUBLE)
1012 plugin_notification_meta_add_double (dst, meta->name,
1013 meta->nm_value.nm_double);
1014 else if (meta->type == NM_TYPE_BOOLEAN)
1015 plugin_notification_meta_add_boolean (dst, meta->name,
1016 meta->nm_value.nm_boolean);
1020 } /* int plugin_notification_meta_copy */
1022 int plugin_notification_meta_free (notification_t *n)
1024 notification_meta_t *this;
1025 notification_meta_t *next;
1029 ERROR ("plugin_notification_meta_free: n == NULL!");
1035 while (this != NULL)
1039 if (this->type == NM_TYPE_STRING)
1041 free ((char *)this->nm_value.nm_string);
1042 this->nm_value.nm_string = NULL;
1050 } /* int plugin_notification_meta_free */