mdserver-web/plugins/op_waf/waf/lua/init_worker.lua

58 lines
1.7 KiB
Lua
Raw Permalink Normal View History

local waf_root = "{$WAF_ROOT}"
2022-10-12 01:49:57 -04:00
2023-02-13 01:16:06 -05:00
-- local waf_cpath = waf_root.."/waf/lua/?.lua;"..waf_root.."/waf/conf/?.lua;"..waf_root.."/waf/html/?.lua;"
-- local waf_sopath = waf_root.."/waf/conf/?.so;"
-- if not package.path:find(waf_cpath) then
-- package.path = waf_cpath .. package.path
-- end
2022-10-12 01:49:57 -04:00
2023-02-13 01:16:06 -05:00
-- if not package.cpath:find(waf_sopath) then
-- package.cpath = waf_sopath .. package.cpath
-- end
local json = require "cjson"
2022-10-12 05:18:51 -04:00
2023-02-11 10:25:29 -05:00
local __WAF_C = require "waf_common"
local WAF_C = __WAF_C:getInstance()
2022-10-14 05:50:26 -04:00
2023-02-11 03:06:25 -05:00
local waf_config = require "waf_config"
local waf_site_config = require "waf_site"
2023-02-11 10:25:29 -05:00
WAF_C:setConfData(waf_config, waf_site_config)
WAF_C:setDebug(true)
2023-02-11 03:06:25 -05:00
2023-02-11 03:45:28 -05:00
-- C:D("init worker"..tostring(ngx.worker.id()))
2023-02-11 04:06:24 -05:00
local function waf_timer_stats_total_log(premature)
2023-02-11 10:25:29 -05:00
WAF_C:timer_stats_total()
2022-10-12 05:18:51 -04:00
end
2023-08-11 05:15:47 -04:00
local function waf_clean_expire_data(premature)
2023-02-11 10:25:29 -05:00
WAF_C:clean_log()
2023-02-11 04:06:24 -05:00
end
2022-10-12 05:18:51 -04:00
2022-10-14 05:50:26 -04:00
ngx.shared.waf_limit:set("cpu_usage", 0, 10)
2023-02-11 04:06:24 -05:00
function waf_timer_every_get_cpu(premature)
2023-02-12 11:26:31 -05:00
if WAF_C:file_exists('/proc/stat') then
local lua_cpu_percent = WAF_C:get_cpu_percent()
-- WAF_C:D("lua_cpu_percent:"..tostring(lua_cpu_percent))
ngx.shared.waf_limit:set("cpu_usage", math.floor(lua_cpu_percent), 10)
2022-10-15 11:28:36 -04:00
else
local cpu_percent = WAF_C:read_file_body(waf_root.."/cpu.info")
-- WAF_C:D("cpu_usage:"..tostring(cpu_percent ))
if cpu_percent then
ngx.shared.waf_limit:set("cpu_usage", tonumber(cpu_percent), 10)
else
ngx.shared.waf_limit:set("cpu_usage", 0, 10)
end
2022-10-15 11:28:36 -04:00
end
2022-10-12 01:49:57 -04:00
end
2022-10-14 00:41:32 -04:00
2023-02-11 04:06:24 -05:00
if ngx.worker.id() == 0 then
ngx.timer.every(6, waf_timer_every_get_cpu)
2023-02-11 04:06:24 -05:00
-- 异步执行
ngx.timer.every(3, waf_timer_stats_total_log)
ngx.timer.every(10, waf_clean_expire_data)
2022-10-12 05:18:51 -04:00
2023-02-11 10:25:29 -05:00
WAF_C:cron()
2023-02-11 04:06:24 -05:00
end