+static int lua_cb_register_read (lua_State *l) /* {{{ */
+{
+ static int count = 0;
+ int nargs = lua_gettop (l); /* number of arguments */
+ clua_read_function_t *rf;
+
+ int num = count++; /* XXX FIXME: Not thread-safe! */
+ char function_name[64];
+
+ if (nargs != 1)
+ {
+ WARNING ("lua plugin: collectd_register_read() called with an invalid "
+ "number of arguments (%i).", nargs);
+ RETURN_LUA (l, -1);
+ }
+
+ /* If the argument is a string, assume it's a global function and try to look
+ * it up. */
+ if (lua_isstring (l, /* stack pos = */ 1))
+ lua_gettable (l, LUA_GLOBALSINDEX);
+
+ if (!lua_isfunction (l, /* stack pos = */ 1))
+ {
+ WARNING ("lua plugin: The first argument of collectd_register_read() "
+ "must be a function.");
+ RETURN_LUA (l, -1);
+ }
+
+ ssnprintf (function_name, sizeof (function_name), "collectd.read_func_%i", num);
+
+ /* Push the name of the global variable */
+ lua_pushstring (l, function_name);
+ /* Push the name down to the first position */
+ lua_insert (l, 1);
+ /* Now set the global variable called "collectd". */
+ lua_settable (l, LUA_REGISTRYINDEX);
+
+ rf = malloc (sizeof (*rf));
+ if (rf == NULL)
+ {
+ ERROR ("lua plugin: malloc failed.");
+ RETURN_LUA (l, -1);
+ }
+ else
+ {
+ user_data_t ud = { rf, /* free func */ NULL /* FIXME */ };
+ char cb_name[DATA_MAX_NAME_LEN];
+
+ memset (rf, 0, sizeof (*rf));
+ rf->lua_state = l;
+ rf->lua_function_name = strdup (function_name);
+
+ ssnprintf (cb_name, sizeof (cb_name), "lua/read_func_%i", num);
+
+ plugin_register_complex_read (/* group = */ "lua",
+ /* name = */ cb_name,
+ /* callback = */ clua_read,
+ /* interval = */ NULL,
+ /* user_data = */ &ud);
+ }
+
+ DEBUG ("lua plugin: Successful call to lua_cb_register_read().");
+
+ RETURN_LUA (l, 0);
+} /* }}} int lua_cb_register_read */
+