8fdd7bba8fde7cf29f07d6b65d53064b753300e6
[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                         continue;
577                 }
578
579                 if (plugin_load_file (filename) == 0)
580                 {
581                         /* success */
582                         ret = 0;
583                         break;
584                 }
585                 else
586                 {
587                         fprintf (stderr, "Unable to load plugin %s.\n", type);
588                 }
589         }
590
591         closedir (dh);
592
593         if (filename[0] == '\0')
594                 fprintf (stderr, "Could not find plugin %s.\n", type);
595
596         return (ret);
597 }
598
599 /*
600  * The `register_*' functions follow
601  */
602 int plugin_register_config (const char *name,
603                 int (*callback) (const char *key, const char *val),
604                 const char **keys, int keys_num)
605 {
606         cf_register (name, callback, keys, keys_num);
607         return (0);
608 } /* int plugin_register_config */
609
610 int plugin_register_complex_config (const char *type,
611                 int (*callback) (oconfig_item_t *))
612 {
613         return (cf_register_complex (type, callback));
614 } /* int plugin_register_complex_config */
615
616 int plugin_register_init (const char *name,
617                 int (*callback) (void))
618 {
619         return (create_register_callback (&list_init, name, (void *) callback,
620                                 /* user_data = */ NULL));
621 } /* plugin_register_init */
622
623 static int plugin_compare_read_func (const void *arg0, const void *arg1)
624 {
625         const read_func_t *rf0;
626         const read_func_t *rf1;
627
628         rf0 = arg0;
629         rf1 = arg1;
630
631         if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
632                 return (-1);
633         else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
634                 return (1);
635         else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
636                 return (-1);
637         else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
638                 return (1);
639         else
640                 return (0);
641 } /* int plugin_compare_read_func */
642
643 int plugin_register_read (const char *name,
644                 int (*callback) (void))
645 {
646         read_func_t *rf;
647
648         if (read_heap == NULL)
649         {
650                 read_heap = c_heap_create (plugin_compare_read_func);
651                 if (read_heap == NULL)
652                 {
653                         ERROR ("plugin_register_complex_read: "
654                                         "c_heap_create failed.");
655                         return (-1);
656                 }
657         }
658
659         rf = (read_func_t *) malloc (sizeof (read_func_t));
660         if (rf == NULL)
661         {
662                 char errbuf[1024];
663                 ERROR ("plugin_register_read: malloc failed: %s",
664                                 sstrerror (errno, errbuf, sizeof (errbuf)));
665                 return (-1);
666         }
667
668         memset (rf, 0, sizeof (read_func_t));
669         rf->rf_callback = (void *) callback;
670         rf->rf_udata.data = NULL;
671         rf->rf_udata.free_func = NULL;
672         sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
673         rf->rf_type = RF_SIMPLE;
674         rf->rf_interval.tv_sec = 0;
675         rf->rf_interval.tv_nsec = 0;
676         rf->rf_effective_interval = rf->rf_interval;
677
678         return (c_heap_insert (read_heap, rf));
679 } /* int plugin_register_read */
680
681 int plugin_register_complex_read (const char *name,
682                 plugin_read_cb callback, user_data_t *user_data)
683 {
684         read_func_t *rf;
685
686         if (read_heap == NULL)
687         {
688                 read_heap = c_heap_create (plugin_compare_read_func);
689                 if (read_heap == NULL)
690                 {
691                         ERROR ("plugin_register_complex_read: "
692                                         "c_heap_create failed.");
693                         return (-1);
694                 }
695         }
696
697         rf = (read_func_t *) malloc (sizeof (read_func_t));
698         if (rf == NULL)
699         {
700                 ERROR ("plugin_register_complex_read: malloc failed.");
701                 return (-1);
702         }
703
704         memset (rf, 0, sizeof (read_func_t));
705         rf->rf_callback = (void *) callback;
706         sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
707         rf->rf_type = RF_COMPLEX;
708         rf->rf_interval.tv_sec = 0;
709         rf->rf_interval.tv_nsec = 0;
710         rf->rf_effective_interval = rf->rf_interval;
711
712         /* Set user data */
713         if (user_data == NULL)
714         {
715                 rf->rf_udata.data = NULL;
716                 rf->rf_udata.free_func = NULL;
717         }
718         else
719         {
720                 rf->rf_udata = *user_data;
721         }
722
723         return (c_heap_insert (read_heap, rf));
724 } /* int plugin_register_complex_read */
725
726 int plugin_register_write (const char *name,
727                 plugin_write_cb callback, user_data_t *ud)
728 {
729         return (create_register_callback (&list_write, name,
730                                 (void *) callback, ud));
731 } /* int plugin_register_write */
732
733 int plugin_register_flush (const char *name,
734                 plugin_flush_cb callback, user_data_t *ud)
735 {
736         return (create_register_callback (&list_flush, name,
737                                 (void *) callback, ud));
738 } /* int plugin_register_flush */
739
740 int plugin_register_shutdown (char *name,
741                 int (*callback) (void))
742 {
743         return (create_register_callback (&list_shutdown, name,
744                                 (void *) callback, /* user_data = */ NULL));
745 } /* int plugin_register_shutdown */
746
747 int plugin_register_data_set (const data_set_t *ds)
748 {
749         data_set_t *ds_copy;
750         int i;
751
752         if ((data_sets != NULL)
753                         && (c_avl_get (data_sets, ds->type, NULL) == 0))
754         {
755                 NOTICE ("Replacing DS `%s' with another version.", ds->type);
756                 plugin_unregister_data_set (ds->type);
757         }
758         else if (data_sets == NULL)
759         {
760                 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
761                 if (data_sets == NULL)
762                         return (-1);
763         }
764
765         ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
766         if (ds_copy == NULL)
767                 return (-1);
768         memcpy(ds_copy, ds, sizeof (data_set_t));
769
770         ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
771                         * ds->ds_num);
772         if (ds_copy->ds == NULL)
773         {
774                 free (ds_copy);
775                 return (-1);
776         }
777
778         for (i = 0; i < ds->ds_num; i++)
779                 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
780
781         return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
782 } /* int plugin_register_data_set */
783
784 int plugin_register_log (const char *name,
785                 plugin_log_cb callback, user_data_t *ud)
786 {
787         return (create_register_callback (&list_log, name,
788                                 (void *) callback, ud));
789 } /* int plugin_register_log */
790
791 int plugin_register_notification (const char *name,
792                 plugin_notification_cb callback, user_data_t *ud)
793 {
794         return (create_register_callback (&list_notification, name,
795                                 (void *) callback, ud));
796 } /* int plugin_register_log */
797
798 int plugin_unregister_config (const char *name)
799 {
800         cf_unregister (name);
801         return (0);
802 } /* int plugin_unregister_config */
803
804 int plugin_unregister_complex_config (const char *name)
805 {
806         cf_unregister_complex (name);
807         return (0);
808 } /* int plugin_unregister_complex_config */
809
810 int plugin_unregister_init (const char *name)
811 {
812         return (plugin_unregister (list_init, name));
813 }
814
815 int plugin_unregister_read (const char *name)
816 {
817         /* TODO: Implement removal of a specific key from the heap. */
818         assert (0);
819         return (-1);
820 }
821
822 int plugin_unregister_write (const char *name)
823 {
824         return (plugin_unregister (list_write, name));
825 }
826
827 int plugin_unregister_flush (const char *name)
828 {
829         return (plugin_unregister (list_flush, name));
830 }
831
832 int plugin_unregister_shutdown (const char *name)
833 {
834         return (plugin_unregister (list_shutdown, name));
835 }
836
837 int plugin_unregister_data_set (const char *name)
838 {
839         data_set_t *ds;
840
841         if (data_sets == NULL)
842                 return (-1);
843
844         if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
845                 return (-1);
846
847         sfree (ds->ds);
848         sfree (ds);
849
850         return (0);
851 } /* int plugin_unregister_data_set */
852
853 int plugin_unregister_log (const char *name)
854 {
855         return (plugin_unregister (list_log, name));
856 }
857
858 int plugin_unregister_notification (const char *name)
859 {
860         return (plugin_unregister (list_notification, name));
861 }
862
863 void plugin_init_all (void)
864 {
865         const char *chain_name;
866         llentry_t *le;
867         int status;
868
869         /* Init the value cache */
870         uc_init ();
871
872         chain_name = global_option_get ("PreCacheChain");
873         pre_cache_chain = fc_chain_get_by_name (chain_name);
874
875         chain_name = global_option_get ("PostCacheChain");
876         post_cache_chain = fc_chain_get_by_name (chain_name);
877
878
879         if ((list_init == NULL) && (read_heap == NULL))
880                 return;
881
882         /* Calling all init callbacks before checking if read callbacks
883          * are available allows the init callbacks to register the read
884          * callback. */
885         le = llist_head (list_init);
886         while (le != NULL)
887         {
888                 callback_func_t *cf;
889                 plugin_init_cb callback;
890
891                 cf = le->value;
892                 callback = cf->cf_callback;
893                 status = (*callback) ();
894
895                 if (status != 0)
896                 {
897                         ERROR ("Initialization of plugin `%s' "
898                                         "failed with status %i. "
899                                         "Plugin will be unloaded.",
900                                         le->key, status);
901                         /* Plugins that register read callbacks from the init
902                          * callback should take care of appropriate error
903                          * handling themselves. */
904                         /* FIXME: Unload _all_ functions */
905                         plugin_unregister_read (le->key);
906                 }
907
908                 le = le->next;
909         }
910
911         /* Start read-threads */
912         if (read_heap != NULL)
913         {
914                 const char *rt;
915                 int num;
916                 rt = global_option_get ("ReadThreads");
917                 num = atoi (rt);
918                 if (num != -1)
919                         start_read_threads ((num > 0) ? num : 5);
920         }
921 } /* void plugin_init_all */
922
923 /* TODO: Rename this function. */
924 void plugin_read_all (void)
925 {
926         uc_check_timeout ();
927
928         return;
929 } /* void plugin_read_all */
930
931 /* Read function called when the `-T' command line argument is given. */
932 int plugin_read_all_once (void)
933 {
934         int status;
935         int return_status = 0;
936
937         if (read_heap == NULL)
938         {
939                 NOTICE ("No read-functions are registered.");
940                 return (0);
941         }
942
943         while (42)
944         {
945                 read_func_t *rf;
946
947                 rf = c_head_get_root (read_heap);
948                 if (rf == NULL)
949                         break;
950
951                 if (rf->rf_type == RF_SIMPLE)
952                 {
953                         int (*callback) (void);
954
955                         callback = rf->rf_callback;
956                         status = (*callback) ();
957                 }
958                 else
959                 {
960                         plugin_read_cb callback;
961
962                         callback = rf->rf_callback;
963                         status = (*callback) (&rf->rf_udata);
964                 }
965
966                 if (status != 0)
967                 {
968                         NOTICE ("read-function of plugin `%s' failed.",
969                                         rf->rf_name);
970                         return_status = -1;
971                 }
972
973                 destroy_callback ((void *) rf);
974         }
975
976         return (return_status);
977 } /* int plugin_read_all_once */
978
979 int plugin_write (const char *plugin, /* {{{ */
980                 const data_set_t *ds, const value_list_t *vl)
981 {
982   llentry_t *le;
983   int status;
984
985   if (vl == NULL)
986     return (EINVAL);
987
988   if (list_write == NULL)
989     return (ENOENT);
990
991   if (ds == NULL)
992   {
993     ds = plugin_get_ds (vl->type);
994     if (ds == NULL)
995     {
996       ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
997       return (ENOENT);
998     }
999   }
1000
1001   if (plugin == NULL)
1002   {
1003     int success = 0;
1004     int failure = 0;
1005
1006     le = llist_head (list_write);
1007     while (le != NULL)
1008     {
1009       callback_func_t *cf = le->value;
1010       plugin_write_cb callback;
1011
1012       DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1013       callback = cf->cf_callback;
1014       status = (*callback) (ds, vl, &cf->cf_udata);
1015       if (status != 0)
1016         failure++;
1017       else
1018         success++;
1019
1020       le = le->next;
1021     }
1022
1023     if ((success == 0) && (failure != 0))
1024       status = -1;
1025     else
1026       status = 0;
1027   }
1028   else /* plugin != NULL */
1029   {
1030     callback_func_t *cf;
1031     plugin_write_cb callback;
1032
1033     le = llist_head (list_write);
1034     while (le != NULL)
1035     {
1036       if (strcasecmp (plugin, le->key) == 0)
1037         break;
1038
1039       le = le->next;
1040     }
1041
1042     if (le == NULL)
1043       return (ENOENT);
1044
1045     cf = le->value;
1046
1047     DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1048     callback = cf->cf_callback;
1049     status = (*callback) (ds, vl, &cf->cf_udata);
1050   }
1051
1052   return (status);
1053 } /* }}} int plugin_write */
1054
1055 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1056 {
1057   llentry_t *le;
1058
1059   if (list_flush == NULL)
1060     return (0);
1061
1062   le = llist_head (list_flush);
1063   while (le != NULL)
1064   {
1065     callback_func_t *cf;
1066     plugin_flush_cb callback;
1067
1068     if ((plugin != NULL)
1069         && (strcmp (plugin, le->key) != 0))
1070     {
1071       le = le->next;
1072       continue;
1073     }
1074
1075     cf = le->value;
1076     callback = cf->cf_callback;
1077
1078     (*callback) (timeout, identifier, &cf->cf_udata);
1079
1080     le = le->next;
1081   }
1082   return (0);
1083 } /* int plugin_flush */
1084
1085 void plugin_shutdown_all (void)
1086 {
1087         llentry_t *le;
1088
1089         stop_read_threads ();
1090
1091         destroy_all_callbacks (&list_init);
1092         destroy_read_heap ();
1093
1094         plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1095                         /* identifier = */ NULL);
1096
1097         le = NULL;
1098         if (list_shutdown != NULL)
1099                 le = llist_head (list_shutdown);
1100
1101         while (le != NULL)
1102         {
1103                 callback_func_t *cf;
1104                 plugin_shutdown_cb callback;
1105
1106                 cf = le->value;
1107                 callback = cf->cf_callback;
1108
1109                 /* Advance the pointer before calling the callback allows
1110                  * shutdown functions to unregister themselves. If done the
1111                  * other way around the memory `le' points to will be freed
1112                  * after callback returns. */
1113                 le = le->next;
1114
1115                 (*callback) ();
1116         }
1117
1118         destroy_all_callbacks (&list_write);
1119         destroy_all_callbacks (&list_flush);
1120         destroy_all_callbacks (&list_notification);
1121         destroy_all_callbacks (&list_shutdown);
1122         destroy_all_callbacks (&list_log);
1123 } /* void plugin_shutdown_all */
1124
1125 int plugin_dispatch_values (value_list_t *vl)
1126 {
1127         int status;
1128         static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1129
1130         value_t *saved_values;
1131         int      saved_values_len;
1132
1133         data_set_t *ds;
1134
1135         if ((vl == NULL) || (vl->type[0] == 0)
1136                         || (vl->values == NULL) || (vl->values_len < 1))
1137         {
1138                 ERROR ("plugin_dispatch_values: Invalid value list.");
1139                 return (-1);
1140         }
1141
1142         if (list_write == NULL)
1143                 c_complain_once (LOG_WARNING, &no_write_complaint,
1144                                 "plugin_dispatch_values: No write callback has been "
1145                                 "registered. Please load at least one output plugin, "
1146                                 "if you want the collected data to be stored.");
1147
1148         if (data_sets == NULL)
1149         {
1150                 ERROR ("plugin_dispatch_values: No data sets registered. "
1151                                 "Could the types database be read? Check "
1152                                 "your `TypesDB' setting!");
1153                 return (-1);
1154         }
1155
1156         if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1157         {
1158                 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1159                 return (-1);
1160         }
1161
1162         if (vl->time == 0)
1163                 vl->time = time (NULL);
1164
1165         DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1166                         "host = %s; "
1167                         "plugin = %s; plugin_instance = %s; "
1168                         "type = %s; type_instance = %s;",
1169                         (unsigned int) vl->time, vl->interval,
1170                         vl->host,
1171                         vl->plugin, vl->plugin_instance,
1172                         vl->type, vl->type_instance);
1173
1174 #if COLLECT_DEBUG
1175         assert (0 == strcmp (ds->type, vl->type));
1176 #else
1177         if (0 != strcmp (ds->type, vl->type))
1178                 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1179                                 ds->type, vl->type);
1180 #endif
1181
1182 #if COLLECT_DEBUG
1183         assert (ds->ds_num == vl->values_len);
1184 #else
1185         if (ds->ds_num != vl->values_len)
1186         {
1187                 ERROR ("plugin_dispatch_values: ds->type = %s: "
1188                                 "(ds->ds_num = %i) != "
1189                                 "(vl->values_len = %i)",
1190                                 ds->type, ds->ds_num, vl->values_len);
1191                 return (-1);
1192         }
1193 #endif
1194
1195         escape_slashes (vl->host, sizeof (vl->host));
1196         escape_slashes (vl->plugin, sizeof (vl->plugin));
1197         escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1198         escape_slashes (vl->type, sizeof (vl->type));
1199         escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1200
1201         /* Copy the values. This way, we can assure `targets' that they get
1202          * dynamically allocated values, which they can free and replace if
1203          * they like. */
1204         if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1205         {
1206                 saved_values     = vl->values;
1207                 saved_values_len = vl->values_len;
1208
1209                 vl->values = (value_t *) calloc (vl->values_len,
1210                                 sizeof (*vl->values));
1211                 if (vl->values == NULL)
1212                 {
1213                         ERROR ("plugin_dispatch_values: calloc failed.");
1214                         vl->values = saved_values;
1215                         return (-1);
1216                 }
1217                 memcpy (vl->values, saved_values,
1218                                 vl->values_len * sizeof (*vl->values));
1219         }
1220         else /* if ((pre == NULL) && (post == NULL)) */
1221         {
1222                 saved_values     = NULL;
1223                 saved_values_len = 0;
1224         }
1225
1226         if (pre_cache_chain != NULL)
1227         {
1228                 status = fc_process_chain (ds, vl, pre_cache_chain);
1229                 if (status < 0)
1230                 {
1231                         WARNING ("plugin_dispatch_values: Running the "
1232                                         "pre-cache chain failed with "
1233                                         "status %i (%#x).",
1234                                         status, status);
1235                 }
1236                 else if (status == FC_TARGET_STOP)
1237                 {
1238                         /* Restore the state of the value_list so that plugins
1239                          * don't get confused.. */
1240                         if (saved_values != NULL)
1241                         {
1242                                 free (vl->values);
1243                                 vl->values     = saved_values;
1244                                 vl->values_len = saved_values_len;
1245                         }
1246                         return (0);
1247                 }
1248         }
1249
1250         /* Update the value cache */
1251         uc_update (ds, vl);
1252
1253         if (post_cache_chain != NULL)
1254         {
1255                 status = fc_process_chain (ds, vl, post_cache_chain);
1256                 if (status < 0)
1257                 {
1258                         WARNING ("plugin_dispatch_values: Running the "
1259                                         "post-cache chain failed with "
1260                                         "status %i (%#x).",
1261                                         status, status);
1262                 }
1263         }
1264         else
1265                 fc_default_action (ds, vl);
1266
1267         /* Restore the state of the value_list so that plugins don't get
1268          * confused.. */
1269         if (saved_values != NULL)
1270         {
1271                 free (vl->values);
1272                 vl->values     = saved_values;
1273                 vl->values_len = saved_values_len;
1274         }
1275
1276         return (0);
1277 } /* int plugin_dispatch_values */
1278
1279 int plugin_dispatch_notification (const notification_t *notif)
1280 {
1281         llentry_t *le;
1282         /* Possible TODO: Add flap detection here */
1283
1284         DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1285                         "time = %u; host = %s;",
1286                         notif->severity, notif->message,
1287                         (unsigned int) notif->time, notif->host);
1288
1289         /* Nobody cares for notifications */
1290         if (list_notification == NULL)
1291                 return (-1);
1292
1293         le = llist_head (list_notification);
1294         while (le != NULL)
1295         {
1296                 callback_func_t *cf;
1297                 plugin_notification_cb callback;
1298                 int status;
1299
1300                 cf = le->value;
1301                 callback = cf->cf_callback;
1302                 status = (*callback) (notif, &cf->cf_udata);
1303                 if (status != 0)
1304                 {
1305                         WARNING ("plugin_dispatch_notification: Notification "
1306                                         "callback %s returned %i.",
1307                                         le->key, status);
1308                 }
1309
1310                 le = le->next;
1311         }
1312
1313         return (0);
1314 } /* int plugin_dispatch_notification */
1315
1316 void plugin_log (int level, const char *format, ...)
1317 {
1318         char msg[1024];
1319         va_list ap;
1320         llentry_t *le;
1321
1322         if (list_log == NULL)
1323                 return;
1324
1325 #if !COLLECT_DEBUG
1326         if (level >= LOG_DEBUG)
1327                 return;
1328 #endif
1329
1330         va_start (ap, format);
1331         vsnprintf (msg, sizeof (msg), format, ap);
1332         msg[sizeof (msg) - 1] = '\0';
1333         va_end (ap);
1334
1335         le = llist_head (list_log);
1336         while (le != NULL)
1337         {
1338                 callback_func_t *cf;
1339                 plugin_log_cb callback;
1340
1341                 cf = le->value;
1342                 callback = cf->cf_callback;
1343
1344                 (*callback) (level, msg, &cf->cf_udata);
1345
1346                 le = le->next;
1347         }
1348 } /* void plugin_log */
1349
1350 const data_set_t *plugin_get_ds (const char *name)
1351 {
1352         data_set_t *ds;
1353
1354         if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1355         {
1356                 DEBUG ("No such dataset registered: %s", name);
1357                 return (NULL);
1358         }
1359
1360         return (ds);
1361 } /* data_set_t *plugin_get_ds */
1362
1363 static int plugin_notification_meta_add (notification_t *n,
1364     const char *name,
1365     enum notification_meta_type_e type,
1366     const void *value)
1367 {
1368   notification_meta_t *meta;
1369   notification_meta_t *tail;
1370
1371   if ((n == NULL) || (name == NULL) || (value == NULL))
1372   {
1373     ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1374     return (-1);
1375   }
1376
1377   meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1378   if (meta == NULL)
1379   {
1380     ERROR ("plugin_notification_meta_add: malloc failed.");
1381     return (-1);
1382   }
1383   memset (meta, 0, sizeof (notification_meta_t));
1384
1385   sstrncpy (meta->name, name, sizeof (meta->name));
1386   meta->type = type;
1387
1388   switch (type)
1389   {
1390     case NM_TYPE_STRING:
1391     {
1392       meta->nm_value.nm_string = strdup ((const char *) value);
1393       if (meta->nm_value.nm_string == NULL)
1394       {
1395         ERROR ("plugin_notification_meta_add: strdup failed.");
1396         sfree (meta);
1397         return (-1);
1398       }
1399       break;
1400     }
1401     case NM_TYPE_SIGNED_INT:
1402     {
1403       meta->nm_value.nm_signed_int = *((int64_t *) value);
1404       break;
1405     }
1406     case NM_TYPE_UNSIGNED_INT:
1407     {
1408       meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1409       break;
1410     }
1411     case NM_TYPE_DOUBLE:
1412     {
1413       meta->nm_value.nm_double = *((double *) value);
1414       break;
1415     }
1416     case NM_TYPE_BOOLEAN:
1417     {
1418       meta->nm_value.nm_boolean = *((bool *) value);
1419       break;
1420     }
1421     default:
1422     {
1423       ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1424       sfree (meta);
1425       return (-1);
1426     }
1427   } /* switch (type) */
1428
1429   meta->next = NULL;
1430   tail = n->meta;
1431   while ((tail != NULL) && (tail->next != NULL))
1432     tail = tail->next;
1433
1434   if (tail == NULL)
1435     n->meta = meta;
1436   else
1437     tail->next = meta;
1438
1439   return (0);
1440 } /* int plugin_notification_meta_add */
1441
1442 int plugin_notification_meta_add_string (notification_t *n,
1443     const char *name,
1444     const char *value)
1445 {
1446   return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1447 }
1448
1449 int plugin_notification_meta_add_signed_int (notification_t *n,
1450     const char *name,
1451     int64_t value)
1452 {
1453   return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1454 }
1455
1456 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1457     const char *name,
1458     uint64_t value)
1459 {
1460   return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1461 }
1462
1463 int plugin_notification_meta_add_double (notification_t *n,
1464     const char *name,
1465     double value)
1466 {
1467   return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1468 }
1469
1470 int plugin_notification_meta_add_boolean (notification_t *n,
1471     const char *name,
1472     bool value)
1473 {
1474   return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1475 }
1476
1477 int plugin_notification_meta_copy (notification_t *dst,
1478     const notification_t *src)
1479 {
1480   notification_meta_t *meta;
1481
1482   assert (dst != NULL);
1483   assert (src != NULL);
1484   assert (dst != src);
1485   assert ((src->meta == NULL) || (src->meta != dst->meta));
1486
1487   for (meta = src->meta; meta != NULL; meta = meta->next)
1488   {
1489     if (meta->type == NM_TYPE_STRING)
1490       plugin_notification_meta_add_string (dst, meta->name,
1491           meta->nm_value.nm_string);
1492     else if (meta->type == NM_TYPE_SIGNED_INT)
1493       plugin_notification_meta_add_signed_int (dst, meta->name,
1494           meta->nm_value.nm_signed_int);
1495     else if (meta->type == NM_TYPE_UNSIGNED_INT)
1496       plugin_notification_meta_add_unsigned_int (dst, meta->name,
1497           meta->nm_value.nm_unsigned_int);
1498     else if (meta->type == NM_TYPE_DOUBLE)
1499       plugin_notification_meta_add_double (dst, meta->name,
1500           meta->nm_value.nm_double);
1501     else if (meta->type == NM_TYPE_BOOLEAN)
1502       plugin_notification_meta_add_boolean (dst, meta->name,
1503           meta->nm_value.nm_boolean);
1504   }
1505
1506   return (0);
1507 } /* int plugin_notification_meta_copy */
1508
1509 int plugin_notification_meta_free (notification_meta_t *n)
1510 {
1511   notification_meta_t *this;
1512   notification_meta_t *next;
1513
1514   if (n == NULL)
1515   {
1516     ERROR ("plugin_notification_meta_free: n == NULL!");
1517     return (-1);
1518   }
1519
1520   this = n;
1521   while (this != NULL)
1522   {
1523     next = this->next;
1524
1525     if (this->type == NM_TYPE_STRING)
1526     {
1527       free ((char *)this->nm_value.nm_string);
1528       this->nm_value.nm_string = NULL;
1529     }
1530     sfree (this);
1531
1532     this = next;
1533   }
1534
1535   return (0);
1536 } /* int plugin_notification_meta_free */
1537
1538 /* vim: set sw=8 ts=8 noet fdm=marker : */