lua plugin: A first and very simple take at read callbacks.
[collectd.git] / src / lua.c
index 74cd41d..b79e0b1 100644 (file)
--- a/src/lua.c
+++ b/src/lua.c
@@ -1,6 +1,7 @@
 /**
  * collectd - src/lua.c
  * Copyright (C) 2010       Julien Ammous
+ * Copyright (C) 2010       Florian Forster
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published by
@@ -18,6 +19,7 @@
  *
  * Authors:
  *   Julien Ammous
+ *   Florian Forster <octo at collectd.org>
  **/
 
 #include "collectd.h"
@@ -31,8 +33,6 @@
 #include <lauxlib.h>
 #include <lualib.h>
 
-#include "lua_exports.c"
-
 typedef struct lua_script_s {
   char          *script_path;
   lua_State     *lua_state;
@@ -40,15 +40,196 @@ typedef struct lua_script_s {
   struct lua_script_s  *next;
 } lua_script_t;
 
+struct clua_read_function_s
+{
+  lua_State *lua_state;
+  char *lua_function_name;
+};
+typedef struct clua_read_function_s clua_read_function_t;
+
+struct lua_c_functions_s
+{
+  char         *name;
+  lua_CFunction func;
+};
+typedef struct lua_c_functions_s lua_c_functions_t;
+
 static char           base_path[PATH_MAX + 1] = "";
 static lua_script_t  *scripts = NULL;
 
+static int clua_read (user_data_t *ud) /* {{{ */
+{
+  clua_read_function_t *rf = ud->data;
+  int status;
+
+  /* Load the function to the stack */
+  lua_pushstring (rf->lua_state, rf->lua_function_name);
+  lua_gettable (rf->lua_state, LUA_REGISTRYINDEX);
+
+  if (!lua_isfunction (rf->lua_state, /* stack pos = */ -1))
+  {
+    ERROR ("lua plugin: Unable to lookup the read function \"%s\".",
+        rf->lua_function_name);
+    /* pop the value again */
+    lua_settop (rf->lua_state, -2);
+    return (-1);
+  }
+
+  lua_call (rf->lua_state, /* nargs = */ 0, /* nresults = */ 1);
+
+  if (lua_isnumber (rf->lua_state, -1))
+  {
+    status = (int) lua_tonumber (rf->lua_state, -1);
+  }
+  else
+  {
+    ERROR ("lua plugin: Read function \"%s\" did not return a numeric status.",
+        rf->lua_function_name);
+    status = -1;
+  }
+  /* pop the value */
+  lua_settop (rf->lua_state, -2);
+
+  return (status);
+} /* }}} int clua_read */
+
+/* Cleans up the stack, pushes the return value as a number onto the stack and
+ * returns the number of values returned (1). */
+#define RETURN_LUA(l,status) do {                    \
+  lua_State *_l_state = (l);                         \
+  lua_settop (_l_state, 0);                          \
+  lua_pushnumber (_l_state, (lua_Number) (status));  \
+  return (1);                                        \
+} while (0)
+
+/*
+ * Exported functions
+ */
+static int lua_cb_log (lua_State *l) /* {{{ */
+{
+  int nargs = lua_gettop (l); /* number of arguments */
+  int severity;
+  const char *msg;
+
+  if (nargs != 2)
+  {
+    WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).",
+        nargs);
+    RETURN_LUA (l, -1);
+  }
+
+  if (!lua_isnumber (l, 1))
+  {
+    WARNING ("lua plugin: The first argument to collectd_log() must be a number.");
+    RETURN_LUA (l, -1);
+  }
+
+  if (!lua_isstring (l, 2))
+  {
+    WARNING ("lua plugin: The second argument to collectd_log() must be a string.");
+    RETURN_LUA (l, -1);
+  }
+
+  severity = (int) lua_tonumber (l, /* stack pos = */ 1);
+  if ((severity != LOG_ERR)
+      && (severity != LOG_WARNING)
+      && (severity != LOG_NOTICE)
+      && (severity != LOG_INFO)
+      && (severity != LOG_DEBUG))
+    severity = LOG_ERR;
+
+  msg = lua_tostring (l, 2);
+  if (msg == NULL)
+  {
+    ERROR ("lua plugin: lua_tostring failed.");
+    RETURN_LUA (l, -1);
+  }
+
+  plugin_log (severity, "%s", msg);
+
+  RETURN_LUA (l, 0);
+} /* }}} int lua_cb_log */
+
+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 */
+
+static lua_c_functions_t lua_c_functions[] =
+{
+  { "collectd_log", lua_cb_log },
+  { "collectd_register_read", lua_cb_register_read }
+};
+
 /* Declare the Lua libraries we wish to use.
  * Note: If you are opening and running a file containing Lua code using
  * 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries used in
  * that file here also. */
 static const luaL_reg lua_load_libs[] =
 {
+  { LUA_COLIBNAME,   luaopen_base   },
+  /* { "luaopen_loadlib", luaopen_loadlib }, */
 #if COLLECT_DEBUG
   { LUA_DBLIBNAME,   luaopen_debug  },
 #endif
@@ -107,7 +288,9 @@ static int lua_script_init (lua_script_t *script) /* {{{ */
   }
 
   /* Register all the functions we implement in C */
-  register_exported_functions (script->lua_state);
+  for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
+    lua_register (script->lua_state,
+        lua_c_functions[i].name, lua_c_functions[i].func);
 
   return (0);
 } /* }}} int lua_script_init */
@@ -139,7 +322,7 @@ static int lua_script_load (const char *script_path) /* {{{ */
     return (-1);
   }
 
-  status = luaL_loadfile (script->lua_state, script->script_path);
+  status = lua_dofile (script->lua_state, script->script_path);
   if (status != 0)
   {
     const char *errmsg;