Last active
August 8, 2017 13:05
-
-
Save zahna/3450f3de83454fd266082bc009e87574 to your computer and use it in GitHub Desktop.
tengine (nginx) backend process healthcheck in lua
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 /health.pgrep { | |
default_type text/plain; | |
access_log off; | |
content_by_lua ' | |
local io = require "io" | |
if (ngx.var.arg_q ~= nil and ngx.var.arg_q ~= "") then | |
-- Note: Testing this over 127.0.0.1 will always succeed. | |
local arg_q = ngx.var.arg_q:gsub("[;&|/\\\\ ]", "") | |
local c = assert(io.popen("/usr/bin/pgrep -f "..arg_q)) | |
local p = tonumber(c:read()) | |
c:close() | |
if (p == nil) then | |
ngx.status = 503 | |
ngx.say(arg_q.." is not found") | |
ngx.exit(503) | |
else | |
ngx.say(arg_q.." is found at pid "..p) | |
end | |
else | |
ngx.status = 503 | |
ngx.say("query term is empty or missing") | |
ngx.exit(503) | |
end | |
'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment