nut plugin: Reconnect to the server if the connection is lost.
[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         llentry_t *e;
441
442         e = llist_search (list_read, name);
443
444         if (e == NULL)
445                 return (-1);
446
447         llist_remove (list_read, e);
448         free (e->value);
449         llentry_destroy (e);
450
451         return (0);
452 }
453
454 int plugin_unregister_write (const char *name)
455 {
456         return (plugin_unregister (list_write, name));
457 }
458
459 int plugin_unregister_shutdown (const char *name)
460 {
461         return (plugin_unregister (list_shutdown, name));
462 }
463
464 int plugin_unregister_data_set (const char *name)
465 {
466         return (plugin_unregister (list_data_set, name));
467 }
468
469 int plugin_unregister_log (const char *name)
470 {
471         return (plugin_unregister (list_log, name));
472 }
473
474 void plugin_init_all (void)
475 {
476         int (*callback) (void);
477         llentry_t *le;
478         int status;
479
480         /* Start read-threads */
481         if (list_read != NULL)
482         {
483                 const char *rt;
484                 int num;
485                 rt = global_option_get ("ReadThreads");
486                 num = atoi (rt);
487                 start_threads ((num > 0) ? num : 5);
488         }
489
490         if (list_init == NULL)
491                 return;
492
493         le = llist_head (list_init);
494         while (le != NULL)
495         {
496                 callback = (int (*) (void)) le->value;
497                 status = (*callback) ();
498
499                 if (status != 0)
500                 {
501                         ERROR ("Initialization of plugin `%s' "
502                                         "failed with status %i. "
503                                         "Plugin will be unloaded. TODO!",
504                                         le->key, status);
505                         plugin_unregister_read (le->key);
506                 }
507
508                 le = le->next;
509         }
510 } /* void plugin_init_all */
511
512 void plugin_read_all (const int *loop)
513 {
514         llentry_t   *le;
515         read_func_t *rf;
516
517         if (list_read == NULL)
518                 return;
519
520         pthread_mutex_lock (&read_lock);
521
522         le = llist_head (list_read);
523         while (le != NULL)
524         {
525                 rf = (read_func_t *) le->value;
526
527                 if (rf->needs_read != DONE)
528                 {
529                         le = le->next;
530                         continue;
531                 }
532
533                 if (rf->wait_left > 0)
534                         rf->wait_left -= interval_g;
535
536                 if (rf->wait_left <= 0)
537                 {
538                         rf->needs_read = TODO;
539                 }
540
541                 le = le->next;
542         }
543
544         DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
545         pthread_cond_broadcast (&read_cond);
546         pthread_mutex_unlock (&read_lock);
547 } /* void plugin_read_all */
548
549 void plugin_shutdown_all (void)
550 {
551         int (*callback) (void);
552         llentry_t *le;
553
554         stop_threads ();
555
556         if (list_shutdown == NULL)
557                 return;
558
559         le = llist_head (list_shutdown);
560         while (le != NULL)
561         {
562                 callback = (int (*) (void)) le->value;
563                 (*callback) ();
564
565                 le = le->next;
566         }
567 } /* void plugin_shutdown_all */
568
569 int plugin_dispatch_values (const char *name, const value_list_t *vl)
570 {
571         int (*callback) (const data_set_t *, const value_list_t *);
572         data_set_t *ds;
573         llentry_t *le;
574
575         if (list_write == NULL)
576                 return (-1);
577
578         le = llist_search (list_data_set, name);
579         if (le == NULL)
580         {
581                 DEBUG ("No such dataset registered: %s", name);
582                 return (-1);
583         }
584
585         ds = (data_set_t *) le->value;
586
587         DEBUG ("plugin: plugin_dispatch_values: time = %u; host = %s; "
588                         "plugin = %s; plugin_instance = %s; type = %s; "
589                         "type_instance = %s;",
590                         (unsigned int) vl->time, vl->host,
591                         vl->plugin, vl->plugin_instance,
592                         ds->type, vl->type_instance);
593
594         le = llist_head (list_write);
595         while (le != NULL)
596         {
597                 callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
598                 (*callback) (ds, vl);
599
600                 le = le->next;
601         }
602
603         return (0);
604 }
605
606 void plugin_log (int level, const char *format, ...)
607 {
608         char msg[512];
609         va_list ap;
610
611         void (*callback) (int, const char *);
612         llentry_t *le;
613
614         if (list_log == NULL)
615                 return;
616
617 #if !COLLECT_DEBUG
618         if (level >= LOG_DEBUG)
619                 return;
620 #endif
621
622         va_start (ap, format);
623         vsnprintf (msg, 512, format, ap);
624         msg[511] = '\0';
625         va_end (ap);
626
627         le = llist_head (list_log);
628         while (le != NULL)
629         {
630                 callback = (void (*) (int, const char *)) le->value;
631                 (*callback) (level, msg);
632
633                 le = le->next;
634         }
635 } /* void plugin_log */
636
637 void plugin_complain (int level, complain_t *c, const char *format, ...)
638 {
639         char message[512];
640         va_list ap;
641
642         if (c->delay > 0)
643         {
644                 c->delay--;
645                 return;
646         }
647
648         if (c->interval < interval_g)
649                 c->interval = interval_g;
650         else
651                 c->interval *= 2;
652
653         if (c->interval > 86400)
654                 c->interval = 86400;
655
656         c->delay = c->interval / interval_g;
657
658         va_start (ap, format);
659         vsnprintf (message, 512, format, ap);
660         message[511] = '\0';
661         va_end (ap);
662
663         plugin_log (level, message);
664 }
665
666 void plugin_relief (int level, complain_t *c, const char *format, ...)
667 {
668         char message[512];
669         va_list ap;
670
671         if (c->interval == 0)
672                 return;
673
674         c->interval = 0;
675
676         va_start (ap, format);
677         vsnprintf (message, 512, format, ap);
678         message[511] = '\0';
679         va_end (ap);
680
681         plugin_log (level, message);
682 }
683
684 const data_set_t *plugin_get_ds (const char *name)
685 {
686         data_set_t *ds;
687         llentry_t *le;
688
689         le = llist_search (list_data_set, name);
690         if (le == NULL)
691         {
692                 DEBUG ("No such dataset registered: %s", name);
693                 return (NULL);
694         }
695
696         ds = (data_set_t *) le->value;
697
698         return (ds);
699 } /* data_set_t *plugin_get_ds */