2 * collectd - src/plugin.c
3 * Copyright (C) 2005,2006 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>
32 #include "configfile.h"
33 #include "utils_llist.h"
34 #include "utils_cache.h"
35 #include "utils_threshold.h"
44 int (*callback) (void);
45 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
47 typedef struct read_func_s read_func_t;
52 static llist_t *list_init;
53 static llist_t *list_read;
54 static llist_t *list_write;
55 static llist_t *list_shutdown;
56 static llist_t *list_data_set;
57 static llist_t *list_log;
58 static llist_t *list_notification;
60 static char *plugindir = NULL;
62 static int read_loop = 1;
63 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
64 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
65 static pthread_t *read_threads = NULL;
66 static int read_threads_num = 0;
71 static const char *plugin_get_dir (void)
73 if (plugindir == NULL)
79 static int register_callback (llist_t **list, const char *name, void *callback)
84 && ((*list = llist_create ()) == NULL))
87 le = llist_search (*list, name);
90 le = llentry_create (name, callback);
94 llist_append (*list, le);
102 } /* int register_callback */
104 static int plugin_unregister (llist_t *list, const char *name)
108 e = llist_search (list, name);
113 llist_remove (list, e);
117 } /* int plugin_unregister */
120 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
121 * object, but it will bitch about a shared object not having a
122 * ``module_register'' symbol..
124 static int plugin_load_file (char *file)
127 void (*reg_handle) (void);
129 DEBUG ("file = %s", file);
132 lt_dlerror (); /* clear errors */
134 if ((dlh = lt_dlopen (file)) == NULL)
136 const char *error = lt_dlerror ();
138 ERROR ("lt_dlopen failed: %s", error);
139 fprintf (stderr, "lt_dlopen failed: %s\n", error);
143 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
145 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
146 file, lt_dlerror ());
156 static void *plugin_read_thread (void *args)
163 pthread_mutex_lock (&read_lock);
165 while (read_loop != 0)
167 le = llist_head (list_read);
170 while ((read_loop != 0) && (le != NULL))
172 rf = (read_func_t *) le->value;
174 if (rf->needs_read != TODO)
180 /* We will do this read function */
181 rf->needs_read = ACTIVE;
183 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
184 (unsigned long int) pthread_self (), le->key);
185 pthread_mutex_unlock (&read_lock);
187 status = rf->callback ();
192 if (rf->wait_time < interval_g)
193 rf->wait_time = interval_g;
194 rf->wait_left = rf->wait_time;
195 rf->wait_time = rf->wait_time * 2;
196 if (rf->wait_time > 86400)
197 rf->wait_time = 86400;
199 NOTICE ("read-function of plugin `%s' "
200 "failed. Will suspend it for %i "
201 "seconds.", le->key, rf->wait_left);
206 rf->wait_time = interval_g;
209 pthread_mutex_lock (&read_lock);
211 rf->needs_read = DONE;
213 } /* while (le != NULL) */
215 if ((read_loop != 0) && (done == 0))
217 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
218 (unsigned long int) pthread_self ());
219 pthread_cond_wait (&read_cond, &read_lock);
221 } /* while (read_loop) */
223 pthread_mutex_unlock (&read_lock);
226 } /* void *plugin_read_thread */
228 static void start_threads (int num)
232 if (read_threads != NULL)
235 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
236 if (read_threads == NULL)
238 ERROR ("plugin: start_threads: calloc failed.");
242 read_threads_num = 0;
243 for (i = 0; i < num; i++)
245 if (pthread_create (read_threads + read_threads_num, NULL,
246 plugin_read_thread, NULL) == 0)
252 ERROR ("plugin: start_threads: pthread_create failed.");
256 } /* void start_threads */
258 static void stop_threads (void)
262 pthread_mutex_lock (&read_lock);
264 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
265 pthread_cond_broadcast (&read_cond);
266 pthread_mutex_unlock (&read_lock);
268 for (i = 0; i < read_threads_num; i++)
270 if (pthread_join (read_threads[i], NULL) != 0)
272 ERROR ("plugin: stop_threads: pthread_join failed.");
274 read_threads[i] = (pthread_t) 0;
276 sfree (read_threads);
277 read_threads_num = 0;
278 } /* void stop_threads */
283 void plugin_set_dir (const char *dir)
285 if (plugindir != NULL)
290 else if ((plugindir = strdup (dir)) == NULL)
293 ERROR ("strdup failed: %s",
294 sstrerror (errno, errbuf, sizeof (errbuf)));
299 int plugin_load (const char *type)
303 char filename[BUFSIZE];
304 char typename[BUFSIZE];
310 DEBUG ("type = %s", type);
312 dir = plugin_get_dir ();
315 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
316 * type when matching the filename */
317 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
319 WARNING ("snprintf: truncated: `%s.so'", type);
322 typename_len = strlen (typename);
324 if ((dh = opendir (dir)) == NULL)
327 ERROR ("opendir (%s): %s", dir,
328 sstrerror (errno, errbuf, sizeof (errbuf)));
332 while ((de = readdir (dh)) != NULL)
334 if (strncasecmp (de->d_name, typename, typename_len))
337 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
339 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
343 if (lstat (filename, &statbuf) == -1)
346 WARNING ("stat %s: %s", filename,
347 sstrerror (errno, errbuf, sizeof (errbuf)));
350 else if (!S_ISREG (statbuf.st_mode))
352 /* don't follow symlinks */
356 if (plugin_load_file (filename) == 0)
364 fprintf (stderr, "Unable to load plugin %s.\n", type);
374 * The `register_*' functions follow
376 int plugin_register_config (const char *name,
377 int (*callback) (const char *key, const char *val),
378 const char **keys, int keys_num)
380 cf_register (name, callback, keys, keys_num);
382 } /* int plugin_register_config */
384 int plugin_register_complex_config (const char *type,
385 int (*callback) (oconfig_item_t *))
387 return (cf_register_complex (type, callback));
388 } /* int plugin_register_complex_config */
390 int plugin_register_init (const char *name,
391 int (*callback) (void))
393 return (register_callback (&list_init, name, (void *) callback));
394 } /* plugin_register_init */
396 int plugin_register_read (const char *name,
397 int (*callback) (void))
401 rf = (read_func_t *) malloc (sizeof (read_func_t));
405 ERROR ("plugin_register_read: malloc failed: %s",
406 sstrerror (errno, errbuf, sizeof (errbuf)));
410 memset (rf, '\0', sizeof (read_func_t));
411 rf->wait_time = interval_g;
413 rf->callback = callback;
414 rf->needs_read = DONE;
416 return (register_callback (&list_read, name, (void *) rf));
417 } /* int plugin_register_read */
419 int plugin_register_write (const char *name,
420 int (*callback) (const data_set_t *ds, const value_list_t *vl))
422 return (register_callback (&list_write, name, (void *) callback));
423 } /* int plugin_register_write */
425 int plugin_register_shutdown (char *name,
426 int (*callback) (void))
428 return (register_callback (&list_shutdown, name, (void *) callback));
429 } /* int plugin_register_shutdown */
431 int plugin_register_data_set (const data_set_t *ds)
436 if ((list_data_set != NULL)
437 && (llist_search (list_data_set, ds->type) != NULL))
439 NOTICE ("Replacing DS `%s' with another version.", ds->type);
440 plugin_unregister_data_set (ds->type);
443 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
446 memcpy(ds_copy, ds, sizeof (data_set_t));
448 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
450 if (ds_copy->ds == NULL)
456 for (i = 0; i < ds->ds_num; i++)
457 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
459 return (register_callback (&list_data_set, ds->type, (void *) ds_copy));
460 } /* int plugin_register_data_set */
462 int plugin_register_log (char *name,
463 void (*callback) (int priority, const char *msg))
465 return (register_callback (&list_log, name, (void *) callback));
466 } /* int plugin_register_log */
468 int plugin_register_notification (const char *name,
469 int (*callback) (const notification_t *notif))
471 return (register_callback (&list_log, name, (void *) callback));
472 } /* int plugin_register_log */
474 int plugin_unregister_config (const char *name)
476 cf_unregister (name);
478 } /* int plugin_unregister_config */
480 int plugin_unregister_complex_config (const char *name)
482 cf_unregister_complex (name);
484 } /* int plugin_unregister_complex_config */
486 int plugin_unregister_init (const char *name)
488 return (plugin_unregister (list_init, name));
491 int plugin_unregister_read (const char *name)
495 e = llist_search (list_read, name);
500 llist_remove (list_read, e);
507 int plugin_unregister_write (const char *name)
509 return (plugin_unregister (list_write, name));
512 int plugin_unregister_shutdown (const char *name)
514 return (plugin_unregister (list_shutdown, name));
517 int plugin_unregister_data_set (const char *name)
522 if (list_data_set == NULL)
525 e = llist_search (list_data_set, name);
530 llist_remove (list_data_set, e);
531 ds = (data_set_t *) e->value;
538 } /* int plugin_unregister_data_set */
540 int plugin_unregister_log (const char *name)
542 return (plugin_unregister (list_log, name));
545 int plugin_unregister_notification (const char *name)
547 return (plugin_unregister (list_notification, name));
550 void plugin_init_all (void)
552 int (*callback) (void);
556 /* Start read-threads */
557 if (list_read != NULL)
561 rt = global_option_get ("ReadThreads");
563 start_threads ((num > 0) ? num : 5);
566 /* Init the value cache */
569 if (list_init == NULL)
572 le = llist_head (list_init);
575 callback = (int (*) (void)) le->value;
576 status = (*callback) ();
580 ERROR ("Initialization of plugin `%s' "
581 "failed with status %i. "
582 "Plugin will be unloaded.",
584 /* FIXME: Unload _all_ functions */
585 plugin_unregister_read (le->key);
590 } /* void plugin_init_all */
592 void plugin_read_all (const int *loop)
597 if (list_read == NULL)
600 pthread_mutex_lock (&read_lock);
602 le = llist_head (list_read);
605 rf = (read_func_t *) le->value;
607 if (rf->needs_read != DONE)
613 if (rf->wait_left > 0)
614 rf->wait_left -= interval_g;
616 if (rf->wait_left <= 0)
618 rf->needs_read = TODO;
624 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
625 pthread_cond_broadcast (&read_cond);
626 pthread_mutex_unlock (&read_lock);
627 } /* void plugin_read_all */
629 void plugin_shutdown_all (void)
631 int (*callback) (void);
636 if (list_shutdown == NULL)
639 le = llist_head (list_shutdown);
642 callback = (int (*) (void)) le->value;
644 /* Advance the pointer before calling the callback allows
645 * shutdown functions to unregister themselves. If done the
646 * other way around the memory `le' points to will be freed
647 * after callback returns. */
652 } /* void plugin_shutdown_all */
654 int plugin_dispatch_values (const char *name, value_list_t *vl)
656 int (*callback) (const data_set_t *, const value_list_t *);
660 if ((list_write == NULL) || (list_data_set == NULL))
663 le = llist_search (list_data_set, name);
666 DEBUG ("No such dataset registered: %s", name);
670 ds = (data_set_t *) le->value;
672 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
674 "plugin = %s; plugin_instance = %s; "
675 "type = %s; type_instance = %s;",
676 (unsigned int) vl->time, vl->interval,
678 vl->plugin, vl->plugin_instance,
679 ds->type, vl->type_instance);
682 assert (ds->ds_num == vl->values_len);
684 if (ds->ds_num != vl->values_len)
686 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
687 "(vl->values_len = %i)",
688 ds->type, ds->ds_num, vl->values_len);
693 escape_slashes (vl->host, sizeof (vl->host));
694 escape_slashes (vl->plugin, sizeof (vl->plugin));
695 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
696 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
698 /* Update the value cache */
700 ut_check_threshold (ds, vl);
702 le = llist_head (list_write);
705 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
706 (*callback) (ds, vl);
712 } /* int plugin_dispatch_values */
714 int plugin_dispatch_notification (const notification_t *notif)
716 int (*callback) (const notification_t *);
718 /* Possible TODO: Add flap detection here */
720 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
721 "time = %u; host = %s;",
722 notif->severity, notif->message,
723 (unsigned int) notif->time, notif->host);
725 /* Nobody cares for notifications */
726 if (list_notification == NULL)
729 le = llist_head (list_notification);
732 callback = (int (*) (const notification_t *)) le->value;
739 } /* int plugin_dispatch_notification */
741 void plugin_log (int level, const char *format, ...)
746 void (*callback) (int, const char *);
749 if (list_log == NULL)
753 if (level >= LOG_DEBUG)
757 va_start (ap, format);
758 vsnprintf (msg, 512, format, ap);
762 le = llist_head (list_log);
765 callback = (void (*) (int, const char *)) le->value;
766 (*callback) (level, msg);
770 } /* void plugin_log */
772 void plugin_complain (int level, complain_t *c, const char *format, ...)
783 if (c->interval < interval_g)
784 c->interval = interval_g;
788 if (c->interval > 86400)
791 c->delay = c->interval / interval_g;
793 va_start (ap, format);
794 vsnprintf (message, 512, format, ap);
798 plugin_log (level, message);
801 void plugin_relief (int level, complain_t *c, const char *format, ...)
806 if (c->interval == 0)
811 va_start (ap, format);
812 vsnprintf (message, 512, format, ap);
816 plugin_log (level, message);
819 const data_set_t *plugin_get_ds (const char *name)
824 le = llist_search (list_data_set, name);
827 DEBUG ("No such dataset registered: %s", name);
831 ds = (data_set_t *) le->value;
834 } /* data_set_t *plugin_get_ds */