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;
53 char hostname[DATA_MAX_NAME_LEN] = "localhost";
58 static const char *plugin_get_dir (void)
60 if (plugindir == NULL)
66 static int register_callback (llist_t **list, const char *name, void *callback)
71 && ((*list = llist_create ()) == NULL))
74 le = llist_search (*list, name);
77 le = llentry_create (name, callback);
81 llist_append (*list, le);
89 } /* int register_callback */
91 static int plugin_unregister (llist_t *list, const char *name)
95 e = llist_search (list, name);
100 llist_remove (list, e);
104 } /* int plugin_unregister */
107 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
108 * object, but it will bitch about a shared object not having a
109 * ``module_register'' symbol..
111 static int plugin_load_file (char *file)
114 void (*reg_handle) (void);
116 DBG ("file = %s", file);
119 lt_dlerror (); /* clear errors */
121 if ((dlh = lt_dlopen (file)) == NULL)
123 const char *error = lt_dlerror ();
125 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
126 DBG ("lt_dlopen failed: %s", error);
130 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
132 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
133 file, lt_dlerror ());
146 void plugin_set_dir (const char *dir)
148 if (plugindir != NULL)
153 else if ((plugindir = strdup (dir)) == NULL)
154 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
158 int plugin_load (const char *type)
162 char filename[BUFSIZE];
163 char typename[BUFSIZE];
169 DBG ("type = %s", type);
171 dir = plugin_get_dir ();
174 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
175 * type when matching the filename */
176 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
178 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
181 typename_len = strlen (typename);
183 if ((dh = opendir (dir)) == NULL)
185 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
189 while ((de = readdir (dh)) != NULL)
191 if (strncasecmp (de->d_name, typename, typename_len))
194 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
196 syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
200 if (lstat (filename, &statbuf) == -1)
202 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
205 else if (!S_ISREG (statbuf.st_mode))
207 /* don't follow symlinks */
211 if (plugin_load_file (filename) == 0)
225 * The `register_*' functions follow
227 int plugin_register_config (const char *name,
228 int (*callback) (const char *key, const char *val),
229 const char **keys, int keys_num)
231 cf_register (name, callback, keys, keys_num);
233 } /* int plugin_register_config */
235 int plugin_register_init (const char *name,
236 int (*callback) (void))
238 return (register_callback (&list_init, name, (void *) callback));
239 } /* plugin_register_init */
241 int plugin_register_read (const char *name,
242 int (*callback) (void))
246 rf = (read_func_t *) malloc (sizeof (read_func_t));
249 syslog (LOG_ERR, "plugin_register_read: malloc failed: %s",
254 memset (rf, '\0', sizeof (read_func_t));
255 rf->wait_time = atoi (COLLECTD_STEP);
257 rf->callback = callback;
259 return (register_callback (&list_read, name, (void *) rf));
260 } /* int plugin_register_read */
262 int plugin_register_write (const char *name,
263 int (*callback) (const data_set_t *ds, const value_list_t *vl))
265 return (register_callback (&list_write, name, (void *) callback));
266 } /* int plugin_register_write */
268 int plugin_register_shutdown (char *name,
269 int (*callback) (void))
271 return (register_callback (&list_shutdown, name, (void *) callback));
272 } /* int plugin_register_shutdown */
274 int plugin_register_data_set (const data_set_t *ds)
276 return (register_callback (&list_data_set, ds->type, (void *) ds));
277 } /* int plugin_register_data_set */
279 int plugin_unregister_init (const char *name)
281 return (plugin_unregister (list_init, name));
284 int plugin_unregister_read (const char *name)
286 return (plugin_unregister (list_read, name));
289 e = llist_search (list_read, name);
294 llist_remove (list_read, e);
301 int plugin_unregister_write (const char *name)
303 return (plugin_unregister (list_write, name));
306 int plugin_unregister_shutdown (const char *name)
308 return (plugin_unregister (list_shutdown, name));
311 int plugin_unregister_data_set (const char *name)
313 return (plugin_unregister (list_data_set, name));
316 void plugin_init_all (void)
318 int (*callback) (void);
322 gethostname (hostname, sizeof (hostname));
324 if (list_init == NULL)
327 le = llist_head (list_init);
330 callback = le->value;
331 status = (*callback) ();
335 syslog (LOG_ERR, "Initialization of plugin `%s' "
336 "failed with status %i. "
337 "Plugin will be unloaded. TODO!",
339 plugin_unregister_read (le->key);
344 } /* void plugin_init_all */
346 void plugin_read_all (const int *loop)
353 if (list_read == NULL)
356 step = atoi (COLLECTD_STEP);
358 le = llist_head (list_read);
359 while ((*loop == 0) && (le != NULL))
361 rf = (read_func_t *) le->value;
363 if (rf->wait_left > 0)
364 rf->wait_left -= step;
365 if (rf->wait_left > 0)
371 status = rf->callback ();
374 rf->wait_left = rf->wait_time;
375 rf->wait_time = rf->wait_time * 2;
376 if (rf->wait_time > 86400)
377 rf->wait_time = 86400;
379 syslog (LOG_NOTICE, "read-function of plugin `%s' "
380 "failed. Will syspend it for %i "
381 "seconds.", le->key, rf->wait_left);
386 rf->wait_time = step;
390 } /* while ((*loop == 0) && (le != NULL)) */
391 } /* void plugin_read_all */
393 void plugin_shutdown_all (void)
395 int (*callback) (void);
398 if (list_shutdown == NULL)
401 le = llist_head (list_shutdown);
404 callback = le->value;
409 } /* void plugin_shutdown_all */
411 int plugin_dispatch_values (const char *name, const value_list_t *vl)
413 int (*callback) (const data_set_t *, const value_list_t *);
417 if (list_write == NULL)
420 le = llist_search (list_data_set, name);
423 DBG ("No such dataset registered: %s", name);
427 ds = (data_set_t *) le->value;
429 DBG ("time = %u; host = %s; "
430 "plugin = %s; plugin_instance = %s; "
431 "type = %s; type_instance = %s;",
432 (unsigned int) vl->time, vl->host,
433 vl->plugin, vl->plugin_instance,
434 ds->type, vl->type_instance);
436 le = llist_head (list_write);
439 callback = le->value;
440 (*callback) (ds, vl);
448 void plugin_complain (int level, complain_t *c, const char *format, ...)
460 step = atoi (COLLECTD_STEP);
463 if (c->interval < step)
468 if (c->interval > 86400)
471 c->delay = c->interval / step;
473 va_start (ap, format);
474 vsnprintf (message, 512, format, ap);
478 syslog (level, message);
481 void plugin_relief (int level, complain_t *c, const char *format, ...)
486 if (c->interval == 0)
491 va_start (ap, format);
492 vsnprintf (message, 512, format, ap);
496 syslog (level, message);