Merge branch 'collectd-4.6' into collectd-4.7
[collectd.git] / src / plugin.c
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  **/
22
23 #include "collectd.h"
24 #include "utils_complain.h"
25
26 #include <ltdl.h>
27
28 #if HAVE_PTHREAD_H
29 # include <pthread.h>
30 #endif
31
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_heap.h"
38 #include "utils_cache.h"
39 #include "utils_threshold.h"
40 #include "filter_chain.h"
41
42 /*
43  * Private structures
44  */
45 struct callback_func_s
46 {
47         void *cf_callback;
48         user_data_t cf_udata;
49 };
50 typedef struct callback_func_s callback_func_t;
51
52 #define RF_SIMPLE  0
53 #define RF_COMPLEX 1
54 struct read_func_s
55 {
56         /* `read_func_t' "inherits" from `callback_func_t'.
57          * The `rf_super' member MUST be the first one in this structure! */
58 #define rf_callback rf_super.cf_callback
59 #define rf_udata rf_super.cf_udata
60         callback_func_t rf_super;
61         char rf_name[DATA_MAX_NAME_LEN];
62         int rf_type;
63         struct timespec rf_interval;
64         struct timespec rf_effective_interval;
65         struct timespec rf_next_read;
66 };
67 typedef struct read_func_s read_func_t;
68
69 /*
70  * Private variables
71  */
72 static llist_t *list_init;
73 static llist_t *list_write;
74 static llist_t *list_flush;
75 static llist_t *list_shutdown;
76 static llist_t *list_log;
77 static llist_t *list_notification;
78
79 static fc_chain_t *pre_cache_chain = NULL;
80 static fc_chain_t *post_cache_chain = NULL;
81
82 static c_avl_tree_t *data_sets;
83
84 static char *plugindir = NULL;
85
86 static c_heap_t       *read_heap = NULL;
87 static int             read_loop = 1;
88 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
89 static pthread_cond_t  read_cond = PTHREAD_COND_INITIALIZER;
90 static pthread_t      *read_threads = NULL;
91 static int             read_threads_num = 0;
92
93 /*
94  * Static functions
95  */
96 static const char *plugin_get_dir (void)
97 {
98         if (plugindir == NULL)
99                 return (PLUGINDIR);
100         else
101                 return (plugindir);
102 }
103
104 static void destroy_callback (callback_func_t *cf) /* {{{ */
105 {
106         if (cf == NULL)
107                 return;
108
109         if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
110         {
111                 cf->cf_udata.free_func (cf->cf_udata.data);
112                 cf->cf_udata.data = NULL;
113                 cf->cf_udata.free_func = NULL;
114         }
115         sfree (cf);
116 } /* }}} void destroy_callback */
117
118 static void destroy_all_callbacks (llist_t **list) /* {{{ */
119 {
120         llentry_t *le;
121
122         if (*list == NULL)
123                 return;
124
125         le = llist_head (*list);
126         while (le != NULL)
127         {
128                 llentry_t *le_next;
129
130                 le_next = le->next;
131
132                 sfree (le->key);
133                 destroy_callback (le->value);
134                 le->value = NULL;
135
136                 le = le_next;
137         }
138
139         llist_destroy (*list);
140         *list = NULL;
141 } /* }}} void destroy_all_callbacks */
142
143 static void destroy_read_heap (void) /* {{{ */
144 {
145         if (read_heap == NULL)
146                 return;
147
148         while (42)
149         {
150                 callback_func_t *cf;
151
152                 cf = c_head_get_root (read_heap);
153                 if (cf == NULL)
154                         break;
155
156                 destroy_callback (cf);
157         }
158
159         c_heap_destroy (read_heap);
160         read_heap = NULL;
161 } /* }}} void destroy_read_heap */
162
163 static int register_callback (llist_t **list, /* {{{ */
164                 const char *name, callback_func_t *cf)
165 {
166         llentry_t *le;
167         char *key;
168
169         if (*list == NULL)
170         {
171                 *list = llist_create ();
172                 if (*list == NULL)
173                 {
174                         ERROR ("plugin: create_register_callback: "
175                                         "llist_create failed.");
176                         destroy_callback (cf);
177                         return (-1);
178                 }
179         }
180
181         key = strdup (name);
182         if (key == NULL)
183         {
184                 ERROR ("plugin: create_register_callback: strdup failed.");
185                 destroy_callback (cf);
186                 return (-1);
187         }
188
189         le = llist_search (*list, name);
190         if (le == NULL)
191         {
192                 le = llentry_create (key, cf);
193                 if (le == NULL)
194                 {
195                         ERROR ("plugin: create_register_callback: "
196                                         "llentry_create failed.");
197                         free (key);
198                         destroy_callback (cf);
199                         return (-1);
200                 }
201
202                 llist_append (*list, le);
203         }
204         else
205         {
206                 callback_func_t *old_cf;
207
208                 old_cf = le->value;
209                 le->value = cf;
210
211                 destroy_callback (old_cf);
212                 sfree (key);
213         }
214
215         return (0);
216 } /* }}} int register_callback */
217
218 static int create_register_callback (llist_t **list, /* {{{ */
219                 const char *name, void *callback, user_data_t *ud)
220 {
221         callback_func_t *cf;
222
223         cf = (callback_func_t *) malloc (sizeof (*cf));
224         if (cf == NULL)
225         {
226                 ERROR ("plugin: create_register_callback: malloc failed.");
227                 return (-1);
228         }
229         memset (cf, 0, sizeof (*cf));
230
231         cf->cf_callback = callback;
232         if (ud == NULL)
233         {
234                 cf->cf_udata.data = NULL;
235                 cf->cf_udata.free_func = NULL;
236         }
237         else
238         {
239                 cf->cf_udata = *ud;
240         }
241
242         return (register_callback (list, name, cf));
243 } /* }}} int create_register_callback */
244
245 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
246 {
247         llentry_t *e;
248
249         if (list == NULL)
250                 return (-1);
251
252         e = llist_search (list, name);
253         if (e == NULL)
254                 return (-1);
255
256         llist_remove (list, e);
257
258         sfree (e->key);
259         destroy_callback (e->value);
260
261         llentry_destroy (e);
262
263         return (0);
264 } /* }}} int plugin_unregister */
265
266 /*
267  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
268  * object, but it will bitch about a shared object not having a
269  * ``module_register'' symbol..
270  */
271 static int plugin_load_file (char *file)
272 {
273         lt_dlhandle dlh;
274         void (*reg_handle) (void);
275
276         DEBUG ("file = %s", file);
277
278         lt_dlinit ();
279         lt_dlerror (); /* clear errors */
280
281         if ((dlh = lt_dlopen (file)) == NULL)
282         {
283                 const char *error = lt_dlerror ();
284
285                 ERROR ("lt_dlopen (%s) failed: %s", file, error);
286                 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
287                 return (1);
288         }
289
290         if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
291         {
292                 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
293                                 file, lt_dlerror ());
294                 lt_dlclose (dlh);
295                 return (-1);
296         }
297
298         (*reg_handle) ();
299
300         return (0);
301 }
302
303 static void *plugin_read_thread (void __attribute__((unused)) *args)
304 {
305         while (read_loop != 0)
306         {
307                 read_func_t *rf;
308                 struct timeval now;
309                 int status;
310
311                 /* Get the read function that needs to be read next. */
312                 rf = c_head_get_root (read_heap);
313                 if (rf == NULL)
314                 {
315                         struct timespec abstime;
316
317                         gettimeofday (&now, /* timezone = */ NULL);
318
319                         abstime.tv_sec = now.tv_sec + interval_g;
320                         abstime.tv_nsec = 1000 * now.tv_usec;
321
322                         pthread_mutex_lock (&read_lock);
323                         pthread_cond_timedwait (&read_cond, &read_lock,
324                                         &abstime);
325                         pthread_mutex_unlock (&read_lock);
326                         continue;
327                 }
328
329                 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
330                 {
331                         gettimeofday (&now, /* timezone = */ NULL);
332
333                         rf->rf_interval.tv_sec = interval_g;
334                         rf->rf_interval.tv_nsec = 0;
335
336                         rf->rf_effective_interval = rf->rf_interval;
337
338                         rf->rf_next_read.tv_sec = now.tv_sec;
339                         rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
340                 }
341
342                 /* sleep until this entry is due,
343                  * using pthread_cond_timedwait */
344                 pthread_mutex_lock (&read_lock);
345                 pthread_cond_timedwait (&read_cond, &read_lock,
346                                 &rf->rf_next_read);
347                 pthread_mutex_unlock (&read_lock);
348
349                 /* Check if we're supposed to stop.. This may have interrupted
350                  * the sleep, too. */
351                 if (read_loop == 0)
352                 {
353                         /* Insert `rf' again, so it can be free'd correctly */
354                         c_heap_insert (read_heap, rf);
355                         break;
356                 }
357
358                 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
359
360                 if (rf->rf_type == RF_SIMPLE)
361                 {
362                         int (*callback) (void);
363
364                         callback = rf->rf_callback;
365                         status = (*callback) ();
366                 }
367                 else
368                 {
369                         plugin_read_cb callback;
370
371                         callback = rf->rf_callback;
372                         status = (*callback) (&rf->rf_udata);
373                 }
374
375                 /* If the function signals failure, we will increase the
376                  * intervals in which it will be called. */
377                 if (status != 0)
378                 {
379                         rf->rf_effective_interval.tv_sec *= 2;
380                         rf->rf_effective_interval.tv_nsec *= 2;
381                         NORMALIZE_TIMESPEC (rf->rf_effective_interval);
382
383                         if (rf->rf_effective_interval.tv_sec >= 86400)
384                         {
385                                 rf->rf_effective_interval.tv_sec = 86400;
386                                 rf->rf_effective_interval.tv_nsec = 0;
387                         }
388
389                         NOTICE ("read-function of plugin `%s' failed. "
390                                         "Will suspend it for %i seconds.",
391                                         rf->rf_name,
392                                         (int) rf->rf_effective_interval.tv_sec);
393                 }
394                 else
395                 {
396                         /* Success: Restore the interval, if it was changed. */
397                         rf->rf_effective_interval = rf->rf_interval;
398                 }
399
400                 /* update the ``next read due'' field */
401                 gettimeofday (&now, /* timezone = */ NULL);
402
403                 DEBUG ("plugin_read_thread: Effective interval of the "
404                                 "%s plugin is %i.%09i.",
405                                 rf->rf_name,
406                                 (int) rf->rf_effective_interval.tv_sec,
407                                 (int) rf->rf_effective_interval.tv_nsec);
408
409                 /* Calculate the next (absolute) time at which this function
410                  * should be called. */
411                 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
412                         + rf->rf_effective_interval.tv_sec;
413                 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
414                         + rf->rf_effective_interval.tv_nsec;
415                 NORMALIZE_TIMESPEC (rf->rf_next_read);
416
417                 /* Check, if `rf_next_read' is in the past. */
418                 if ((rf->rf_next_read.tv_sec < now.tv_sec)
419                                 || ((rf->rf_next_read.tv_sec == now.tv_sec)
420                                         && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
421                 {
422                         /* `rf_next_read' is in the past. Insert `now'
423                          * so this value doesn't trail off into the
424                          * past too much. */
425                         rf->rf_next_read.tv_sec = now.tv_sec;
426                         rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
427                 }
428
429                 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
430                                 rf->rf_name,
431                                 (int) rf->rf_next_read.tv_sec,
432                                 (int) rf->rf_next_read.tv_nsec);
433
434                 /* Re-insert this read function into the heap again. */
435                 c_heap_insert (read_heap, rf);
436         } /* while (read_loop) */
437
438         pthread_exit (NULL);
439         return ((void *) 0);
440 } /* void *plugin_read_thread */
441
442 static void start_read_threads (int num)
443 {
444         int i;
445
446         if (read_threads != NULL)
447                 return;
448
449         read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
450         if (read_threads == NULL)
451         {
452                 ERROR ("plugin: start_read_threads: calloc failed.");
453                 return;
454         }
455
456         read_threads_num = 0;
457         for (i = 0; i < num; i++)
458         {
459                 if (pthread_create (read_threads + read_threads_num, NULL,
460                                         plugin_read_thread, NULL) == 0)
461                 {
462                         read_threads_num++;
463                 }
464                 else
465                 {
466                         ERROR ("plugin: start_read_threads: pthread_create failed.");
467                         return;
468                 }
469         } /* for (i) */
470 } /* void start_read_threads */
471
472 static void stop_read_threads (void)
473 {
474         int i;
475
476         if (read_threads == NULL)
477                 return;
478
479         INFO ("collectd: Stopping %i read threads.", read_threads_num);
480
481         pthread_mutex_lock (&read_lock);
482         read_loop = 0;
483         DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
484         pthread_cond_broadcast (&read_cond);
485         pthread_mutex_unlock (&read_lock);
486
487         for (i = 0; i < read_threads_num; i++)
488         {
489                 if (pthread_join (read_threads[i], NULL) != 0)
490                 {
491                         ERROR ("plugin: stop_read_threads: pthread_join failed.");
492                 }
493                 read_threads[i] = (pthread_t) 0;
494         }
495         sfree (read_threads);
496         read_threads_num = 0;
497 } /* void stop_read_threads */
498
499 /*
500  * Public functions
501  */
502 void plugin_set_dir (const char *dir)
503 {
504         if (plugindir != NULL)
505                 free (plugindir);
506
507         if (dir == NULL)
508                 plugindir = NULL;
509         else if ((plugindir = strdup (dir)) == NULL)
510         {
511                 char errbuf[1024];
512                 ERROR ("strdup failed: %s",
513                                 sstrerror (errno, errbuf, sizeof (errbuf)));
514         }
515 }
516
517 #define BUFSIZE 512
518 int plugin_load (const char *type)
519 {
520         DIR  *dh;
521         const char *dir;
522         char  filename[BUFSIZE] = "";
523         char  typename[BUFSIZE];
524         int   typename_len;
525         int   ret;
526         struct stat    statbuf;
527         struct dirent *de;
528         int status;
529
530         DEBUG ("type = %s", type);
531
532         dir = plugin_get_dir ();
533         ret = 1;
534
535         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
536          * type when matching the filename */
537         status = ssnprintf (typename, sizeof (typename), "%s.so", type);
538         if ((status < 0) || ((size_t) status >= sizeof (typename)))
539         {
540                 WARNING ("snprintf: truncated: `%s.so'", type);
541                 return (-1);
542         }
543         typename_len = strlen (typename);
544
545         if ((dh = opendir (dir)) == NULL)
546         {
547                 char errbuf[1024];
548                 ERROR ("opendir (%s): %s", dir,
549                                 sstrerror (errno, errbuf, sizeof (errbuf)));
550                 return (-1);
551         }
552
553         while ((de = readdir (dh)) != NULL)
554         {
555                 if (strncasecmp (de->d_name, typename, typename_len))
556                         continue;
557
558                 status = ssnprintf (filename, sizeof (filename),
559                                 "%s/%s", dir, de->d_name);
560                 if ((status < 0) || ((size_t) status >= sizeof (filename)))
561                 {
562                         WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
563                         continue;
564                 }
565
566                 if (lstat (filename, &statbuf) == -1)
567                 {
568                         char errbuf[1024];
569                         WARNING ("stat %s: %s", filename,
570                                         sstrerror (errno, errbuf, sizeof (errbuf)));
571                         continue;
572                 }
573                 else if (!S_ISREG (statbuf.st_mode))
574                 {
575                         /* don't follow symlinks */
576                         WARNING ("stat %s: not a regular file", filename);
577                         continue;
578                 }
579
580                 if (plugin_load_file (filename) == 0)
581                 {
582                         /* success */
583                         ret = 0;
584                         break;
585                 }
586                 else
587                 {
588                         fprintf (stderr, "Unable to load plugin %s.\n", type);
589                 }
590         }
591
592         closedir (dh);
593
594         if (filename[0] == '\0')
595                 fprintf (stderr, "Could not find plugin %s.\n", type);
596
597         return (ret);
598 }
599
600 /*
601  * The `register_*' functions follow
602  */
603 int plugin_register_config (const char *name,
604                 int (*callback) (const char *key, const char *val),
605                 const char **keys, int keys_num)
606 {
607         cf_register (name, callback, keys, keys_num);
608         return (0);
609 } /* int plugin_register_config */
610
611 int plugin_register_complex_config (const char *type,
612                 int (*callback) (oconfig_item_t *))
613 {
614         return (cf_register_complex (type, callback));
615 } /* int plugin_register_complex_config */
616
617 int plugin_register_init (const char *name,
618                 int (*callback) (void))
619 {
620         return (create_register_callback (&list_init, name, (void *) callback,
621                                 /* user_data = */ NULL));
622 } /* plugin_register_init */
623
624 static int plugin_compare_read_func (const void *arg0, const void *arg1)
625 {
626         const read_func_t *rf0;
627         const read_func_t *rf1;
628
629         rf0 = arg0;
630         rf1 = arg1;
631
632         if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
633                 return (-1);
634         else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
635                 return (1);
636         else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
637                 return (-1);
638         else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
639                 return (1);
640         else
641                 return (0);
642 } /* int plugin_compare_read_func */
643
644 int plugin_register_read (const char *name,
645                 int (*callback) (void))
646 {
647         read_func_t *rf;
648
649         if (read_heap == NULL)
650         {
651                 read_heap = c_heap_create (plugin_compare_read_func);
652                 if (read_heap == NULL)
653                 {
654                         ERROR ("plugin_register_read: "
655                                         "c_heap_create failed.");
656                         return (-1);
657                 }
658         }
659
660         rf = (read_func_t *) malloc (sizeof (read_func_t));
661         if (rf == NULL)
662         {
663                 char errbuf[1024];
664                 ERROR ("plugin_register_read: malloc failed: %s",
665                                 sstrerror (errno, errbuf, sizeof (errbuf)));
666                 return (-1);
667         }
668
669         memset (rf, 0, sizeof (read_func_t));
670         rf->rf_callback = (void *) callback;
671         rf->rf_udata.data = NULL;
672         rf->rf_udata.free_func = NULL;
673         sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
674         rf->rf_type = RF_SIMPLE;
675         rf->rf_interval.tv_sec = 0;
676         rf->rf_interval.tv_nsec = 0;
677         rf->rf_effective_interval = rf->rf_interval;
678
679         return (c_heap_insert (read_heap, rf));
680 } /* int plugin_register_read */
681
682 int plugin_register_complex_read (const char *name,
683                 plugin_read_cb callback,
684                 const struct timespec *interval,
685                 user_data_t *user_data)
686 {
687         read_func_t *rf;
688
689         if (read_heap == NULL)
690         {
691                 read_heap = c_heap_create (plugin_compare_read_func);
692                 if (read_heap == NULL)
693                 {
694                         ERROR ("plugin_register_read: c_heap_create failed.");
695                         return (-1);
696                 }
697         }
698
699         rf = (read_func_t *) malloc (sizeof (read_func_t));
700         if (rf == NULL)
701         {
702                 ERROR ("plugin_register_complex_read: malloc failed.");
703                 return (-1);
704         }
705
706         memset (rf, 0, sizeof (read_func_t));
707         rf->rf_callback = (void *) callback;
708         sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
709         rf->rf_type = RF_COMPLEX;
710         if (interval != NULL)
711         {
712                 rf->rf_interval = *interval;
713         }
714         rf->rf_effective_interval = rf->rf_interval;
715
716         /* Set user data */
717         if (user_data == NULL)
718         {
719                 rf->rf_udata.data = NULL;
720                 rf->rf_udata.free_func = NULL;
721         }
722         else
723         {
724                 rf->rf_udata = *user_data;
725         }
726
727         return (c_heap_insert (read_heap, rf));
728 } /* int plugin_register_complex_read */
729
730 int plugin_register_write (const char *name,
731                 plugin_write_cb callback, user_data_t *ud)
732 {
733         return (create_register_callback (&list_write, name,
734                                 (void *) callback, ud));
735 } /* int plugin_register_write */
736
737 int plugin_register_flush (const char *name,
738                 plugin_flush_cb callback, user_data_t *ud)
739 {
740         return (create_register_callback (&list_flush, name,
741                                 (void *) callback, ud));
742 } /* int plugin_register_flush */
743
744 int plugin_register_shutdown (char *name,
745                 int (*callback) (void))
746 {
747         return (create_register_callback (&list_shutdown, name,
748                                 (void *) callback, /* user_data = */ NULL));
749 } /* int plugin_register_shutdown */
750
751 int plugin_register_data_set (const data_set_t *ds)
752 {
753         data_set_t *ds_copy;
754         int i;
755
756         if ((data_sets != NULL)
757                         && (c_avl_get (data_sets, ds->type, NULL) == 0))
758         {
759                 NOTICE ("Replacing DS `%s' with another version.", ds->type);
760                 plugin_unregister_data_set (ds->type);
761         }
762         else if (data_sets == NULL)
763         {
764                 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
765                 if (data_sets == NULL)
766                         return (-1);
767         }
768
769         ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
770         if (ds_copy == NULL)
771                 return (-1);
772         memcpy(ds_copy, ds, sizeof (data_set_t));
773
774         ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
775                         * ds->ds_num);
776         if (ds_copy->ds == NULL)
777         {
778                 free (ds_copy);
779                 return (-1);
780         }
781
782         for (i = 0; i < ds->ds_num; i++)
783                 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
784
785         return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
786 } /* int plugin_register_data_set */
787
788 int plugin_register_log (const char *name,
789                 plugin_log_cb callback, user_data_t *ud)
790 {
791         return (create_register_callback (&list_log, name,
792                                 (void *) callback, ud));
793 } /* int plugin_register_log */
794
795 int plugin_register_notification (const char *name,
796                 plugin_notification_cb callback, user_data_t *ud)
797 {
798         return (create_register_callback (&list_notification, name,
799                                 (void *) callback, ud));
800 } /* int plugin_register_log */
801
802 int plugin_unregister_config (const char *name)
803 {
804         cf_unregister (name);
805         return (0);
806 } /* int plugin_unregister_config */
807
808 int plugin_unregister_complex_config (const char *name)
809 {
810         cf_unregister_complex (name);
811         return (0);
812 } /* int plugin_unregister_complex_config */
813
814 int plugin_unregister_init (const char *name)
815 {
816         return (plugin_unregister (list_init, name));
817 }
818
819 int plugin_unregister_read (const char *name)
820 {
821         /* TODO: Implement removal of a specific key from the heap. */
822         assert (0);
823         return (-1);
824 }
825
826 int plugin_unregister_write (const char *name)
827 {
828         return (plugin_unregister (list_write, name));
829 }
830
831 int plugin_unregister_flush (const char *name)
832 {
833         return (plugin_unregister (list_flush, name));
834 }
835
836 int plugin_unregister_shutdown (const char *name)
837 {
838         return (plugin_unregister (list_shutdown, name));
839 }
840
841 int plugin_unregister_data_set (const char *name)
842 {
843         data_set_t *ds;
844
845         if (data_sets == NULL)
846                 return (-1);
847
848         if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
849                 return (-1);
850
851         sfree (ds->ds);
852         sfree (ds);
853
854         return (0);
855 } /* int plugin_unregister_data_set */
856
857 int plugin_unregister_log (const char *name)
858 {
859         return (plugin_unregister (list_log, name));
860 }
861
862 int plugin_unregister_notification (const char *name)
863 {
864         return (plugin_unregister (list_notification, name));
865 }
866
867 void plugin_init_all (void)
868 {
869         const char *chain_name;
870         llentry_t *le;
871         int status;
872
873         /* Init the value cache */
874         uc_init ();
875
876         chain_name = global_option_get ("PreCacheChain");
877         pre_cache_chain = fc_chain_get_by_name (chain_name);
878
879         chain_name = global_option_get ("PostCacheChain");
880         post_cache_chain = fc_chain_get_by_name (chain_name);
881
882
883         if ((list_init == NULL) && (read_heap == NULL))
884                 return;
885
886         /* Calling all init callbacks before checking if read callbacks
887          * are available allows the init callbacks to register the read
888          * callback. */
889         le = llist_head (list_init);
890         while (le != NULL)
891         {
892                 callback_func_t *cf;
893                 plugin_init_cb callback;
894
895                 cf = le->value;
896                 callback = cf->cf_callback;
897                 status = (*callback) ();
898
899                 if (status != 0)
900                 {
901                         ERROR ("Initialization of plugin `%s' "
902                                         "failed with status %i. "
903                                         "Plugin will be unloaded.",
904                                         le->key, status);
905                         /* Plugins that register read callbacks from the init
906                          * callback should take care of appropriate error
907                          * handling themselves. */
908                         /* FIXME: Unload _all_ functions */
909                         plugin_unregister_read (le->key);
910                 }
911
912                 le = le->next;
913         }
914
915         /* Start read-threads */
916         if (read_heap != NULL)
917         {
918                 const char *rt;
919                 int num;
920                 rt = global_option_get ("ReadThreads");
921                 num = atoi (rt);
922                 if (num != -1)
923                         start_read_threads ((num > 0) ? num : 5);
924         }
925 } /* void plugin_init_all */
926
927 /* TODO: Rename this function. */
928 void plugin_read_all (void)
929 {
930         uc_check_timeout ();
931
932         return;
933 } /* void plugin_read_all */
934
935 /* Read function called when the `-T' command line argument is given. */
936 int plugin_read_all_once (void)
937 {
938         int status;
939         int return_status = 0;
940
941         if (read_heap == NULL)
942         {
943                 NOTICE ("No read-functions are registered.");
944                 return (0);
945         }
946
947         while (42)
948         {
949                 read_func_t *rf;
950
951                 rf = c_head_get_root (read_heap);
952                 if (rf == NULL)
953                         break;
954
955                 if (rf->rf_type == RF_SIMPLE)
956                 {
957                         int (*callback) (void);
958
959                         callback = rf->rf_callback;
960                         status = (*callback) ();
961                 }
962                 else
963                 {
964                         plugin_read_cb callback;
965
966                         callback = rf->rf_callback;
967                         status = (*callback) (&rf->rf_udata);
968                 }
969
970                 if (status != 0)
971                 {
972                         NOTICE ("read-function of plugin `%s' failed.",
973                                         rf->rf_name);
974                         return_status = -1;
975                 }
976
977                 destroy_callback ((void *) rf);
978         }
979
980         return (return_status);
981 } /* int plugin_read_all_once */
982
983 int plugin_write (const char *plugin, /* {{{ */
984                 const data_set_t *ds, const value_list_t *vl)
985 {
986   llentry_t *le;
987   int status;
988
989   if (vl == NULL)
990     return (EINVAL);
991
992   if (list_write == NULL)
993     return (ENOENT);
994
995   if (ds == NULL)
996   {
997     ds = plugin_get_ds (vl->type);
998     if (ds == NULL)
999     {
1000       ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1001       return (ENOENT);
1002     }
1003   }
1004
1005   if (plugin == NULL)
1006   {
1007     int success = 0;
1008     int failure = 0;
1009
1010     le = llist_head (list_write);
1011     while (le != NULL)
1012     {
1013       callback_func_t *cf = le->value;
1014       plugin_write_cb callback;
1015
1016       DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1017       callback = cf->cf_callback;
1018       status = (*callback) (ds, vl, &cf->cf_udata);
1019       if (status != 0)
1020         failure++;
1021       else
1022         success++;
1023
1024       le = le->next;
1025     }
1026
1027     if ((success == 0) && (failure != 0))
1028       status = -1;
1029     else
1030       status = 0;
1031   }
1032   else /* plugin != NULL */
1033   {
1034     callback_func_t *cf;
1035     plugin_write_cb callback;
1036
1037     le = llist_head (list_write);
1038     while (le != NULL)
1039     {
1040       if (strcasecmp (plugin, le->key) == 0)
1041         break;
1042
1043       le = le->next;
1044     }
1045
1046     if (le == NULL)
1047       return (ENOENT);
1048
1049     cf = le->value;
1050
1051     DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1052     callback = cf->cf_callback;
1053     status = (*callback) (ds, vl, &cf->cf_udata);
1054   }
1055
1056   return (status);
1057 } /* }}} int plugin_write */
1058
1059 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1060 {
1061   llentry_t *le;
1062
1063   if (list_flush == NULL)
1064     return (0);
1065
1066   le = llist_head (list_flush);
1067   while (le != NULL)
1068   {
1069     callback_func_t *cf;
1070     plugin_flush_cb callback;
1071
1072     if ((plugin != NULL)
1073         && (strcmp (plugin, le->key) != 0))
1074     {
1075       le = le->next;
1076       continue;
1077     }
1078
1079     cf = le->value;
1080     callback = cf->cf_callback;
1081
1082     (*callback) (timeout, identifier, &cf->cf_udata);
1083
1084     le = le->next;
1085   }
1086   return (0);
1087 } /* int plugin_flush */
1088
1089 void plugin_shutdown_all (void)
1090 {
1091         llentry_t *le;
1092
1093         stop_read_threads ();
1094
1095         destroy_all_callbacks (&list_init);
1096         destroy_read_heap ();
1097
1098         plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1099                         /* identifier = */ NULL);
1100
1101         le = NULL;
1102         if (list_shutdown != NULL)
1103                 le = llist_head (list_shutdown);
1104
1105         while (le != NULL)
1106         {
1107                 callback_func_t *cf;
1108                 plugin_shutdown_cb callback;
1109
1110                 cf = le->value;
1111                 callback = cf->cf_callback;
1112
1113                 /* Advance the pointer before calling the callback allows
1114                  * shutdown functions to unregister themselves. If done the
1115                  * other way around the memory `le' points to will be freed
1116                  * after callback returns. */
1117                 le = le->next;
1118
1119                 (*callback) ();
1120         }
1121
1122         destroy_all_callbacks (&list_write);
1123         destroy_all_callbacks (&list_flush);
1124         destroy_all_callbacks (&list_notification);
1125         destroy_all_callbacks (&list_shutdown);
1126         destroy_all_callbacks (&list_log);
1127 } /* void plugin_shutdown_all */
1128
1129 int plugin_dispatch_values (value_list_t *vl)
1130 {
1131         int status;
1132         static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1133
1134         value_t *saved_values;
1135         int      saved_values_len;
1136
1137         data_set_t *ds;
1138
1139         if ((vl == NULL) || (vl->type[0] == 0)
1140                         || (vl->values == NULL) || (vl->values_len < 1))
1141         {
1142                 ERROR ("plugin_dispatch_values: Invalid value list.");
1143                 return (-1);
1144         }
1145
1146         if (list_write == NULL)
1147                 c_complain_once (LOG_WARNING, &no_write_complaint,
1148                                 "plugin_dispatch_values: No write callback has been "
1149                                 "registered. Please load at least one output plugin, "
1150                                 "if you want the collected data to be stored.");
1151
1152         if (data_sets == NULL)
1153         {
1154                 ERROR ("plugin_dispatch_values: No data sets registered. "
1155                                 "Could the types database be read? Check "
1156                                 "your `TypesDB' setting!");
1157                 return (-1);
1158         }
1159
1160         if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1161         {
1162                 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1163                 return (-1);
1164         }
1165
1166         if (vl->time == 0)
1167                 vl->time = time (NULL);
1168
1169         if (vl->interval <= 0)
1170                 vl->interval = interval_g;
1171
1172         DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1173                         "host = %s; "
1174                         "plugin = %s; plugin_instance = %s; "
1175                         "type = %s; type_instance = %s;",
1176                         (unsigned int) vl->time, vl->interval,
1177                         vl->host,
1178                         vl->plugin, vl->plugin_instance,
1179                         vl->type, vl->type_instance);
1180
1181 #if COLLECT_DEBUG
1182         assert (0 == strcmp (ds->type, vl->type));
1183 #else
1184         if (0 != strcmp (ds->type, vl->type))
1185                 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1186                                 ds->type, vl->type);
1187 #endif
1188
1189 #if COLLECT_DEBUG
1190         assert (ds->ds_num == vl->values_len);
1191 #else
1192         if (ds->ds_num != vl->values_len)
1193         {
1194                 ERROR ("plugin_dispatch_values: ds->type = %s: "
1195                                 "(ds->ds_num = %i) != "
1196                                 "(vl->values_len = %i)",
1197                                 ds->type, ds->ds_num, vl->values_len);
1198                 return (-1);
1199         }
1200 #endif
1201
1202         escape_slashes (vl->host, sizeof (vl->host));
1203         escape_slashes (vl->plugin, sizeof (vl->plugin));
1204         escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1205         escape_slashes (vl->type, sizeof (vl->type));
1206         escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1207
1208         /* Copy the values. This way, we can assure `targets' that they get
1209          * dynamically allocated values, which they can free and replace if
1210          * they like. */
1211         if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1212         {
1213                 saved_values     = vl->values;
1214                 saved_values_len = vl->values_len;
1215
1216                 vl->values = (value_t *) calloc (vl->values_len,
1217                                 sizeof (*vl->values));
1218                 if (vl->values == NULL)
1219                 {
1220                         ERROR ("plugin_dispatch_values: calloc failed.");
1221                         vl->values = saved_values;
1222                         return (-1);
1223                 }
1224                 memcpy (vl->values, saved_values,
1225                                 vl->values_len * sizeof (*vl->values));
1226         }
1227         else /* if ((pre == NULL) && (post == NULL)) */
1228         {
1229                 saved_values     = NULL;
1230                 saved_values_len = 0;
1231         }
1232
1233         if (pre_cache_chain != NULL)
1234         {
1235                 status = fc_process_chain (ds, vl, pre_cache_chain);
1236                 if (status < 0)
1237                 {
1238                         WARNING ("plugin_dispatch_values: Running the "
1239                                         "pre-cache chain failed with "
1240                                         "status %i (%#x).",
1241                                         status, status);
1242                 }
1243                 else if (status == FC_TARGET_STOP)
1244                 {
1245                         /* Restore the state of the value_list so that plugins
1246                          * don't get confused.. */
1247                         if (saved_values != NULL)
1248                         {
1249                                 free (vl->values);
1250                                 vl->values     = saved_values;
1251                                 vl->values_len = saved_values_len;
1252                         }
1253                         return (0);
1254                 }
1255         }
1256
1257         /* Update the value cache */
1258         uc_update (ds, vl);
1259
1260         if (post_cache_chain != NULL)
1261         {
1262                 status = fc_process_chain (ds, vl, post_cache_chain);
1263                 if (status < 0)
1264                 {
1265                         WARNING ("plugin_dispatch_values: Running the "
1266                                         "post-cache chain failed with "
1267                                         "status %i (%#x).",
1268                                         status, status);
1269                 }
1270         }
1271         else
1272                 fc_default_action (ds, vl);
1273
1274         /* Restore the state of the value_list so that plugins don't get
1275          * confused.. */
1276         if (saved_values != NULL)
1277         {
1278                 free (vl->values);
1279                 vl->values     = saved_values;
1280                 vl->values_len = saved_values_len;
1281         }
1282
1283         return (0);
1284 } /* int plugin_dispatch_values */
1285
1286 int plugin_dispatch_notification (const notification_t *notif)
1287 {
1288         llentry_t *le;
1289         /* Possible TODO: Add flap detection here */
1290
1291         DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1292                         "time = %u; host = %s;",
1293                         notif->severity, notif->message,
1294                         (unsigned int) notif->time, notif->host);
1295
1296         /* Nobody cares for notifications */
1297         if (list_notification == NULL)
1298                 return (-1);
1299
1300         le = llist_head (list_notification);
1301         while (le != NULL)
1302         {
1303                 callback_func_t *cf;
1304                 plugin_notification_cb callback;
1305                 int status;
1306
1307                 cf = le->value;
1308                 callback = cf->cf_callback;
1309                 status = (*callback) (notif, &cf->cf_udata);
1310                 if (status != 0)
1311                 {
1312                         WARNING ("plugin_dispatch_notification: Notification "
1313                                         "callback %s returned %i.",
1314                                         le->key, status);
1315                 }
1316
1317                 le = le->next;
1318         }
1319
1320         return (0);
1321 } /* int plugin_dispatch_notification */
1322
1323 void plugin_log (int level, const char *format, ...)
1324 {
1325         char msg[1024];
1326         va_list ap;
1327         llentry_t *le;
1328
1329         if (list_log == NULL)
1330                 return;
1331
1332 #if !COLLECT_DEBUG
1333         if (level >= LOG_DEBUG)
1334                 return;
1335 #endif
1336
1337         va_start (ap, format);
1338         vsnprintf (msg, sizeof (msg), format, ap);
1339         msg[sizeof (msg) - 1] = '\0';
1340         va_end (ap);
1341
1342         le = llist_head (list_log);
1343         while (le != NULL)
1344         {
1345                 callback_func_t *cf;
1346                 plugin_log_cb callback;
1347
1348                 cf = le->value;
1349                 callback = cf->cf_callback;
1350
1351                 (*callback) (level, msg, &cf->cf_udata);
1352
1353                 le = le->next;
1354         }
1355 } /* void plugin_log */
1356
1357 const data_set_t *plugin_get_ds (const char *name)
1358 {
1359         data_set_t *ds;
1360
1361         if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1362         {
1363                 DEBUG ("No such dataset registered: %s", name);
1364                 return (NULL);
1365         }
1366
1367         return (ds);
1368 } /* data_set_t *plugin_get_ds */
1369
1370 static int plugin_notification_meta_add (notification_t *n,
1371     const char *name,
1372     enum notification_meta_type_e type,
1373     const void *value)
1374 {
1375   notification_meta_t *meta;
1376   notification_meta_t *tail;
1377
1378   if ((n == NULL) || (name == NULL) || (value == NULL))
1379   {
1380     ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1381     return (-1);
1382   }
1383
1384   meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1385   if (meta == NULL)
1386   {
1387     ERROR ("plugin_notification_meta_add: malloc failed.");
1388     return (-1);
1389   }
1390   memset (meta, 0, sizeof (notification_meta_t));
1391
1392   sstrncpy (meta->name, name, sizeof (meta->name));
1393   meta->type = type;
1394
1395   switch (type)
1396   {
1397     case NM_TYPE_STRING:
1398     {
1399       meta->nm_value.nm_string = strdup ((const char *) value);
1400       if (meta->nm_value.nm_string == NULL)
1401       {
1402         ERROR ("plugin_notification_meta_add: strdup failed.");
1403         sfree (meta);
1404         return (-1);
1405       }
1406       break;
1407     }
1408     case NM_TYPE_SIGNED_INT:
1409     {
1410       meta->nm_value.nm_signed_int = *((int64_t *) value);
1411       break;
1412     }
1413     case NM_TYPE_UNSIGNED_INT:
1414     {
1415       meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1416       break;
1417     }
1418     case NM_TYPE_DOUBLE:
1419     {
1420       meta->nm_value.nm_double = *((double *) value);
1421       break;
1422     }
1423     case NM_TYPE_BOOLEAN:
1424     {
1425       meta->nm_value.nm_boolean = *((bool *) value);
1426       break;
1427     }
1428     default:
1429     {
1430       ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1431       sfree (meta);
1432       return (-1);
1433     }
1434   } /* switch (type) */
1435
1436   meta->next = NULL;
1437   tail = n->meta;
1438   while ((tail != NULL) && (tail->next != NULL))
1439     tail = tail->next;
1440
1441   if (tail == NULL)
1442     n->meta = meta;
1443   else
1444     tail->next = meta;
1445
1446   return (0);
1447 } /* int plugin_notification_meta_add */
1448
1449 int plugin_notification_meta_add_string (notification_t *n,
1450     const char *name,
1451     const char *value)
1452 {
1453   return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1454 }
1455
1456 int plugin_notification_meta_add_signed_int (notification_t *n,
1457     const char *name,
1458     int64_t value)
1459 {
1460   return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1461 }
1462
1463 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1464     const char *name,
1465     uint64_t value)
1466 {
1467   return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1468 }
1469
1470 int plugin_notification_meta_add_double (notification_t *n,
1471     const char *name,
1472     double value)
1473 {
1474   return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1475 }
1476
1477 int plugin_notification_meta_add_boolean (notification_t *n,
1478     const char *name,
1479     bool value)
1480 {
1481   return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1482 }
1483
1484 int plugin_notification_meta_copy (notification_t *dst,
1485     const notification_t *src)
1486 {
1487   notification_meta_t *meta;
1488
1489   assert (dst != NULL);
1490   assert (src != NULL);
1491   assert (dst != src);
1492   assert ((src->meta == NULL) || (src->meta != dst->meta));
1493
1494   for (meta = src->meta; meta != NULL; meta = meta->next)
1495   {
1496     if (meta->type == NM_TYPE_STRING)
1497       plugin_notification_meta_add_string (dst, meta->name,
1498           meta->nm_value.nm_string);
1499     else if (meta->type == NM_TYPE_SIGNED_INT)
1500       plugin_notification_meta_add_signed_int (dst, meta->name,
1501           meta->nm_value.nm_signed_int);
1502     else if (meta->type == NM_TYPE_UNSIGNED_INT)
1503       plugin_notification_meta_add_unsigned_int (dst, meta->name,
1504           meta->nm_value.nm_unsigned_int);
1505     else if (meta->type == NM_TYPE_DOUBLE)
1506       plugin_notification_meta_add_double (dst, meta->name,
1507           meta->nm_value.nm_double);
1508     else if (meta->type == NM_TYPE_BOOLEAN)
1509       plugin_notification_meta_add_boolean (dst, meta->name,
1510           meta->nm_value.nm_boolean);
1511   }
1512
1513   return (0);
1514 } /* int plugin_notification_meta_copy */
1515
1516 int plugin_notification_meta_free (notification_meta_t *n)
1517 {
1518   notification_meta_t *this;
1519   notification_meta_t *next;
1520
1521   if (n == NULL)
1522   {
1523     ERROR ("plugin_notification_meta_free: n == NULL!");
1524     return (-1);
1525   }
1526
1527   this = n;
1528   while (this != NULL)
1529   {
1530     next = this->next;
1531
1532     if (this->type == NM_TYPE_STRING)
1533     {
1534       free ((char *)this->nm_value.nm_string);
1535       this->nm_value.nm_string = NULL;
1536     }
1537     sfree (this);
1538
1539     this = next;
1540   }
1541
1542   return (0);
1543 } /* int plugin_notification_meta_free */
1544
1545 /* vim: set sw=8 ts=8 noet fdm=marker : */