Skip to content

Instantly share code, notes, and snippets.

@zahna
Last active August 8, 2017 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zahna/3450f3de83454fd266082bc009e87574 to your computer and use it in GitHub Desktop.
Save zahna/3450f3de83454fd266082bc009e87574 to your computer and use it in GitHub Desktop.
tengine (nginx) backend process healthcheck in lua
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