Merge branch 'pull/collectd-4'
[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
321         gethostname (hostname, sizeof (hostname));
322
323         if (list_init == NULL)
324                 return;
325
326         le = llist_head (list_init);
327         while (le != NULL)
328         {
329                 callback = le->value;
330                 (*callback) ();
331
332                 le = le->next;
333         }
334 } /* void plugin_init_all */
335
336 void plugin_read_all (const int *loop)
337 {
338         llentry_t   *le;
339         read_func_t *rf;
340         int          status;
341         int          step;
342
343         if (list_read == NULL)
344                 return;
345
346         step = atoi (COLLECTD_STEP);
347
348         le = llist_head (list_read);
349         while ((*loop == 0) && (le != NULL))
350         {
351                 rf = (read_func_t *) le->value;
352
353                 if (rf->wait_left > 0)
354                         rf->wait_left -= step;
355                 if (rf->wait_left > 0)
356                 {
357                         le = le->next;
358                         continue;
359                 }
360
361                 status = rf->callback ();
362                 if (status != 0)
363                 {
364                         rf->wait_left = rf->wait_time;
365                         rf->wait_time = rf->wait_time * 2;
366                         if (rf->wait_time > 86400)
367                                 rf->wait_time = 86400;
368
369                         syslog (LOG_NOTICE, "read-function of plugin `%s' "
370                                         "failed. Will syspend it for %i "
371                                         "seconds.", le->key, rf->wait_left);
372                 }
373                 else
374                 {
375                         rf->wait_left = 0;
376                         rf->wait_time = step;
377                 }
378
379                 le = le->next;
380         } /* while ((*loop == 0) && (le != NULL)) */
381 } /* void plugin_read_all */
382
383 void plugin_shutdown_all (void)
384 {
385         int (*callback) (void);
386         llentry_t *le;
387
388         if (list_shutdown == NULL)
389                 return;
390
391         le = llist_head (list_shutdown);
392         while (le != NULL)
393         {
394                 callback = le->value;
395                 (*callback) ();
396
397                 le = le->next;
398         }
399 } /* void plugin_shutdown_all */
400
401 int plugin_dispatch_values (const char *name, const value_list_t *vl)
402 {
403         int (*callback) (const data_set_t *, const value_list_t *);
404         data_set_t *ds;
405         llentry_t *le;
406
407         if (list_write == NULL)
408                 return (-1);
409
410         le = llist_search (list_data_set, name);
411         if (le == NULL)
412         {
413                 DBG ("No such dataset registered: %s", name);
414                 return (-1);
415         }
416
417         ds = (data_set_t *) le->value;
418
419         DBG ("time = %u; host = %s; "
420                         "plugin = %s; plugin_instance = %s; "
421                         "type = %s; type_instance = %s;",
422                         (unsigned int) vl->time, vl->host,
423                         vl->plugin, vl->plugin_instance,
424                         ds->type, vl->type_instance);
425
426         le = llist_head (list_write);
427         while (le != NULL)
428         {
429                 callback = le->value;
430                 (*callback) (ds, vl);
431
432                 le = le->next;
433         }
434
435         return (0);
436 }
437
438 void plugin_complain (int level, complain_t *c, const char *format, ...)
439 {
440         char message[512];
441         va_list ap;
442         int step;
443
444         if (c->delay > 0)
445         {
446                 c->delay--;
447                 return;
448         }
449
450         step = atoi (COLLECTD_STEP);
451         assert (step > 0);
452
453         if (c->interval < step)
454                 c->interval = step;
455         else
456                 c->interval *= 2;
457
458         if (c->interval > 86400)
459                 c->interval = 86400;
460
461         c->delay = c->interval / step;
462
463         va_start (ap, format);
464         vsnprintf (message, 512, format, ap);
465         message[511] = '\0';
466         va_end (ap);
467
468         syslog (level, message);
469 }
470
471 void plugin_relief (int level, complain_t *c, const char *format, ...)
472 {
473         char message[512];
474         va_list ap;
475
476         if (c->interval == 0)
477                 return;
478
479         c->interval = 0;
480
481         va_start (ap, format);
482         vsnprintf (message, 512, format, ap);
483         message[511] = '\0';
484         va_end (ap);
485
486         syslog (level, message);
487 }