Implemented `plugin_get_dir' and `plugin_set_dir'
[collectd.git] / src / plugin.c
1 /**
2  * collectd - src/plugin.c
3  * Copyright (C) 2005  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  **/
22
23 #include "collectd.h"
24
25 #include <ltdl.h>
26
27 #include "plugin.h"
28 #include "multicast.h"
29
30 typedef struct plugin
31 {
32         char *type;
33         void (*init) (void);
34         void (*read) (void);
35         void (*write) (char *host, char *inst, char *val);
36         struct plugin *next;
37 } plugin_t;
38
39 static plugin_t *first_plugin = NULL;
40
41 #ifdef HAVE_LIBRRD
42 extern int operating_mode;
43 #endif
44
45 static char *plugindir = NULL;
46
47 char *plugin_get_dir (void)
48 {
49         if (plugindir == NULL)
50                 return (PLUGINDIR);
51         else
52                 return (plugindir);
53 }
54
55 void plugin_set_dir (char *dir)
56 {
57         if (plugindir != NULL)
58                 free (plugindir);
59
60         if (dir == NULL)
61                 plugindir = NULL;
62         else if ((plugindir = strdup (dir)) == NULL)
63                 syslog (LOG_ERR, "strdup: %s", strerror (errno));
64 }
65
66 /*
67  * Returns the number of plugins registered
68  */
69 int plugin_count (void)
70 {
71         int i;
72         plugin_t *p;
73
74         for (i = 0, p = first_plugin; p != NULL; p = p->next)
75                 i++;
76
77         return (i);
78 }
79
80 /*
81  * Returns the plugins with the type `type' or NULL if it's not found.
82  */
83 plugin_t *plugin_search (char *type)
84 {
85         plugin_t *ret;
86
87         if (type == NULL)
88                 return (NULL);
89
90         for (ret = first_plugin; ret != NULL; ret = ret->next)
91                 if (strcmp (ret->type, type) == 0)
92                         break;
93
94         return (ret);
95 }
96
97 /*
98  * Returns true if the plugin is loaded (i.e. `exists') and false otherwise.
99  * This is used in `configfile.c' to skip sections that are not needed..
100  */
101 int plugin_exists (char *type)
102 {
103         if (plugin_search (type) == NULL)
104                 return (0);
105         else
106                 return (1);
107 }
108
109 /*
110  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
111  * object, but it will bitch about a shared object not having a
112  * ``module_register'' symbol..
113  */
114 int plugin_load_file (char *file)
115 {
116         lt_dlhandle dlh;
117         void (*reg_handle) (void);
118
119         lt_dlinit ();
120         lt_dlerror (); /* clear errors */
121
122         if ((dlh = lt_dlopen (file)) == NULL)
123                 return (1);
124
125         if ((reg_handle = lt_dlsym (dlh, "module_register")) == NULL)
126         {
127                 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
128                                 file, lt_dlerror ());
129                 lt_dlclose (dlh);
130                 return (-1);
131         }
132
133         (*reg_handle) ();
134
135         return (0);
136 }
137
138 #define BUFSIZE 512
139 int plugin_load (char *type)
140 {
141         DIR  *dh;
142         char *dir;
143         char  filename[BUFSIZE];
144         char  typename[BUFSIZE];
145         int   typename_len;
146         int   ret;
147         struct stat    statbuf;
148         struct dirent *de;
149
150         dir = plugin_get_dir ();
151         ret = 1;
152
153         /* don't load twice */
154         if (plugin_search (type) != NULL)
155                 return (0);
156
157         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
158          * type when matching the filename */
159         if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
160         {
161                 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
162                 return (-1);
163         }
164         typename_len = strlen (typename);
165
166         if ((dh = opendir (dir)) == NULL)
167         {
168                 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
169                 return (-1);
170         }
171
172         while ((de = readdir (dh)) != NULL)
173         {
174                 if (strncmp (de->d_name, typename, typename_len))
175                         continue;
176
177                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
178                 {
179                         syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
180                         continue;
181                 }
182
183                 if (lstat (filename, &statbuf) == -1)
184                 {
185                         syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
186                         continue;
187                 }
188                 else if (!S_ISREG (statbuf.st_mode))
189                 {
190                         /* don't follow symlinks */
191                         continue;
192                 }
193
194                 if (plugin_load_file (filename) == 0)
195                 {
196                         /* success */
197                         ret = 0;
198                         break;
199                 }
200         }
201
202         closedir (dh);
203
204         return (ret);
205 }
206
207 /*
208  * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
209  */
210 int plugin_load_all (char *dir)
211 {
212         DIR *dh;
213         struct dirent *de;
214         char filename[BUFSIZE];
215         struct stat statbuf;
216
217         if (dir == NULL)
218                 dir = plugin_get_dir ();
219         else
220                 plugin_set_dir (dir);
221
222         if ((dh = opendir (dir)) == NULL)
223         {
224                 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
225                 return (0);
226         }
227
228         while ((de = readdir (dh)) != NULL)
229         {
230                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
231                 {
232                         syslog (LOG_WARNING, "snprintf: truncated: %s/%s", dir, de->d_name);
233                         continue;
234                 }
235
236                 if (lstat (filename, &statbuf) == -1)
237                 {
238                         syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
239                         continue;
240                 }
241                 else if (!S_ISREG (statbuf.st_mode))
242                 {
243                         continue;
244                 }
245
246                 plugin_load_file (filename);
247         }
248
249         closedir (dh);
250
251         return (plugin_count ());
252 }
253 #undef BUFSIZE
254
255 /*
256  * Call `init' on all plugins (if given)
257  */
258 void plugin_init_all (void)
259 {
260         plugin_t *p;
261
262         for (p = first_plugin; p != NULL; p = p->next)
263                 if (p->init != NULL)
264                         (*p->init) ();
265 }
266
267 /*
268  * Call `read' on all plugins (if given)
269  */
270 void plugin_read_all (void)
271 {
272         plugin_t *p;
273
274         for (p = first_plugin; p != NULL; p = p->next)
275                 if (p->read != NULL)
276                         (*p->read) ();
277 }
278
279 /*
280  * Add plugin to the linked list of registered plugins.
281  */
282 void plugin_register (char *type,
283                 void (*init) (void),
284                 void (*read) (void),
285                 void (*write) (char *, char *, char *))
286 {
287         plugin_t *p;
288
289         if (plugin_search (type) != NULL)
290                 return;
291
292         if ((p = (plugin_t *) malloc (sizeof (plugin_t))) == NULL)
293                 return;
294
295         if ((p->type = strdup (type)) == NULL)
296         {
297                 free (p);
298                 return;
299         }
300
301         p->init  = init;
302         p->read  = read;
303         p->write = write;
304
305         p->next = first_plugin;
306         first_plugin = p;
307 }
308
309 /*
310  * Send received data back to the plugin/module which will append DS
311  * definitions and pass it on to ``rrd_update_file''.
312  */
313 #ifdef HAVE_LIBRRD
314 void plugin_write (char *host, char *type, char *inst, char *val)
315 {
316         plugin_t *p;
317
318         if ((p = plugin_search (type)) == NULL)
319                 return;
320
321         if (p->write == NULL)
322                 return;
323
324         (*p->write) (host, inst, val);
325 }
326 #endif /* HAVE_LIBRRD */
327
328 /*
329  * Receive data from the plugin/module and get it somehow to ``plugin_write'':
330  * Either using ``multicast_send'' (when in network/client mode) or call it
331  * directly (in local mode).
332  */
333 void plugin_submit (char *type, char *inst, char *val)
334 {
335 #ifdef HAVE_LIBRRD
336         if (operating_mode == MODE_LOCAL)
337                 plugin_write (NULL, type, inst, val);
338         else if (operating_mode == MODE_CLIENT)
339                 multicast_send (type, inst, val);
340         else /* operating_mode == MODE_SERVER */
341                 syslog (LOG_ERR, "WTF is the server doing in ``plugin_submit''?!?\n");
342 #else
343         multicast_send (type, inst, val);
344 #endif
345 }