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;
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 DBG ("file = %s", file);
117 lt_dlerror (); /* clear errors */
119 if ((dlh = lt_dlopen (file)) == NULL)
121 const char *error = lt_dlerror ();
123 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
124 DBG ("lt_dlopen failed: %s", error);
128 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
130 syslog (LOG_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 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
156 int plugin_load (const char *type)
160 char filename[BUFSIZE];
161 char typename[BUFSIZE];
167 DBG ("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 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
179 typename_len = strlen (typename);
181 if ((dh = opendir (dir)) == NULL)
183 syslog (LOG_ERR, "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 syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
198 if (lstat (filename, &statbuf) == -1)
200 syslog (LOG_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 syslog (LOG_ERR, "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_unregister_init (const char *name)
279 return (plugin_unregister (list_init, name));
282 int plugin_unregister_read (const char *name)
284 return (plugin_unregister (list_read, name));
287 e = llist_search (list_read, name);
292 llist_remove (list_read, e);
299 int plugin_unregister_write (const char *name)
301 return (plugin_unregister (list_write, name));
304 int plugin_unregister_shutdown (const char *name)
306 return (plugin_unregister (list_shutdown, name));
309 int plugin_unregister_data_set (const char *name)
311 return (plugin_unregister (list_data_set, name));
314 void plugin_init_all (void)
316 int (*callback) (void);
320 if (list_init == NULL)
323 le = llist_head (list_init);
326 callback = le->value;
327 status = (*callback) ();
331 syslog (LOG_ERR, "Initialization of plugin `%s' "
332 "failed with status %i. "
333 "Plugin will be unloaded. TODO!",
335 plugin_unregister_read (le->key);
340 } /* void plugin_init_all */
342 void plugin_read_all (const int *loop)
349 if (list_read == NULL)
352 step = atoi (COLLECTD_STEP);
354 le = llist_head (list_read);
355 while ((*loop == 0) && (le != NULL))
357 rf = (read_func_t *) le->value;
359 if (rf->wait_left > 0)
360 rf->wait_left -= step;
361 if (rf->wait_left > 0)
367 status = rf->callback ();
370 rf->wait_left = rf->wait_time;
371 rf->wait_time = rf->wait_time * 2;
372 if (rf->wait_time > 86400)
373 rf->wait_time = 86400;
375 syslog (LOG_NOTICE, "read-function of plugin `%s' "
376 "failed. Will syspend it for %i "
377 "seconds.", le->key, rf->wait_left);
382 rf->wait_time = step;
386 } /* while ((*loop == 0) && (le != NULL)) */
387 } /* void plugin_read_all */
389 void plugin_shutdown_all (void)
391 int (*callback) (void);
394 if (list_shutdown == NULL)
397 le = llist_head (list_shutdown);
400 callback = le->value;
405 } /* void plugin_shutdown_all */
407 int plugin_dispatch_values (const char *name, const value_list_t *vl)
409 int (*callback) (const data_set_t *, const value_list_t *);
413 if (list_write == NULL)
416 le = llist_search (list_data_set, name);
419 DBG ("No such dataset registered: %s", name);
423 ds = (data_set_t *) le->value;
425 DBG ("time = %u; host = %s; "
426 "plugin = %s; plugin_instance = %s; "
427 "type = %s; type_instance = %s;",
428 (unsigned int) vl->time, vl->host,
429 vl->plugin, vl->plugin_instance,
430 ds->type, vl->type_instance);
432 le = llist_head (list_write);
435 callback = le->value;
436 (*callback) (ds, vl);
444 void plugin_complain (int level, complain_t *c, const char *format, ...)
456 step = atoi (COLLECTD_STEP);
459 if (c->interval < step)
464 if (c->interval > 86400)
467 c->delay = c->interval / step;
469 va_start (ap, format);
470 vsnprintf (message, 512, format, ap);
474 syslog (level, message);
477 void plugin_relief (int level, complain_t *c, const char *format, ...)
482 if (c->interval == 0)
487 va_start (ap, format);
488 vsnprintf (message, 512, format, ap);
492 syslog (level, message);