c6762dec7103402e2c31b903642ea112c8dde09b
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 #if HAVE_PTHREAD_H
27 # include <pthread.h>
28 #endif
30 #include "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
33 #include "utils_llist.h"
35 /*
36 * Private structures
37 */
38 struct read_func_s
39 {
40 int wait_time;
41 int wait_left;
42 int (*callback) (void);
43 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
44 };
45 typedef struct read_func_s read_func_t;
47 /*
48 * Private variables
49 */
50 static llist_t *list_init;
51 static llist_t *list_read;
52 static llist_t *list_write;
53 static llist_t *list_shutdown;
54 static llist_t *list_data_set;
55 static llist_t *list_log;
57 static char *plugindir = NULL;
59 static int read_loop = 1;
60 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
61 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
62 static pthread_t *read_threads = NULL;
63 static int read_threads_num = 0;
65 /*
66 * Static functions
67 */
68 static const char *plugin_get_dir (void)
69 {
70 if (plugindir == NULL)
71 return (PLUGINDIR);
72 else
73 return (plugindir);
74 }
76 static int register_callback (llist_t **list, const char *name, void *callback)
77 {
78 llentry_t *le;
80 if ((*list == NULL)
81 && ((*list = llist_create ()) == NULL))
82 return (-1);
84 le = llist_search (*list, name);
85 if (le == NULL)
86 {
87 le = llentry_create (name, callback);
88 if (le == NULL)
89 return (-1);
91 llist_append (*list, le);
92 }
93 else
94 {
95 le->value = callback;
96 }
98 return (0);
99 } /* int register_callback */
101 static int plugin_unregister (llist_t *list, const char *name)
102 {
103 llentry_t *e;
105 e = llist_search (list, name);
107 if (e == NULL)
108 return (-1);
110 llist_remove (list, e);
111 llentry_destroy (e);
113 return (0);
114 } /* int plugin_unregister */
116 /*
117 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
118 * object, but it will bitch about a shared object not having a
119 * ``module_register'' symbol..
120 */
121 static int plugin_load_file (char *file, modreg_e load)
122 {
123 lt_dlhandle dlh;
124 void (*reg_handle) (modreg_e mr);
126 DEBUG ("file = %s", file);
128 lt_dlinit ();
129 lt_dlerror (); /* clear errors */
131 if ((dlh = lt_dlopen (file)) == NULL)
132 {
133 const char *error = lt_dlerror ();
135 ERROR ("lt_dlopen failed: %s", error);
136 DEBUG ("lt_dlopen failed: %s", error);
137 return (1);
138 }
140 if ((reg_handle = (void (*) (modreg_e)) lt_dlsym (dlh, "module_register")) == NULL)
141 {
142 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
143 file, lt_dlerror ());
144 lt_dlclose (dlh);
145 return (-1);
146 }
148 (*reg_handle) (load);
150 return (0);
151 }
153 static void *plugin_read_thread (void *args)
154 {
155 llentry_t *le;
156 read_func_t *rf;
157 int status;
158 int done;
160 pthread_mutex_lock (&read_lock);
162 while (read_loop != 0)
163 {
164 le = llist_head (list_read);
165 done = 0;
167 while ((read_loop != 0) && (le != NULL))
168 {
169 rf = (read_func_t *) le->value;
171 if (rf->needs_read != TODO)
172 {
173 le = le->next;
174 continue;
175 }
177 /* We will do this read function */
178 rf->needs_read = ACTIVE;
180 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
181 (unsigned long int) pthread_self (), le->key);
182 pthread_mutex_unlock (&read_lock);
184 status = rf->callback ();
185 done++;
187 if (status != 0)
188 {
189 rf->wait_left = rf->wait_time;
190 rf->wait_time = rf->wait_time * 2;
191 if (rf->wait_time > 86400)
192 rf->wait_time = 86400;
194 NOTICE ("read-function of plugin `%s' "
195 "failed. Will syspend it for %i "
196 "seconds.", le->key, rf->wait_left);
197 }
198 else
199 {
200 rf->wait_left = 0;
201 rf->wait_time = interval_g;
202 }
204 pthread_mutex_lock (&read_lock);
206 rf->needs_read = DONE;
207 le = le->next;
208 } /* while (le != NULL) */
210 if ((read_loop != 0) && (done == 0))
211 {
212 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
213 (unsigned long int) pthread_self ());
214 pthread_cond_wait (&read_cond, &read_lock);
215 }
216 } /* while (read_loop) */
218 pthread_mutex_unlock (&read_lock);
220 pthread_exit (NULL);
221 } /* void *plugin_read_thread */
223 static void start_threads (int num)
224 {
225 int i;
227 if (read_threads != NULL)
228 return;
230 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
231 if (read_threads == NULL)
232 {
233 ERROR ("plugin: start_threads: calloc failed.");
234 return;
235 }
237 read_threads_num = 0;
238 for (i = 0; i < num; i++)
239 {
240 if (pthread_create (read_threads + read_threads_num, NULL,
241 plugin_read_thread, NULL) == 0)
242 {
243 read_threads_num++;
244 }
245 else
246 {
247 ERROR ("plugin: start_threads: pthread_create failed.");
248 return;
249 }
250 } /* for (i) */
251 } /* void start_threads */
253 static void stop_threads (void)
254 {
255 int i;
257 pthread_mutex_lock (&read_lock);
258 read_loop = 0;
259 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
260 pthread_cond_broadcast (&read_cond);
261 pthread_mutex_unlock (&read_lock);
263 for (i = 0; i < read_threads_num; i++)
264 {
265 if (pthread_join (read_threads[i], NULL) != 0)
266 {
267 ERROR ("plugin: stop_threads: pthread_join failed.");
268 }
269 read_threads[i] = -1;
270 }
271 sfree (read_threads);
272 read_threads_num = 0;
273 } /* void stop_threads */
275 /*
276 * Public functions
277 */
278 void plugin_set_dir (const char *dir)
279 {
280 if (plugindir != NULL)
281 free (plugindir);
283 if (dir == NULL)
284 plugindir = NULL;
285 else if ((plugindir = strdup (dir)) == NULL)
286 {
287 char errbuf[1024];
288 ERROR ("strdup failed: %s",
289 sstrerror (errno, errbuf, sizeof (errbuf)));
290 }
291 }
293 #define BUFSIZE 512
294 int plugin_load (const char *type, modreg_e mr)
295 {
296 DIR *dh;
297 const char *dir;
298 char filename[BUFSIZE];
299 char typename[BUFSIZE];
300 int typename_len;
301 int ret;
302 struct stat statbuf;
303 struct dirent *de;
305 DEBUG ("type = %s", type);
307 dir = plugin_get_dir ();
308 ret = 1;
310 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
311 * type when matching the filename */
312 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
313 {
314 WARNING ("snprintf: truncated: `%s.so'", type);
315 return (-1);
316 }
317 typename_len = strlen (typename);
319 if ((dh = opendir (dir)) == NULL)
320 {
321 char errbuf[1024];
322 ERROR ("opendir (%s): %s", dir,
323 sstrerror (errno, errbuf, sizeof (errbuf)));
324 return (-1);
325 }
327 while ((de = readdir (dh)) != NULL)
328 {
329 if (strncasecmp (de->d_name, typename, typename_len))
330 continue;
332 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
333 {
334 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
335 continue;
336 }
338 if (lstat (filename, &statbuf) == -1)
339 {
340 char errbuf[1024];
341 WARNING ("stat %s: %s", filename,
342 sstrerror (errno, errbuf, sizeof (errbuf)));
343 continue;
344 }
345 else if (!S_ISREG (statbuf.st_mode))
346 {
347 /* don't follow symlinks */
348 continue;
349 }
351 if (plugin_load_file (filename, mr) == 0)
352 {
353 /* success */
354 ret = 0;
355 break;
356 }
357 }
359 closedir (dh);
361 return (ret);
362 }
364 /*
365 * The `register_*' functions follow
366 */
367 int plugin_register_config (const char *name,
368 int (*callback) (const char *key, const char *val),
369 const char **keys, int keys_num)
370 {
371 cf_register (name, callback, keys, keys_num);
372 return (0);
373 } /* int plugin_register_config */
375 int plugin_register_init (const char *name,
376 int (*callback) (void))
377 {
378 return (register_callback (&list_init, name, (void *) callback));
379 } /* plugin_register_init */
381 int plugin_register_read (const char *name,
382 int (*callback) (void))
383 {
384 read_func_t *rf;
386 rf = (read_func_t *) malloc (sizeof (read_func_t));
387 if (rf == NULL)
388 {
389 char errbuf[1024];
390 ERROR ("plugin_register_read: malloc failed: %s",
391 sstrerror (errno, errbuf, sizeof (errbuf)));
392 return (-1);
393 }
395 memset (rf, '\0', sizeof (read_func_t));
396 rf->wait_time = interval_g;
397 rf->wait_left = 0;
398 rf->callback = callback;
399 rf->needs_read = DONE;
401 return (register_callback (&list_read, name, (void *) rf));
402 } /* int plugin_register_read */
404 int plugin_register_write (const char *name,
405 int (*callback) (const data_set_t *ds, const value_list_t *vl))
406 {
407 return (register_callback (&list_write, name, (void *) callback));
408 } /* int plugin_register_write */
410 int plugin_register_shutdown (char *name,
411 int (*callback) (void))
412 {
413 return (register_callback (&list_shutdown, name, (void *) callback));
414 } /* int plugin_register_shutdown */
416 int plugin_register_data_set (const data_set_t *ds)
417 {
418 return (register_callback (&list_data_set, ds->type, (void *) ds));
419 } /* int plugin_register_data_set */
421 int plugin_register_log (char *name,
422 void (*callback) (int priority, const char *msg))
423 {
424 return (register_callback (&list_log, name, (void *) callback));
425 } /* int plugin_register_log */
427 int plugin_unregister_config (const char *name)
428 {
429 cf_unregister (name);
430 return (0);
431 } /* int plugin_unregister_config */
433 int plugin_unregister_init (const char *name)
434 {
435 return (plugin_unregister (list_init, name));
436 }
438 int plugin_unregister_read (const char *name)
439 {
440 return (plugin_unregister (list_read, name));
441 llentry_t *e;
443 e = llist_search (list_read, name);
445 if (e == NULL)
446 return (-1);
448 llist_remove (list_read, e);
449 free (e->value);
450 llentry_destroy (e);
452 return (0);
453 }
455 int plugin_unregister_write (const char *name)
456 {
457 return (plugin_unregister (list_write, name));
458 }
460 int plugin_unregister_shutdown (const char *name)
461 {
462 return (plugin_unregister (list_shutdown, name));
463 }
465 int plugin_unregister_data_set (const char *name)
466 {
467 return (plugin_unregister (list_data_set, name));
468 }
470 int plugin_unregister_log (const char *name)
471 {
472 return (plugin_unregister (list_log, name));
473 }
475 void plugin_init_all (void)
476 {
477 int (*callback) (void);
478 llentry_t *le;
479 int status;
481 /* Start read-threads */
482 if (list_read != NULL)
483 {
484 const char *rt;
485 int num;
486 rt = global_option_get ("ReadThreads");
487 num = atoi (rt);
488 start_threads ((num > 0) ? num : 5);
489 }
491 if (list_init == NULL)
492 return;
494 le = llist_head (list_init);
495 while (le != NULL)
496 {
497 callback = le->value;
498 status = (*callback) ();
500 if (status != 0)
501 {
502 ERROR ("Initialization of plugin `%s' "
503 "failed with status %i. "
504 "Plugin will be unloaded. TODO!",
505 le->key, status);
506 plugin_unregister_read (le->key);
507 }
509 le = le->next;
510 }
511 } /* void plugin_init_all */
513 void plugin_read_all (const int *loop)
514 {
515 llentry_t *le;
516 read_func_t *rf;
518 if (list_read == NULL)
519 return;
521 pthread_mutex_lock (&read_lock);
523 le = llist_head (list_read);
524 while (le != NULL)
525 {
526 rf = (read_func_t *) le->value;
528 if (rf->needs_read != DONE)
529 continue;
531 if (rf->wait_left > 0)
532 rf->wait_left -= interval_g;
534 if (rf->wait_left <= 0)
535 {
536 rf->needs_read = TODO;
537 }
539 le = le->next;
540 }
542 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
543 pthread_cond_broadcast (&read_cond);
544 pthread_mutex_unlock (&read_lock);
545 } /* void plugin_read_all */
547 void plugin_shutdown_all (void)
548 {
549 int (*callback) (void);
550 llentry_t *le;
552 stop_threads ();
554 if (list_shutdown == NULL)
555 return;
557 le = llist_head (list_shutdown);
558 while (le != NULL)
559 {
560 callback = le->value;
561 (*callback) ();
563 le = le->next;
564 }
565 } /* void plugin_shutdown_all */
567 int plugin_dispatch_values (const char *name, const value_list_t *vl)
568 {
569 int (*callback) (const data_set_t *, const value_list_t *);
570 data_set_t *ds;
571 llentry_t *le;
573 if (list_write == NULL)
574 return (-1);
576 le = llist_search (list_data_set, name);
577 if (le == NULL)
578 {
579 DEBUG ("No such dataset registered: %s", name);
580 return (-1);
581 }
583 ds = (data_set_t *) le->value;
585 DEBUG ("plugin: plugin_dispatch_values: time = %u; host = %s; "
586 "plugin = %s; plugin_instance = %s; type = %s; "
587 "type_instance = %s;",
588 (unsigned int) vl->time, vl->host,
589 vl->plugin, vl->plugin_instance,
590 ds->type, vl->type_instance);
592 le = llist_head (list_write);
593 while (le != NULL)
594 {
595 callback = le->value;
596 (*callback) (ds, vl);
598 le = le->next;
599 }
601 return (0);
602 }
604 void plugin_log (int level, const char *format, ...)
605 {
606 char msg[512];
607 va_list ap;
609 void (*callback) (int, const char *);
610 llentry_t *le;
612 if (list_log == NULL)
613 return;
615 #if !COLLECT_DEBUG
616 if (level >= LOG_DEBUG)
617 return;
618 #endif
620 va_start (ap, format);
621 vsnprintf (msg, 512, format, ap);
622 msg[511] = '\0';
623 va_end (ap);
625 le = llist_head (list_log);
626 while (le != NULL)
627 {
628 callback = le->value;
629 (*callback) (level, msg);
631 le = le->next;
632 }
633 } /* void plugin_log */
635 void plugin_complain (int level, complain_t *c, const char *format, ...)
636 {
637 char message[512];
638 va_list ap;
640 if (c->delay > 0)
641 {
642 c->delay--;
643 return;
644 }
646 if (c->interval < interval_g)
647 c->interval = interval_g;
648 else
649 c->interval *= 2;
651 if (c->interval > 86400)
652 c->interval = 86400;
654 c->delay = c->interval / interval_g;
656 va_start (ap, format);
657 vsnprintf (message, 512, format, ap);
658 message[511] = '\0';
659 va_end (ap);
661 plugin_log (level, message);
662 }
664 void plugin_relief (int level, complain_t *c, const char *format, ...)
665 {
666 char message[512];
667 va_list ap;
669 if (c->interval == 0)
670 return;
672 c->interval = 0;
674 va_start (ap, format);
675 vsnprintf (message, 512, format, ap);
676 message[511] = '\0';
677 va_end (ap);
679 plugin_log (level, message);
680 }
682 const data_set_t *plugin_get_ds (const char *name)
683 {
684 data_set_t *ds;
685 llentry_t *le;
687 le = llist_search (list_data_set, name);
688 if (le == NULL)
689 {
690 DEBUG ("No such dataset registered: %s", name);
691 return (NULL);
692 }
694 ds = (data_set_t *) le->value;
696 return (ds);
697 } /* data_set_t *plugin_get_ds */