74a6e5ebb44f3571dde6a92f8661c8d565e5d8b7
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 **/
22 #include "collectd.h"
24 #include <ltdl.h>
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_llist.h"
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;
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;
52 static char *plugindir = NULL;
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 }
65 static int register_callback (llist_t **list, const char *name, void *callback)
66 {
67 llentry_t *le;
69 if ((*list == NULL)
70 && ((*list = llist_create ()) == NULL))
71 return (-1);
73 le = llist_search (*list, name);
74 if (le == NULL)
75 {
76 le = llentry_create (name, callback);
77 if (le == NULL)
78 return (-1);
80 llist_append (*list, le);
81 }
82 else
83 {
84 le->value = callback;
85 }
87 return (0);
88 } /* int register_callback */
90 static int plugin_unregister (llist_t *list, const char *name)
91 {
92 llentry_t *e;
94 e = llist_search (list, name);
96 if (e == NULL)
97 return (-1);
99 llist_remove (list, e);
100 llentry_destroy (e);
102 return (0);
103 } /* int plugin_unregister */
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);
115 DEBUG ("file = %s", file);
117 lt_dlinit ();
118 lt_dlerror (); /* clear errors */
120 if ((dlh = lt_dlopen (file)) == NULL)
121 {
122 const char *error = lt_dlerror ();
124 ERROR ("lt_dlopen failed: %s", error);
125 DEBUG ("lt_dlopen failed: %s", error);
126 return (1);
127 }
129 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
130 {
131 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
132 file, lt_dlerror ());
133 lt_dlclose (dlh);
134 return (-1);
135 }
137 (*reg_handle) ();
139 return (0);
140 }
142 /*
143 * Public functions
144 */
145 void plugin_set_dir (const char *dir)
146 {
147 if (plugindir != NULL)
148 free (plugindir);
150 if (dir == NULL)
151 plugindir = NULL;
152 else if ((plugindir = strdup (dir)) == NULL)
153 {
154 char errbuf[1024];
155 ERROR ("strdup failed: %s",
156 sstrerror (errno, errbuf, sizeof (errbuf)));
157 }
158 }
160 #define BUFSIZE 512
161 int plugin_load (const char *type)
162 {
163 DIR *dh;
164 const char *dir;
165 char filename[BUFSIZE];
166 char typename[BUFSIZE];
167 int typename_len;
168 int ret;
169 struct stat statbuf;
170 struct dirent *de;
172 DEBUG ("type = %s", type);
174 dir = plugin_get_dir ();
175 ret = 1;
177 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
178 * type when matching the filename */
179 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
180 {
181 WARNING ("snprintf: truncated: `%s.so'", type);
182 return (-1);
183 }
184 typename_len = strlen (typename);
186 if ((dh = opendir (dir)) == NULL)
187 {
188 char errbuf[1024];
189 ERROR ("opendir (%s): %s", dir,
190 sstrerror (errno, errbuf, sizeof (errbuf)));
191 return (-1);
192 }
194 while ((de = readdir (dh)) != NULL)
195 {
196 if (strncasecmp (de->d_name, typename, typename_len))
197 continue;
199 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
200 {
201 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
202 continue;
203 }
205 if (lstat (filename, &statbuf) == -1)
206 {
207 char errbuf[1024];
208 WARNING ("stat %s: %s", filename,
209 sstrerror (errno, errbuf, sizeof (errbuf)));
210 continue;
211 }
212 else if (!S_ISREG (statbuf.st_mode))
213 {
214 /* don't follow symlinks */
215 continue;
216 }
218 if (plugin_load_file (filename) == 0)
219 {
220 /* success */
221 ret = 0;
222 break;
223 }
224 }
226 closedir (dh);
228 return (ret);
229 }
231 /*
232 * The `register_*' functions follow
233 */
234 int plugin_register_config (const char *name,
235 int (*callback) (const char *key, const char *val),
236 const char **keys, int keys_num)
237 {
238 cf_register (name, callback, keys, keys_num);
239 return (0);
240 } /* int plugin_register_config */
242 int plugin_register_init (const char *name,
243 int (*callback) (void))
244 {
245 return (register_callback (&list_init, name, (void *) callback));
246 } /* plugin_register_init */
248 int plugin_register_read (const char *name,
249 int (*callback) (void))
250 {
251 read_func_t *rf;
253 rf = (read_func_t *) malloc (sizeof (read_func_t));
254 if (rf == NULL)
255 {
256 char errbuf[1024];
257 ERROR ("plugin_register_read: malloc failed: %s",
258 sstrerror (errno, errbuf, sizeof (errbuf)));
259 return (-1);
260 }
262 memset (rf, '\0', sizeof (read_func_t));
263 rf->wait_time = atoi (COLLECTD_STEP);
264 rf->wait_left = 0;
265 rf->callback = callback;
267 return (register_callback (&list_read, name, (void *) rf));
268 } /* int plugin_register_read */
270 int plugin_register_write (const char *name,
271 int (*callback) (const data_set_t *ds, const value_list_t *vl))
272 {
273 return (register_callback (&list_write, name, (void *) callback));
274 } /* int plugin_register_write */
276 int plugin_register_shutdown (char *name,
277 int (*callback) (void))
278 {
279 return (register_callback (&list_shutdown, name, (void *) callback));
280 } /* int plugin_register_shutdown */
282 int plugin_register_data_set (const data_set_t *ds)
283 {
284 return (register_callback (&list_data_set, ds->type, (void *) ds));
285 } /* int plugin_register_data_set */
287 int plugin_register_log (char *name,
288 void (*callback) (int priority, const char *msg))
289 {
290 return (register_callback (&list_log, name, (void *) callback));
291 } /* int plugin_register_log */
293 int plugin_unregister_init (const char *name)
294 {
295 return (plugin_unregister (list_init, name));
296 }
298 int plugin_unregister_read (const char *name)
299 {
300 return (plugin_unregister (list_read, name));
301 llentry_t *e;
303 e = llist_search (list_read, name);
305 if (e == NULL)
306 return (-1);
308 llist_remove (list_read, e);
309 free (e->value);
310 llentry_destroy (e);
312 return (0);
313 }
315 int plugin_unregister_write (const char *name)
316 {
317 return (plugin_unregister (list_write, name));
318 }
320 int plugin_unregister_shutdown (const char *name)
321 {
322 return (plugin_unregister (list_shutdown, name));
323 }
325 int plugin_unregister_data_set (const char *name)
326 {
327 return (plugin_unregister (list_data_set, name));
328 }
330 int plugin_unregister_log (const char *name)
331 {
332 return (plugin_unregister (list_log, name));
333 }
335 void plugin_init_all (void)
336 {
337 int (*callback) (void);
338 llentry_t *le;
339 int status;
341 if (list_init == NULL)
342 return;
344 le = llist_head (list_init);
345 while (le != NULL)
346 {
347 callback = le->value;
348 status = (*callback) ();
350 if (status != 0)
351 {
352 ERROR ("Initialization of plugin `%s' "
353 "failed with status %i. "
354 "Plugin will be unloaded. TODO!",
355 le->key, status);
356 plugin_unregister_read (le->key);
357 }
359 le = le->next;
360 }
361 } /* void plugin_init_all */
363 void plugin_read_all (const int *loop)
364 {
365 llentry_t *le;
366 read_func_t *rf;
367 int status;
368 int step;
370 if (list_read == NULL)
371 return;
373 step = atoi (COLLECTD_STEP);
375 le = llist_head (list_read);
376 while ((*loop == 0) && (le != NULL))
377 {
378 rf = (read_func_t *) le->value;
380 if (rf->wait_left > 0)
381 rf->wait_left -= step;
382 if (rf->wait_left > 0)
383 {
384 le = le->next;
385 continue;
386 }
388 status = rf->callback ();
389 if (status != 0)
390 {
391 rf->wait_left = rf->wait_time;
392 rf->wait_time = rf->wait_time * 2;
393 if (rf->wait_time > 86400)
394 rf->wait_time = 86400;
396 NOTICE ("read-function of plugin `%s' "
397 "failed. Will syspend it for %i "
398 "seconds.", le->key, rf->wait_left);
399 }
400 else
401 {
402 rf->wait_left = 0;
403 rf->wait_time = step;
404 }
406 le = le->next;
407 } /* while ((*loop == 0) && (le != NULL)) */
408 } /* void plugin_read_all */
410 void plugin_shutdown_all (void)
411 {
412 int (*callback) (void);
413 llentry_t *le;
415 if (list_shutdown == NULL)
416 return;
418 le = llist_head (list_shutdown);
419 while (le != NULL)
420 {
421 callback = le->value;
422 (*callback) ();
424 le = le->next;
425 }
426 } /* void plugin_shutdown_all */
428 int plugin_dispatch_values (const char *name, const value_list_t *vl)
429 {
430 int (*callback) (const data_set_t *, const value_list_t *);
431 data_set_t *ds;
432 llentry_t *le;
434 if (list_write == NULL)
435 return (-1);
437 le = llist_search (list_data_set, name);
438 if (le == NULL)
439 {
440 DEBUG ("No such dataset registered: %s", name);
441 return (-1);
442 }
444 ds = (data_set_t *) le->value;
446 DEBUG ("time = %u; host = %s; "
447 "plugin = %s; plugin_instance = %s; "
448 "type = %s; type_instance = %s;",
449 (unsigned int) vl->time, vl->host,
450 vl->plugin, vl->plugin_instance,
451 ds->type, vl->type_instance);
453 le = llist_head (list_write);
454 while (le != NULL)
455 {
456 callback = le->value;
457 (*callback) (ds, vl);
459 le = le->next;
460 }
462 return (0);
463 }
465 void plugin_log (int level, const char *format, ...)
466 {
467 char msg[512];
468 va_list ap;
470 void (*callback) (int, const char *);
471 llentry_t *le;
473 if (list_log == NULL)
474 return;
476 #if !COLLECT_DEBUG
477 if (level >= LOG_DEBUG)
478 return;
479 #endif
481 va_start (ap, format);
482 vsnprintf (msg, 512, format, ap);
483 msg[511] = '\0';
484 va_end (ap);
486 le = llist_head (list_log);
487 while (le != NULL)
488 {
489 callback = le->value;
490 (*callback) (level, msg);
492 le = le->next;
493 }
494 } /* void plugin_log */
496 void plugin_complain (int level, complain_t *c, const char *format, ...)
497 {
498 char message[512];
499 va_list ap;
500 int step;
502 if (c->delay > 0)
503 {
504 c->delay--;
505 return;
506 }
508 step = atoi (COLLECTD_STEP);
509 assert (step > 0);
511 if (c->interval < step)
512 c->interval = step;
513 else
514 c->interval *= 2;
516 if (c->interval > 86400)
517 c->interval = 86400;
519 c->delay = c->interval / step;
521 va_start (ap, format);
522 vsnprintf (message, 512, format, ap);
523 message[511] = '\0';
524 va_end (ap);
526 plugin_log (level, message);
527 }
529 void plugin_relief (int level, complain_t *c, const char *format, ...)
530 {
531 char message[512];
532 va_list ap;
534 if (c->interval == 0)
535 return;
537 c->interval = 0;
539 va_start (ap, format);
540 vsnprintf (message, 512, format, ap);
541 message[511] = '\0';
542 va_end (ap);
544 plugin_log (level, message);
545 }
547 const data_set_t *plugin_get_ds (const char *name)
548 {
549 data_set_t *ds;
550 llentry_t *le;
552 le = llist_search (list_data_set, name);
553 if (le == NULL)
554 {
555 DEBUG ("No such dataset registered: %s", name);
556 return (NULL);
557 }
559 ds = (data_set_t *) le->value;
561 return (ds);
562 } /* data_set_t *plugin_get_ds */