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