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