From f61aa6356eb8928894d75858087bef5b3c2a0750 Mon Sep 17 00:00:00 2001 From: Thierry Fournier Date: Fri, 19 Feb 2016 20:56:00 +0100 Subject: [PATCH] MINOR: lua: add class proxy This class provides the access to the proxy struct, it allows some manipulations and retrieve informations. --- doc/lua-api/index.rst | 73 +++++++++++++++++++++++ include/types/hlua.h | 1 + src/hlua_fcn.c | 136 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 209 insertions(+), 1 deletion(-) diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index fa16519b6..55d140a5a 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -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 diff --git a/include/types/hlua.h b/include/types/hlua.h index 34bd648a9..17cf141b8 100644 --- a/include/types/hlua.h +++ b/include/types/hlua.h @@ -19,6 +19,7 @@ #define CLASS_MAP "Map" #define CLASS_APPLET_TCP "AppletTCP" #define CLASS_APPLET_HTTP "AppletHTTP" +#define CLASS_PROXY "Proxy" struct stream; diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c index f577be4a3..c8b640f22 100644 --- a/src/hlua_fcn.c +++ b/src/hlua_fcn.c @@ -19,16 +19,21 @@ #include #include +#include #include +#include #include #include +#include +#include /* 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; imode); + 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; }