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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
28 #include "configfile.h"
29 #include "utils_llist.h"
30 #include "utils_debug.h"
35 static llist_t *list_init;
36 static llist_t *list_read;
37 static llist_t *list_write;
38 static llist_t *list_shutdown;
39 static llist_t *list_data_set;
41 static char *plugindir = NULL;
46 static const char *plugin_get_dir (void)
48 if (plugindir == NULL)
54 static int register_callback (llist_t **list, const char *name, void *callback)
59 && ((*list = llist_create ()) == NULL))
62 le = llist_search (*list, name);
65 le = llentry_create (name, callback);
69 llist_append (*list, le);
77 } /* int register_callback */
80 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
81 * object, but it will bitch about a shared object not having a
82 * ``module_register'' symbol..
84 static int plugin_load_file (char *file)
87 void (*reg_handle) (void);
89 DBG ("file = %s", file);
92 lt_dlerror (); /* clear errors */
94 if ((dlh = lt_dlopen (file)) == NULL)
96 const char *error = lt_dlerror ();
98 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
99 DBG ("lt_dlopen failed: %s", error);
103 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
105 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
106 file, lt_dlerror ());
119 void plugin_set_dir (const char *dir)
121 if (plugindir != NULL)
126 else if ((plugindir = strdup (dir)) == NULL)
127 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
131 int plugin_load (const char *type)
135 char filename[BUFSIZE];
136 char typename[BUFSIZE];
142 DBG ("type = %s", type);
144 dir = plugin_get_dir ();
147 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
148 * type when matching the filename */
149 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
151 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
154 typename_len = strlen (typename);
156 if ((dh = opendir (dir)) == NULL)
158 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
162 while ((de = readdir (dh)) != NULL)
164 if (strncasecmp (de->d_name, typename, typename_len))
167 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
169 syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
173 if (lstat (filename, &statbuf) == -1)
175 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
178 else if (!S_ISREG (statbuf.st_mode))
180 /* don't follow symlinks */
184 if (plugin_load_file (filename) == 0)
198 * The `register_*' functions follow
200 int plugin_register_config (const char *name,
201 int (*callback) (const char *key, const char *val),
202 const char **keys, int keys_num)
204 cf_register (name, callback, keys, keys_num);
206 } /* int plugin_register_config */
208 int plugin_register_init (const char *name,
209 int (*callback) (void))
211 return (register_callback (&list_init, name, (void *) callback));
212 } /* plugin_register_init */
214 int plugin_register_read (const char *name,
215 int (*callback) (void))
217 return (register_callback (&list_read, name, (void *) callback));
218 } /* int plugin_register_read */
220 int plugin_register_write (const char *name,
221 int (*callback) (const data_set_t *ds, const value_list_t *vl))
223 return (register_callback (&list_write, name, (void *) callback));
224 } /* int plugin_register_write */
226 int plugin_register_shutdown (char *name,
227 int (*callback) (void))
229 return (register_callback (&list_shutdown, name, (void *) callback));
230 } /* int plugin_register_shutdown */
232 int plugin_register_data_set (const data_set_t *ds)
234 return (register_callback (&list_data_set, ds->type, (void *) ds));
235 } /* int plugin_register_data_set */
237 void plugin_init_all (void)
239 int (*callback) (void);
242 if (list_init == NULL)
245 le = llist_head (list_init);
248 callback = le->value;
253 } /* void plugin_init_all */
255 void plugin_read_all (const int *loop)
257 int (*callback) (void);
260 if (list_read == NULL)
263 le = llist_head (list_read);
264 while ((*loop == 0) && (le != NULL))
266 callback = le->value;
271 } /* void plugin_read_all */
273 void plugin_shutdown_all (void)
275 int (*callback) (void);
278 if (list_shutdown == NULL)
281 le = llist_head (list_shutdown);
284 callback = le->value;
289 } /* void plugin_shutdown_all */
291 int plugin_dispatch_values (const char *name, const value_list_t *vl)
293 int (*callback) (const data_set_t *, const value_list_t *);
297 if (list_write == NULL)
300 le = llist_search (list_data_set, name);
304 ds = (data_set_t *) le->value;
306 le = llist_head (list_write);
309 callback = le->value;
310 (*callback) (ds, vl);
318 void plugin_complain (int level, complain_t *c, const char *format, ...)
330 step = atoi (COLLECTD_STEP);
333 if (c->interval < step)
338 if (c->interval > 86400)
341 c->delay = c->interval / step;
343 va_start (ap, format);
344 vsnprintf (message, 512, format, ap);
348 syslog (level, message);
351 void plugin_relief (int level, complain_t *c, const char *format, ...)
356 if (c->interval == 0)
361 va_start (ap, format);
362 vsnprintf (message, 512, format, ap);
366 syslog (level, message);