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"
29 #include "utils_debug.h"
31 /*
32 * Private variables
33 */
34 static llist_t *list_init;
35 static llist_t *list_read;
36 static llist_t *list_write;
37 static llist_t *list_shutdown;
38 static llist_t *list_data_set;
40 static char *plugindir = NULL;
42 char hostname[DATA_MAX_NAME_LEN] = "localhost";
44 /*
45 * Static functions
46 */
47 static const char *plugin_get_dir (void)
48 {
49 if (plugindir == NULL)
50 return (PLUGINDIR);
51 else
52 return (plugindir);
53 }
55 static int register_callback (llist_t **list, const char *name, void *callback)
56 {
57 llentry_t *le;
59 if ((*list == NULL)
60 && ((*list = llist_create ()) == NULL))
61 return (-1);
63 le = llist_search (*list, name);
64 if (le == NULL)
65 {
66 le = llentry_create (name, callback);
67 if (le == NULL)
68 return (-1);
70 llist_append (*list, le);
71 }
72 else
73 {
74 le->value = callback;
75 }
77 return (0);
78 } /* int register_callback */
80 /*
81 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
82 * object, but it will bitch about a shared object not having a
83 * ``module_register'' symbol..
84 */
85 static int plugin_load_file (char *file)
86 {
87 lt_dlhandle dlh;
88 void (*reg_handle) (void);
90 DBG ("file = %s", file);
92 lt_dlinit ();
93 lt_dlerror (); /* clear errors */
95 if ((dlh = lt_dlopen (file)) == NULL)
96 {
97 const char *error = lt_dlerror ();
99 syslog (LOG_ERR, "lt_dlopen failed: %s", error);
100 DBG ("lt_dlopen failed: %s", error);
101 return (1);
102 }
104 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
105 {
106 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
107 file, lt_dlerror ());
108 lt_dlclose (dlh);
109 return (-1);
110 }
112 (*reg_handle) ();
114 return (0);
115 }
117 /*
118 * Public functions
119 */
120 void plugin_set_dir (const char *dir)
121 {
122 if (plugindir != NULL)
123 free (plugindir);
125 if (dir == NULL)
126 plugindir = NULL;
127 else if ((plugindir = strdup (dir)) == NULL)
128 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
129 }
131 #define BUFSIZE 512
132 int plugin_load (const char *type)
133 {
134 DIR *dh;
135 const char *dir;
136 char filename[BUFSIZE];
137 char typename[BUFSIZE];
138 int typename_len;
139 int ret;
140 struct stat statbuf;
141 struct dirent *de;
143 DBG ("type = %s", type);
145 dir = plugin_get_dir ();
146 ret = 1;
148 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
149 * type when matching the filename */
150 if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
151 {
152 syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
153 return (-1);
154 }
155 typename_len = strlen (typename);
157 if ((dh = opendir (dir)) == NULL)
158 {
159 syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
160 return (-1);
161 }
163 while ((de = readdir (dh)) != NULL)
164 {
165 if (strncasecmp (de->d_name, typename, typename_len))
166 continue;
168 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
169 {
170 syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
171 continue;
172 }
174 if (lstat (filename, &statbuf) == -1)
175 {
176 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
177 continue;
178 }
179 else if (!S_ISREG (statbuf.st_mode))
180 {
181 /* don't follow symlinks */
182 continue;
183 }
185 if (plugin_load_file (filename) == 0)
186 {
187 /* success */
188 ret = 0;
189 break;
190 }
191 }
193 closedir (dh);
195 return (ret);
196 }
198 /*
199 * The `register_*' functions follow
200 */
201 int plugin_register_config (const char *name,
202 int (*callback) (const char *key, const char *val),
203 const char **keys, int keys_num)
204 {
205 cf_register (name, callback, keys, keys_num);
206 return (0);
207 } /* int plugin_register_config */
209 int plugin_register_init (const char *name,
210 int (*callback) (void))
211 {
212 return (register_callback (&list_init, name, (void *) callback));
213 } /* plugin_register_init */
215 int plugin_register_read (const char *name,
216 int (*callback) (void))
217 {
218 return (register_callback (&list_read, name, (void *) callback));
219 } /* int plugin_register_read */
221 int plugin_register_write (const char *name,
222 int (*callback) (const data_set_t *ds, const value_list_t *vl))
223 {
224 return (register_callback (&list_write, name, (void *) callback));
225 } /* int plugin_register_write */
227 int plugin_register_shutdown (char *name,
228 int (*callback) (void))
229 {
230 return (register_callback (&list_shutdown, name, (void *) callback));
231 } /* int plugin_register_shutdown */
233 int plugin_register_data_set (const data_set_t *ds)
234 {
235 return (register_callback (&list_data_set, ds->type, (void *) ds));
236 } /* int plugin_register_data_set */
238 void plugin_init_all (void)
239 {
240 int (*callback) (void);
241 llentry_t *le;
243 gethostname (hostname, sizeof (hostname));
245 if (list_init == NULL)
246 return;
248 le = llist_head (list_init);
249 while (le != NULL)
250 {
251 callback = le->value;
252 (*callback) ();
254 le = le->next;
255 }
256 } /* void plugin_init_all */
258 void plugin_read_all (const int *loop)
259 {
260 int (*callback) (void);
261 llentry_t *le;
263 if (list_read == NULL)
264 return;
266 le = llist_head (list_read);
267 while ((*loop == 0) && (le != NULL))
268 {
269 callback = le->value;
270 (*callback) ();
272 le = le->next;
273 }
274 } /* void plugin_read_all */
276 void plugin_shutdown_all (void)
277 {
278 int (*callback) (void);
279 llentry_t *le;
281 if (list_shutdown == NULL)
282 return;
284 le = llist_head (list_shutdown);
285 while (le != NULL)
286 {
287 callback = le->value;
288 (*callback) ();
290 le = le->next;
291 }
292 } /* void plugin_shutdown_all */
294 int plugin_dispatch_values (const char *name, const value_list_t *vl)
295 {
296 int (*callback) (const data_set_t *, const value_list_t *);
297 data_set_t *ds;
298 llentry_t *le;
300 if (list_write == NULL)
301 return (-1);
303 le = llist_search (list_data_set, name);
304 if (le == NULL)
305 {
306 DBG ("No such dataset registered: %s", name);
307 return (-1);
308 }
310 ds = (data_set_t *) le->value;
312 DBG ("time = %u; host = %s; "
313 "plugin = %s; plugin_instance = %s; "
314 "type = %s; type_instance = %s;",
315 (unsigned int) vl->time, vl->host,
316 vl->plugin, vl->plugin_instance,
317 ds->type, vl->type_instance);
319 le = llist_head (list_write);
320 while (le != NULL)
321 {
322 callback = le->value;
323 (*callback) (ds, vl);
325 le = le->next;
326 }
328 return (0);
329 }
331 void plugin_complain (int level, complain_t *c, const char *format, ...)
332 {
333 char message[512];
334 va_list ap;
335 int step;
337 if (c->delay > 0)
338 {
339 c->delay--;
340 return;
341 }
343 step = atoi (COLLECTD_STEP);
344 assert (step > 0);
346 if (c->interval < step)
347 c->interval = step;
348 else
349 c->interval *= 2;
351 if (c->interval > 86400)
352 c->interval = 86400;
354 c->delay = c->interval / step;
356 va_start (ap, format);
357 vsnprintf (message, 512, format, ap);
358 message[511] = '\0';
359 va_end (ap);
361 syslog (level, message);
362 }
364 void plugin_relief (int level, complain_t *c, const char *format, ...)
365 {
366 char message[512];
367 va_list ap;
369 if (c->interval == 0)
370 return;
372 c->interval = 0;
374 va_start (ap, format);
375 vsnprintf (message, 512, format, ap);
376 message[511] = '\0';
377 va_end (ap);
379 syslog (level, message);
380 }