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"
29 #include "utils_debug.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 DBG ("file = %s", file);
118 lt_dlerror (); /* clear errors */
120 if ((dlh = lt_dlopen (file)) == NULL)
122 const char *error = lt_dlerror ();
124 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
125 DBG ("lt_dlopen failed: %s", error);
129 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
131 syslog (LOG_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)
153 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
157 int plugin_load (const char *type)
161 char filename[BUFSIZE];
162 char typename[BUFSIZE];
168 DBG ("type = %s", type);
170 dir = plugin_get_dir ();
173 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
174 * type when matching the filename */
175 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
177 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
180 typename_len = strlen (typename);
182 if ((dh = opendir (dir)) == NULL)
184 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
188 while ((de = readdir (dh)) != NULL)
190 if (strncasecmp (de->d_name, typename, typename_len))
193 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
195 syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
199 if (lstat (filename, &statbuf) == -1)
201 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
204 else if (!S_ISREG (statbuf.st_mode))
206 /* don't follow symlinks */
210 if (plugin_load_file (filename) == 0)
224 * The `register_*' functions follow
226 int plugin_register_config (const char *name,
227 int (*callback) (const char *key, const char *val),
228 const char **keys, int keys_num)
230 cf_register (name, callback, keys, keys_num);
232 } /* int plugin_register_config */
234 int plugin_register_init (const char *name,
235 int (*callback) (void))
237 return (register_callback (&list_init, name, (void *) callback));
238 } /* plugin_register_init */
240 int plugin_register_read (const char *name,
241 int (*callback) (void))
245 rf = (read_func_t *) malloc (sizeof (read_func_t));
248 syslog (LOG_ERR, "plugin_register_read: malloc failed: %s",
253 memset (rf, '\0', sizeof (read_func_t));
254 rf->wait_time = atoi (COLLECTD_STEP);
256 rf->callback = callback;
258 return (register_callback (&list_read, name, (void *) rf));
259 } /* int plugin_register_read */
261 int plugin_register_write (const char *name,
262 int (*callback) (const data_set_t *ds, const value_list_t *vl))
264 return (register_callback (&list_write, name, (void *) callback));
265 } /* int plugin_register_write */
267 int plugin_register_shutdown (char *name,
268 int (*callback) (void))
270 return (register_callback (&list_shutdown, name, (void *) callback));
271 } /* int plugin_register_shutdown */
273 int plugin_register_data_set (const data_set_t *ds)
275 return (register_callback (&list_data_set, ds->type, (void *) ds));
276 } /* int plugin_register_data_set */
278 int plugin_register_log (char *name,
279 void (*callback) (int priority, const char *msg))
281 return (register_callback (&list_log, name, (void *) callback));
282 } /* int plugin_register_log */
284 int plugin_unregister_init (const char *name)
286 return (plugin_unregister (list_init, name));
289 int plugin_unregister_read (const char *name)
291 return (plugin_unregister (list_read, name));
294 e = llist_search (list_read, name);
299 llist_remove (list_read, e);
306 int plugin_unregister_write (const char *name)
308 return (plugin_unregister (list_write, name));
311 int plugin_unregister_shutdown (const char *name)
313 return (plugin_unregister (list_shutdown, name));
316 int plugin_unregister_data_set (const char *name)
318 return (plugin_unregister (list_data_set, name));
321 int plugin_unregister_log (const char *name)
323 return (plugin_unregister (list_log, name));
326 void plugin_init_all (void)
328 int (*callback) (void);
332 if (list_init == NULL)
335 le = llist_head (list_init);
338 callback = le->value;
339 status = (*callback) ();
343 syslog (LOG_ERR, "Initialization of plugin `%s' "
344 "failed with status %i. "
345 "Plugin will be unloaded. TODO!",
347 plugin_unregister_read (le->key);
352 } /* void plugin_init_all */
354 void plugin_read_all (const int *loop)
361 if (list_read == NULL)
364 step = atoi (COLLECTD_STEP);
366 le = llist_head (list_read);
367 while ((*loop == 0) && (le != NULL))
369 rf = (read_func_t *) le->value;
371 if (rf->wait_left > 0)
372 rf->wait_left -= step;
373 if (rf->wait_left > 0)
379 status = rf->callback ();
382 rf->wait_left = rf->wait_time;
383 rf->wait_time = rf->wait_time * 2;
384 if (rf->wait_time > 86400)
385 rf->wait_time = 86400;
387 syslog (LOG_NOTICE, "read-function of plugin `%s' "
388 "failed. Will syspend it for %i "
389 "seconds.", le->key, rf->wait_left);
394 rf->wait_time = step;
398 } /* while ((*loop == 0) && (le != NULL)) */
399 } /* void plugin_read_all */
401 void plugin_shutdown_all (void)
403 int (*callback) (void);
406 if (list_shutdown == NULL)
409 le = llist_head (list_shutdown);
412 callback = le->value;
417 } /* void plugin_shutdown_all */
419 int plugin_dispatch_values (const char *name, const value_list_t *vl)
421 int (*callback) (const data_set_t *, const value_list_t *);
425 if (list_write == NULL)
428 le = llist_search (list_data_set, name);
431 DBG ("No such dataset registered: %s", name);
435 ds = (data_set_t *) le->value;
437 DBG ("time = %u; host = %s; "
438 "plugin = %s; plugin_instance = %s; "
439 "type = %s; type_instance = %s;",
440 (unsigned int) vl->time, vl->host,
441 vl->plugin, vl->plugin_instance,
442 ds->type, vl->type_instance);
444 le = llist_head (list_write);
447 callback = le->value;
448 (*callback) (ds, vl);
456 void plugin_log (int level, const char *format, ...)
461 void (*callback) (int, const char *);
465 if (level >= LOG_DEBUG)
469 va_start (ap, format);
470 vsnprintf (msg, 512, format, ap);
474 le = llist_head (list_log);
477 callback = le->value;
478 (*callback) (level, msg);
482 } /* void plugin_log */
484 void plugin_complain (int level, complain_t *c, const char *format, ...)
496 step = atoi (COLLECTD_STEP);
499 if (c->interval < step)
504 if (c->interval > 86400)
507 c->delay = c->interval / step;
509 va_start (ap, format);
510 vsnprintf (message, 512, format, ap);
514 syslog (level, message);
517 void plugin_relief (int level, complain_t *c, const char *format, ...)
522 if (c->interval == 0)
527 va_start (ap, format);
528 vsnprintf (message, 512, format, ap);
532 syslog (level, message);