Last active
March 20, 2017 04:53
-
-
Save sitano/73ae43e0733cc8158823005422b1f4af to your computer and use it in GitHub Desktop.
openresty check upstreams status example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
location = /status { | |
default_type text/plain; | |
content_by_lua ' | |
local ok, upstream = pcall(require, "ngx.upstream") | |
if not ok then | |
error("ngx_upstream_lua module required") | |
end | |
local get_primary_peers = upstream.get_primary_peers | |
local get_backup_peers = upstream.get_backup_peers | |
local get_upstreams = upstream.get_upstreams | |
local function is_good(peers) | |
local npeers = #peers | |
for i = 1, npeers do | |
local peer = peers[i] | |
if not peer.down then | |
return true | |
end | |
end | |
return false | |
end | |
local us, err = get_upstreams() | |
if not us then | |
return "failed to get upstream names: " .. err | |
end | |
local n = #us | |
for i = 1, n do | |
local primary = get_primary_peers(us[i]) | |
local backup = get_backup_peers(us[i]) | |
if not is_good(primary) and not is_good(backup) then | |
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE | |
ngx.say("unavailable") | |
return | |
end | |
end | |
ngx.status = ngx.HTTP_OK | |
ngx.say("ok") | |
'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems that the line 18 statement "if not peer.down then" is forever true whatever the server is up or down because the down element is always nil .
I donnot know why?