src/collectd.h: Define a useable `NAN', based on `configure's findings.
[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 #include "plugin.h"
27 #include "configfile.h"
28 #include "utils_llist.h"
29 #include "utils_debug.h"
30
31 /*
32  * Private structures
33  */
34 struct read_func_s
35 {
36         int wait_time;
37         int wait_left;
38         int (*callback) (void);
39 };
40 typedef struct read_func_s read_func_t;
41
42 /*
43  * Private variables
44  */
45 static llist_t *list_init;
46 static llist_t *list_read;
47 static llist_t *list_write;
48 static llist_t *list_shutdown;
49 static llist_t *list_data_set;
50
51 static char *plugindir = NULL;
52
53 /*
54  * Static functions
55  */
56 static const char *plugin_get_dir (void)
57 {
58         if (plugindir == NULL)
59                 return (PLUGINDIR);
60         else
61                 return (plugindir);
62 }
63
64 static int register_callback (llist_t **list, const char *name, void *callback)
65 {
66         llentry_t *le;
67
68         if ((*list == NULL)
69                         && ((*list = llist_create ()) == NULL))
70                 return (-1);
71
72         le = llist_search (*list, name);
73         if (le == NULL)
74         {
75                 le = llentry_create (name, callback);
76                 if (le == NULL)
77                         return (-1);
78
79                 llist_append (*list, le);
80         }
81         else
82         {
83                 le->value = callback;
84         }
85
86         return (0);
87 } /* int register_callback */
88
89 static int plugin_unregister (llist_t *list, const char *name)
90 {
91         llentry_t *e;
92
93         e = llist_search (list, name);
94
95         if (e == NULL)
96                 return (-1);
97
98         llist_remove (list, e);
99         llentry_destroy (e);
100
101         return (0);
102 } /* int plugin_unregister */
103
104 /*
105  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
106  * object, but it will bitch about a shared object not having a
107  * ``module_register'' symbol..
108  */
109 static int plugin_load_file (char *file)
110 {
111         lt_dlhandle dlh;
112         void (*reg_handle) (void);
113
114         DBG ("file = %s", file);
115
116         lt_dlinit ();
117         lt_dlerror (); /* clear errors */
118
119         if ((dlh = lt_dlopen (file)) == NULL)
120         {
121                 const char *error = lt_dlerror ();
122
123                 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
124                 DBG ("lt_dlopen failed: %s", error);
125                 return (1);
126         }
127
128         if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
129         {
130                 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
131                                 file, lt_dlerror ());
132                 lt_dlclose (dlh);
133                 return (-1);
134         }
135
136         (*reg_handle) ();
137
138         return (0);
139 }
140
141 /*
142  * Public functions
143  */
144 void plugin_set_dir (const char *dir)
145 {
146         if (plugindir != NULL)
147                 free (plugindir);
148
149         if (dir == NULL)
150                 plugindir = NULL;
151         else if ((plugindir = strdup (dir)) == NULL)
152                 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
153 }
154
155 #define BUFSIZE 512
156 int plugin_load (const char *type)
157 {
158         DIR  *dh;
159         const char *dir;
160         char  filename[BUFSIZE];
161         char  typename[BUFSIZE];
162         int   typename_len;
163         int   ret;
164         struct stat    statbuf;
165         struct dirent *de;
166
167         DBG ("type = %s", type);
168
169         dir = plugin_get_dir ();
170         ret = 1;
171
172         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
173          * type when matching the filename */
174         if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
175         {
176                 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
177                 return (-1);
178         }
179         typename_len = strlen (typename);
180
181         if ((dh = opendir (dir)) == NULL)
182         {
183                 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
184                 return (-1);
185         }
186
187         while ((de = readdir (dh)) != NULL)
188         {
189                 if (strncasecmp (de->d_name, typename, typename_len))
190                         continue;
191
192                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
193                 {
194                         syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
195                         continue;
196                 }
197
198                 if (lstat (filename, &statbuf) == -1)
199                 {
200                         syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
201                         continue;
202                 }
203                 else if (!S_ISREG (statbuf.st_mode))
204                 {
205                         /* don't follow symlinks */
206                         continue;
207                 }
208
209                 if (plugin_load_file (filename) == 0)
210                 {
211                         /* success */
212                         ret = 0;
213                         break;
214                 }
215         }
216
217         closedir (dh);
218
219         return (ret);
220 }
221
222 /*
223  * The `register_*' functions follow
224  */
225 int plugin_register_config (const char *name,
226                 int (*callback) (const char *key, const char *val),
227                 const char **keys, int keys_num)
228 {
229         cf_register (name, callback, keys, keys_num);
230         return (0);
231 } /* int plugin_register_config */
232
233 int plugin_register_init (const char *name,
234                 int (*callback) (void))
235 {
236         return (register_callback (&list_init, name, (void *) callback));
237 } /* plugin_register_init */
238
239 int plugin_register_read (const char *name,
240                 int (*callback) (void))
241 {
242         read_func_t *rf;
243
244         rf = (read_func_t *) malloc (sizeof (read_func_t));
245         if (rf == NULL)
246         {
247                 syslog (LOG_ERR, "plugin_register_read: malloc failed: %s",
248                                 strerror (errno));
249                 return (-1);
250         }
251
252         memset (rf, '\0', sizeof (read_func_t));
253         rf->wait_time = atoi (COLLECTD_STEP);
254         rf->wait_left = 0;
255         rf->callback = callback;
256
257         return (register_callback (&list_read, name, (void *) rf));
258 } /* int plugin_register_read */
259
260 int plugin_register_write (const char *name,
261                 int (*callback) (const data_set_t *ds, const value_list_t *vl))
262 {
263         return (register_callback (&list_write, name, (void *) callback));
264 } /* int plugin_register_write */
265
266 int plugin_register_shutdown (char *name,
267                 int (*callback) (void))
268 {
269         return (register_callback (&list_shutdown, name, (void *) callback));
270 } /* int plugin_register_shutdown */
271
272 int plugin_register_data_set (const data_set_t *ds)
273 {
274         return (register_callback (&list_data_set, ds->type, (void *) ds));
275 } /* int plugin_register_data_set */
276
277 int plugin_unregister_init (const char *name)
278 {
279         return (plugin_unregister (list_init, name));
280 }
281
282 int plugin_unregister_read (const char *name)
283 {
284         return (plugin_unregister (list_read, name));
285         llentry_t *e;
286
287         e = llist_search (list_read, name);
288
289         if (e == NULL)
290                 return (-1);
291
292         llist_remove (list_read, e);
293         free (e->value);
294         llentry_destroy (e);
295
296         return (0);
297 }
298
299 int plugin_unregister_write (const char *name)
300 {
301         return (plugin_unregister (list_write, name));
302 }
303
304 int plugin_unregister_shutdown (const char *name)
305 {
306         return (plugin_unregister (list_shutdown, name));
307 }
308
309 int plugin_unregister_data_set (const char *name)
310 {
311         return (plugin_unregister (list_data_set, name));
312 }
313
314 void plugin_init_all (void)
315 {
316         int (*callback) (void);
317         llentry_t *le;
318         int status;
319
320         if (list_init == NULL)
321                 return;
322
323         le = llist_head (list_init);
324         while (le != NULL)
325         {
326                 callback = le->value;
327                 status = (*callback) ();
328
329                 if (status != 0)
330                 {
331                         syslog (LOG_ERR, "Initialization of plugin `%s' "
332                                         "failed with status %i. "
333                                         "Plugin will be unloaded. TODO!",
334                                         le->key, status);
335                         plugin_unregister_read (le->key);
336                 }
337
338                 le = le->next;
339         }
340 } /* void plugin_init_all */
341
342 void plugin_read_all (const int *loop)
343 {
344         llentry_t   *le;
345         read_func_t *rf;
346         int          status;
347         int          step;
348
349         if (list_read == NULL)
350                 return;
351
352         step = atoi (COLLECTD_STEP);
353
354         le = llist_head (list_read);
355         while ((*loop == 0) && (le != NULL))
356         {
357                 rf = (read_func_t *) le->value;
358
359                 if (rf->wait_left > 0)
360                         rf->wait_left -= step;
361                 if (rf->wait_left > 0)
362                 {
363                         le = le->next;
364                         continue;
365                 }
366
367                 status = rf->callback ();
368                 if (status != 0)
369                 {
370                         rf->wait_left = rf->wait_time;
371                         rf->wait_time = rf->wait_time * 2;
372                         if (rf->wait_time > 86400)
373                                 rf->wait_time = 86400;
374
375                         syslog (LOG_NOTICE, "read-function of plugin `%s' "
376                                         "failed. Will syspend it for %i "
377                                         "seconds.", le->key, rf->wait_left);
378                 }
379                 else
380                 {
381                         rf->wait_left = 0;
382                         rf->wait_time = step;
383                 }
384
385                 le = le->next;
386         } /* while ((*loop == 0) && (le != NULL)) */
387 } /* void plugin_read_all */
388
389 void plugin_shutdown_all (void)
390 {
391         int (*callback) (void);
392         llentry_t *le;
393
394         if (list_shutdown == NULL)
395                 return;
396
397         le = llist_head (list_shutdown);
398         while (le != NULL)
399         {
400                 callback = le->value;
401                 (*callback) ();
402
403                 le = le->next;
404         }
405 } /* void plugin_shutdown_all */
406
407 int plugin_dispatch_values (const char *name, const value_list_t *vl)
408 {
409         int (*callback) (const data_set_t *, const value_list_t *);
410         data_set_t *ds;
411         llentry_t *le;
412
413         if (list_write == NULL)
414                 return (-1);
415
416         le = llist_search (list_data_set, name);
417         if (le == NULL)
418         {
419                 DBG ("No such dataset registered: %s", name);
420                 return (-1);
421         }
422
423         ds = (data_set_t *) le->value;
424
425         DBG ("time = %u; host = %s; "
426                         "plugin = %s; plugin_instance = %s; "
427                         "type = %s; type_instance = %s;",
428                         (unsigned int) vl->time, vl->host,
429                         vl->plugin, vl->plugin_instance,
430                         ds->type, vl->type_instance);
431
432         le = llist_head (list_write);
433         while (le != NULL)
434         {
435                 callback = le->value;
436                 (*callback) (ds, vl);
437
438                 le = le->next;
439         }
440
441         return (0);
442 }
443
444 void plugin_complain (int level, complain_t *c, const char *format, ...)
445 {
446         char message[512];
447         va_list ap;
448         int step;
449
450         if (c->delay > 0)
451         {
452                 c->delay--;
453                 return;
454         }
455
456         step = atoi (COLLECTD_STEP);
457         assert (step > 0);
458
459         if (c->interval < step)
460                 c->interval = step;
461         else
462                 c->interval *= 2;
463
464         if (c->interval > 86400)
465                 c->interval = 86400;
466
467         c->delay = c->interval / step;
468
469         va_start (ap, format);
470         vsnprintf (message, 512, format, ap);
471         message[511] = '\0';
472         va_end (ap);
473
474         syslog (level, message);
475 }
476
477 void plugin_relief (int level, complain_t *c, const char *format, ...)
478 {
479         char message[512];
480         va_list ap;
481
482         if (c->interval == 0)
483                 return;
484
485         c->interval = 0;
486
487         va_start (ap, format);
488         vsnprintf (message, 512, format, ap);
489         message[511] = '\0';
490         va_end (ap);
491
492         syslog (level, message);
493 }