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"
34 #include "utils_cache.h"
35 #include "utils_threshold.h"
37 /*
38 * Private structures
39 */
40 struct read_func_s
41 {
42 int wait_time;
43 int wait_left;
44 int (*callback) (void);
45 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
46 };
47 typedef struct read_func_s read_func_t;
49 /*
50 * Private variables
51 */
52 static llist_t *list_init;
53 static llist_t *list_read;
54 static llist_t *list_write;
55 static llist_t *list_shutdown;
56 static llist_t *list_data_set;
57 static llist_t *list_log;
58 static llist_t *list_notification;
60 static char *plugindir = NULL;
62 static int read_loop = 1;
63 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
64 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
65 static pthread_t *read_threads = NULL;
66 static int read_threads_num = 0;
68 /*
69 * Static functions
70 */
71 static const char *plugin_get_dir (void)
72 {
73 if (plugindir == NULL)
74 return (PLUGINDIR);
75 else
76 return (plugindir);
77 }
79 static int register_callback (llist_t **list, const char *name, void *callback)
80 {
81 llentry_t *le;
82 char *key;
84 if ((*list == NULL)
85 && ((*list = llist_create ()) == NULL))
86 return (-1);
88 le = llist_search (*list, name);
89 if (le == NULL)
90 {
91 key = strdup (name);
92 if (key == NULL)
93 return (-1);
95 le = llentry_create (key, callback);
96 if (le == NULL)
97 {
98 free (key);
99 return (-1);
100 }
102 llist_append (*list, le);
103 }
104 else
105 {
106 le->value = callback;
107 }
109 return (0);
110 } /* int register_callback */
112 static int plugin_unregister (llist_t *list, const char *name)
113 {
114 llentry_t *e;
116 e = llist_search (list, name);
118 if (e == NULL)
119 return (-1);
121 llist_remove (list, e);
122 free (e->key);
123 llentry_destroy (e);
125 return (0);
126 } /* int plugin_unregister */
128 /*
129 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
130 * object, but it will bitch about a shared object not having a
131 * ``module_register'' symbol..
132 */
133 static int plugin_load_file (char *file)
134 {
135 lt_dlhandle dlh;
136 void (*reg_handle) (void);
138 DEBUG ("file = %s", file);
140 lt_dlinit ();
141 lt_dlerror (); /* clear errors */
143 if ((dlh = lt_dlopen (file)) == NULL)
144 {
145 const char *error = lt_dlerror ();
147 ERROR ("lt_dlopen failed: %s", error);
148 fprintf (stderr, "lt_dlopen failed: %s\n", error);
149 return (1);
150 }
152 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
153 {
154 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
155 file, lt_dlerror ());
156 lt_dlclose (dlh);
157 return (-1);
158 }
160 (*reg_handle) ();
162 return (0);
163 }
165 static void *plugin_read_thread (void *args)
166 {
167 llentry_t *le;
168 read_func_t *rf;
169 int status;
170 int done;
172 pthread_mutex_lock (&read_lock);
174 while (read_loop != 0)
175 {
176 le = llist_head (list_read);
177 done = 0;
179 while ((read_loop != 0) && (le != NULL))
180 {
181 rf = (read_func_t *) le->value;
183 if (rf->needs_read != TODO)
184 {
185 le = le->next;
186 continue;
187 }
189 /* We will do this read function */
190 rf->needs_read = ACTIVE;
192 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
193 (unsigned long int) pthread_self (), le->key);
194 pthread_mutex_unlock (&read_lock);
196 status = rf->callback ();
197 done++;
199 if (status != 0)
200 {
201 if (rf->wait_time < interval_g)
202 rf->wait_time = interval_g;
203 rf->wait_left = rf->wait_time;
204 rf->wait_time = rf->wait_time * 2;
205 if (rf->wait_time > 86400)
206 rf->wait_time = 86400;
208 NOTICE ("read-function of plugin `%s' "
209 "failed. Will suspend it for %i "
210 "seconds.", le->key, rf->wait_left);
211 }
212 else
213 {
214 rf->wait_left = 0;
215 rf->wait_time = interval_g;
216 }
218 pthread_mutex_lock (&read_lock);
220 rf->needs_read = DONE;
221 le = le->next;
222 } /* while (le != NULL) */
224 if ((read_loop != 0) && (done == 0))
225 {
226 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
227 (unsigned long int) pthread_self ());
228 pthread_cond_wait (&read_cond, &read_lock);
229 }
230 } /* while (read_loop) */
232 pthread_mutex_unlock (&read_lock);
234 pthread_exit (NULL);
235 } /* void *plugin_read_thread */
237 static void start_threads (int num)
238 {
239 int i;
241 if (read_threads != NULL)
242 return;
244 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
245 if (read_threads == NULL)
246 {
247 ERROR ("plugin: start_threads: calloc failed.");
248 return;
249 }
251 read_threads_num = 0;
252 for (i = 0; i < num; i++)
253 {
254 if (pthread_create (read_threads + read_threads_num, NULL,
255 plugin_read_thread, NULL) == 0)
256 {
257 read_threads_num++;
258 }
259 else
260 {
261 ERROR ("plugin: start_threads: pthread_create failed.");
262 return;
263 }
264 } /* for (i) */
265 } /* void start_threads */
267 static void stop_threads (void)
268 {
269 int i;
271 pthread_mutex_lock (&read_lock);
272 read_loop = 0;
273 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
274 pthread_cond_broadcast (&read_cond);
275 pthread_mutex_unlock (&read_lock);
277 for (i = 0; i < read_threads_num; i++)
278 {
279 if (pthread_join (read_threads[i], NULL) != 0)
280 {
281 ERROR ("plugin: stop_threads: pthread_join failed.");
282 }
283 read_threads[i] = (pthread_t) 0;
284 }
285 sfree (read_threads);
286 read_threads_num = 0;
287 } /* void stop_threads */
289 /*
290 * Public functions
291 */
292 void plugin_set_dir (const char *dir)
293 {
294 if (plugindir != NULL)
295 free (plugindir);
297 if (dir == NULL)
298 plugindir = NULL;
299 else if ((plugindir = strdup (dir)) == NULL)
300 {
301 char errbuf[1024];
302 ERROR ("strdup failed: %s",
303 sstrerror (errno, errbuf, sizeof (errbuf)));
304 }
305 }
307 #define BUFSIZE 512
308 int plugin_load (const char *type)
309 {
310 DIR *dh;
311 const char *dir;
312 char filename[BUFSIZE];
313 char typename[BUFSIZE];
314 int typename_len;
315 int ret;
316 struct stat statbuf;
317 struct dirent *de;
319 DEBUG ("type = %s", type);
321 dir = plugin_get_dir ();
322 ret = 1;
324 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
325 * type when matching the filename */
326 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
327 {
328 WARNING ("snprintf: truncated: `%s.so'", type);
329 return (-1);
330 }
331 typename_len = strlen (typename);
333 if ((dh = opendir (dir)) == NULL)
334 {
335 char errbuf[1024];
336 ERROR ("opendir (%s): %s", dir,
337 sstrerror (errno, errbuf, sizeof (errbuf)));
338 return (-1);
339 }
341 while ((de = readdir (dh)) != NULL)
342 {
343 if (strncasecmp (de->d_name, typename, typename_len))
344 continue;
346 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
347 {
348 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
349 continue;
350 }
352 if (lstat (filename, &statbuf) == -1)
353 {
354 char errbuf[1024];
355 WARNING ("stat %s: %s", filename,
356 sstrerror (errno, errbuf, sizeof (errbuf)));
357 continue;
358 }
359 else if (!S_ISREG (statbuf.st_mode))
360 {
361 /* don't follow symlinks */
362 continue;
363 }
365 if (plugin_load_file (filename) == 0)
366 {
367 /* success */
368 ret = 0;
369 break;
370 }
371 else
372 {
373 fprintf (stderr, "Unable to load plugin %s.\n", type);
374 }
375 }
377 closedir (dh);
379 return (ret);
380 }
382 /*
383 * The `register_*' functions follow
384 */
385 int plugin_register_config (const char *name,
386 int (*callback) (const char *key, const char *val),
387 const char **keys, int keys_num)
388 {
389 cf_register (name, callback, keys, keys_num);
390 return (0);
391 } /* int plugin_register_config */
393 int plugin_register_complex_config (const char *type,
394 int (*callback) (oconfig_item_t *))
395 {
396 return (cf_register_complex (type, callback));
397 } /* int plugin_register_complex_config */
399 int plugin_register_init (const char *name,
400 int (*callback) (void))
401 {
402 return (register_callback (&list_init, name, (void *) callback));
403 } /* plugin_register_init */
405 int plugin_register_read (const char *name,
406 int (*callback) (void))
407 {
408 read_func_t *rf;
410 rf = (read_func_t *) malloc (sizeof (read_func_t));
411 if (rf == NULL)
412 {
413 char errbuf[1024];
414 ERROR ("plugin_register_read: malloc failed: %s",
415 sstrerror (errno, errbuf, sizeof (errbuf)));
416 return (-1);
417 }
419 memset (rf, '\0', sizeof (read_func_t));
420 rf->wait_time = interval_g;
421 rf->wait_left = 0;
422 rf->callback = callback;
423 rf->needs_read = DONE;
425 return (register_callback (&list_read, name, (void *) rf));
426 } /* int plugin_register_read */
428 int plugin_register_write (const char *name,
429 int (*callback) (const data_set_t *ds, const value_list_t *vl))
430 {
431 return (register_callback (&list_write, name, (void *) callback));
432 } /* int plugin_register_write */
434 int plugin_register_shutdown (char *name,
435 int (*callback) (void))
436 {
437 return (register_callback (&list_shutdown, name, (void *) callback));
438 } /* int plugin_register_shutdown */
440 int plugin_register_data_set (const data_set_t *ds)
441 {
442 data_set_t *ds_copy;
443 int i;
445 if ((list_data_set != NULL)
446 && (llist_search (list_data_set, ds->type) != NULL))
447 {
448 NOTICE ("Replacing DS `%s' with another version.", ds->type);
449 plugin_unregister_data_set (ds->type);
450 }
452 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
453 if (ds_copy == NULL)
454 return (-1);
455 memcpy(ds_copy, ds, sizeof (data_set_t));
457 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
458 * ds->ds_num);
459 if (ds_copy->ds == NULL)
460 {
461 free (ds_copy);
462 return (-1);
463 }
465 for (i = 0; i < ds->ds_num; i++)
466 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
468 return (register_callback (&list_data_set, ds->type, (void *) ds_copy));
469 } /* int plugin_register_data_set */
471 int plugin_register_log (char *name,
472 void (*callback) (int priority, const char *msg))
473 {
474 return (register_callback (&list_log, name, (void *) callback));
475 } /* int plugin_register_log */
477 int plugin_register_notification (const char *name,
478 int (*callback) (const notification_t *notif))
479 {
480 return (register_callback (&list_notification, name, (void *) callback));
481 } /* int plugin_register_log */
483 int plugin_unregister_config (const char *name)
484 {
485 cf_unregister (name);
486 return (0);
487 } /* int plugin_unregister_config */
489 int plugin_unregister_complex_config (const char *name)
490 {
491 cf_unregister_complex (name);
492 return (0);
493 } /* int plugin_unregister_complex_config */
495 int plugin_unregister_init (const char *name)
496 {
497 return (plugin_unregister (list_init, name));
498 }
500 int plugin_unregister_read (const char *name)
501 {
502 llentry_t *e;
504 e = llist_search (list_read, name);
506 if (e == NULL)
507 return (-1);
509 llist_remove (list_read, e);
510 free (e->value);
511 free (e->key);
512 llentry_destroy (e);
514 return (0);
515 }
517 int plugin_unregister_write (const char *name)
518 {
519 return (plugin_unregister (list_write, name));
520 }
522 int plugin_unregister_shutdown (const char *name)
523 {
524 return (plugin_unregister (list_shutdown, name));
525 }
527 int plugin_unregister_data_set (const char *name)
528 {
529 llentry_t *e;
530 data_set_t *ds;
532 if (list_data_set == NULL)
533 return (-1);
535 e = llist_search (list_data_set, name);
537 if (e == NULL)
538 return (-1);
540 llist_remove (list_data_set, e);
541 ds = (data_set_t *) e->value;
542 free (e->key);
543 llentry_destroy (e);
545 sfree (ds->ds);
546 sfree (ds);
548 return (0);
549 } /* int plugin_unregister_data_set */
551 int plugin_unregister_log (const char *name)
552 {
553 return (plugin_unregister (list_log, name));
554 }
556 int plugin_unregister_notification (const char *name)
557 {
558 return (plugin_unregister (list_notification, name));
559 }
561 void plugin_init_all (void)
562 {
563 int (*callback) (void);
564 llentry_t *le;
565 int status;
567 /* Start read-threads */
568 if (list_read != NULL)
569 {
570 const char *rt;
571 int num;
572 rt = global_option_get ("ReadThreads");
573 num = atoi (rt);
574 start_threads ((num > 0) ? num : 5);
575 }
577 /* Init the value cache */
578 uc_init ();
580 if (list_init == NULL)
581 return;
583 le = llist_head (list_init);
584 while (le != NULL)
585 {
586 callback = (int (*) (void)) le->value;
587 status = (*callback) ();
589 if (status != 0)
590 {
591 ERROR ("Initialization of plugin `%s' "
592 "failed with status %i. "
593 "Plugin will be unloaded.",
594 le->key, status);
595 /* FIXME: Unload _all_ functions */
596 plugin_unregister_read (le->key);
597 }
599 le = le->next;
600 }
601 } /* void plugin_init_all */
603 void plugin_read_all (void)
604 {
605 llentry_t *le;
606 read_func_t *rf;
608 uc_check_timeout ();
610 if (list_read == NULL)
611 return;
613 pthread_mutex_lock (&read_lock);
615 le = llist_head (list_read);
616 while (le != NULL)
617 {
618 rf = (read_func_t *) le->value;
620 if (rf->needs_read != DONE)
621 {
622 le = le->next;
623 continue;
624 }
626 if (rf->wait_left > 0)
627 rf->wait_left -= interval_g;
629 if (rf->wait_left <= 0)
630 {
631 rf->needs_read = TODO;
632 }
634 le = le->next;
635 }
637 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
638 pthread_cond_broadcast (&read_cond);
639 pthread_mutex_unlock (&read_lock);
640 } /* void plugin_read_all */
642 void plugin_shutdown_all (void)
643 {
644 int (*callback) (void);
645 llentry_t *le;
647 stop_threads ();
649 if (list_shutdown == NULL)
650 return;
652 le = llist_head (list_shutdown);
653 while (le != NULL)
654 {
655 callback = (int (*) (void)) le->value;
657 /* Advance the pointer before calling the callback allows
658 * shutdown functions to unregister themselves. If done the
659 * other way around the memory `le' points to will be freed
660 * after callback returns. */
661 le = le->next;
663 (*callback) ();
664 }
665 } /* void plugin_shutdown_all */
667 int plugin_dispatch_values (const char *name, value_list_t *vl)
668 {
669 int (*callback) (const data_set_t *, const value_list_t *);
670 data_set_t *ds;
671 llentry_t *le;
673 if ((list_write == NULL) || (list_data_set == NULL))
674 return (-1);
676 le = llist_search (list_data_set, name);
677 if (le == NULL)
678 {
679 DEBUG ("No such dataset registered: %s", name);
680 return (-1);
681 }
683 ds = (data_set_t *) le->value;
685 DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
686 "host = %s; "
687 "plugin = %s; plugin_instance = %s; "
688 "type = %s; type_instance = %s;",
689 (unsigned int) vl->time, vl->interval,
690 vl->host,
691 vl->plugin, vl->plugin_instance,
692 ds->type, vl->type_instance);
694 #if COLLECT_DEBUG
695 assert (ds->ds_num == vl->values_len);
696 #else
697 if (ds->ds_num != vl->values_len)
698 {
699 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
700 "(vl->values_len = %i)",
701 ds->type, ds->ds_num, vl->values_len);
702 return (-1);
703 }
704 #endif
706 escape_slashes (vl->host, sizeof (vl->host));
707 escape_slashes (vl->plugin, sizeof (vl->plugin));
708 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
709 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
711 /* Update the value cache */
712 uc_update (ds, vl);
713 ut_check_threshold (ds, vl);
715 le = llist_head (list_write);
716 while (le != NULL)
717 {
718 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
719 (*callback) (ds, vl);
721 le = le->next;
722 }
724 return (0);
725 } /* int plugin_dispatch_values */
727 int plugin_dispatch_notification (const notification_t *notif)
728 {
729 int (*callback) (const notification_t *);
730 llentry_t *le;
731 /* Possible TODO: Add flap detection here */
733 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
734 "time = %u; host = %s;",
735 notif->severity, notif->message,
736 (unsigned int) notif->time, notif->host);
738 /* Nobody cares for notifications */
739 if (list_notification == NULL)
740 return (-1);
742 le = llist_head (list_notification);
743 while (le != NULL)
744 {
745 callback = (int (*) (const notification_t *)) le->value;
746 (*callback) (notif);
748 le = le->next;
749 }
751 return (0);
752 } /* int plugin_dispatch_notification */
754 void plugin_log (int level, const char *format, ...)
755 {
756 char msg[512];
757 va_list ap;
759 void (*callback) (int, const char *);
760 llentry_t *le;
762 if (list_log == NULL)
763 return;
765 #if !COLLECT_DEBUG
766 if (level >= LOG_DEBUG)
767 return;
768 #endif
770 va_start (ap, format);
771 vsnprintf (msg, 512, format, ap);
772 msg[511] = '\0';
773 va_end (ap);
775 le = llist_head (list_log);
776 while (le != NULL)
777 {
778 callback = (void (*) (int, const char *)) le->value;
779 (*callback) (level, msg);
781 le = le->next;
782 }
783 } /* void plugin_log */
785 const data_set_t *plugin_get_ds (const char *name)
786 {
787 data_set_t *ds;
788 llentry_t *le;
790 le = llist_search (list_data_set, name);
791 if (le == NULL)
792 {
793 DEBUG ("No such dataset registered: %s", name);
794 return (NULL);
795 }
797 ds = (data_set_t *) le->value;
799 return (ds);
800 } /* data_set_t *plugin_get_ds */