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 = atoi (COLLECTD_STEP);
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_init (const char *name)
295 return (plugin_unregister (list_init, name));
298 int plugin_unregister_read (const char *name)
300 return (plugin_unregister (list_read, name));
303 e = llist_search (list_read, name);
308 llist_remove (list_read, e);
315 int plugin_unregister_write (const char *name)
317 return (plugin_unregister (list_write, name));
320 int plugin_unregister_shutdown (const char *name)
322 return (plugin_unregister (list_shutdown, name));
325 int plugin_unregister_data_set (const char *name)
327 return (plugin_unregister (list_data_set, name));
330 int plugin_unregister_log (const char *name)
332 return (plugin_unregister (list_log, name));
335 void plugin_init_all (void)
337 int (*callback) (void);
341 if (list_init == NULL)
344 le = llist_head (list_init);
347 callback = le->value;
348 status = (*callback) ();
352 ERROR ("Initialization of plugin `%s' "
353 "failed with status %i. "
354 "Plugin will be unloaded. TODO!",
356 plugin_unregister_read (le->key);
361 } /* void plugin_init_all */
363 void plugin_read_all (const int *loop)
370 if (list_read == NULL)
373 step = atoi (COLLECTD_STEP);
375 le = llist_head (list_read);
376 while ((*loop == 0) && (le != NULL))
378 rf = (read_func_t *) le->value;
380 if (rf->wait_left > 0)
381 rf->wait_left -= step;
382 if (rf->wait_left > 0)
388 status = rf->callback ();
391 rf->wait_left = rf->wait_time;
392 rf->wait_time = rf->wait_time * 2;
393 if (rf->wait_time > 86400)
394 rf->wait_time = 86400;
396 NOTICE ("read-function of plugin `%s' "
397 "failed. Will syspend it for %i "
398 "seconds.", le->key, rf->wait_left);
403 rf->wait_time = step;
407 } /* while ((*loop == 0) && (le != NULL)) */
408 } /* void plugin_read_all */
410 void plugin_shutdown_all (void)
412 int (*callback) (void);
415 if (list_shutdown == NULL)
418 le = llist_head (list_shutdown);
421 callback = le->value;
426 } /* void plugin_shutdown_all */
428 int plugin_dispatch_values (const char *name, const value_list_t *vl)
430 int (*callback) (const data_set_t *, const value_list_t *);
434 if (list_write == NULL)
437 le = llist_search (list_data_set, name);
440 DEBUG ("No such dataset registered: %s", name);
444 ds = (data_set_t *) le->value;
446 DEBUG ("time = %u; host = %s; "
447 "plugin = %s; plugin_instance = %s; "
448 "type = %s; type_instance = %s;",
449 (unsigned int) vl->time, vl->host,
450 vl->plugin, vl->plugin_instance,
451 ds->type, vl->type_instance);
453 le = llist_head (list_write);
456 callback = le->value;
457 (*callback) (ds, vl);
465 void plugin_log (int level, const char *format, ...)
470 void (*callback) (int, const char *);
474 if (level >= LOG_DEBUG)
478 va_start (ap, format);
479 vsnprintf (msg, 512, format, ap);
483 le = llist_head (list_log);
486 callback = le->value;
487 (*callback) (level, msg);
491 } /* void plugin_log */
493 void plugin_complain (int level, complain_t *c, const char *format, ...)
505 step = atoi (COLLECTD_STEP);
508 if (c->interval < step)
513 if (c->interval > 86400)
516 c->delay = c->interval / step;
518 va_start (ap, format);
519 vsnprintf (message, 512, format, ap);
523 plugin_log (level, message);
526 void plugin_relief (int level, complain_t *c, const char *format, ...)
531 if (c->interval == 0)
536 va_start (ap, format);
537 vsnprintf (message, 512, format, ap);
541 plugin_log (level, message);
544 const data_set_t *plugin_get_ds (const char *name)
549 le = llist_search (list_data_set, name);
552 DEBUG ("No such dataset registered: %s", name);
556 ds = (data_set_t *) le->value;
559 } /* data_set_t *plugin_get_ds */