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"
42 int (*callback) (void);
43 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
45 typedef struct read_func_s read_func_t;
50 static llist_t *list_init;
51 static llist_t *list_read;
52 static llist_t *list_write;
53 static llist_t *list_shutdown;
54 static llist_t *list_data_set;
55 static llist_t *list_log;
57 static char *plugindir = NULL;
59 static int read_loop = 1;
60 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
61 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
62 static pthread_t *read_threads = NULL;
63 static int read_threads_num = 0;
68 static const char *plugin_get_dir (void)
70 if (plugindir == NULL)
76 static int register_callback (llist_t **list, const char *name, void *callback)
81 && ((*list = llist_create ()) == NULL))
84 le = llist_search (*list, name);
87 le = llentry_create (name, callback);
91 llist_append (*list, le);
99 } /* int register_callback */
101 static int plugin_unregister (llist_t *list, const char *name)
105 e = llist_search (list, name);
110 llist_remove (list, e);
114 } /* int plugin_unregister */
117 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
118 * object, but it will bitch about a shared object not having a
119 * ``module_register'' symbol..
121 static int plugin_load_file (char *file, modreg_e load)
124 void (*reg_handle) (modreg_e mr);
126 DEBUG ("file = %s", file);
129 lt_dlerror (); /* clear errors */
131 if ((dlh = lt_dlopen (file)) == NULL)
133 const char *error = lt_dlerror ();
135 ERROR ("lt_dlopen failed: %s", error);
136 DEBUG ("lt_dlopen failed: %s", error);
140 if ((reg_handle = (void (*) (modreg_e)) lt_dlsym (dlh, "module_register")) == NULL)
142 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
143 file, lt_dlerror ());
148 (*reg_handle) (load);
153 static void *plugin_read_thread (void *args)
160 pthread_mutex_lock (&read_lock);
162 while (read_loop != 0)
164 le = llist_head (list_read);
167 while ((read_loop != 0) && (le != NULL))
169 rf = (read_func_t *) le->value;
171 if (rf->needs_read != TODO)
177 /* We will do this read function */
178 rf->needs_read = ACTIVE;
180 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
181 (unsigned long int) pthread_self (), le->key);
182 pthread_mutex_unlock (&read_lock);
184 status = rf->callback ();
189 rf->wait_left = rf->wait_time;
190 rf->wait_time = rf->wait_time * 2;
191 if (rf->wait_time > 86400)
192 rf->wait_time = 86400;
194 NOTICE ("read-function of plugin `%s' "
195 "failed. Will syspend it for %i "
196 "seconds.", le->key, rf->wait_left);
201 rf->wait_time = interval_g;
204 pthread_mutex_lock (&read_lock);
206 rf->needs_read = DONE;
208 } /* while (le != NULL) */
210 if ((read_loop != 0) && (done == 0))
212 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
213 (unsigned long int) pthread_self ());
214 pthread_cond_wait (&read_cond, &read_lock);
216 } /* while (read_loop) */
218 pthread_mutex_unlock (&read_lock);
221 } /* void *plugin_read_thread */
223 static void start_threads (int num)
227 if (read_threads != NULL)
230 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
231 if (read_threads == NULL)
233 ERROR ("plugin: start_threads: calloc failed.");
237 read_threads_num = 0;
238 for (i = 0; i < num; i++)
240 if (pthread_create (read_threads + read_threads_num, NULL,
241 plugin_read_thread, NULL) == 0)
247 ERROR ("plugin: start_threads: pthread_create failed.");
251 } /* void start_threads */
253 static void stop_threads (void)
257 pthread_mutex_lock (&read_lock);
259 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
260 pthread_cond_broadcast (&read_cond);
261 pthread_mutex_unlock (&read_lock);
263 for (i = 0; i < read_threads_num; i++)
265 if (pthread_join (read_threads[i], NULL) != 0)
267 ERROR ("plugin: stop_threads: pthread_join failed.");
269 read_threads[i] = (pthread_t) 0;
271 sfree (read_threads);
272 read_threads_num = 0;
273 } /* void stop_threads */
278 void plugin_set_dir (const char *dir)
280 if (plugindir != NULL)
285 else if ((plugindir = strdup (dir)) == NULL)
288 ERROR ("strdup failed: %s",
289 sstrerror (errno, errbuf, sizeof (errbuf)));
294 int plugin_load (const char *type, modreg_e mr)
298 char filename[BUFSIZE];
299 char typename[BUFSIZE];
305 DEBUG ("type = %s", type);
307 dir = plugin_get_dir ();
310 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
311 * type when matching the filename */
312 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
314 WARNING ("snprintf: truncated: `%s.so'", type);
317 typename_len = strlen (typename);
319 if ((dh = opendir (dir)) == NULL)
322 ERROR ("opendir (%s): %s", dir,
323 sstrerror (errno, errbuf, sizeof (errbuf)));
327 while ((de = readdir (dh)) != NULL)
329 if (strncasecmp (de->d_name, typename, typename_len))
332 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
334 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
338 if (lstat (filename, &statbuf) == -1)
341 WARNING ("stat %s: %s", filename,
342 sstrerror (errno, errbuf, sizeof (errbuf)));
345 else if (!S_ISREG (statbuf.st_mode))
347 /* don't follow symlinks */
351 if (plugin_load_file (filename, mr) == 0)
365 * The `register_*' functions follow
367 int plugin_register_config (const char *name,
368 int (*callback) (const char *key, const char *val),
369 const char **keys, int keys_num)
371 cf_register (name, callback, keys, keys_num);
373 } /* int plugin_register_config */
375 int plugin_register_init (const char *name,
376 int (*callback) (void))
378 return (register_callback (&list_init, name, (void *) callback));
379 } /* plugin_register_init */
381 int plugin_register_read (const char *name,
382 int (*callback) (void))
386 rf = (read_func_t *) malloc (sizeof (read_func_t));
390 ERROR ("plugin_register_read: malloc failed: %s",
391 sstrerror (errno, errbuf, sizeof (errbuf)));
395 memset (rf, '\0', sizeof (read_func_t));
396 rf->wait_time = interval_g;
398 rf->callback = callback;
399 rf->needs_read = DONE;
401 return (register_callback (&list_read, name, (void *) rf));
402 } /* int plugin_register_read */
404 int plugin_register_write (const char *name,
405 int (*callback) (const data_set_t *ds, const value_list_t *vl))
407 return (register_callback (&list_write, name, (void *) callback));
408 } /* int plugin_register_write */
410 int plugin_register_shutdown (char *name,
411 int (*callback) (void))
413 return (register_callback (&list_shutdown, name, (void *) callback));
414 } /* int plugin_register_shutdown */
416 int plugin_register_data_set (const data_set_t *ds)
418 return (register_callback (&list_data_set, ds->type, (void *) ds));
419 } /* int plugin_register_data_set */
421 int plugin_register_log (char *name,
422 void (*callback) (int priority, const char *msg))
424 return (register_callback (&list_log, name, (void *) callback));
425 } /* int plugin_register_log */
427 int plugin_unregister_config (const char *name)
429 cf_unregister (name);
431 } /* int plugin_unregister_config */
433 int plugin_unregister_init (const char *name)
435 return (plugin_unregister (list_init, name));
438 int plugin_unregister_read (const char *name)
440 return (plugin_unregister (list_read, name));
443 e = llist_search (list_read, name);
448 llist_remove (list_read, e);
455 int plugin_unregister_write (const char *name)
457 return (plugin_unregister (list_write, name));
460 int plugin_unregister_shutdown (const char *name)
462 return (plugin_unregister (list_shutdown, name));
465 int plugin_unregister_data_set (const char *name)
467 return (plugin_unregister (list_data_set, name));
470 int plugin_unregister_log (const char *name)
472 return (plugin_unregister (list_log, name));
475 void plugin_init_all (void)
477 int (*callback) (void);
481 /* Start read-threads */
482 if (list_read != NULL)
486 rt = global_option_get ("ReadThreads");
488 start_threads ((num > 0) ? num : 5);
491 if (list_init == NULL)
494 le = llist_head (list_init);
497 callback = le->value;
498 status = (*callback) ();
502 ERROR ("Initialization of plugin `%s' "
503 "failed with status %i. "
504 "Plugin will be unloaded. TODO!",
506 plugin_unregister_read (le->key);
511 } /* void plugin_init_all */
513 void plugin_read_all (const int *loop)
518 if (list_read == NULL)
521 pthread_mutex_lock (&read_lock);
523 le = llist_head (list_read);
526 rf = (read_func_t *) le->value;
528 if (rf->needs_read != DONE)
534 if (rf->wait_left > 0)
535 rf->wait_left -= interval_g;
537 if (rf->wait_left <= 0)
539 rf->needs_read = TODO;
545 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
546 pthread_cond_broadcast (&read_cond);
547 pthread_mutex_unlock (&read_lock);
548 } /* void plugin_read_all */
550 void plugin_shutdown_all (void)
552 int (*callback) (void);
557 if (list_shutdown == NULL)
560 le = llist_head (list_shutdown);
563 callback = le->value;
568 } /* void plugin_shutdown_all */
570 int plugin_dispatch_values (const char *name, const value_list_t *vl)
572 int (*callback) (const data_set_t *, const value_list_t *);
576 if (list_write == NULL)
579 le = llist_search (list_data_set, name);
582 DEBUG ("No such dataset registered: %s", name);
586 ds = (data_set_t *) le->value;
588 DEBUG ("plugin: plugin_dispatch_values: time = %u; host = %s; "
589 "plugin = %s; plugin_instance = %s; type = %s; "
590 "type_instance = %s;",
591 (unsigned int) vl->time, vl->host,
592 vl->plugin, vl->plugin_instance,
593 ds->type, vl->type_instance);
595 le = llist_head (list_write);
598 callback = le->value;
599 (*callback) (ds, vl);
607 void plugin_log (int level, const char *format, ...)
612 void (*callback) (int, const char *);
615 if (list_log == NULL)
619 if (level >= LOG_DEBUG)
623 va_start (ap, format);
624 vsnprintf (msg, 512, format, ap);
628 le = llist_head (list_log);
631 callback = le->value;
632 (*callback) (level, msg);
636 } /* void plugin_log */
638 void plugin_complain (int level, complain_t *c, const char *format, ...)
649 if (c->interval < interval_g)
650 c->interval = interval_g;
654 if (c->interval > 86400)
657 c->delay = c->interval / interval_g;
659 va_start (ap, format);
660 vsnprintf (message, 512, format, ap);
664 plugin_log (level, message);
667 void plugin_relief (int level, complain_t *c, const char *format, ...)
672 if (c->interval == 0)
677 va_start (ap, format);
678 vsnprintf (message, 512, format, ap);
682 plugin_log (level, message);
685 const data_set_t *plugin_get_ds (const char *name)
690 le = llist_search (list_data_set, name);
693 DEBUG ("No such dataset registered: %s", name);
697 ds = (data_set_t *) le->value;
700 } /* data_set_t *plugin_get_ds */