#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
+#include "utils_lua.h"
#if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
# undef sprintf
RETURN_LUA (l, 0);
} /* }}} int lua_cb_log */
+static int lua_cb_dispatch_values (lua_State *l) /* {{{ */
+{
+ value_list_t *vl;
+ int nargs = lua_gettop (l); /* number of arguments */
+ char identifier[6 * DATA_MAX_NAME_LEN];
+
+ if (nargs != 1)
+ {
+ WARNING ("lua plugin: collectd_dispatch_values() called "
+ "with an invalid number of arguments (%i).", nargs);
+ RETURN_LUA (l, -1);
+ }
+
+ if (!lua_istable (l, 1))
+ {
+ WARNING ("lua plugin: The first argument to collectd_dispatch_values() "
+ "must be a \"value list\" (i.e. a table).");
+ RETURN_LUA (l, -1);
+ }
+
+ vl = luaC_tovaluelist (l, /* idx = */ -1);
+ if (vl == NULL)
+ {
+ WARNING ("lua plugin: ltoc_value_list failed.");
+ RETURN_LUA (l, -1);
+ }
+
+ FORMAT_VL (identifier, sizeof (identifier), vl);
+
+ DEBUG ("lua plugin: collectd_dispatch_values: Received value list \"%s\", time %.3f, interval %.3f.",
+ identifier, CDTIME_T_TO_DOUBLE (vl->time), CDTIME_T_TO_DOUBLE (vl->interval));
+
+ sfree (vl->values);
+ sfree (vl);
+ RETURN_LUA (l, 0);
+} /* }}} lua_cb_dispatch_values */
+
static int lua_cb_register_read (lua_State *l) /* {{{ */
{
static int count = 0;
static lua_c_functions_t lua_c_functions[] =
{
{ "collectd_log", lua_cb_log },
+ { "collectd_dispatch_values", lua_cb_dispatch_values },
{ "collectd_register_read", lua_cb_register_read }
};
return (-1);
}
- status = luaL_dofile (script->lua_state, script->script_path);
+ status = luaL_loadfile (script->lua_state, script->script_path);
+ if (status != 0)
+ {
+ ERROR ("lua plugin: luaL_loadfile failed with status %i", status);
+ lua_script_free (script);
+ return (-1);
+ }
+
+ status = lua_pcall (script->lua_state,
+ /* nargs = */ 0,
+ /* nresults = */ LUA_MULTRET,
+ /* errfunc = */ 0);
if (status != 0)
{
const char *errmsg;
- switch (status)
- {
- case LUA_ERRSYNTAX: errmsg = "Syntax error"; break;
- case LUA_ERRFILE: errmsg = "File I/O error"; break;
- case LUA_ERRMEM: errmsg = "Memory allocation error"; break;
- default: errmsg = "Unexpected error";
- }
+ errmsg = lua_tostring (script->lua_state, /* stack pos = */ -1);
- ERROR ("lua plugin: Loading script \"%s\" failed: %s",
- script->script_path, errmsg);
+ if (errmsg == NULL)
+ ERROR ("lua plugin: lua_pcall failed with status %i. "
+ "In addition, no error message could be retrieved from the stack.",
+ status);
+ else
+ ERROR ("lua plugin: Executing script \"%s\" failed:\n%s",
+ script->script_path, errmsg);
lua_script_free (script);
return (-1);