MINOR: lua: add class proxy
authorThierry Fournier <tfournier@arpalert.org>
Fri, 19 Feb 2016 19:56:00 +0000 (20:56 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 30 Mar 2016 16:43:42 +0000 (18:43 +0200)
This class provides the access to the proxy struct, it allows
some manipulations and retrieve informations.

doc/lua-api/index.rst
include/types/hlua.h
src/hlua_fcn.c

index fa16519..55d140a 100644 (file)
@@ -305,6 +305,14 @@ Core class
 
   :param integer milliseconds: the required milliseconds.
 
+.. js:attribute:: core.proxies
+
+  **context**: body, init, task, action, sample-fetch, converter
+
+  proxies is an array containing the list of all proxies declared in the
+  configuration file. Each entry of the proxies array is an object of type
+  :ref:`proxy_class`
+
 .. js:function:: core.register_action(name, actions, func)
 
   **context**: body
@@ -574,6 +582,71 @@ Core class
   Give back the hand at the HAProxy scheduler. It is used when the LUA
   processing consumes a lot of processing time.
 
+.. _proxy_class:
+
+Proxy class
+============
+
+.. js:class:: Proxy
+
+  This class provides a way for manipulating proxy and retrieving information
+  like statistics.
+
+.. js:function:: Proxy.pause(px)
+
+  Pause the proxy. See the management socket documentation for more information.
+
+  :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
+    proxy.
+
+.. js:function:: Proxy.resume(px)
+
+  Resume the proxy. See the management socket documentation for more
+  information.
+
+  :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
+    proxy.
+
+.. js:function:: Proxy.stop(px)
+
+  Stop the proxy. See the management socket documentation for more information.
+
+  :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
+    proxy.
+
+.. js:function:: Proxy.shut_bcksess(px)
+
+  Kill the session attached to a backup server. See the management socket
+  documentation for more information.
+
+  :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
+    proxy.
+
+.. js:function:: Proxy.get_cap(px)
+
+  Returns a string describing the capabilities of the proxy.
+
+  :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
+    proxy.
+  :returns: a string "frontend", "backend", "proxy" or "ruleset".
+
+.. js:function:: Proxy.get_mode(px)
+
+  Returns a string describing the mode of the current proxy.
+
+  :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
+    proxy.
+  :returns: a string "tcp", "http", "health" or "unknown"
+
+.. js:function:: Proxy.get_stats(px)
+
+  Returns an array containg the proxy statistics. The statistics returned are
+  not the same if the proxy is frontend or a backend.
+
+  :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
+    proxy.
+  :returns: a key/value array containing stats
+
 .. _concat_class:
 
 Concat class
index 34bd648..17cf141 100644 (file)
@@ -19,6 +19,7 @@
 #define CLASS_MAP          "Map"
 #define CLASS_APPLET_TCP   "AppletTCP"
 #define CLASS_APPLET_HTTP  "AppletHTTP"
+#define CLASS_PROXY        "Proxy"
 
 struct stream;
 
index f577be4..c8b640f 100644 (file)
 #include <lualib.h>
 
 #include <common/time.h>
+#include <common/uri_auth.h>
 
 #include <types/hlua.h>
+#include <types/proxy.h>
 
 #include <proto/dumpstats.h>
 #include <proto/proto_http.h>
+#include <proto/proxy.h>
+#include <proto/server.h>
 
 /* Contains the class reference of the concat object. */
 static int class_concat_ref;
+static int class_proxy_ref;
 
-#define STATS_LEN (INF_TOTAL_FIELDS)
+#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
 
 static struct field stats[STATS_LEN];
 
@@ -429,8 +434,123 @@ static int hlua_concat_init(lua_State *L)
        return 1;
 }
 
+int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
+{
+       lua_newtable(L);
+
+       /* Pop a class sesison metatable and affect it to the userdata. */
+       lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
+       lua_setmetatable(L, -2);
+
+       lua_pushlightuserdata(L, px);
+       lua_rawseti(L, -2, 0);
+       return 1;
+}
+
+static struct proxy *hlua_check_proxy(lua_State *L, int ud)
+{
+       return (struct proxy *)(hlua_checkudata(L, ud, class_proxy_ref));
+}
+
+int hlua_proxy_pause(lua_State *L)
+{
+       struct proxy *px;
+
+       px = hlua_check_proxy(L, 1);
+       pause_proxy(px);
+       return 0;
+}
+
+int hlua_proxy_resume(lua_State *L)
+{
+       struct proxy *px;
+
+       px = hlua_check_proxy(L, 1);
+       resume_proxy(px);
+       return 0;
+}
+
+int hlua_proxy_stop(lua_State *L)
+{
+       struct proxy *px;
+
+       px = hlua_check_proxy(L, 1);
+       stop_proxy(px);
+       return 0;
+}
+
+int hlua_proxy_get_cap(lua_State *L)
+{
+       struct proxy *px;
+       const char *str;
+
+       px = hlua_check_proxy(L, 1);
+       str = proxy_cap_str(px->cap);
+       lua_pushstring(L, str);
+       return 1;
+}
+
+int hlua_proxy_get_stats(lua_State *L)
+{
+       struct proxy *px;
+       int i;
+
+       px = hlua_check_proxy(L, 1);
+       if (px->cap & PR_CAP_BE)
+               stats_fill_be_stats(px, ST_SHLGNDS, stats, STATS_LEN);
+       else
+               stats_fill_fe_stats(px, stats, STATS_LEN);
+       lua_newtable(L);
+       for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
+               lua_pushstring(L, stat_field_names[i]);
+               hlua_fcn_pushfield(L, &stats[i]);
+               lua_settable(L, -3);
+       }
+       return 1;
+}
+
+int hlua_proxy_get_mode(lua_State *L)
+{
+       struct proxy *px;
+       const char *str;
+
+       px = hlua_check_proxy(L, 1);
+       str = proxy_mode_str(px->mode);
+       lua_pushstring(L, str);
+       return 1;
+}
+
+int hlua_proxy_shut_bcksess(lua_State *L)
+{
+       struct proxy *px;
+
+       px = hlua_check_proxy(L, 1);
+       srv_shutdown_backup_streams(px, SF_ERR_KILLED);
+       return 0;
+}
+
 int hlua_fcn_post_init(lua_State *L)
 {
+       struct proxy *px;
+
+       /* get core array. */
+       if (lua_getglobal(L, "core") != LUA_TTABLE)
+               lua_error(L);
+
+       /* Create proxies entry. */
+       lua_pushstring(L, "proxies");
+       lua_newtable(L);
+
+       /* List all proxies. */
+       for (px = proxy; px; px = px->next) {
+               lua_pushstring(L, px->id);
+               hlua_fcn_new_proxy(L, px);
+               lua_settable(L, -3);
+       }
+
+       /* push "proxies" in "core" */
+       lua_settable(L, -3);
+
        return 1;
 }
 
@@ -447,5 +567,19 @@ int hlua_fcn_reg_core_fcn(lua_State *L)
        hlua_class_function(L, "concat", hlua_concat_new);
        hlua_class_function(L, "get_info", hlua_get_info);
 
+       /* Create proxy object. */
+       lua_newtable(L);
+       lua_pushstring(L, "__index");
+       lua_newtable(L);
+       hlua_class_function(L, "pause", hlua_proxy_pause);
+       hlua_class_function(L, "resume", hlua_proxy_resume);
+       hlua_class_function(L, "stop", hlua_proxy_stop);
+       hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
+       hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
+       hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
+       hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
+       lua_settable(L, -3); /* -> META["__index"] = TABLE */
+       class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
+
        return 5;
 }