1 /**
2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2011 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 collectd.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_heap.h"
38 #include "utils_cache.h"
39 #include "filter_chain.h"
41 /*
42 * Private structures
43 */
44 struct callback_func_s
45 {
46 void *cf_callback;
47 user_data_t cf_udata;
48 };
49 typedef struct callback_func_s callback_func_t;
51 #define RF_SIMPLE 0
52 #define RF_COMPLEX 1
53 #define RF_REMOVE 65535
54 struct read_func_s
55 {
56 /* `read_func_t' "inherits" from `callback_func_t'.
57 * The `rf_super' member MUST be the first one in this structure! */
58 #define rf_callback rf_super.cf_callback
59 #define rf_udata rf_super.cf_udata
60 callback_func_t rf_super;
61 char rf_group[DATA_MAX_NAME_LEN];
62 char rf_name[DATA_MAX_NAME_LEN];
63 int rf_type;
64 struct timespec rf_interval;
65 struct timespec rf_effective_interval;
66 struct timespec rf_next_read;
67 };
68 typedef struct read_func_s read_func_t;
70 /*
71 * Private variables
72 */
73 static llist_t *list_init;
74 static llist_t *list_write;
75 static llist_t *list_flush;
76 static llist_t *list_missing;
77 static llist_t *list_shutdown;
78 static llist_t *list_log;
79 static llist_t *list_notification;
81 static fc_chain_t *pre_cache_chain = NULL;
82 static fc_chain_t *post_cache_chain = NULL;
84 static c_avl_tree_t *data_sets;
86 static char *plugindir = NULL;
88 static c_heap_t *read_heap = NULL;
89 static llist_t *read_list;
90 static int read_loop = 1;
91 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
92 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
93 static pthread_t *read_threads = NULL;
94 static int read_threads_num = 0;
96 /*
97 * Static functions
98 */
99 static const char *plugin_get_dir (void)
100 {
101 if (plugindir == NULL)
102 return (PLUGINDIR);
103 else
104 return (plugindir);
105 }
107 static void destroy_callback (callback_func_t *cf) /* {{{ */
108 {
109 if (cf == NULL)
110 return;
112 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
113 {
114 cf->cf_udata.free_func (cf->cf_udata.data);
115 cf->cf_udata.data = NULL;
116 cf->cf_udata.free_func = NULL;
117 }
118 sfree (cf);
119 } /* }}} void destroy_callback */
121 static void destroy_all_callbacks (llist_t **list) /* {{{ */
122 {
123 llentry_t *le;
125 if (*list == NULL)
126 return;
128 le = llist_head (*list);
129 while (le != NULL)
130 {
131 llentry_t *le_next;
133 le_next = le->next;
135 sfree (le->key);
136 destroy_callback (le->value);
137 le->value = NULL;
139 le = le_next;
140 }
142 llist_destroy (*list);
143 *list = NULL;
144 } /* }}} void destroy_all_callbacks */
146 static void destroy_read_heap (void) /* {{{ */
147 {
148 if (read_heap == NULL)
149 return;
151 while (42)
152 {
153 callback_func_t *cf;
155 cf = c_heap_get_root (read_heap);
156 if (cf == NULL)
157 break;
159 destroy_callback (cf);
160 }
162 c_heap_destroy (read_heap);
163 read_heap = NULL;
164 } /* }}} void destroy_read_heap */
166 static int register_callback (llist_t **list, /* {{{ */
167 const char *name, callback_func_t *cf)
168 {
169 llentry_t *le;
170 char *key;
172 if (*list == NULL)
173 {
174 *list = llist_create ();
175 if (*list == NULL)
176 {
177 ERROR ("plugin: register_callback: "
178 "llist_create failed.");
179 destroy_callback (cf);
180 return (-1);
181 }
182 }
184 key = strdup (name);
185 if (key == NULL)
186 {
187 ERROR ("plugin: register_callback: strdup failed.");
188 destroy_callback (cf);
189 return (-1);
190 }
192 le = llist_search (*list, name);
193 if (le == NULL)
194 {
195 le = llentry_create (key, cf);
196 if (le == NULL)
197 {
198 ERROR ("plugin: register_callback: "
199 "llentry_create failed.");
200 free (key);
201 destroy_callback (cf);
202 return (-1);
203 }
205 llist_append (*list, le);
206 }
207 else
208 {
209 callback_func_t *old_cf;
211 old_cf = le->value;
212 le->value = cf;
214 WARNING ("plugin: register_callback: "
215 "a callback named `%s' already exists - "
216 "overwriting the old entry!", name);
218 destroy_callback (old_cf);
219 sfree (key);
220 }
222 return (0);
223 } /* }}} int register_callback */
225 static int create_register_callback (llist_t **list, /* {{{ */
226 const char *name, void *callback, user_data_t *ud)
227 {
228 callback_func_t *cf;
230 cf = (callback_func_t *) malloc (sizeof (*cf));
231 if (cf == NULL)
232 {
233 ERROR ("plugin: create_register_callback: malloc failed.");
234 return (-1);
235 }
236 memset (cf, 0, sizeof (*cf));
238 cf->cf_callback = callback;
239 if (ud == NULL)
240 {
241 cf->cf_udata.data = NULL;
242 cf->cf_udata.free_func = NULL;
243 }
244 else
245 {
246 cf->cf_udata = *ud;
247 }
249 return (register_callback (list, name, cf));
250 } /* }}} int create_register_callback */
252 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
253 {
254 llentry_t *e;
256 if (list == NULL)
257 return (-1);
259 e = llist_search (list, name);
260 if (e == NULL)
261 return (-1);
263 llist_remove (list, e);
265 sfree (e->key);
266 destroy_callback (e->value);
268 llentry_destroy (e);
270 return (0);
271 } /* }}} int plugin_unregister */
273 /*
274 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
275 * object, but it will bitch about a shared object not having a
276 * ``module_register'' symbol..
277 */
278 static int plugin_load_file (char *file, uint32_t flags)
279 {
280 lt_dlhandle dlh;
281 void (*reg_handle) (void);
283 lt_dlinit ();
284 lt_dlerror (); /* clear errors */
286 #if LIBTOOL_VERSION == 2
287 if (flags & PLUGIN_FLAGS_GLOBAL) {
288 lt_dladvise advise;
289 lt_dladvise_init(&advise);
290 lt_dladvise_global(&advise);
291 dlh = lt_dlopenadvise(file, advise);
292 lt_dladvise_destroy(&advise);
293 } else {
294 dlh = lt_dlopen (file);
295 }
296 #else /* if LIBTOOL_VERSION == 1 */
297 if (flags & PLUGIN_FLAGS_GLOBAL)
298 WARNING ("plugin_load_file: The global flag is not supported, "
299 "libtool 2 is required for this.");
300 dlh = lt_dlopen (file);
301 #endif
303 if (dlh == NULL)
304 {
305 char errbuf[1024] = "";
307 ssnprintf (errbuf, sizeof (errbuf),
308 "lt_dlopen (\"%s\") failed: %s. "
309 "The most common cause for this problem are "
310 "missing dependencies. Use ldd(1) to check "
311 "the dependencies of the plugin "
312 "/ shared object.",
313 file, lt_dlerror ());
315 ERROR ("%s", errbuf);
316 /* Make sure this is printed to STDERR in any case, but also
317 * make sure it's printed only once. */
318 if (list_log != NULL)
319 fprintf (stderr, "ERROR: %s\n", errbuf);
321 return (1);
322 }
324 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
325 {
326 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
327 file, lt_dlerror ());
328 lt_dlclose (dlh);
329 return (-1);
330 }
332 (*reg_handle) ();
334 return (0);
335 }
337 static _Bool timeout_reached(struct timespec timeout)
338 {
339 struct timeval now;
340 gettimeofday(&now, NULL);
341 return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
342 }
344 static void *plugin_read_thread (void __attribute__((unused)) *args)
345 {
346 while (read_loop != 0)
347 {
348 read_func_t *rf;
349 cdtime_t now;
350 int status;
351 int rf_type;
352 int rc;
354 /* Get the read function that needs to be read next. */
355 rf = c_heap_get_root (read_heap);
356 if (rf == NULL)
357 {
358 struct timespec abstime;
360 now = cdtime ();
362 CDTIME_T_TO_TIMESPEC (now + interval_g, &abstime);
364 pthread_mutex_lock (&read_lock);
365 pthread_cond_timedwait (&read_cond, &read_lock,
366 &abstime);
367 pthread_mutex_unlock (&read_lock);
368 continue;
369 }
371 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
372 {
373 now = cdtime ();
375 CDTIME_T_TO_TIMESPEC (interval_g, &rf->rf_interval);
377 rf->rf_effective_interval = rf->rf_interval;
379 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
380 }
382 /* sleep until this entry is due,
383 * using pthread_cond_timedwait */
384 pthread_mutex_lock (&read_lock);
385 /* In pthread_cond_timedwait, spurious wakeups are possible
386 * (and really happen, at least on NetBSD with > 1 CPU), thus
387 * we need to re-evaluate the condition every time
388 * pthread_cond_timedwait returns. */
389 rc = 0;
390 while ((read_loop != 0)
391 && !timeout_reached(rf->rf_next_read)
392 && rc == 0)
393 {
394 rc = pthread_cond_timedwait (&read_cond, &read_lock,
395 &rf->rf_next_read);
396 }
398 /* Must hold `read_lock' when accessing `rf->rf_type'. */
399 rf_type = rf->rf_type;
400 pthread_mutex_unlock (&read_lock);
402 /* Check if we're supposed to stop.. This may have interrupted
403 * the sleep, too. */
404 if (read_loop == 0)
405 {
406 /* Insert `rf' again, so it can be free'd correctly */
407 c_heap_insert (read_heap, rf);
408 break;
409 }
411 /* The entry has been marked for deletion. The linked list
412 * entry has already been removed by `plugin_unregister_read'.
413 * All we have to do here is free the `read_func_t' and
414 * continue. */
415 if (rf_type == RF_REMOVE)
416 {
417 DEBUG ("plugin_read_thread: Destroying the `%s' "
418 "callback.", rf->rf_name);
419 destroy_callback ((callback_func_t *) rf);
420 rf = NULL;
421 continue;
422 }
424 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
426 if (rf_type == RF_SIMPLE)
427 {
428 int (*callback) (void);
430 callback = rf->rf_callback;
431 status = (*callback) ();
432 }
433 else
434 {
435 plugin_read_cb callback;
437 assert (rf_type == RF_COMPLEX);
439 callback = rf->rf_callback;
440 status = (*callback) (&rf->rf_udata);
441 }
443 /* If the function signals failure, we will increase the
444 * intervals in which it will be called. */
445 if (status != 0)
446 {
447 rf->rf_effective_interval.tv_sec *= 2;
448 rf->rf_effective_interval.tv_nsec *= 2;
449 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
451 if (rf->rf_effective_interval.tv_sec >= 86400)
452 {
453 rf->rf_effective_interval.tv_sec = 86400;
454 rf->rf_effective_interval.tv_nsec = 0;
455 }
457 NOTICE ("read-function of plugin `%s' failed. "
458 "Will suspend it for %i seconds.",
459 rf->rf_name,
460 (int) rf->rf_effective_interval.tv_sec);
461 }
462 else
463 {
464 /* Success: Restore the interval, if it was changed. */
465 rf->rf_effective_interval = rf->rf_interval;
466 }
468 /* update the ``next read due'' field */
469 now = cdtime ();
471 DEBUG ("plugin_read_thread: Effective interval of the "
472 "%s plugin is %i.%09i.",
473 rf->rf_name,
474 (int) rf->rf_effective_interval.tv_sec,
475 (int) rf->rf_effective_interval.tv_nsec);
477 /* Calculate the next (absolute) time at which this function
478 * should be called. */
479 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
480 + rf->rf_effective_interval.tv_sec;
481 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
482 + rf->rf_effective_interval.tv_nsec;
483 NORMALIZE_TIMESPEC (rf->rf_next_read);
485 /* Check, if `rf_next_read' is in the past. */
486 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
487 {
488 /* `rf_next_read' is in the past. Insert `now'
489 * so this value doesn't trail off into the
490 * past too much. */
491 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
492 }
494 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
495 rf->rf_name,
496 (int) rf->rf_next_read.tv_sec,
497 (int) rf->rf_next_read.tv_nsec);
499 /* Re-insert this read function into the heap again. */
500 c_heap_insert (read_heap, rf);
501 } /* while (read_loop) */
503 pthread_exit (NULL);
504 return ((void *) 0);
505 } /* void *plugin_read_thread */
507 static void start_read_threads (int num)
508 {
509 int i;
511 if (read_threads != NULL)
512 return;
514 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
515 if (read_threads == NULL)
516 {
517 ERROR ("plugin: start_read_threads: calloc failed.");
518 return;
519 }
521 read_threads_num = 0;
522 for (i = 0; i < num; i++)
523 {
524 if (pthread_create (read_threads + read_threads_num, NULL,
525 plugin_read_thread, NULL) == 0)
526 {
527 read_threads_num++;
528 }
529 else
530 {
531 ERROR ("plugin: start_read_threads: pthread_create failed.");
532 return;
533 }
534 } /* for (i) */
535 } /* void start_read_threads */
537 static void stop_read_threads (void)
538 {
539 int i;
541 if (read_threads == NULL)
542 return;
544 INFO ("collectd: Stopping %i read threads.", read_threads_num);
546 pthread_mutex_lock (&read_lock);
547 read_loop = 0;
548 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
549 pthread_cond_broadcast (&read_cond);
550 pthread_mutex_unlock (&read_lock);
552 for (i = 0; i < read_threads_num; i++)
553 {
554 if (pthread_join (read_threads[i], NULL) != 0)
555 {
556 ERROR ("plugin: stop_read_threads: pthread_join failed.");
557 }
558 read_threads[i] = (pthread_t) 0;
559 }
560 sfree (read_threads);
561 read_threads_num = 0;
562 } /* void stop_read_threads */
564 /*
565 * Public functions
566 */
567 void plugin_set_dir (const char *dir)
568 {
569 if (plugindir != NULL)
570 free (plugindir);
572 if (dir == NULL)
573 plugindir = NULL;
574 else if ((plugindir = strdup (dir)) == NULL)
575 {
576 char errbuf[1024];
577 ERROR ("strdup failed: %s",
578 sstrerror (errno, errbuf, sizeof (errbuf)));
579 }
580 }
582 #define BUFSIZE 512
583 int plugin_load (const char *type, uint32_t flags)
584 {
585 DIR *dh;
586 const char *dir;
587 char filename[BUFSIZE] = "";
588 char typename[BUFSIZE];
589 int typename_len;
590 int ret;
591 struct stat statbuf;
592 struct dirent *de;
593 int status;
595 DEBUG ("type = %s", type);
597 dir = plugin_get_dir ();
598 ret = 1;
600 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
601 * type when matching the filename */
602 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
603 if ((status < 0) || ((size_t) status >= sizeof (typename)))
604 {
605 WARNING ("snprintf: truncated: `%s.so'", type);
606 return (-1);
607 }
608 typename_len = strlen (typename);
610 if ((dh = opendir (dir)) == NULL)
611 {
612 char errbuf[1024];
613 ERROR ("opendir (%s): %s", dir,
614 sstrerror (errno, errbuf, sizeof (errbuf)));
615 return (-1);
616 }
618 while ((de = readdir (dh)) != NULL)
619 {
620 if (strncasecmp (de->d_name, typename, typename_len))
621 continue;
623 status = ssnprintf (filename, sizeof (filename),
624 "%s/%s", dir, de->d_name);
625 if ((status < 0) || ((size_t) status >= sizeof (filename)))
626 {
627 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
628 continue;
629 }
631 if (lstat (filename, &statbuf) == -1)
632 {
633 char errbuf[1024];
634 WARNING ("stat %s: %s", filename,
635 sstrerror (errno, errbuf, sizeof (errbuf)));
636 continue;
637 }
638 else if (!S_ISREG (statbuf.st_mode))
639 {
640 /* don't follow symlinks */
641 WARNING ("stat %s: not a regular file", filename);
642 continue;
643 }
645 if (plugin_load_file (filename, flags) == 0)
646 {
647 /* success */
648 ret = 0;
649 break;
650 }
651 else
652 {
653 fprintf (stderr, "Unable to load plugin %s.\n", type);
654 }
655 }
657 closedir (dh);
659 if (filename[0] == '\0')
660 fprintf (stderr, "Could not find plugin %s.\n", type);
662 return (ret);
663 }
665 /*
666 * The `register_*' functions follow
667 */
668 int plugin_register_config (const char *name,
669 int (*callback) (const char *key, const char *val),
670 const char **keys, int keys_num)
671 {
672 cf_register (name, callback, keys, keys_num);
673 return (0);
674 } /* int plugin_register_config */
676 int plugin_register_complex_config (const char *type,
677 int (*callback) (oconfig_item_t *))
678 {
679 return (cf_register_complex (type, callback));
680 } /* int plugin_register_complex_config */
682 int plugin_register_init (const char *name,
683 int (*callback) (void))
684 {
685 return (create_register_callback (&list_init, name, (void *) callback,
686 /* user_data = */ NULL));
687 } /* plugin_register_init */
689 static int plugin_compare_read_func (const void *arg0, const void *arg1)
690 {
691 const read_func_t *rf0;
692 const read_func_t *rf1;
694 rf0 = arg0;
695 rf1 = arg1;
697 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
698 return (-1);
699 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
700 return (1);
701 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
702 return (-1);
703 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
704 return (1);
705 else
706 return (0);
707 } /* int plugin_compare_read_func */
709 /* Add a read function to both, the heap and a linked list. The linked list if
710 * used to look-up read functions, especially for the remove function. The heap
711 * is used to determine which plugin to read next. */
712 static int plugin_insert_read (read_func_t *rf)
713 {
714 int status;
715 llentry_t *le;
717 pthread_mutex_lock (&read_lock);
719 if (read_list == NULL)
720 {
721 read_list = llist_create ();
722 if (read_list == NULL)
723 {
724 pthread_mutex_unlock (&read_lock);
725 ERROR ("plugin_insert_read: read_list failed.");
726 return (-1);
727 }
728 }
730 if (read_heap == NULL)
731 {
732 read_heap = c_heap_create (plugin_compare_read_func);
733 if (read_heap == NULL)
734 {
735 pthread_mutex_unlock (&read_lock);
736 ERROR ("plugin_insert_read: c_heap_create failed.");
737 return (-1);
738 }
739 }
741 le = llist_search (read_list, rf->rf_name);
742 if (le != NULL)
743 {
744 pthread_mutex_unlock (&read_lock);
745 WARNING ("The read function \"%s\" is already registered. "
746 "Check for duplicate \"LoadPlugin\" lines "
747 "in your configuration!",
748 rf->rf_name);
749 return (EINVAL);
750 }
752 le = llentry_create (rf->rf_name, rf);
753 if (le == NULL)
754 {
755 pthread_mutex_unlock (&read_lock);
756 ERROR ("plugin_insert_read: llentry_create failed.");
757 return (-1);
758 }
760 status = c_heap_insert (read_heap, rf);
761 if (status != 0)
762 {
763 pthread_mutex_unlock (&read_lock);
764 ERROR ("plugin_insert_read: c_heap_insert failed.");
765 llentry_destroy (le);
766 return (-1);
767 }
769 /* This does not fail. */
770 llist_append (read_list, le);
772 pthread_mutex_unlock (&read_lock);
773 return (0);
774 } /* int plugin_insert_read */
776 int plugin_register_read (const char *name,
777 int (*callback) (void))
778 {
779 read_func_t *rf;
780 int status;
782 rf = malloc (sizeof (*rf));
783 if (rf == NULL)
784 {
785 ERROR ("plugin_register_read: malloc failed.");
786 return (ENOMEM);
787 }
789 memset (rf, 0, sizeof (read_func_t));
790 rf->rf_callback = (void *) callback;
791 rf->rf_udata.data = NULL;
792 rf->rf_udata.free_func = NULL;
793 rf->rf_group[0] = '\0';
794 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
795 rf->rf_type = RF_SIMPLE;
796 rf->rf_interval.tv_sec = 0;
797 rf->rf_interval.tv_nsec = 0;
798 rf->rf_effective_interval = rf->rf_interval;
800 status = plugin_insert_read (rf);
801 if (status != 0)
802 sfree (rf);
804 return (status);
805 } /* int plugin_register_read */
807 int plugin_register_complex_read (const char *group, const char *name,
808 plugin_read_cb callback,
809 const struct timespec *interval,
810 user_data_t *user_data)
811 {
812 read_func_t *rf;
813 int status;
815 rf = malloc (sizeof (*rf));
816 if (rf == NULL)
817 {
818 ERROR ("plugin_register_complex_read: malloc failed.");
819 return (ENOMEM);
820 }
822 memset (rf, 0, sizeof (read_func_t));
823 rf->rf_callback = (void *) callback;
824 if (group != NULL)
825 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
826 else
827 rf->rf_group[0] = '\0';
828 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
829 rf->rf_type = RF_COMPLEX;
830 if (interval != NULL)
831 {
832 rf->rf_interval = *interval;
833 }
834 rf->rf_effective_interval = rf->rf_interval;
836 /* Set user data */
837 if (user_data == NULL)
838 {
839 rf->rf_udata.data = NULL;
840 rf->rf_udata.free_func = NULL;
841 }
842 else
843 {
844 rf->rf_udata = *user_data;
845 }
847 status = plugin_insert_read (rf);
848 if (status != 0)
849 sfree (rf);
851 return (status);
852 } /* int plugin_register_complex_read */
854 int plugin_register_write (const char *name,
855 plugin_write_cb callback, user_data_t *ud)
856 {
857 return (create_register_callback (&list_write, name,
858 (void *) callback, ud));
859 } /* int plugin_register_write */
861 int plugin_register_flush (const char *name,
862 plugin_flush_cb callback, user_data_t *ud)
863 {
864 return (create_register_callback (&list_flush, name,
865 (void *) callback, ud));
866 } /* int plugin_register_flush */
868 int plugin_register_missing (const char *name,
869 plugin_missing_cb callback, user_data_t *ud)
870 {
871 return (create_register_callback (&list_missing, name,
872 (void *) callback, ud));
873 } /* int plugin_register_missing */
875 int plugin_register_shutdown (const char *name,
876 int (*callback) (void))
877 {
878 return (create_register_callback (&list_shutdown, name,
879 (void *) callback, /* user_data = */ NULL));
880 } /* int plugin_register_shutdown */
882 int plugin_register_data_set (const data_set_t *ds)
883 {
884 data_set_t *ds_copy;
885 int i;
887 if ((data_sets != NULL)
888 && (c_avl_get (data_sets, ds->type, NULL) == 0))
889 {
890 NOTICE ("Replacing DS `%s' with another version.", ds->type);
891 plugin_unregister_data_set (ds->type);
892 }
893 else if (data_sets == NULL)
894 {
895 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
896 if (data_sets == NULL)
897 return (-1);
898 }
900 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
901 if (ds_copy == NULL)
902 return (-1);
903 memcpy(ds_copy, ds, sizeof (data_set_t));
905 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
906 * ds->ds_num);
907 if (ds_copy->ds == NULL)
908 {
909 free (ds_copy);
910 return (-1);
911 }
913 for (i = 0; i < ds->ds_num; i++)
914 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
916 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
917 } /* int plugin_register_data_set */
919 int plugin_register_log (const char *name,
920 plugin_log_cb callback, user_data_t *ud)
921 {
922 return (create_register_callback (&list_log, name,
923 (void *) callback, ud));
924 } /* int plugin_register_log */
926 int plugin_register_notification (const char *name,
927 plugin_notification_cb callback, user_data_t *ud)
928 {
929 return (create_register_callback (&list_notification, name,
930 (void *) callback, ud));
931 } /* int plugin_register_log */
933 int plugin_unregister_config (const char *name)
934 {
935 cf_unregister (name);
936 return (0);
937 } /* int plugin_unregister_config */
939 int plugin_unregister_complex_config (const char *name)
940 {
941 cf_unregister_complex (name);
942 return (0);
943 } /* int plugin_unregister_complex_config */
945 int plugin_unregister_init (const char *name)
946 {
947 return (plugin_unregister (list_init, name));
948 }
950 int plugin_unregister_read (const char *name) /* {{{ */
951 {
952 llentry_t *le;
953 read_func_t *rf;
955 if (name == NULL)
956 return (-ENOENT);
958 pthread_mutex_lock (&read_lock);
960 if (read_list == NULL)
961 {
962 pthread_mutex_unlock (&read_lock);
963 return (-ENOENT);
964 }
966 le = llist_search (read_list, name);
967 if (le == NULL)
968 {
969 pthread_mutex_unlock (&read_lock);
970 WARNING ("plugin_unregister_read: No such read function: %s",
971 name);
972 return (-ENOENT);
973 }
975 llist_remove (read_list, le);
977 rf = le->value;
978 assert (rf != NULL);
979 rf->rf_type = RF_REMOVE;
981 pthread_mutex_unlock (&read_lock);
983 llentry_destroy (le);
985 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
987 return (0);
988 } /* }}} int plugin_unregister_read */
990 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
991 {
992 read_func_t *rf = e->value;
993 char *group = ud;
995 return strcmp (rf->rf_group, (const char *)group);
996 } /* }}} int compare_read_func_group */
998 int plugin_unregister_read_group (const char *group) /* {{{ */
999 {
1000 llentry_t *le;
1001 read_func_t *rf;
1003 int found = 0;
1005 if (group == NULL)
1006 return (-ENOENT);
1008 pthread_mutex_lock (&read_lock);
1010 if (read_list == NULL)
1011 {
1012 pthread_mutex_unlock (&read_lock);
1013 return (-ENOENT);
1014 }
1016 while (42)
1017 {
1018 le = llist_search_custom (read_list,
1019 compare_read_func_group, (void *)group);
1021 if (le == NULL)
1022 break;
1024 ++found;
1026 llist_remove (read_list, le);
1028 rf = le->value;
1029 assert (rf != NULL);
1030 rf->rf_type = RF_REMOVE;
1032 llentry_destroy (le);
1034 DEBUG ("plugin_unregister_read_group: "
1035 "Marked `%s' (group `%s') for removal.",
1036 rf->rf_name, group);
1037 }
1039 pthread_mutex_unlock (&read_lock);
1041 if (found == 0)
1042 {
1043 WARNING ("plugin_unregister_read_group: No such "
1044 "group of read function: %s", group);
1045 return (-ENOENT);
1046 }
1048 return (0);
1049 } /* }}} int plugin_unregister_read_group */
1051 int plugin_unregister_write (const char *name)
1052 {
1053 return (plugin_unregister (list_write, name));
1054 }
1056 int plugin_unregister_flush (const char *name)
1057 {
1058 return (plugin_unregister (list_flush, name));
1059 }
1061 int plugin_unregister_missing (const char *name)
1062 {
1063 return (plugin_unregister (list_missing, name));
1064 }
1066 int plugin_unregister_shutdown (const char *name)
1067 {
1068 return (plugin_unregister (list_shutdown, name));
1069 }
1071 int plugin_unregister_data_set (const char *name)
1072 {
1073 data_set_t *ds;
1075 if (data_sets == NULL)
1076 return (-1);
1078 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1079 return (-1);
1081 sfree (ds->ds);
1082 sfree (ds);
1084 return (0);
1085 } /* int plugin_unregister_data_set */
1087 int plugin_unregister_log (const char *name)
1088 {
1089 return (plugin_unregister (list_log, name));
1090 }
1092 int plugin_unregister_notification (const char *name)
1093 {
1094 return (plugin_unregister (list_notification, name));
1095 }
1097 void plugin_init_all (void)
1098 {
1099 const char *chain_name;
1100 llentry_t *le;
1101 int status;
1103 /* Init the value cache */
1104 uc_init ();
1106 chain_name = global_option_get ("PreCacheChain");
1107 pre_cache_chain = fc_chain_get_by_name (chain_name);
1109 chain_name = global_option_get ("PostCacheChain");
1110 post_cache_chain = fc_chain_get_by_name (chain_name);
1113 if ((list_init == NULL) && (read_heap == NULL))
1114 return;
1116 /* Calling all init callbacks before checking if read callbacks
1117 * are available allows the init callbacks to register the read
1118 * callback. */
1119 le = llist_head (list_init);
1120 while (le != NULL)
1121 {
1122 callback_func_t *cf;
1123 plugin_init_cb callback;
1125 cf = le->value;
1126 callback = cf->cf_callback;
1127 status = (*callback) ();
1129 if (status != 0)
1130 {
1131 ERROR ("Initialization of plugin `%s' "
1132 "failed with status %i. "
1133 "Plugin will be unloaded.",
1134 le->key, status);
1135 /* Plugins that register read callbacks from the init
1136 * callback should take care of appropriate error
1137 * handling themselves. */
1138 /* FIXME: Unload _all_ functions */
1139 plugin_unregister_read (le->key);
1140 }
1142 le = le->next;
1143 }
1145 /* Start read-threads */
1146 if (read_heap != NULL)
1147 {
1148 const char *rt;
1149 int num;
1150 rt = global_option_get ("ReadThreads");
1151 num = atoi (rt);
1152 if (num != -1)
1153 start_read_threads ((num > 0) ? num : 5);
1154 }
1155 } /* void plugin_init_all */
1157 /* TODO: Rename this function. */
1158 void plugin_read_all (void)
1159 {
1160 uc_check_timeout ();
1162 return;
1163 } /* void plugin_read_all */
1165 /* Read function called when the `-T' command line argument is given. */
1166 int plugin_read_all_once (void)
1167 {
1168 int status;
1169 int return_status = 0;
1171 if (read_heap == NULL)
1172 {
1173 NOTICE ("No read-functions are registered.");
1174 return (0);
1175 }
1177 while (42)
1178 {
1179 read_func_t *rf;
1181 rf = c_heap_get_root (read_heap);
1182 if (rf == NULL)
1183 break;
1185 if (rf->rf_type == RF_SIMPLE)
1186 {
1187 int (*callback) (void);
1189 callback = rf->rf_callback;
1190 status = (*callback) ();
1191 }
1192 else
1193 {
1194 plugin_read_cb callback;
1196 callback = rf->rf_callback;
1197 status = (*callback) (&rf->rf_udata);
1198 }
1200 if (status != 0)
1201 {
1202 NOTICE ("read-function of plugin `%s' failed.",
1203 rf->rf_name);
1204 return_status = -1;
1205 }
1207 destroy_callback ((void *) rf);
1208 }
1210 return (return_status);
1211 } /* int plugin_read_all_once */
1213 int plugin_write (const char *plugin, /* {{{ */
1214 const data_set_t *ds, const value_list_t *vl)
1215 {
1216 llentry_t *le;
1217 int status;
1219 if (vl == NULL)
1220 return (EINVAL);
1222 if (list_write == NULL)
1223 return (ENOENT);
1225 if (ds == NULL)
1226 {
1227 ds = plugin_get_ds (vl->type);
1228 if (ds == NULL)
1229 {
1230 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1231 return (ENOENT);
1232 }
1233 }
1235 if (plugin == NULL)
1236 {
1237 int success = 0;
1238 int failure = 0;
1240 le = llist_head (list_write);
1241 while (le != NULL)
1242 {
1243 callback_func_t *cf = le->value;
1244 plugin_write_cb callback;
1246 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1247 callback = cf->cf_callback;
1248 status = (*callback) (ds, vl, &cf->cf_udata);
1249 if (status != 0)
1250 failure++;
1251 else
1252 success++;
1254 le = le->next;
1255 }
1257 if ((success == 0) && (failure != 0))
1258 status = -1;
1259 else
1260 status = 0;
1261 }
1262 else /* plugin != NULL */
1263 {
1264 callback_func_t *cf;
1265 plugin_write_cb callback;
1267 le = llist_head (list_write);
1268 while (le != NULL)
1269 {
1270 if (strcasecmp (plugin, le->key) == 0)
1271 break;
1273 le = le->next;
1274 }
1276 if (le == NULL)
1277 return (ENOENT);
1279 cf = le->value;
1281 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1282 callback = cf->cf_callback;
1283 status = (*callback) (ds, vl, &cf->cf_udata);
1284 }
1286 return (status);
1287 } /* }}} int plugin_write */
1289 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1290 {
1291 llentry_t *le;
1293 if (list_flush == NULL)
1294 return (0);
1296 le = llist_head (list_flush);
1297 while (le != NULL)
1298 {
1299 callback_func_t *cf;
1300 plugin_flush_cb callback;
1302 if ((plugin != NULL)
1303 && (strcmp (plugin, le->key) != 0))
1304 {
1305 le = le->next;
1306 continue;
1307 }
1309 cf = le->value;
1310 callback = cf->cf_callback;
1312 (*callback) (timeout, identifier, &cf->cf_udata);
1314 le = le->next;
1315 }
1316 return (0);
1317 } /* int plugin_flush */
1319 void plugin_shutdown_all (void)
1320 {
1321 llentry_t *le;
1323 stop_read_threads ();
1325 destroy_all_callbacks (&list_init);
1327 pthread_mutex_lock (&read_lock);
1328 llist_destroy (read_list);
1329 read_list = NULL;
1330 pthread_mutex_unlock (&read_lock);
1332 destroy_read_heap ();
1334 plugin_flush (/* plugin = */ NULL,
1335 /* timeout = */ 0,
1336 /* identifier = */ NULL);
1338 le = NULL;
1339 if (list_shutdown != NULL)
1340 le = llist_head (list_shutdown);
1342 while (le != NULL)
1343 {
1344 callback_func_t *cf;
1345 plugin_shutdown_cb callback;
1347 cf = le->value;
1348 callback = cf->cf_callback;
1350 /* Advance the pointer before calling the callback allows
1351 * shutdown functions to unregister themselves. If done the
1352 * other way around the memory `le' points to will be freed
1353 * after callback returns. */
1354 le = le->next;
1356 (*callback) ();
1357 }
1359 /* Write plugins which use the `user_data' pointer usually need the
1360 * same data available to the flush callback. If this is the case, set
1361 * the free_function to NULL when registering the flush callback and to
1362 * the real free function when registering the write callback. This way
1363 * the data isn't freed twice. */
1364 destroy_all_callbacks (&list_flush);
1365 destroy_all_callbacks (&list_missing);
1366 destroy_all_callbacks (&list_write);
1368 destroy_all_callbacks (&list_notification);
1369 destroy_all_callbacks (&list_shutdown);
1370 destroy_all_callbacks (&list_log);
1371 } /* void plugin_shutdown_all */
1373 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1374 {
1375 llentry_t *le;
1377 if (list_missing == NULL)
1378 return (0);
1380 le = llist_head (list_missing);
1381 while (le != NULL)
1382 {
1383 callback_func_t *cf;
1384 plugin_missing_cb callback;
1385 int status;
1387 cf = le->value;
1388 callback = cf->cf_callback;
1390 status = (*callback) (vl, &cf->cf_udata);
1391 if (status != 0)
1392 {
1393 if (status < 0)
1394 {
1395 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1396 "failed with status %i.",
1397 le->key, status);
1398 return (status);
1399 }
1400 else
1401 {
1402 return (0);
1403 }
1404 }
1406 le = le->next;
1407 }
1408 return (0);
1409 } /* int }}} plugin_dispatch_missing */
1411 int plugin_dispatch_values (value_list_t *vl)
1412 {
1413 int status;
1414 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1416 value_t *saved_values;
1417 int saved_values_len;
1419 data_set_t *ds;
1421 int free_meta_data = 0;
1423 if ((vl == NULL) || (vl->type[0] == 0)
1424 || (vl->values == NULL) || (vl->values_len < 1))
1425 {
1426 ERROR ("plugin_dispatch_values: Invalid value list.");
1427 return (-1);
1428 }
1430 /* Free meta data only if the calling function didn't specify any. In
1431 * this case matches and targets may add some and the calling function
1432 * may not expect (and therefore free) that data. */
1433 if (vl->meta == NULL)
1434 free_meta_data = 1;
1436 if (list_write == NULL)
1437 c_complain_once (LOG_WARNING, &no_write_complaint,
1438 "plugin_dispatch_values: No write callback has been "
1439 "registered. Please load at least one output plugin, "
1440 "if you want the collected data to be stored.");
1442 if (data_sets == NULL)
1443 {
1444 ERROR ("plugin_dispatch_values: No data sets registered. "
1445 "Could the types database be read? Check "
1446 "your `TypesDB' setting!");
1447 return (-1);
1448 }
1450 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1451 {
1452 char ident[6 * DATA_MAX_NAME_LEN];
1454 FORMAT_VL (ident, sizeof (ident), vl);
1455 INFO ("plugin_dispatch_values: Dataset not found: %s "
1456 "(from \"%s\"), check your types.db!",
1457 vl->type, ident);
1458 return (-1);
1459 }
1461 if (vl->time == 0)
1462 vl->time = cdtime ();
1464 if (vl->interval <= 0)
1465 vl->interval = interval_g;
1467 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1468 "host = %s; "
1469 "plugin = %s; plugin_instance = %s; "
1470 "type = %s; type_instance = %s;",
1471 CDTIME_T_TO_DOUBLE (vl->time),
1472 CDTIME_T_TO_DOUBLE (vl->interval),
1473 vl->host,
1474 vl->plugin, vl->plugin_instance,
1475 vl->type, vl->type_instance);
1477 #if COLLECT_DEBUG
1478 assert (0 == strcmp (ds->type, vl->type));
1479 #else
1480 if (0 != strcmp (ds->type, vl->type))
1481 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1482 ds->type, vl->type);
1483 #endif
1485 #if COLLECT_DEBUG
1486 assert (ds->ds_num == vl->values_len);
1487 #else
1488 if (ds->ds_num != vl->values_len)
1489 {
1490 ERROR ("plugin_dispatch_values: ds->type = %s: "
1491 "(ds->ds_num = %i) != "
1492 "(vl->values_len = %i)",
1493 ds->type, ds->ds_num, vl->values_len);
1494 return (-1);
1495 }
1496 #endif
1498 escape_slashes (vl->host, sizeof (vl->host));
1499 escape_slashes (vl->plugin, sizeof (vl->plugin));
1500 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1501 escape_slashes (vl->type, sizeof (vl->type));
1502 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1504 /* Copy the values. This way, we can assure `targets' that they get
1505 * dynamically allocated values, which they can free and replace if
1506 * they like. */
1507 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1508 {
1509 saved_values = vl->values;
1510 saved_values_len = vl->values_len;
1512 vl->values = (value_t *) calloc (vl->values_len,
1513 sizeof (*vl->values));
1514 if (vl->values == NULL)
1515 {
1516 ERROR ("plugin_dispatch_values: calloc failed.");
1517 vl->values = saved_values;
1518 return (-1);
1519 }
1520 memcpy (vl->values, saved_values,
1521 vl->values_len * sizeof (*vl->values));
1522 }
1523 else /* if ((pre == NULL) && (post == NULL)) */
1524 {
1525 saved_values = NULL;
1526 saved_values_len = 0;
1527 }
1529 if (pre_cache_chain != NULL)
1530 {
1531 status = fc_process_chain (ds, vl, pre_cache_chain);
1532 if (status < 0)
1533 {
1534 WARNING ("plugin_dispatch_values: Running the "
1535 "pre-cache chain failed with "
1536 "status %i (%#x).",
1537 status, status);
1538 }
1539 else if (status == FC_TARGET_STOP)
1540 {
1541 /* Restore the state of the value_list so that plugins
1542 * don't get confused.. */
1543 if (saved_values != NULL)
1544 {
1545 free (vl->values);
1546 vl->values = saved_values;
1547 vl->values_len = saved_values_len;
1548 }
1549 return (0);
1550 }
1551 }
1553 /* Update the value cache */
1554 uc_update (ds, vl);
1556 if (post_cache_chain != NULL)
1557 {
1558 status = fc_process_chain (ds, vl, post_cache_chain);
1559 if (status < 0)
1560 {
1561 WARNING ("plugin_dispatch_values: Running the "
1562 "post-cache chain failed with "
1563 "status %i (%#x).",
1564 status, status);
1565 }
1566 }
1567 else
1568 fc_default_action (ds, vl);
1570 /* Restore the state of the value_list so that plugins don't get
1571 * confused.. */
1572 if (saved_values != NULL)
1573 {
1574 free (vl->values);
1575 vl->values = saved_values;
1576 vl->values_len = saved_values_len;
1577 }
1579 if ((free_meta_data != 0) && (vl->meta != NULL))
1580 {
1581 meta_data_destroy (vl->meta);
1582 vl->meta = NULL;
1583 }
1585 return (0);
1586 } /* int plugin_dispatch_values */
1588 int plugin_dispatch_notification (const notification_t *notif)
1589 {
1590 llentry_t *le;
1591 /* Possible TODO: Add flap detection here */
1593 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1594 "time = %.3f; host = %s;",
1595 notif->severity, notif->message,
1596 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1598 /* Nobody cares for notifications */
1599 if (list_notification == NULL)
1600 return (-1);
1602 le = llist_head (list_notification);
1603 while (le != NULL)
1604 {
1605 callback_func_t *cf;
1606 plugin_notification_cb callback;
1607 int status;
1609 cf = le->value;
1610 callback = cf->cf_callback;
1611 status = (*callback) (notif, &cf->cf_udata);
1612 if (status != 0)
1613 {
1614 WARNING ("plugin_dispatch_notification: Notification "
1615 "callback %s returned %i.",
1616 le->key, status);
1617 }
1619 le = le->next;
1620 }
1622 return (0);
1623 } /* int plugin_dispatch_notification */
1625 void plugin_log (int level, const char *format, ...)
1626 {
1627 char msg[1024];
1628 va_list ap;
1629 llentry_t *le;
1631 #if !COLLECT_DEBUG
1632 if (level >= LOG_DEBUG)
1633 return;
1634 #endif
1636 va_start (ap, format);
1637 vsnprintf (msg, sizeof (msg), format, ap);
1638 msg[sizeof (msg) - 1] = '\0';
1639 va_end (ap);
1641 if (list_log == NULL)
1642 {
1643 fprintf (stderr, "%s\n", msg);
1644 return;
1645 }
1647 le = llist_head (list_log);
1648 while (le != NULL)
1649 {
1650 callback_func_t *cf;
1651 plugin_log_cb callback;
1653 cf = le->value;
1654 callback = cf->cf_callback;
1656 (*callback) (level, msg, &cf->cf_udata);
1658 le = le->next;
1659 }
1660 } /* void plugin_log */
1662 const data_set_t *plugin_get_ds (const char *name)
1663 {
1664 data_set_t *ds;
1666 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1667 {
1668 DEBUG ("No such dataset registered: %s", name);
1669 return (NULL);
1670 }
1672 return (ds);
1673 } /* data_set_t *plugin_get_ds */
1675 static int plugin_notification_meta_add (notification_t *n,
1676 const char *name,
1677 enum notification_meta_type_e type,
1678 const void *value)
1679 {
1680 notification_meta_t *meta;
1681 notification_meta_t *tail;
1683 if ((n == NULL) || (name == NULL) || (value == NULL))
1684 {
1685 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1686 return (-1);
1687 }
1689 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1690 if (meta == NULL)
1691 {
1692 ERROR ("plugin_notification_meta_add: malloc failed.");
1693 return (-1);
1694 }
1695 memset (meta, 0, sizeof (notification_meta_t));
1697 sstrncpy (meta->name, name, sizeof (meta->name));
1698 meta->type = type;
1700 switch (type)
1701 {
1702 case NM_TYPE_STRING:
1703 {
1704 meta->nm_value.nm_string = strdup ((const char *) value);
1705 if (meta->nm_value.nm_string == NULL)
1706 {
1707 ERROR ("plugin_notification_meta_add: strdup failed.");
1708 sfree (meta);
1709 return (-1);
1710 }
1711 break;
1712 }
1713 case NM_TYPE_SIGNED_INT:
1714 {
1715 meta->nm_value.nm_signed_int = *((int64_t *) value);
1716 break;
1717 }
1718 case NM_TYPE_UNSIGNED_INT:
1719 {
1720 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1721 break;
1722 }
1723 case NM_TYPE_DOUBLE:
1724 {
1725 meta->nm_value.nm_double = *((double *) value);
1726 break;
1727 }
1728 case NM_TYPE_BOOLEAN:
1729 {
1730 meta->nm_value.nm_boolean = *((_Bool *) value);
1731 break;
1732 }
1733 default:
1734 {
1735 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1736 sfree (meta);
1737 return (-1);
1738 }
1739 } /* switch (type) */
1741 meta->next = NULL;
1742 tail = n->meta;
1743 while ((tail != NULL) && (tail->next != NULL))
1744 tail = tail->next;
1746 if (tail == NULL)
1747 n->meta = meta;
1748 else
1749 tail->next = meta;
1751 return (0);
1752 } /* int plugin_notification_meta_add */
1754 int plugin_notification_meta_add_string (notification_t *n,
1755 const char *name,
1756 const char *value)
1757 {
1758 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1759 }
1761 int plugin_notification_meta_add_signed_int (notification_t *n,
1762 const char *name,
1763 int64_t value)
1764 {
1765 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1766 }
1768 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1769 const char *name,
1770 uint64_t value)
1771 {
1772 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1773 }
1775 int plugin_notification_meta_add_double (notification_t *n,
1776 const char *name,
1777 double value)
1778 {
1779 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1780 }
1782 int plugin_notification_meta_add_boolean (notification_t *n,
1783 const char *name,
1784 _Bool value)
1785 {
1786 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1787 }
1789 int plugin_notification_meta_copy (notification_t *dst,
1790 const notification_t *src)
1791 {
1792 notification_meta_t *meta;
1794 assert (dst != NULL);
1795 assert (src != NULL);
1796 assert (dst != src);
1797 assert ((src->meta == NULL) || (src->meta != dst->meta));
1799 for (meta = src->meta; meta != NULL; meta = meta->next)
1800 {
1801 if (meta->type == NM_TYPE_STRING)
1802 plugin_notification_meta_add_string (dst, meta->name,
1803 meta->nm_value.nm_string);
1804 else if (meta->type == NM_TYPE_SIGNED_INT)
1805 plugin_notification_meta_add_signed_int (dst, meta->name,
1806 meta->nm_value.nm_signed_int);
1807 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1808 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1809 meta->nm_value.nm_unsigned_int);
1810 else if (meta->type == NM_TYPE_DOUBLE)
1811 plugin_notification_meta_add_double (dst, meta->name,
1812 meta->nm_value.nm_double);
1813 else if (meta->type == NM_TYPE_BOOLEAN)
1814 plugin_notification_meta_add_boolean (dst, meta->name,
1815 meta->nm_value.nm_boolean);
1816 }
1818 return (0);
1819 } /* int plugin_notification_meta_copy */
1821 int plugin_notification_meta_free (notification_meta_t *n)
1822 {
1823 notification_meta_t *this;
1824 notification_meta_t *next;
1826 if (n == NULL)
1827 {
1828 ERROR ("plugin_notification_meta_free: n == NULL!");
1829 return (-1);
1830 }
1832 this = n;
1833 while (this != NULL)
1834 {
1835 next = this->next;
1837 if (this->type == NM_TYPE_STRING)
1838 {
1839 free ((char *)this->nm_value.nm_string);
1840 this->nm_value.nm_string = NULL;
1841 }
1842 sfree (this);
1844 this = next;
1845 }
1847 return (0);
1848 } /* int plugin_notification_meta_free */
1850 /* vim: set sw=8 ts=8 noet fdm=marker : */