1 /**
2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 * Sebastian Harl <sh at tokkee.org>
21 **/
23 #include "collectd.h"
24 #include "utils_complain.h"
26 #include <ltdl.h>
28 #if HAVE_PTHREAD_H
29 # include <pthread.h>
30 #endif
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_cache.h"
38 #include "utils_threshold.h"
39 #include "filter_chain.h"
41 /*
42 * Private structures
43 */
44 struct read_func_s
45 {
46 int wait_time;
47 int wait_left;
48 int (*callback) (void);
49 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
50 };
51 typedef struct read_func_s read_func_t;
53 /*
54 * Private variables
55 */
56 static llist_t *list_init;
57 static llist_t *list_read;
58 static llist_t *list_write;
59 static llist_t *list_flush;
60 static llist_t *list_shutdown;
61 static llist_t *list_log;
62 static llist_t *list_notification;
64 static fc_chain_t *pre_cache_chain = NULL;
65 static fc_chain_t *post_cache_chain = NULL;
67 static c_avl_tree_t *data_sets;
69 static char *plugindir = NULL;
71 static int read_loop = 1;
72 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
73 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
74 static pthread_t *read_threads = NULL;
75 static int read_threads_num = 0;
77 /*
78 * Static functions
79 */
80 static const char *plugin_get_dir (void)
81 {
82 if (plugindir == NULL)
83 return (PLUGINDIR);
84 else
85 return (plugindir);
86 }
88 static int register_callback (llist_t **list, const char *name, void *callback)
89 {
90 llentry_t *le;
91 char *key;
93 if ((*list == NULL)
94 && ((*list = llist_create ()) == NULL))
95 return (-1);
97 le = llist_search (*list, name);
98 if (le == NULL)
99 {
100 key = strdup (name);
101 if (key == NULL)
102 return (-1);
104 le = llentry_create (key, callback);
105 if (le == NULL)
106 {
107 free (key);
108 return (-1);
109 }
111 llist_append (*list, le);
112 }
113 else
114 {
115 le->value = callback;
116 }
118 return (0);
119 } /* int register_callback */
121 static int plugin_unregister (llist_t *list, const char *name)
122 {
123 llentry_t *e;
125 e = llist_search (list, name);
127 if (e == NULL)
128 return (-1);
130 llist_remove (list, e);
131 free (e->key);
132 llentry_destroy (e);
134 return (0);
135 } /* int plugin_unregister */
137 /*
138 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
139 * object, but it will bitch about a shared object not having a
140 * ``module_register'' symbol..
141 */
142 static int plugin_load_file (char *file)
143 {
144 lt_dlhandle dlh;
145 void (*reg_handle) (void);
147 DEBUG ("file = %s", file);
149 lt_dlinit ();
150 lt_dlerror (); /* clear errors */
152 if ((dlh = lt_dlopen (file)) == NULL)
153 {
154 const char *error = lt_dlerror ();
156 ERROR ("lt_dlopen (%s) failed: %s", file, error);
157 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
158 return (1);
159 }
161 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
162 {
163 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
164 file, lt_dlerror ());
165 lt_dlclose (dlh);
166 return (-1);
167 }
169 (*reg_handle) ();
171 return (0);
172 }
174 static void *plugin_read_thread (void *args)
175 {
176 llentry_t *le;
177 read_func_t *rf;
178 int status;
179 int done;
181 pthread_mutex_lock (&read_lock);
183 while (read_loop != 0)
184 {
185 le = llist_head (list_read);
186 done = 0;
188 while ((read_loop != 0) && (le != NULL))
189 {
190 rf = (read_func_t *) le->value;
192 if (rf->needs_read != TODO)
193 {
194 le = le->next;
195 continue;
196 }
198 /* We will do this read function */
199 rf->needs_read = ACTIVE;
201 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
202 (unsigned long int) pthread_self (), le->key);
203 pthread_mutex_unlock (&read_lock);
205 status = rf->callback ();
206 done++;
208 if (status != 0)
209 {
210 if (rf->wait_time < interval_g)
211 rf->wait_time = interval_g;
212 rf->wait_left = rf->wait_time;
213 rf->wait_time = rf->wait_time * 2;
214 if (rf->wait_time > 86400)
215 rf->wait_time = 86400;
217 NOTICE ("read-function of plugin `%s' "
218 "failed. Will suspend it for %i "
219 "seconds.", le->key, rf->wait_left);
220 }
221 else
222 {
223 rf->wait_left = 0;
224 rf->wait_time = interval_g;
225 }
227 pthread_mutex_lock (&read_lock);
229 rf->needs_read = DONE;
230 le = le->next;
231 } /* while (le != NULL) */
233 if ((read_loop != 0) && (done == 0))
234 {
235 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
236 (unsigned long int) pthread_self ());
237 pthread_cond_wait (&read_cond, &read_lock);
238 }
239 } /* while (read_loop) */
241 pthread_mutex_unlock (&read_lock);
243 pthread_exit (NULL);
244 return ((void *) 0);
245 } /* void *plugin_read_thread */
247 static void start_threads (int num)
248 {
249 int i;
251 if (read_threads != NULL)
252 return;
254 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
255 if (read_threads == NULL)
256 {
257 ERROR ("plugin: start_threads: calloc failed.");
258 return;
259 }
261 read_threads_num = 0;
262 for (i = 0; i < num; i++)
263 {
264 if (pthread_create (read_threads + read_threads_num, NULL,
265 plugin_read_thread, NULL) == 0)
266 {
267 read_threads_num++;
268 }
269 else
270 {
271 ERROR ("plugin: start_threads: pthread_create failed.");
272 return;
273 }
274 } /* for (i) */
275 } /* void start_threads */
277 static void stop_threads (void)
278 {
279 int i;
281 if (read_threads == NULL)
282 return;
284 pthread_mutex_lock (&read_lock);
285 read_loop = 0;
286 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
287 pthread_cond_broadcast (&read_cond);
288 pthread_mutex_unlock (&read_lock);
290 for (i = 0; i < read_threads_num; i++)
291 {
292 if (pthread_join (read_threads[i], NULL) != 0)
293 {
294 ERROR ("plugin: stop_threads: pthread_join failed.");
295 }
296 read_threads[i] = (pthread_t) 0;
297 }
298 sfree (read_threads);
299 read_threads_num = 0;
300 } /* void stop_threads */
302 /*
303 * Public functions
304 */
305 void plugin_set_dir (const char *dir)
306 {
307 if (plugindir != NULL)
308 free (plugindir);
310 if (dir == NULL)
311 plugindir = NULL;
312 else if ((plugindir = strdup (dir)) == NULL)
313 {
314 char errbuf[1024];
315 ERROR ("strdup failed: %s",
316 sstrerror (errno, errbuf, sizeof (errbuf)));
317 }
318 }
320 #define BUFSIZE 512
321 int plugin_load (const char *type)
322 {
323 DIR *dh;
324 const char *dir;
325 char filename[BUFSIZE] = "";
326 char typename[BUFSIZE];
327 int typename_len;
328 int ret;
329 struct stat statbuf;
330 struct dirent *de;
332 DEBUG ("type = %s", type);
334 dir = plugin_get_dir ();
335 ret = 1;
337 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
338 * type when matching the filename */
339 if (ssnprintf (typename, sizeof (typename),
340 "%s.so", type) >= sizeof (typename))
341 {
342 WARNING ("snprintf: truncated: `%s.so'", type);
343 return (-1);
344 }
345 typename_len = strlen (typename);
347 if ((dh = opendir (dir)) == NULL)
348 {
349 char errbuf[1024];
350 ERROR ("opendir (%s): %s", dir,
351 sstrerror (errno, errbuf, sizeof (errbuf)));
352 return (-1);
353 }
355 while ((de = readdir (dh)) != NULL)
356 {
357 if (strncasecmp (de->d_name, typename, typename_len))
358 continue;
360 if (ssnprintf (filename, sizeof (filename),
361 "%s/%s", dir, de->d_name) >= sizeof (filename))
362 {
363 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
364 continue;
365 }
367 if (lstat (filename, &statbuf) == -1)
368 {
369 char errbuf[1024];
370 WARNING ("stat %s: %s", filename,
371 sstrerror (errno, errbuf, sizeof (errbuf)));
372 continue;
373 }
374 else if (!S_ISREG (statbuf.st_mode))
375 {
376 /* don't follow symlinks */
377 continue;
378 }
380 if (plugin_load_file (filename) == 0)
381 {
382 /* success */
383 ret = 0;
384 break;
385 }
386 else
387 {
388 fprintf (stderr, "Unable to load plugin %s.\n", type);
389 }
390 }
392 closedir (dh);
394 if (filename[0] == '\0')
395 fprintf (stderr, "Could not find plugin %s.\n", type);
397 return (ret);
398 }
400 /*
401 * The `register_*' functions follow
402 */
403 int plugin_register_config (const char *name,
404 int (*callback) (const char *key, const char *val),
405 const char **keys, int keys_num)
406 {
407 cf_register (name, callback, keys, keys_num);
408 return (0);
409 } /* int plugin_register_config */
411 int plugin_register_complex_config (const char *type,
412 int (*callback) (oconfig_item_t *))
413 {
414 return (cf_register_complex (type, callback));
415 } /* int plugin_register_complex_config */
417 int plugin_register_init (const char *name,
418 int (*callback) (void))
419 {
420 return (register_callback (&list_init, name, (void *) callback));
421 } /* plugin_register_init */
423 int plugin_register_read (const char *name,
424 int (*callback) (void))
425 {
426 read_func_t *rf;
428 rf = (read_func_t *) malloc (sizeof (read_func_t));
429 if (rf == NULL)
430 {
431 char errbuf[1024];
432 ERROR ("plugin_register_read: malloc failed: %s",
433 sstrerror (errno, errbuf, sizeof (errbuf)));
434 return (-1);
435 }
437 memset (rf, '\0', sizeof (read_func_t));
438 rf->wait_time = interval_g;
439 rf->wait_left = 0;
440 rf->callback = callback;
441 rf->needs_read = DONE;
443 return (register_callback (&list_read, name, (void *) rf));
444 } /* int plugin_register_read */
446 int plugin_register_write (const char *name,
447 int (*callback) (const data_set_t *ds, const value_list_t *vl))
448 {
449 return (register_callback (&list_write, name, (void *) callback));
450 } /* int plugin_register_write */
452 int plugin_register_flush (const char *name,
453 int (*callback) (const int timeout, const char *identifier))
454 {
455 return (register_callback (&list_flush, name, (void *) callback));
456 } /* int plugin_register_flush */
458 int plugin_register_shutdown (char *name,
459 int (*callback) (void))
460 {
461 return (register_callback (&list_shutdown, name, (void *) callback));
462 } /* int plugin_register_shutdown */
464 int plugin_register_data_set (const data_set_t *ds)
465 {
466 data_set_t *ds_copy;
467 int i;
469 if ((data_sets != NULL)
470 && (c_avl_get (data_sets, ds->type, NULL) == 0))
471 {
472 NOTICE ("Replacing DS `%s' with another version.", ds->type);
473 plugin_unregister_data_set (ds->type);
474 }
475 else if (data_sets == NULL)
476 {
477 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
478 if (data_sets == NULL)
479 return (-1);
480 }
482 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
483 if (ds_copy == NULL)
484 return (-1);
485 memcpy(ds_copy, ds, sizeof (data_set_t));
487 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
488 * ds->ds_num);
489 if (ds_copy->ds == NULL)
490 {
491 free (ds_copy);
492 return (-1);
493 }
495 for (i = 0; i < ds->ds_num; i++)
496 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
498 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
499 } /* int plugin_register_data_set */
501 int plugin_register_log (char *name,
502 void (*callback) (int priority, const char *msg))
503 {
504 return (register_callback (&list_log, name, (void *) callback));
505 } /* int plugin_register_log */
507 int plugin_register_notification (const char *name,
508 int (*callback) (const notification_t *notif))
509 {
510 return (register_callback (&list_notification, name, (void *) callback));
511 } /* int plugin_register_log */
513 int plugin_unregister_config (const char *name)
514 {
515 cf_unregister (name);
516 return (0);
517 } /* int plugin_unregister_config */
519 int plugin_unregister_complex_config (const char *name)
520 {
521 cf_unregister_complex (name);
522 return (0);
523 } /* int plugin_unregister_complex_config */
525 int plugin_unregister_init (const char *name)
526 {
527 return (plugin_unregister (list_init, name));
528 }
530 int plugin_unregister_read (const char *name)
531 {
532 llentry_t *e;
534 e = llist_search (list_read, name);
536 if (e == NULL)
537 return (-1);
539 llist_remove (list_read, e);
540 free (e->value);
541 free (e->key);
542 llentry_destroy (e);
544 return (0);
545 }
547 int plugin_unregister_write (const char *name)
548 {
549 return (plugin_unregister (list_write, name));
550 }
552 int plugin_unregister_flush (const char *name)
553 {
554 return (plugin_unregister (list_flush, name));
555 }
557 int plugin_unregister_shutdown (const char *name)
558 {
559 return (plugin_unregister (list_shutdown, name));
560 }
562 int plugin_unregister_data_set (const char *name)
563 {
564 data_set_t *ds;
566 if (data_sets == NULL)
567 return (-1);
569 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
570 return (-1);
572 sfree (ds->ds);
573 sfree (ds);
575 return (0);
576 } /* int plugin_unregister_data_set */
578 int plugin_unregister_log (const char *name)
579 {
580 return (plugin_unregister (list_log, name));
581 }
583 int plugin_unregister_notification (const char *name)
584 {
585 return (plugin_unregister (list_notification, name));
586 }
588 void plugin_init_all (void)
589 {
590 const char *chain_name;
591 int (*callback) (void);
592 llentry_t *le;
593 int status;
595 /* Init the value cache */
596 uc_init ();
598 chain_name = global_option_get ("PreCacheChain");
599 pre_cache_chain = fc_chain_get_by_name (chain_name);
601 chain_name = global_option_get ("PostCacheChain");
602 post_cache_chain = fc_chain_get_by_name (chain_name);
605 if ((list_init == NULL) && (list_read == NULL))
606 return;
608 /* Calling all init callbacks before checking if read callbacks
609 * are available allows the init callbacks to register the read
610 * callback. */
611 le = llist_head (list_init);
612 while (le != NULL)
613 {
614 callback = (int (*) (void)) le->value;
615 status = (*callback) ();
617 if (status != 0)
618 {
619 ERROR ("Initialization of plugin `%s' "
620 "failed with status %i. "
621 "Plugin will be unloaded.",
622 le->key, status);
623 /* Plugins that register read callbacks from the init
624 * callback should take care of appropriate error
625 * handling themselves. */
626 /* FIXME: Unload _all_ functions */
627 plugin_unregister_read (le->key);
628 }
630 le = le->next;
631 }
633 /* Start read-threads */
634 if (list_read != NULL)
635 {
636 const char *rt;
637 int num;
638 rt = global_option_get ("ReadThreads");
639 num = atoi (rt);
640 if (num != -1)
641 start_threads ((num > 0) ? num : 5);
642 }
643 } /* void plugin_init_all */
645 void plugin_read_all (void)
646 {
647 llentry_t *le;
648 read_func_t *rf;
650 uc_check_timeout ();
652 if (list_read == NULL)
653 return;
655 pthread_mutex_lock (&read_lock);
657 le = llist_head (list_read);
658 while (le != NULL)
659 {
660 rf = (read_func_t *) le->value;
662 if (rf->needs_read != DONE)
663 {
664 le = le->next;
665 continue;
666 }
668 if (rf->wait_left > 0)
669 rf->wait_left -= interval_g;
671 if (rf->wait_left <= 0)
672 {
673 rf->needs_read = TODO;
674 }
676 le = le->next;
677 }
679 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
680 pthread_cond_broadcast (&read_cond);
681 pthread_mutex_unlock (&read_lock);
682 } /* void plugin_read_all */
684 /* Read function called when the `-T' command line argument is given. */
685 int plugin_read_all_once (void)
686 {
687 llentry_t *le;
688 read_func_t *rf;
689 int status;
690 int return_status = 0;
692 if (list_read == NULL)
693 {
694 NOTICE ("No read-functions are registered.");
695 return (0);
696 }
698 for (le = llist_head (list_read);
699 le != NULL;
700 le = le->next)
701 {
702 rf = (read_func_t *) le->value;
703 status = rf->callback ();
704 if (status != 0)
705 {
706 NOTICE ("read-function of plugin `%s' failed.",
707 le->key);
708 return_status = -1;
709 }
710 }
712 return (return_status);
713 } /* int plugin_read_all_once */
715 int plugin_write (const char *plugin, /* {{{ */
716 const data_set_t *ds, const value_list_t *vl)
717 {
718 int (*callback) (const data_set_t *ds, const value_list_t *vl);
719 llentry_t *le;
720 int status;
722 if (vl == NULL)
723 return (EINVAL);
725 if (list_write == NULL)
726 return (ENOENT);
728 if (ds == NULL)
729 {
730 ds = plugin_get_ds (vl->type);
731 if (ds == NULL)
732 {
733 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
734 return (ENOENT);
735 }
736 }
738 if (plugin == NULL)
739 {
740 int success = 0;
741 int failure = 0;
743 le = llist_head (list_write);
744 while (le != NULL)
745 {
746 callback = le->value;
747 status = (*callback) (ds, vl);
748 if (status != 0)
749 failure++;
750 else
751 success++;
753 le = le->next;
754 }
756 if ((success == 0) && (failure != 0))
757 status = -1;
758 else
759 status = 0;
760 }
761 else /* plugin != NULL */
762 {
763 le = llist_head (list_write);
764 while (le != NULL)
765 {
766 if (strcasecmp (plugin, le->key) == 0)
767 break;
769 le = le->next;
770 }
772 if (le == NULL)
773 return (ENOENT);
775 callback = le->value;
776 status = (*callback) (ds, vl);
777 }
779 return (status);
780 } /* }}} int plugin_write */
782 int plugin_flush (const char *plugin, int timeout, const char *identifier)
783 {
784 int (*callback) (int timeout, const char *identifier);
785 llentry_t *le;
787 if (list_flush == NULL)
788 return (0);
790 le = llist_head (list_flush);
791 while (le != NULL)
792 {
793 if ((plugin != NULL)
794 && (strcmp (plugin, le->key) != 0))
795 {
796 le = le->next;
797 continue;
798 }
800 callback = (int (*) (int, const char *)) le->value;
801 (*callback) (timeout, identifier);
803 le = le->next;
804 }
805 return (0);
806 } /* int plugin_flush */
808 void plugin_shutdown_all (void)
809 {
810 int (*callback) (void);
811 llentry_t *le;
813 stop_threads ();
815 if (list_shutdown == NULL)
816 return;
818 le = llist_head (list_shutdown);
819 while (le != NULL)
820 {
821 callback = (int (*) (void)) le->value;
823 /* Advance the pointer before calling the callback allows
824 * shutdown functions to unregister themselves. If done the
825 * other way around the memory `le' points to will be freed
826 * after callback returns. */
827 le = le->next;
829 (*callback) ();
830 }
831 } /* void plugin_shutdown_all */
833 int plugin_dispatch_values (value_list_t *vl)
834 {
835 int status;
836 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
838 data_set_t *ds;
840 if ((vl == NULL) || (*vl->type == '\0')) {
841 ERROR ("plugin_dispatch_values: Invalid value list.");
842 return (-1);
843 }
845 if (list_write == NULL)
846 c_complain_once (LOG_WARNING, &no_write_complaint,
847 "plugin_dispatch_values: No write callback has been "
848 "registered. Please load at least one output plugin, "
849 "if you want the collected data to be stored.");
851 if (data_sets == NULL)
852 {
853 ERROR ("plugin_dispatch_values: No data sets registered. "
854 "Could the types database be read? Check "
855 "your `TypesDB' setting!");
856 return (-1);
857 }
859 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
860 {
861 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
862 return (-1);
863 }
865 if (vl->time == 0)
866 vl->time = time (NULL);
868 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
869 "host = %s; "
870 "plugin = %s; plugin_instance = %s; "
871 "type = %s; type_instance = %s;",
872 (unsigned int) vl->time, vl->interval,
873 vl->host,
874 vl->plugin, vl->plugin_instance,
875 vl->type, vl->type_instance);
877 #if COLLECT_DEBUG
878 assert (0 == strcmp (ds->type, vl->type));
879 #else
880 if (0 != strcmp (ds->type, vl->type))
881 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
882 ds->type, vl->type);
883 #endif
885 #if COLLECT_DEBUG
886 assert (ds->ds_num == vl->values_len);
887 #else
888 if (ds->ds_num != vl->values_len)
889 {
890 ERROR ("plugin_dispatch_values: ds->type = %s: "
891 "(ds->ds_num = %i) != "
892 "(vl->values_len = %i)",
893 ds->type, ds->ds_num, vl->values_len);
894 return (-1);
895 }
896 #endif
898 escape_slashes (vl->host, sizeof (vl->host));
899 escape_slashes (vl->plugin, sizeof (vl->plugin));
900 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
901 escape_slashes (vl->type, sizeof (vl->type));
902 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
904 if (pre_cache_chain != NULL)
905 {
906 status = fc_process_chain (ds, vl, pre_cache_chain);
907 if (status < 0)
908 {
909 WARNING ("plugin_dispatch_values: Running the "
910 "pre-cache chain failed with "
911 "status %i (%#x).",
912 status, status);
913 }
914 else if (status == FC_TARGET_STOP)
915 return (0);
916 }
918 /* Update the value cache */
919 uc_update (ds, vl);
921 if (post_cache_chain != NULL)
922 {
923 status = fc_process_chain (ds, vl, post_cache_chain);
924 if (status < 0)
925 {
926 WARNING ("plugin_dispatch_values: Running the "
927 "post-cache chain failed with "
928 "status %i (%#x).",
929 status, status);
930 }
931 }
932 else
933 fc_default_action (ds, vl);
935 return (0);
936 } /* int plugin_dispatch_values */
938 int plugin_dispatch_notification (const notification_t *notif)
939 {
940 int (*callback) (const notification_t *);
941 llentry_t *le;
942 /* Possible TODO: Add flap detection here */
944 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
945 "time = %u; host = %s;",
946 notif->severity, notif->message,
947 (unsigned int) notif->time, notif->host);
949 /* Nobody cares for notifications */
950 if (list_notification == NULL)
951 return (-1);
953 le = llist_head (list_notification);
954 while (le != NULL)
955 {
956 callback = (int (*) (const notification_t *)) le->value;
957 (*callback) (notif);
959 le = le->next;
960 }
962 return (0);
963 } /* int plugin_dispatch_notification */
965 void plugin_log (int level, const char *format, ...)
966 {
967 char msg[1024];
968 va_list ap;
970 void (*callback) (int, const char *);
971 llentry_t *le;
973 if (list_log == NULL)
974 return;
976 #if !COLLECT_DEBUG
977 if (level >= LOG_DEBUG)
978 return;
979 #endif
981 va_start (ap, format);
982 vsnprintf (msg, sizeof (msg), format, ap);
983 msg[sizeof (msg) - 1] = '\0';
984 va_end (ap);
986 le = llist_head (list_log);
987 while (le != NULL)
988 {
989 callback = (void (*) (int, const char *)) le->value;
990 (*callback) (level, msg);
992 le = le->next;
993 }
994 } /* void plugin_log */
996 const data_set_t *plugin_get_ds (const char *name)
997 {
998 data_set_t *ds;
1000 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1001 {
1002 DEBUG ("No such dataset registered: %s", name);
1003 return (NULL);
1004 }
1006 return (ds);
1007 } /* data_set_t *plugin_get_ds */
1009 static int plugin_notification_meta_add (notification_t *n,
1010 const char *name,
1011 enum notification_meta_type_e type,
1012 const void *value)
1013 {
1014 notification_meta_t *meta;
1015 notification_meta_t *tail;
1017 if ((n == NULL) || (name == NULL) || (value == NULL))
1018 {
1019 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1020 return (-1);
1021 }
1023 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1024 if (meta == NULL)
1025 {
1026 ERROR ("plugin_notification_meta_add: malloc failed.");
1027 return (-1);
1028 }
1029 memset (meta, 0, sizeof (notification_meta_t));
1031 sstrncpy (meta->name, name, sizeof (meta->name));
1032 meta->type = type;
1034 switch (type)
1035 {
1036 case NM_TYPE_STRING:
1037 {
1038 meta->nm_value.nm_string = strdup ((const char *) value);
1039 if (meta->nm_value.nm_string == NULL)
1040 {
1041 ERROR ("plugin_notification_meta_add: strdup failed.");
1042 sfree (meta);
1043 return (-1);
1044 }
1045 break;
1046 }
1047 case NM_TYPE_SIGNED_INT:
1048 {
1049 meta->nm_value.nm_signed_int = *((int64_t *) value);
1050 break;
1051 }
1052 case NM_TYPE_UNSIGNED_INT:
1053 {
1054 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1055 break;
1056 }
1057 case NM_TYPE_DOUBLE:
1058 {
1059 meta->nm_value.nm_double = *((double *) value);
1060 break;
1061 }
1062 case NM_TYPE_BOOLEAN:
1063 {
1064 meta->nm_value.nm_boolean = *((bool *) value);
1065 break;
1066 }
1067 default:
1068 {
1069 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1070 sfree (meta);
1071 return (-1);
1072 }
1073 } /* switch (type) */
1075 meta->next = NULL;
1076 tail = n->meta;
1077 while ((tail != NULL) && (tail->next != NULL))
1078 tail = tail->next;
1080 if (tail == NULL)
1081 n->meta = meta;
1082 else
1083 tail->next = meta;
1085 return (0);
1086 } /* int plugin_notification_meta_add */
1088 int plugin_notification_meta_add_string (notification_t *n,
1089 const char *name,
1090 const char *value)
1091 {
1092 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1093 }
1095 int plugin_notification_meta_add_signed_int (notification_t *n,
1096 const char *name,
1097 int64_t value)
1098 {
1099 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1100 }
1102 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1103 const char *name,
1104 uint64_t value)
1105 {
1106 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1107 }
1109 int plugin_notification_meta_add_double (notification_t *n,
1110 const char *name,
1111 double value)
1112 {
1113 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1114 }
1116 int plugin_notification_meta_add_boolean (notification_t *n,
1117 const char *name,
1118 bool value)
1119 {
1120 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1121 }
1123 int plugin_notification_meta_copy (notification_t *dst,
1124 const notification_t *src)
1125 {
1126 notification_meta_t *meta;
1128 assert (dst != NULL);
1129 assert (src != NULL);
1130 assert (dst != src);
1131 assert ((src->meta == NULL) || (src->meta != dst->meta));
1133 for (meta = src->meta; meta != NULL; meta = meta->next)
1134 {
1135 if (meta->type == NM_TYPE_STRING)
1136 plugin_notification_meta_add_string (dst, meta->name,
1137 meta->nm_value.nm_string);
1138 else if (meta->type == NM_TYPE_SIGNED_INT)
1139 plugin_notification_meta_add_signed_int (dst, meta->name,
1140 meta->nm_value.nm_signed_int);
1141 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1142 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1143 meta->nm_value.nm_unsigned_int);
1144 else if (meta->type == NM_TYPE_DOUBLE)
1145 plugin_notification_meta_add_double (dst, meta->name,
1146 meta->nm_value.nm_double);
1147 else if (meta->type == NM_TYPE_BOOLEAN)
1148 plugin_notification_meta_add_boolean (dst, meta->name,
1149 meta->nm_value.nm_boolean);
1150 }
1152 return (0);
1153 } /* int plugin_notification_meta_copy */
1155 int plugin_notification_meta_free (notification_t *n)
1156 {
1157 notification_meta_t *this;
1158 notification_meta_t *next;
1160 if (n == NULL)
1161 {
1162 ERROR ("plugin_notification_meta_free: n == NULL!");
1163 return (-1);
1164 }
1166 this = n->meta;
1167 n->meta = NULL;
1168 while (this != NULL)
1169 {
1170 next = this->next;
1172 if (this->type == NM_TYPE_STRING)
1173 {
1174 free ((char *)this->nm_value.nm_string);
1175 this->nm_value.nm_string = NULL;
1176 }
1177 sfree (this);
1179 this = next;
1180 }
1182 return (0);
1183 } /* int plugin_notification_meta_free */