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>
33 #include "configfile.h"
34 #include "utils_avltree.h"
35 #include "utils_llist.h"
36 #include "utils_cache.h"
37 #include "utils_threshold.h"
46 int (*callback) (void);
47 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
49 typedef struct read_func_s read_func_t;
54 static llist_t *list_init;
55 static llist_t *list_read;
56 static llist_t *list_write;
57 static llist_t *list_flush;
58 static llist_t *list_shutdown;
59 static llist_t *list_log;
60 static llist_t *list_notification;
62 static c_avl_tree_t *data_sets;
64 static char *plugindir = NULL;
66 static int read_loop = 1;
67 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
68 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
69 static pthread_t *read_threads = NULL;
70 static int read_threads_num = 0;
75 static const char *plugin_get_dir (void)
77 if (plugindir == NULL)
83 static int register_callback (llist_t **list, const char *name, void *callback)
89 && ((*list = llist_create ()) == NULL))
92 le = llist_search (*list, name);
99 le = llentry_create (key, callback);
106 llist_append (*list, le);
110 le->value = callback;
114 } /* int register_callback */
116 static int plugin_unregister (llist_t *list, const char *name)
120 e = llist_search (list, name);
125 llist_remove (list, e);
130 } /* int plugin_unregister */
133 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
134 * object, but it will bitch about a shared object not having a
135 * ``module_register'' symbol..
137 static int plugin_load_file (char *file)
140 void (*reg_handle) (void);
142 DEBUG ("file = %s", file);
145 lt_dlerror (); /* clear errors */
147 if ((dlh = lt_dlopen (file)) == NULL)
149 const char *error = lt_dlerror ();
151 ERROR ("lt_dlopen failed: %s", error);
152 fprintf (stderr, "lt_dlopen failed: %s\n", error);
156 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
158 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
159 file, lt_dlerror ());
169 static void *plugin_read_thread (void *args)
176 pthread_mutex_lock (&read_lock);
178 while (read_loop != 0)
180 le = llist_head (list_read);
183 while ((read_loop != 0) && (le != NULL))
185 rf = (read_func_t *) le->value;
187 if (rf->needs_read != TODO)
193 /* We will do this read function */
194 rf->needs_read = ACTIVE;
196 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
197 (unsigned long int) pthread_self (), le->key);
198 pthread_mutex_unlock (&read_lock);
200 status = rf->callback ();
205 if (rf->wait_time < interval_g)
206 rf->wait_time = interval_g;
207 rf->wait_left = rf->wait_time;
208 rf->wait_time = rf->wait_time * 2;
209 if (rf->wait_time > 86400)
210 rf->wait_time = 86400;
212 NOTICE ("read-function of plugin `%s' "
213 "failed. Will suspend it for %i "
214 "seconds.", le->key, rf->wait_left);
219 rf->wait_time = interval_g;
222 pthread_mutex_lock (&read_lock);
224 rf->needs_read = DONE;
226 } /* while (le != NULL) */
228 if ((read_loop != 0) && (done == 0))
230 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
231 (unsigned long int) pthread_self ());
232 pthread_cond_wait (&read_cond, &read_lock);
234 } /* while (read_loop) */
236 pthread_mutex_unlock (&read_lock);
239 } /* void *plugin_read_thread */
241 static void start_threads (int num)
245 if (read_threads != NULL)
248 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
249 if (read_threads == NULL)
251 ERROR ("plugin: start_threads: calloc failed.");
255 read_threads_num = 0;
256 for (i = 0; i < num; i++)
258 if (pthread_create (read_threads + read_threads_num, NULL,
259 plugin_read_thread, NULL) == 0)
265 ERROR ("plugin: start_threads: pthread_create failed.");
269 } /* void start_threads */
271 static void stop_threads (void)
275 pthread_mutex_lock (&read_lock);
277 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
278 pthread_cond_broadcast (&read_cond);
279 pthread_mutex_unlock (&read_lock);
281 for (i = 0; i < read_threads_num; i++)
283 if (pthread_join (read_threads[i], NULL) != 0)
285 ERROR ("plugin: stop_threads: pthread_join failed.");
287 read_threads[i] = (pthread_t) 0;
289 sfree (read_threads);
290 read_threads_num = 0;
291 } /* void stop_threads */
296 void plugin_set_dir (const char *dir)
298 if (plugindir != NULL)
303 else if ((plugindir = strdup (dir)) == NULL)
306 ERROR ("strdup failed: %s",
307 sstrerror (errno, errbuf, sizeof (errbuf)));
312 int plugin_load (const char *type)
316 char filename[BUFSIZE];
317 char typename[BUFSIZE];
323 DEBUG ("type = %s", type);
325 dir = plugin_get_dir ();
328 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
329 * type when matching the filename */
330 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
332 WARNING ("snprintf: truncated: `%s.so'", type);
335 typename_len = strlen (typename);
337 if ((dh = opendir (dir)) == NULL)
340 ERROR ("opendir (%s): %s", dir,
341 sstrerror (errno, errbuf, sizeof (errbuf)));
345 while ((de = readdir (dh)) != NULL)
347 if (strncasecmp (de->d_name, typename, typename_len))
350 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
352 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
356 if (lstat (filename, &statbuf) == -1)
359 WARNING ("stat %s: %s", filename,
360 sstrerror (errno, errbuf, sizeof (errbuf)));
363 else if (!S_ISREG (statbuf.st_mode))
365 /* don't follow symlinks */
369 if (plugin_load_file (filename) == 0)
377 fprintf (stderr, "Unable to load plugin %s.\n", type);
387 * The `register_*' functions follow
389 int plugin_register_config (const char *name,
390 int (*callback) (const char *key, const char *val),
391 const char **keys, int keys_num)
393 cf_register (name, callback, keys, keys_num);
395 } /* int plugin_register_config */
397 int plugin_register_complex_config (const char *type,
398 int (*callback) (oconfig_item_t *))
400 return (cf_register_complex (type, callback));
401 } /* int plugin_register_complex_config */
403 int plugin_register_init (const char *name,
404 int (*callback) (void))
406 return (register_callback (&list_init, name, (void *) callback));
407 } /* plugin_register_init */
409 int plugin_register_read (const char *name,
410 int (*callback) (void))
414 rf = (read_func_t *) malloc (sizeof (read_func_t));
418 ERROR ("plugin_register_read: malloc failed: %s",
419 sstrerror (errno, errbuf, sizeof (errbuf)));
423 memset (rf, '\0', sizeof (read_func_t));
424 rf->wait_time = interval_g;
426 rf->callback = callback;
427 rf->needs_read = DONE;
429 return (register_callback (&list_read, name, (void *) rf));
430 } /* int plugin_register_read */
432 int plugin_register_write (const char *name,
433 int (*callback) (const data_set_t *ds, const value_list_t *vl))
435 return (register_callback (&list_write, name, (void *) callback));
436 } /* int plugin_register_write */
438 int plugin_register_flush (const char *name, int (*callback) (const int))
440 return (register_callback (&list_flush, name, (void *) callback));
441 } /* int plugin_register_flush */
443 int plugin_register_shutdown (char *name,
444 int (*callback) (void))
446 return (register_callback (&list_shutdown, name, (void *) callback));
447 } /* int plugin_register_shutdown */
449 int plugin_register_data_set (const data_set_t *ds)
454 if ((data_sets != NULL)
455 && (c_avl_get (data_sets, ds->type, NULL) == 0))
457 NOTICE ("Replacing DS `%s' with another version.", ds->type);
458 plugin_unregister_data_set (ds->type);
460 else if (data_sets == NULL)
462 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
463 if (data_sets == NULL)
467 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
470 memcpy(ds_copy, ds, sizeof (data_set_t));
472 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
474 if (ds_copy->ds == NULL)
480 for (i = 0; i < ds->ds_num; i++)
481 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
483 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
484 } /* int plugin_register_data_set */
486 int plugin_register_log (char *name,
487 void (*callback) (int priority, const char *msg))
489 return (register_callback (&list_log, name, (void *) callback));
490 } /* int plugin_register_log */
492 int plugin_register_notification (const char *name,
493 int (*callback) (const notification_t *notif))
495 return (register_callback (&list_notification, name, (void *) callback));
496 } /* int plugin_register_log */
498 int plugin_unregister_config (const char *name)
500 cf_unregister (name);
502 } /* int plugin_unregister_config */
504 int plugin_unregister_complex_config (const char *name)
506 cf_unregister_complex (name);
508 } /* int plugin_unregister_complex_config */
510 int plugin_unregister_init (const char *name)
512 return (plugin_unregister (list_init, name));
515 int plugin_unregister_read (const char *name)
519 e = llist_search (list_read, name);
524 llist_remove (list_read, e);
532 int plugin_unregister_write (const char *name)
534 return (plugin_unregister (list_write, name));
537 int plugin_unregister_flush (const char *name)
539 return (plugin_unregister (list_flush, name));
542 int plugin_unregister_shutdown (const char *name)
544 return (plugin_unregister (list_shutdown, name));
547 int plugin_unregister_data_set (const char *name)
551 if (data_sets == NULL)
554 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
561 } /* int plugin_unregister_data_set */
563 int plugin_unregister_log (const char *name)
565 return (plugin_unregister (list_log, name));
568 int plugin_unregister_notification (const char *name)
570 return (plugin_unregister (list_notification, name));
573 void plugin_init_all (void)
575 int (*callback) (void);
579 /* Start read-threads */
580 if (list_read != NULL)
584 rt = global_option_get ("ReadThreads");
586 start_threads ((num > 0) ? num : 5);
589 /* Init the value cache */
592 if (list_init == NULL)
595 le = llist_head (list_init);
598 callback = (int (*) (void)) le->value;
599 status = (*callback) ();
603 ERROR ("Initialization of plugin `%s' "
604 "failed with status %i. "
605 "Plugin will be unloaded.",
607 /* FIXME: Unload _all_ functions */
608 plugin_unregister_read (le->key);
613 } /* void plugin_init_all */
615 void plugin_read_all (void)
622 if (list_read == NULL)
625 pthread_mutex_lock (&read_lock);
627 le = llist_head (list_read);
630 rf = (read_func_t *) le->value;
632 if (rf->needs_read != DONE)
638 if (rf->wait_left > 0)
639 rf->wait_left -= interval_g;
641 if (rf->wait_left <= 0)
643 rf->needs_read = TODO;
649 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
650 pthread_cond_broadcast (&read_cond);
651 pthread_mutex_unlock (&read_lock);
652 } /* void plugin_read_all */
654 int plugin_flush_one (int timeout, const char *name)
656 int (*callback) (int);
660 if (list_flush == NULL)
663 le = llist_search (list_flush, name);
666 callback = (int (*) (int)) le->value;
668 status = (*callback) (timeout);
671 } /* int plugin_flush_ont */
673 void plugin_flush_all (int timeout)
675 int (*callback) (int);
678 if (list_flush == NULL)
681 le = llist_head (list_flush);
684 callback = (int (*) (int)) le->value;
687 (*callback) (timeout);
689 } /* void plugin_flush_all */
691 void plugin_shutdown_all (void)
693 int (*callback) (void);
698 if (list_shutdown == NULL)
701 le = llist_head (list_shutdown);
704 callback = (int (*) (void)) le->value;
706 /* Advance the pointer before calling the callback allows
707 * shutdown functions to unregister themselves. If done the
708 * other way around the memory `le' points to will be freed
709 * after callback returns. */
714 } /* void plugin_shutdown_all */
716 int plugin_dispatch_values (const char *name, value_list_t *vl)
718 int (*callback) (const data_set_t *, const value_list_t *);
722 if ((list_write == NULL) || (data_sets == NULL))
725 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
727 DEBUG ("No such dataset registered: %s", name);
731 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
733 "plugin = %s; plugin_instance = %s; "
734 "type = %s; type_instance = %s;",
735 (unsigned int) vl->time, vl->interval,
737 vl->plugin, vl->plugin_instance,
738 ds->type, vl->type_instance);
741 assert (ds->ds_num == vl->values_len);
743 if (ds->ds_num != vl->values_len)
745 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
746 "(vl->values_len = %i)",
747 ds->type, ds->ds_num, vl->values_len);
752 escape_slashes (vl->host, sizeof (vl->host));
753 escape_slashes (vl->plugin, sizeof (vl->plugin));
754 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
755 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
757 /* Update the value cache */
759 ut_check_threshold (ds, vl);
761 le = llist_head (list_write);
764 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
765 (*callback) (ds, vl);
771 } /* int plugin_dispatch_values */
773 int plugin_dispatch_notification (const notification_t *notif)
775 int (*callback) (const notification_t *);
777 /* Possible TODO: Add flap detection here */
779 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
780 "time = %u; host = %s;",
781 notif->severity, notif->message,
782 (unsigned int) notif->time, notif->host);
784 /* Nobody cares for notifications */
785 if (list_notification == NULL)
788 le = llist_head (list_notification);
791 callback = (int (*) (const notification_t *)) le->value;
798 } /* int plugin_dispatch_notification */
800 void plugin_log (int level, const char *format, ...)
805 void (*callback) (int, const char *);
808 if (list_log == NULL)
812 if (level >= LOG_DEBUG)
816 va_start (ap, format);
817 vsnprintf (msg, 512, format, ap);
821 le = llist_head (list_log);
824 callback = (void (*) (int, const char *)) le->value;
825 (*callback) (level, msg);
829 } /* void plugin_log */
831 const data_set_t *plugin_get_ds (const char *name)
835 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
837 DEBUG ("No such dataset registered: %s", name);
842 } /* data_set_t *plugin_get_ds */