The previous article introduced the configuration of nginx cache [ using nginx cache to accelerate H5 loading speed ], and the statistics of cache hit rate by analyzing access logs, because the author is using openresty, and later thought that it is better to use ngx.shared.DICT to use lua The real-time statistics of scripts are more convenient, and zabbix is used to collect and integrate them.
36.4.49.117 - [31/Jan/2018:13:17:14 +0800] "GET /page/140019?code=nLfrSW&time=1517338888000
HTTP/1.1" 200 9848 "https://xxxxxx"
"Mozilla/5.0 (Linux; Android 5.0.2; vivo Y33 Build/LRX21M; wv) AppleWebKit/537.36
(KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2
TBS/043906 Mobile Safari/537.36 MicroMessenger/6.6.1.1220(0x26060135) NetType/WIFI Language/zh_CN" "36.4.49.117" "-" "-" "-" "0.001" "HIT"
The use of access log statistics is too low, not real-time, and slow. Of course, it has the advantage of offline, without introducing other modules.
Gossip resting, let's get to the point.
openresty configuration
The lua script is as follows:
local cache = ngx.shared.cache_stat
local upstream_cache_status = ngx.var.upstream_cache_status;
local newval, err = cache:incr(upstream_cache_status,1)
if not newval and err == "not found" then
cache:add(upstream_cache_status,1)
end
local total = "TOTAL"
local newval1, err1 = cache:incr(total,1)
if not newval1 and err1 == "not found" then
cache:add(total, 1)
end
cache:incr is an atomic operation and will not be lost. It is picked up in the openresty configuration file:
#Use 1m memory (a bit of a waste, only a few metrics, can be changed to 10k)
lua_shared_dict cache_stat 1m;server {
......location /page {
proxy_cache page_cache;
proxy_cache_key $scheme$uri?code=$arg_code&time=$arg_time;
proxy_cache_valid 200 5d;
log_by_lua 'local cache = ngx.shared.cache_stat
local upstream_cache_status = ngx.var.upstream_cache_status;
local newval, err = cache:incr(upstream_cache_status,1)
if not newval and err == "not found" then
cache:add(upstream_cache_status,1)
end
local total = "TOTAL"
local newval1, err1 = cache:incr(total,1)
if not newval1 and err1 == "not found" then
cache:add(total, 1)
end
'
}
}
Of course, the script can also be placed in a separate file, and use log_by_lua_file to specify the file path, which is more concise.
Write a location, open it to the outside world, to collect statistics in real time:
location /cache-status {
allow 127.0.0.1;
deny all;
default_type 'text/plain' ;
content_by_lua '
local cache = ngx.shared.cache_stat
local keys = cache:get_keys()
for idx, key in pairs(keys) do
ngx.say(key .. " " .. cache:get(key))
end
local hit = cache:get("HIT")
local total = cache:get("TOTAL")
ngx.say("RATIO ".. string.format("%.2f", hit * 100/total))
';}
A test is to take a look:
Zabbix monitoring collection configuration
Create a new script ngx_cache_stat.sh in the scripts directory of the zabbix agent of this openresty machine. The content looks like:
#!/bin/bash
HOST=127.0.0.1
PORT=80
# Functions to return cache stats
function hit {
/usr/bin/curl "http://$HOST:$PORT/cache-status" 2>/dev/null| grep 'HIT' | awk '{print $2}'
}
function miss {
/usr/bin/curl "http://$HOST:$PORT/cache-status" 2>/dev/null| grep 'MISS' | awk '{print $2}'
}
function expired {
/usr/bin/curl "http://$HOST:$PORT/cache-status" 2>/dev/null| grep 'EXPIRED' | awk '{print $2}'
}
function updating {
/usr/bin/curl "http://$HOST:$PORT/cache-status" 2>/dev/null| grep 'UPDATING' | awk '{print $2}'
}
function stale {
/usr/bin/curl "http://$HOST:$PORT/cache-status" 2>/dev/null|grep 'STALE' | awk '{print $2}'
}
function bypass {
/usr/bin/curl "http://$HOST:$PORT/cache-status" 2>/dev/null|grep 'BYPASS' | awk '{print $2}'
}
function ratio {
/usr/bin/curl "http://$HOST:$PORT/cache-status" 2>/dev/null|grep 'RATIO' | awk '{print $2}'
}
$1
upstream_cache_status contains the following status:
- MISS misses, the request is passed to the backend
- HIT cache hit
- EXPIRED cache has expired The request was sent to the backend
- UPDATING is updating the cache, will use the old answer
- STALE will get expired responses
- ·BYPASS penetrates the cache and enters the backend
Under test:
sh ngx_cache_stat.sh hit
If there is digital content input, it means ok.
Create a new file ngx_cache_stat.conf in etc/zabbix_agentd.conf.d/ of Zabbix Agent HOME, the content is as follows
# ngx_cache_stat.conf
Timeout=10
UnsafeUserParameters=1
UserParameter=nginx.cache.status[*],/usr/local/zabbix/script/ngx_cache_stat.sh $1
restart zabbix-agentd,
killall zabbix_agentd
zabbix server execution: zabbix_get test
./zabbix_get -s 10.0.x.x -k nginx.cache.status[ratio]
It's ok to get the correct value.
Get the zabbix template from github and import it into zabbix,
After the collection starts, the monitoring information is as follows, and the cache hit rate is about 95%.
The following monitoring is the incremental value of HIT, MISS, EXPIRED (30 seconds)
All scripts and templates are on github:
https://github.com/Jijun/zabbix-template-ngx-cache-lua.git
Invite the author for coffee: