c927522afe14f8940cdc08a1ec1a7015a8f7897e
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] = (pthread_t) 0;
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 llentry_t *e;
442 e = llist_search (list_read, name);
444 if (e == NULL)
445 return (-1);
447 llist_remove (list_read, e);
448 free (e->value);
449 llentry_destroy (e);
451 return (0);
452 }
454 int plugin_unregister_write (const char *name)
455 {
456 return (plugin_unregister (list_write, name));
457 }
459 int plugin_unregister_shutdown (const char *name)
460 {
461 return (plugin_unregister (list_shutdown, name));
462 }
464 int plugin_unregister_data_set (const char *name)
465 {
466 return (plugin_unregister (list_data_set, name));
467 }
469 int plugin_unregister_log (const char *name)
470 {
471 return (plugin_unregister (list_log, name));
472 }
474 void plugin_init_all (void)
475 {
476 int (*callback) (void);
477 llentry_t *le;
478 int status;
480 /* Start read-threads */
481 if (list_read != NULL)
482 {
483 const char *rt;
484 int num;
485 rt = global_option_get ("ReadThreads");
486 num = atoi (rt);
487 start_threads ((num > 0) ? num : 5);
488 }
490 if (list_init == NULL)
491 return;
493 le = llist_head (list_init);
494 while (le != NULL)
495 {
496 callback = (int (*) (void)) le->value;
497 status = (*callback) ();
499 if (status != 0)
500 {
501 ERROR ("Initialization of plugin `%s' "
502 "failed with status %i. "
503 "Plugin will be unloaded. TODO!",
504 le->key, status);
505 plugin_unregister_read (le->key);
506 }
508 le = le->next;
509 }
510 } /* void plugin_init_all */
512 void plugin_read_all (const int *loop)
513 {
514 llentry_t *le;
515 read_func_t *rf;
517 if (list_read == NULL)
518 return;
520 pthread_mutex_lock (&read_lock);
522 le = llist_head (list_read);
523 while (le != NULL)
524 {
525 rf = (read_func_t *) le->value;
527 if (rf->needs_read != DONE)
528 {
529 le = le->next;
530 continue;
531 }
533 if (rf->wait_left > 0)
534 rf->wait_left -= interval_g;
536 if (rf->wait_left <= 0)
537 {
538 rf->needs_read = TODO;
539 }
541 le = le->next;
542 }
544 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
545 pthread_cond_broadcast (&read_cond);
546 pthread_mutex_unlock (&read_lock);
547 } /* void plugin_read_all */
549 void plugin_shutdown_all (void)
550 {
551 int (*callback) (void);
552 llentry_t *le;
554 stop_threads ();
556 if (list_shutdown == NULL)
557 return;
559 le = llist_head (list_shutdown);
560 while (le != NULL)
561 {
562 callback = (int (*) (void)) le->value;
563 (*callback) ();
565 le = le->next;
566 }
567 } /* void plugin_shutdown_all */
569 int plugin_dispatch_values (const char *name, const value_list_t *vl)
570 {
571 int (*callback) (const data_set_t *, const value_list_t *);
572 data_set_t *ds;
573 llentry_t *le;
575 if (list_write == NULL)
576 return (-1);
578 le = llist_search (list_data_set, name);
579 if (le == NULL)
580 {
581 DEBUG ("No such dataset registered: %s", name);
582 return (-1);
583 }
585 ds = (data_set_t *) le->value;
587 DEBUG ("plugin: plugin_dispatch_values: time = %u; host = %s; "
588 "plugin = %s; plugin_instance = %s; type = %s; "
589 "type_instance = %s;",
590 (unsigned int) vl->time, vl->host,
591 vl->plugin, vl->plugin_instance,
592 ds->type, vl->type_instance);
594 le = llist_head (list_write);
595 while (le != NULL)
596 {
597 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
598 (*callback) (ds, vl);
600 le = le->next;
601 }
603 return (0);
604 }
606 void plugin_log (int level, const char *format, ...)
607 {
608 char msg[512];
609 va_list ap;
611 void (*callback) (int, const char *);
612 llentry_t *le;
614 if (list_log == NULL)
615 return;
617 #if !COLLECT_DEBUG
618 if (level >= LOG_DEBUG)
619 return;
620 #endif
622 va_start (ap, format);
623 vsnprintf (msg, 512, format, ap);
624 msg[511] = '\0';
625 va_end (ap);
627 le = llist_head (list_log);
628 while (le != NULL)
629 {
630 callback = (void (*) (int, const char *)) le->value;
631 (*callback) (level, msg);
633 le = le->next;
634 }
635 } /* void plugin_log */
637 void plugin_complain (int level, complain_t *c, const char *format, ...)
638 {
639 char message[512];
640 va_list ap;
642 if (c->delay > 0)
643 {
644 c->delay--;
645 return;
646 }
648 if (c->interval < interval_g)
649 c->interval = interval_g;
650 else
651 c->interval *= 2;
653 if (c->interval > 86400)
654 c->interval = 86400;
656 c->delay = c->interval / interval_g;
658 va_start (ap, format);
659 vsnprintf (message, 512, format, ap);
660 message[511] = '\0';
661 va_end (ap);
663 plugin_log (level, message);
664 }
666 void plugin_relief (int level, complain_t *c, const char *format, ...)
667 {
668 char message[512];
669 va_list ap;
671 if (c->interval == 0)
672 return;
674 c->interval = 0;
676 va_start (ap, format);
677 vsnprintf (message, 512, format, ap);
678 message[511] = '\0';
679 va_end (ap);
681 plugin_log (level, message);
682 }
684 const data_set_t *plugin_get_ds (const char *name)
685 {
686 data_set_t *ds;
687 llentry_t *le;
689 le = llist_search (list_data_set, name);
690 if (le == NULL)
691 {
692 DEBUG ("No such dataset registered: %s", name);
693 return (NULL);
694 }
696 ds = (data_set_t *) le->value;
698 return (ds);
699 } /* data_set_t *plugin_get_ds */