2 * collectd - src/plugin.c
3 * Copyright (C) 2005 Florian octo Forster
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
28 #include "multicast.h"
35 void (*write) (char *host, char *inst, char *val);
39 static plugin_t *first_plugin = NULL;
42 extern int operating_mode;
46 * Returns the number of plugins registered
48 int plugin_count (void)
53 for (i = 0, p = first_plugin; p != NULL; p = p->next)
60 * Returns the plugins with the type `type' or NULL if it's not found.
62 plugin_t *plugin_search (char *type)
69 for (ret = first_plugin; ret != NULL; ret = ret->next)
70 if (strcmp (ret->type, type) == 0)
77 * (Try to) load the shared object `name'. Won't complain if it isn't a shared
78 * object, but it will bitch about a shared object not having a
79 * ``module_register'' symbol..
81 void plugin_load (char *name)
84 void (*reg_handle) (void);
87 lt_dlerror (); /* clear errors */
89 if ((dlh = lt_dlopen (name)) == NULL)
92 if ((reg_handle = lt_dlsym (dlh, "module_register")) == NULL)
94 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
104 * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
107 int plugin_load_all (char *dir)
111 char filename[BUFSIZE];
117 if ((dh = opendir (dir)) == NULL)
119 fprintf (stderr, "Error: Cannot read plugin directory `%s'\n", dir);
123 while ((de = readdir (dh)) != NULL)
125 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
128 if (lstat (filename, &statbuf) == -1)
130 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
133 else if (!S_ISREG (statbuf.st_mode))
138 plugin_load (filename);
143 return (plugin_count ());
148 * Call `init' on all plugins (if given)
150 void plugin_init_all (void)
154 for (p = first_plugin; p != NULL; p = p->next)
160 * Call `read' on all plugins (if given)
162 void plugin_read_all (void)
166 for (p = first_plugin; p != NULL; p = p->next)
172 * Add plugin to the linked list of registered plugins.
174 void plugin_register (char *type,
177 void (*write) (char *, char *, char *))
181 if (plugin_search (type) != NULL)
184 if ((p = (plugin_t *) malloc (sizeof (plugin_t))) == NULL)
187 if ((p->type = strdup (type)) == NULL)
197 p->next = first_plugin;
202 * Send received data back to the plugin/module which will append DS
203 * definitions and pass it on to ``rrd_update_file''.
206 void plugin_write (char *host, char *type, char *inst, char *val)
210 if ((p = plugin_search (type)) == NULL)
213 if (p->write == NULL)
216 (*p->write) (host, inst, val);
218 #endif /* HAVE_LIBRRD */
221 * Receive data from the plugin/module and get it somehow to ``plugin_write'':
222 * Either using ``multicast_send'' (when in network/client mode) or call it
223 * directly (in local mode).
225 void plugin_submit (char *type, char *inst, char *val)
228 if (operating_mode == MODE_LOCAL)
229 plugin_write (NULL, type, inst, val);
230 else if (operating_mode == MODE_CLIENT)
231 multicast_send (type, inst, val);
232 else /* operating_mode == MODE_SERVER */
233 syslog (LOG_ERR, "WTF is the server doing in ``plugin_submit''?!?\n");
235 multicast_send (type, inst, val);