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 cdtime_t now = cdtime ();
718 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
720 pthread_mutex_lock (&read_lock);
722 if (read_list == NULL)
723 {
724 read_list = llist_create ();
725 if (read_list == NULL)
726 {
727 pthread_mutex_unlock (&read_lock);
728 ERROR ("plugin_insert_read: read_list failed.");
729 return (-1);
730 }
731 }
733 if (read_heap == NULL)
734 {
735 read_heap = c_heap_create (plugin_compare_read_func);
736 if (read_heap == NULL)
737 {
738 pthread_mutex_unlock (&read_lock);
739 ERROR ("plugin_insert_read: c_heap_create failed.");
740 return (-1);
741 }
742 }
744 le = llist_search (read_list, rf->rf_name);
745 if (le != NULL)
746 {
747 pthread_mutex_unlock (&read_lock);
748 WARNING ("The read function \"%s\" is already registered. "
749 "Check for duplicate \"LoadPlugin\" lines "
750 "in your configuration!",
751 rf->rf_name);
752 return (EINVAL);
753 }
755 le = llentry_create (rf->rf_name, rf);
756 if (le == NULL)
757 {
758 pthread_mutex_unlock (&read_lock);
759 ERROR ("plugin_insert_read: llentry_create failed.");
760 return (-1);
761 }
763 status = c_heap_insert (read_heap, rf);
764 if (status != 0)
765 {
766 pthread_mutex_unlock (&read_lock);
767 ERROR ("plugin_insert_read: c_heap_insert failed.");
768 llentry_destroy (le);
769 return (-1);
770 }
772 /* This does not fail. */
773 llist_append (read_list, le);
775 pthread_mutex_unlock (&read_lock);
776 return (0);
777 } /* int plugin_insert_read */
779 int plugin_register_read (const char *name,
780 int (*callback) (void))
781 {
782 read_func_t *rf;
783 int status;
785 rf = malloc (sizeof (*rf));
786 if (rf == NULL)
787 {
788 ERROR ("plugin_register_read: malloc failed.");
789 return (ENOMEM);
790 }
792 memset (rf, 0, sizeof (read_func_t));
793 rf->rf_callback = (void *) callback;
794 rf->rf_udata.data = NULL;
795 rf->rf_udata.free_func = NULL;
796 rf->rf_group[0] = '\0';
797 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
798 rf->rf_type = RF_SIMPLE;
799 rf->rf_interval.tv_sec = 0;
800 rf->rf_interval.tv_nsec = 0;
801 rf->rf_effective_interval = rf->rf_interval;
803 status = plugin_insert_read (rf);
804 if (status != 0)
805 sfree (rf);
807 return (status);
808 } /* int plugin_register_read */
810 int plugin_register_complex_read (const char *group, const char *name,
811 plugin_read_cb callback,
812 const struct timespec *interval,
813 user_data_t *user_data)
814 {
815 read_func_t *rf;
816 int status;
818 rf = malloc (sizeof (*rf));
819 if (rf == NULL)
820 {
821 ERROR ("plugin_register_complex_read: malloc failed.");
822 return (ENOMEM);
823 }
825 memset (rf, 0, sizeof (read_func_t));
826 rf->rf_callback = (void *) callback;
827 if (group != NULL)
828 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
829 else
830 rf->rf_group[0] = '\0';
831 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
832 rf->rf_type = RF_COMPLEX;
833 if (interval != NULL)
834 {
835 rf->rf_interval = *interval;
836 }
837 rf->rf_effective_interval = rf->rf_interval;
839 /* Set user data */
840 if (user_data == NULL)
841 {
842 rf->rf_udata.data = NULL;
843 rf->rf_udata.free_func = NULL;
844 }
845 else
846 {
847 rf->rf_udata = *user_data;
848 }
850 status = plugin_insert_read (rf);
851 if (status != 0)
852 sfree (rf);
854 return (status);
855 } /* int plugin_register_complex_read */
857 int plugin_register_write (const char *name,
858 plugin_write_cb callback, user_data_t *ud)
859 {
860 return (create_register_callback (&list_write, name,
861 (void *) callback, ud));
862 } /* int plugin_register_write */
864 int plugin_register_flush (const char *name,
865 plugin_flush_cb callback, user_data_t *ud)
866 {
867 return (create_register_callback (&list_flush, name,
868 (void *) callback, ud));
869 } /* int plugin_register_flush */
871 int plugin_register_missing (const char *name,
872 plugin_missing_cb callback, user_data_t *ud)
873 {
874 return (create_register_callback (&list_missing, name,
875 (void *) callback, ud));
876 } /* int plugin_register_missing */
878 int plugin_register_shutdown (const char *name,
879 int (*callback) (void))
880 {
881 return (create_register_callback (&list_shutdown, name,
882 (void *) callback, /* user_data = */ NULL));
883 } /* int plugin_register_shutdown */
885 int plugin_register_data_set (const data_set_t *ds)
886 {
887 data_set_t *ds_copy;
888 int i;
890 if ((data_sets != NULL)
891 && (c_avl_get (data_sets, ds->type, NULL) == 0))
892 {
893 NOTICE ("Replacing DS `%s' with another version.", ds->type);
894 plugin_unregister_data_set (ds->type);
895 }
896 else if (data_sets == NULL)
897 {
898 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
899 if (data_sets == NULL)
900 return (-1);
901 }
903 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
904 if (ds_copy == NULL)
905 return (-1);
906 memcpy(ds_copy, ds, sizeof (data_set_t));
908 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
909 * ds->ds_num);
910 if (ds_copy->ds == NULL)
911 {
912 free (ds_copy);
913 return (-1);
914 }
916 for (i = 0; i < ds->ds_num; i++)
917 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
919 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
920 } /* int plugin_register_data_set */
922 int plugin_register_log (const char *name,
923 plugin_log_cb callback, user_data_t *ud)
924 {
925 return (create_register_callback (&list_log, name,
926 (void *) callback, ud));
927 } /* int plugin_register_log */
929 int plugin_register_notification (const char *name,
930 plugin_notification_cb callback, user_data_t *ud)
931 {
932 return (create_register_callback (&list_notification, name,
933 (void *) callback, ud));
934 } /* int plugin_register_log */
936 int plugin_unregister_config (const char *name)
937 {
938 cf_unregister (name);
939 return (0);
940 } /* int plugin_unregister_config */
942 int plugin_unregister_complex_config (const char *name)
943 {
944 cf_unregister_complex (name);
945 return (0);
946 } /* int plugin_unregister_complex_config */
948 int plugin_unregister_init (const char *name)
949 {
950 return (plugin_unregister (list_init, name));
951 }
953 int plugin_unregister_read (const char *name) /* {{{ */
954 {
955 llentry_t *le;
956 read_func_t *rf;
958 if (name == NULL)
959 return (-ENOENT);
961 pthread_mutex_lock (&read_lock);
963 if (read_list == NULL)
964 {
965 pthread_mutex_unlock (&read_lock);
966 return (-ENOENT);
967 }
969 le = llist_search (read_list, name);
970 if (le == NULL)
971 {
972 pthread_mutex_unlock (&read_lock);
973 WARNING ("plugin_unregister_read: No such read function: %s",
974 name);
975 return (-ENOENT);
976 }
978 llist_remove (read_list, le);
980 rf = le->value;
981 assert (rf != NULL);
982 rf->rf_type = RF_REMOVE;
984 pthread_mutex_unlock (&read_lock);
986 llentry_destroy (le);
988 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
990 return (0);
991 } /* }}} int plugin_unregister_read */
993 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
994 {
995 read_func_t *rf = e->value;
996 char *group = ud;
998 return strcmp (rf->rf_group, (const char *)group);
999 } /* }}} int compare_read_func_group */
1001 int plugin_unregister_read_group (const char *group) /* {{{ */
1002 {
1003 llentry_t *le;
1004 read_func_t *rf;
1006 int found = 0;
1008 if (group == NULL)
1009 return (-ENOENT);
1011 pthread_mutex_lock (&read_lock);
1013 if (read_list == NULL)
1014 {
1015 pthread_mutex_unlock (&read_lock);
1016 return (-ENOENT);
1017 }
1019 while (42)
1020 {
1021 le = llist_search_custom (read_list,
1022 compare_read_func_group, (void *)group);
1024 if (le == NULL)
1025 break;
1027 ++found;
1029 llist_remove (read_list, le);
1031 rf = le->value;
1032 assert (rf != NULL);
1033 rf->rf_type = RF_REMOVE;
1035 llentry_destroy (le);
1037 DEBUG ("plugin_unregister_read_group: "
1038 "Marked `%s' (group `%s') for removal.",
1039 rf->rf_name, group);
1040 }
1042 pthread_mutex_unlock (&read_lock);
1044 if (found == 0)
1045 {
1046 WARNING ("plugin_unregister_read_group: No such "
1047 "group of read function: %s", group);
1048 return (-ENOENT);
1049 }
1051 return (0);
1052 } /* }}} int plugin_unregister_read_group */
1054 int plugin_unregister_write (const char *name)
1055 {
1056 return (plugin_unregister (list_write, name));
1057 }
1059 int plugin_unregister_flush (const char *name)
1060 {
1061 return (plugin_unregister (list_flush, name));
1062 }
1064 int plugin_unregister_missing (const char *name)
1065 {
1066 return (plugin_unregister (list_missing, name));
1067 }
1069 int plugin_unregister_shutdown (const char *name)
1070 {
1071 return (plugin_unregister (list_shutdown, name));
1072 }
1074 int plugin_unregister_data_set (const char *name)
1075 {
1076 data_set_t *ds;
1078 if (data_sets == NULL)
1079 return (-1);
1081 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1082 return (-1);
1084 sfree (ds->ds);
1085 sfree (ds);
1087 return (0);
1088 } /* int plugin_unregister_data_set */
1090 int plugin_unregister_log (const char *name)
1091 {
1092 return (plugin_unregister (list_log, name));
1093 }
1095 int plugin_unregister_notification (const char *name)
1096 {
1097 return (plugin_unregister (list_notification, name));
1098 }
1100 void plugin_init_all (void)
1101 {
1102 const char *chain_name;
1103 llentry_t *le;
1104 int status;
1106 /* Init the value cache */
1107 uc_init ();
1109 chain_name = global_option_get ("PreCacheChain");
1110 pre_cache_chain = fc_chain_get_by_name (chain_name);
1112 chain_name = global_option_get ("PostCacheChain");
1113 post_cache_chain = fc_chain_get_by_name (chain_name);
1116 if ((list_init == NULL) && (read_heap == NULL))
1117 return;
1119 /* Calling all init callbacks before checking if read callbacks
1120 * are available allows the init callbacks to register the read
1121 * callback. */
1122 le = llist_head (list_init);
1123 while (le != NULL)
1124 {
1125 callback_func_t *cf;
1126 plugin_init_cb callback;
1128 cf = le->value;
1129 callback = cf->cf_callback;
1130 status = (*callback) ();
1132 if (status != 0)
1133 {
1134 ERROR ("Initialization of plugin `%s' "
1135 "failed with status %i. "
1136 "Plugin will be unloaded.",
1137 le->key, status);
1138 /* Plugins that register read callbacks from the init
1139 * callback should take care of appropriate error
1140 * handling themselves. */
1141 /* FIXME: Unload _all_ functions */
1142 plugin_unregister_read (le->key);
1143 }
1145 le = le->next;
1146 }
1148 /* Start read-threads */
1149 if (read_heap != NULL)
1150 {
1151 const char *rt;
1152 int num;
1153 rt = global_option_get ("ReadThreads");
1154 num = atoi (rt);
1155 if (num != -1)
1156 start_read_threads ((num > 0) ? num : 5);
1157 }
1158 } /* void plugin_init_all */
1160 /* TODO: Rename this function. */
1161 void plugin_read_all (void)
1162 {
1163 uc_check_timeout ();
1165 return;
1166 } /* void plugin_read_all */
1168 /* Read function called when the `-T' command line argument is given. */
1169 int plugin_read_all_once (void)
1170 {
1171 int status;
1172 int return_status = 0;
1174 if (read_heap == NULL)
1175 {
1176 NOTICE ("No read-functions are registered.");
1177 return (0);
1178 }
1180 while (42)
1181 {
1182 read_func_t *rf;
1184 rf = c_heap_get_root (read_heap);
1185 if (rf == NULL)
1186 break;
1188 if (rf->rf_type == RF_SIMPLE)
1189 {
1190 int (*callback) (void);
1192 callback = rf->rf_callback;
1193 status = (*callback) ();
1194 }
1195 else
1196 {
1197 plugin_read_cb callback;
1199 callback = rf->rf_callback;
1200 status = (*callback) (&rf->rf_udata);
1201 }
1203 if (status != 0)
1204 {
1205 NOTICE ("read-function of plugin `%s' failed.",
1206 rf->rf_name);
1207 return_status = -1;
1208 }
1210 destroy_callback ((void *) rf);
1211 }
1213 return (return_status);
1214 } /* int plugin_read_all_once */
1216 int plugin_write (const char *plugin, /* {{{ */
1217 const data_set_t *ds, const value_list_t *vl)
1218 {
1219 llentry_t *le;
1220 int status;
1222 if (vl == NULL)
1223 return (EINVAL);
1225 if (list_write == NULL)
1226 return (ENOENT);
1228 if (ds == NULL)
1229 {
1230 ds = plugin_get_ds (vl->type);
1231 if (ds == NULL)
1232 {
1233 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1234 return (ENOENT);
1235 }
1236 }
1238 if (plugin == NULL)
1239 {
1240 int success = 0;
1241 int failure = 0;
1243 le = llist_head (list_write);
1244 while (le != NULL)
1245 {
1246 callback_func_t *cf = le->value;
1247 plugin_write_cb callback;
1249 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1250 callback = cf->cf_callback;
1251 status = (*callback) (ds, vl, &cf->cf_udata);
1252 if (status != 0)
1253 failure++;
1254 else
1255 success++;
1257 le = le->next;
1258 }
1260 if ((success == 0) && (failure != 0))
1261 status = -1;
1262 else
1263 status = 0;
1264 }
1265 else /* plugin != NULL */
1266 {
1267 callback_func_t *cf;
1268 plugin_write_cb callback;
1270 le = llist_head (list_write);
1271 while (le != NULL)
1272 {
1273 if (strcasecmp (plugin, le->key) == 0)
1274 break;
1276 le = le->next;
1277 }
1279 if (le == NULL)
1280 return (ENOENT);
1282 cf = le->value;
1284 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1285 callback = cf->cf_callback;
1286 status = (*callback) (ds, vl, &cf->cf_udata);
1287 }
1289 return (status);
1290 } /* }}} int plugin_write */
1292 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1293 {
1294 llentry_t *le;
1296 if (list_flush == NULL)
1297 return (0);
1299 le = llist_head (list_flush);
1300 while (le != NULL)
1301 {
1302 callback_func_t *cf;
1303 plugin_flush_cb callback;
1305 if ((plugin != NULL)
1306 && (strcmp (plugin, le->key) != 0))
1307 {
1308 le = le->next;
1309 continue;
1310 }
1312 cf = le->value;
1313 callback = cf->cf_callback;
1315 (*callback) (timeout, identifier, &cf->cf_udata);
1317 le = le->next;
1318 }
1319 return (0);
1320 } /* int plugin_flush */
1322 void plugin_shutdown_all (void)
1323 {
1324 llentry_t *le;
1326 stop_read_threads ();
1328 destroy_all_callbacks (&list_init);
1330 pthread_mutex_lock (&read_lock);
1331 llist_destroy (read_list);
1332 read_list = NULL;
1333 pthread_mutex_unlock (&read_lock);
1335 destroy_read_heap ();
1337 plugin_flush (/* plugin = */ NULL,
1338 /* timeout = */ 0,
1339 /* identifier = */ NULL);
1341 le = NULL;
1342 if (list_shutdown != NULL)
1343 le = llist_head (list_shutdown);
1345 while (le != NULL)
1346 {
1347 callback_func_t *cf;
1348 plugin_shutdown_cb callback;
1350 cf = le->value;
1351 callback = cf->cf_callback;
1353 /* Advance the pointer before calling the callback allows
1354 * shutdown functions to unregister themselves. If done the
1355 * other way around the memory `le' points to will be freed
1356 * after callback returns. */
1357 le = le->next;
1359 (*callback) ();
1360 }
1362 /* Write plugins which use the `user_data' pointer usually need the
1363 * same data available to the flush callback. If this is the case, set
1364 * the free_function to NULL when registering the flush callback and to
1365 * the real free function when registering the write callback. This way
1366 * the data isn't freed twice. */
1367 destroy_all_callbacks (&list_flush);
1368 destroy_all_callbacks (&list_missing);
1369 destroy_all_callbacks (&list_write);
1371 destroy_all_callbacks (&list_notification);
1372 destroy_all_callbacks (&list_shutdown);
1373 destroy_all_callbacks (&list_log);
1374 } /* void plugin_shutdown_all */
1376 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1377 {
1378 llentry_t *le;
1380 if (list_missing == NULL)
1381 return (0);
1383 le = llist_head (list_missing);
1384 while (le != NULL)
1385 {
1386 callback_func_t *cf;
1387 plugin_missing_cb callback;
1388 int status;
1390 cf = le->value;
1391 callback = cf->cf_callback;
1393 status = (*callback) (vl, &cf->cf_udata);
1394 if (status != 0)
1395 {
1396 if (status < 0)
1397 {
1398 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1399 "failed with status %i.",
1400 le->key, status);
1401 return (status);
1402 }
1403 else
1404 {
1405 return (0);
1406 }
1407 }
1409 le = le->next;
1410 }
1411 return (0);
1412 } /* int }}} plugin_dispatch_missing */
1414 int plugin_dispatch_values (value_list_t *vl)
1415 {
1416 int status;
1417 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1419 value_t *saved_values;
1420 int saved_values_len;
1422 data_set_t *ds;
1424 int free_meta_data = 0;
1426 if ((vl == NULL) || (vl->type[0] == 0)
1427 || (vl->values == NULL) || (vl->values_len < 1))
1428 {
1429 ERROR ("plugin_dispatch_values: Invalid value list "
1430 "from plugin %s.", vl->plugin);
1431 return (-1);
1432 }
1434 /* Free meta data only if the calling function didn't specify any. In
1435 * this case matches and targets may add some and the calling function
1436 * may not expect (and therefore free) that data. */
1437 if (vl->meta == NULL)
1438 free_meta_data = 1;
1440 if (list_write == NULL)
1441 c_complain_once (LOG_WARNING, &no_write_complaint,
1442 "plugin_dispatch_values: No write callback has been "
1443 "registered. Please load at least one output plugin, "
1444 "if you want the collected data to be stored.");
1446 if (data_sets == NULL)
1447 {
1448 ERROR ("plugin_dispatch_values: No data sets registered. "
1449 "Could the types database be read? Check "
1450 "your `TypesDB' setting!");
1451 return (-1);
1452 }
1454 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1455 {
1456 char ident[6 * DATA_MAX_NAME_LEN];
1458 FORMAT_VL (ident, sizeof (ident), vl);
1459 INFO ("plugin_dispatch_values: Dataset not found: %s "
1460 "(from \"%s\"), check your types.db!",
1461 vl->type, ident);
1462 return (-1);
1463 }
1465 if (vl->time == 0)
1466 vl->time = cdtime ();
1468 if (vl->interval <= 0)
1469 vl->interval = interval_g;
1471 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1472 "host = %s; "
1473 "plugin = %s; plugin_instance = %s; "
1474 "type = %s; type_instance = %s;",
1475 CDTIME_T_TO_DOUBLE (vl->time),
1476 CDTIME_T_TO_DOUBLE (vl->interval),
1477 vl->host,
1478 vl->plugin, vl->plugin_instance,
1479 vl->type, vl->type_instance);
1481 #if COLLECT_DEBUG
1482 assert (0 == strcmp (ds->type, vl->type));
1483 #else
1484 if (0 != strcmp (ds->type, vl->type))
1485 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1486 ds->type, vl->type);
1487 #endif
1489 #if COLLECT_DEBUG
1490 assert (ds->ds_num == vl->values_len);
1491 #else
1492 if (ds->ds_num != vl->values_len)
1493 {
1494 ERROR ("plugin_dispatch_values: ds->type = %s: "
1495 "(ds->ds_num = %i) != "
1496 "(vl->values_len = %i)",
1497 ds->type, ds->ds_num, vl->values_len);
1498 return (-1);
1499 }
1500 #endif
1502 escape_slashes (vl->host, sizeof (vl->host));
1503 escape_slashes (vl->plugin, sizeof (vl->plugin));
1504 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1505 escape_slashes (vl->type, sizeof (vl->type));
1506 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1508 /* Copy the values. This way, we can assure `targets' that they get
1509 * dynamically allocated values, which they can free and replace if
1510 * they like. */
1511 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1512 {
1513 saved_values = vl->values;
1514 saved_values_len = vl->values_len;
1516 vl->values = (value_t *) calloc (vl->values_len,
1517 sizeof (*vl->values));
1518 if (vl->values == NULL)
1519 {
1520 ERROR ("plugin_dispatch_values: calloc failed.");
1521 vl->values = saved_values;
1522 return (-1);
1523 }
1524 memcpy (vl->values, saved_values,
1525 vl->values_len * sizeof (*vl->values));
1526 }
1527 else /* if ((pre == NULL) && (post == NULL)) */
1528 {
1529 saved_values = NULL;
1530 saved_values_len = 0;
1531 }
1533 if (pre_cache_chain != NULL)
1534 {
1535 status = fc_process_chain (ds, vl, pre_cache_chain);
1536 if (status < 0)
1537 {
1538 WARNING ("plugin_dispatch_values: Running the "
1539 "pre-cache chain failed with "
1540 "status %i (%#x).",
1541 status, status);
1542 }
1543 else if (status == FC_TARGET_STOP)
1544 {
1545 /* Restore the state of the value_list so that plugins
1546 * don't get confused.. */
1547 if (saved_values != NULL)
1548 {
1549 free (vl->values);
1550 vl->values = saved_values;
1551 vl->values_len = saved_values_len;
1552 }
1553 return (0);
1554 }
1555 }
1557 /* Update the value cache */
1558 uc_update (ds, vl);
1560 if (post_cache_chain != NULL)
1561 {
1562 status = fc_process_chain (ds, vl, post_cache_chain);
1563 if (status < 0)
1564 {
1565 WARNING ("plugin_dispatch_values: Running the "
1566 "post-cache chain failed with "
1567 "status %i (%#x).",
1568 status, status);
1569 }
1570 }
1571 else
1572 fc_default_action (ds, vl);
1574 /* Restore the state of the value_list so that plugins don't get
1575 * confused.. */
1576 if (saved_values != NULL)
1577 {
1578 free (vl->values);
1579 vl->values = saved_values;
1580 vl->values_len = saved_values_len;
1581 }
1583 if ((free_meta_data != 0) && (vl->meta != NULL))
1584 {
1585 meta_data_destroy (vl->meta);
1586 vl->meta = NULL;
1587 }
1589 return (0);
1590 } /* int plugin_dispatch_values */
1592 int plugin_dispatch_values_secure (const value_list_t *vl)
1593 {
1594 value_list_t vl_copy;
1595 int status;
1597 if (vl == NULL)
1598 return EINVAL;
1600 memcpy (&vl_copy, vl, sizeof (vl_copy));
1602 /* Write callbacks must not change the values and meta pointers, so we can
1603 * savely skip copying those and make this more efficient. */
1604 if ((pre_cache_chain == NULL) && (post_cache_chain == NULL))
1605 return (plugin_dispatch_values (&vl_copy));
1607 /* Set pointers to NULL, just to be on the save side. */
1608 vl_copy.values = NULL;
1609 vl_copy.meta = NULL;
1611 vl_copy.values = malloc (sizeof (*vl_copy.values) * vl->values_len);
1612 if (vl_copy.values == NULL)
1613 {
1614 ERROR ("plugin_dispatch_values_secure: malloc failed.");
1615 return (ENOMEM);
1616 }
1617 memcpy (vl_copy.values, vl->values, sizeof (*vl_copy.values) * vl->values_len);
1619 if (vl->meta != NULL)
1620 {
1621 vl_copy.meta = meta_data_clone (vl->meta);
1622 if (vl_copy.meta == NULL)
1623 {
1624 ERROR ("plugin_dispatch_values_secure: meta_data_clone failed.");
1625 free (vl_copy.values);
1626 return (ENOMEM);
1627 }
1628 } /* if (vl->meta) */
1630 status = plugin_dispatch_values (&vl_copy);
1632 meta_data_destroy (vl_copy.meta);
1633 free (vl_copy.values);
1635 return (status);
1636 } /* int plugin_dispatch_values_secure */
1638 int plugin_dispatch_notification (const notification_t *notif)
1639 {
1640 llentry_t *le;
1641 /* Possible TODO: Add flap detection here */
1643 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1644 "time = %.3f; host = %s;",
1645 notif->severity, notif->message,
1646 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1648 /* Nobody cares for notifications */
1649 if (list_notification == NULL)
1650 return (-1);
1652 le = llist_head (list_notification);
1653 while (le != NULL)
1654 {
1655 callback_func_t *cf;
1656 plugin_notification_cb callback;
1657 int status;
1659 cf = le->value;
1660 callback = cf->cf_callback;
1661 status = (*callback) (notif, &cf->cf_udata);
1662 if (status != 0)
1663 {
1664 WARNING ("plugin_dispatch_notification: Notification "
1665 "callback %s returned %i.",
1666 le->key, status);
1667 }
1669 le = le->next;
1670 }
1672 return (0);
1673 } /* int plugin_dispatch_notification */
1675 void plugin_log (int level, const char *format, ...)
1676 {
1677 char msg[1024];
1678 va_list ap;
1679 llentry_t *le;
1681 #if !COLLECT_DEBUG
1682 if (level >= LOG_DEBUG)
1683 return;
1684 #endif
1686 va_start (ap, format);
1687 vsnprintf (msg, sizeof (msg), format, ap);
1688 msg[sizeof (msg) - 1] = '\0';
1689 va_end (ap);
1691 if (list_log == NULL)
1692 {
1693 fprintf (stderr, "%s\n", msg);
1694 return;
1695 }
1697 le = llist_head (list_log);
1698 while (le != NULL)
1699 {
1700 callback_func_t *cf;
1701 plugin_log_cb callback;
1703 cf = le->value;
1704 callback = cf->cf_callback;
1706 (*callback) (level, msg, &cf->cf_udata);
1708 le = le->next;
1709 }
1710 } /* void plugin_log */
1712 int parse_log_severity (const char *severity)
1713 {
1714 int log_level = -1;
1716 if ((0 == strcasecmp (severity, "emerg"))
1717 || (0 == strcasecmp (severity, "alert"))
1718 || (0 == strcasecmp (severity, "crit"))
1719 || (0 == strcasecmp (severity, "err")))
1720 log_level = LOG_ERR;
1721 else if (0 == strcasecmp (severity, "warning"))
1722 log_level = LOG_WARNING;
1723 else if (0 == strcasecmp (severity, "notice"))
1724 log_level = LOG_NOTICE;
1725 else if (0 == strcasecmp (severity, "info"))
1726 log_level = LOG_INFO;
1727 #if COLLECT_DEBUG
1728 else if (0 == strcasecmp (severity, "debug"))
1729 log_level = LOG_DEBUG;
1730 #endif /* COLLECT_DEBUG */
1732 return (log_level);
1733 } /* int parse_log_severity */
1735 int parse_notif_severity (const char *severity)
1736 {
1737 int notif_severity = -1;
1739 if (strcasecmp (severity, "FAILURE") == 0)
1740 notif_severity = NOTIF_FAILURE;
1741 else if (strcmp (severity, "OKAY") == 0)
1742 notif_severity = NOTIF_OKAY;
1743 else if ((strcmp (severity, "WARNING") == 0)
1744 || (strcmp (severity, "WARN") == 0))
1745 notif_severity = NOTIF_WARNING;
1747 return (notif_severity);
1748 } /* int parse_notif_severity */
1750 const data_set_t *plugin_get_ds (const char *name)
1751 {
1752 data_set_t *ds;
1754 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1755 {
1756 DEBUG ("No such dataset registered: %s", name);
1757 return (NULL);
1758 }
1760 return (ds);
1761 } /* data_set_t *plugin_get_ds */
1763 static int plugin_notification_meta_add (notification_t *n,
1764 const char *name,
1765 enum notification_meta_type_e type,
1766 const void *value)
1767 {
1768 notification_meta_t *meta;
1769 notification_meta_t *tail;
1771 if ((n == NULL) || (name == NULL) || (value == NULL))
1772 {
1773 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1774 return (-1);
1775 }
1777 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1778 if (meta == NULL)
1779 {
1780 ERROR ("plugin_notification_meta_add: malloc failed.");
1781 return (-1);
1782 }
1783 memset (meta, 0, sizeof (notification_meta_t));
1785 sstrncpy (meta->name, name, sizeof (meta->name));
1786 meta->type = type;
1788 switch (type)
1789 {
1790 case NM_TYPE_STRING:
1791 {
1792 meta->nm_value.nm_string = strdup ((const char *) value);
1793 if (meta->nm_value.nm_string == NULL)
1794 {
1795 ERROR ("plugin_notification_meta_add: strdup failed.");
1796 sfree (meta);
1797 return (-1);
1798 }
1799 break;
1800 }
1801 case NM_TYPE_SIGNED_INT:
1802 {
1803 meta->nm_value.nm_signed_int = *((int64_t *) value);
1804 break;
1805 }
1806 case NM_TYPE_UNSIGNED_INT:
1807 {
1808 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1809 break;
1810 }
1811 case NM_TYPE_DOUBLE:
1812 {
1813 meta->nm_value.nm_double = *((double *) value);
1814 break;
1815 }
1816 case NM_TYPE_BOOLEAN:
1817 {
1818 meta->nm_value.nm_boolean = *((_Bool *) value);
1819 break;
1820 }
1821 default:
1822 {
1823 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1824 sfree (meta);
1825 return (-1);
1826 }
1827 } /* switch (type) */
1829 meta->next = NULL;
1830 tail = n->meta;
1831 while ((tail != NULL) && (tail->next != NULL))
1832 tail = tail->next;
1834 if (tail == NULL)
1835 n->meta = meta;
1836 else
1837 tail->next = meta;
1839 return (0);
1840 } /* int plugin_notification_meta_add */
1842 int plugin_notification_meta_add_string (notification_t *n,
1843 const char *name,
1844 const char *value)
1845 {
1846 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1847 }
1849 int plugin_notification_meta_add_signed_int (notification_t *n,
1850 const char *name,
1851 int64_t value)
1852 {
1853 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1854 }
1856 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1857 const char *name,
1858 uint64_t value)
1859 {
1860 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1861 }
1863 int plugin_notification_meta_add_double (notification_t *n,
1864 const char *name,
1865 double value)
1866 {
1867 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1868 }
1870 int plugin_notification_meta_add_boolean (notification_t *n,
1871 const char *name,
1872 _Bool value)
1873 {
1874 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1875 }
1877 int plugin_notification_meta_copy (notification_t *dst,
1878 const notification_t *src)
1879 {
1880 notification_meta_t *meta;
1882 assert (dst != NULL);
1883 assert (src != NULL);
1884 assert (dst != src);
1885 assert ((src->meta == NULL) || (src->meta != dst->meta));
1887 for (meta = src->meta; meta != NULL; meta = meta->next)
1888 {
1889 if (meta->type == NM_TYPE_STRING)
1890 plugin_notification_meta_add_string (dst, meta->name,
1891 meta->nm_value.nm_string);
1892 else if (meta->type == NM_TYPE_SIGNED_INT)
1893 plugin_notification_meta_add_signed_int (dst, meta->name,
1894 meta->nm_value.nm_signed_int);
1895 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1896 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1897 meta->nm_value.nm_unsigned_int);
1898 else if (meta->type == NM_TYPE_DOUBLE)
1899 plugin_notification_meta_add_double (dst, meta->name,
1900 meta->nm_value.nm_double);
1901 else if (meta->type == NM_TYPE_BOOLEAN)
1902 plugin_notification_meta_add_boolean (dst, meta->name,
1903 meta->nm_value.nm_boolean);
1904 }
1906 return (0);
1907 } /* int plugin_notification_meta_copy */
1909 int plugin_notification_meta_free (notification_meta_t *n)
1910 {
1911 notification_meta_t *this;
1912 notification_meta_t *next;
1914 if (n == NULL)
1915 {
1916 ERROR ("plugin_notification_meta_free: n == NULL!");
1917 return (-1);
1918 }
1920 this = n;
1921 while (this != NULL)
1922 {
1923 next = this->next;
1925 if (this->type == NM_TYPE_STRING)
1926 {
1927 free ((char *)this->nm_value.nm_string);
1928 this->nm_value.nm_string = NULL;
1929 }
1930 sfree (this);
1932 this = next;
1933 }
1935 return (0);
1936 } /* int plugin_notification_meta_free */
1938 /* vim: set sw=8 ts=8 noet fdm=marker : */