2022-07-18 13:54:10 -04:00
|
|
|
log_by_lua_block {
|
|
|
|
|
|
2022-07-18 23:59:11 -04:00
|
|
|
local cpath = "{$SERVER_APP}/lua/"
|
|
|
|
|
if not package.cpath:find(cpath) then
|
|
|
|
|
package.cpath = cpath .. "?.so;" .. package.cpath
|
|
|
|
|
end
|
|
|
|
|
if not package.path:find(cpath) then
|
|
|
|
|
package.path = cpath .. "?.lua;" .. package.path
|
|
|
|
|
end
|
|
|
|
|
|
2023-02-11 11:22:39 -05:00
|
|
|
local ver = '0.2.4'
|
2022-10-16 12:44:04 -04:00
|
|
|
local debug_mode = true
|
|
|
|
|
|
|
|
|
|
local __C = require "webstats_common"
|
|
|
|
|
local C = __C:getInstance()
|
2022-07-18 13:54:10 -04:00
|
|
|
|
2022-07-19 02:18:37 -04:00
|
|
|
-- cache start ---
|
|
|
|
|
local cache = ngx.shared.mw_total
|
2026-07-02 00:52:05 -04:00
|
|
|
local function cache_set(server_name, id, key, val)
|
|
|
|
|
local line_kv = "log_kv_" .. server_name .. '_' .. id .. "_" .. key
|
2022-07-19 02:18:37 -04:00
|
|
|
cache:set(line_kv, val)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function cache_clear(server_name, id, key)
|
|
|
|
|
local line_kv = "log_kv_"..server_name..'_'..id.."_"..key
|
|
|
|
|
cache:delete(line_kv)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function cache_get(server_name, id, key)
|
|
|
|
|
local line_kv = "log_kv_"..server_name..'_'..id.."_"..key
|
|
|
|
|
local value = cache:get(line_kv)
|
|
|
|
|
return value
|
|
|
|
|
end
|
2022-10-16 12:44:04 -04:00
|
|
|
|
|
|
|
|
|
2022-07-19 02:18:37 -04:00
|
|
|
-- cache end ---
|
|
|
|
|
|
|
|
|
|
-- domain config is import
|
|
|
|
|
local db = nil
|
2022-07-19 11:02:05 -04:00
|
|
|
local json = require "cjson"
|
|
|
|
|
local sqlite3 = require "lsqlite3"
|
2022-10-16 12:44:04 -04:00
|
|
|
local config = require "webstats_config"
|
|
|
|
|
local sites = require "webstats_sites"
|
|
|
|
|
|
2022-11-30 23:33:09 -05:00
|
|
|
-- string.gsub(C:get_sn(ngx.var.server_name),'_','.')
|
2023-02-11 08:42:57 -05:00
|
|
|
local server_name = C:get_sn(ngx.var.server_name)
|
2022-11-30 23:33:09 -05:00
|
|
|
|
2022-10-17 09:12:42 -04:00
|
|
|
|
2022-10-16 12:44:04 -04:00
|
|
|
C:setConfData(config, sites)
|
2022-07-19 11:27:00 -04:00
|
|
|
|
2022-10-17 09:12:42 -04:00
|
|
|
local auto_config = C:setInputSn(server_name)
|
|
|
|
|
|
2022-10-17 11:29:57 -04:00
|
|
|
local request_header = ngx.req.get_headers()
|
|
|
|
|
local method = ngx.req.get_method()
|
|
|
|
|
local excluded = false
|
|
|
|
|
|
2022-10-17 09:12:42 -04:00
|
|
|
local day = os.date("%d")
|
|
|
|
|
local number_day = tonumber(day)
|
|
|
|
|
local day_column = "day"..number_day
|
|
|
|
|
local flow_column = "flow"..number_day
|
|
|
|
|
local spider_column = "spider_flow"..number_day
|
2022-07-19 11:02:05 -04:00
|
|
|
--- default common var end ---
|
|
|
|
|
|
2022-07-19 11:27:00 -04:00
|
|
|
local function init_var()
|
2022-07-20 11:49:09 -04:00
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-21 06:13:32 -04:00
|
|
|
--------------------- exclude_func start --------------------------
|
|
|
|
|
local function load_global_exclude_ip()
|
|
|
|
|
local load_key = "global_exclude_ip_load"
|
|
|
|
|
-- update global exclude ip
|
2022-07-26 11:06:24 -04:00
|
|
|
local global_exclude_ip = auto_config["exclude_ip"]
|
2022-07-21 06:13:32 -04:00
|
|
|
if global_exclude_ip then
|
|
|
|
|
for i, _ip in pairs(global_exclude_ip)
|
|
|
|
|
do
|
|
|
|
|
-- global
|
2022-07-21 23:56:28 -04:00
|
|
|
-- D("set global exclude ip: ".._ip)
|
2022-07-21 06:13:32 -04:00
|
|
|
if not cache:get("global_exclude_ip_".._ip) then
|
|
|
|
|
cache:set("global_exclude_ip_".._ip, true)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
-- set tag
|
|
|
|
|
cache:set(load_key, true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function load_exclude_ip(input_server_name)
|
|
|
|
|
|
|
|
|
|
local load_key = input_server_name .. "_exclude_ip_load"
|
|
|
|
|
local site_config = config[input_server_name]
|
|
|
|
|
|
|
|
|
|
local site_exclude_ip = nil
|
|
|
|
|
if site_config then
|
|
|
|
|
site_exclude_ip = site_config["exclude_ip"]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- update server_name exclude ip
|
|
|
|
|
if site_exclude_ip then
|
|
|
|
|
for i, _ip in pairs(site_exclude_ip)
|
|
|
|
|
do
|
|
|
|
|
cache:set(input_server_name .. "_exclude_ip_".._ip, true)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- set tag
|
|
|
|
|
cache:set(load_key, true)
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function filter_status()
|
2022-07-26 11:06:24 -04:00
|
|
|
if not auto_config['exclude_status'] then return false end
|
2022-07-20 12:41:02 -04:00
|
|
|
local the_status = tostring(ngx.status)
|
2022-07-26 11:06:24 -04:00
|
|
|
for _,v in ipairs(auto_config['exclude_status'])
|
2022-07-20 12:41:02 -04:00
|
|
|
do
|
|
|
|
|
if the_status == v then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function exclude_extension()
|
2026-07-02 00:52:05 -04:00
|
|
|
local uri = ngx.var.uri
|
|
|
|
|
if not uri then return false end
|
|
|
|
|
|
|
|
|
|
local exclude_ext = auto_config['exclude_extension']
|
|
|
|
|
if not exclude_ext then return false end
|
|
|
|
|
|
|
|
|
|
local lower_uri = string.lower(uri)
|
|
|
|
|
for _, ext in ipairs(exclude_ext) do
|
|
|
|
|
if string.find(lower_uri, "." .. ext .. "$", 1, true) then
|
2022-07-20 12:41:02 -04:00
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function exclude_url()
|
2026-07-02 00:52:05 -04:00
|
|
|
local request_uri = ngx.var.request_uri
|
|
|
|
|
if not request_uri then return false end
|
|
|
|
|
|
|
|
|
|
local url_conf = auto_config['exclude_url']
|
|
|
|
|
if not url_conf then return false end
|
|
|
|
|
|
|
|
|
|
local the_uri = string.sub(request_uri, 2)
|
|
|
|
|
for _, conf in ipairs(url_conf) do
|
2022-07-20 12:41:02 -04:00
|
|
|
local mode = conf["mode"]
|
|
|
|
|
local url = conf["url"]
|
|
|
|
|
if mode == "regular" then
|
|
|
|
|
if ngx.re.find(the_uri, url, "ijo") then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
if the_uri == url then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return false
|
|
|
|
|
end
|
2022-07-21 06:13:32 -04:00
|
|
|
|
|
|
|
|
local function exclude_ip(input_server_name, ip)
|
|
|
|
|
local site_config = config[input_server_name]
|
|
|
|
|
if site_config then
|
2026-07-02 00:52:05 -04:00
|
|
|
local server_exclude_ips = site_config["exclude_ip"]
|
|
|
|
|
if server_exclude_ips then
|
|
|
|
|
for _, _ip in pairs(server_exclude_ips) do
|
|
|
|
|
if cache:get(input_server_name .. "_exclude_ip_" .. ip) then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
break
|
|
|
|
|
end
|
2022-07-21 06:13:32 -04:00
|
|
|
end
|
|
|
|
|
end
|
2026-07-02 00:52:05 -04:00
|
|
|
|
|
|
|
|
return cache:get("global_exclude_ip_" .. ip) ~= nil
|
2022-07-21 06:13:32 -04:00
|
|
|
end
|
|
|
|
|
--------------------- exclude_func end ---------------------------
|
|
|
|
|
|
2026-07-02 00:52:05 -04:00
|
|
|
local status_codes_to_log = {
|
|
|
|
|
["400"] = true, ["401"] = true, ["402"] = true, ["403"] = true, ["404"] = true,
|
|
|
|
|
["405"] = true, ["406"] = true, ["407"] = true, ["408"] = true, ["409"] = true,
|
|
|
|
|
["410"] = true, ["411"] = true, ["412"] = true, ["413"] = true, ["414"] = true,
|
|
|
|
|
["415"] = true, ["416"] = true, ["417"] = true, ["418"] = true, ["421"] = true,
|
|
|
|
|
["422"] = true, ["423"] = true, ["424"] = true, ["425"] = true, ["426"] = true,
|
|
|
|
|
["449"] = true, ["451"] = true, ["499"] = true, ["500"] = true, ["501"] = true,
|
|
|
|
|
["502"] = true, ["503"] = true, ["504"] = true, ["505"] = true, ["506"] = true,
|
|
|
|
|
["507"] = true, ["509"] = true, ["510"] = true
|
|
|
|
|
}
|
2022-10-16 12:44:04 -04:00
|
|
|
|
2026-07-02 00:52:05 -04:00
|
|
|
local http_methods = {["get"] = true, ["post"] = true, ["put"] = true, ["patch"] = true, ["delete"] = true}
|
2022-10-18 04:38:18 -04:00
|
|
|
|
2026-07-02 00:52:05 -04:00
|
|
|
local cache = ngx.shared.mw_total
|
|
|
|
|
local total_key = "log_kv_total"
|
2022-10-18 04:38:18 -04:00
|
|
|
|
2026-07-02 00:52:05 -04:00
|
|
|
local function cache_logs(input_sn)
|
2022-10-18 04:38:18 -04:00
|
|
|
local new_id = C:get_last_id(input_sn)
|
|
|
|
|
local ip = C:get_client_ip()
|
2026-07-02 00:52:05 -04:00
|
|
|
local excluded = filter_status() or exclude_extension() or exclude_url() or exclude_ip(input_sn, ip)
|
2022-10-18 04:38:18 -04:00
|
|
|
|
|
|
|
|
local ip_list = request_header["x-forwarded-for"]
|
|
|
|
|
if ip and not ip_list then
|
|
|
|
|
ip_list = ip
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local remote_addr = ngx.var.remote_addr
|
2023-11-17 13:36:18 -05:00
|
|
|
if "table" == type(ip_list) then
|
2026-07-02 00:52:05 -04:00
|
|
|
ip_list = json.encode(ip_list)
|
|
|
|
|
end
|
2023-11-17 13:36:18 -05:00
|
|
|
|
2026-07-02 00:52:05 -04:00
|
|
|
if remote_addr and not string.find(ip_list, remote_addr, 1, true) then
|
|
|
|
|
ip_list = ip_list .. "," .. remote_addr
|
2022-10-18 04:38:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local request_time = C:get_request_time()
|
|
|
|
|
local client_port = ngx.var.remote_port
|
2022-10-23 23:10:23 -04:00
|
|
|
local uri = tostring(ngx.var.uri)
|
2022-10-18 04:38:18 -04:00
|
|
|
local status_code = ngx.status
|
|
|
|
|
local protocol = ngx.var.server_protocol
|
|
|
|
|
local request_uri = ngx.var.request_uri
|
|
|
|
|
local body_length = C:get_length()
|
|
|
|
|
local domain = C:get_domain()
|
|
|
|
|
local referer = ngx.var.http_referer
|
2026-07-02 00:52:05 -04:00
|
|
|
local user_agent = request_header['user-agent']
|
|
|
|
|
local now_time = ngx.time()
|
2022-10-18 04:38:18 -04:00
|
|
|
|
|
|
|
|
local kv = {
|
2026-07-02 00:52:05 -04:00
|
|
|
id = new_id,
|
|
|
|
|
time_key = os.date("%Y%m%d%H", now_time),
|
|
|
|
|
time = now_time,
|
|
|
|
|
ip = ip,
|
|
|
|
|
domain = domain,
|
|
|
|
|
server_name = input_sn,
|
|
|
|
|
real_server_name = input_sn,
|
|
|
|
|
method = method,
|
|
|
|
|
status_code = status_code,
|
|
|
|
|
uri = uri,
|
|
|
|
|
request_uri = request_uri,
|
|
|
|
|
body_length = body_length,
|
|
|
|
|
referer = referer,
|
|
|
|
|
user_agent = user_agent,
|
|
|
|
|
protocol = protocol,
|
|
|
|
|
is_spider = 0,
|
|
|
|
|
request_time = request_time,
|
|
|
|
|
excluded = excluded,
|
|
|
|
|
request_headers = '',
|
|
|
|
|
ip_list = ip_list,
|
|
|
|
|
client_port = client_port
|
2022-10-18 04:38:18 -04:00
|
|
|
}
|
|
|
|
|
|
2026-07-02 00:52:05 -04:00
|
|
|
local request_stat_fields = {req = 1, length = body_length}
|
2022-10-18 04:38:18 -04:00
|
|
|
local spider_stat_fields = {}
|
|
|
|
|
local client_stat_fields = {}
|
|
|
|
|
|
|
|
|
|
if not excluded then
|
2026-07-02 00:52:05 -04:00
|
|
|
if status_code == 500 or (method == "POST" and config['global']["record_post_args"] == true) or (status_code == 403 and config['global']["record_get_403_args"] == true) then
|
|
|
|
|
local ok, data = pcall(function() return C:get_http_origin() end)
|
|
|
|
|
if ok and data then
|
2022-10-18 04:38:18 -04:00
|
|
|
kv["request_headers"] = data
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2026-07-02 00:52:05 -04:00
|
|
|
if status_codes_to_log[tostring(status_code)] then
|
|
|
|
|
request_stat_fields["status_" .. status_code] = 1
|
2022-10-18 04:38:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local lower_method = string.lower(method)
|
2026-07-02 00:52:05 -04:00
|
|
|
if http_methods[lower_method] then
|
|
|
|
|
request_stat_fields["http_" .. lower_method] = 1
|
2022-10-18 04:38:18 -04:00
|
|
|
end
|
|
|
|
|
|
2026-07-02 00:52:05 -04:00
|
|
|
local is_spider, request_spider, spider_index = C:match_spider(user_agent)
|
|
|
|
|
if not is_spider then
|
|
|
|
|
client_stat_fields = C:match_client_arr(user_agent)
|
|
|
|
|
local pvc, uvc = C:statistics_request(ip, is_spider, body_length)
|
|
|
|
|
local ipc = C:statistics_ipc(input_sn, ip)
|
|
|
|
|
|
|
|
|
|
if ipc > 0 then request_stat_fields["ip"] = 1 end
|
|
|
|
|
if uvc > 0 then request_stat_fields["uv"] = 1 end
|
|
|
|
|
if pvc > 0 then request_stat_fields["pv"] = 1 end
|
2022-10-18 04:38:18 -04:00
|
|
|
else
|
|
|
|
|
kv["is_spider"] = spider_index
|
|
|
|
|
spider_stat_fields[request_spider] = 1
|
2026-07-02 00:52:05 -04:00
|
|
|
request_stat_fields["spider"] = 1
|
2022-10-18 04:38:18 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-10-17 12:56:31 -04:00
|
|
|
local data = {
|
2026-07-02 00:52:05 -04:00
|
|
|
server_name = input_sn,
|
|
|
|
|
stat_fields = {
|
|
|
|
|
request_stat_fields = request_stat_fields,
|
|
|
|
|
client_stat_fields = client_stat_fields,
|
|
|
|
|
spider_stat_fields = spider_stat_fields,
|
|
|
|
|
},
|
|
|
|
|
log_kv = kv,
|
2022-10-17 12:56:31 -04:00
|
|
|
}
|
2022-10-16 12:44:04 -04:00
|
|
|
|
2026-07-02 00:52:05 -04:00
|
|
|
cache:rpush(total_key, json.encode(data))
|
2022-07-19 11:02:05 -04:00
|
|
|
end
|
2022-07-19 02:18:37 -04:00
|
|
|
|
2022-07-18 13:54:10 -04:00
|
|
|
local function run_app()
|
2022-07-19 11:27:00 -04:00
|
|
|
init_var()
|
2022-07-21 06:13:32 -04:00
|
|
|
load_global_exclude_ip()
|
|
|
|
|
load_exclude_ip(server_name)
|
2022-10-17 09:12:42 -04:00
|
|
|
cache_logs(server_name)
|
2022-07-19 11:02:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function run_app_ok()
|
|
|
|
|
if not debug_mode then return run_app() end
|
2022-07-18 13:54:10 -04:00
|
|
|
|
2022-12-01 04:09:39 -05:00
|
|
|
local presult, err = pcall( function() run_app() end)
|
2022-07-18 13:54:10 -04:00
|
|
|
if not presult then
|
2022-10-16 12:44:04 -04:00
|
|
|
C:D("debug error on :"..tostring(err))
|
2022-07-18 13:54:10 -04:00
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-19 11:02:05 -04:00
|
|
|
return run_app_ok()
|
2022-07-18 13:54:10 -04:00
|
|
|
}
|
|
|
|
|
|