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 * Returns true if the plugin is loaded (i.e. `exists') and false otherwise.
78 * This is used in `configfile.c' to skip sections that are not needed..
80 int plugin_exists (char *type)
82 if (plugin_search (type) == NULL)
89 * (Try to) load the shared object `name'. Won't complain if it isn't a shared
90 * object, but it will bitch about a shared object not having a
91 * ``module_register'' symbol..
93 void plugin_load (char *name)
96 void (*reg_handle) (void);
99 lt_dlerror (); /* clear errors */
101 if ((dlh = lt_dlopen (name)) == NULL)
104 if ((reg_handle = lt_dlsym (dlh, "module_register")) == NULL)
106 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
107 name, lt_dlerror ());
116 * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
119 int plugin_load_all (char *dir)
123 char filename[BUFSIZE];
129 if ((dh = opendir (dir)) == NULL)
131 fprintf (stderr, "Error: Cannot read plugin directory `%s'\n", dir);
135 while ((de = readdir (dh)) != NULL)
137 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
140 if (lstat (filename, &statbuf) == -1)
142 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
145 else if (!S_ISREG (statbuf.st_mode))
150 plugin_load (filename);
155 return (plugin_count ());
160 * Call `init' on all plugins (if given)
162 void plugin_init_all (void)
166 for (p = first_plugin; p != NULL; p = p->next)
172 * Call `read' on all plugins (if given)
174 void plugin_read_all (void)
178 for (p = first_plugin; p != NULL; p = p->next)
184 * Add plugin to the linked list of registered plugins.
186 void plugin_register (char *type,
189 void (*write) (char *, char *, char *))
193 if (plugin_search (type) != NULL)
196 if ((p = (plugin_t *) malloc (sizeof (plugin_t))) == NULL)
199 if ((p->type = strdup (type)) == NULL)
209 p->next = first_plugin;
214 * Send received data back to the plugin/module which will append DS
215 * definitions and pass it on to ``rrd_update_file''.
218 void plugin_write (char *host, char *type, char *inst, char *val)
222 if ((p = plugin_search (type)) == NULL)
225 if (p->write == NULL)
228 (*p->write) (host, inst, val);
230 #endif /* HAVE_LIBRRD */
233 * Receive data from the plugin/module and get it somehow to ``plugin_write'':
234 * Either using ``multicast_send'' (when in network/client mode) or call it
235 * directly (in local mode).
237 void plugin_submit (char *type, char *inst, char *val)
240 if (operating_mode == MODE_LOCAL)
241 plugin_write (NULL, type, inst, val);
242 else if (operating_mode == MODE_CLIENT)
243 multicast_send (type, inst, val);
244 else /* operating_mode == MODE_SERVER */
245 syslog (LOG_ERR, "WTF is the server doing in ``plugin_submit''?!?\n");
247 multicast_send (type, inst, val);