lua plugin: Fix includes.
[collectd.git] / src / lua.c
1 /**
2  * collectd - src/lua.c
3  * Copyright (C) 2010       Julien Ammous
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; only version 2.1 of the License is
8  * applicable.
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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors:
20  *   Julien Ammous
21  **/
22
23 #include "collectd.h"
24 #include "plugin.h"
25 #include "common.h"
26 #include "configfile.h"
27 #include "utils_cache.h"
28
29 /* Include the Lua API header files. */
30 #include <lua.h>
31 #include <lauxlib.h>
32 #include <lualib.h>
33
34 #include "lua_exports.c"
35
36 typedef struct lua_script_s {
37   char          *script_path;
38   lua_State     *lua_state;
39   
40   struct lua_script_s  *next;
41 } lua_script_t;
42
43
44 static char           base_path[MAXPATHLEN];
45 static lua_script_t   scripts;
46
47
48
49 /* Declare the Lua libraries we wish to use. */
50 /* Note: If you are opening and running a file containing Lua code */
51 /* using 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries */
52 /* used in that file here also. */
53 static const luaL_reg lualibs[] =
54 {
55         // { "base",       luaopen_base },
56         { NULL,         NULL }
57 };
58
59 /* A function to open up all the Lua libraries you declared above. */
60 static void openlualibs(lua_State *l)
61 {
62   const luaL_reg *lib;
63   for( lib = lualibs; lib->func != NULL; lib++) {
64     lib->func(l);
65     lua_settop(l, 0);
66   }
67 }
68
69
70
71
72
73 static int lua_config_base_path (const oconfig_item_t *ci)
74 {
75   if( (ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING) ){
76     ERROR("lua plugin: The '%s' config option requires a single string argument", ci->key);
77     return -1;
78   }
79   
80   strncpy(base_path, ci->values[0].value.string, sizeof(base_path));
81   
82   /* add ending slash if not provided */
83   if( base_path[strlen(base_path) - 1] != '/' ){
84     base_path[strlen(base_path)] = '/';
85   }
86   
87   INFO("lua plugin: BasePath = '%s'", base_path);
88   return 0;
89 }
90
91 static int lua_config_script (const oconfig_item_t *ci)
92 {
93   lua_script_t *script = &scripts;
94   
95   if( (ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING) ){
96     ERROR("lua plugin: The '%s' config option requires a single string argument", ci->key);
97     return -1;
98   }
99   
100   /* find a free slot for the structure */
101   while( script->next != NULL ){
102     script = script->next;
103   }
104   
105   /* build full path : base_path + given path + \0 */
106   script->script_path = malloc(
107       strlen(base_path) +
108       strlen(ci->values[0].value.string) + 1
109     );
110   
111   strncpy(script->script_path, base_path, sizeof(script->script_path));
112   strncpy(script->script_path + strlen(base_path), ci->values[0].value.string, sizeof(script->script_path));
113   
114   /* check if the file exists and we can read it */
115   if( access(script->script_path, R_OK) == -1 ) {
116     ERROR("lua plugin: Cannot read file '%s' : %s", script->script_path, strerror(errno));
117     free(script->script_path);
118     return -1;
119   }
120   
121   /* initialize the lua context */
122   script->lua_state = lua_open();
123   
124   openlualibs(script->lua_state);
125   
126   if( register_exported_functions(script->lua_state) != 0 ) {
127     ERROR("lua plugin: Cannot register exported functions, aborting");
128     free(script->script_path);
129     return -1;
130   }
131   
132   /* and try to load the file */
133   if( luaL_dofile(script->lua_state, "script.lua") != 0 ) {
134     ERROR("lua plugin: error while loading '%s' => %s\n", script->script_path, lua_tostring(script->lua_state, -1));
135     free(script->script_path);
136     return -1;
137   }
138   
139   INFO("lua plugin: file '%s' loaded succesfully", script->script_path);
140   
141   return 0;
142 }
143
144 // <Plugin lua>
145 //   BasePath /
146 //   Script script1.lua
147 //   Script script2.lua
148 // </Plugin>
149 static int lua_config(oconfig_item_t *ci)
150 {
151   int i;
152
153   for (i = 0; i < ci->children_num; i++)
154   {
155     oconfig_item_t *child = ci->children + i;
156
157     if (strcasecmp ("BasePath", child->key) == 0) {
158       lua_config_base_path(child);
159     }
160     else if (strcasecmp ("Script", child->key) == 0){
161       lua_config_script(child);
162     }
163     else
164     {
165       WARNING ("network plugin: Option `%s' is not allowed here.",
166           child->key);
167     }
168   }
169   
170   return 0;
171 }
172
173 static int lua_init()
174 {
175   INFO("Lua plugin loaded.");
176   
177   return 0;
178 }
179
180 void module_register()
181 {
182   plugin_register_complex_config("lua", lua_config);
183   plugin_register_init("lua", lua_init);
184 }
185
186
187