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 __attribute__((unused)) *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;
331 int status;
333 DEBUG ("type = %s", type);
335 dir = plugin_get_dir ();
336 ret = 1;
338 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
339 * type when matching the filename */
340 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
341 if ((status < 0) || ((size_t) status >= sizeof (typename)))
342 {
343 WARNING ("snprintf: truncated: `%s.so'", type);
344 return (-1);
345 }
346 typename_len = strlen (typename);
348 if ((dh = opendir (dir)) == NULL)
349 {
350 char errbuf[1024];
351 ERROR ("opendir (%s): %s", dir,
352 sstrerror (errno, errbuf, sizeof (errbuf)));
353 return (-1);
354 }
356 while ((de = readdir (dh)) != NULL)
357 {
358 if (strncasecmp (de->d_name, typename, typename_len))
359 continue;
361 status = ssnprintf (filename, sizeof (filename),
362 "%s/%s", dir, de->d_name);
363 if ((status < 0) || ((size_t) status >= sizeof (filename)))
364 {
365 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
366 continue;
367 }
369 if (lstat (filename, &statbuf) == -1)
370 {
371 char errbuf[1024];
372 WARNING ("stat %s: %s", filename,
373 sstrerror (errno, errbuf, sizeof (errbuf)));
374 continue;
375 }
376 else if (!S_ISREG (statbuf.st_mode))
377 {
378 /* don't follow symlinks */
379 continue;
380 }
382 if (plugin_load_file (filename) == 0)
383 {
384 /* success */
385 ret = 0;
386 break;
387 }
388 else
389 {
390 fprintf (stderr, "Unable to load plugin %s.\n", type);
391 }
392 }
394 closedir (dh);
396 if (filename[0] == '\0')
397 fprintf (stderr, "Could not find plugin %s.\n", type);
399 return (ret);
400 }
402 /*
403 * The `register_*' functions follow
404 */
405 int plugin_register_config (const char *name,
406 int (*callback) (const char *key, const char *val),
407 const char **keys, int keys_num)
408 {
409 cf_register (name, callback, keys, keys_num);
410 return (0);
411 } /* int plugin_register_config */
413 int plugin_register_complex_config (const char *type,
414 int (*callback) (oconfig_item_t *))
415 {
416 return (cf_register_complex (type, callback));
417 } /* int plugin_register_complex_config */
419 int plugin_register_init (const char *name,
420 int (*callback) (void))
421 {
422 return (register_callback (&list_init, name, (void *) callback));
423 } /* plugin_register_init */
425 int plugin_register_read (const char *name,
426 int (*callback) (void))
427 {
428 read_func_t *rf;
430 rf = (read_func_t *) malloc (sizeof (read_func_t));
431 if (rf == NULL)
432 {
433 char errbuf[1024];
434 ERROR ("plugin_register_read: malloc failed: %s",
435 sstrerror (errno, errbuf, sizeof (errbuf)));
436 return (-1);
437 }
439 memset (rf, '\0', sizeof (read_func_t));
440 rf->wait_time = interval_g;
441 rf->wait_left = 0;
442 rf->callback = callback;
443 rf->needs_read = DONE;
445 return (register_callback (&list_read, name, (void *) rf));
446 } /* int plugin_register_read */
448 int plugin_register_write (const char *name,
449 int (*callback) (const data_set_t *ds, const value_list_t *vl))
450 {
451 return (register_callback (&list_write, name, (void *) callback));
452 } /* int plugin_register_write */
454 int plugin_register_flush (const char *name,
455 int (*callback) (const int timeout, const char *identifier))
456 {
457 return (register_callback (&list_flush, name, (void *) callback));
458 } /* int plugin_register_flush */
460 int plugin_register_shutdown (char *name,
461 int (*callback) (void))
462 {
463 return (register_callback (&list_shutdown, name, (void *) callback));
464 } /* int plugin_register_shutdown */
466 int plugin_register_data_set (const data_set_t *ds)
467 {
468 data_set_t *ds_copy;
469 int i;
471 if ((data_sets != NULL)
472 && (c_avl_get (data_sets, ds->type, NULL) == 0))
473 {
474 NOTICE ("Replacing DS `%s' with another version.", ds->type);
475 plugin_unregister_data_set (ds->type);
476 }
477 else if (data_sets == NULL)
478 {
479 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
480 if (data_sets == NULL)
481 return (-1);
482 }
484 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
485 if (ds_copy == NULL)
486 return (-1);
487 memcpy(ds_copy, ds, sizeof (data_set_t));
489 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
490 * ds->ds_num);
491 if (ds_copy->ds == NULL)
492 {
493 free (ds_copy);
494 return (-1);
495 }
497 for (i = 0; i < ds->ds_num; i++)
498 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
500 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
501 } /* int plugin_register_data_set */
503 int plugin_register_log (char *name,
504 void (*callback) (int priority, const char *msg))
505 {
506 return (register_callback (&list_log, name, (void *) callback));
507 } /* int plugin_register_log */
509 int plugin_register_notification (const char *name,
510 int (*callback) (const notification_t *notif))
511 {
512 return (register_callback (&list_notification, name, (void *) callback));
513 } /* int plugin_register_log */
515 int plugin_unregister_config (const char *name)
516 {
517 cf_unregister (name);
518 return (0);
519 } /* int plugin_unregister_config */
521 int plugin_unregister_complex_config (const char *name)
522 {
523 cf_unregister_complex (name);
524 return (0);
525 } /* int plugin_unregister_complex_config */
527 int plugin_unregister_init (const char *name)
528 {
529 return (plugin_unregister (list_init, name));
530 }
532 int plugin_unregister_read (const char *name)
533 {
534 llentry_t *e;
536 e = llist_search (list_read, name);
538 if (e == NULL)
539 return (-1);
541 llist_remove (list_read, e);
542 free (e->value);
543 free (e->key);
544 llentry_destroy (e);
546 return (0);
547 }
549 int plugin_unregister_write (const char *name)
550 {
551 return (plugin_unregister (list_write, name));
552 }
554 int plugin_unregister_flush (const char *name)
555 {
556 return (plugin_unregister (list_flush, name));
557 }
559 int plugin_unregister_shutdown (const char *name)
560 {
561 return (plugin_unregister (list_shutdown, name));
562 }
564 int plugin_unregister_data_set (const char *name)
565 {
566 data_set_t *ds;
568 if (data_sets == NULL)
569 return (-1);
571 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
572 return (-1);
574 sfree (ds->ds);
575 sfree (ds);
577 return (0);
578 } /* int plugin_unregister_data_set */
580 int plugin_unregister_log (const char *name)
581 {
582 return (plugin_unregister (list_log, name));
583 }
585 int plugin_unregister_notification (const char *name)
586 {
587 return (plugin_unregister (list_notification, name));
588 }
590 void plugin_init_all (void)
591 {
592 const char *chain_name;
593 int (*callback) (void);
594 llentry_t *le;
595 int status;
597 /* Init the value cache */
598 uc_init ();
600 chain_name = global_option_get ("PreCacheChain");
601 pre_cache_chain = fc_chain_get_by_name (chain_name);
603 chain_name = global_option_get ("PostCacheChain");
604 post_cache_chain = fc_chain_get_by_name (chain_name);
607 if ((list_init == NULL) && (list_read == NULL))
608 return;
610 /* Calling all init callbacks before checking if read callbacks
611 * are available allows the init callbacks to register the read
612 * callback. */
613 le = llist_head (list_init);
614 while (le != NULL)
615 {
616 callback = (int (*) (void)) le->value;
617 status = (*callback) ();
619 if (status != 0)
620 {
621 ERROR ("Initialization of plugin `%s' "
622 "failed with status %i. "
623 "Plugin will be unloaded.",
624 le->key, status);
625 /* Plugins that register read callbacks from the init
626 * callback should take care of appropriate error
627 * handling themselves. */
628 /* FIXME: Unload _all_ functions */
629 plugin_unregister_read (le->key);
630 }
632 le = le->next;
633 }
635 /* Start read-threads */
636 if (list_read != NULL)
637 {
638 const char *rt;
639 int num;
640 rt = global_option_get ("ReadThreads");
641 num = atoi (rt);
642 if (num != -1)
643 start_threads ((num > 0) ? num : 5);
644 }
645 } /* void plugin_init_all */
647 void plugin_read_all (void)
648 {
649 llentry_t *le;
650 read_func_t *rf;
652 uc_check_timeout ();
654 if (list_read == NULL)
655 return;
657 pthread_mutex_lock (&read_lock);
659 le = llist_head (list_read);
660 while (le != NULL)
661 {
662 rf = (read_func_t *) le->value;
664 if (rf->needs_read != DONE)
665 {
666 le = le->next;
667 continue;
668 }
670 if (rf->wait_left > 0)
671 rf->wait_left -= interval_g;
673 if (rf->wait_left <= 0)
674 {
675 rf->needs_read = TODO;
676 }
678 le = le->next;
679 }
681 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
682 pthread_cond_broadcast (&read_cond);
683 pthread_mutex_unlock (&read_lock);
684 } /* void plugin_read_all */
686 /* Read function called when the `-T' command line argument is given. */
687 int plugin_read_all_once (void)
688 {
689 llentry_t *le;
690 read_func_t *rf;
691 int status;
692 int return_status = 0;
694 if (list_read == NULL)
695 {
696 NOTICE ("No read-functions are registered.");
697 return (0);
698 }
700 for (le = llist_head (list_read);
701 le != NULL;
702 le = le->next)
703 {
704 rf = (read_func_t *) le->value;
705 status = rf->callback ();
706 if (status != 0)
707 {
708 NOTICE ("read-function of plugin `%s' failed.",
709 le->key);
710 return_status = -1;
711 }
712 }
714 return (return_status);
715 } /* int plugin_read_all_once */
717 int plugin_write (const char *plugin, /* {{{ */
718 const data_set_t *ds, const value_list_t *vl)
719 {
720 int (*callback) (const data_set_t *ds, const value_list_t *vl);
721 llentry_t *le;
722 int status;
724 if (vl == NULL)
725 return (EINVAL);
727 if (list_write == NULL)
728 return (ENOENT);
730 if (ds == NULL)
731 {
732 ds = plugin_get_ds (vl->type);
733 if (ds == NULL)
734 {
735 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
736 return (ENOENT);
737 }
738 }
740 if (plugin == NULL)
741 {
742 int success = 0;
743 int failure = 0;
745 le = llist_head (list_write);
746 while (le != NULL)
747 {
748 callback = le->value;
749 status = (*callback) (ds, vl);
750 if (status != 0)
751 failure++;
752 else
753 success++;
755 le = le->next;
756 }
758 if ((success == 0) && (failure != 0))
759 status = -1;
760 else
761 status = 0;
762 }
763 else /* plugin != NULL */
764 {
765 le = llist_head (list_write);
766 while (le != NULL)
767 {
768 if (strcasecmp (plugin, le->key) == 0)
769 break;
771 le = le->next;
772 }
774 if (le == NULL)
775 return (ENOENT);
777 callback = le->value;
778 status = (*callback) (ds, vl);
779 }
781 return (status);
782 } /* }}} int plugin_write */
784 int plugin_flush (const char *plugin, int timeout, const char *identifier)
785 {
786 int (*callback) (int timeout, const char *identifier);
787 llentry_t *le;
789 if (list_flush == NULL)
790 return (0);
792 le = llist_head (list_flush);
793 while (le != NULL)
794 {
795 if ((plugin != NULL)
796 && (strcmp (plugin, le->key) != 0))
797 {
798 le = le->next;
799 continue;
800 }
802 callback = (int (*) (int, const char *)) le->value;
803 (*callback) (timeout, identifier);
805 le = le->next;
806 }
807 return (0);
808 } /* int plugin_flush */
810 void plugin_shutdown_all (void)
811 {
812 int (*callback) (void);
813 llentry_t *le;
815 stop_threads ();
817 if (list_shutdown == NULL)
818 return;
820 le = llist_head (list_shutdown);
821 while (le != NULL)
822 {
823 callback = (int (*) (void)) le->value;
825 /* Advance the pointer before calling the callback allows
826 * shutdown functions to unregister themselves. If done the
827 * other way around the memory `le' points to will be freed
828 * after callback returns. */
829 le = le->next;
831 (*callback) ();
832 }
833 } /* void plugin_shutdown_all */
835 int plugin_dispatch_values (value_list_t *vl)
836 {
837 int status;
838 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
840 value_t *saved_values;
841 int saved_values_len;
843 data_set_t *ds;
845 if ((vl == NULL) || (vl->type[0] == 0)
846 || (vl->values == NULL) || (vl->values_len < 1))
847 {
848 ERROR ("plugin_dispatch_values: Invalid value list.");
849 return (-1);
850 }
852 if (list_write == NULL)
853 c_complain_once (LOG_WARNING, &no_write_complaint,
854 "plugin_dispatch_values: No write callback has been "
855 "registered. Please load at least one output plugin, "
856 "if you want the collected data to be stored.");
858 if (data_sets == NULL)
859 {
860 ERROR ("plugin_dispatch_values: No data sets registered. "
861 "Could the types database be read? Check "
862 "your `TypesDB' setting!");
863 return (-1);
864 }
866 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
867 {
868 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
869 return (-1);
870 }
872 if (vl->time == 0)
873 vl->time = time (NULL);
875 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
876 "host = %s; "
877 "plugin = %s; plugin_instance = %s; "
878 "type = %s; type_instance = %s;",
879 (unsigned int) vl->time, vl->interval,
880 vl->host,
881 vl->plugin, vl->plugin_instance,
882 vl->type, vl->type_instance);
884 #if COLLECT_DEBUG
885 assert (0 == strcmp (ds->type, vl->type));
886 #else
887 if (0 != strcmp (ds->type, vl->type))
888 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
889 ds->type, vl->type);
890 #endif
892 #if COLLECT_DEBUG
893 assert (ds->ds_num == vl->values_len);
894 #else
895 if (ds->ds_num != vl->values_len)
896 {
897 ERROR ("plugin_dispatch_values: ds->type = %s: "
898 "(ds->ds_num = %i) != "
899 "(vl->values_len = %i)",
900 ds->type, ds->ds_num, vl->values_len);
901 return (-1);
902 }
903 #endif
905 escape_slashes (vl->host, sizeof (vl->host));
906 escape_slashes (vl->plugin, sizeof (vl->plugin));
907 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
908 escape_slashes (vl->type, sizeof (vl->type));
909 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
911 /* Copy the values. This way, we can assure `targets' that they get
912 * dynamically allocated values, which they can free and replace if
913 * they like. */
914 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
915 {
916 saved_values = vl->values;
917 saved_values_len = vl->values_len;
919 vl->values = (value_t *) calloc (vl->values_len,
920 sizeof (*vl->values));
921 if (vl->values == NULL)
922 {
923 ERROR ("plugin_dispatch_values: calloc failed.");
924 vl->values = saved_values;
925 return (-1);
926 }
927 memcpy (vl->values, saved_values,
928 vl->values_len * sizeof (*vl->values));
929 }
930 else /* if ((pre == NULL) && (post == NULL)) */
931 {
932 saved_values = NULL;
933 saved_values_len = 0;
934 }
936 if (pre_cache_chain != NULL)
937 {
938 status = fc_process_chain (ds, vl, pre_cache_chain);
939 if (status < 0)
940 {
941 WARNING ("plugin_dispatch_values: Running the "
942 "pre-cache chain failed with "
943 "status %i (%#x).",
944 status, status);
945 }
946 else if (status == FC_TARGET_STOP)
947 {
948 /* Restore the state of the value_list so that plugins
949 * don't get confused.. */
950 if (saved_values != NULL)
951 {
952 free (vl->values);
953 vl->values = saved_values;
954 vl->values_len = saved_values_len;
955 }
956 return (0);
957 }
958 }
960 /* Update the value cache */
961 uc_update (ds, vl);
963 if (post_cache_chain != NULL)
964 {
965 status = fc_process_chain (ds, vl, post_cache_chain);
966 if (status < 0)
967 {
968 WARNING ("plugin_dispatch_values: Running the "
969 "post-cache chain failed with "
970 "status %i (%#x).",
971 status, status);
972 }
973 }
974 else
975 fc_default_action (ds, vl);
977 /* Restore the state of the value_list so that plugins don't get
978 * confused.. */
979 if (saved_values != NULL)
980 {
981 free (vl->values);
982 vl->values = saved_values;
983 vl->values_len = saved_values_len;
984 }
986 return (0);
987 } /* int plugin_dispatch_values */
989 int plugin_dispatch_notification (const notification_t *notif)
990 {
991 int (*callback) (const notification_t *);
992 llentry_t *le;
993 /* Possible TODO: Add flap detection here */
995 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
996 "time = %u; host = %s;",
997 notif->severity, notif->message,
998 (unsigned int) notif->time, notif->host);
1000 /* Nobody cares for notifications */
1001 if (list_notification == NULL)
1002 return (-1);
1004 le = llist_head (list_notification);
1005 while (le != NULL)
1006 {
1007 callback = (int (*) (const notification_t *)) le->value;
1008 (*callback) (notif);
1010 le = le->next;
1011 }
1013 return (0);
1014 } /* int plugin_dispatch_notification */
1016 void plugin_log (int level, const char *format, ...)
1017 {
1018 char msg[1024];
1019 va_list ap;
1021 void (*callback) (int, const char *);
1022 llentry_t *le;
1024 if (list_log == NULL)
1025 return;
1027 #if !COLLECT_DEBUG
1028 if (level >= LOG_DEBUG)
1029 return;
1030 #endif
1032 va_start (ap, format);
1033 vsnprintf (msg, sizeof (msg), format, ap);
1034 msg[sizeof (msg) - 1] = '\0';
1035 va_end (ap);
1037 le = llist_head (list_log);
1038 while (le != NULL)
1039 {
1040 callback = (void (*) (int, const char *)) le->value;
1041 (*callback) (level, msg);
1043 le = le->next;
1044 }
1045 } /* void plugin_log */
1047 const data_set_t *plugin_get_ds (const char *name)
1048 {
1049 data_set_t *ds;
1051 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1052 {
1053 DEBUG ("No such dataset registered: %s", name);
1054 return (NULL);
1055 }
1057 return (ds);
1058 } /* data_set_t *plugin_get_ds */
1060 static int plugin_notification_meta_add (notification_t *n,
1061 const char *name,
1062 enum notification_meta_type_e type,
1063 const void *value)
1064 {
1065 notification_meta_t *meta;
1066 notification_meta_t *tail;
1068 if ((n == NULL) || (name == NULL) || (value == NULL))
1069 {
1070 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1071 return (-1);
1072 }
1074 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1075 if (meta == NULL)
1076 {
1077 ERROR ("plugin_notification_meta_add: malloc failed.");
1078 return (-1);
1079 }
1080 memset (meta, 0, sizeof (notification_meta_t));
1082 sstrncpy (meta->name, name, sizeof (meta->name));
1083 meta->type = type;
1085 switch (type)
1086 {
1087 case NM_TYPE_STRING:
1088 {
1089 meta->nm_value.nm_string = strdup ((const char *) value);
1090 if (meta->nm_value.nm_string == NULL)
1091 {
1092 ERROR ("plugin_notification_meta_add: strdup failed.");
1093 sfree (meta);
1094 return (-1);
1095 }
1096 break;
1097 }
1098 case NM_TYPE_SIGNED_INT:
1099 {
1100 meta->nm_value.nm_signed_int = *((int64_t *) value);
1101 break;
1102 }
1103 case NM_TYPE_UNSIGNED_INT:
1104 {
1105 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1106 break;
1107 }
1108 case NM_TYPE_DOUBLE:
1109 {
1110 meta->nm_value.nm_double = *((double *) value);
1111 break;
1112 }
1113 case NM_TYPE_BOOLEAN:
1114 {
1115 meta->nm_value.nm_boolean = *((bool *) value);
1116 break;
1117 }
1118 default:
1119 {
1120 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1121 sfree (meta);
1122 return (-1);
1123 }
1124 } /* switch (type) */
1126 meta->next = NULL;
1127 tail = n->meta;
1128 while ((tail != NULL) && (tail->next != NULL))
1129 tail = tail->next;
1131 if (tail == NULL)
1132 n->meta = meta;
1133 else
1134 tail->next = meta;
1136 return (0);
1137 } /* int plugin_notification_meta_add */
1139 int plugin_notification_meta_add_string (notification_t *n,
1140 const char *name,
1141 const char *value)
1142 {
1143 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1144 }
1146 int plugin_notification_meta_add_signed_int (notification_t *n,
1147 const char *name,
1148 int64_t value)
1149 {
1150 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1151 }
1153 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1154 const char *name,
1155 uint64_t value)
1156 {
1157 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1158 }
1160 int plugin_notification_meta_add_double (notification_t *n,
1161 const char *name,
1162 double value)
1163 {
1164 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1165 }
1167 int plugin_notification_meta_add_boolean (notification_t *n,
1168 const char *name,
1169 bool value)
1170 {
1171 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1172 }
1174 int plugin_notification_meta_copy (notification_t *dst,
1175 const notification_t *src)
1176 {
1177 notification_meta_t *meta;
1179 assert (dst != NULL);
1180 assert (src != NULL);
1181 assert (dst != src);
1182 assert ((src->meta == NULL) || (src->meta != dst->meta));
1184 for (meta = src->meta; meta != NULL; meta = meta->next)
1185 {
1186 if (meta->type == NM_TYPE_STRING)
1187 plugin_notification_meta_add_string (dst, meta->name,
1188 meta->nm_value.nm_string);
1189 else if (meta->type == NM_TYPE_SIGNED_INT)
1190 plugin_notification_meta_add_signed_int (dst, meta->name,
1191 meta->nm_value.nm_signed_int);
1192 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1193 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1194 meta->nm_value.nm_unsigned_int);
1195 else if (meta->type == NM_TYPE_DOUBLE)
1196 plugin_notification_meta_add_double (dst, meta->name,
1197 meta->nm_value.nm_double);
1198 else if (meta->type == NM_TYPE_BOOLEAN)
1199 plugin_notification_meta_add_boolean (dst, meta->name,
1200 meta->nm_value.nm_boolean);
1201 }
1203 return (0);
1204 } /* int plugin_notification_meta_copy */
1206 int plugin_notification_meta_free (notification_meta_t *n)
1207 {
1208 notification_meta_t *this;
1209 notification_meta_t *next;
1211 if (n == NULL)
1212 {
1213 ERROR ("plugin_notification_meta_free: n == NULL!");
1214 return (-1);
1215 }
1217 this = n;
1218 while (this != NULL)
1219 {
1220 next = this->next;
1222 if (this->type == NM_TYPE_STRING)
1223 {
1224 free ((char *)this->nm_value.nm_string);
1225 this->nm_value.nm_string = NULL;
1226 }
1227 sfree (this);
1229 this = next;
1230 }
1232 return (0);
1233 } /* int plugin_notification_meta_free */