3 * Copyright (C) 2010 Julien Ammous
4 * Copyright (C) 2010 Florian Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; only version 2.1 of the License is
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Florian Forster <octo at collectd.org>
25 /* <lua5.1/luaconf.h> defines a macro using "sprintf". Although not used here,
26 * GCC will complain about the macro definition. */
27 #define DONT_POISON_SPRINTF_YET
32 #include "configfile.h"
33 #include "utils_cache.h"
35 /* Include the Lua API header files. */
40 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
42 # pragma GCC poison sprintf
45 typedef struct lua_script_s {
49 struct lua_script_s *next;
52 struct clua_read_function_s
55 char *lua_function_name;
57 typedef struct clua_read_function_s clua_read_function_t;
59 struct lua_c_functions_s
64 typedef struct lua_c_functions_s lua_c_functions_t;
66 static char base_path[PATH_MAX + 1] = "";
67 static lua_script_t *scripts = NULL;
69 static int clua_read (user_data_t *ud) /* {{{ */
71 clua_read_function_t *rf = ud->data;
74 /* Load the function to the stack */
75 lua_pushstring (rf->lua_state, rf->lua_function_name);
76 lua_gettable (rf->lua_state, LUA_REGISTRYINDEX);
78 if (!lua_isfunction (rf->lua_state, /* stack pos = */ -1))
80 ERROR ("lua plugin: Unable to lookup the read function \"%s\".",
81 rf->lua_function_name);
82 /* pop the value again */
83 lua_settop (rf->lua_state, -2);
87 lua_call (rf->lua_state, /* nargs = */ 0, /* nresults = */ 1);
89 if (lua_isnumber (rf->lua_state, -1))
91 status = (int) lua_tonumber (rf->lua_state, -1);
95 ERROR ("lua plugin: Read function \"%s\" did not return a numeric status.",
96 rf->lua_function_name);
100 lua_settop (rf->lua_state, -2);
103 } /* }}} int clua_read */
105 /* Cleans up the stack, pushes the return value as a number onto the stack and
106 * returns the number of values returned (1). */
107 #define RETURN_LUA(l,status) do { \
108 lua_State *_l_state = (l); \
109 lua_settop (_l_state, 0); \
110 lua_pushnumber (_l_state, (lua_Number) (status)); \
117 static int lua_cb_log (lua_State *l) /* {{{ */
119 int nargs = lua_gettop (l); /* number of arguments */
125 WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).",
130 if (!lua_isnumber (l, 1))
132 WARNING ("lua plugin: The first argument to collectd_log() must be a number.");
136 if (!lua_isstring (l, 2))
138 WARNING ("lua plugin: The second argument to collectd_log() must be a string.");
142 severity = (int) lua_tonumber (l, /* stack pos = */ 1);
143 if ((severity != LOG_ERR)
144 && (severity != LOG_WARNING)
145 && (severity != LOG_NOTICE)
146 && (severity != LOG_INFO)
147 && (severity != LOG_DEBUG))
150 msg = lua_tostring (l, 2);
153 ERROR ("lua plugin: lua_tostring failed.");
157 plugin_log (severity, "%s", msg);
160 } /* }}} int lua_cb_log */
162 static int lua_cb_register_read (lua_State *l) /* {{{ */
164 static int count = 0;
165 int nargs = lua_gettop (l); /* number of arguments */
166 clua_read_function_t *rf;
168 int num = count++; /* XXX FIXME: Not thread-safe! */
169 char function_name[64];
173 WARNING ("lua plugin: collectd_register_read() called with an invalid "
174 "number of arguments (%i).", nargs);
178 /* If the argument is a string, assume it's a global function and try to look
180 if (lua_isstring (l, /* stack pos = */ 1))
181 lua_gettable (l, LUA_GLOBALSINDEX);
183 if (!lua_isfunction (l, /* stack pos = */ 1))
185 WARNING ("lua plugin: The first argument of collectd_register_read() "
186 "must be a function.");
190 ssnprintf (function_name, sizeof (function_name), "collectd.read_func_%i", num);
192 /* Push the name of the global variable */
193 lua_pushstring (l, function_name);
194 /* Push the name down to the first position */
196 /* Now set the global variable called "collectd". */
197 lua_settable (l, LUA_REGISTRYINDEX);
199 rf = malloc (sizeof (*rf));
202 ERROR ("lua plugin: malloc failed.");
207 user_data_t ud = { rf, /* free func */ NULL /* FIXME */ };
208 char cb_name[DATA_MAX_NAME_LEN];
210 memset (rf, 0, sizeof (*rf));
212 rf->lua_function_name = strdup (function_name);
214 ssnprintf (cb_name, sizeof (cb_name), "lua/read_func_%i", num);
216 plugin_register_complex_read (/* group = */ "lua",
217 /* name = */ cb_name,
218 /* callback = */ clua_read,
219 /* interval = */ NULL,
220 /* user_data = */ &ud);
223 DEBUG ("lua plugin: Successful call to lua_cb_register_read().");
226 } /* }}} int lua_cb_register_read */
228 static lua_c_functions_t lua_c_functions[] =
230 { "collectd_log", lua_cb_log },
231 { "collectd_register_read", lua_cb_register_read }
234 /* Declare the Lua libraries we wish to use.
235 * Note: If you are opening and running a file containing Lua code using
236 * 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries used in
237 * that file here also. */
238 static const luaL_reg lua_load_libs[] =
240 { LUA_COLIBNAME, luaopen_base },
241 /* { "luaopen_loadlib", luaopen_loadlib }, */
243 { LUA_DBLIBNAME, luaopen_debug },
245 { LUA_TABLIBNAME, luaopen_table },
246 { LUA_IOLIBNAME, luaopen_io },
247 { LUA_STRLIBNAME, luaopen_string },
248 { LUA_MATHLIBNAME, luaopen_math }
251 static void lua_script_free (lua_script_t *script) /* {{{ */
260 if (script->lua_state != NULL)
262 lua_close (script->lua_state);
263 script->lua_state = NULL;
266 sfree (script->script_path);
269 lua_script_free (next);
270 } /* }}} void lua_script_free */
272 static int lua_script_init (lua_script_t *script) /* {{{ */
276 memset (script, 0, sizeof (*script));
277 script->script_path = NULL;
280 /* initialize the lua context */
281 script->lua_state = lua_open();
282 if (script->lua_state == NULL)
284 ERROR ("lua plugin: lua_open failed.");
288 /* Open up all the Lua libraries declared above. */
289 for (i = 0; i < STATIC_ARRAY_SIZE (lua_load_libs); i++)
293 status = (*lua_load_libs[i].func) (script->lua_state);
295 WARNING ("lua plugin: Loading library \"%s\" failed.",
296 lua_load_libs[i].name);
299 /* Register all the functions we implement in C */
300 for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
301 lua_register (script->lua_state,
302 lua_c_functions[i].name, lua_c_functions[i].func);
305 } /* }}} int lua_script_init */
307 static int lua_script_load (const char *script_path) /* {{{ */
309 lua_script_t *script;
312 script = malloc (sizeof (*script));
315 ERROR ("lua plugin: malloc failed.");
319 status = lua_script_init (script);
322 lua_script_free (script);
326 script->script_path = strdup (script_path);
327 if (script->script_path == NULL)
329 ERROR ("lua plugin: strdup failed.");
330 lua_script_free (script);
334 status = luaL_dofile (script->lua_state, script->script_path);
341 case LUA_ERRSYNTAX: errmsg = "Syntax error"; break;
342 case LUA_ERRFILE: errmsg = "File I/O error"; break;
343 case LUA_ERRMEM: errmsg = "Memory allocation error"; break;
344 default: errmsg = "Unexpected error";
347 ERROR ("lua plugin: Loading script \"%s\" failed: %s",
348 script->script_path, errmsg);
350 lua_script_free (script);
354 /* Append this script to the global list of scripts. */
364 while (last->next != NULL)
371 } /* }}} int lua_script_load */
373 static int lua_config_base_path (const oconfig_item_t *ci) /* {{{ */
378 status = cf_util_get_string_buffer (ci, base_path, sizeof (base_path));
382 len = strlen (base_path);
383 while ((len > 0) && (base_path[len - 1] == '/'))
389 DEBUG ("lua plugin: base_path = \"%s\";", base_path);
392 } /* }}} int lua_config_base_path */
394 static int lua_config_script (const oconfig_item_t *ci) /* {{{ */
396 char rel_path[PATH_MAX + 1];
397 char abs_path[PATH_MAX + 1];
400 status = cf_util_get_string_buffer (ci, rel_path, sizeof (rel_path));
404 if (base_path[0] == 0)
405 sstrncpy (abs_path, rel_path, sizeof (abs_path));
407 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", base_path, rel_path);
409 DEBUG ("lua plugin: abs_path = \"%s\";", abs_path);
411 status = lua_script_load (abs_path);
415 INFO("lua plugin: File \"%s\" loaded succesfully", abs_path);
418 } /* }}} int lua_config_script */
423 * Script "script1.lua"
424 * Script "script2.lua"
427 static int lua_config (oconfig_item_t *ci) /* {{{ */
431 for (i = 0; i < ci->children_num; i++)
433 oconfig_item_t *child = ci->children + i;
435 if (strcasecmp ("BasePath", child->key) == 0) {
436 lua_config_base_path(child);
438 else if (strcasecmp ("Script", child->key) == 0){
439 lua_config_script(child);
443 WARNING ("network plugin: Option `%s' is not allowed here.",
449 } /* }}} int lua_config */
451 static int lua_shutdown (void) /* {{{ */
453 lua_script_free (scripts);
457 } /* }}} int lua_shutdown */
459 void module_register()
461 plugin_register_complex_config("lua", lua_config);
462 plugin_register_shutdown("lua", lua_shutdown);
465 /* vim: set sw=2 sts=2 et fdm=marker : */