1 /**
2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2008 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 * Sebastian Harl <sh at tokkee.org>
21 **/
23 #include "collectd.h"
24 #include "utils_complain.h"
26 #include <ltdl.h>
28 #if HAVE_PTHREAD_H
29 # include <pthread.h>
30 #endif
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_cache.h"
38 #include "utils_threshold.h"
40 /*
41 * Private structures
42 */
43 struct read_func_s
44 {
45 int wait_time;
46 int wait_left;
47 int (*callback) (void);
48 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
49 };
50 typedef struct read_func_s read_func_t;
52 /*
53 * Private variables
54 */
55 static llist_t *list_init;
56 static llist_t *list_read;
57 static llist_t *list_write;
58 static llist_t *list_filter;
59 static llist_t *list_flush;
60 static llist_t *list_shutdown;
61 static llist_t *list_log;
62 static llist_t *list_notification;
64 static c_avl_tree_t *data_sets;
66 static char *plugindir = NULL;
68 static int read_loop = 1;
69 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
70 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
71 static pthread_t *read_threads = NULL;
72 static int read_threads_num = 0;
74 /*
75 * Static functions
76 */
77 static const char *plugin_get_dir (void)
78 {
79 if (plugindir == NULL)
80 return (PLUGINDIR);
81 else
82 return (plugindir);
83 }
85 static int register_callback (llist_t **list, const char *name, void *callback)
86 {
87 llentry_t *le;
88 char *key;
90 if ((*list == NULL)
91 && ((*list = llist_create ()) == NULL))
92 return (-1);
94 le = llist_search (*list, name);
95 if (le == NULL)
96 {
97 key = strdup (name);
98 if (key == NULL)
99 return (-1);
101 le = llentry_create (key, callback);
102 if (le == NULL)
103 {
104 free (key);
105 return (-1);
106 }
108 llist_append (*list, le);
109 }
110 else
111 {
112 le->value = callback;
113 }
115 return (0);
116 } /* int register_callback */
118 static int plugin_unregister (llist_t *list, const char *name)
119 {
120 llentry_t *e;
122 e = llist_search (list, name);
124 if (e == NULL)
125 return (-1);
127 llist_remove (list, e);
128 free (e->key);
129 llentry_destroy (e);
131 return (0);
132 } /* int plugin_unregister */
134 /*
135 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
136 * object, but it will bitch about a shared object not having a
137 * ``module_register'' symbol..
138 */
139 static int plugin_load_file (char *file)
140 {
141 lt_dlhandle dlh;
142 void (*reg_handle) (void);
144 DEBUG ("file = %s", file);
146 lt_dlinit ();
147 lt_dlerror (); /* clear errors */
149 if ((dlh = lt_dlopen (file)) == NULL)
150 {
151 const char *error = lt_dlerror ();
153 ERROR ("lt_dlopen (%s) failed: %s", file, error);
154 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
155 return (1);
156 }
158 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
159 {
160 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
161 file, lt_dlerror ());
162 lt_dlclose (dlh);
163 return (-1);
164 }
166 (*reg_handle) ();
168 return (0);
169 }
171 static void *plugin_read_thread (void *args)
172 {
173 llentry_t *le;
174 read_func_t *rf;
175 int status;
176 int done;
178 pthread_mutex_lock (&read_lock);
180 while (read_loop != 0)
181 {
182 le = llist_head (list_read);
183 done = 0;
185 while ((read_loop != 0) && (le != NULL))
186 {
187 rf = (read_func_t *) le->value;
189 if (rf->needs_read != TODO)
190 {
191 le = le->next;
192 continue;
193 }
195 /* We will do this read function */
196 rf->needs_read = ACTIVE;
198 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
199 (unsigned long int) pthread_self (), le->key);
200 pthread_mutex_unlock (&read_lock);
202 status = rf->callback ();
203 done++;
205 if (status != 0)
206 {
207 if (rf->wait_time < interval_g)
208 rf->wait_time = interval_g;
209 rf->wait_left = rf->wait_time;
210 rf->wait_time = rf->wait_time * 2;
211 if (rf->wait_time > 86400)
212 rf->wait_time = 86400;
214 NOTICE ("read-function of plugin `%s' "
215 "failed. Will suspend it for %i "
216 "seconds.", le->key, rf->wait_left);
217 }
218 else
219 {
220 rf->wait_left = 0;
221 rf->wait_time = interval_g;
222 }
224 pthread_mutex_lock (&read_lock);
226 rf->needs_read = DONE;
227 le = le->next;
228 } /* while (le != NULL) */
230 if ((read_loop != 0) && (done == 0))
231 {
232 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
233 (unsigned long int) pthread_self ());
234 pthread_cond_wait (&read_cond, &read_lock);
235 }
236 } /* while (read_loop) */
238 pthread_mutex_unlock (&read_lock);
240 pthread_exit (NULL);
241 return ((void *) 0);
242 } /* void *plugin_read_thread */
244 static void start_threads (int num)
245 {
246 int i;
248 if (read_threads != NULL)
249 return;
251 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
252 if (read_threads == NULL)
253 {
254 ERROR ("plugin: start_threads: calloc failed.");
255 return;
256 }
258 read_threads_num = 0;
259 for (i = 0; i < num; i++)
260 {
261 if (pthread_create (read_threads + read_threads_num, NULL,
262 plugin_read_thread, NULL) == 0)
263 {
264 read_threads_num++;
265 }
266 else
267 {
268 ERROR ("plugin: start_threads: pthread_create failed.");
269 return;
270 }
271 } /* for (i) */
272 } /* void start_threads */
274 static void stop_threads (void)
275 {
276 int i;
278 pthread_mutex_lock (&read_lock);
279 read_loop = 0;
280 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
281 pthread_cond_broadcast (&read_cond);
282 pthread_mutex_unlock (&read_lock);
284 for (i = 0; i < read_threads_num; i++)
285 {
286 if (pthread_join (read_threads[i], NULL) != 0)
287 {
288 ERROR ("plugin: stop_threads: pthread_join failed.");
289 }
290 read_threads[i] = (pthread_t) 0;
291 }
292 sfree (read_threads);
293 read_threads_num = 0;
294 } /* void stop_threads */
296 /*
297 * Public functions
298 */
299 void plugin_set_dir (const char *dir)
300 {
301 if (plugindir != NULL)
302 free (plugindir);
304 if (dir == NULL)
305 plugindir = NULL;
306 else if ((plugindir = strdup (dir)) == NULL)
307 {
308 char errbuf[1024];
309 ERROR ("strdup failed: %s",
310 sstrerror (errno, errbuf, sizeof (errbuf)));
311 }
312 }
314 #define BUFSIZE 512
315 int plugin_load (const char *type)
316 {
317 DIR *dh;
318 const char *dir;
319 char filename[BUFSIZE] = "";
320 char typename[BUFSIZE];
321 int typename_len;
322 int ret;
323 struct stat statbuf;
324 struct dirent *de;
326 DEBUG ("type = %s", type);
328 dir = plugin_get_dir ();
329 ret = 1;
331 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
332 * type when matching the filename */
333 if (ssnprintf (typename, sizeof (typename),
334 "%s.so", type) >= sizeof (typename))
335 {
336 WARNING ("snprintf: truncated: `%s.so'", type);
337 return (-1);
338 }
339 typename_len = strlen (typename);
341 if ((dh = opendir (dir)) == NULL)
342 {
343 char errbuf[1024];
344 ERROR ("opendir (%s): %s", dir,
345 sstrerror (errno, errbuf, sizeof (errbuf)));
346 return (-1);
347 }
349 while ((de = readdir (dh)) != NULL)
350 {
351 if (strncasecmp (de->d_name, typename, typename_len))
352 continue;
354 if (ssnprintf (filename, sizeof (filename),
355 "%s/%s", dir, de->d_name) >= sizeof (filename))
356 {
357 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
358 continue;
359 }
361 if (lstat (filename, &statbuf) == -1)
362 {
363 char errbuf[1024];
364 WARNING ("stat %s: %s", filename,
365 sstrerror (errno, errbuf, sizeof (errbuf)));
366 continue;
367 }
368 else if (!S_ISREG (statbuf.st_mode))
369 {
370 /* don't follow symlinks */
371 continue;
372 }
374 if (plugin_load_file (filename) == 0)
375 {
376 /* success */
377 ret = 0;
378 break;
379 }
380 else
381 {
382 fprintf (stderr, "Unable to load plugin %s.\n", type);
383 }
384 }
386 closedir (dh);
388 if (filename[0] == '\0')
389 fprintf (stderr, "Could not find plugin %s.\n", type);
391 return (ret);
392 }
394 /*
395 * The `register_*' functions follow
396 */
397 int plugin_register_config (const char *name,
398 int (*callback) (const char *key, const char *val),
399 const char **keys, int keys_num)
400 {
401 cf_register (name, callback, keys, keys_num);
402 return (0);
403 } /* int plugin_register_config */
405 int plugin_register_complex_config (const char *type,
406 int (*callback) (oconfig_item_t *))
407 {
408 return (cf_register_complex (type, callback));
409 } /* int plugin_register_complex_config */
411 int plugin_register_init (const char *name,
412 int (*callback) (void))
413 {
414 return (register_callback (&list_init, name, (void *) callback));
415 } /* plugin_register_init */
417 int plugin_register_read (const char *name,
418 int (*callback) (void))
419 {
420 read_func_t *rf;
422 rf = (read_func_t *) malloc (sizeof (read_func_t));
423 if (rf == NULL)
424 {
425 char errbuf[1024];
426 ERROR ("plugin_register_read: malloc failed: %s",
427 sstrerror (errno, errbuf, sizeof (errbuf)));
428 return (-1);
429 }
431 memset (rf, '\0', sizeof (read_func_t));
432 rf->wait_time = interval_g;
433 rf->wait_left = 0;
434 rf->callback = callback;
435 rf->needs_read = DONE;
437 return (register_callback (&list_read, name, (void *) rf));
438 } /* int plugin_register_read */
440 int plugin_register_write (const char *name,
441 int (*callback) (const data_set_t *ds, const value_list_t *vl))
442 {
443 return (register_callback (&list_write, name, (void *) callback));
444 } /* int plugin_register_write */
446 int plugin_register_filter (const char *name,
447 int (*callback) (const data_set_t *ds, value_list_t *vl))
448 {
449 return (register_callback (&list_filter, name, (void *) callback));
450 } /* int plugin_register_filter */
452 int plugin_register_flush (const char *name,
453 int (*callback) (const int timeout, const char *identifier))
454 {
455 return (register_callback (&list_flush, name, (void *) callback));
456 } /* int plugin_register_flush */
458 int plugin_register_shutdown (char *name,
459 int (*callback) (void))
460 {
461 return (register_callback (&list_shutdown, name, (void *) callback));
462 } /* int plugin_register_shutdown */
464 int plugin_register_data_set (const data_set_t *ds)
465 {
466 data_set_t *ds_copy;
467 int i;
469 if ((data_sets != NULL)
470 && (c_avl_get (data_sets, ds->type, NULL) == 0))
471 {
472 NOTICE ("Replacing DS `%s' with another version.", ds->type);
473 plugin_unregister_data_set (ds->type);
474 }
475 else if (data_sets == NULL)
476 {
477 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
478 if (data_sets == NULL)
479 return (-1);
480 }
482 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
483 if (ds_copy == NULL)
484 return (-1);
485 memcpy(ds_copy, ds, sizeof (data_set_t));
487 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
488 * ds->ds_num);
489 if (ds_copy->ds == NULL)
490 {
491 free (ds_copy);
492 return (-1);
493 }
495 for (i = 0; i < ds->ds_num; i++)
496 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
498 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
499 } /* int plugin_register_data_set */
501 int plugin_register_log (char *name,
502 void (*callback) (int priority, const char *msg))
503 {
504 return (register_callback (&list_log, name, (void *) callback));
505 } /* int plugin_register_log */
507 int plugin_register_notification (const char *name,
508 int (*callback) (const notification_t *notif))
509 {
510 return (register_callback (&list_notification, name, (void *) callback));
511 } /* int plugin_register_log */
513 int plugin_unregister_config (const char *name)
514 {
515 cf_unregister (name);
516 return (0);
517 } /* int plugin_unregister_config */
519 int plugin_unregister_complex_config (const char *name)
520 {
521 cf_unregister_complex (name);
522 return (0);
523 } /* int plugin_unregister_complex_config */
525 int plugin_unregister_init (const char *name)
526 {
527 return (plugin_unregister (list_init, name));
528 }
530 int plugin_unregister_read (const char *name)
531 {
532 llentry_t *e;
534 e = llist_search (list_read, name);
536 if (e == NULL)
537 return (-1);
539 llist_remove (list_read, e);
540 free (e->value);
541 free (e->key);
542 llentry_destroy (e);
544 return (0);
545 }
547 int plugin_unregister_write (const char *name)
548 {
549 return (plugin_unregister (list_write, name));
550 }
552 int plugin_unregister_filter (const char *name)
553 {
554 return (plugin_unregister (list_filter, name));
555 }
557 int plugin_unregister_flush (const char *name)
558 {
559 return (plugin_unregister (list_flush, name));
560 }
562 int plugin_unregister_shutdown (const char *name)
563 {
564 return (plugin_unregister (list_shutdown, name));
565 }
567 int plugin_unregister_data_set (const char *name)
568 {
569 data_set_t *ds;
571 if (data_sets == NULL)
572 return (-1);
574 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
575 return (-1);
577 sfree (ds->ds);
578 sfree (ds);
580 return (0);
581 } /* int plugin_unregister_data_set */
583 int plugin_unregister_log (const char *name)
584 {
585 return (plugin_unregister (list_log, name));
586 }
588 int plugin_unregister_notification (const char *name)
589 {
590 return (plugin_unregister (list_notification, name));
591 }
593 void plugin_init_all (void)
594 {
595 int (*callback) (void);
596 llentry_t *le;
597 int status;
599 /* Init the value cache */
600 uc_init ();
602 if ((list_init == NULL) && (list_read == NULL))
603 return;
605 /* Calling all init callbacks before checking if read callbacks
606 * are available allows the init callbacks to register the read
607 * callback. */
608 le = llist_head (list_init);
609 while (le != NULL)
610 {
611 callback = (int (*) (void)) le->value;
612 status = (*callback) ();
614 if (status != 0)
615 {
616 ERROR ("Initialization of plugin `%s' "
617 "failed with status %i. "
618 "Plugin will be unloaded.",
619 le->key, status);
620 /* Plugins that register read callbacks from the init
621 * callback should take care of appropriate error
622 * handling themselves. */
623 /* FIXME: Unload _all_ functions */
624 plugin_unregister_read (le->key);
625 }
627 le = le->next;
628 }
630 /* Start read-threads */
631 if (list_read != NULL)
632 {
633 const char *rt;
634 int num;
635 rt = global_option_get ("ReadThreads");
636 num = atoi (rt);
637 start_threads ((num > 0) ? num : 5);
638 }
639 } /* void plugin_init_all */
641 void plugin_read_all (void)
642 {
643 llentry_t *le;
644 read_func_t *rf;
646 uc_check_timeout ();
648 if (list_read == NULL)
649 return;
651 pthread_mutex_lock (&read_lock);
653 le = llist_head (list_read);
654 while (le != NULL)
655 {
656 rf = (read_func_t *) le->value;
658 if (rf->needs_read != DONE)
659 {
660 le = le->next;
661 continue;
662 }
664 if (rf->wait_left > 0)
665 rf->wait_left -= interval_g;
667 if (rf->wait_left <= 0)
668 {
669 rf->needs_read = TODO;
670 }
672 le = le->next;
673 }
675 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
676 pthread_cond_broadcast (&read_cond);
677 pthread_mutex_unlock (&read_lock);
678 } /* void plugin_read_all */
680 int plugin_write (const char *plugin, /* {{{ */
681 const data_set_t *ds, const value_list_t *vl)
682 {
683 int (*callback) (const data_set_t *ds, const value_list_t *vl);
684 llentry_t *le;
685 int status;
687 if (vl == NULL)
688 return (EINVAL);
690 if (list_write == NULL)
691 return (ENOENT);
693 if (ds == NULL)
694 {
695 ds = plugin_get_ds (vl->type);
696 if (ds == NULL)
697 {
698 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
699 return (ENOENT);
700 }
701 }
703 if (plugin == NULL)
704 {
705 int success = 0;
706 int failure = 0;
708 le = llist_head (list_write);
709 while (le != NULL)
710 {
711 callback = le->value;
712 status = (*callback) (ds, vl);
713 if (status != 0)
714 failure++;
715 else
716 success++;
718 le = le->next;
719 }
721 if ((success == 0) && (failure != 0))
722 status = -1;
723 else
724 status = 0;
725 }
726 else /* plugin != NULL */
727 {
728 le = llist_head (list_write);
729 while (le != NULL)
730 {
731 if (strcasecmp (plugin, le->key) == 0)
732 break;
734 le = le->next;
735 }
737 if (le == NULL)
738 return (ENOENT);
740 callback = le->value;
741 status = (*callback) (ds, vl);
742 }
744 return (status);
745 } /* }}} int plugin_write */
747 int plugin_flush (const char *plugin, int timeout, const char *identifier)
748 {
749 int (*callback) (int timeout, const char *identifier);
750 llentry_t *le;
752 if (list_flush == NULL)
753 return (0);
755 le = llist_head (list_flush);
756 while (le != NULL)
757 {
758 if ((plugin != NULL)
759 && (strcmp (plugin, le->key) != 0))
760 {
761 le = le->next;
762 continue;
763 }
765 callback = (int (*) (int, const char *)) le->value;
766 (*callback) (timeout, identifier);
768 le = le->next;
769 }
770 return (0);
771 } /* int plugin_flush */
773 void plugin_shutdown_all (void)
774 {
775 int (*callback) (void);
776 llentry_t *le;
778 stop_threads ();
780 if (list_shutdown == NULL)
781 return;
783 le = llist_head (list_shutdown);
784 while (le != NULL)
785 {
786 callback = (int (*) (void)) le->value;
788 /* Advance the pointer before calling the callback allows
789 * shutdown functions to unregister themselves. If done the
790 * other way around the memory `le' points to will be freed
791 * after callback returns. */
792 le = le->next;
794 (*callback) ();
795 }
796 } /* void plugin_shutdown_all */
798 int plugin_dispatch_values (value_list_t *vl)
799 {
800 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
802 data_set_t *ds;
803 llentry_t *le;
805 int filter = 0;
807 if ((vl == NULL) || (*vl->type == '\0')) {
808 ERROR ("plugin_dispatch_values: Invalid value list.");
809 return (-1);
810 }
812 if (list_write == NULL)
813 c_complain_once (LOG_WARNING, &no_write_complaint,
814 "plugin_dispatch_values: No write callback has been "
815 "registered. Please load at least one output plugin, "
816 "if you want the collected data to be stored.");
818 if (data_sets == NULL)
819 {
820 ERROR ("plugin_dispatch_values: No data sets registered. "
821 "Could the types database be read? Check "
822 "your `TypesDB' setting!");
823 return (-1);
824 }
826 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
827 {
828 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
829 return (-1);
830 }
832 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
833 "host = %s; "
834 "plugin = %s; plugin_instance = %s; "
835 "type = %s; type_instance = %s;",
836 (unsigned int) vl->time, vl->interval,
837 vl->host,
838 vl->plugin, vl->plugin_instance,
839 vl->type, vl->type_instance);
841 #if COLLECT_DEBUG
842 assert (0 == strcmp (ds->type, vl->type));
843 #else
844 if (0 != strcmp (ds->type, vl->type))
845 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
846 ds->type, vl->type);
847 #endif
849 #if COLLECT_DEBUG
850 assert (ds->ds_num == vl->values_len);
851 #else
852 if (ds->ds_num != vl->values_len)
853 {
854 ERROR ("plugin_dispatch_values: ds->type = %s: "
855 "(ds->ds_num = %i) != "
856 "(vl->values_len = %i)",
857 ds->type, ds->ds_num, vl->values_len);
858 return (-1);
859 }
860 #endif
862 escape_slashes (vl->host, sizeof (vl->host));
863 escape_slashes (vl->plugin, sizeof (vl->plugin));
864 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
865 escape_slashes (vl->type, sizeof (vl->type));
866 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
868 le = llist_head (list_filter);
869 while (le != NULL)
870 {
871 int (*filter_callback) (const data_set_t *, value_list_t *) =
872 (int (*) (const data_set_t *, value_list_t *)) le->value;
874 filter |= (*filter_callback) (ds, vl);
876 if (filter == FILTER_IGNORE)
877 return (-1);
879 le = le->next;
880 }
882 /* Update the value cache */
883 uc_update (ds, vl);
885 if ((filter & FILTER_NOTHRESHOLD_CHECK) == 0)
886 ut_check_threshold (ds, vl);
888 if (filter & FILTER_NOWRITE)
889 return (0);
891 le = llist_head (list_write);
892 while (le != NULL)
893 {
894 int (*write_callback) (const data_set_t *, const value_list_t *) =
895 (int (*) (const data_set_t *, const value_list_t *)) le->value;
897 (*write_callback) (ds, vl);
899 le = le->next;
900 }
902 return (0);
903 } /* int plugin_dispatch_values */
905 int plugin_dispatch_notification (const notification_t *notif)
906 {
907 int (*callback) (const notification_t *);
908 llentry_t *le;
909 /* Possible TODO: Add flap detection here */
911 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
912 "time = %u; host = %s;",
913 notif->severity, notif->message,
914 (unsigned int) notif->time, notif->host);
916 /* Nobody cares for notifications */
917 if (list_notification == NULL)
918 return (-1);
920 le = llist_head (list_notification);
921 while (le != NULL)
922 {
923 callback = (int (*) (const notification_t *)) le->value;
924 (*callback) (notif);
926 le = le->next;
927 }
929 return (0);
930 } /* int plugin_dispatch_notification */
932 void plugin_log (int level, const char *format, ...)
933 {
934 char msg[1024];
935 va_list ap;
937 void (*callback) (int, const char *);
938 llentry_t *le;
940 if (list_log == NULL)
941 return;
943 #if !COLLECT_DEBUG
944 if (level >= LOG_DEBUG)
945 return;
946 #endif
948 va_start (ap, format);
949 vsnprintf (msg, sizeof (msg), format, ap);
950 msg[sizeof (msg) - 1] = '\0';
951 va_end (ap);
953 le = llist_head (list_log);
954 while (le != NULL)
955 {
956 callback = (void (*) (int, const char *)) le->value;
957 (*callback) (level, msg);
959 le = le->next;
960 }
961 } /* void plugin_log */
963 const data_set_t *plugin_get_ds (const char *name)
964 {
965 data_set_t *ds;
967 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
968 {
969 DEBUG ("No such dataset registered: %s", name);
970 return (NULL);
971 }
973 return (ds);
974 } /* data_set_t *plugin_get_ds */
976 static int plugin_notification_meta_add (notification_t *n,
977 const char *name,
978 enum notification_meta_type_e type,
979 const void *value)
980 {
981 notification_meta_t *meta;
982 notification_meta_t *tail;
984 if ((n == NULL) || (name == NULL) || (value == NULL))
985 {
986 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
987 return (-1);
988 }
990 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
991 if (meta == NULL)
992 {
993 ERROR ("plugin_notification_meta_add: malloc failed.");
994 return (-1);
995 }
996 memset (meta, 0, sizeof (notification_meta_t));
998 sstrncpy (meta->name, name, sizeof (meta->name));
999 meta->type = type;
1001 switch (type)
1002 {
1003 case NM_TYPE_STRING:
1004 {
1005 meta->value_string = strdup ((const char *) value);
1006 if (meta->value_string == NULL)
1007 {
1008 ERROR ("plugin_notification_meta_add: strdup failed.");
1009 sfree (meta);
1010 return (-1);
1011 }
1012 break;
1013 }
1014 case NM_TYPE_SIGNED_INT:
1015 {
1016 meta->value_signed_int = *((int64_t *) value);
1017 break;
1018 }
1019 case NM_TYPE_UNSIGNED_INT:
1020 {
1021 meta->value_unsigned_int = *((uint64_t *) value);
1022 break;
1023 }
1024 case NM_TYPE_DOUBLE:
1025 {
1026 meta->value_double = *((double *) value);
1027 break;
1028 }
1029 case NM_TYPE_BOOLEAN:
1030 {
1031 meta->value_boolean = *((bool *) value);
1032 break;
1033 }
1034 default:
1035 {
1036 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1037 sfree (meta);
1038 return (-1);
1039 }
1040 } /* switch (type) */
1042 meta->next = NULL;
1043 tail = n->meta;
1044 while ((tail != NULL) && (tail->next != NULL))
1045 tail = tail->next;
1047 if (tail == NULL)
1048 n->meta = meta;
1049 else
1050 tail->next = meta;
1052 return (0);
1053 } /* int plugin_notification_meta_add */
1055 int plugin_notification_meta_add_string (notification_t *n,
1056 const char *name,
1057 const char *value)
1058 {
1059 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1060 }
1062 int plugin_notification_meta_add_signed_int (notification_t *n,
1063 const char *name,
1064 int64_t value)
1065 {
1066 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1067 }
1069 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1070 const char *name,
1071 uint64_t value)
1072 {
1073 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1074 }
1076 int plugin_notification_meta_add_double (notification_t *n,
1077 const char *name,
1078 double value)
1079 {
1080 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1081 }
1083 int plugin_notification_meta_add_boolean (notification_t *n,
1084 const char *name,
1085 bool value)
1086 {
1087 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1088 }
1090 int plugin_notification_meta_copy (notification_t *dst,
1091 const notification_t *src)
1092 {
1093 notification_meta_t *meta;
1095 assert (dst != NULL);
1096 assert (src != NULL);
1097 assert (dst != src);
1098 assert ((src->meta == NULL) || (src->meta != dst->meta));
1100 for (meta = src->meta; meta != NULL; meta = meta->next)
1101 {
1102 if (meta->type == NM_TYPE_STRING)
1103 plugin_notification_meta_add_string (dst, meta->name,
1104 meta->value_string);
1105 else if (meta->type == NM_TYPE_SIGNED_INT)
1106 plugin_notification_meta_add_signed_int (dst, meta->name,
1107 meta->value_signed_int);
1108 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1109 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1110 meta->value_unsigned_int);
1111 else if (meta->type == NM_TYPE_DOUBLE)
1112 plugin_notification_meta_add_double (dst, meta->name,
1113 meta->value_double);
1114 else if (meta->type == NM_TYPE_BOOLEAN)
1115 plugin_notification_meta_add_boolean (dst, meta->name,
1116 meta->value_boolean);
1117 }
1119 return (0);
1120 } /* int plugin_notification_meta_copy */
1122 int plugin_notification_meta_free (notification_t *n)
1123 {
1124 notification_meta_t *this;
1125 notification_meta_t *next;
1127 if (n == NULL)
1128 {
1129 ERROR ("plugin_notification_meta_free: n == NULL!");
1130 return (-1);
1131 }
1133 this = n->meta;
1134 n->meta = NULL;
1135 while (this != NULL)
1136 {
1137 next = this->next;
1139 if (this->type == NM_TYPE_STRING)
1140 {
1141 free ((char *)this->value_string);
1142 this->value_string = NULL;
1143 }
1144 sfree (this);
1146 this = next;
1147 }
1149 return (0);
1150 } /* int plugin_notification_meta_free */