Skip to content

Instantly share code, notes, and snippets.

@rk295
Created October 8, 2018 13:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rk295/6213332bb6e653fbfaa3b724062ada13 to your computer and use it in GitHub Desktop.
Save rk295/6213332bb6e653fbfaa3b724062ada13 to your computer and use it in GitHub Desktop.
-- `core` is a static class provided by haproxy containing all
-- the haproxy methods we can use.
-- `register_init` registers a function to be executed after
-- configuration parsing.
core.register_init(function ()
core.log(core.info, "script loaded: case-200-ok")
end)
-- `register_service` registers a lua function to be executed
-- as a service. Once it's been properly registered, the service
-- can be referenced in the haproxy configuration as `lua.<svc_name>`.
--
-- Depending on the mode (tcp or http), the applet is either an
-- AppletHTTP or AppletTCP class instance.
core.register_service("status_service", "http", function (applet)
local backend_name = "simple-ad"
local r = ""
backend = core.proxies[backend_name]
servers = backend["servers"]
local any_up = false
for k, v in pairs(servers) do
status = v.get_stats(v)["status"]
-- If _any_ of the servers are up, we will return OK
if (status == "UP") then
any_up = true
end
end
if ( any_up ) then
core.log(core.info, "Found some servers up")
applet:set_status(200)
r = "OK"
else
core.log(core.info, "Found NO servers up")
applet:set_status(500)
r = "FAILED"
end
applet:start_response()
applet:send(r)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment