pull/368/head
midoks 2 years ago
parent f500dcac8b
commit 3a26b71828
  1. 4
      plugins/op_waf/shell/cpu_usage_file.sh
  2. 14
      plugins/op_waf/waf/lua/init_worker.lua
  3. 40
      plugins/op_waf/waf/lua/waf_common.lua

@ -15,10 +15,10 @@ function GetCpuUsage(){
cpu_total_time=`expr $cpu_total_time + $ci`
fi
done
#echo "user_cpu:${user_cpu}"
#echo "idle_cpu:${idle_cpu}"
#echo "cpu_total_time:${cpu_total_time}"
cpu_percet=$(awk "BEGIN{print ((${cpu_total_time}-${user_cpu})/${cpu_total_time})*100}")
cpu_percet=$(awk "BEGIN{print ((${cpu_total_time}-${idle_cpu})/${cpu_total_time})*100}")
echo "${cpu_percet}"
return 0
}

@ -20,6 +20,11 @@ local waf_site_config = require "waf_site"
C:setConfData(waf_config, waf_site_config)
C:setDebug(true)
if ngx.worker.id() == 0 then
C:cron()
end
-- C:D("init worker"..tostring(ngx.worker.id()))
local function timer_stats_total_log(premature)
C:timer_stats_total()
end
@ -28,6 +33,7 @@ end
ngx.shared.waf_limit:set("cpu_usage", 0, 10)
function timer_every_get_cpu(premature)
local cpu_percent = C:read_file_body(waf_root.."/cpu.info")
-- C:D("cpu_usage:"..tostring(cpu_percent ))
if cpu_percent then
ngx.shared.waf_limit:set("cpu_usage", tonumber(cpu_percent), 10)
else
@ -35,9 +41,7 @@ function timer_every_get_cpu(premature)
end
end
if 0 == ngx.worker.id() then
ngx.timer.every(5, timer_every_get_cpu)
ngx.timer.every(5, timer_every_get_cpu)
-- 异步执行
ngx.timer.every(3, timer_stats_total_log)
end
-- 异步执行
ngx.timer.every(3, timer_stats_total_log)

@ -44,35 +44,11 @@ end
function _M.getInstance(self)
if self.instance == nil then
self.instance = self:new()
self.instance:initCron()
end
assert(self.instance ~= nil)
return self.instance
end
function _M:initCron(self)
if 0 == ngx.worker.id() then
self:cron()
end
end
function _M.initDB(self)
local path = log_dir .. "/waf.db"
db, err = sqlite3.open(path)
if err then
self:D("initDB err:"..tostring(err))
return nil
end
db:exec([[PRAGMA synchronous = 0]])
db:exec([[PRAGMA cache_size = 8000]])
db:exec([[PRAGMA page_size = 32768]])
db:exec([[PRAGMA journal_mode = wal]])
db:exec([[PRAGMA journal_size_limit = 1073741824]])
return db
end
-- 后台任务
function _M.cron(self)
local timer_every_get_data = function(premature)
@ -139,6 +115,22 @@ function _M.cron(self)
ngx.timer.every(0.5, timer_every_import_data)
end
function _M.initDB(self)
local path = log_dir .. "/waf.db"
db, err = sqlite3.open(path)
if err then
self:D("initDB err:"..tostring(err))
return nil
end
db:exec([[PRAGMA synchronous = 0]])
db:exec([[PRAGMA cache_size = 8000]])
db:exec([[PRAGMA page_size = 32768]])
db:exec([[PRAGMA journal_mode = wal]])
db:exec([[PRAGMA journal_size_limit = 1073741824]])
return db
end
function _M.clean_log(self)
local db = self:initDB()

Loading…
Cancel
Save