Skip to content

Commit

Permalink
Merge pull request #1 from dmyerscough/master
Browse files Browse the repository at this point in the history
Damian Myerscough contribution
  • Loading branch information
ciceroverneck committed Oct 26, 2015
2 parents dafbdb4 + 132314c commit 7083733
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Watch live nginx stats from your servers
## Installation

1. Install [nginx](http://nginx.org/) configured with [--add-module=/path/to/lua-nginx-module](https://github.com/chaoslawful/lua-nginx-module)
2. Compile [lua CJSON](http://www.kyne.com.au/~mark/software/lua-cjson.php) and copy cjson.so to path specified by ```lua_package_cpath``` policy
3. Clone [ngx_stats](https://github.com/StudioSol/ngx_stats) in path specified by ```lua_package_path``` policy
2. Clone [ngx_stats](https://github.com/StudioSol/ngx_stats) in path specified by ```lua_package_path``` policy

```sh
cd /etc/nginx/lua/
Expand Down Expand Up @@ -145,6 +144,5 @@ http {
## See Also

- [lua-nginx-module](https://github.com/chaoslawful/lua-nginx-module) Embed the Power of Lua into NginX
- [lua CJSON](http://www.kyne.com.au/~mark/software/lua-cjson.php) Lua CJSON provides JSON support for Lua
- [LuaJIT](http://luajit.org/) a Just-In-Time Compiler for Lua
- [nginx](http://nginx.org/) HTTP and reverse proxy server
7 changes: 6 additions & 1 deletion common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function _M.in_table ( e, t )
end



function _M.format_response(key, value, response)
local path = split(tostring(key), ':')
key = table.remove(path, 1)
Expand Down Expand Up @@ -74,4 +73,10 @@ function _M.get_status_code_class(status)
end
end


function _M.update(stats, key, value)
stats:set(key, value)
end


return _M
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cjson = require "cjson"
cjson = require "json"
common = require "stats.common"
cache_status = {"MISS", "BYPASS", "EXPIRED", "STALE", "UPDATING", "REVALIDATED", "HIT"}
local stats = ngx.shared.ngx_stats;
Expand Down
45 changes: 43 additions & 2 deletions log.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ngx.update_time()
local stats = ngx.shared.ngx_stats;
local group = ngx.var.stats_group
local req_time = ngx.now() - ngx.req.start_time()
local req_time = (tonumber(ngx.now() - ngx.req.start_time()) * 1000)
local status = tostring(ngx.status)

-- Geral stats
Expand All @@ -14,7 +14,16 @@ end


common.incr_or_create(stats, common.key({group, 'requests_total'}), 1)
common.incr_or_create(stats, common.key({group, 'request_time', 'sum'}), req_time)

if req_time >= 0 and req_time < 100 then
common.incr_or_create(stats, common.key({group, 'request_times', '0-100'}), 1)
elseif req_time >= 100 and req_time < 500 then
common.incr_or_create(stats, common.key({group, 'request_times', '100-500'}), 1)
elseif req_time >= 500 and req_time < 1000 then
common.incr_or_create(stats, common.key({group, 'request_times', '500-1000'}), 1)
elseif req_time >= 1000 then
common.incr_or_create(stats, common.key({group, 'request_times', '1000-inf'}), 1)
end

if upstream_response_time then
common.incr_or_create(stats, common.key({group, 'upstream_requests_total'}), 1)
Expand All @@ -28,3 +37,35 @@ if common.in_table(ngx.var.upstream_cache_status, cache_status) then
end

common.incr_or_create(stats, common.key({group, 'status', common.get_status_code_class(status)}), 1)

-- Traffic being sent to and from the client
common.update(stats, common.key({group, 'traffic', 'received'}), ngx.var.request_length)
common.update(stats, common.key({group, 'traffic', 'sent'}), ngx.var.bytes_sent)


--[[ Connection statistics
Active connections
==================
The current number of active client connections including Waiting connections.
Reading connections
===================
The current number of connections where nginx is reading the request header.
Waiting connections
===================
The current number of idle client connections waiting for a request.
Writing connections
===================
The current number of connections where nginx is writing the response back to the client.
]]--
common.update(stats, common.key({'connections', 'active'}), ngx.var.connections_active)
common.update(stats, common.key({'connections', 'idle'}), ngx.var.connections_waiting)
common.update(stats, common.key({'connections', 'reading'}), ngx.var.connections_reading)
common.update(stats, common.key({'connections', 'writing'}), ngx.var.connections_writing)

common.update(stats, common.key({'requests', 'current'}), ngx.var.connection_requests)
common.incr_or_create(stats, common.key({'requests', 'total'}), 1)
2 changes: 1 addition & 1 deletion show.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ for k,v in pairs(keys) do
response = common.format_response(v, stats:get(v), response)
end

ngx.say(cjson.encode(response))
ngx.say(json.encode(response))

0 comments on commit 7083733

Please sign in to comment.