Added copyright/GPL header to all .c and .h files in trunk
[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  * (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..
80  */
81 void plugin_load (char *name)
82 {
83         lt_dlhandle dlh;
84         void (*reg_handle) (void);
85
86         lt_dlinit ();
87         lt_dlerror (); /* clear errors */
88
89         if ((dlh = lt_dlopen (name)) == NULL)
90                 return;
91
92         if ((reg_handle = lt_dlsym (dlh, "module_register")) == NULL)
93         {
94                 syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
95                                 name, lt_dlerror ());
96                 lt_dlclose (dlh);
97                 return;
98         }
99
100         (*reg_handle) ();
101 }
102
103 /*
104  * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
105  */
106 #define BUFSIZE 512
107 int plugin_load_all (char *dir)
108 {
109         DIR *dh;
110         struct dirent *de;
111         char filename[BUFSIZE];
112         struct stat statbuf;
113
114         if (dir == NULL)
115                 dir = PLUGINDIR;
116
117         if ((dh = opendir (dir)) == NULL)
118         {
119                 fprintf (stderr, "Error: Cannot read plugin directory `%s'\n", dir);
120                 return (0);
121         }
122
123         while ((de = readdir (dh)) != NULL)
124         {
125                 if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
126                         continue;
127
128                 if (lstat (filename, &statbuf) == -1)
129                 {
130                         syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
131                         continue;
132                 }
133                 else if (!S_ISREG (statbuf.st_mode))
134                 {
135                         continue;
136                 }
137
138                 plugin_load (filename);
139         }
140
141         closedir (dh);
142
143         return (plugin_count ());
144 }
145 #undef BUFSIZE
146
147 /*
148  * Call `init' on all plugins (if given)
149  */
150 void plugin_init_all (void)
151 {
152         plugin_t *p;
153
154         for (p = first_plugin; p != NULL; p = p->next)
155                 if (p->init != NULL)
156                         (*p->init) ();
157 }
158
159 /*
160  * Call `read' on all plugins (if given)
161  */
162 void plugin_read_all (void)
163 {
164         plugin_t *p;
165
166         for (p = first_plugin; p != NULL; p = p->next)
167                 if (p->read != NULL)
168                         (*p->read) ();
169 }
170
171 /*
172  * Add plugin to the linked list of registered plugins.
173  */
174 void plugin_register (char *type,
175                 void (*init) (void),
176                 void (*read) (void),
177                 void (*write) (char *, char *, char *))
178 {
179         plugin_t *p;
180
181         if (plugin_search (type) != NULL)
182                 return;
183
184         if ((p = (plugin_t *) malloc (sizeof (plugin_t))) == NULL)
185                 return;
186
187         if ((p->type = strdup (type)) == NULL)
188         {
189                 free (p);
190                 return;
191         }
192
193         p->init  = init;
194         p->read  = read;
195         p->write = write;
196
197         p->next = first_plugin;
198         first_plugin = p;
199 }
200
201 /*
202  * Send received data back to the plugin/module which will append DS
203  * definitions and pass it on to ``rrd_update_file''.
204  */
205 #ifdef HAVE_LIBRRD
206 void plugin_write (char *host, char *type, char *inst, char *val)
207 {
208         plugin_t *p;
209
210         if ((p = plugin_search (type)) == NULL)
211                 return;
212
213         if (p->write == NULL)
214                 return;
215
216         (*p->write) (host, inst, val);
217 }
218 #endif /* HAVE_LIBRRD */
219
220 /*
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).
224  */
225 void plugin_submit (char *type, char *inst, char *val)
226 {
227 #ifdef HAVE_LIBRRD
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");
234 #else
235         multicast_send (type, inst, val);
236 #endif
237 }