plugin interface: If an init-function fails, write an error to syslog and remove...
[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 structures
33  */
34 struct read_func_s
35 {
36         int wait_time;
37         int wait_left;
38         int (*callback) (void);
39 };
40 typedef struct read_func_s read_func_t;
41
42 /*
43  * Private variables
44  */
45 static llist_t *list_init;
46 static llist_t *list_read;
47 static llist_t *list_write;
48 static llist_t *list_shutdown;
49 static llist_t *list_data_set;
50
51 static char *plugindir = NULL;
52
53 char hostname[DATA_MAX_NAME_LEN] = "localhost";
54
55 /*
56  * Static functions
57  */
58 static const char *plugin_get_dir (void)
59 {
60         if (plugindir == NULL)
61                 return (PLUGINDIR);
62         else
63                 return (plugindir);
64 }
65
66 static int register_callback (llist_t **list, const char *name, void *callback)
67 {
68         llentry_t *le;
69
70         if ((*list == NULL)
71                         && ((*list = llist_create ()) == NULL))
72                 return (-1);
73
74         le = llist_search (*list, name);
75         if (le == NULL)
76         {
77                 le = llentry_create (name, callback);
78                 if (le == NULL)
79                         return (-1);
80
81                 llist_append (*list, le);
82         }
83         else
84         {
85                 le->value = callback;
86         }
87
88         return (0);
89 } /* int register_callback */
90
91 static int plugin_unregister (llist_t *list, const char *name)
92 {
93         llentry_t *e;
94
95         e = llist_search (list, name);
96
97         if (e == NULL)
98                 return (-1);
99
100         llist_remove (list, e);
101         llentry_destroy (e);
102
103         return (0);
104 } /* int plugin_unregister */
105
106 /*
107  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
108  * object, but it will bitch about a shared object not having a
109  * ``module_register'' symbol..
110  */
111 static int plugin_load_file (char *file)
112 {
113         lt_dlhandle dlh;
114         void (*reg_handle) (void);
115
116         DBG ("file = %s", file);
117
118         lt_dlinit ();
119         lt_dlerror (); /* clear errors */
120
121         if ((dlh = lt_dlopen (file)) == NULL)
122         {
123                 const char *error = lt_dlerror ();
124
125                 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
126                 DBG ("lt_dlopen failed: %s", error);
127                 return (1);
128         }
129
130         if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
131         {
132                 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
133                                 file, lt_dlerror ());
134                 lt_dlclose (dlh);
135                 return (-1);
136         }
137
138         (*reg_handle) ();
139
140         return (0);
141 }
142
143 /*
144  * Public functions
145  */
146 void plugin_set_dir (const char *dir)
147 {
148         if (plugindir != NULL)
149                 free (plugindir);
150
151         if (dir == NULL)
152                 plugindir = NULL;
153         else if ((plugindir = strdup (dir)) == NULL)
154                 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
155 }
156
157 #define BUFSIZE 512
158 int plugin_load (const char *type)
159 {
160         DIR  *dh;
161         const char *dir;
162         char  filename[BUFSIZE];
163         char  typename[BUFSIZE];
164         int   typename_len;
165         int   ret;
166         struct stat    statbuf;
167         struct dirent *de;
168
169         DBG ("type = %s", type);
170
171         dir = plugin_get_dir ();
172         ret = 1;
173
174         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
175          * type when matching the filename */
176         if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
177         {
178                 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
179                 return (-1);
180         }
181         typename_len = strlen (typename);
182
183         if ((dh = opendir (dir)) == NULL)
184         {
185                 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
186                 return (-1);
187         }
188
189         while ((de = readdir (dh)) != NULL)
190         {
191                 if (strncasecmp (de->d_name, typename, typename_len))
192                         continue;
193
194                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
195                 {
196                         syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
197                         continue;
198                 }
199
200                 if (lstat (filename, &statbuf) == -1)
201                 {
202                         syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
203                         continue;
204                 }
205                 else if (!S_ISREG (statbuf.st_mode))
206                 {
207                         /* don't follow symlinks */
208                         continue;
209                 }
210
211                 if (plugin_load_file (filename) == 0)
212                 {
213                         /* success */
214                         ret = 0;
215                         break;
216                 }
217         }
218
219         closedir (dh);
220
221         return (ret);
222 }
223
224 /*
225  * The `register_*' functions follow
226  */
227 int plugin_register_config (const char *name,
228                 int (*callback) (const char *key, const char *val),
229                 const char **keys, int keys_num)
230 {
231         cf_register (name, callback, keys, keys_num);
232         return (0);
233 } /* int plugin_register_config */
234
235 int plugin_register_init (const char *name,
236                 int (*callback) (void))
237 {
238         return (register_callback (&list_init, name, (void *) callback));
239 } /* plugin_register_init */
240
241 int plugin_register_read (const char *name,
242                 int (*callback) (void))
243 {
244         read_func_t *rf;
245
246         rf = (read_func_t *) malloc (sizeof (read_func_t));
247         if (rf == NULL)
248         {
249                 syslog (LOG_ERR, "plugin_register_read: malloc failed: %s",
250                                 strerror (errno));
251                 return (-1);
252         }
253
254         memset (rf, '\0', sizeof (read_func_t));
255         rf->wait_time = atoi (COLLECTD_STEP);
256         rf->wait_left = 0;
257         rf->callback = callback;
258
259         return (register_callback (&list_read, name, (void *) rf));
260 } /* int plugin_register_read */
261
262 int plugin_register_write (const char *name,
263                 int (*callback) (const data_set_t *ds, const value_list_t *vl))
264 {
265         return (register_callback (&list_write, name, (void *) callback));
266 } /* int plugin_register_write */
267
268 int plugin_register_shutdown (char *name,
269                 int (*callback) (void))
270 {
271         return (register_callback (&list_shutdown, name, (void *) callback));
272 } /* int plugin_register_shutdown */
273
274 int plugin_register_data_set (const data_set_t *ds)
275 {
276         return (register_callback (&list_data_set, ds->type, (void *) ds));
277 } /* int plugin_register_data_set */
278
279 int plugin_unregister_init (const char *name)
280 {
281         return (plugin_unregister (list_init, name));
282 }
283
284 int plugin_unregister_read (const char *name)
285 {
286         return (plugin_unregister (list_read, name));
287         llentry_t *e;
288
289         e = llist_search (list_read, name);
290
291         if (e == NULL)
292                 return (-1);
293
294         llist_remove (list_read, e);
295         free (e->value);
296         llentry_destroy (e);
297
298         return (0);
299 }
300
301 int plugin_unregister_write (const char *name)
302 {
303         return (plugin_unregister (list_write, name));
304 }
305
306 int plugin_unregister_shutdown (const char *name)
307 {
308         return (plugin_unregister (list_shutdown, name));
309 }
310
311 int plugin_unregister_data_set (const char *name)
312 {
313         return (plugin_unregister (list_data_set, name));
314 }
315
316 void plugin_init_all (void)
317 {
318         int (*callback) (void);
319         llentry_t *le;
320         int status;
321
322         gethostname (hostname, sizeof (hostname));
323
324         if (list_init == NULL)
325                 return;
326
327         le = llist_head (list_init);
328         while (le != NULL)
329         {
330                 callback = le->value;
331                 status = (*callback) ();
332
333                 if (status != 0)
334                 {
335                         syslog (LOG_ERR, "Initialization of plugin `%s' "
336                                         "failed with status %i. "
337                                         "Plugin will be unloaded. TODO!",
338                                         le->key, status);
339                         plugin_unregister_read (le->key);
340                 }
341
342                 le = le->next;
343         }
344 } /* void plugin_init_all */
345
346 void plugin_read_all (const int *loop)
347 {
348         llentry_t   *le;
349         read_func_t *rf;
350         int          status;
351         int          step;
352
353         if (list_read == NULL)
354                 return;
355
356         step = atoi (COLLECTD_STEP);
357
358         le = llist_head (list_read);
359         while ((*loop == 0) && (le != NULL))
360         {
361                 rf = (read_func_t *) le->value;
362
363                 if (rf->wait_left > 0)
364                         rf->wait_left -= step;
365                 if (rf->wait_left > 0)
366                 {
367                         le = le->next;
368                         continue;
369                 }
370
371                 status = rf->callback ();
372                 if (status != 0)
373                 {
374                         rf->wait_left = rf->wait_time;
375                         rf->wait_time = rf->wait_time * 2;
376                         if (rf->wait_time > 86400)
377                                 rf->wait_time = 86400;
378
379                         syslog (LOG_NOTICE, "read-function of plugin `%s' "
380                                         "failed. Will syspend it for %i "
381                                         "seconds.", le->key, rf->wait_left);
382                 }
383                 else
384                 {
385                         rf->wait_left = 0;
386                         rf->wait_time = step;
387                 }
388
389                 le = le->next;
390         } /* while ((*loop == 0) && (le != NULL)) */
391 } /* void plugin_read_all */
392
393 void plugin_shutdown_all (void)
394 {
395         int (*callback) (void);
396         llentry_t *le;
397
398         if (list_shutdown == NULL)
399                 return;
400
401         le = llist_head (list_shutdown);
402         while (le != NULL)
403         {
404                 callback = le->value;
405                 (*callback) ();
406
407                 le = le->next;
408         }
409 } /* void plugin_shutdown_all */
410
411 int plugin_dispatch_values (const char *name, const value_list_t *vl)
412 {
413         int (*callback) (const data_set_t *, const value_list_t *);
414         data_set_t *ds;
415         llentry_t *le;
416
417         if (list_write == NULL)
418                 return (-1);
419
420         le = llist_search (list_data_set, name);
421         if (le == NULL)
422         {
423                 DBG ("No such dataset registered: %s", name);
424                 return (-1);
425         }
426
427         ds = (data_set_t *) le->value;
428
429         DBG ("time = %u; host = %s; "
430                         "plugin = %s; plugin_instance = %s; "
431                         "type = %s; type_instance = %s;",
432                         (unsigned int) vl->time, vl->host,
433                         vl->plugin, vl->plugin_instance,
434                         ds->type, vl->type_instance);
435
436         le = llist_head (list_write);
437         while (le != NULL)
438         {
439                 callback = le->value;
440                 (*callback) (ds, vl);
441
442                 le = le->next;
443         }
444
445         return (0);
446 }
447
448 void plugin_complain (int level, complain_t *c, const char *format, ...)
449 {
450         char message[512];
451         va_list ap;
452         int step;
453
454         if (c->delay > 0)
455         {
456                 c->delay--;
457                 return;
458         }
459
460         step = atoi (COLLECTD_STEP);
461         assert (step > 0);
462
463         if (c->interval < step)
464                 c->interval = step;
465         else
466                 c->interval *= 2;
467
468         if (c->interval > 86400)
469                 c->interval = 86400;
470
471         c->delay = c->interval / step;
472
473         va_start (ap, format);
474         vsnprintf (message, 512, format, ap);
475         message[511] = '\0';
476         va_end (ap);
477
478         syslog (level, message);
479 }
480
481 void plugin_relief (int level, complain_t *c, const char *format, ...)
482 {
483         char message[512];
484         va_list ap;
485
486         if (c->interval == 0)
487                 return;
488
489         c->interval = 0;
490
491         va_start (ap, format);
492         vsnprintf (message, 512, format, ap);
493         message[511] = '\0';
494         va_end (ap);
495
496         syslog (level, message);
497 }