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"
53 int (*cb_simple) (void);
54 plugin_read_cb cb_complex;
56 enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
59 typedef struct read_func_s read_func_t;
63 plugin_write_cb callback;
66 typedef struct write_func_s write_func_t;
70 plugin_flush_cb callback;
73 typedef struct flush_func_s flush_func_t;
77 plugin_log_cb callback;
80 typedef struct log_func_s log_func_t;
85 static llist_t *list_init;
86 static llist_t *list_read;
87 static llist_t *list_write;
88 static llist_t *list_flush;
89 static llist_t *list_shutdown;
90 static llist_t *list_log;
91 static llist_t *list_notification;
93 static fc_chain_t *pre_cache_chain = NULL;
94 static fc_chain_t *post_cache_chain = NULL;
96 static c_avl_tree_t *data_sets;
98 static char *plugindir = NULL;
100 static int read_loop = 1;
101 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
102 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
103 static pthread_t *read_threads = NULL;
104 static int read_threads_num = 0;
109 static const char *plugin_get_dir (void)
111 if (plugindir == NULL)
117 static int register_callback (llist_t **list, const char *name, void *callback)
123 && ((*list = llist_create ()) == NULL))
126 le = llist_search (*list, name);
133 le = llentry_create (key, callback);
140 llist_append (*list, le);
144 le->value = callback;
148 } /* int register_callback */
150 static void plugin_user_data_destroy (user_data_t *ud)
152 if ((ud != NULL) && (ud->data != NULL) && (ud->free_func != NULL))
154 ud->free_func (ud->data);
156 ud->free_func = NULL;
158 } /* void plugin_user_data_destroy */
160 static int plugin_unregister (llist_t *list, const char *name)
164 e = llist_search (list, name);
169 llist_remove (list, e);
174 } /* int plugin_unregister */
177 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
178 * object, but it will bitch about a shared object not having a
179 * ``module_register'' symbol..
181 static int plugin_load_file (char *file)
184 void (*reg_handle) (void);
186 DEBUG ("file = %s", file);
189 lt_dlerror (); /* clear errors */
191 if ((dlh = lt_dlopen (file)) == NULL)
193 const char *error = lt_dlerror ();
195 ERROR ("lt_dlopen (%s) failed: %s", file, error);
196 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
200 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
202 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
203 file, lt_dlerror ());
213 static void *plugin_read_thread (void __attribute__((unused)) *args)
220 pthread_mutex_lock (&read_lock);
222 while (read_loop != 0)
224 le = llist_head (list_read);
227 while ((read_loop != 0) && (le != NULL))
229 rf = (read_func_t *) le->value;
231 if (rf->needs_read != TODO)
237 /* We will do this read function */
238 rf->needs_read = ACTIVE;
240 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
241 (unsigned long int) pthread_self (), le->key);
242 pthread_mutex_unlock (&read_lock);
244 if (rf->type == RF_SIMPLE)
246 status = rf->callback.cb_simple ();
250 assert (rf->type == RF_COMPLEX);
251 status = rf->callback.cb_complex (&rf->udata);
257 if (rf->wait_time < interval_g)
258 rf->wait_time = interval_g;
259 rf->wait_left = rf->wait_time;
260 rf->wait_time = rf->wait_time * 2;
261 if (rf->wait_time > 86400)
262 rf->wait_time = 86400;
264 NOTICE ("read-function of plugin `%s' "
265 "failed. Will suspend it for %i "
266 "seconds.", le->key, rf->wait_left);
271 rf->wait_time = interval_g;
274 pthread_mutex_lock (&read_lock);
276 rf->needs_read = DONE;
278 } /* while (le != NULL) */
280 if ((read_loop != 0) && (done == 0))
282 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
283 (unsigned long int) pthread_self ());
284 pthread_cond_wait (&read_cond, &read_lock);
286 } /* while (read_loop) */
288 pthread_mutex_unlock (&read_lock);
292 } /* void *plugin_read_thread */
294 static void start_read_threads (int num)
298 if (read_threads != NULL)
301 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
302 if (read_threads == NULL)
304 ERROR ("plugin: start_read_threads: calloc failed.");
308 read_threads_num = 0;
309 for (i = 0; i < num; i++)
311 if (pthread_create (read_threads + read_threads_num, NULL,
312 plugin_read_thread, NULL) == 0)
318 ERROR ("plugin: start_read_threads: pthread_create failed.");
322 } /* void start_read_threads */
324 static void stop_read_threads (void)
328 if (read_threads == NULL)
331 pthread_mutex_lock (&read_lock);
333 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
334 pthread_cond_broadcast (&read_cond);
335 pthread_mutex_unlock (&read_lock);
337 for (i = 0; i < read_threads_num; i++)
339 if (pthread_join (read_threads[i], NULL) != 0)
341 ERROR ("plugin: stop_read_threads: pthread_join failed.");
343 read_threads[i] = (pthread_t) 0;
345 sfree (read_threads);
346 read_threads_num = 0;
347 } /* void stop_read_threads */
349 static int remove_read_functions (void)
353 if (list_read == NULL)
356 this = llist_head (list_read);
363 rf = (read_func_t *) this->value;
367 plugin_user_data_destroy (&rf->udata);
373 llist_destroy (list_read);
377 } /* }}} int remove_read_functions */
382 void plugin_set_dir (const char *dir)
384 if (plugindir != NULL)
389 else if ((plugindir = strdup (dir)) == NULL)
392 ERROR ("strdup failed: %s",
393 sstrerror (errno, errbuf, sizeof (errbuf)));
398 int plugin_load (const char *type)
402 char filename[BUFSIZE] = "";
403 char typename[BUFSIZE];
410 DEBUG ("type = %s", type);
412 dir = plugin_get_dir ();
415 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
416 * type when matching the filename */
417 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
418 if ((status < 0) || ((size_t) status >= sizeof (typename)))
420 WARNING ("snprintf: truncated: `%s.so'", type);
423 typename_len = strlen (typename);
425 if ((dh = opendir (dir)) == NULL)
428 ERROR ("opendir (%s): %s", dir,
429 sstrerror (errno, errbuf, sizeof (errbuf)));
433 while ((de = readdir (dh)) != NULL)
435 if (strncasecmp (de->d_name, typename, typename_len))
438 status = ssnprintf (filename, sizeof (filename),
439 "%s/%s", dir, de->d_name);
440 if ((status < 0) || ((size_t) status >= sizeof (filename)))
442 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
446 if (lstat (filename, &statbuf) == -1)
449 WARNING ("stat %s: %s", filename,
450 sstrerror (errno, errbuf, sizeof (errbuf)));
453 else if (!S_ISREG (statbuf.st_mode))
455 /* don't follow symlinks */
459 if (plugin_load_file (filename) == 0)
467 fprintf (stderr, "Unable to load plugin %s.\n", type);
473 if (filename[0] == '\0')
474 fprintf (stderr, "Could not find plugin %s.\n", type);
480 * The `register_*' functions follow
482 int plugin_register_config (const char *name,
483 int (*callback) (const char *key, const char *val),
484 const char **keys, int keys_num)
486 cf_register (name, callback, keys, keys_num);
488 } /* int plugin_register_config */
490 int plugin_register_complex_config (const char *type,
491 int (*callback) (oconfig_item_t *))
493 return (cf_register_complex (type, callback));
494 } /* int plugin_register_complex_config */
496 int plugin_register_init (const char *name,
497 int (*callback) (void))
499 return (register_callback (&list_init, name, (void *) callback));
500 } /* plugin_register_init */
502 int plugin_register_read (const char *name,
503 int (*callback) (void))
507 rf = (read_func_t *) malloc (sizeof (read_func_t));
511 ERROR ("plugin_register_read: malloc failed: %s",
512 sstrerror (errno, errbuf, sizeof (errbuf)));
516 memset (rf, 0, sizeof (read_func_t));
517 rf->wait_time = interval_g;
519 rf->type = RF_SIMPLE;
520 rf->callback.cb_simple = callback;
521 rf->needs_read = DONE;
522 rf->udata.data = NULL;
523 rf->udata.free_func = NULL;
525 return (register_callback (&list_read, name, (void *) rf));
526 } /* int plugin_register_read */
528 int plugin_register_complex_read (const char *name,
529 plugin_read_cb callback, user_data_t *user_data)
533 rf = (read_func_t *) malloc (sizeof (read_func_t));
536 ERROR ("plugin_register_complex_read: malloc failed.");
540 memset (rf, 0, sizeof (read_func_t));
541 rf->wait_time = interval_g;
543 rf->type = RF_COMPLEX;
544 rf->callback.cb_complex = callback;
545 rf->needs_read = DONE;
548 if (user_data == NULL)
550 rf->udata.data = NULL;
551 rf->udata.free_func = NULL;
555 rf->udata = *user_data;
558 return (register_callback (&list_read, name, (void *) rf));
559 } /* int plugin_register_complex_read */
561 int plugin_register_write (const char *name,
562 plugin_write_cb callback, user_data_t *user_data)
566 wf = (write_func_t *) malloc (sizeof (*wf));
569 ERROR ("plugin_register_write: malloc failed.");
572 memset (wf, 0, sizeof (*wf));
574 wf->callback = callback;
575 if (user_data == NULL)
577 wf->udata.data = NULL;
578 wf->udata.free_func = NULL;
582 wf->udata = *user_data;
585 return (register_callback (&list_write, name, (void *) wf));
586 } /* int plugin_register_write */
588 int plugin_register_flush (const char *name,
589 plugin_flush_cb callback, user_data_t *user_data)
593 ff = (flush_func_t *) malloc (sizeof (*ff));
596 ERROR ("plugin_register_flush: malloc failed.");
599 memset (ff, 0, sizeof (*ff));
601 ff->callback = callback;
602 if (user_data == NULL)
604 ff->udata.data = NULL;
605 ff->udata.free_func = NULL;
609 ff->udata = *user_data;
612 return (register_callback (&list_flush, name, (void *) ff));
613 } /* int plugin_register_flush */
615 int plugin_register_shutdown (char *name,
616 int (*callback) (void))
618 return (register_callback (&list_shutdown, name, (void *) callback));
619 } /* int plugin_register_shutdown */
621 int plugin_register_data_set (const data_set_t *ds)
626 if ((data_sets != NULL)
627 && (c_avl_get (data_sets, ds->type, NULL) == 0))
629 NOTICE ("Replacing DS `%s' with another version.", ds->type);
630 plugin_unregister_data_set (ds->type);
632 else if (data_sets == NULL)
634 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
635 if (data_sets == NULL)
639 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
642 memcpy(ds_copy, ds, sizeof (data_set_t));
644 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
646 if (ds_copy->ds == NULL)
652 for (i = 0; i < ds->ds_num; i++)
653 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
655 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
656 } /* int plugin_register_data_set */
658 int plugin_register_log (const char *name,
659 plugin_log_cb callback, user_data_t *user_data)
663 lf = (log_func_t *) malloc (sizeof (*lf));
666 ERROR ("plugin_register_log: malloc failed.");
669 memset (lf, 0, sizeof (*lf));
671 lf->callback = callback;
672 if (user_data == NULL)
674 lf->udata.data = NULL;
675 lf->udata.free_func = NULL;
679 lf->udata = *user_data;
682 return (register_callback (&list_log, name, (void *) lf));
683 } /* int plugin_register_log */
685 int plugin_register_notification (const char *name,
686 int (*callback) (const notification_t *notif))
688 return (register_callback (&list_notification, name, (void *) callback));
689 } /* int plugin_register_log */
691 int plugin_unregister_config (const char *name)
693 cf_unregister (name);
695 } /* int plugin_unregister_config */
697 int plugin_unregister_complex_config (const char *name)
699 cf_unregister_complex (name);
701 } /* int plugin_unregister_complex_config */
703 int plugin_unregister_init (const char *name)
705 return (plugin_unregister (list_init, name));
708 int plugin_unregister_read (const char *name)
713 e = llist_search (list_read, name);
718 llist_remove (list_read, e);
720 rf = (read_func_t *) e->value;
721 plugin_user_data_destroy (&rf->udata);
730 int plugin_unregister_write (const char *name)
735 e = llist_search (list_write, name);
740 llist_remove (list_write, e);
742 wf = (write_func_t *) e->value;
743 plugin_user_data_destroy (&wf->udata);
752 int plugin_unregister_flush (const char *name)
757 e = llist_search (list_flush, name);
762 llist_remove (list_flush, e);
764 ff = (flush_func_t *) e->value;
765 plugin_user_data_destroy (&ff->udata);
774 int plugin_unregister_shutdown (const char *name)
776 return (plugin_unregister (list_shutdown, name));
779 int plugin_unregister_data_set (const char *name)
783 if (data_sets == NULL)
786 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
793 } /* int plugin_unregister_data_set */
795 int plugin_unregister_log (const char *name)
800 e = llist_search (list_log, name);
805 llist_remove (list_log, e);
807 lf = (log_func_t *) e->value;
808 plugin_user_data_destroy (&lf->udata);
817 int plugin_unregister_notification (const char *name)
819 return (plugin_unregister (list_notification, name));
822 void plugin_init_all (void)
824 const char *chain_name;
825 int (*callback) (void);
829 /* Init the value cache */
832 chain_name = global_option_get ("PreCacheChain");
833 pre_cache_chain = fc_chain_get_by_name (chain_name);
835 chain_name = global_option_get ("PostCacheChain");
836 post_cache_chain = fc_chain_get_by_name (chain_name);
839 if ((list_init == NULL) && (list_read == NULL))
842 /* Calling all init callbacks before checking if read callbacks
843 * are available allows the init callbacks to register the read
845 le = llist_head (list_init);
848 callback = (int (*) (void)) le->value;
849 status = (*callback) ();
853 ERROR ("Initialization of plugin `%s' "
854 "failed with status %i. "
855 "Plugin will be unloaded.",
857 /* Plugins that register read callbacks from the init
858 * callback should take care of appropriate error
859 * handling themselves. */
860 /* FIXME: Unload _all_ functions */
861 plugin_unregister_read (le->key);
867 /* Start read-threads */
868 if (list_read != NULL)
872 rt = global_option_get ("ReadThreads");
875 start_read_threads ((num > 0) ? num : 5);
877 } /* void plugin_init_all */
879 void plugin_read_all (void)
886 if (list_read == NULL)
889 pthread_mutex_lock (&read_lock);
891 le = llist_head (list_read);
894 rf = (read_func_t *) le->value;
896 if (rf->needs_read != DONE)
902 if (rf->wait_left > 0)
903 rf->wait_left -= interval_g;
905 if (rf->wait_left <= 0)
907 rf->needs_read = TODO;
913 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
914 pthread_cond_broadcast (&read_cond);
915 pthread_mutex_unlock (&read_lock);
916 } /* void plugin_read_all */
918 /* Read function called when the `-T' command line argument is given. */
919 int plugin_read_all_once (void)
924 int return_status = 0;
926 if (list_read == NULL)
928 NOTICE ("No read-functions are registered.");
932 for (le = llist_head (list_read);
936 rf = (read_func_t *) le->value;
938 if (rf->type == RF_SIMPLE)
940 status = rf->callback.cb_simple ();
944 assert (rf->type == RF_COMPLEX);
945 status = rf->callback.cb_complex (&rf->udata);
950 NOTICE ("read-function of plugin `%s' failed.",
956 return (return_status);
957 } /* int plugin_read_all_once */
959 int plugin_write (const char *plugin, /* {{{ */
960 const data_set_t *ds, const value_list_t *vl)
968 if (list_write == NULL)
973 ds = plugin_get_ds (vl->type);
976 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
986 le = llist_head (list_write);
989 write_func_t *wf = le->value;
991 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
992 status = wf->callback (ds, vl, &wf->udata);
1001 if ((success == 0) && (failure != 0))
1006 else /* plugin != NULL */
1009 le = llist_head (list_write);
1012 if (strcasecmp (plugin, le->key) == 0)
1023 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1024 status = wf->callback (ds, vl, &wf->udata);
1028 } /* }}} int plugin_write */
1030 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1034 if (list_flush == NULL)
1037 le = llist_head (list_flush);
1042 if ((plugin != NULL)
1043 && (strcmp (plugin, le->key) != 0))
1049 ff = (flush_func_t *) le->value;
1051 ff->callback (timeout, identifier, &ff->udata);
1056 } /* int plugin_flush */
1058 void plugin_shutdown_all (void)
1060 int (*callback) (void);
1063 stop_read_threads ();
1064 remove_read_functions ();
1066 if (list_shutdown == NULL)
1069 le = llist_head (list_shutdown);
1072 callback = (int (*) (void)) le->value;
1074 /* Advance the pointer before calling the callback allows
1075 * shutdown functions to unregister themselves. If done the
1076 * other way around the memory `le' points to will be freed
1077 * after callback returns. */
1082 } /* void plugin_shutdown_all */
1084 int plugin_dispatch_values (value_list_t *vl)
1087 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1091 if ((vl == NULL) || (*vl->type == '\0')) {
1092 ERROR ("plugin_dispatch_values: Invalid value list.");
1096 if (list_write == NULL)
1097 c_complain_once (LOG_WARNING, &no_write_complaint,
1098 "plugin_dispatch_values: No write callback has been "
1099 "registered. Please load at least one output plugin, "
1100 "if you want the collected data to be stored.");
1102 if (data_sets == NULL)
1104 ERROR ("plugin_dispatch_values: No data sets registered. "
1105 "Could the types database be read? Check "
1106 "your `TypesDB' setting!");
1110 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1112 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1117 vl->time = time (NULL);
1119 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1121 "plugin = %s; plugin_instance = %s; "
1122 "type = %s; type_instance = %s;",
1123 (unsigned int) vl->time, vl->interval,
1125 vl->plugin, vl->plugin_instance,
1126 vl->type, vl->type_instance);
1129 assert (0 == strcmp (ds->type, vl->type));
1131 if (0 != strcmp (ds->type, vl->type))
1132 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1133 ds->type, vl->type);
1137 assert (ds->ds_num == vl->values_len);
1139 if (ds->ds_num != vl->values_len)
1141 ERROR ("plugin_dispatch_values: ds->type = %s: "
1142 "(ds->ds_num = %i) != "
1143 "(vl->values_len = %i)",
1144 ds->type, ds->ds_num, vl->values_len);
1149 escape_slashes (vl->host, sizeof (vl->host));
1150 escape_slashes (vl->plugin, sizeof (vl->plugin));
1151 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1152 escape_slashes (vl->type, sizeof (vl->type));
1153 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1155 if (pre_cache_chain != NULL)
1157 status = fc_process_chain (ds, vl, pre_cache_chain);
1160 WARNING ("plugin_dispatch_values: Running the "
1161 "pre-cache chain failed with "
1165 else if (status == FC_TARGET_STOP)
1169 /* Update the value cache */
1172 if (post_cache_chain != NULL)
1174 status = fc_process_chain (ds, vl, post_cache_chain);
1177 WARNING ("plugin_dispatch_values: Running the "
1178 "post-cache chain failed with "
1184 fc_default_action (ds, vl);
1187 } /* int plugin_dispatch_values */
1189 int plugin_dispatch_notification (const notification_t *notif)
1191 int (*callback) (const notification_t *);
1193 /* Possible TODO: Add flap detection here */
1195 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1196 "time = %u; host = %s;",
1197 notif->severity, notif->message,
1198 (unsigned int) notif->time, notif->host);
1200 /* Nobody cares for notifications */
1201 if (list_notification == NULL)
1204 le = llist_head (list_notification);
1207 callback = (int (*) (const notification_t *)) le->value;
1208 (*callback) (notif);
1214 } /* int plugin_dispatch_notification */
1216 void plugin_log (int level, const char *format, ...)
1222 if (list_log == NULL)
1226 if (level >= LOG_DEBUG)
1230 va_start (ap, format);
1231 vsnprintf (msg, sizeof (msg), format, ap);
1232 msg[sizeof (msg) - 1] = '\0';
1235 le = llist_head (list_log);
1240 lf = (log_func_t *) le->value;
1242 lf->callback (level, msg, &lf->udata);
1246 } /* void plugin_log */
1248 const data_set_t *plugin_get_ds (const char *name)
1252 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1254 DEBUG ("No such dataset registered: %s", name);
1259 } /* data_set_t *plugin_get_ds */
1261 static int plugin_notification_meta_add (notification_t *n,
1263 enum notification_meta_type_e type,
1266 notification_meta_t *meta;
1267 notification_meta_t *tail;
1269 if ((n == NULL) || (name == NULL) || (value == NULL))
1271 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1275 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1278 ERROR ("plugin_notification_meta_add: malloc failed.");
1281 memset (meta, 0, sizeof (notification_meta_t));
1283 sstrncpy (meta->name, name, sizeof (meta->name));
1288 case NM_TYPE_STRING:
1290 meta->nm_value.nm_string = strdup ((const char *) value);
1291 if (meta->nm_value.nm_string == NULL)
1293 ERROR ("plugin_notification_meta_add: strdup failed.");
1299 case NM_TYPE_SIGNED_INT:
1301 meta->nm_value.nm_signed_int = *((int64_t *) value);
1304 case NM_TYPE_UNSIGNED_INT:
1306 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1309 case NM_TYPE_DOUBLE:
1311 meta->nm_value.nm_double = *((double *) value);
1314 case NM_TYPE_BOOLEAN:
1316 meta->nm_value.nm_boolean = *((bool *) value);
1321 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1325 } /* switch (type) */
1329 while ((tail != NULL) && (tail->next != NULL))
1338 } /* int plugin_notification_meta_add */
1340 int plugin_notification_meta_add_string (notification_t *n,
1344 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1347 int plugin_notification_meta_add_signed_int (notification_t *n,
1351 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1354 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1358 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1361 int plugin_notification_meta_add_double (notification_t *n,
1365 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1368 int plugin_notification_meta_add_boolean (notification_t *n,
1372 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1375 int plugin_notification_meta_copy (notification_t *dst,
1376 const notification_t *src)
1378 notification_meta_t *meta;
1380 assert (dst != NULL);
1381 assert (src != NULL);
1382 assert (dst != src);
1383 assert ((src->meta == NULL) || (src->meta != dst->meta));
1385 for (meta = src->meta; meta != NULL; meta = meta->next)
1387 if (meta->type == NM_TYPE_STRING)
1388 plugin_notification_meta_add_string (dst, meta->name,
1389 meta->nm_value.nm_string);
1390 else if (meta->type == NM_TYPE_SIGNED_INT)
1391 plugin_notification_meta_add_signed_int (dst, meta->name,
1392 meta->nm_value.nm_signed_int);
1393 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1394 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1395 meta->nm_value.nm_unsigned_int);
1396 else if (meta->type == NM_TYPE_DOUBLE)
1397 plugin_notification_meta_add_double (dst, meta->name,
1398 meta->nm_value.nm_double);
1399 else if (meta->type == NM_TYPE_BOOLEAN)
1400 plugin_notification_meta_add_boolean (dst, meta->name,
1401 meta->nm_value.nm_boolean);
1405 } /* int plugin_notification_meta_copy */
1407 int plugin_notification_meta_free (notification_meta_t *n)
1409 notification_meta_t *this;
1410 notification_meta_t *next;
1414 ERROR ("plugin_notification_meta_free: n == NULL!");
1419 while (this != NULL)
1423 if (this->type == NM_TYPE_STRING)
1425 free ((char *)this->nm_value.nm_string);
1426 this->nm_value.nm_string = NULL;
1434 } /* int plugin_notification_meta_free */