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>
27 #include "configfile.h"
28 #include "utils_llist.h"
37 int (*callback) (void);
39 typedef struct read_func_s read_func_t;
44 static llist_t *list_init;
45 static llist_t *list_read;
46 static llist_t *list_write;
47 static llist_t *list_shutdown;
48 static llist_t *list_data_set;
49 static llist_t *list_log;
51 static char *plugindir = NULL;
56 static const char *plugin_get_dir (void)
58 if (plugindir == NULL)
64 static int register_callback (llist_t **list, const char *name, void *callback)
69 && ((*list = llist_create ()) == NULL))
72 le = llist_search (*list, name);
75 le = llentry_create (name, callback);
79 llist_append (*list, le);
87 } /* int register_callback */
89 static int plugin_unregister (llist_t *list, const char *name)
93 e = llist_search (list, name);
98 llist_remove (list, e);
102 } /* int plugin_unregister */
105 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
106 * object, but it will bitch about a shared object not having a
107 * ``module_register'' symbol..
109 static int plugin_load_file (char *file)
112 void (*reg_handle) (void);
114 DEBUG ("file = %s", file);
117 lt_dlerror (); /* clear errors */
119 if ((dlh = lt_dlopen (file)) == NULL)
121 const char *error = lt_dlerror ();
123 ERROR ("lt_dlopen failed: %s", error);
124 DEBUG ("lt_dlopen failed: %s", error);
128 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
130 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
131 file, lt_dlerror ());
144 void plugin_set_dir (const char *dir)
146 if (plugindir != NULL)
151 else if ((plugindir = strdup (dir)) == NULL)
152 ERROR ("strdup failed: %s", strerror (errno));
156 int plugin_load (const char *type)
160 char filename[BUFSIZE];
161 char typename[BUFSIZE];
167 DEBUG ("type = %s", type);
169 dir = plugin_get_dir ();
172 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
173 * type when matching the filename */
174 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
176 WARNING ("snprintf: truncated: `%s.so'", type);
179 typename_len = strlen (typename);
181 if ((dh = opendir (dir)) == NULL)
183 ERROR ("opendir (%s): %s", dir, strerror (errno));
187 while ((de = readdir (dh)) != NULL)
189 if (strncasecmp (de->d_name, typename, typename_len))
192 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
194 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
198 if (lstat (filename, &statbuf) == -1)
200 WARNING ("stat %s: %s", filename, strerror (errno));
203 else if (!S_ISREG (statbuf.st_mode))
205 /* don't follow symlinks */
209 if (plugin_load_file (filename) == 0)
223 * The `register_*' functions follow
225 int plugin_register_config (const char *name,
226 int (*callback) (const char *key, const char *val),
227 const char **keys, int keys_num)
229 cf_register (name, callback, keys, keys_num);
231 } /* int plugin_register_config */
233 int plugin_register_init (const char *name,
234 int (*callback) (void))
236 return (register_callback (&list_init, name, (void *) callback));
237 } /* plugin_register_init */
239 int plugin_register_read (const char *name,
240 int (*callback) (void))
244 rf = (read_func_t *) malloc (sizeof (read_func_t));
247 ERROR ("plugin_register_read: malloc failed: %s",
252 memset (rf, '\0', sizeof (read_func_t));
253 rf->wait_time = atoi (COLLECTD_STEP);
255 rf->callback = callback;
257 return (register_callback (&list_read, name, (void *) rf));
258 } /* int plugin_register_read */
260 int plugin_register_write (const char *name,
261 int (*callback) (const data_set_t *ds, const value_list_t *vl))
263 return (register_callback (&list_write, name, (void *) callback));
264 } /* int plugin_register_write */
266 int plugin_register_shutdown (char *name,
267 int (*callback) (void))
269 return (register_callback (&list_shutdown, name, (void *) callback));
270 } /* int plugin_register_shutdown */
272 int plugin_register_data_set (const data_set_t *ds)
274 return (register_callback (&list_data_set, ds->type, (void *) ds));
275 } /* int plugin_register_data_set */
277 int plugin_register_log (char *name,
278 void (*callback) (int priority, const char *msg))
280 return (register_callback (&list_log, name, (void *) callback));
281 } /* int plugin_register_log */
283 int plugin_unregister_init (const char *name)
285 return (plugin_unregister (list_init, name));
288 int plugin_unregister_read (const char *name)
290 return (plugin_unregister (list_read, name));
293 e = llist_search (list_read, name);
298 llist_remove (list_read, e);
305 int plugin_unregister_write (const char *name)
307 return (plugin_unregister (list_write, name));
310 int plugin_unregister_shutdown (const char *name)
312 return (plugin_unregister (list_shutdown, name));
315 int plugin_unregister_data_set (const char *name)
317 return (plugin_unregister (list_data_set, name));
320 int plugin_unregister_log (const char *name)
322 return (plugin_unregister (list_log, name));
325 void plugin_init_all (void)
327 int (*callback) (void);
331 if (list_init == NULL)
334 le = llist_head (list_init);
337 callback = le->value;
338 status = (*callback) ();
342 ERROR ("Initialization of plugin `%s' "
343 "failed with status %i. "
344 "Plugin will be unloaded. TODO!",
346 plugin_unregister_read (le->key);
351 } /* void plugin_init_all */
353 void plugin_read_all (const int *loop)
360 if (list_read == NULL)
363 step = atoi (COLLECTD_STEP);
365 le = llist_head (list_read);
366 while ((*loop == 0) && (le != NULL))
368 rf = (read_func_t *) le->value;
370 if (rf->wait_left > 0)
371 rf->wait_left -= step;
372 if (rf->wait_left > 0)
378 status = rf->callback ();
381 rf->wait_left = rf->wait_time;
382 rf->wait_time = rf->wait_time * 2;
383 if (rf->wait_time > 86400)
384 rf->wait_time = 86400;
386 NOTICE ("read-function of plugin `%s' "
387 "failed. Will syspend it for %i "
388 "seconds.", le->key, rf->wait_left);
393 rf->wait_time = step;
397 } /* while ((*loop == 0) && (le != NULL)) */
398 } /* void plugin_read_all */
400 void plugin_shutdown_all (void)
402 int (*callback) (void);
405 if (list_shutdown == NULL)
408 le = llist_head (list_shutdown);
411 callback = le->value;
416 } /* void plugin_shutdown_all */
418 int plugin_dispatch_values (const char *name, const value_list_t *vl)
420 int (*callback) (const data_set_t *, const value_list_t *);
424 if (list_write == NULL)
427 le = llist_search (list_data_set, name);
430 DEBUG ("No such dataset registered: %s", name);
434 ds = (data_set_t *) le->value;
436 DEBUG ("time = %u; host = %s; "
437 "plugin = %s; plugin_instance = %s; "
438 "type = %s; type_instance = %s;",
439 (unsigned int) vl->time, vl->host,
440 vl->plugin, vl->plugin_instance,
441 ds->type, vl->type_instance);
443 le = llist_head (list_write);
446 callback = le->value;
447 (*callback) (ds, vl);
455 void plugin_log (int level, const char *format, ...)
460 void (*callback) (int, const char *);
464 if (level >= LOG_DEBUG)
468 va_start (ap, format);
469 vsnprintf (msg, 512, format, ap);
473 le = llist_head (list_log);
476 callback = le->value;
477 (*callback) (level, msg);
481 } /* void plugin_log */
483 void plugin_complain (int level, complain_t *c, const char *format, ...)
495 step = atoi (COLLECTD_STEP);
498 if (c->interval < step)
503 if (c->interval > 86400)
506 c->delay = c->interval / step;
508 va_start (ap, format);
509 vsnprintf (message, 512, format, ap);
513 plugin_log (level, message);
516 void plugin_relief (int level, complain_t *c, const char *format, ...)
521 if (c->interval == 0)
526 va_start (ap, format);
527 vsnprintf (message, 512, format, ap);
531 plugin_log (level, message);