1 /**
2 * collectd - src/plugin.c
3 * Copyright (C) 2005,2006 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
25 #include <ltdl.h>
27 #include "plugin.h"
28 #include "network.h"
29 #include "utils_debug.h"
31 typedef struct plugin
32 {
33 char *type;
34 void (*init) (void);
35 void (*read) (void);
36 void (*write) (char *host, char *inst, char *val);
37 struct plugin *next;
38 } plugin_t;
40 static plugin_t *first_plugin = NULL;
42 extern int operating_mode;
44 static char *plugindir = NULL;
46 char *plugin_get_dir (void)
47 {
48 if (plugindir == NULL)
49 return (PLUGINDIR);
50 else
51 return (plugindir);
52 }
54 void plugin_set_dir (const char *dir)
55 {
56 if (plugindir != NULL)
57 free (plugindir);
59 if (dir == NULL)
60 plugindir = NULL;
61 else if ((plugindir = strdup (dir)) == NULL)
62 syslog (LOG_ERR, "strdup: %s", strerror (errno));
63 }
65 /*
66 * Returns the number of plugins registered
67 */
68 int plugin_count (void)
69 {
70 int i;
71 plugin_t *p;
73 for (i = 0, p = first_plugin; p != NULL; p = p->next)
74 i++;
76 return (i);
77 }
79 /*
80 * Returns the plugins with the type `type' or NULL if it's not found.
81 */
82 plugin_t *plugin_search (const char *type)
83 {
84 plugin_t *ret;
86 if (type == NULL)
87 return (NULL);
89 for (ret = first_plugin; ret != NULL; ret = ret->next)
90 if (strcmp (ret->type, type) == 0)
91 break;
93 return (ret);
94 }
96 /*
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..
99 */
100 int plugin_exists (char *type)
101 {
102 if (plugin_search (type) == NULL)
103 return (0);
104 else
105 return (1);
106 }
108 /*
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..
112 */
113 int plugin_load_file (char *file)
114 {
115 lt_dlhandle dlh;
116 void (*reg_handle) (void);
118 DBG ("file = %s", file);
120 lt_dlinit ();
121 lt_dlerror (); /* clear errors */
123 if ((dlh = lt_dlopen (file)) == NULL)
124 {
125 const char *error = lt_dlerror ();
127 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
128 DBG ("lt_dlopen failed: %s", error);
129 return (1);
130 }
132 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
133 {
134 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
135 file, lt_dlerror ());
136 lt_dlclose (dlh);
137 return (-1);
138 }
140 (*reg_handle) ();
142 return (0);
143 }
145 #define BUFSIZE 512
146 int plugin_load (const char *type)
147 {
148 DIR *dh;
149 char *dir;
150 char filename[BUFSIZE];
151 char typename[BUFSIZE];
152 int typename_len;
153 int ret;
154 struct stat statbuf;
155 struct dirent *de;
157 DBG ("type = %s", type);
159 dir = plugin_get_dir ();
160 ret = 1;
162 /* don't load twice */
163 if (plugin_search (type) != NULL)
164 return (0);
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)
169 {
170 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
171 return (-1);
172 }
173 typename_len = strlen (typename);
175 if ((dh = opendir (dir)) == NULL)
176 {
177 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
178 return (-1);
179 }
181 while ((de = readdir (dh)) != NULL)
182 {
183 if (strncasecmp (de->d_name, typename, typename_len))
184 continue;
186 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
187 {
188 syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
189 continue;
190 }
192 if (lstat (filename, &statbuf) == -1)
193 {
194 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
195 continue;
196 }
197 else if (!S_ISREG (statbuf.st_mode))
198 {
199 /* don't follow symlinks */
200 continue;
201 }
203 if (plugin_load_file (filename) == 0)
204 {
205 /* success */
206 ret = 0;
207 break;
208 }
209 }
211 closedir (dh);
213 return (ret);
214 }
216 /*
217 * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
218 */
219 int plugin_load_all (char *dir)
220 {
221 DIR *dh;
222 struct dirent *de;
223 char filename[BUFSIZE];
224 struct stat statbuf;
226 if (dir == NULL)
227 dir = plugin_get_dir ();
228 else
229 plugin_set_dir (dir);
231 if ((dh = opendir (dir)) == NULL)
232 {
233 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
234 return (0);
235 }
237 while ((de = readdir (dh)) != NULL)
238 {
239 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
240 {
241 syslog (LOG_WARNING, "snprintf: truncated: %s/%s", dir, de->d_name);
242 continue;
243 }
245 if (lstat (filename, &statbuf) == -1)
246 {
247 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
248 continue;
249 }
250 else if (!S_ISREG (statbuf.st_mode))
251 {
252 continue;
253 }
255 plugin_load_file (filename);
256 }
258 closedir (dh);
260 return (plugin_count ());
261 }
262 #undef BUFSIZE
264 /*
265 * Call `init' on all plugins (if given)
266 */
267 void plugin_init_all (void)
268 {
269 plugin_t *p;
271 for (p = first_plugin; p != NULL; p = p->next)
272 if (p->init != NULL)
273 (*p->init) ();
274 }
276 /*
277 * Call `read' on all plugins (if given)
278 */
279 void plugin_read_all (void)
280 {
281 plugin_t *p;
283 for (p = first_plugin; p != NULL; p = p->next)
284 if (p->read != NULL)
285 (*p->read) ();
286 }
288 /*
289 * Add plugin to the linked list of registered plugins.
290 */
291 void plugin_register (char *type,
292 void (*init) (void),
293 void (*read) (void),
294 void (*write) (char *, char *, char *))
295 {
296 plugin_t *p;
298 if (plugin_search (type) != NULL)
299 return;
301 #ifdef HAVE_LIBRRD
302 if (operating_mode != MODE_SERVER)
303 #endif
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)
308 return;
310 if ((p->type = strdup (type)) == NULL)
311 {
312 free (p);
313 return;
314 }
316 p->init = init;
317 p->read = read;
318 p->write = write;
320 p->next = first_plugin;
321 first_plugin = p;
322 }
324 /*
325 * Send received data back to the plugin/module which will append DS
326 * definitions and pass it on to ``rrd_update_file''.
327 */
328 void plugin_write (char *host, char *type, char *inst, char *val)
329 {
330 plugin_t *p;
332 if ((p = plugin_search (type)) == NULL)
333 return;
335 if (p->write == NULL)
336 return;
338 (*p->write) (host, inst, val);
339 }
341 /*
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).
345 */
346 void plugin_submit (char *type, char *inst, char *val)
347 {
348 if (operating_mode == MODE_CLIENT)
349 network_send (type, inst, val);
350 else
351 plugin_write (NULL, type, inst, val);
352 }