2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2009 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
20 * Sebastian Harl <sh at tokkee.org>
24 #include "utils_complain.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"
44 struct callback_func_s
49 typedef struct callback_func_s callback_func_t;
55 /* `read_func_t' "inherits" from `callback_func_t'.
56 * The `rf_super' member MUST be the first one in this structure! */
57 #define rf_callback rf_super.cf_callback
58 #define rf_udata rf_super.cf_udata
59 callback_func_t rf_super;
63 enum { DONE = 0, TODO = 1, ACTIVE = 2 } rf_needs_read;
65 typedef struct read_func_s read_func_t;
70 static llist_t *list_init;
71 static llist_t *list_read;
72 static llist_t *list_write;
73 static llist_t *list_flush;
74 static llist_t *list_shutdown;
75 static llist_t *list_log;
76 static llist_t *list_notification;
78 static fc_chain_t *pre_cache_chain = NULL;
79 static fc_chain_t *post_cache_chain = NULL;
81 static c_avl_tree_t *data_sets;
83 static char *plugindir = NULL;
85 static int read_loop = 1;
86 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
87 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
88 static pthread_t *read_threads = NULL;
89 static int read_threads_num = 0;
94 static const char *plugin_get_dir (void)
96 if (plugindir == NULL)
102 static void destroy_callback (callback_func_t *cf) /* {{{ */
107 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
109 cf->cf_udata.free_func (cf->cf_udata.data);
110 cf->cf_udata.data = NULL;
111 cf->cf_udata.free_func = NULL;
114 } /* }}} void destroy_callback */
116 static void destroy_all_callbacks (llist_t **list) /* {{{ */
123 le = llist_head (*list);
131 destroy_callback (le->value);
137 llist_destroy (*list);
139 } /* }}} void destroy_all_callbacks */
141 static int register_callback (llist_t **list, /* {{{ */
142 const char *name, callback_func_t *cf)
149 *list = llist_create ();
152 ERROR ("plugin: create_register_callback: "
153 "llist_create failed.");
154 destroy_callback (cf);
162 ERROR ("plugin: create_register_callback: strdup failed.");
163 destroy_callback (cf);
167 le = llist_search (*list, name);
170 le = llentry_create (key, cf);
173 ERROR ("plugin: create_register_callback: "
174 "llentry_create failed.");
176 destroy_callback (cf);
180 llist_append (*list, le);
184 callback_func_t *old_cf;
189 destroy_callback (old_cf);
194 } /* }}} int register_callback */
196 static int create_register_callback (llist_t **list, /* {{{ */
197 const char *name, void *callback, user_data_t *ud)
202 cf = (callback_func_t *) malloc (sizeof (*cf));
205 ERROR ("plugin: create_register_callback: malloc failed.");
209 memset (cf, 0, sizeof (*cf));
211 cf->cf_callback = callback;
214 cf->cf_udata.data = NULL;
215 cf->cf_udata.free_func = NULL;
222 return (register_callback (list, name, cf));
223 } /* }}} int create_register_callback */
225 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
232 e = llist_search (list, name);
236 llist_remove (list, e);
239 destroy_callback (e->value);
244 } /* }}} int plugin_unregister */
247 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
248 * object, but it will bitch about a shared object not having a
249 * ``module_register'' symbol..
251 static int plugin_load_file (char *file)
254 void (*reg_handle) (void);
256 DEBUG ("file = %s", file);
259 lt_dlerror (); /* clear errors */
261 if ((dlh = lt_dlopen (file)) == NULL)
263 const char *error = lt_dlerror ();
265 ERROR ("lt_dlopen (%s) failed: %s", file, error);
266 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
270 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
272 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
273 file, lt_dlerror ());
283 static void *plugin_read_thread (void __attribute__((unused)) *args)
290 pthread_mutex_lock (&read_lock);
292 while (read_loop != 0)
294 le = llist_head (list_read);
297 while ((read_loop != 0) && (le != NULL))
299 rf = (read_func_t *) le->value;
301 if (rf->rf_needs_read != TODO)
307 /* We will do this read function */
308 rf->rf_needs_read = ACTIVE;
310 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
311 (unsigned long int) pthread_self (), le->key);
312 pthread_mutex_unlock (&read_lock);
314 if (rf->rf_type == RF_SIMPLE)
316 int (*callback) (void);
318 callback = rf->rf_callback;
319 status = (*callback) ();
323 plugin_read_cb callback;
325 callback = rf->rf_callback;
326 status = (*callback) (&rf->rf_udata);
333 if (rf->rf_wait_time < interval_g)
334 rf->rf_wait_time = interval_g;
335 rf->rf_wait_left = rf->rf_wait_time;
336 rf->rf_wait_time = rf->rf_wait_time * 2;
337 if (rf->rf_wait_time > 86400)
338 rf->rf_wait_time = 86400;
340 NOTICE ("read-function of plugin `%s' "
341 "failed. Will suspend it for %i "
342 "seconds.", le->key, rf->rf_wait_left);
346 rf->rf_wait_left = 0;
347 rf->rf_wait_time = interval_g;
350 pthread_mutex_lock (&read_lock);
352 rf->rf_needs_read = DONE;
354 } /* while (le != NULL) */
356 if ((read_loop != 0) && (done == 0))
358 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
359 (unsigned long int) pthread_self ());
360 pthread_cond_wait (&read_cond, &read_lock);
362 } /* while (read_loop) */
364 pthread_mutex_unlock (&read_lock);
368 } /* void *plugin_read_thread */
370 static void start_read_threads (int num)
374 if (read_threads != NULL)
377 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
378 if (read_threads == NULL)
380 ERROR ("plugin: start_read_threads: calloc failed.");
384 read_threads_num = 0;
385 for (i = 0; i < num; i++)
387 if (pthread_create (read_threads + read_threads_num, NULL,
388 plugin_read_thread, NULL) == 0)
394 ERROR ("plugin: start_read_threads: pthread_create failed.");
398 } /* void start_read_threads */
400 static void stop_read_threads (void)
404 if (read_threads == NULL)
407 pthread_mutex_lock (&read_lock);
409 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
410 pthread_cond_broadcast (&read_cond);
411 pthread_mutex_unlock (&read_lock);
413 for (i = 0; i < read_threads_num; i++)
415 if (pthread_join (read_threads[i], NULL) != 0)
417 ERROR ("plugin: stop_read_threads: pthread_join failed.");
419 read_threads[i] = (pthread_t) 0;
421 sfree (read_threads);
422 read_threads_num = 0;
423 } /* void stop_read_threads */
428 void plugin_set_dir (const char *dir)
430 if (plugindir != NULL)
435 else if ((plugindir = strdup (dir)) == NULL)
438 ERROR ("strdup failed: %s",
439 sstrerror (errno, errbuf, sizeof (errbuf)));
444 int plugin_load (const char *type)
448 char filename[BUFSIZE] = "";
449 char typename[BUFSIZE];
456 DEBUG ("type = %s", type);
458 dir = plugin_get_dir ();
461 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
462 * type when matching the filename */
463 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
464 if ((status < 0) || ((size_t) status >= sizeof (typename)))
466 WARNING ("snprintf: truncated: `%s.so'", type);
469 typename_len = strlen (typename);
471 if ((dh = opendir (dir)) == NULL)
474 ERROR ("opendir (%s): %s", dir,
475 sstrerror (errno, errbuf, sizeof (errbuf)));
479 while ((de = readdir (dh)) != NULL)
481 if (strncasecmp (de->d_name, typename, typename_len))
484 status = ssnprintf (filename, sizeof (filename),
485 "%s/%s", dir, de->d_name);
486 if ((status < 0) || ((size_t) status >= sizeof (filename)))
488 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
492 if (lstat (filename, &statbuf) == -1)
495 WARNING ("stat %s: %s", filename,
496 sstrerror (errno, errbuf, sizeof (errbuf)));
499 else if (!S_ISREG (statbuf.st_mode))
501 /* don't follow symlinks */
505 if (plugin_load_file (filename) == 0)
513 fprintf (stderr, "Unable to load plugin %s.\n", type);
519 if (filename[0] == '\0')
520 fprintf (stderr, "Could not find plugin %s.\n", type);
526 * The `register_*' functions follow
528 int plugin_register_config (const char *name,
529 int (*callback) (const char *key, const char *val),
530 const char **keys, int keys_num)
532 cf_register (name, callback, keys, keys_num);
534 } /* int plugin_register_config */
536 int plugin_register_complex_config (const char *type,
537 int (*callback) (oconfig_item_t *))
539 return (cf_register_complex (type, callback));
540 } /* int plugin_register_complex_config */
542 int plugin_register_init (const char *name,
543 int (*callback) (void))
545 return (create_register_callback (&list_init, name, (void *) callback,
546 /* user_data = */ NULL));
547 } /* plugin_register_init */
549 int plugin_register_read (const char *name,
550 int (*callback) (void))
554 rf = (read_func_t *) malloc (sizeof (read_func_t));
558 ERROR ("plugin_register_read: malloc failed: %s",
559 sstrerror (errno, errbuf, sizeof (errbuf)));
563 memset (rf, 0, sizeof (read_func_t));
564 rf->rf_callback = (void *) callback;
565 rf->rf_udata.data = NULL;
566 rf->rf_udata.free_func = NULL;
567 rf->rf_wait_time = interval_g;
568 rf->rf_wait_left = 0;
569 rf->rf_type = RF_SIMPLE;
570 rf->rf_needs_read = DONE;
572 return (register_callback (&list_read, name, (callback_func_t *) rf));
573 } /* int plugin_register_read */
575 int plugin_register_complex_read (const char *name,
576 plugin_read_cb callback, user_data_t *user_data)
580 rf = (read_func_t *) malloc (sizeof (read_func_t));
583 ERROR ("plugin_register_complex_read: malloc failed.");
587 memset (rf, 0, sizeof (read_func_t));
588 rf->rf_callback = (void *) callback;
589 rf->rf_wait_time = interval_g;
590 rf->rf_wait_left = 0;
591 rf->rf_type = RF_COMPLEX;
592 rf->rf_needs_read = DONE;
595 if (user_data == NULL)
597 rf->rf_udata.data = NULL;
598 rf->rf_udata.free_func = NULL;
602 rf->rf_udata = *user_data;
605 return (register_callback (&list_read, name, (callback_func_t *) rf));
606 } /* int plugin_register_complex_read */
608 int plugin_register_write (const char *name,
609 plugin_write_cb callback, user_data_t *ud)
611 return (create_register_callback (&list_write, name,
612 (void *) callback, ud));
613 } /* int plugin_register_write */
615 int plugin_register_flush (const char *name,
616 plugin_flush_cb callback, user_data_t *ud)
618 return (create_register_callback (&list_flush, name,
619 (void *) callback, ud));
620 } /* int plugin_register_flush */
622 int plugin_register_shutdown (char *name,
623 int (*callback) (void))
625 return (create_register_callback (&list_shutdown, name,
626 (void *) callback, /* user_data = */ NULL));
627 } /* int plugin_register_shutdown */
629 int plugin_register_data_set (const data_set_t *ds)
634 if ((data_sets != NULL)
635 && (c_avl_get (data_sets, ds->type, NULL) == 0))
637 NOTICE ("Replacing DS `%s' with another version.", ds->type);
638 plugin_unregister_data_set (ds->type);
640 else if (data_sets == NULL)
642 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
643 if (data_sets == NULL)
647 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
650 memcpy(ds_copy, ds, sizeof (data_set_t));
652 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
654 if (ds_copy->ds == NULL)
660 for (i = 0; i < ds->ds_num; i++)
661 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
663 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
664 } /* int plugin_register_data_set */
666 int plugin_register_log (const char *name,
667 plugin_log_cb callback, user_data_t *ud)
669 return (create_register_callback (&list_log, name,
670 (void *) callback, ud));
671 } /* int plugin_register_log */
673 int plugin_register_notification (const char *name,
674 plugin_notification_cb callback, user_data_t *ud)
676 return (create_register_callback (&list_notification, name,
677 (void *) callback, ud));
678 } /* int plugin_register_log */
680 int plugin_unregister_config (const char *name)
682 cf_unregister (name);
684 } /* int plugin_unregister_config */
686 int plugin_unregister_complex_config (const char *name)
688 cf_unregister_complex (name);
690 } /* int plugin_unregister_complex_config */
692 int plugin_unregister_init (const char *name)
694 return (plugin_unregister (list_init, name));
697 int plugin_unregister_read (const char *name)
699 return (plugin_unregister (list_read, name));
702 int plugin_unregister_write (const char *name)
704 return (plugin_unregister (list_write, name));
707 int plugin_unregister_flush (const char *name)
709 return (plugin_unregister (list_flush, name));
712 int plugin_unregister_shutdown (const char *name)
714 return (plugin_unregister (list_shutdown, name));
717 int plugin_unregister_data_set (const char *name)
721 if (data_sets == NULL)
724 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
731 } /* int plugin_unregister_data_set */
733 int plugin_unregister_log (const char *name)
735 return (plugin_unregister (list_log, name));
738 int plugin_unregister_notification (const char *name)
740 return (plugin_unregister (list_notification, name));
743 void plugin_init_all (void)
745 const char *chain_name;
749 /* Init the value cache */
752 chain_name = global_option_get ("PreCacheChain");
753 pre_cache_chain = fc_chain_get_by_name (chain_name);
755 chain_name = global_option_get ("PostCacheChain");
756 post_cache_chain = fc_chain_get_by_name (chain_name);
759 if ((list_init == NULL) && (list_read == NULL))
762 /* Calling all init callbacks before checking if read callbacks
763 * are available allows the init callbacks to register the read
765 le = llist_head (list_init);
769 plugin_init_cb callback;
772 callback = cf->cf_callback;
773 status = (*callback) ();
777 ERROR ("Initialization of plugin `%s' "
778 "failed with status %i. "
779 "Plugin will be unloaded.",
781 /* Plugins that register read callbacks from the init
782 * callback should take care of appropriate error
783 * handling themselves. */
784 /* FIXME: Unload _all_ functions */
785 plugin_unregister_read (le->key);
791 /* Start read-threads */
792 if (list_read != NULL)
796 rt = global_option_get ("ReadThreads");
799 start_read_threads ((num > 0) ? num : 5);
801 } /* void plugin_init_all */
803 void plugin_read_all (void)
810 if (list_read == NULL)
813 pthread_mutex_lock (&read_lock);
815 le = llist_head (list_read);
818 rf = (read_func_t *) le->value;
820 if (rf->rf_needs_read != DONE)
826 if (rf->rf_wait_left > 0)
827 rf->rf_wait_left -= interval_g;
829 if (rf->rf_wait_left <= 0)
831 rf->rf_needs_read = TODO;
837 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
838 pthread_cond_broadcast (&read_cond);
839 pthread_mutex_unlock (&read_lock);
840 } /* void plugin_read_all */
842 /* Read function called when the `-T' command line argument is given. */
843 int plugin_read_all_once (void)
848 int return_status = 0;
850 if (list_read == NULL)
852 NOTICE ("No read-functions are registered.");
856 for (le = llist_head (list_read);
860 rf = (read_func_t *) le->value;
862 if (rf->rf_type == RF_SIMPLE)
864 int (*callback) (void);
866 callback = rf->rf_callback;
867 status = (*callback) ();
871 plugin_read_cb callback;
873 callback = rf->rf_callback;
874 status = (*callback) (&rf->rf_udata);
879 NOTICE ("read-function of plugin `%s' failed.",
885 return (return_status);
886 } /* int plugin_read_all_once */
888 int plugin_write (const char *plugin, /* {{{ */
889 const data_set_t *ds, const value_list_t *vl)
897 if (list_write == NULL)
902 ds = plugin_get_ds (vl->type);
905 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
915 le = llist_head (list_write);
918 callback_func_t *cf = le->value;
919 plugin_write_cb callback;
921 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
922 callback = cf->cf_callback;
923 status = (*callback) (ds, vl, &cf->cf_udata);
932 if ((success == 0) && (failure != 0))
937 else /* plugin != NULL */
940 plugin_write_cb callback;
942 le = llist_head (list_write);
945 if (strcasecmp (plugin, le->key) == 0)
956 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
957 callback = cf->cf_callback;
958 status = (*callback) (ds, vl, &cf->cf_udata);
962 } /* }}} int plugin_write */
964 int plugin_flush (const char *plugin, int timeout, const char *identifier)
968 if (list_flush == NULL)
971 le = llist_head (list_flush);
975 plugin_flush_cb callback;
978 && (strcmp (plugin, le->key) != 0))
985 callback = cf->cf_callback;
987 (*callback) (timeout, identifier, &cf->cf_udata);
992 } /* int plugin_flush */
994 void plugin_shutdown_all (void)
998 stop_read_threads ();
1000 destroy_all_callbacks (&list_init);
1001 destroy_all_callbacks (&list_read);
1003 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1004 /* identifier = */ NULL);
1007 if (list_shutdown != NULL)
1008 le = llist_head (list_shutdown);
1012 callback_func_t *cf;
1013 plugin_shutdown_cb callback;
1016 callback = cf->cf_callback;
1018 /* Advance the pointer before calling the callback allows
1019 * shutdown functions to unregister themselves. If done the
1020 * other way around the memory `le' points to will be freed
1021 * after callback returns. */
1027 destroy_all_callbacks (&list_write);
1028 destroy_all_callbacks (&list_flush);
1029 destroy_all_callbacks (&list_notification);
1030 destroy_all_callbacks (&list_shutdown);
1031 destroy_all_callbacks (&list_log);
1032 } /* void plugin_shutdown_all */
1034 int plugin_dispatch_values (value_list_t *vl)
1037 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1039 value_t *saved_values;
1040 int saved_values_len;
1044 if ((vl == NULL) || (vl->type[0] == 0)
1045 || (vl->values == NULL) || (vl->values_len < 1))
1047 ERROR ("plugin_dispatch_values: Invalid value list.");
1051 if (list_write == NULL)
1052 c_complain_once (LOG_WARNING, &no_write_complaint,
1053 "plugin_dispatch_values: No write callback has been "
1054 "registered. Please load at least one output plugin, "
1055 "if you want the collected data to be stored.");
1057 if (data_sets == NULL)
1059 ERROR ("plugin_dispatch_values: No data sets registered. "
1060 "Could the types database be read? Check "
1061 "your `TypesDB' setting!");
1065 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1067 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1072 vl->time = time (NULL);
1074 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1076 "plugin = %s; plugin_instance = %s; "
1077 "type = %s; type_instance = %s;",
1078 (unsigned int) vl->time, vl->interval,
1080 vl->plugin, vl->plugin_instance,
1081 vl->type, vl->type_instance);
1084 assert (0 == strcmp (ds->type, vl->type));
1086 if (0 != strcmp (ds->type, vl->type))
1087 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1088 ds->type, vl->type);
1092 assert (ds->ds_num == vl->values_len);
1094 if (ds->ds_num != vl->values_len)
1096 ERROR ("plugin_dispatch_values: ds->type = %s: "
1097 "(ds->ds_num = %i) != "
1098 "(vl->values_len = %i)",
1099 ds->type, ds->ds_num, vl->values_len);
1104 escape_slashes (vl->host, sizeof (vl->host));
1105 escape_slashes (vl->plugin, sizeof (vl->plugin));
1106 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1107 escape_slashes (vl->type, sizeof (vl->type));
1108 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1110 /* Copy the values. This way, we can assure `targets' that they get
1111 * dynamically allocated values, which they can free and replace if
1113 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1115 saved_values = vl->values;
1116 saved_values_len = vl->values_len;
1118 vl->values = (value_t *) calloc (vl->values_len,
1119 sizeof (*vl->values));
1120 if (vl->values == NULL)
1122 ERROR ("plugin_dispatch_values: calloc failed.");
1123 vl->values = saved_values;
1126 memcpy (vl->values, saved_values,
1127 vl->values_len * sizeof (*vl->values));
1129 else /* if ((pre == NULL) && (post == NULL)) */
1131 saved_values = NULL;
1132 saved_values_len = 0;
1135 if (pre_cache_chain != NULL)
1137 status = fc_process_chain (ds, vl, pre_cache_chain);
1140 WARNING ("plugin_dispatch_values: Running the "
1141 "pre-cache chain failed with "
1145 else if (status == FC_TARGET_STOP)
1147 /* Restore the state of the value_list so that plugins
1148 * don't get confused.. */
1149 if (saved_values != NULL)
1152 vl->values = saved_values;
1153 vl->values_len = saved_values_len;
1159 /* Update the value cache */
1162 if (post_cache_chain != NULL)
1164 status = fc_process_chain (ds, vl, post_cache_chain);
1167 WARNING ("plugin_dispatch_values: Running the "
1168 "post-cache chain failed with "
1174 fc_default_action (ds, vl);
1176 /* Restore the state of the value_list so that plugins don't get
1178 if (saved_values != NULL)
1181 vl->values = saved_values;
1182 vl->values_len = saved_values_len;
1186 } /* int plugin_dispatch_values */
1188 int plugin_dispatch_notification (const notification_t *notif)
1191 /* Possible TODO: Add flap detection here */
1193 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1194 "time = %u; host = %s;",
1195 notif->severity, notif->message,
1196 (unsigned int) notif->time, notif->host);
1198 /* Nobody cares for notifications */
1199 if (list_notification == NULL)
1202 le = llist_head (list_notification);
1205 callback_func_t *cf;
1206 plugin_notification_cb callback;
1210 callback = cf->cf_callback;
1211 status = (*callback) (notif, &cf->cf_udata);
1214 WARNING ("plugin_dispatch_notification: Notification "
1215 "callback %s returned %i.",
1223 } /* int plugin_dispatch_notification */
1225 void plugin_log (int level, const char *format, ...)
1231 if (list_log == NULL)
1235 if (level >= LOG_DEBUG)
1239 va_start (ap, format);
1240 vsnprintf (msg, sizeof (msg), format, ap);
1241 msg[sizeof (msg) - 1] = '\0';
1244 le = llist_head (list_log);
1247 callback_func_t *cf;
1248 plugin_log_cb callback;
1251 callback = cf->cf_callback;
1253 (*callback) (level, msg, &cf->cf_udata);
1257 } /* void plugin_log */
1259 const data_set_t *plugin_get_ds (const char *name)
1263 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1265 DEBUG ("No such dataset registered: %s", name);
1270 } /* data_set_t *plugin_get_ds */
1272 static int plugin_notification_meta_add (notification_t *n,
1274 enum notification_meta_type_e type,
1277 notification_meta_t *meta;
1278 notification_meta_t *tail;
1280 if ((n == NULL) || (name == NULL) || (value == NULL))
1282 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1286 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1289 ERROR ("plugin_notification_meta_add: malloc failed.");
1292 memset (meta, 0, sizeof (notification_meta_t));
1294 sstrncpy (meta->name, name, sizeof (meta->name));
1299 case NM_TYPE_STRING:
1301 meta->nm_value.nm_string = strdup ((const char *) value);
1302 if (meta->nm_value.nm_string == NULL)
1304 ERROR ("plugin_notification_meta_add: strdup failed.");
1310 case NM_TYPE_SIGNED_INT:
1312 meta->nm_value.nm_signed_int = *((int64_t *) value);
1315 case NM_TYPE_UNSIGNED_INT:
1317 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1320 case NM_TYPE_DOUBLE:
1322 meta->nm_value.nm_double = *((double *) value);
1325 case NM_TYPE_BOOLEAN:
1327 meta->nm_value.nm_boolean = *((bool *) value);
1332 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1336 } /* switch (type) */
1340 while ((tail != NULL) && (tail->next != NULL))
1349 } /* int plugin_notification_meta_add */
1351 int plugin_notification_meta_add_string (notification_t *n,
1355 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1358 int plugin_notification_meta_add_signed_int (notification_t *n,
1362 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1365 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1369 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1372 int plugin_notification_meta_add_double (notification_t *n,
1376 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1379 int plugin_notification_meta_add_boolean (notification_t *n,
1383 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1386 int plugin_notification_meta_copy (notification_t *dst,
1387 const notification_t *src)
1389 notification_meta_t *meta;
1391 assert (dst != NULL);
1392 assert (src != NULL);
1393 assert (dst != src);
1394 assert ((src->meta == NULL) || (src->meta != dst->meta));
1396 for (meta = src->meta; meta != NULL; meta = meta->next)
1398 if (meta->type == NM_TYPE_STRING)
1399 plugin_notification_meta_add_string (dst, meta->name,
1400 meta->nm_value.nm_string);
1401 else if (meta->type == NM_TYPE_SIGNED_INT)
1402 plugin_notification_meta_add_signed_int (dst, meta->name,
1403 meta->nm_value.nm_signed_int);
1404 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1405 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1406 meta->nm_value.nm_unsigned_int);
1407 else if (meta->type == NM_TYPE_DOUBLE)
1408 plugin_notification_meta_add_double (dst, meta->name,
1409 meta->nm_value.nm_double);
1410 else if (meta->type == NM_TYPE_BOOLEAN)
1411 plugin_notification_meta_add_boolean (dst, meta->name,
1412 meta->nm_value.nm_boolean);
1416 } /* int plugin_notification_meta_copy */
1418 int plugin_notification_meta_free (notification_meta_t *n)
1420 notification_meta_t *this;
1421 notification_meta_t *next;
1425 ERROR ("plugin_notification_meta_free: n == NULL!");
1430 while (this != NULL)
1434 if (this->type == NM_TYPE_STRING)
1436 free ((char *)this->nm_value.nm_string);
1437 this->nm_value.nm_string = NULL;
1445 } /* int plugin_notification_meta_free */
1447 /* vim: set sw=8 ts=8 noet fdm=marker : */