Bumped version to 3.11.6; Updated ChangeLog.
[collectd.git] / src / plugin.c
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  **/
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         void (*shutdown) (void);
38         struct plugin *next;
39 } plugin_t;
40
41 static plugin_t *first_plugin = NULL;
42
43 extern int operating_mode;
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 (const 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 (const 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         DBG ("file = %s", file);
120
121         lt_dlinit ();
122         lt_dlerror (); /* clear errors */
123
124         if ((dlh = lt_dlopen (file)) == NULL)
125         {
126                 const char *error = lt_dlerror ();
127
128                 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
129                 DBG ("lt_dlopen failed: %s", error);
130                 return (1);
131         }
132
133         if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
134         {
135                 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
136                                 file, lt_dlerror ());
137                 lt_dlclose (dlh);
138                 return (-1);
139         }
140
141         (*reg_handle) ();
142
143         return (0);
144 }
145
146 #define BUFSIZE 512
147 int plugin_load (const char *type)
148 {
149         DIR  *dh;
150         char *dir;
151         char  filename[BUFSIZE];
152         char  typename[BUFSIZE];
153         int   typename_len;
154         int   ret;
155         struct stat    statbuf;
156         struct dirent *de;
157
158         DBG ("type = %s", type);
159
160         dir = plugin_get_dir ();
161         ret = 1;
162
163         /* don't load twice */
164         if (plugin_search (type) != NULL)
165                 return (0);
166
167         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
168          * type when matching the filename */
169         if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
170         {
171                 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
172                 return (-1);
173         }
174         typename_len = strlen (typename);
175
176         if ((dh = opendir (dir)) == NULL)
177         {
178                 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
179                 return (-1);
180         }
181
182         while ((de = readdir (dh)) != NULL)
183         {
184                 if (strncasecmp (de->d_name, typename, typename_len))
185                         continue;
186
187                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
188                 {
189                         syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
190                         continue;
191                 }
192
193                 if (lstat (filename, &statbuf) == -1)
194                 {
195                         syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
196                         continue;
197                 }
198                 else if (!S_ISREG (statbuf.st_mode))
199                 {
200                         /* don't follow symlinks */
201                         continue;
202                 }
203
204                 if (plugin_load_file (filename) == 0)
205                 {
206                         /* success */
207                         ret = 0;
208                         break;
209                 }
210         }
211
212         closedir (dh);
213
214         return (ret);
215 }
216
217 /*
218  * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
219  */
220 int plugin_load_all (char *dir)
221 {
222         DIR *dh;
223         struct dirent *de;
224         char filename[BUFSIZE];
225         struct stat statbuf;
226
227         if (dir == NULL)
228                 dir = plugin_get_dir ();
229         else
230                 plugin_set_dir (dir);
231
232         if ((dh = opendir (dir)) == NULL)
233         {
234                 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
235                 return (0);
236         }
237
238         while ((de = readdir (dh)) != NULL)
239         {
240                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
241                 {
242                         syslog (LOG_WARNING, "snprintf: truncated: %s/%s", dir, de->d_name);
243                         continue;
244                 }
245
246                 if (lstat (filename, &statbuf) == -1)
247                 {
248                         syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
249                         continue;
250                 }
251                 else if (!S_ISREG (statbuf.st_mode))
252                 {
253                         continue;
254                 }
255
256                 plugin_load_file (filename);
257         }
258
259         closedir (dh);
260
261         return (plugin_count ());
262 }
263 #undef BUFSIZE
264
265 /*
266  * Call `init' on all plugins (if given)
267  */
268 void plugin_init_all (void)
269 {
270         plugin_t *p;
271
272         for (p = first_plugin; p != NULL; p = p->next)
273                 if (p->init != NULL)
274                         (*p->init) ();
275 }
276
277 /*
278  * Call `read' on all plugins (if given)
279  */
280 void plugin_read_all (const int *loop)
281 {
282         plugin_t *p;
283
284         for (p = first_plugin; (*loop == 0) && (p != NULL); p = p->next)
285                 if (p->read != NULL)
286                         (*p->read) ();
287 }
288
289 /*
290  * Call `shutdown' on all plugins (if given)
291  */
292 void plugin_shutdown_all (void)
293 {
294         plugin_t *p;
295
296         for (p = first_plugin; NULL != p; p = p->next)
297                 if (NULL != p->shutdown)
298                         (*p->shutdown) ();
299         return;
300 }
301
302 /*
303  * Add plugin to the linked list of registered plugins.
304  */
305 void plugin_register (char *type,
306                 void (*init) (void),
307                 void (*read) (void),
308                 void (*write) (char *, char *, char *))
309 {
310         plugin_t *p;
311
312         if (plugin_search (type) != NULL)
313                 return;
314
315 #ifdef HAVE_LIBRRD
316         if (operating_mode != MODE_SERVER)
317 #endif
318                 if ((init != NULL) && (read == NULL))
319                         syslog (LOG_NOTICE, "Plugin `%s' doesn't provide a read function.", type);
320
321         if ((p = (plugin_t *) malloc (sizeof (plugin_t))) == NULL)
322                 return;
323
324         if ((p->type = strdup (type)) == NULL)
325         {
326                 free (p);
327                 return;
328         }
329
330         p->init  = init;
331         p->read  = read;
332         p->write = write;
333
334         p->shutdown = NULL;
335
336         p->next = first_plugin;
337         first_plugin = p;
338 }
339
340 /*
341  * Register the shutdown function (optional).
342  */
343 int plugin_register_shutdown (char *type, void (*shutdown) (void))
344 {
345         plugin_t *p = plugin_search (type);
346
347         if (NULL == p)
348                 return -1;
349
350         p->shutdown = shutdown;
351         return 0;
352 }
353
354 /*
355  * Send received data back to the plugin/module which will append DS
356  * definitions and pass it on to ``rrd_update_file''.
357  */
358 void plugin_write (char *host, char *type, char *inst, char *val)
359 {
360         plugin_t *p;
361
362         if ((p = plugin_search (type)) == NULL)
363                 return;
364
365         if (p->write == NULL)
366                 return;
367
368         (*p->write) (host, inst, val);
369 }
370
371 /*
372  * Receive data from the plugin/module and get it somehow to ``plugin_write'':
373  * Either using ``network_send'' (when in network/client mode) or call it
374  * directly (in local mode).
375  */
376 void plugin_submit (char *type, char *inst, char *val)
377 {
378         if (inst == NULL)
379                 inst = "-";
380
381         if ((type == NULL) || (val == NULL))
382         {
383                 DBG ("Help! NULL-pointer! type = %s; inst = %s; val = %s;",
384                                 (type == NULL) ? "(null)" : type,
385                                 inst,
386                                 (val == NULL) ? "(null)" : val);
387                 return;
388         }
389
390         if (operating_mode == MODE_CLIENT)
391                 network_send (type, inst, val);
392         else
393                 plugin_write (NULL, type, inst, val);
394 }
395
396 void plugin_complain (int level, complain_t *c, const char *format, ...)
397 {
398         char message[512];
399         va_list ap;
400         int step;
401
402         if (c->delay > 0)
403         {
404                 c->delay--;
405                 return;
406         }
407
408         step = atoi (COLLECTD_STEP);
409         assert (step > 0);
410
411         if (c->interval < step)
412                 c->interval = step;
413         else
414                 c->interval *= 2;
415
416         if (c->interval > 86400)
417                 c->interval = 86400;
418
419         c->delay = c->interval / step;
420
421         va_start (ap, format);
422         vsnprintf (message, 512, format, ap);
423         message[511] = '\0';
424         va_end (ap);
425
426         syslog (level, message);
427 }
428
429 void plugin_relief (int level, complain_t *c, const char *format, ...)
430 {
431         char message[512];
432         va_list ap;
433
434         if (c->interval == 0)
435                 return;
436
437         c->interval = 0;
438
439         va_start (ap, format);
440         vsnprintf (message, 512, format, ap);
441         message[511] = '\0';
442         va_end (ap);
443
444         syslog (level, message);
445 }