87fdcce69b88bb4fd123ae1ccbd86965f25fadbe
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 **/
22 #include "collectd.h"
24 #include <ltdl.h>
26 #include "plugin.h"
27 #include "configfile.h"
28 #include "utils_llist.h"
30 /*
31 * Private structures
32 */
33 struct read_func_s
34 {
35 int wait_time;
36 int wait_left;
37 int (*callback) (void);
38 };
39 typedef struct read_func_s read_func_t;
41 /*
42 * Private variables
43 */
44 static llist_t *list_init;
45 static llist_t *list_read;
46 static llist_t *list_write;
47 static llist_t *list_shutdown;
48 static llist_t *list_data_set;
49 static llist_t *list_log;
51 static char *plugindir = NULL;
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 }
64 static int register_callback (llist_t **list, const char *name, void *callback)
65 {
66 llentry_t *le;
68 if ((*list == NULL)
69 && ((*list = llist_create ()) == NULL))
70 return (-1);
72 le = llist_search (*list, name);
73 if (le == NULL)
74 {
75 le = llentry_create (name, callback);
76 if (le == NULL)
77 return (-1);
79 llist_append (*list, le);
80 }
81 else
82 {
83 le->value = callback;
84 }
86 return (0);
87 } /* int register_callback */
89 static int plugin_unregister (llist_t *list, const char *name)
90 {
91 llentry_t *e;
93 e = llist_search (list, name);
95 if (e == NULL)
96 return (-1);
98 llist_remove (list, e);
99 llentry_destroy (e);
101 return (0);
102 } /* int plugin_unregister */
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);
114 DEBUG ("file = %s", file);
116 lt_dlinit ();
117 lt_dlerror (); /* clear errors */
119 if ((dlh = lt_dlopen (file)) == NULL)
120 {
121 const char *error = lt_dlerror ();
123 ERROR ("lt_dlopen failed: %s", error);
124 DEBUG ("lt_dlopen failed: %s", error);
125 return (1);
126 }
128 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
129 {
130 WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
131 file, lt_dlerror ());
132 lt_dlclose (dlh);
133 return (-1);
134 }
136 (*reg_handle) ();
138 return (0);
139 }
141 /*
142 * Public functions
143 */
144 void plugin_set_dir (const char *dir)
145 {
146 if (plugindir != NULL)
147 free (plugindir);
149 if (dir == NULL)
150 plugindir = NULL;
151 else if ((plugindir = strdup (dir)) == NULL)
152 ERROR ("strdup failed: %s", strerror (errno));
153 }
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;
167 DEBUG ("type = %s", type);
169 dir = plugin_get_dir ();
170 ret = 1;
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 WARNING ("snprintf: truncated: `%s.so'", type);
177 return (-1);
178 }
179 typename_len = strlen (typename);
181 if ((dh = opendir (dir)) == NULL)
182 {
183 ERROR ("opendir (%s): %s", dir, strerror (errno));
184 return (-1);
185 }
187 while ((de = readdir (dh)) != NULL)
188 {
189 if (strncasecmp (de->d_name, typename, typename_len))
190 continue;
192 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
193 {
194 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
195 continue;
196 }
198 if (lstat (filename, &statbuf) == -1)
199 {
200 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 }
209 if (plugin_load_file (filename) == 0)
210 {
211 /* success */
212 ret = 0;
213 break;
214 }
215 }
217 closedir (dh);
219 return (ret);
220 }
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 */
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 */
239 int plugin_register_read (const char *name,
240 int (*callback) (void))
241 {
242 read_func_t *rf;
244 rf = (read_func_t *) malloc (sizeof (read_func_t));
245 if (rf == NULL)
246 {
247 ERROR ("plugin_register_read: malloc failed: %s",
248 strerror (errno));
249 return (-1);
250 }
252 memset (rf, '\0', sizeof (read_func_t));
253 rf->wait_time = atoi (COLLECTD_STEP);
254 rf->wait_left = 0;
255 rf->callback = callback;
257 return (register_callback (&list_read, name, (void *) rf));
258 } /* int plugin_register_read */
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 */
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 */
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 */
277 int plugin_register_log (char *name,
278 void (*callback) (int priority, const char *msg))
279 {
280 return (register_callback (&list_log, name, (void *) callback));
281 } /* int plugin_register_log */
283 int plugin_unregister_init (const char *name)
284 {
285 return (plugin_unregister (list_init, name));
286 }
288 int plugin_unregister_read (const char *name)
289 {
290 return (plugin_unregister (list_read, name));
291 llentry_t *e;
293 e = llist_search (list_read, name);
295 if (e == NULL)
296 return (-1);
298 llist_remove (list_read, e);
299 free (e->value);
300 llentry_destroy (e);
302 return (0);
303 }
305 int plugin_unregister_write (const char *name)
306 {
307 return (plugin_unregister (list_write, name));
308 }
310 int plugin_unregister_shutdown (const char *name)
311 {
312 return (plugin_unregister (list_shutdown, name));
313 }
315 int plugin_unregister_data_set (const char *name)
316 {
317 return (plugin_unregister (list_data_set, name));
318 }
320 int plugin_unregister_log (const char *name)
321 {
322 return (plugin_unregister (list_log, name));
323 }
325 void plugin_init_all (void)
326 {
327 int (*callback) (void);
328 llentry_t *le;
329 int status;
331 if (list_init == NULL)
332 return;
334 le = llist_head (list_init);
335 while (le != NULL)
336 {
337 callback = le->value;
338 status = (*callback) ();
340 if (status != 0)
341 {
342 ERROR ("Initialization of plugin `%s' "
343 "failed with status %i. "
344 "Plugin will be unloaded. TODO!",
345 le->key, status);
346 plugin_unregister_read (le->key);
347 }
349 le = le->next;
350 }
351 } /* void plugin_init_all */
353 void plugin_read_all (const int *loop)
354 {
355 llentry_t *le;
356 read_func_t *rf;
357 int status;
358 int step;
360 if (list_read == NULL)
361 return;
363 step = atoi (COLLECTD_STEP);
365 le = llist_head (list_read);
366 while ((*loop == 0) && (le != NULL))
367 {
368 rf = (read_func_t *) le->value;
370 if (rf->wait_left > 0)
371 rf->wait_left -= step;
372 if (rf->wait_left > 0)
373 {
374 le = le->next;
375 continue;
376 }
378 status = rf->callback ();
379 if (status != 0)
380 {
381 rf->wait_left = rf->wait_time;
382 rf->wait_time = rf->wait_time * 2;
383 if (rf->wait_time > 86400)
384 rf->wait_time = 86400;
386 NOTICE ("read-function of plugin `%s' "
387 "failed. Will syspend it for %i "
388 "seconds.", le->key, rf->wait_left);
389 }
390 else
391 {
392 rf->wait_left = 0;
393 rf->wait_time = step;
394 }
396 le = le->next;
397 } /* while ((*loop == 0) && (le != NULL)) */
398 } /* void plugin_read_all */
400 void plugin_shutdown_all (void)
401 {
402 int (*callback) (void);
403 llentry_t *le;
405 if (list_shutdown == NULL)
406 return;
408 le = llist_head (list_shutdown);
409 while (le != NULL)
410 {
411 callback = le->value;
412 (*callback) ();
414 le = le->next;
415 }
416 } /* void plugin_shutdown_all */
418 int plugin_dispatch_values (const char *name, const value_list_t *vl)
419 {
420 int (*callback) (const data_set_t *, const value_list_t *);
421 data_set_t *ds;
422 llentry_t *le;
424 if (list_write == NULL)
425 return (-1);
427 le = llist_search (list_data_set, name);
428 if (le == NULL)
429 {
430 DEBUG ("No such dataset registered: %s", name);
431 return (-1);
432 }
434 ds = (data_set_t *) le->value;
436 DEBUG ("time = %u; host = %s; "
437 "plugin = %s; plugin_instance = %s; "
438 "type = %s; type_instance = %s;",
439 (unsigned int) vl->time, vl->host,
440 vl->plugin, vl->plugin_instance,
441 ds->type, vl->type_instance);
443 le = llist_head (list_write);
444 while (le != NULL)
445 {
446 callback = le->value;
447 (*callback) (ds, vl);
449 le = le->next;
450 }
452 return (0);
453 }
455 void plugin_log (int level, const char *format, ...)
456 {
457 char msg[512];
458 va_list ap;
460 void (*callback) (int, const char *);
461 llentry_t *le;
463 #if !COLLECT_DEBUG
464 if (level >= LOG_DEBUG)
465 return;
466 #endif
468 va_start (ap, format);
469 vsnprintf (msg, 512, format, ap);
470 msg[511] = '\0';
471 va_end (ap);
473 le = llist_head (list_log);
474 while (le != NULL)
475 {
476 callback = le->value;
477 (*callback) (level, msg);
479 le = le->next;
480 }
481 } /* void plugin_log */
483 void plugin_complain (int level, complain_t *c, const char *format, ...)
484 {
485 char message[512];
486 va_list ap;
487 int step;
489 if (c->delay > 0)
490 {
491 c->delay--;
492 return;
493 }
495 step = atoi (COLLECTD_STEP);
496 assert (step > 0);
498 if (c->interval < step)
499 c->interval = step;
500 else
501 c->interval *= 2;
503 if (c->interval > 86400)
504 c->interval = 86400;
506 c->delay = c->interval / step;
508 va_start (ap, format);
509 vsnprintf (message, 512, format, ap);
510 message[511] = '\0';
511 va_end (ap);
513 plugin_log (level, message);
514 }
516 void plugin_relief (int level, complain_t *c, const char *format, ...)
517 {
518 char message[512];
519 va_list ap;
521 if (c->interval == 0)
522 return;
524 c->interval = 0;
526 va_start (ap, format);
527 vsnprintf (message, 512, format, ap);
528 message[511] = '\0';
529 va_end (ap);
531 plugin_log (level, message);
532 }