2 * collectd - src/plugin.c
3 * Copyright (C) 2005 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>
35 void (*write) (char *host, char *inst, char *val);
39 static plugin_t *first_plugin = NULL;
41 extern int operating_mode;
43 static char *plugindir = NULL;
45 char *plugin_get_dir (void)
47 if (plugindir == NULL)
53 void plugin_set_dir (const char *dir)
55 if (plugindir != NULL)
60 else if ((plugindir = strdup (dir)) == NULL)
61 syslog (LOG_ERR, "strdup: %s", strerror (errno));
65 * Returns the number of plugins registered
67 int plugin_count (void)
72 for (i = 0, p = first_plugin; p != NULL; p = p->next)
79 * Returns the plugins with the type `type' or NULL if it's not found.
81 plugin_t *plugin_search (const char *type)
88 for (ret = first_plugin; ret != NULL; ret = ret->next)
89 if (strcmp (ret->type, type) == 0)
96 * Returns true if the plugin is loaded (i.e. `exists') and false otherwise.
97 * This is used in `configfile.c' to skip sections that are not needed..
99 int plugin_exists (char *type)
101 if (plugin_search (type) == NULL)
108 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
109 * object, but it will bitch about a shared object not having a
110 * ``module_register'' symbol..
112 int plugin_load_file (char *file)
115 void (*reg_handle) (void);
118 lt_dlerror (); /* clear errors */
120 if ((dlh = lt_dlopen (file)) == NULL)
123 if ((reg_handle = lt_dlsym (dlh, "module_register")) == NULL)
125 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
126 file, lt_dlerror ());
137 int plugin_load (const char *type)
141 char filename[BUFSIZE];
142 char typename[BUFSIZE];
148 dir = plugin_get_dir ();
151 /* don't load twice */
152 if (plugin_search (type) != NULL)
155 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
156 * type when matching the filename */
157 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
159 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
162 typename_len = strlen (typename);
164 if ((dh = opendir (dir)) == NULL)
166 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
170 while ((de = readdir (dh)) != NULL)
172 if (strncasecmp (de->d_name, typename, typename_len))
175 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
177 syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
181 if (lstat (filename, &statbuf) == -1)
183 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
186 else if (!S_ISREG (statbuf.st_mode))
188 /* don't follow symlinks */
192 if (plugin_load_file (filename) == 0)
206 * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
208 int plugin_load_all (char *dir)
212 char filename[BUFSIZE];
216 dir = plugin_get_dir ();
218 plugin_set_dir (dir);
220 if ((dh = opendir (dir)) == NULL)
222 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
226 while ((de = readdir (dh)) != NULL)
228 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
230 syslog (LOG_WARNING, "snprintf: truncated: %s/%s", dir, de->d_name);
234 if (lstat (filename, &statbuf) == -1)
236 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
239 else if (!S_ISREG (statbuf.st_mode))
244 plugin_load_file (filename);
249 return (plugin_count ());
254 * Call `init' on all plugins (if given)
256 void plugin_init_all (void)
260 for (p = first_plugin; p != NULL; p = p->next)
266 * Call `read' on all plugins (if given)
268 void plugin_read_all (void)
272 for (p = first_plugin; p != NULL; p = p->next)
278 * Add plugin to the linked list of registered plugins.
280 void plugin_register (char *type,
283 void (*write) (char *, char *, char *))
287 if (plugin_search (type) != NULL)
291 if (operating_mode != MODE_SERVER)
293 if ((init != NULL) && (read == NULL))
294 syslog (LOG_NOTICE, "Plugin `%s' doesn't provide a read function.", type);
296 if ((p = (plugin_t *) malloc (sizeof (plugin_t))) == NULL)
299 if ((p->type = strdup (type)) == NULL)
309 p->next = first_plugin;
314 * Send received data back to the plugin/module which will append DS
315 * definitions and pass it on to ``rrd_update_file''.
317 void plugin_write (char *host, char *type, char *inst, char *val)
321 if ((p = plugin_search (type)) == NULL)
324 if (p->write == NULL)
327 (*p->write) (host, inst, val);
331 * Receive data from the plugin/module and get it somehow to ``plugin_write'':
332 * Either using ``network_send'' (when in network/client mode) or call it
333 * directly (in local mode).
335 void plugin_submit (char *type, char *inst, char *val)
337 if (operating_mode == MODE_CLIENT)
338 network_send (type, inst, val);
340 plugin_write (NULL, type, inst, val);