src/plugin.[ch]: Provide unregister-functions for plugins to remove certain functions.
[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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23
24 #include <ltdl.h>
25
26 #include "plugin.h"
27 #include "configfile.h"
28 #include "utils_llist.h"
29 #include "utils_debug.h"
30
31 /*
32  * Private variables
33  */
34 static llist_t *list_init;
35 static llist_t *list_read;
36 static llist_t *list_write;
37 static llist_t *list_shutdown;
38 static llist_t *list_data_set;
39
40 static char *plugindir = NULL;
41
42 char hostname[DATA_MAX_NAME_LEN] = "localhost";
43
44 /*
45  * Static functions
46  */
47 static const char *plugin_get_dir (void)
48 {
49         if (plugindir == NULL)
50                 return (PLUGINDIR);
51         else
52                 return (plugindir);
53 }
54
55 static int register_callback (llist_t **list, const char *name, void *callback)
56 {
57         llentry_t *le;
58
59         if ((*list == NULL)
60                         && ((*list = llist_create ()) == NULL))
61                 return (-1);
62
63         le = llist_search (*list, name);
64         if (le == NULL)
65         {
66                 le = llentry_create (name, callback);
67                 if (le == NULL)
68                         return (-1);
69
70                 llist_append (*list, le);
71         }
72         else
73         {
74                 le->value = callback;
75         }
76
77         return (0);
78 } /* int register_callback */
79
80 static int plugin_unregister (llist_t *list, const char *name)
81 {
82         llentry_t *e;
83
84         e = llist_search (list, name);
85
86         if (e == NULL)
87                 return (-1);
88
89         llist_remove (list, e);
90
91         return (0);
92 } /* int plugin_unregister */
93
94 /*
95  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
96  * object, but it will bitch about a shared object not having a
97  * ``module_register'' symbol..
98  */
99 static int plugin_load_file (char *file)
100 {
101         lt_dlhandle dlh;
102         void (*reg_handle) (void);
103
104         DBG ("file = %s", file);
105
106         lt_dlinit ();
107         lt_dlerror (); /* clear errors */
108
109         if ((dlh = lt_dlopen (file)) == NULL)
110         {
111                 const char *error = lt_dlerror ();
112
113                 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
114                 DBG ("lt_dlopen failed: %s", error);
115                 return (1);
116         }
117
118         if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
119         {
120                 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
121                                 file, lt_dlerror ());
122                 lt_dlclose (dlh);
123                 return (-1);
124         }
125
126         (*reg_handle) ();
127
128         return (0);
129 }
130
131 /*
132  * Public functions
133  */
134 void plugin_set_dir (const char *dir)
135 {
136         if (plugindir != NULL)
137                 free (plugindir);
138
139         if (dir == NULL)
140                 plugindir = NULL;
141         else if ((plugindir = strdup (dir)) == NULL)
142                 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
143 }
144
145 #define BUFSIZE 512
146 int plugin_load (const char *type)
147 {
148         DIR  *dh;
149         const 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;
156
157         DBG ("type = %s", type);
158
159         dir = plugin_get_dir ();
160         ret = 1;
161
162         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
163          * type when matching the filename */
164         if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
165         {
166                 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
167                 return (-1);
168         }
169         typename_len = strlen (typename);
170
171         if ((dh = opendir (dir)) == NULL)
172         {
173                 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
174                 return (-1);
175         }
176
177         while ((de = readdir (dh)) != NULL)
178         {
179                 if (strncasecmp (de->d_name, typename, typename_len))
180                         continue;
181
182                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
183                 {
184                         syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
185                         continue;
186                 }
187
188                 if (lstat (filename, &statbuf) == -1)
189                 {
190                         syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
191                         continue;
192                 }
193                 else if (!S_ISREG (statbuf.st_mode))
194                 {
195                         /* don't follow symlinks */
196                         continue;
197                 }
198
199                 if (plugin_load_file (filename) == 0)
200                 {
201                         /* success */
202                         ret = 0;
203                         break;
204                 }
205         }
206
207         closedir (dh);
208
209         return (ret);
210 }
211
212 /*
213  * The `register_*' functions follow
214  */
215 int plugin_register_config (const char *name,
216                 int (*callback) (const char *key, const char *val),
217                 const char **keys, int keys_num)
218 {
219         cf_register (name, callback, keys, keys_num);
220         return (0);
221 } /* int plugin_register_config */
222
223 int plugin_register_init (const char *name,
224                 int (*callback) (void))
225 {
226         return (register_callback (&list_init, name, (void *) callback));
227 } /* plugin_register_init */
228
229 int plugin_register_read (const char *name,
230                 int (*callback) (void))
231 {
232         return (register_callback (&list_read, name, (void *) callback));
233 } /* int plugin_register_read */
234
235 int plugin_register_write (const char *name,
236                 int (*callback) (const data_set_t *ds, const value_list_t *vl))
237 {
238         return (register_callback (&list_write, name, (void *) callback));
239 } /* int plugin_register_write */
240
241 int plugin_register_shutdown (char *name,
242                 int (*callback) (void))
243 {
244         return (register_callback (&list_shutdown, name, (void *) callback));
245 } /* int plugin_register_shutdown */
246
247 int plugin_register_data_set (const data_set_t *ds)
248 {
249         return (register_callback (&list_data_set, ds->type, (void *) ds));
250 } /* int plugin_register_data_set */
251
252 int plugin_unregister_init (const char *name)
253 {
254         return (plugin_unregister (list_init, name));
255 }
256
257 int plugin_unregister_read (const char *name)
258 {
259         return (plugin_unregister (list_read, name));
260 }
261
262 int plugin_unregister_write (const char *name)
263 {
264         return (plugin_unregister (list_write, name));
265 }
266
267 int plugin_unregister_shutdown (const char *name)
268 {
269         return (plugin_unregister (list_shutdown, name));
270 }
271
272 int plugin_unregister_data_set (const char *name)
273 {
274         return (plugin_unregister (list_data_set, name));
275 }
276
277 void plugin_init_all (void)
278 {
279         int (*callback) (void);
280         llentry_t *le;
281
282         gethostname (hostname, sizeof (hostname));
283
284         if (list_init == NULL)
285                 return;
286
287         le = llist_head (list_init);
288         while (le != NULL)
289         {
290                 callback = le->value;
291                 (*callback) ();
292
293                 le = le->next;
294         }
295 } /* void plugin_init_all */
296
297 void plugin_read_all (const int *loop)
298 {
299         int (*callback) (void);
300         llentry_t *le;
301
302         if (list_read == NULL)
303                 return;
304
305         le = llist_head (list_read);
306         while ((*loop == 0) && (le != NULL))
307         {
308                 callback = le->value;
309                 (*callback) ();
310
311                 le = le->next;
312         }
313 } /* void plugin_read_all */
314
315 void plugin_shutdown_all (void)
316 {
317         int (*callback) (void);
318         llentry_t *le;
319
320         if (list_shutdown == NULL)
321                 return;
322
323         le = llist_head (list_shutdown);
324         while (le != NULL)
325         {
326                 callback = le->value;
327                 (*callback) ();
328
329                 le = le->next;
330         }
331 } /* void plugin_shutdown_all */
332
333 int plugin_dispatch_values (const char *name, const value_list_t *vl)
334 {
335         int (*callback) (const data_set_t *, const value_list_t *);
336         data_set_t *ds;
337         llentry_t *le;
338
339         if (list_write == NULL)
340                 return (-1);
341
342         le = llist_search (list_data_set, name);
343         if (le == NULL)
344         {
345                 DBG ("No such dataset registered: %s", name);
346                 return (-1);
347         }
348
349         ds = (data_set_t *) le->value;
350
351         DBG ("time = %u; host = %s; "
352                         "plugin = %s; plugin_instance = %s; "
353                         "type = %s; type_instance = %s;",
354                         (unsigned int) vl->time, vl->host,
355                         vl->plugin, vl->plugin_instance,
356                         ds->type, vl->type_instance);
357
358         le = llist_head (list_write);
359         while (le != NULL)
360         {
361                 callback = le->value;
362                 (*callback) (ds, vl);
363
364                 le = le->next;
365         }
366
367         return (0);
368 }
369
370 void plugin_complain (int level, complain_t *c, const char *format, ...)
371 {
372         char message[512];
373         va_list ap;
374         int step;
375
376         if (c->delay > 0)
377         {
378                 c->delay--;
379                 return;
380         }
381
382         step = atoi (COLLECTD_STEP);
383         assert (step > 0);
384
385         if (c->interval < step)
386                 c->interval = step;
387         else
388                 c->interval *= 2;
389
390         if (c->interval > 86400)
391                 c->interval = 86400;
392
393         c->delay = c->interval / step;
394
395         va_start (ap, format);
396         vsnprintf (message, 512, format, ap);
397         message[511] = '\0';
398         va_end (ap);
399
400         syslog (level, message);
401 }
402
403 void plugin_relief (int level, complain_t *c, const char *format, ...)
404 {
405         char message[512];
406         va_list ap;
407
408         if (c->interval == 0)
409                 return;
410
411         c->interval = 0;
412
413         va_start (ap, format);
414         vsnprintf (message, 512, format, ap);
415         message[511] = '\0';
416         va_end (ap);
417
418         syslog (level, message);
419 }