13 void (*write) (char *host, char *inst, char *val);
17 static plugin_t *first_plugin = NULL;
20 extern int operating_mode;
24 * Returns the number of plugins registered
26 int plugin_count (void)
31 for (i = 0, p = first_plugin; p != NULL; p = p->next)
38 * Returns the plugins with the type `type' or NULL if it's not found.
40 plugin_t *plugin_search (char *type)
47 for (ret = first_plugin; ret != NULL; ret = ret->next)
48 if (strcmp (ret->type, type) == 0)
55 * (Try to) load the shared object `name'. Won't complain if it isn't a shared
56 * object, but it will bitch about a shared object not having a
57 * ``module_register'' symbol..
59 void plugin_load (char *name)
62 void (*reg_handle) (void);
65 lt_dlerror (); /* clear errors */
67 if ((dlh = lt_dlopen (name)) == NULL)
70 if ((reg_handle = lt_dlsym (dlh, "module_register")) == NULL)
72 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
82 * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
85 int plugin_load_all (char *dir)
89 char filename[BUFSIZE];
95 if ((dh = opendir (dir)) == NULL)
97 fprintf (stderr, "Error: Cannot read plugin directory `%s'\n", dir);
101 while ((de = readdir (dh)) != NULL)
103 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
106 if (lstat (filename, &statbuf) == -1)
108 syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
111 else if (!S_ISREG (statbuf.st_mode))
116 plugin_load (filename);
121 return (plugin_count ());
126 * Call `init' on all plugins (if given)
128 void plugin_init_all (void)
132 for (p = first_plugin; p != NULL; p = p->next)
138 * Call `read' on all plugins (if given)
140 void plugin_read_all (void)
144 for (p = first_plugin; p != NULL; p = p->next)
150 * Add plugin to the linked list of registered plugins.
152 void plugin_register (char *type,
155 void (*write) (char *, char *, char *))
159 if (plugin_search (type) != NULL)
162 if ((p = (plugin_t *) malloc (sizeof (plugin_t))) == NULL)
165 if ((p->type = strdup (type)) == NULL)
175 p->next = first_plugin;
180 * Send received data back to the plugin/module which will append DS
181 * definitions and pass it on to ``rrd_update_file''.
184 void plugin_write (char *host, char *type, char *inst, char *val)
188 if ((p = plugin_search (type)) == NULL)
191 if (p->write == NULL)
194 (*p->write) (host, inst, val);
196 #endif /* HAVE_LIBRRD */
199 * Receive data from the plugin/module and get it somehow to ``plugin_write'':
200 * Either using ``multicast_send'' (when in network/client mode) or call it
201 * directly (in local mode).
203 void plugin_submit (char *type, char *inst, char *val)
206 if (operating_mode == MODE_LOCAL)
207 plugin_write (NULL, type, inst, val);
208 else if (operating_mode == MODE_CLIENT)
209 multicast_send (type, inst, val);
210 else /* operating_mode == MODE_SERVER */
211 syslog (LOG_ERR, "WTF is the server doing in ``plugin_submit''?!?\n");
213 multicast_send (type, inst, val);