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>
28 #include "configfile.h"
29 #include "utils_llist.h"
38 int (*callback) (void);
40 typedef struct read_func_s read_func_t;
45 static llist_t *list_init;
46 static llist_t *list_read;
47 static llist_t *list_write;
48 static llist_t *list_shutdown;
49 static llist_t *list_data_set;
50 static llist_t *list_log;
52 static char *plugindir = NULL;
57 static const char *plugin_get_dir (void)
59 if (plugindir == NULL)
65 static int register_callback (llist_t **list, const char *name, void *callback)
70 && ((*list = llist_create ()) == NULL))
73 le = llist_search (*list, name);
76 le = llentry_create (name, callback);
80 llist_append (*list, le);
88 } /* int register_callback */
90 static int plugin_unregister (llist_t *list, const char *name)
94 e = llist_search (list, name);
99 llist_remove (list, e);
103 } /* int plugin_unregister */
106 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
107 * object, but it will bitch about a shared object not having a
108 * ``module_register'' symbol..
110 static int plugin_load_file (char *file)
113 void (*reg_handle) (void);
115 DEBUG ("file = %s", file);
118 lt_dlerror (); /* clear errors */
120 if ((dlh = lt_dlopen (file)) == NULL)
122 const char *error = lt_dlerror ();
124 ERROR ("lt_dlopen failed: %s", error);
125 DEBUG ("lt_dlopen failed: %s", error);
129 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
131 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
132 file, lt_dlerror ());
145 void plugin_set_dir (const char *dir)
147 if (plugindir != NULL)
152 else if ((plugindir = strdup (dir)) == NULL)
155 ERROR ("strdup failed: %s",
156 sstrerror (errno, errbuf, sizeof (errbuf)));
161 int plugin_load (const char *type)
165 char filename[BUFSIZE];
166 char typename[BUFSIZE];
172 DEBUG ("type = %s", type);
174 dir = plugin_get_dir ();
177 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
178 * type when matching the filename */
179 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
181 WARNING ("snprintf: truncated: `%s.so'", type);
184 typename_len = strlen (typename);
186 if ((dh = opendir (dir)) == NULL)
189 ERROR ("opendir (%s): %s", dir,
190 sstrerror (errno, errbuf, sizeof (errbuf)));
194 while ((de = readdir (dh)) != NULL)
196 if (strncasecmp (de->d_name, typename, typename_len))
199 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
201 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
205 if (lstat (filename, &statbuf) == -1)
208 WARNING ("stat %s: %s", filename,
209 sstrerror (errno, errbuf, sizeof (errbuf)));
212 else if (!S_ISREG (statbuf.st_mode))
214 /* don't follow symlinks */
218 if (plugin_load_file (filename) == 0)
232 * The `register_*' functions follow
234 int plugin_register_config (const char *name,
235 int (*callback) (const char *key, const char *val),
236 const char **keys, int keys_num)
238 cf_register (name, callback, keys, keys_num);
240 } /* int plugin_register_config */
242 int plugin_register_init (const char *name,
243 int (*callback) (void))
245 return (register_callback (&list_init, name, (void *) callback));
246 } /* plugin_register_init */
248 int plugin_register_read (const char *name,
249 int (*callback) (void))
253 rf = (read_func_t *) malloc (sizeof (read_func_t));
257 ERROR ("plugin_register_read: malloc failed: %s",
258 sstrerror (errno, errbuf, sizeof (errbuf)));
262 memset (rf, '\0', sizeof (read_func_t));
263 rf->wait_time = interval_g;
265 rf->callback = callback;
267 return (register_callback (&list_read, name, (void *) rf));
268 } /* int plugin_register_read */
270 int plugin_register_write (const char *name,
271 int (*callback) (const data_set_t *ds, const value_list_t *vl))
273 return (register_callback (&list_write, name, (void *) callback));
274 } /* int plugin_register_write */
276 int plugin_register_shutdown (char *name,
277 int (*callback) (void))
279 return (register_callback (&list_shutdown, name, (void *) callback));
280 } /* int plugin_register_shutdown */
282 int plugin_register_data_set (const data_set_t *ds)
284 return (register_callback (&list_data_set, ds->type, (void *) ds));
285 } /* int plugin_register_data_set */
287 int plugin_register_log (char *name,
288 void (*callback) (int priority, const char *msg))
290 return (register_callback (&list_log, name, (void *) callback));
291 } /* int plugin_register_log */
293 int plugin_unregister_config (const char *name)
295 cf_unregister (name);
297 } /* int plugin_unregister_config */
299 int plugin_unregister_init (const char *name)
301 return (plugin_unregister (list_init, name));
304 int plugin_unregister_read (const char *name)
306 return (plugin_unregister (list_read, name));
309 e = llist_search (list_read, name);
314 llist_remove (list_read, e);
321 int plugin_unregister_write (const char *name)
323 return (plugin_unregister (list_write, name));
326 int plugin_unregister_shutdown (const char *name)
328 return (plugin_unregister (list_shutdown, name));
331 int plugin_unregister_data_set (const char *name)
333 return (plugin_unregister (list_data_set, name));
336 int plugin_unregister_log (const char *name)
338 return (plugin_unregister (list_log, name));
341 void plugin_init_all (void)
343 int (*callback) (void);
347 if (list_init == NULL)
350 le = llist_head (list_init);
353 callback = le->value;
354 status = (*callback) ();
358 ERROR ("Initialization of plugin `%s' "
359 "failed with status %i. "
360 "Plugin will be unloaded. TODO!",
362 plugin_unregister_read (le->key);
367 } /* void plugin_init_all */
369 void plugin_read_all (const int *loop)
375 if (list_read == NULL)
378 le = llist_head (list_read);
379 while ((*loop == 0) && (le != NULL))
381 rf = (read_func_t *) le->value;
383 if (rf->wait_left > 0)
384 rf->wait_left -= interval_g;
385 if (rf->wait_left > 0)
391 status = rf->callback ();
394 rf->wait_left = rf->wait_time;
395 rf->wait_time = rf->wait_time * 2;
396 if (rf->wait_time > 86400)
397 rf->wait_time = 86400;
399 NOTICE ("read-function of plugin `%s' "
400 "failed. Will syspend it for %i "
401 "seconds.", le->key, rf->wait_left);
406 rf->wait_time = interval_g;
410 } /* while ((*loop == 0) && (le != NULL)) */
411 } /* void plugin_read_all */
413 void plugin_shutdown_all (void)
415 int (*callback) (void);
418 if (list_shutdown == NULL)
421 le = llist_head (list_shutdown);
424 callback = le->value;
429 } /* void plugin_shutdown_all */
431 int plugin_dispatch_values (const char *name, const value_list_t *vl)
433 int (*callback) (const data_set_t *, const value_list_t *);
437 if (list_write == NULL)
440 le = llist_search (list_data_set, name);
443 DEBUG ("No such dataset registered: %s", name);
447 ds = (data_set_t *) le->value;
449 DEBUG ("time = %u; host = %s; "
450 "plugin = %s; plugin_instance = %s; "
451 "type = %s; type_instance = %s;",
452 (unsigned int) vl->time, vl->host,
453 vl->plugin, vl->plugin_instance,
454 ds->type, vl->type_instance);
456 le = llist_head (list_write);
459 callback = le->value;
460 (*callback) (ds, vl);
468 void plugin_log (int level, const char *format, ...)
473 void (*callback) (int, const char *);
476 if (list_log == NULL)
480 if (level >= LOG_DEBUG)
484 va_start (ap, format);
485 vsnprintf (msg, 512, format, ap);
489 le = llist_head (list_log);
492 callback = le->value;
493 (*callback) (level, msg);
497 } /* void plugin_log */
499 void plugin_complain (int level, complain_t *c, const char *format, ...)
510 if (c->interval < interval_g)
511 c->interval = interval_g;
515 if (c->interval > 86400)
518 c->delay = c->interval / interval_g;
520 va_start (ap, format);
521 vsnprintf (message, 512, format, ap);
525 plugin_log (level, message);
528 void plugin_relief (int level, complain_t *c, const char *format, ...)
533 if (c->interval == 0)
538 va_start (ap, format);
539 vsnprintf (message, 512, format, ap);
543 plugin_log (level, message);
546 const data_set_t *plugin_get_ds (const char *name)
551 le = llist_search (list_data_set, name);
554 DEBUG ("No such dataset registered: %s", name);
558 ds = (data_set_t *) le->value;
561 } /* data_set_t *plugin_get_ds */