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