cf4af74a34735904d88fbe5f17d68c366d48e129
[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, modreg_e load)
122 {
123         lt_dlhandle dlh;
124         void (*reg_handle) (modreg_e mr);
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 (*) (modreg_e)) 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) (load);
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                                 rf->wait_left = rf->wait_time;
190                                 rf->wait_time = rf->wait_time * 2;
191                                 if (rf->wait_time > 86400)
192                                         rf->wait_time = 86400;
193
194                                 NOTICE ("read-function of plugin `%s' "
195                                                 "failed. Will syspend it for %i "
196                                                 "seconds.", le->key, rf->wait_left);
197                         }
198                         else
199                         {
200                                 rf->wait_left = 0;
201                                 rf->wait_time = interval_g;
202                         }
203
204                         pthread_mutex_lock (&read_lock);
205
206                         rf->needs_read = DONE;
207                         le = le->next;
208                 } /* while (le != NULL) */
209
210                 if ((read_loop != 0) && (done == 0))
211                 {
212                         DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
213                                         (unsigned long int) pthread_self ());
214                         pthread_cond_wait (&read_cond, &read_lock);
215                 }
216         } /* while (read_loop) */
217
218         pthread_mutex_unlock (&read_lock);
219
220         pthread_exit (NULL);
221 } /* void *plugin_read_thread */
222
223 static void start_threads (int num)
224 {
225         int i;
226
227         if (read_threads != NULL)
228                 return;
229
230         read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
231         if (read_threads == NULL)
232         {
233                 ERROR ("plugin: start_threads: calloc failed.");
234                 return;
235         }
236
237         read_threads_num = 0;
238         for (i = 0; i < num; i++)
239         {
240                 if (pthread_create (read_threads + read_threads_num, NULL,
241                                         plugin_read_thread, NULL) == 0)
242                 {
243                         read_threads_num++;
244                 }
245                 else
246                 {
247                         ERROR ("plugin: start_threads: pthread_create failed.");
248                         return;
249                 }
250         } /* for (i) */
251 } /* void start_threads */
252
253 static void stop_threads (void)
254 {
255         int i;
256
257         pthread_mutex_lock (&read_lock);
258         read_loop = 0;
259         DEBUG ("plugin: stop_threads: Signalling `read_cond'");
260         pthread_cond_broadcast (&read_cond);
261         pthread_mutex_unlock (&read_lock);
262
263         for (i = 0; i < read_threads_num; i++)
264         {
265                 if (pthread_join (read_threads[i], NULL) != 0)
266                 {
267                         ERROR ("plugin: stop_threads: pthread_join failed.");
268                 }
269                 read_threads[i] = (pthread_t) 0;
270         }
271         sfree (read_threads);
272         read_threads_num = 0;
273 } /* void stop_threads */
274
275 /*
276  * Public functions
277  */
278 void plugin_set_dir (const char *dir)
279 {
280         if (plugindir != NULL)
281                 free (plugindir);
282
283         if (dir == NULL)
284                 plugindir = NULL;
285         else if ((plugindir = strdup (dir)) == NULL)
286         {
287                 char errbuf[1024];
288                 ERROR ("strdup failed: %s",
289                                 sstrerror (errno, errbuf, sizeof (errbuf)));
290         }
291 }
292
293 #define BUFSIZE 512
294 int plugin_load (const char *type, modreg_e mr)
295 {
296         DIR  *dh;
297         const char *dir;
298         char  filename[BUFSIZE];
299         char  typename[BUFSIZE];
300         int   typename_len;
301         int   ret;
302         struct stat    statbuf;
303         struct dirent *de;
304
305         DEBUG ("type = %s", type);
306
307         dir = plugin_get_dir ();
308         ret = 1;
309
310         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
311          * type when matching the filename */
312         if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
313         {
314                 WARNING ("snprintf: truncated: `%s.so'", type);
315                 return (-1);
316         }
317         typename_len = strlen (typename);
318
319         if ((dh = opendir (dir)) == NULL)
320         {
321                 char errbuf[1024];
322                 ERROR ("opendir (%s): %s", dir,
323                                 sstrerror (errno, errbuf, sizeof (errbuf)));
324                 return (-1);
325         }
326
327         while ((de = readdir (dh)) != NULL)
328         {
329                 if (strncasecmp (de->d_name, typename, typename_len))
330                         continue;
331
332                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
333                 {
334                         WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
335                         continue;
336                 }
337
338                 if (lstat (filename, &statbuf) == -1)
339                 {
340                         char errbuf[1024];
341                         WARNING ("stat %s: %s", filename,
342                                         sstrerror (errno, errbuf, sizeof (errbuf)));
343                         continue;
344                 }
345                 else if (!S_ISREG (statbuf.st_mode))
346                 {
347                         /* don't follow symlinks */
348                         continue;
349                 }
350
351                 if (plugin_load_file (filename, mr) == 0)
352                 {
353                         /* success */
354                         ret = 0;
355                         break;
356                 }
357         }
358
359         closedir (dh);
360
361         return (ret);
362 }
363
364 /*
365  * The `register_*' functions follow
366  */
367 int plugin_register_config (const char *name,
368                 int (*callback) (const char *key, const char *val),
369                 const char **keys, int keys_num)
370 {
371         cf_register (name, callback, keys, keys_num);
372         return (0);
373 } /* int plugin_register_config */
374
375 int plugin_register_init (const char *name,
376                 int (*callback) (void))
377 {
378         return (register_callback (&list_init, name, (void *) callback));
379 } /* plugin_register_init */
380
381 int plugin_register_read (const char *name,
382                 int (*callback) (void))
383 {
384         read_func_t *rf;
385
386         rf = (read_func_t *) malloc (sizeof (read_func_t));
387         if (rf == NULL)
388         {
389                 char errbuf[1024];
390                 ERROR ("plugin_register_read: malloc failed: %s",
391                                 sstrerror (errno, errbuf, sizeof (errbuf)));
392                 return (-1);
393         }
394
395         memset (rf, '\0', sizeof (read_func_t));
396         rf->wait_time = interval_g;
397         rf->wait_left = 0;
398         rf->callback = callback;
399         rf->needs_read = DONE;
400
401         return (register_callback (&list_read, name, (void *) rf));
402 } /* int plugin_register_read */
403
404 int plugin_register_write (const char *name,
405                 int (*callback) (const data_set_t *ds, const value_list_t *vl))
406 {
407         return (register_callback (&list_write, name, (void *) callback));
408 } /* int plugin_register_write */
409
410 int plugin_register_shutdown (char *name,
411                 int (*callback) (void))
412 {
413         return (register_callback (&list_shutdown, name, (void *) callback));
414 } /* int plugin_register_shutdown */
415
416 int plugin_register_data_set (const data_set_t *ds)
417 {
418         return (register_callback (&list_data_set, ds->type, (void *) ds));
419 } /* int plugin_register_data_set */
420
421 int plugin_register_log (char *name,
422                 void (*callback) (int priority, const char *msg))
423 {
424         return (register_callback (&list_log, name, (void *) callback));
425 } /* int plugin_register_log */
426
427 int plugin_unregister_config (const char *name)
428 {
429         cf_unregister (name);
430         return (0);
431 } /* int plugin_unregister_config */
432
433 int plugin_unregister_init (const char *name)
434 {
435         return (plugin_unregister (list_init, name));
436 }
437
438 int plugin_unregister_read (const char *name)
439 {
440         return (plugin_unregister (list_read, name));
441         llentry_t *e;
442
443         e = llist_search (list_read, name);
444
445         if (e == NULL)
446                 return (-1);
447
448         llist_remove (list_read, e);
449         free (e->value);
450         llentry_destroy (e);
451
452         return (0);
453 }
454
455 int plugin_unregister_write (const char *name)
456 {
457         return (plugin_unregister (list_write, name));
458 }
459
460 int plugin_unregister_shutdown (const char *name)
461 {
462         return (plugin_unregister (list_shutdown, name));
463 }
464
465 int plugin_unregister_data_set (const char *name)
466 {
467         return (plugin_unregister (list_data_set, name));
468 }
469
470 int plugin_unregister_log (const char *name)
471 {
472         return (plugin_unregister (list_log, name));
473 }
474
475 void plugin_init_all (void)
476 {
477         int (*callback) (void);
478         llentry_t *le;
479         int status;
480
481         /* Start read-threads */
482         if (list_read != NULL)
483         {
484                 const char *rt;
485                 int num;
486                 rt = global_option_get ("ReadThreads");
487                 num = atoi (rt);
488                 start_threads ((num > 0) ? num : 5);
489         }
490
491         if (list_init == NULL)
492                 return;
493
494         le = llist_head (list_init);
495         while (le != NULL)
496         {
497                 callback = le->value;
498                 status = (*callback) ();
499
500                 if (status != 0)
501                 {
502                         ERROR ("Initialization of plugin `%s' "
503                                         "failed with status %i. "
504                                         "Plugin will be unloaded. TODO!",
505                                         le->key, status);
506                         plugin_unregister_read (le->key);
507                 }
508
509                 le = le->next;
510         }
511 } /* void plugin_init_all */
512
513 void plugin_read_all (const int *loop)
514 {
515         llentry_t   *le;
516         read_func_t *rf;
517
518         if (list_read == NULL)
519                 return;
520
521         pthread_mutex_lock (&read_lock);
522
523         le = llist_head (list_read);
524         while (le != NULL)
525         {
526                 rf = (read_func_t *) le->value;
527
528                 if (rf->needs_read != DONE)
529                 {
530                         le = le->next;
531                         continue;
532                 }
533
534                 if (rf->wait_left > 0)
535                         rf->wait_left -= interval_g;
536
537                 if (rf->wait_left <= 0)
538                 {
539                         rf->needs_read = TODO;
540                 }
541
542                 le = le->next;
543         }
544
545         DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
546         pthread_cond_broadcast (&read_cond);
547         pthread_mutex_unlock (&read_lock);
548 } /* void plugin_read_all */
549
550 void plugin_shutdown_all (void)
551 {
552         int (*callback) (void);
553         llentry_t *le;
554
555         stop_threads ();
556
557         if (list_shutdown == NULL)
558                 return;
559
560         le = llist_head (list_shutdown);
561         while (le != NULL)
562         {
563                 callback = le->value;
564                 (*callback) ();
565
566                 le = le->next;
567         }
568 } /* void plugin_shutdown_all */
569
570 int plugin_dispatch_values (const char *name, const value_list_t *vl)
571 {
572         int (*callback) (const data_set_t *, const value_list_t *);
573         data_set_t *ds;
574         llentry_t *le;
575
576         if (list_write == NULL)
577                 return (-1);
578
579         le = llist_search (list_data_set, name);
580         if (le == NULL)
581         {
582                 DEBUG ("No such dataset registered: %s", name);
583                 return (-1);
584         }
585
586         ds = (data_set_t *) le->value;
587
588         DEBUG ("plugin: plugin_dispatch_values: time = %u; host = %s; "
589                         "plugin = %s; plugin_instance = %s; type = %s; "
590                         "type_instance = %s;",
591                         (unsigned int) vl->time, vl->host,
592                         vl->plugin, vl->plugin_instance,
593                         ds->type, vl->type_instance);
594
595         le = llist_head (list_write);
596         while (le != NULL)
597         {
598                 callback = le->value;
599                 (*callback) (ds, vl);
600
601                 le = le->next;
602         }
603
604         return (0);
605 }
606
607 void plugin_log (int level, const char *format, ...)
608 {
609         char msg[512];
610         va_list ap;
611
612         void (*callback) (int, const char *);
613         llentry_t *le;
614
615         if (list_log == NULL)
616                 return;
617
618 #if !COLLECT_DEBUG
619         if (level >= LOG_DEBUG)
620                 return;
621 #endif
622
623         va_start (ap, format);
624         vsnprintf (msg, 512, format, ap);
625         msg[511] = '\0';
626         va_end (ap);
627
628         le = llist_head (list_log);
629         while (le != NULL)
630         {
631                 callback = le->value;
632                 (*callback) (level, msg);
633
634                 le = le->next;
635         }
636 } /* void plugin_log */
637
638 void plugin_complain (int level, complain_t *c, const char *format, ...)
639 {
640         char message[512];
641         va_list ap;
642
643         if (c->delay > 0)
644         {
645                 c->delay--;
646                 return;
647         }
648
649         if (c->interval < interval_g)
650                 c->interval = interval_g;
651         else
652                 c->interval *= 2;
653
654         if (c->interval > 86400)
655                 c->interval = 86400;
656
657         c->delay = c->interval / interval_g;
658
659         va_start (ap, format);
660         vsnprintf (message, 512, format, ap);
661         message[511] = '\0';
662         va_end (ap);
663
664         plugin_log (level, message);
665 }
666
667 void plugin_relief (int level, complain_t *c, const char *format, ...)
668 {
669         char message[512];
670         va_list ap;
671
672         if (c->interval == 0)
673                 return;
674
675         c->interval = 0;
676
677         va_start (ap, format);
678         vsnprintf (message, 512, format, ap);
679         message[511] = '\0';
680         va_end (ap);
681
682         plugin_log (level, message);
683 }
684
685 const data_set_t *plugin_get_ds (const char *name)
686 {
687         data_set_t *ds;
688         llentry_t *le;
689
690         le = llist_search (list_data_set, name);
691         if (le == NULL)
692         {
693                 DEBUG ("No such dataset registered: %s", name);
694                 return (NULL);
695         }
696
697         ds = (data_set_t *) le->value;
698
699         return (ds);
700 } /* data_set_t *plugin_get_ds */