]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: add class listener
authorThierry Fournier <tfournier@arpalert.org>
Thu, 25 Feb 2016 07:36:46 +0000 (08:36 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 30 Mar 2016 16:43:47 +0000 (18:43 +0200)
This class provides the access to the listener struct, it allows
some manipulations and retrieve informations.

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

index 07ade69152631f0ec530b2a50d98d8eb95f69b06..2bca4d07f512ea440f245b7180f362b5b9e0affc 100644 (file)
@@ -597,6 +597,11 @@ Proxy class
   Contain an array with the attached servers. Each server entry is an object of
   type :ref:`server_class`.
 
+.. js:attribute:: Proxy.listeners
+
+  Contain an array with the attached listeners. Each listeners entry is an
+  object of type :ref:`listener_class`.
+
 .. js:function:: Proxy.pause(px)
 
   Pause the proxy. See the management socket documentation for more information.
@@ -811,6 +816,19 @@ Server class
   :param class_server sv: A :ref:`server_class` which indicates the manipulated
     server.
 
+.. _listener_class:
+
+Listener class
+==============
+
+.. js:function:: Listener.get_stats(ls)
+
+  Returns server statistics.
+
+  :param class_listener ls: A :ref:`listener_class` which indicates the
+    manipulated listener.
+  :returns: a key/value array containing stats
+
 .. _concat_class:
 
 Concat class
index afe870a3713d1dfd949b3c2bbfbddfbeebf3026a..43e9f1a6cd22b5039e9a92d40288a4c7cf28d949 100644 (file)
@@ -21,6 +21,7 @@
 #define CLASS_APPLET_HTTP  "AppletHTTP"
 #define CLASS_PROXY        "Proxy"
 #define CLASS_SERVER       "Server"
+#define CLASS_LISTENER     "Listener"
 
 struct stream;
 
index 0e5a33d37e00bd9c99cb4ed026960d3027c01e1b..2ee3393559fae6756a5828175cc2e861e457692c 100644 (file)
@@ -33,6 +33,7 @@
 static int class_concat_ref;
 static int class_proxy_ref;
 static int class_server_ref;
+static int class_listener_ref;
 
 #define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
 
@@ -435,6 +436,48 @@ static int hlua_concat_init(lua_State *L)
        return 1;
 }
 
+int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
+{
+       lua_newtable(L);
+
+       /* Pop a class sesison metatable and affect it to the userdata. */
+       lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
+       lua_setmetatable(L, -2);
+
+       lua_pushlightuserdata(L, lst);
+       lua_rawseti(L, -2, 0);
+       return 1;
+}
+
+static struct listener *hlua_check_listener(lua_State *L, int ud)
+{
+       return (struct listener *)(hlua_checkudata(L, ud, class_listener_ref));
+}
+
+int hlua_listener_get_stats(lua_State *L)
+{
+       struct listener *li;
+       int i;
+
+       li = hlua_check_listener(L, 1);
+
+       if (!li->frontend) {
+               lua_pushnil(L);
+               return 1;
+       }
+
+       stats_fill_li_stats(li->frontend, li, ST_SHLGNDS, 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_fcn_new_server(lua_State *L, struct server *srv)
 {
        lua_newtable(L);
@@ -713,6 +756,9 @@ int hlua_server_agent_force_down(lua_State *L)
 int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
 {
        struct server *srv;
+       struct listener *lst;
+       int lid;
+       char buffer[10];
 
        lua_newtable(L);
 
@@ -733,6 +779,23 @@ int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
        }
        lua_settable(L, -3);
 
+       /* Browse and register listeners. */
+       lua_pushstring(L, "listeners");
+       lua_newtable(L);
+       lid = 1;
+       list_for_each_entry(lst, &px->conf.listeners, by_fe) {
+               if (lst->name)
+                       lua_pushstring(L, lst->name);
+               else {
+                       snprintf(buffer, 10, "sock-%d", lid);
+                       lid++;
+                       lua_pushstring(L, buffer);
+               }
+               hlua_fcn_new_listener(L, lst);
+               lua_settable(L, -3);
+       }
+       lua_settable(L, -3);
+
        return 1;
 }
 
@@ -856,6 +919,14 @@ 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 listener object. */
+       lua_newtable(L);
+       lua_pushstring(L, "__index");
+       lua_newtable(L);
+       hlua_class_function(L, "get_stats", hlua_listener_get_stats);
+       lua_settable(L, -3); /* -> META["__index"] = TABLE */
+       class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
+
        /* Create server object. */
        lua_newtable(L);
        lua_pushstring(L, "__index");