9bc02abec50fff8f1ceb9a5437e4eaff9f7951da
[collectd.git] / src / plugin.c
1 /**
2  * collectd - src/plugin.c
3  * Copyright (C) 2005  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  **/
22
23 #include "collectd.h"
24
25 #include <ltdl.h>
26
27 #include "plugin.h"
28 #include "multicast.h"
29
30 typedef struct plugin
31 {
32         char *type;
33         void (*init) (void);
34         void (*read) (void);
35         void (*write) (char *host, char *inst, char *val);
36         struct plugin *next;
37 } plugin_t;
38
39 static plugin_t *first_plugin = NULL;
40
41 #ifdef HAVE_LIBRRD
42 extern int operating_mode;
43 #endif
44
45 /*
46  * Returns the number of plugins registered
47  */
48 int plugin_count (void)
49 {
50         int i;
51         plugin_t *p;
52
53         for (i = 0, p = first_plugin; p != NULL; p = p->next)
54                 i++;
55
56         return (i);
57 }
58
59 /*
60  * Returns the plugins with the type `type' or NULL if it's not found.
61  */
62 plugin_t *plugin_search (char *type)
63 {
64         plugin_t *ret;
65
66         if (type == NULL)
67                 return (NULL);
68
69         for (ret = first_plugin; ret != NULL; ret = ret->next)
70                 if (strcmp (ret->type, type) == 0)
71                         break;
72
73         return (ret);
74 }
75
76 /*
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..
79  */
80 int plugin_exists (char *type)
81 {
82         if (plugin_search (type) == NULL)
83                 return (0);
84         else
85                 return (1);
86 }
87
88 /*
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..
92  */
93 void plugin_load (char *name)
94 {
95         lt_dlhandle dlh;
96         void (*reg_handle) (void);
97
98         lt_dlinit ();
99         lt_dlerror (); /* clear errors */
100
101         if ((dlh = lt_dlopen (name)) == NULL)
102                 return;
103
104         if ((reg_handle = lt_dlsym (dlh, "module_register")) == NULL)
105         {
106                 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
107                                 name, lt_dlerror ());
108                 lt_dlclose (dlh);
109                 return;
110         }
111
112         (*reg_handle) ();
113 }
114
115 /*
116  * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
117  */
118 #define BUFSIZE 512
119 int plugin_load_all (char *dir)
120 {
121         DIR *dh;
122         struct dirent *de;
123         char filename[BUFSIZE];
124         struct stat statbuf;
125
126         if (dir == NULL)
127                 dir = PLUGINDIR;
128
129         if ((dh = opendir (dir)) == NULL)
130         {
131                 fprintf (stderr, "Error: Cannot read plugin directory `%s'\n", dir);
132                 return (0);
133         }
134
135         while ((de = readdir (dh)) != NULL)
136         {
137                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
138                         continue;
139
140                 if (lstat (filename, &statbuf) == -1)
141                 {
142                         syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
143                         continue;
144                 }
145                 else if (!S_ISREG (statbuf.st_mode))
146                 {
147                         continue;
148                 }
149
150                 plugin_load (filename);
151         }
152
153         closedir (dh);
154
155         return (plugin_count ());
156 }
157 #undef BUFSIZE
158
159 /*
160  * Call `init' on all plugins (if given)
161  */
162 void plugin_init_all (void)
163 {
164         plugin_t *p;
165
166         for (p = first_plugin; p != NULL; p = p->next)
167                 if (p->init != NULL)
168                         (*p->init) ();
169 }
170
171 /*
172  * Call `read' on all plugins (if given)
173  */
174 void plugin_read_all (void)
175 {
176         plugin_t *p;
177
178         for (p = first_plugin; p != NULL; p = p->next)
179                 if (p->read != NULL)
180                         (*p->read) ();
181 }
182
183 /*
184  * Add plugin to the linked list of registered plugins.
185  */
186 void plugin_register (char *type,
187                 void (*init) (void),
188                 void (*read) (void),
189                 void (*write) (char *, char *, char *))
190 {
191         plugin_t *p;
192
193         if (plugin_search (type) != NULL)
194                 return;
195
196         if ((p = (plugin_t *) malloc (sizeof (plugin_t))) == NULL)
197                 return;
198
199         if ((p->type = strdup (type)) == NULL)
200         {
201                 free (p);
202                 return;
203         }
204
205         p->init  = init;
206         p->read  = read;
207         p->write = write;
208
209         p->next = first_plugin;
210         first_plugin = p;
211 }
212
213 /*
214  * Send received data back to the plugin/module which will append DS
215  * definitions and pass it on to ``rrd_update_file''.
216  */
217 #ifdef HAVE_LIBRRD
218 void plugin_write (char *host, char *type, char *inst, char *val)
219 {
220         plugin_t *p;
221
222         if ((p = plugin_search (type)) == NULL)
223                 return;
224
225         if (p->write == NULL)
226                 return;
227
228         (*p->write) (host, inst, val);
229 }
230 #endif /* HAVE_LIBRRD */
231
232 /*
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).
236  */
237 void plugin_submit (char *type, char *inst, char *val)
238 {
239 #ifdef HAVE_LIBRRD
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");
246 #else
247         multicast_send (type, inst, val);
248 #endif
249 }