cd7b10834c4559f8e456ef8e6efe20bdf51f4271
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_filter;
60 static llist_t *list_flush;
61 static llist_t *list_shutdown;
62 static llist_t *list_log;
63 static llist_t *list_notification;
65 static fc_chain_t *pre_cache_chain = NULL;
66 static fc_chain_t *post_cache_chain = NULL;
68 static c_avl_tree_t *data_sets;
70 static char *plugindir = NULL;
72 static int read_loop = 1;
73 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
75 static pthread_t *read_threads = NULL;
76 static int read_threads_num = 0;
78 /*
79 * Static functions
80 */
81 static const char *plugin_get_dir (void)
82 {
83 if (plugindir == NULL)
84 return (PLUGINDIR);
85 else
86 return (plugindir);
87 }
89 static int register_callback (llist_t **list, const char *name, void *callback)
90 {
91 llentry_t *le;
92 char *key;
94 if ((*list == NULL)
95 && ((*list = llist_create ()) == NULL))
96 return (-1);
98 le = llist_search (*list, name);
99 if (le == NULL)
100 {
101 key = strdup (name);
102 if (key == NULL)
103 return (-1);
105 le = llentry_create (key, callback);
106 if (le == NULL)
107 {
108 free (key);
109 return (-1);
110 }
112 llist_append (*list, le);
113 }
114 else
115 {
116 le->value = callback;
117 }
119 return (0);
120 } /* int register_callback */
122 static int plugin_unregister (llist_t *list, const char *name)
123 {
124 llentry_t *e;
126 e = llist_search (list, name);
128 if (e == NULL)
129 return (-1);
131 llist_remove (list, e);
132 free (e->key);
133 llentry_destroy (e);
135 return (0);
136 } /* int plugin_unregister */
138 /*
139 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
140 * object, but it will bitch about a shared object not having a
141 * ``module_register'' symbol..
142 */
143 static int plugin_load_file (char *file)
144 {
145 lt_dlhandle dlh;
146 void (*reg_handle) (void);
148 DEBUG ("file = %s", file);
150 lt_dlinit ();
151 lt_dlerror (); /* clear errors */
153 if ((dlh = lt_dlopen (file)) == NULL)
154 {
155 const char *error = lt_dlerror ();
157 ERROR ("lt_dlopen (%s) failed: %s", file, error);
158 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
159 return (1);
160 }
162 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
163 {
164 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
165 file, lt_dlerror ());
166 lt_dlclose (dlh);
167 return (-1);
168 }
170 (*reg_handle) ();
172 return (0);
173 }
175 static void *plugin_read_thread (void *args)
176 {
177 llentry_t *le;
178 read_func_t *rf;
179 int status;
180 int done;
182 pthread_mutex_lock (&read_lock);
184 while (read_loop != 0)
185 {
186 le = llist_head (list_read);
187 done = 0;
189 while ((read_loop != 0) && (le != NULL))
190 {
191 rf = (read_func_t *) le->value;
193 if (rf->needs_read != TODO)
194 {
195 le = le->next;
196 continue;
197 }
199 /* We will do this read function */
200 rf->needs_read = ACTIVE;
202 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
203 (unsigned long int) pthread_self (), le->key);
204 pthread_mutex_unlock (&read_lock);
206 status = rf->callback ();
207 done++;
209 if (status != 0)
210 {
211 if (rf->wait_time < interval_g)
212 rf->wait_time = interval_g;
213 rf->wait_left = rf->wait_time;
214 rf->wait_time = rf->wait_time * 2;
215 if (rf->wait_time > 86400)
216 rf->wait_time = 86400;
218 NOTICE ("read-function of plugin `%s' "
219 "failed. Will suspend it for %i "
220 "seconds.", le->key, rf->wait_left);
221 }
222 else
223 {
224 rf->wait_left = 0;
225 rf->wait_time = interval_g;
226 }
228 pthread_mutex_lock (&read_lock);
230 rf->needs_read = DONE;
231 le = le->next;
232 } /* while (le != NULL) */
234 if ((read_loop != 0) && (done == 0))
235 {
236 DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
237 (unsigned long int) pthread_self ());
238 pthread_cond_wait (&read_cond, &read_lock);
239 }
240 } /* while (read_loop) */
242 pthread_mutex_unlock (&read_lock);
244 pthread_exit (NULL);
245 return ((void *) 0);
246 } /* void *plugin_read_thread */
248 static void start_threads (int num)
249 {
250 int i;
252 if (read_threads != NULL)
253 return;
255 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
256 if (read_threads == NULL)
257 {
258 ERROR ("plugin: start_threads: calloc failed.");
259 return;
260 }
262 read_threads_num = 0;
263 for (i = 0; i < num; i++)
264 {
265 if (pthread_create (read_threads + read_threads_num, NULL,
266 plugin_read_thread, NULL) == 0)
267 {
268 read_threads_num++;
269 }
270 else
271 {
272 ERROR ("plugin: start_threads: pthread_create failed.");
273 return;
274 }
275 } /* for (i) */
276 } /* void start_threads */
278 static void stop_threads (void)
279 {
280 int i;
282 if (read_threads == NULL)
283 return;
285 pthread_mutex_lock (&read_lock);
286 read_loop = 0;
287 DEBUG ("plugin: stop_threads: Signalling `read_cond'");
288 pthread_cond_broadcast (&read_cond);
289 pthread_mutex_unlock (&read_lock);
291 for (i = 0; i < read_threads_num; i++)
292 {
293 if (pthread_join (read_threads[i], NULL) != 0)
294 {
295 ERROR ("plugin: stop_threads: pthread_join failed.");
296 }
297 read_threads[i] = (pthread_t) 0;
298 }
299 sfree (read_threads);
300 read_threads_num = 0;
301 } /* void stop_threads */
303 /*
304 * Public functions
305 */
306 void plugin_set_dir (const char *dir)
307 {
308 if (plugindir != NULL)
309 free (plugindir);
311 if (dir == NULL)
312 plugindir = NULL;
313 else if ((plugindir = strdup (dir)) == NULL)
314 {
315 char errbuf[1024];
316 ERROR ("strdup failed: %s",
317 sstrerror (errno, errbuf, sizeof (errbuf)));
318 }
319 }
321 #define BUFSIZE 512
322 int plugin_load (const char *type)
323 {
324 DIR *dh;
325 const char *dir;
326 char filename[BUFSIZE] = "";
327 char typename[BUFSIZE];
328 int typename_len;
329 int ret;
330 struct stat statbuf;
331 struct dirent *de;
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 if (ssnprintf (typename, sizeof (typename),
341 "%s.so", type) >= 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 if (ssnprintf (filename, sizeof (filename),
362 "%s/%s", dir, de->d_name) >= sizeof (filename))
363 {
364 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
365 continue;
366 }
368 if (lstat (filename, &statbuf) == -1)
369 {
370 char errbuf[1024];
371 WARNING ("stat %s: %s", filename,
372 sstrerror (errno, errbuf, sizeof (errbuf)));
373 continue;
374 }
375 else if (!S_ISREG (statbuf.st_mode))
376 {
377 /* don't follow symlinks */
378 continue;
379 }
381 if (plugin_load_file (filename) == 0)
382 {
383 /* success */
384 ret = 0;
385 break;
386 }
387 else
388 {
389 fprintf (stderr, "Unable to load plugin %s.\n", type);
390 }
391 }
393 closedir (dh);
395 if (filename[0] == '\0')
396 fprintf (stderr, "Could not find plugin %s.\n", type);
398 return (ret);
399 }
401 /*
402 * The `register_*' functions follow
403 */
404 int plugin_register_config (const char *name,
405 int (*callback) (const char *key, const char *val),
406 const char **keys, int keys_num)
407 {
408 cf_register (name, callback, keys, keys_num);
409 return (0);
410 } /* int plugin_register_config */
412 int plugin_register_complex_config (const char *type,
413 int (*callback) (oconfig_item_t *))
414 {
415 return (cf_register_complex (type, callback));
416 } /* int plugin_register_complex_config */
418 int plugin_register_init (const char *name,
419 int (*callback) (void))
420 {
421 return (register_callback (&list_init, name, (void *) callback));
422 } /* plugin_register_init */
424 int plugin_register_read (const char *name,
425 int (*callback) (void))
426 {
427 read_func_t *rf;
429 rf = (read_func_t *) malloc (sizeof (read_func_t));
430 if (rf == NULL)
431 {
432 char errbuf[1024];
433 ERROR ("plugin_register_read: malloc failed: %s",
434 sstrerror (errno, errbuf, sizeof (errbuf)));
435 return (-1);
436 }
438 memset (rf, '\0', sizeof (read_func_t));
439 rf->wait_time = interval_g;
440 rf->wait_left = 0;
441 rf->callback = callback;
442 rf->needs_read = DONE;
444 return (register_callback (&list_read, name, (void *) rf));
445 } /* int plugin_register_read */
447 int plugin_register_write (const char *name,
448 int (*callback) (const data_set_t *ds, const value_list_t *vl))
449 {
450 return (register_callback (&list_write, name, (void *) callback));
451 } /* int plugin_register_write */
453 int plugin_register_filter (const char *name,
454 int (*callback) (const data_set_t *ds, value_list_t *vl))
455 {
456 return (register_callback (&list_filter, name, (void *) callback));
457 } /* int plugin_register_filter */
459 int plugin_register_flush (const char *name,
460 int (*callback) (const int timeout, const char *identifier))
461 {
462 return (register_callback (&list_flush, name, (void *) callback));
463 } /* int plugin_register_flush */
465 int plugin_register_shutdown (char *name,
466 int (*callback) (void))
467 {
468 return (register_callback (&list_shutdown, name, (void *) callback));
469 } /* int plugin_register_shutdown */
471 int plugin_register_data_set (const data_set_t *ds)
472 {
473 data_set_t *ds_copy;
474 int i;
476 if ((data_sets != NULL)
477 && (c_avl_get (data_sets, ds->type, NULL) == 0))
478 {
479 NOTICE ("Replacing DS `%s' with another version.", ds->type);
480 plugin_unregister_data_set (ds->type);
481 }
482 else if (data_sets == NULL)
483 {
484 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
485 if (data_sets == NULL)
486 return (-1);
487 }
489 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
490 if (ds_copy == NULL)
491 return (-1);
492 memcpy(ds_copy, ds, sizeof (data_set_t));
494 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
495 * ds->ds_num);
496 if (ds_copy->ds == NULL)
497 {
498 free (ds_copy);
499 return (-1);
500 }
502 for (i = 0; i < ds->ds_num; i++)
503 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
505 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
506 } /* int plugin_register_data_set */
508 int plugin_register_log (char *name,
509 void (*callback) (int priority, const char *msg))
510 {
511 return (register_callback (&list_log, name, (void *) callback));
512 } /* int plugin_register_log */
514 int plugin_register_notification (const char *name,
515 int (*callback) (const notification_t *notif))
516 {
517 return (register_callback (&list_notification, name, (void *) callback));
518 } /* int plugin_register_log */
520 int plugin_unregister_config (const char *name)
521 {
522 cf_unregister (name);
523 return (0);
524 } /* int plugin_unregister_config */
526 int plugin_unregister_complex_config (const char *name)
527 {
528 cf_unregister_complex (name);
529 return (0);
530 } /* int plugin_unregister_complex_config */
532 int plugin_unregister_init (const char *name)
533 {
534 return (plugin_unregister (list_init, name));
535 }
537 int plugin_unregister_read (const char *name)
538 {
539 llentry_t *e;
541 e = llist_search (list_read, name);
543 if (e == NULL)
544 return (-1);
546 llist_remove (list_read, e);
547 free (e->value);
548 free (e->key);
549 llentry_destroy (e);
551 return (0);
552 }
554 int plugin_unregister_write (const char *name)
555 {
556 return (plugin_unregister (list_write, name));
557 }
559 int plugin_unregister_filter (const char *name)
560 {
561 return (plugin_unregister (list_filter, name));
562 }
564 int plugin_unregister_flush (const char *name)
565 {
566 return (plugin_unregister (list_flush, name));
567 }
569 int plugin_unregister_shutdown (const char *name)
570 {
571 return (plugin_unregister (list_shutdown, name));
572 }
574 int plugin_unregister_data_set (const char *name)
575 {
576 data_set_t *ds;
578 if (data_sets == NULL)
579 return (-1);
581 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
582 return (-1);
584 sfree (ds->ds);
585 sfree (ds);
587 return (0);
588 } /* int plugin_unregister_data_set */
590 int plugin_unregister_log (const char *name)
591 {
592 return (plugin_unregister (list_log, name));
593 }
595 int plugin_unregister_notification (const char *name)
596 {
597 return (plugin_unregister (list_notification, name));
598 }
600 void plugin_init_all (void)
601 {
602 const char *chain_name;
603 int (*callback) (void);
604 llentry_t *le;
605 int status;
607 /* Init the value cache */
608 uc_init ();
610 chain_name = global_option_get ("PreCacheChain");
611 pre_cache_chain = fc_chain_get_by_name (chain_name);
613 chain_name = global_option_get ("PostCacheChain");
614 post_cache_chain = fc_chain_get_by_name (chain_name);
617 if ((list_init == NULL) && (list_read == NULL))
618 return;
620 /* Calling all init callbacks before checking if read callbacks
621 * are available allows the init callbacks to register the read
622 * callback. */
623 le = llist_head (list_init);
624 while (le != NULL)
625 {
626 callback = (int (*) (void)) le->value;
627 status = (*callback) ();
629 if (status != 0)
630 {
631 ERROR ("Initialization of plugin `%s' "
632 "failed with status %i. "
633 "Plugin will be unloaded.",
634 le->key, status);
635 /* Plugins that register read callbacks from the init
636 * callback should take care of appropriate error
637 * handling themselves. */
638 /* FIXME: Unload _all_ functions */
639 plugin_unregister_read (le->key);
640 }
642 le = le->next;
643 }
645 /* Start read-threads */
646 if (list_read != NULL)
647 {
648 const char *rt;
649 int num;
650 rt = global_option_get ("ReadThreads");
651 num = atoi (rt);
652 if (num != -1)
653 start_threads ((num > 0) ? num : 5);
654 }
655 } /* void plugin_init_all */
657 void plugin_read_all (void)
658 {
659 llentry_t *le;
660 read_func_t *rf;
662 uc_check_timeout ();
664 if (list_read == NULL)
665 return;
667 pthread_mutex_lock (&read_lock);
669 le = llist_head (list_read);
670 while (le != NULL)
671 {
672 rf = (read_func_t *) le->value;
674 if (rf->needs_read != DONE)
675 {
676 le = le->next;
677 continue;
678 }
680 if (rf->wait_left > 0)
681 rf->wait_left -= interval_g;
683 if (rf->wait_left <= 0)
684 {
685 rf->needs_read = TODO;
686 }
688 le = le->next;
689 }
691 DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
692 pthread_cond_broadcast (&read_cond);
693 pthread_mutex_unlock (&read_lock);
694 } /* void plugin_read_all */
696 /* Read function called when the `-T' command line argument is given. */
697 int plugin_read_all_once (void)
698 {
699 llentry_t *le;
700 read_func_t *rf;
701 int status;
702 int return_status = 0;
704 if (list_read == NULL)
705 {
706 NOTICE ("No read-functions are registered.");
707 return (0);
708 }
710 for (le = llist_head (list_read);
711 le != NULL;
712 le = le->next)
713 {
714 rf = (read_func_t *) le->value;
715 status = rf->callback ();
716 if (status != 0)
717 {
718 NOTICE ("read-function of plugin `%s' failed.",
719 le->key);
720 return_status = -1;
721 }
722 }
724 return (return_status);
725 } /* int plugin_read_all_once */
727 int plugin_write (const char *plugin, /* {{{ */
728 const data_set_t *ds, const value_list_t *vl)
729 {
730 int (*callback) (const data_set_t *ds, const value_list_t *vl);
731 llentry_t *le;
732 int status;
734 if (vl == NULL)
735 return (EINVAL);
737 if (list_write == NULL)
738 return (ENOENT);
740 if (ds == NULL)
741 {
742 ds = plugin_get_ds (vl->type);
743 if (ds == NULL)
744 {
745 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
746 return (ENOENT);
747 }
748 }
750 if (plugin == NULL)
751 {
752 int success = 0;
753 int failure = 0;
755 le = llist_head (list_write);
756 while (le != NULL)
757 {
758 callback = le->value;
759 status = (*callback) (ds, vl);
760 if (status != 0)
761 failure++;
762 else
763 success++;
765 le = le->next;
766 }
768 if ((success == 0) && (failure != 0))
769 status = -1;
770 else
771 status = 0;
772 }
773 else /* plugin != NULL */
774 {
775 le = llist_head (list_write);
776 while (le != NULL)
777 {
778 if (strcasecmp (plugin, le->key) == 0)
779 break;
781 le = le->next;
782 }
784 if (le == NULL)
785 return (ENOENT);
787 callback = le->value;
788 status = (*callback) (ds, vl);
789 }
791 return (status);
792 } /* }}} int plugin_write */
794 int plugin_flush (const char *plugin, int timeout, const char *identifier)
795 {
796 int (*callback) (int timeout, const char *identifier);
797 llentry_t *le;
799 if (list_flush == NULL)
800 return (0);
802 le = llist_head (list_flush);
803 while (le != NULL)
804 {
805 if ((plugin != NULL)
806 && (strcmp (plugin, le->key) != 0))
807 {
808 le = le->next;
809 continue;
810 }
812 callback = (int (*) (int, const char *)) le->value;
813 (*callback) (timeout, identifier);
815 le = le->next;
816 }
817 return (0);
818 } /* int plugin_flush */
820 void plugin_shutdown_all (void)
821 {
822 int (*callback) (void);
823 llentry_t *le;
825 stop_threads ();
827 if (list_shutdown == NULL)
828 return;
830 le = llist_head (list_shutdown);
831 while (le != NULL)
832 {
833 callback = (int (*) (void)) le->value;
835 /* Advance the pointer before calling the callback allows
836 * shutdown functions to unregister themselves. If done the
837 * other way around the memory `le' points to will be freed
838 * after callback returns. */
839 le = le->next;
841 (*callback) ();
842 }
843 } /* void plugin_shutdown_all */
845 int plugin_dispatch_values (value_list_t *vl)
846 {
847 int status;
848 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
850 data_set_t *ds;
852 if ((vl == NULL) || (*vl->type == '\0')) {
853 ERROR ("plugin_dispatch_values: Invalid value list.");
854 return (-1);
855 }
857 if (list_write == NULL)
858 c_complain_once (LOG_WARNING, &no_write_complaint,
859 "plugin_dispatch_values: No write callback has been "
860 "registered. Please load at least one output plugin, "
861 "if you want the collected data to be stored.");
863 if (data_sets == NULL)
864 {
865 ERROR ("plugin_dispatch_values: No data sets registered. "
866 "Could the types database be read? Check "
867 "your `TypesDB' setting!");
868 return (-1);
869 }
871 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
872 {
873 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
874 return (-1);
875 }
877 if (vl->time == 0)
878 vl->time = time (NULL);
880 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
881 "host = %s; "
882 "plugin = %s; plugin_instance = %s; "
883 "type = %s; type_instance = %s;",
884 (unsigned int) vl->time, vl->interval,
885 vl->host,
886 vl->plugin, vl->plugin_instance,
887 vl->type, vl->type_instance);
889 #if COLLECT_DEBUG
890 assert (0 == strcmp (ds->type, vl->type));
891 #else
892 if (0 != strcmp (ds->type, vl->type))
893 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
894 ds->type, vl->type);
895 #endif
897 #if COLLECT_DEBUG
898 assert (ds->ds_num == vl->values_len);
899 #else
900 if (ds->ds_num != vl->values_len)
901 {
902 ERROR ("plugin_dispatch_values: ds->type = %s: "
903 "(ds->ds_num = %i) != "
904 "(vl->values_len = %i)",
905 ds->type, ds->ds_num, vl->values_len);
906 return (-1);
907 }
908 #endif
910 escape_slashes (vl->host, sizeof (vl->host));
911 escape_slashes (vl->plugin, sizeof (vl->plugin));
912 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
913 escape_slashes (vl->type, sizeof (vl->type));
914 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
916 if (pre_cache_chain != NULL)
917 {
918 status = fc_process_chain (ds, vl, pre_cache_chain);
919 if (status < 0)
920 {
921 WARNING ("plugin_dispatch_values: Running the "
922 "pre-cache chain failed with "
923 "status %i (%#x).",
924 status, status);
925 }
926 else if (status == FC_TARGET_STOP)
927 return (0);
928 }
930 /* Update the value cache */
931 uc_update (ds, vl);
933 if (post_cache_chain != NULL)
934 {
935 status = fc_process_chain (ds, vl, post_cache_chain);
936 if (status < 0)
937 {
938 WARNING ("plugin_dispatch_values: Running the "
939 "post-cache chain failed with "
940 "status %i (%#x).",
941 status, status);
942 }
943 }
944 else
945 fc_default_action (ds, vl);
947 return (0);
948 } /* int plugin_dispatch_values */
950 int plugin_dispatch_notification (const notification_t *notif)
951 {
952 int (*callback) (const notification_t *);
953 llentry_t *le;
954 /* Possible TODO: Add flap detection here */
956 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
957 "time = %u; host = %s;",
958 notif->severity, notif->message,
959 (unsigned int) notif->time, notif->host);
961 /* Nobody cares for notifications */
962 if (list_notification == NULL)
963 return (-1);
965 le = llist_head (list_notification);
966 while (le != NULL)
967 {
968 callback = (int (*) (const notification_t *)) le->value;
969 (*callback) (notif);
971 le = le->next;
972 }
974 return (0);
975 } /* int plugin_dispatch_notification */
977 void plugin_log (int level, const char *format, ...)
978 {
979 char msg[1024];
980 va_list ap;
982 void (*callback) (int, const char *);
983 llentry_t *le;
985 if (list_log == NULL)
986 return;
988 #if !COLLECT_DEBUG
989 if (level >= LOG_DEBUG)
990 return;
991 #endif
993 va_start (ap, format);
994 vsnprintf (msg, sizeof (msg), format, ap);
995 msg[sizeof (msg) - 1] = '\0';
996 va_end (ap);
998 le = llist_head (list_log);
999 while (le != NULL)
1000 {
1001 callback = (void (*) (int, const char *)) le->value;
1002 (*callback) (level, msg);
1004 le = le->next;
1005 }
1006 } /* void plugin_log */
1008 const data_set_t *plugin_get_ds (const char *name)
1009 {
1010 data_set_t *ds;
1012 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1013 {
1014 DEBUG ("No such dataset registered: %s", name);
1015 return (NULL);
1016 }
1018 return (ds);
1019 } /* data_set_t *plugin_get_ds */
1021 static int plugin_notification_meta_add (notification_t *n,
1022 const char *name,
1023 enum notification_meta_type_e type,
1024 const void *value)
1025 {
1026 notification_meta_t *meta;
1027 notification_meta_t *tail;
1029 if ((n == NULL) || (name == NULL) || (value == NULL))
1030 {
1031 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1032 return (-1);
1033 }
1035 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1036 if (meta == NULL)
1037 {
1038 ERROR ("plugin_notification_meta_add: malloc failed.");
1039 return (-1);
1040 }
1041 memset (meta, 0, sizeof (notification_meta_t));
1043 sstrncpy (meta->name, name, sizeof (meta->name));
1044 meta->type = type;
1046 switch (type)
1047 {
1048 case NM_TYPE_STRING:
1049 {
1050 meta->nm_value.nm_string = strdup ((const char *) value);
1051 if (meta->nm_value.nm_string == NULL)
1052 {
1053 ERROR ("plugin_notification_meta_add: strdup failed.");
1054 sfree (meta);
1055 return (-1);
1056 }
1057 break;
1058 }
1059 case NM_TYPE_SIGNED_INT:
1060 {
1061 meta->nm_value.nm_signed_int = *((int64_t *) value);
1062 break;
1063 }
1064 case NM_TYPE_UNSIGNED_INT:
1065 {
1066 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1067 break;
1068 }
1069 case NM_TYPE_DOUBLE:
1070 {
1071 meta->nm_value.nm_double = *((double *) value);
1072 break;
1073 }
1074 case NM_TYPE_BOOLEAN:
1075 {
1076 meta->nm_value.nm_boolean = *((bool *) value);
1077 break;
1078 }
1079 default:
1080 {
1081 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1082 sfree (meta);
1083 return (-1);
1084 }
1085 } /* switch (type) */
1087 meta->next = NULL;
1088 tail = n->meta;
1089 while ((tail != NULL) && (tail->next != NULL))
1090 tail = tail->next;
1092 if (tail == NULL)
1093 n->meta = meta;
1094 else
1095 tail->next = meta;
1097 return (0);
1098 } /* int plugin_notification_meta_add */
1100 int plugin_notification_meta_add_string (notification_t *n,
1101 const char *name,
1102 const char *value)
1103 {
1104 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1105 }
1107 int plugin_notification_meta_add_signed_int (notification_t *n,
1108 const char *name,
1109 int64_t value)
1110 {
1111 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1112 }
1114 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1115 const char *name,
1116 uint64_t value)
1117 {
1118 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1119 }
1121 int plugin_notification_meta_add_double (notification_t *n,
1122 const char *name,
1123 double value)
1124 {
1125 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1126 }
1128 int plugin_notification_meta_add_boolean (notification_t *n,
1129 const char *name,
1130 bool value)
1131 {
1132 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1133 }
1135 int plugin_notification_meta_copy (notification_t *dst,
1136 const notification_t *src)
1137 {
1138 notification_meta_t *meta;
1140 assert (dst != NULL);
1141 assert (src != NULL);
1142 assert (dst != src);
1143 assert ((src->meta == NULL) || (src->meta != dst->meta));
1145 for (meta = src->meta; meta != NULL; meta = meta->next)
1146 {
1147 if (meta->type == NM_TYPE_STRING)
1148 plugin_notification_meta_add_string (dst, meta->name,
1149 meta->nm_value.nm_string);
1150 else if (meta->type == NM_TYPE_SIGNED_INT)
1151 plugin_notification_meta_add_signed_int (dst, meta->name,
1152 meta->nm_value.nm_signed_int);
1153 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1154 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1155 meta->nm_value.nm_unsigned_int);
1156 else if (meta->type == NM_TYPE_DOUBLE)
1157 plugin_notification_meta_add_double (dst, meta->name,
1158 meta->nm_value.nm_double);
1159 else if (meta->type == NM_TYPE_BOOLEAN)
1160 plugin_notification_meta_add_boolean (dst, meta->name,
1161 meta->nm_value.nm_boolean);
1162 }
1164 return (0);
1165 } /* int plugin_notification_meta_copy */
1167 int plugin_notification_meta_free (notification_t *n)
1168 {
1169 notification_meta_t *this;
1170 notification_meta_t *next;
1172 if (n == NULL)
1173 {
1174 ERROR ("plugin_notification_meta_free: n == NULL!");
1175 return (-1);
1176 }
1178 this = n->meta;
1179 n->meta = NULL;
1180 while (this != NULL)
1181 {
1182 next = this->next;
1184 if (this->type == NM_TYPE_STRING)
1185 {
1186 free ((char *)this->nm_value.nm_string);
1187 this->nm_value.nm_string = NULL;
1188 }
1189 sfree (this);
1191 this = next;
1192 }
1194 return (0);
1195 } /* int plugin_notification_meta_free */