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"
34 static llist_t *list_init;
35 static llist_t *list_read;
36 static llist_t *list_write;
37 static llist_t *list_shutdown;
38 static llist_t *list_data_set;
40 static char *plugindir = NULL;
42 char hostname[DATA_MAX_NAME_LEN] = "localhost";
47 static const char *plugin_get_dir (void)
49 if (plugindir == NULL)
55 static int register_callback (llist_t **list, const char *name, void *callback)
60 && ((*list = llist_create ()) == NULL))
63 le = llist_search (*list, name);
66 le = llentry_create (name, callback);
70 llist_append (*list, le);
78 } /* int register_callback */
80 static int plugin_unregister (llist_t *list, const char *name)
84 e = llist_search (list, name);
89 llist_remove (list, e);
92 } /* int plugin_unregister */
95 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
96 * object, but it will bitch about a shared object not having a
97 * ``module_register'' symbol..
99 static int plugin_load_file (char *file)
102 void (*reg_handle) (void);
104 DBG ("file = %s", file);
107 lt_dlerror (); /* clear errors */
109 if ((dlh = lt_dlopen (file)) == NULL)
111 const char *error = lt_dlerror ();
113 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
114 DBG ("lt_dlopen failed: %s", error);
118 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
120 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
121 file, lt_dlerror ());
134 void plugin_set_dir (const char *dir)
136 if (plugindir != NULL)
141 else if ((plugindir = strdup (dir)) == NULL)
142 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
146 int plugin_load (const char *type)
150 char filename[BUFSIZE];
151 char typename[BUFSIZE];
157 DBG ("type = %s", type);
159 dir = plugin_get_dir ();
162 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
163 * type when matching the filename */
164 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
166 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
169 typename_len = strlen (typename);
171 if ((dh = opendir (dir)) == NULL)
173 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
177 while ((de = readdir (dh)) != NULL)
179 if (strncasecmp (de->d_name, typename, typename_len))
182 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
184 syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
188 if (lstat (filename, &statbuf) == -1)
190 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
193 else if (!S_ISREG (statbuf.st_mode))
195 /* don't follow symlinks */
199 if (plugin_load_file (filename) == 0)
213 * The `register_*' functions follow
215 int plugin_register_config (const char *name,
216 int (*callback) (const char *key, const char *val),
217 const char **keys, int keys_num)
219 cf_register (name, callback, keys, keys_num);
221 } /* int plugin_register_config */
223 int plugin_register_init (const char *name,
224 int (*callback) (void))
226 return (register_callback (&list_init, name, (void *) callback));
227 } /* plugin_register_init */
229 int plugin_register_read (const char *name,
230 int (*callback) (void))
232 return (register_callback (&list_read, name, (void *) callback));
233 } /* int plugin_register_read */
235 int plugin_register_write (const char *name,
236 int (*callback) (const data_set_t *ds, const value_list_t *vl))
238 return (register_callback (&list_write, name, (void *) callback));
239 } /* int plugin_register_write */
241 int plugin_register_shutdown (char *name,
242 int (*callback) (void))
244 return (register_callback (&list_shutdown, name, (void *) callback));
245 } /* int plugin_register_shutdown */
247 int plugin_register_data_set (const data_set_t *ds)
249 return (register_callback (&list_data_set, ds->type, (void *) ds));
250 } /* int plugin_register_data_set */
252 int plugin_unregister_init (const char *name)
254 return (plugin_unregister (list_init, name));
257 int plugin_unregister_read (const char *name)
259 return (plugin_unregister (list_read, name));
262 int plugin_unregister_write (const char *name)
264 return (plugin_unregister (list_write, name));
267 int plugin_unregister_shutdown (const char *name)
269 return (plugin_unregister (list_shutdown, name));
272 int plugin_unregister_data_set (const char *name)
274 return (plugin_unregister (list_data_set, name));
277 void plugin_init_all (void)
279 int (*callback) (void);
282 gethostname (hostname, sizeof (hostname));
284 if (list_init == NULL)
287 le = llist_head (list_init);
290 callback = le->value;
295 } /* void plugin_init_all */
297 void plugin_read_all (const int *loop)
299 int (*callback) (void);
302 if (list_read == NULL)
305 le = llist_head (list_read);
306 while ((*loop == 0) && (le != NULL))
308 callback = le->value;
313 } /* void plugin_read_all */
315 void plugin_shutdown_all (void)
317 int (*callback) (void);
320 if (list_shutdown == NULL)
323 le = llist_head (list_shutdown);
326 callback = le->value;
331 } /* void plugin_shutdown_all */
333 int plugin_dispatch_values (const char *name, const value_list_t *vl)
335 int (*callback) (const data_set_t *, const value_list_t *);
339 if (list_write == NULL)
342 le = llist_search (list_data_set, name);
345 DBG ("No such dataset registered: %s", name);
349 ds = (data_set_t *) le->value;
351 DBG ("time = %u; host = %s; "
352 "plugin = %s; plugin_instance = %s; "
353 "type = %s; type_instance = %s;",
354 (unsigned int) vl->time, vl->host,
355 vl->plugin, vl->plugin_instance,
356 ds->type, vl->type_instance);
358 le = llist_head (list_write);
361 callback = le->value;
362 (*callback) (ds, vl);
370 void plugin_complain (int level, complain_t *c, const char *format, ...)
382 step = atoi (COLLECTD_STEP);
385 if (c->interval < step)
390 if (c->interval > 86400)
393 c->delay = c->interval / step;
395 va_start (ap, format);
396 vsnprintf (message, 512, format, ap);
400 syslog (level, message);
403 void plugin_relief (int level, complain_t *c, const char *format, ...)
408 if (c->interval == 0)
413 va_start (ap, format);
414 vsnprintf (message, 512, format, ap);
418 syslog (level, message);