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>
29 #include "utils_debug.h"
36 void (*write) (char *host, char *inst, char *val);
40 static plugin_t *first_plugin = NULL;
42 extern int operating_mode;
44 static char *plugindir = NULL;
46 char *plugin_get_dir (void)
48 if (plugindir == NULL)
54 void plugin_set_dir (const char *dir)
56 if (plugindir != NULL)
61 else if ((plugindir = strdup (dir)) == NULL)
62 syslog (LOG_ERR, "strdup: %s", strerror (errno));
66 * Returns the number of plugins registered
68 int plugin_count (void)
73 for (i = 0, p = first_plugin; p != NULL; p = p->next)
80 * Returns the plugins with the type `type' or NULL if it's not found.
82 plugin_t *plugin_search (const char *type)
89 for (ret = first_plugin; ret != NULL; ret = ret->next)
90 if (strcmp (ret->type, type) == 0)
97 * Returns true if the plugin is loaded (i.e. `exists') and false otherwise.
98 * This is used in `configfile.c' to skip sections that are not needed..
100 int plugin_exists (char *type)
102 if (plugin_search (type) == NULL)
109 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
110 * object, but it will bitch about a shared object not having a
111 * ``module_register'' symbol..
113 int plugin_load_file (char *file)
116 void (*reg_handle) (void);
118 DBG ("file = %s", file);
121 lt_dlerror (); /* clear errors */
123 if ((dlh = lt_dlopen (file)) == NULL)
125 const char *error = lt_dlerror ();
127 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
128 DBG ("lt_dlopen failed: %s", error);
132 if ((reg_handle = lt_dlsym (dlh, "module_register")) == NULL)
134 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
135 file, lt_dlerror ());
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 /* don't load twice */
163 if (plugin_search (type) != NULL)
166 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
167 * type when matching the filename */
168 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
170 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
173 typename_len = strlen (typename);
175 if ((dh = opendir (dir)) == NULL)
177 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
181 while ((de = readdir (dh)) != NULL)
183 if (strncasecmp (de->d_name, typename, typename_len))
186 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
188 syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
192 if (lstat (filename, &statbuf) == -1)
194 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
197 else if (!S_ISREG (statbuf.st_mode))
199 /* don't follow symlinks */
203 if (plugin_load_file (filename) == 0)
217 * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
219 int plugin_load_all (char *dir)
223 char filename[BUFSIZE];
227 dir = plugin_get_dir ();
229 plugin_set_dir (dir);
231 if ((dh = opendir (dir)) == NULL)
233 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
237 while ((de = readdir (dh)) != NULL)
239 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
241 syslog (LOG_WARNING, "snprintf: truncated: %s/%s", dir, de->d_name);
245 if (lstat (filename, &statbuf) == -1)
247 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
250 else if (!S_ISREG (statbuf.st_mode))
255 plugin_load_file (filename);
260 return (plugin_count ());
265 * Call `init' on all plugins (if given)
267 void plugin_init_all (void)
271 for (p = first_plugin; p != NULL; p = p->next)
277 * Call `read' on all plugins (if given)
279 void plugin_read_all (void)
283 for (p = first_plugin; p != NULL; p = p->next)
289 * Add plugin to the linked list of registered plugins.
291 void plugin_register (char *type,
294 void (*write) (char *, char *, char *))
298 if (plugin_search (type) != NULL)
302 if (operating_mode != MODE_SERVER)
304 if ((init != NULL) && (read == NULL))
305 syslog (LOG_NOTICE, "Plugin `%s' doesn't provide a read function.", type);
307 if ((p = (plugin_t *) malloc (sizeof (plugin_t))) == NULL)
310 if ((p->type = strdup (type)) == NULL)
320 p->next = first_plugin;
325 * Send received data back to the plugin/module which will append DS
326 * definitions and pass it on to ``rrd_update_file''.
328 void plugin_write (char *host, char *type, char *inst, char *val)
332 if ((p = plugin_search (type)) == NULL)
335 if (p->write == NULL)
338 (*p->write) (host, inst, val);
342 * Receive data from the plugin/module and get it somehow to ``plugin_write'':
343 * Either using ``network_send'' (when in network/client mode) or call it
344 * directly (in local mode).
346 void plugin_submit (char *type, char *inst, char *val)
348 if (operating_mode == MODE_CLIENT)
349 network_send (type, inst, val);
351 plugin_write (NULL, type, inst, val);