collectd, plugin: Added support for "flush" callbacks.
[collectd.git] / src / plugin.c
1 /**
2  * collectd - src/plugin.c
3  * Copyright (C) 2005,2006  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  **/
21
22 #include "collectd.h"
23
24 #include <ltdl.h>
25
26 #if HAVE_PTHREAD_H
27 # include <pthread.h>
28 #endif
29
30 #include "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
33 #include "utils_avltree.h"
34 #include "utils_llist.h"
35 #include "utils_cache.h"
36 #include "utils_threshold.h"
37
38 /*
39  * Private structures
40  */
41 struct read_func_s
42 {
43         int wait_time;
44         int wait_left;
45         int (*callback) (void);
46         enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
47 };
48 typedef struct read_func_s read_func_t;
49
50 /*
51  * Private variables
52  */
53 static llist_t *list_init;
54 static llist_t *list_read;
55 static llist_t *list_write;
56 static llist_t *list_flush;
57 static llist_t *list_shutdown;
58 static llist_t *list_log;
59 static llist_t *list_notification;
60
61 static c_avl_tree_t *data_sets;
62
63 static char *plugindir = NULL;
64
65 static int             read_loop = 1;
66 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
67 static pthread_cond_t  read_cond = PTHREAD_COND_INITIALIZER;
68 static pthread_t      *read_threads = NULL;
69 static int             read_threads_num = 0;
70
71 /*
72  * Static functions
73  */
74 static const char *plugin_get_dir (void)
75 {
76         if (plugindir == NULL)
77                 return (PLUGINDIR);
78         else
79                 return (plugindir);
80 }
81
82 static int register_callback (llist_t **list, const char *name, void *callback)
83 {
84         llentry_t *le;
85         char *key;
86
87         if ((*list == NULL)
88                         && ((*list = llist_create ()) == NULL))
89                 return (-1);
90
91         le = llist_search (*list, name);
92         if (le == NULL)
93         {
94                 key = strdup (name);
95                 if (key == NULL)
96                         return (-1);
97
98                 le = llentry_create (key, callback);
99                 if (le == NULL)
100                 {
101                         free (key);
102                         return (-1);
103                 }
104
105                 llist_append (*list, le);
106         }
107         else
108         {
109                 le->value = callback;
110         }
111
112         return (0);
113 } /* int register_callback */
114
115 static int plugin_unregister (llist_t *list, const char *name)
116 {
117         llentry_t *e;
118
119         e = llist_search (list, name);
120
121         if (e == NULL)
122                 return (-1);
123
124         llist_remove (list, e);
125         free (e->key);
126         llentry_destroy (e);
127
128         return (0);
129 } /* int plugin_unregister */
130
131 /*
132  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
133  * object, but it will bitch about a shared object not having a
134  * ``module_register'' symbol..
135  */
136 static int plugin_load_file (char *file)
137 {
138         lt_dlhandle dlh;
139         void (*reg_handle) (void);
140
141         DEBUG ("file = %s", file);
142
143         lt_dlinit ();
144         lt_dlerror (); /* clear errors */
145
146         if ((dlh = lt_dlopen (file)) == NULL)
147         {
148                 const char *error = lt_dlerror ();
149
150                 ERROR ("lt_dlopen failed: %s", error);
151                 fprintf (stderr, "lt_dlopen failed: %s\n", error);
152                 return (1);
153         }
154
155         if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
156         {
157                 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
158                                 file, lt_dlerror ());
159                 lt_dlclose (dlh);
160                 return (-1);
161         }
162
163         (*reg_handle) ();
164
165         return (0);
166 }
167
168 static void *plugin_read_thread (void *args)
169 {
170         llentry_t   *le;
171         read_func_t *rf;
172         int          status;
173         int          done;
174
175         pthread_mutex_lock (&read_lock);
176
177         while (read_loop != 0)
178         {
179                 le = llist_head (list_read);
180                 done = 0;
181
182                 while ((read_loop != 0) && (le != NULL))
183                 {
184                         rf = (read_func_t *) le->value;
185
186                         if (rf->needs_read != TODO)
187                         {
188                                 le = le->next;
189                                 continue;
190                         }
191
192                         /* We will do this read function */
193                         rf->needs_read = ACTIVE;
194
195                         DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
196                                         (unsigned long int) pthread_self (), le->key);
197                         pthread_mutex_unlock (&read_lock);
198
199                         status = rf->callback ();
200                         done++;
201
202                         if (status != 0)
203                         {
204                                 if (rf->wait_time < interval_g)
205                                         rf->wait_time = interval_g;
206                                 rf->wait_left = rf->wait_time;
207                                 rf->wait_time = rf->wait_time * 2;
208                                 if (rf->wait_time > 86400)
209                                         rf->wait_time = 86400;
210
211                                 NOTICE ("read-function of plugin `%s' "
212                                                 "failed. Will suspend it for %i "
213                                                 "seconds.", le->key, rf->wait_left);
214                         }
215                         else
216                         {
217                                 rf->wait_left = 0;
218                                 rf->wait_time = interval_g;
219                         }
220
221                         pthread_mutex_lock (&read_lock);
222
223                         rf->needs_read = DONE;
224                         le = le->next;
225                 } /* while (le != NULL) */
226
227                 if ((read_loop != 0) && (done == 0))
228                 {
229                         DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
230                                         (unsigned long int) pthread_self ());
231                         pthread_cond_wait (&read_cond, &read_lock);
232                 }
233         } /* while (read_loop) */
234
235         pthread_mutex_unlock (&read_lock);
236
237         pthread_exit (NULL);
238 } /* void *plugin_read_thread */
239
240 static void start_threads (int num)
241 {
242         int i;
243
244         if (read_threads != NULL)
245                 return;
246
247         read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
248         if (read_threads == NULL)
249         {
250                 ERROR ("plugin: start_threads: calloc failed.");
251                 return;
252         }
253
254         read_threads_num = 0;
255         for (i = 0; i < num; i++)
256         {
257                 if (pthread_create (read_threads + read_threads_num, NULL,
258                                         plugin_read_thread, NULL) == 0)
259                 {
260                         read_threads_num++;
261                 }
262                 else
263                 {
264                         ERROR ("plugin: start_threads: pthread_create failed.");
265                         return;
266                 }
267         } /* for (i) */
268 } /* void start_threads */
269
270 static void stop_threads (void)
271 {
272         int i;
273
274         pthread_mutex_lock (&read_lock);
275         read_loop = 0;
276         DEBUG ("plugin: stop_threads: Signalling `read_cond'");
277         pthread_cond_broadcast (&read_cond);
278         pthread_mutex_unlock (&read_lock);
279
280         for (i = 0; i < read_threads_num; i++)
281         {
282                 if (pthread_join (read_threads[i], NULL) != 0)
283                 {
284                         ERROR ("plugin: stop_threads: pthread_join failed.");
285                 }
286                 read_threads[i] = (pthread_t) 0;
287         }
288         sfree (read_threads);
289         read_threads_num = 0;
290 } /* void stop_threads */
291
292 /*
293  * Public functions
294  */
295 void plugin_set_dir (const char *dir)
296 {
297         if (plugindir != NULL)
298                 free (plugindir);
299
300         if (dir == NULL)
301                 plugindir = NULL;
302         else if ((plugindir = strdup (dir)) == NULL)
303         {
304                 char errbuf[1024];
305                 ERROR ("strdup failed: %s",
306                                 sstrerror (errno, errbuf, sizeof (errbuf)));
307         }
308 }
309
310 #define BUFSIZE 512
311 int plugin_load (const char *type)
312 {
313         DIR  *dh;
314         const char *dir;
315         char  filename[BUFSIZE];
316         char  typename[BUFSIZE];
317         int   typename_len;
318         int   ret;
319         struct stat    statbuf;
320         struct dirent *de;
321
322         DEBUG ("type = %s", type);
323
324         dir = plugin_get_dir ();
325         ret = 1;
326
327         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
328          * type when matching the filename */
329         if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
330         {
331                 WARNING ("snprintf: truncated: `%s.so'", type);
332                 return (-1);
333         }
334         typename_len = strlen (typename);
335
336         if ((dh = opendir (dir)) == NULL)
337         {
338                 char errbuf[1024];
339                 ERROR ("opendir (%s): %s", dir,
340                                 sstrerror (errno, errbuf, sizeof (errbuf)));
341                 return (-1);
342         }
343
344         while ((de = readdir (dh)) != NULL)
345         {
346                 if (strncasecmp (de->d_name, typename, typename_len))
347                         continue;
348
349                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
350                 {
351                         WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
352                         continue;
353                 }
354
355                 if (lstat (filename, &statbuf) == -1)
356                 {
357                         char errbuf[1024];
358                         WARNING ("stat %s: %s", filename,
359                                         sstrerror (errno, errbuf, sizeof (errbuf)));
360                         continue;
361                 }
362                 else if (!S_ISREG (statbuf.st_mode))
363                 {
364                         /* don't follow symlinks */
365                         continue;
366                 }
367
368                 if (plugin_load_file (filename) == 0)
369                 {
370                         /* success */
371                         ret = 0;
372                         break;
373                 }
374                 else
375                 {
376                         fprintf (stderr, "Unable to load plugin %s.\n", type);
377                 }
378         }
379
380         closedir (dh);
381
382         return (ret);
383 }
384
385 /*
386  * The `register_*' functions follow
387  */
388 int plugin_register_config (const char *name,
389                 int (*callback) (const char *key, const char *val),
390                 const char **keys, int keys_num)
391 {
392         cf_register (name, callback, keys, keys_num);
393         return (0);
394 } /* int plugin_register_config */
395
396 int plugin_register_complex_config (const char *type,
397                 int (*callback) (oconfig_item_t *))
398 {
399         return (cf_register_complex (type, callback));
400 } /* int plugin_register_complex_config */
401
402 int plugin_register_init (const char *name,
403                 int (*callback) (void))
404 {
405         return (register_callback (&list_init, name, (void *) callback));
406 } /* plugin_register_init */
407
408 int plugin_register_read (const char *name,
409                 int (*callback) (void))
410 {
411         read_func_t *rf;
412
413         rf = (read_func_t *) malloc (sizeof (read_func_t));
414         if (rf == NULL)
415         {
416                 char errbuf[1024];
417                 ERROR ("plugin_register_read: malloc failed: %s",
418                                 sstrerror (errno, errbuf, sizeof (errbuf)));
419                 return (-1);
420         }
421
422         memset (rf, '\0', sizeof (read_func_t));
423         rf->wait_time = interval_g;
424         rf->wait_left = 0;
425         rf->callback = callback;
426         rf->needs_read = DONE;
427
428         return (register_callback (&list_read, name, (void *) rf));
429 } /* int plugin_register_read */
430
431 int plugin_register_write (const char *name,
432                 int (*callback) (const data_set_t *ds, const value_list_t *vl))
433 {
434         return (register_callback (&list_write, name, (void *) callback));
435 } /* int plugin_register_write */
436
437 int plugin_register_flush (const char *name, int (*callback) (const int))
438 {
439         return (register_callback (&list_flush, name, (void *) callback));
440 } /* int plugin_register_flush */
441
442 int plugin_register_shutdown (char *name,
443                 int (*callback) (void))
444 {
445         return (register_callback (&list_shutdown, name, (void *) callback));
446 } /* int plugin_register_shutdown */
447
448 int plugin_register_data_set (const data_set_t *ds)
449 {
450         data_set_t *ds_copy;
451         int i;
452
453         if ((data_sets != NULL)
454                         && (c_avl_get (data_sets, ds->type, NULL) == 0))
455         {
456                 NOTICE ("Replacing DS `%s' with another version.", ds->type);
457                 plugin_unregister_data_set (ds->type);
458         }
459         else if (data_sets == NULL)
460         {
461                 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
462                 if (data_sets == NULL)
463                         return (-1);
464         }
465
466         ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
467         if (ds_copy == NULL)
468                 return (-1);
469         memcpy(ds_copy, ds, sizeof (data_set_t));
470
471         ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
472                         * ds->ds_num);
473         if (ds_copy->ds == NULL)
474         {
475                 free (ds_copy);
476                 return (-1);
477         }
478
479         for (i = 0; i < ds->ds_num; i++)
480                 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
481
482         return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
483 } /* int plugin_register_data_set */
484
485 int plugin_register_log (char *name,
486                 void (*callback) (int priority, const char *msg))
487 {
488         return (register_callback (&list_log, name, (void *) callback));
489 } /* int plugin_register_log */
490
491 int plugin_register_notification (const char *name,
492                 int (*callback) (const notification_t *notif))
493 {
494         return (register_callback (&list_notification, name, (void *) callback));
495 } /* int plugin_register_log */
496
497 int plugin_unregister_config (const char *name)
498 {
499         cf_unregister (name);
500         return (0);
501 } /* int plugin_unregister_config */
502
503 int plugin_unregister_complex_config (const char *name)
504 {
505         cf_unregister_complex (name);
506         return (0);
507 } /* int plugin_unregister_complex_config */
508
509 int plugin_unregister_init (const char *name)
510 {
511         return (plugin_unregister (list_init, name));
512 }
513
514 int plugin_unregister_read (const char *name)
515 {
516         llentry_t *e;
517
518         e = llist_search (list_read, name);
519
520         if (e == NULL)
521                 return (-1);
522
523         llist_remove (list_read, e);
524         free (e->value);
525         free (e->key);
526         llentry_destroy (e);
527
528         return (0);
529 }
530
531 int plugin_unregister_write (const char *name)
532 {
533         return (plugin_unregister (list_write, name));
534 }
535
536 int plugin_unregister_flush (const char *name)
537 {
538         return (plugin_unregister (list_flush, name));
539 }
540
541 int plugin_unregister_shutdown (const char *name)
542 {
543         return (plugin_unregister (list_shutdown, name));
544 }
545
546 int plugin_unregister_data_set (const char *name)
547 {
548         data_set_t *ds;
549
550         if (data_sets == NULL)
551                 return (-1);
552
553         if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
554                 return (-1);
555
556         sfree (ds->ds);
557         sfree (ds);
558
559         return (0);
560 } /* int plugin_unregister_data_set */
561
562 int plugin_unregister_log (const char *name)
563 {
564         return (plugin_unregister (list_log, name));
565 }
566
567 int plugin_unregister_notification (const char *name)
568 {
569         return (plugin_unregister (list_notification, name));
570 }
571
572 void plugin_init_all (void)
573 {
574         int (*callback) (void);
575         llentry_t *le;
576         int status;
577
578         /* Start read-threads */
579         if (list_read != NULL)
580         {
581                 const char *rt;
582                 int num;
583                 rt = global_option_get ("ReadThreads");
584                 num = atoi (rt);
585                 start_threads ((num > 0) ? num : 5);
586         }
587
588         /* Init the value cache */
589         uc_init ();
590
591         if (list_init == NULL)
592                 return;
593
594         le = llist_head (list_init);
595         while (le != NULL)
596         {
597                 callback = (int (*) (void)) le->value;
598                 status = (*callback) ();
599
600                 if (status != 0)
601                 {
602                         ERROR ("Initialization of plugin `%s' "
603                                         "failed with status %i. "
604                                         "Plugin will be unloaded.",
605                                         le->key, status);
606                         /* FIXME: Unload _all_ functions */
607                         plugin_unregister_read (le->key);
608                 }
609
610                 le = le->next;
611         }
612 } /* void plugin_init_all */
613
614 void plugin_read_all (void)
615 {
616         llentry_t   *le;
617         read_func_t *rf;
618
619         uc_check_timeout ();
620
621         if (list_read == NULL)
622                 return;
623
624         pthread_mutex_lock (&read_lock);
625
626         le = llist_head (list_read);
627         while (le != NULL)
628         {
629                 rf = (read_func_t *) le->value;
630
631                 if (rf->needs_read != DONE)
632                 {
633                         le = le->next;
634                         continue;
635                 }
636
637                 if (rf->wait_left > 0)
638                         rf->wait_left -= interval_g;
639
640                 if (rf->wait_left <= 0)
641                 {
642                         rf->needs_read = TODO;
643                 }
644
645                 le = le->next;
646         }
647
648         DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
649         pthread_cond_broadcast (&read_cond);
650         pthread_mutex_unlock (&read_lock);
651 } /* void plugin_read_all */
652
653 void plugin_flush_all (int timeout)
654 {
655         int (*callback) (int);
656         llentry_t *le;
657
658         if (list_flush == NULL)
659                 return;
660
661         le = llist_head (list_flush);
662         while (le != NULL)
663         {
664                 callback = (int (*) (int)) le->value;
665                 le = le->next;
666
667                 (*callback) (timeout);
668         }
669 } /* void plugin_flush_all */
670
671 void plugin_shutdown_all (void)
672 {
673         int (*callback) (void);
674         llentry_t *le;
675
676         stop_threads ();
677
678         if (list_shutdown == NULL)
679                 return;
680
681         le = llist_head (list_shutdown);
682         while (le != NULL)
683         {
684                 callback = (int (*) (void)) le->value;
685
686                 /* Advance the pointer before calling the callback allows
687                  * shutdown functions to unregister themselves. If done the
688                  * other way around the memory `le' points to will be freed
689                  * after callback returns. */
690                 le = le->next;
691
692                 (*callback) ();
693         }
694 } /* void plugin_shutdown_all */
695
696 int plugin_dispatch_values (const char *name, value_list_t *vl)
697 {
698         int (*callback) (const data_set_t *, const value_list_t *);
699         data_set_t *ds;
700         llentry_t *le;
701
702         if ((list_write == NULL) || (data_sets == NULL))
703                 return (-1);
704
705         if (c_avl_get (data_sets, name, (void *) &ds) != 0)
706         {
707                 DEBUG ("No such dataset registered: %s", name);
708                 return (-1);
709         }
710
711         DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
712                         "host = %s; "
713                         "plugin = %s; plugin_instance = %s; "
714                         "type = %s; type_instance = %s;",
715                         (unsigned int) vl->time, vl->interval,
716                         vl->host,
717                         vl->plugin, vl->plugin_instance,
718                         ds->type, vl->type_instance);
719
720 #if COLLECT_DEBUG
721         assert (ds->ds_num == vl->values_len);
722 #else
723         if (ds->ds_num != vl->values_len)
724         {
725                 ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != "
726                                 "(vl->values_len = %i)",
727                                 ds->type, ds->ds_num, vl->values_len);
728                 return (-1);
729         }
730 #endif
731
732         escape_slashes (vl->host, sizeof (vl->host));
733         escape_slashes (vl->plugin, sizeof (vl->plugin));
734         escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
735         escape_slashes (vl->type_instance, sizeof (vl->type_instance));
736
737         /* Update the value cache */
738         uc_update (ds, vl);
739         ut_check_threshold (ds, vl);
740
741         le = llist_head (list_write);
742         while (le != NULL)
743         {
744                 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
745                 (*callback) (ds, vl);
746
747                 le = le->next;
748         }
749
750         return (0);
751 } /* int plugin_dispatch_values */
752
753 int plugin_dispatch_notification (const notification_t *notif)
754 {
755         int (*callback) (const notification_t *);
756         llentry_t *le;
757         /* Possible TODO: Add flap detection here */
758
759         DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
760                         "time = %u; host = %s;",
761                         notif->severity, notif->message,
762                         (unsigned int) notif->time, notif->host);
763
764         /* Nobody cares for notifications */
765         if (list_notification == NULL)
766                 return (-1);
767
768         le = llist_head (list_notification);
769         while (le != NULL)
770         {
771                 callback = (int (*) (const notification_t *)) le->value;
772                 (*callback) (notif);
773
774                 le = le->next;
775         }
776
777         return (0);
778 } /* int plugin_dispatch_notification */
779
780 void plugin_log (int level, const char *format, ...)
781 {
782         char msg[512];
783         va_list ap;
784
785         void (*callback) (int, const char *);
786         llentry_t *le;
787
788         if (list_log == NULL)
789                 return;
790
791 #if !COLLECT_DEBUG
792         if (level >= LOG_DEBUG)
793                 return;
794 #endif
795
796         va_start (ap, format);
797         vsnprintf (msg, 512, format, ap);
798         msg[511] = '\0';
799         va_end (ap);
800
801         le = llist_head (list_log);
802         while (le != NULL)
803         {
804                 callback = (void (*) (int, const char *)) le->value;
805                 (*callback) (level, msg);
806
807                 le = le->next;
808         }
809 } /* void plugin_log */
810
811 const data_set_t *plugin_get_ds (const char *name)
812 {
813         data_set_t *ds;
814
815         if (c_avl_get (data_sets, name, (void *) &ds) != 0)
816         {
817                 DEBUG ("No such dataset registered: %s", name);
818                 return (NULL);
819         }
820
821         return (ds);
822 } /* data_set_t *plugin_get_ds */