Search code examples
nginxluapopenopenresty

Strange io.popen behaviour in lua


I have lua block in nginx config, which checks for redis-server connection and if 4 java-processes are up, and then returns 200 or 500 status accordingly to this checks.

location = /healthcheck {

    content_by_lua_block {
        local redis = require "resty.redis"
        local red = redis:new()  
        red:set_timeout(1000)
        local ok, err = red:connect("127.0.0.1", 6379)
        if not ok then
            ngx.status = 500
            return
        end


        local ps = io.popen("/bin/ps aux |grep -c '[j]ava.*63'")
        local output = tostring(ps:read('*a'))
        ps:close()
        if string.match(output, '4') then
            ngx.status = 200
        else
            ngx.status = 500
        end
    }

}

But periodically output variable takes nil value, while it shouldn't. Just can't get why it happens.

Thanks in advance, comrades.

UPD:

Using tonumber fails on

bad argument #2 to 'tonumber' (number expected, got string)

Updated location config:

   location = /healthcheck {

        content_by_lua_block {
            local redis = require "resty.redis"
            local red = redis:new()
            red:set_timeout(1000)
            local ok, err = red:connect("127.0.0.1", 6379)
            if not ok then
                ngx.status = 500
                return
            end

            local ps = io.popen("/bin/ps aux |grep -c '[j]ava.*63'")
            local output = tonumber(ps:read())
            ps:close()
            if (output == 4) then
                ngx.status = 200
            else
                ngx.status = 500
            end
        }

    }

UPD2:

Logging to nginx error (with tostring) prints next:

grep: write error: Broken pipe
2016/04/19 17:54:48 [error] 301#0: *5 [lua] content_by_lua(healthcheck.conf:33):20: OUTPUT:nil

UPD3:

Using command grep -c '[j]ava.*63' <(/bin/ps aux) to avoid the use of pipe:

local ps = io.popen("grep -c '[j]ava.*63' <(/bin/ps aux)")

getting next error:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `grep -c '[j]ava.*63' <(/bin/ps aux)'

Solution

  • According to this answer and some testing next config do the job:

    location = /healthcheck {
    
        content_by_lua_block {
            local redis = require "resty.redis"
            local red = redis:new()
            red:set_timeout(1000)
            local ok, err = red:connect("127.0.0.1", 6379)
            if not ok then
                ngx.status = 500
                return
            end
    
            local ps = io.popen("/bin/ps aux >/tmp/ps.out", "w")
            ps:close()
            local ps = io.popen("grep -c '[j]ava.*63' /tmp/ps.out")
            local output = tostring(ps:read())
            ps:close()
            if string.match(output, '4') then
                ngx.status = 200
            else
                ngx.status = 500
            end
    
        }
    
    }