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