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; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
25 #include <ltdl.h>
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_llist.h"
30 #include "utils_debug.h"
32 /*
33 * Private variables
34 */
35 static llist_t *list_init;
36 static llist_t *list_read;
37 static llist_t *list_write;
38 static llist_t *list_shutdown;
39 static llist_t *list_data_set;
41 static char *plugindir = NULL;
43 /*
44 * Static functions
45 */
46 static const char *plugin_get_dir (void)
47 {
48 if (plugindir == NULL)
49 return (PLUGINDIR);
50 else
51 return (plugindir);
52 }
54 static int register_callback (llist_t **list, const char *name, void *callback)
55 {
56 llentry_t *le;
58 if ((*list == NULL)
59 && ((*list = llist_create ()) == NULL))
60 return (-1);
62 le = llist_search (*list, name);
63 if (le == NULL)
64 {
65 le = llentry_create (name, callback);
66 if (le == NULL)
67 return (-1);
69 llist_append (*list, le);
70 }
71 else
72 {
73 le->value = callback;
74 }
76 return (0);
77 } /* int register_callback */
79 /*
80 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
81 * object, but it will bitch about a shared object not having a
82 * ``module_register'' symbol..
83 */
84 static int plugin_load_file (char *file)
85 {
86 lt_dlhandle dlh;
87 void (*reg_handle) (void);
89 DBG ("file = %s", file);
91 lt_dlinit ();
92 lt_dlerror (); /* clear errors */
94 if ((dlh = lt_dlopen (file)) == NULL)
95 {
96 const char *error = lt_dlerror ();
98 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
99 DBG ("lt_dlopen failed: %s", error);
100 return (1);
101 }
103 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
104 {
105 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
106 file, lt_dlerror ());
107 lt_dlclose (dlh);
108 return (-1);
109 }
111 (*reg_handle) ();
113 return (0);
114 }
116 /*
117 * Public functions
118 */
119 void plugin_set_dir (const char *dir)
120 {
121 if (plugindir != NULL)
122 free (plugindir);
124 if (dir == NULL)
125 plugindir = NULL;
126 else if ((plugindir = strdup (dir)) == NULL)
127 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
128 }
130 #define BUFSIZE 512
131 int plugin_load (const char *type)
132 {
133 DIR *dh;
134 const char *dir;
135 char filename[BUFSIZE];
136 char typename[BUFSIZE];
137 int typename_len;
138 int ret;
139 struct stat statbuf;
140 struct dirent *de;
142 DBG ("type = %s", type);
144 dir = plugin_get_dir ();
145 ret = 1;
147 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
148 * type when matching the filename */
149 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
150 {
151 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
152 return (-1);
153 }
154 typename_len = strlen (typename);
156 if ((dh = opendir (dir)) == NULL)
157 {
158 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
159 return (-1);
160 }
162 while ((de = readdir (dh)) != NULL)
163 {
164 if (strncasecmp (de->d_name, typename, typename_len))
165 continue;
167 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
168 {
169 syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
170 continue;
171 }
173 if (lstat (filename, &statbuf) == -1)
174 {
175 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
176 continue;
177 }
178 else if (!S_ISREG (statbuf.st_mode))
179 {
180 /* don't follow symlinks */
181 continue;
182 }
184 if (plugin_load_file (filename) == 0)
185 {
186 /* success */
187 ret = 0;
188 break;
189 }
190 }
192 closedir (dh);
194 return (ret);
195 }
197 /*
198 * The `register_*' functions follow
199 */
200 int plugin_register_config (const char *name,
201 int (*callback) (const char *key, const char *val),
202 const char **keys, int keys_num)
203 {
204 cf_register (name, callback, keys, keys_num);
205 return (0);
206 } /* int plugin_register_config */
208 int plugin_register_init (const char *name,
209 int (*callback) (void))
210 {
211 return (register_callback (&list_init, name, (void *) callback));
212 } /* plugin_register_init */
214 int plugin_register_read (const char *name,
215 int (*callback) (void))
216 {
217 return (register_callback (&list_read, name, (void *) callback));
218 } /* int plugin_register_read */
220 int plugin_register_write (const char *name,
221 int (*callback) (const data_set_t *ds, const value_list_t *vl))
222 {
223 return (register_callback (&list_write, name, (void *) callback));
224 } /* int plugin_register_write */
226 int plugin_register_shutdown (char *name,
227 int (*callback) (void))
228 {
229 return (register_callback (&list_shutdown, name, (void *) callback));
230 } /* int plugin_register_shutdown */
232 int plugin_register_data_set (const data_set_t *ds)
233 {
234 return (register_callback (&list_data_set, ds->type, (void *) ds));
235 } /* int plugin_register_data_set */
237 void plugin_init_all (void)
238 {
239 int (*callback) (void);
240 llentry_t *le;
242 if (list_init == NULL)
243 return;
245 le = llist_head (list_init);
246 while (le != NULL)
247 {
248 callback = le->value;
249 (*callback) ();
251 le = le->next;
252 }
253 } /* void plugin_init_all */
255 void plugin_read_all (const int *loop)
256 {
257 int (*callback) (void);
258 llentry_t *le;
260 if (list_read == NULL)
261 return;
263 le = llist_head (list_read);
264 while ((*loop == 0) && (le != NULL))
265 {
266 callback = le->value;
267 (*callback) ();
269 le = le->next;
270 }
271 } /* void plugin_read_all */
273 void plugin_shutdown_all (void)
274 {
275 int (*callback) (void);
276 llentry_t *le;
278 if (list_shutdown == NULL)
279 return;
281 le = llist_head (list_shutdown);
282 while (le != NULL)
283 {
284 callback = le->value;
285 (*callback) ();
287 le = le->next;
288 }
289 } /* void plugin_shutdown_all */
291 int plugin_dispatch_values (const char *name, const value_list_t *vl)
292 {
293 int (*callback) (const data_set_t *, const value_list_t *);
294 data_set_t *ds;
295 llentry_t *le;
297 if (list_write == NULL)
298 return (-1);
300 le = llist_search (list_data_set, name);
301 if (le == NULL)
302 return (-1);
304 ds = (data_set_t *) le->value;
306 le = llist_head (list_write);
307 while (le != NULL)
308 {
309 callback = le->value;
310 (*callback) (ds, vl);
312 le = le->next;
313 }
315 return (0);
316 }
318 void plugin_complain (int level, complain_t *c, const char *format, ...)
319 {
320 char message[512];
321 va_list ap;
322 int step;
324 if (c->delay > 0)
325 {
326 c->delay--;
327 return;
328 }
330 step = atoi (COLLECTD_STEP);
331 assert (step > 0);
333 if (c->interval < step)
334 c->interval = step;
335 else
336 c->interval *= 2;
338 if (c->interval > 86400)
339 c->interval = 86400;
341 c->delay = c->interval / step;
343 va_start (ap, format);
344 vsnprintf (message, 512, format, ap);
345 message[511] = '\0';
346 va_end (ap);
348 syslog (level, message);
349 }
351 void plugin_relief (int level, complain_t *c, const char *format, ...)
352 {
353 char message[512];
354 va_list ap;
356 if (c->interval == 0)
357 return;
359 c->interval = 0;
361 va_start (ap, format);
362 vsnprintf (message, 512, format, ap);
363 message[511] = '\0';
364 va_end (ap);
366 syslog (level, message);
367 }