This commit is contained in:
midoks
2023-02-13 00:26:31 +08:00
parent 083fe59fa5
commit bf281107b2
3 changed files with 64 additions and 23 deletions
+5
View File
@@ -32,6 +32,11 @@ end
ngx.shared.waf_limit:set("cpu_usage", 0, 10)
function waf_timer_every_get_cpu(premature)
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))
end
local cpu_percent = WAF_C:read_file_body(waf_root.."/cpu.info")
-- WAF_C:D("cpu_usage:"..tostring(cpu_percent ))
if cpu_percent then
+28
View File
@@ -765,6 +765,34 @@ function _M.is_key(self, arr, key)
return false
end
function _M.get_cpu_stat(self)
local cpu_total = 0
local fp = io.open('/proc/stat','r')
local cpu_line = fp:read()
fp:close()
local list = ngx_re.split(cpu_line," ")
table.remove(list,1)
table.remove(list,1)
local idie = list[4]
for i,v in pairs(list)
do
cpu_total = cpu_total + v
end
local use_percent = tonumber(100-(idie/cpu_total)*100)
return cpu_total,idie,use_percent
end
function _M.get_cpu_percent(self)
local cpu_total,idie,use_percent = self:get_cpu_stat()
ngx.sleep(2)
local cpu_total2,idie2,use_percent2 = self:get_cpu_stat()
local cpu_usage_percent = tonumber(100-(((idie2-idie)/(cpu_total2-cpu_total))*100))
return cpu_usage_percent
end
function _M.return_post_data(self)
if method ~= "POST" then return false end