2016-04-18 13 views
0

Ich habe lua Block in nginx config, die für Redis-Server-Verbindung überprüft und wenn 4 Java-Prozesse sind, und gibt dann 200 oder 500 Status entsprechend dieser überprüft zurück.Strange io.popen Verhalten in lua

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 
    } 

} 

Aber periodisch output Variable nimmt nil Wert, während es nicht sollte. Kann einfach nicht verstehen, warum es passiert.

Vielen Dank im Voraus, Kameraden.

UPD:

tonumber fehl am

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

aktualisiert Standort 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:

Protokollierung nginx Fehler (tostring) druckt weiter:

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:

Befehl Mit grep -c '[j]ava.*63' <(/bin/ps aux) die Verwendung von Rohr zu vermeiden:

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

bekommt nächste Fehler :

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

ist es null oder „Null“? – Piglet

+0

Ich habe es getestet, indem ich Variablen in das nginx-Protokoll geschrieben habe, und es gab keine doppelten Anführungszeichen, soweit ich mich erinnere. –

+0

gibt es die Zeichenfolge Null oder den Wert Null zurück? warum verwendest du trotzdem tostring? read gibt bereits eine Zeichenfolge zurück – Piglet

Antwort

0

Nach this Antwort und einige Tests nächsten Config den 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 

    } 

}