Aurelien DARRAGON | 620382a | 2023-03-17 19:50:35 +0100 | [diff] [blame] | 1 | -- haproxy event-handling from Lua |
| 2 | -- |
| 3 | -- This file serves as a demo to show you the various events that |
| 4 | -- can be handled directly from custom lua functions. |
| 5 | -- Events captured from lua will be printed directly to STDOUT |
| 6 | -- It may not be exhaustive, please refer to the lua documentation |
| 7 | -- in doc/lua-api/index.rst for up-to-date content and further explanations |
| 8 | |
| 9 | -- subscribe to every SERVER family events, this is the equivalent of doing: |
| 10 | -- core.event_sub({"SERVER_ADD", "SERVER_DEL", "SERVER_UP", "SERVER_DOWN"}, ...) |
| 11 | core.event_sub({"SERVER"}, function(event, data) |
| 12 | -- This function will be called when: |
| 13 | -- - new server is added from the CLI (SERVER_ADD) |
| 14 | -- - existing server is removed from the CLI (SERVER_DEL) |
| 15 | -- - existing server state changes from UP to DOWN (SERVER_DOWN) |
| 16 | -- - existing server state changes from DOWN to UP (SERVER_UP) |
| 17 | -- If the server still exists at the time the function is called, data["reference"] |
| 18 | -- contains a valid reference to the lua server object related to the event |
| 19 | -- |
| 20 | sv_status = data["reference"] ~= nil and data["reference"]:get_stats().status or "DELETED" |
| 21 | print("[DEBUG - FROM LUA]", "EventType." .. event .. ": " .. |
| 22 | "server " .. data["proxy_name"] .. "/" .. data["name"] .. " " .. |
| 23 | "is " .. sv_status) |
| 24 | end) |
| 25 | -- Please note that you may also use Server.event_sub() method to subscribe to events |
| 26 | -- relative to a specific server only. See the lua documentation for more information. |
| 27 | |
| 28 | -- New event families will be added over time... |