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