From 0a3ed652daf2444bb7d3c79bf1b68167a8564ecd Mon Sep 17 00:00:00 2001 From: xiyoo0812 Date: Sat, 7 Oct 2023 15:13:56 +0800 Subject: [PATCH] =?UTF-8?q?webhook=E4=B8=8A=E6=8A=A5=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/share.conf | 9 ++- script/agent/proxy_agent.lua | 97 ----------------------- script/basic/logger.lua | 23 +++--- script/constant.lua | 4 +- script/driver/webhook.lua | 62 +++++++++++---- script/feature/worker.lua | 9 +-- script/kernel.lua | 9 +-- script/kernel/perfeval_mgr.lua | 6 +- script/network/net_client.lua | 7 +- script/network/net_server.lua | 9 +-- script/network/rpc_client.lua | 5 +- script/network/rpc_server.lua | 5 +- server/worker/proxy.lua | 8 -- server/worker/proxy/proxy_mgr.lua | 48 ------------ server/worker/proxy/statis_mgr.lua | 121 ----------------------------- 15 files changed, 80 insertions(+), 342 deletions(-) delete mode 100644 script/agent/proxy_agent.lua delete mode 100644 server/worker/proxy.lua delete mode 100644 server/worker/proxy/proxy_mgr.lua delete mode 100644 server/worker/proxy/statis_mgr.lua diff --git a/bin/share.conf b/bin/share.conf index 526f5fe2..94db4188 100644 --- a/bin/share.conf +++ b/bin/share.conf @@ -137,12 +137,15 @@ set_env("QUANTA_DB_CACHE_MAX", "4096") --每帧落地最大数量 set_env("QUANTA_DB_CACHE_FLUSH", "10") ---webhook日志等级 +--webhook设置 ----------------------------------------------------- -set_env("QUANTA_WEBHOOK_LVL", "6") +--log: 日志采集 +--http:http发送 +set_env("QUANTA_WEBHOOK_MODE", "log") +--webhook地址设置 --set_env("QUANTA_LARK_URL", "https://open.feishu.cn/open-apis/bot/v2/hook/9a6565bf-51b4-48ab-80f0-64caf4d58ab8") --set_env("QUANTA_DING_URL", "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=a4c64781-adad-4ddc-a615-6bc232ce71ef") ---set_env("QUANTA_WECHAT_URL", "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ee6d97c5-477d-436f-83db-dd5361a5e8bd") +set_env("QUANTA_WECHAT_URL", "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d7cd918c-f608-4762-87af-bb8c7fc51074") --graylog配置 ----------------------------------------------------- diff --git a/script/agent/proxy_agent.lua b/script/agent/proxy_agent.lua deleted file mode 100644 index a6951320..00000000 --- a/script/agent/proxy_agent.lua +++ /dev/null @@ -1,97 +0,0 @@ ---proxy_agent.lua -local sformat = string.format -local tunpack = table.unpack -local send_worker = quanta.send_worker -local call_worker = quanta.call_worker - -local TITLE = quanta.title -local HOST_IP = environ.get("QUANTA_HOST_IP") - -local event_mgr = quanta.get("event_mgr") -local scheduler = quanta.load("scheduler") - -local ProxyAgent = singleton() -local prop = property(ProxyAgent) -prop:reader("ignore_statistics", {}) - -function ProxyAgent:__init() - if scheduler then - --启动代理线程 - scheduler:startup("proxy", "worker.proxy") - end - --添加忽略的rpc统计事件 - self:ignore_statis("rpc_heartbeat") - - local wlvl = environ.number("QUANTA_WEBHOOK_LVL") - if wlvl then - --添加webhook功能 - logger.add_monitor(self, wlvl) - end -end - ---dispatch_log -function ProxyAgent:dispatch_log(content, lvl_name) - self:fire_webhook(content, lvl_name) -end - ---webhook -function ProxyAgent:fire_webhook(content, lvl_name) - local title = sformat("%s | %s | %s", HOST_IP, quanta.service_name, lvl_name) - self:send("rpc_fire_webhook", title, content) -end - ---http_get -function ProxyAgent:http_get(url, querys, headers) - return self:call("rpc_http_get", url, querys, headers) -end - ---http_post -function ProxyAgent:http_post(url, post_data, headers, querys) - return self:call("rpc_http_post", url, post_data, headers, querys) -end - ---http_put -function ProxyAgent:http_put(url, put_data, headers, querys) - return self:call("rpc_http_put", url, put_data, headers, querys) -end - ---http_del -function ProxyAgent:http_del(url, querys, headers) - return self:call("rpc_http_del", url, querys, headers) -end - -function ProxyAgent:ignore_statis(name) - self.ignore_statistics[name] = true -end - -function ProxyAgent:statistics(event, name, ...) - if self.ignore_statistics[name] then - return - end - self:send(event, name, ...) -end - -function ProxyAgent:send(rpc, ...) - if scheduler then - return scheduler:send("proxy", rpc, ...) - end - if TITLE ~= "proxy" then - return send_worker("proxy", rpc, ...) - end - event_mgr:notify_listener(rpc, ...) -end - -function ProxyAgent:call(rpc, ...) - if scheduler then - return scheduler:call("proxy", rpc, ...) - end - if TITLE ~= "proxy" then - return call_worker("proxy", rpc, ...) - end - local rpc_datas = event_mgr:notify_listener(rpc, ...) - return tunpack(rpc_datas) -end - -quanta.proxy_agent = ProxyAgent() - -return ProxyAgent diff --git a/script/basic/logger.lua b/script/basic/logger.lua index a86bda04..aa461690 100644 --- a/script/basic/logger.lua +++ b/script/basic/logger.lua @@ -13,7 +13,6 @@ local LOG_LEVEL = log.LOG_LEVEL local title = quanta.title local monitors = _ENV.monitors or {} -local dispatch = false logger = {} logfeature = {} @@ -36,8 +35,8 @@ function logger.daemon(daemon) log.daemon(daemon) end -function logger.add_monitor(monitor, lvl) - monitors[monitor] = lvl +function logger.add_monitor(monitor) + monitors[monitor] = true end function logger.remove_monitor(monitor) @@ -73,7 +72,11 @@ local function logger_output(flag, feature, lvl, lvl_name, fmt, ...) lprint(LOG_LEVEL.WARN, 0, title, feature, wfmt, lvl_name, msg, info.short_src, info.linedefined) return end - return msg + if msg then + for monitor in pairs(monitors) do + monitor:dispatch_log(msg, lvl_name) + end + end end local LOG_LEVEL_OPTIONS = { @@ -84,19 +87,11 @@ local LOG_LEVEL_OPTIONS = { [LOG_LEVEL.FATAL] = { "fatal", 0x01 }, [LOG_LEVEL.DUMP] = { "dump", 0x01 | 0x02 }, } + for lvl, conf in pairs(LOG_LEVEL_OPTIONS) do local lvl_name, flag = tunpack(conf) logger[lvl_name] = function(fmt, ...) - local msg = logger_output(flag, "", lvl, lvl_name, fmt, ...) - if msg and (not dispatch) then - dispatch = true - pcall(function () - for monitor in pairs(monitors) do - monitor:dispatch_log(msg, lvl_name) - end - end) - dispatch = false - end + logger_output(flag, "", lvl, lvl_name, fmt, ...) end end diff --git a/script/constant.lua b/script/constant.lua index c91a473a..32e93a3a 100644 --- a/script/constant.lua +++ b/script/constant.lua @@ -4,8 +4,8 @@ local KernCode = enum("KernCode", 0) KernCode.SUCCESS = 0 --成功 KernCode.FAILED = 1 --系统错误,请重试 -KernCode.PARAM_ERROR = 2 --业务参数错误 -KernCode.TOO_FAST = 3 --操作太快 +KernCode.TOO_FAST = 2 --操作太快 +KernCode.PARAM_ERROR = 3 --业务参数错误 KernCode.UPHOLD = 4 --服务维护 KernCode.RPC_FAILED = 5 --RPC调用失败 KernCode.OPERATOR_SELF = 6 --不能对自己操作 diff --git a/script/driver/webhook.lua b/script/driver/webhook.lua index 400300fb..27005d05 100644 --- a/script/driver/webhook.lua +++ b/script/driver/webhook.lua @@ -1,51 +1,81 @@ --webhook.lua import("network/http_client.lua") +local jencode = json.encode local sformat = string.format -local LIMIT_COUNT = 3 -- 周期内最大次数 +local WEBPATH = environ.get("QUANTA_WEBHOOK_PATH", "./webhooks/") +local log_dump = logfeature.dump("webhooks", WEBPATH, true) +local thread_mgr = quanta.get("thread_mgr") local http_client = quanta.get("http_client") + +local HOST_IP = environ.get("QUANTA_HOST_IP") local HOUR_S = quanta.enum("PeriodTime", "HOUR_S") +local LIMIT_COUNT = 3 -- 周期内最大次数 + local Webhook = singleton() local prop = property(Webhook) +prop:reader("mode", nil) --mode +prop:reader("title", "") --title prop:reader("hooks", {}) --webhook通知接口 prop:reader("notify_limit", {}) --控制同样消息的发送频率 function Webhook:__init() - self.hooks.lark_log = environ.get("QUANTA_LARK_URL") - self.hooks.ding_log = environ.get("QUANTA_DING_URL") - self.hooks.wechat_log = environ.get("QUANTA_WECHAT_URL") + local mode = environ.get("QUANTA_WEBHOOK_MODE") + if mode then + --添加webhook功能 + self.mode = mode + logger.add_monitor(self) + self.title = sformat("%s | %s", HOST_IP, quanta.service_name) + --初始化hooks + self.hooks.lark_log = environ.get("QUANTA_LARK_URL") + self.hooks.ding_log = environ.get("QUANTA_DING_URL") + self.hooks.wechat_log = environ.get("QUANTA_WECHAT_URL") + end +end + +--hook_log +function Webhook:hook_log(url, body) + if self.mode == "log" then + log_dump(jencode(body)) + return + end + --http输出 + thread_mgr:entry(url, function() + http_client:call_post(url, body) + end) end --飞书 -function Webhook:lark_log(url, title, context) - local text = sformat("%s\n %s", title, context) +function Webhook:lark_log(url, context) + local text = sformat("%s\n %s", self.title, context) local body = { msg_type = "text", content = { text = text } } - http_client:call_post(url, body) + self:hook_log(url, body) end --企业微信 --at_members: 成员列表,数组,如 at_members = {"wangqing", "@all"} --at_mobiles: 手机号列表,数组, 如 at_mobiles = {"156xxxx8827", "@all"} -function Webhook:wechat_log(url, title, context, at_mobiles, at_members) - local text = sformat("%s\n %s", title, context) +function Webhook:wechat_log(url, context, at_mobiles, at_members) + local text = sformat("%s\n %s", self.title, context) local body = { msgtype = "text", text = { content = text, mentioned_list = at_members, mentioned_mobile_list = at_mobiles } } - http_client:call_post(url, body) + self:hook_log(url, body) end --钉钉 --at_all: 是否群at,如 at_all = false/false --at_mobiles: 手机号列表,数组, 如 at_mobiles = {"189xxxx8325", "156xxxx8827"} -function Webhook:ding_log(url, title, context, at_mobiles, at_all) - local text = sformat("%s\n %s", title, context) +function Webhook:ding_log(url, context, at_mobiles, at_all) + local text = sformat("%s\n %s", self.title, context) local body = { msgtype = "text", text = { content = text }, at = { atMobiles = at_mobiles, isAtAll = at_all } } - http_client:call_post(url, body) + self:hook_log(url, body) end -function Webhook:notify(title, content, ...) - if next(self.hooks) then +--dispatch_log +function Webhook:dispatch_log(content) + if self.mode then local now = quanta.now local notify = self.notify_limit[content] if not notify then @@ -60,7 +90,7 @@ function Webhook:notify(title, content, ...) end notify.count = notify.count + 1 for hook_api, url in pairs(self.hooks) do - self[hook_api](self, url, title, content, ...) + self[hook_api](self, url, content) end end end diff --git a/script/feature/worker.lua b/script/feature/worker.lua index b98a9661..bdb5a30d 100644 --- a/script/feature/worker.lua +++ b/script/feature/worker.lua @@ -30,6 +30,7 @@ local function init_core() import("kernel/thread_mgr.lua") import("kernel/event_mgr.lua") import("kernel/config_mgr.lua") + import("kernel/perfeval_mgr.lua") end --初始化网络 @@ -39,12 +40,6 @@ local function init_network() quanta.socket_mgr = socket_mgr end ---初始化统计 -local function init_statis() - import("agent/proxy_agent.lua") - import("kernel/perfeval_mgr.lua") -end - --协程改造 local function init_coroutine() coroutine.yield = function(...) @@ -75,6 +70,7 @@ end local function init_mainloop() import("kernel/timer_mgr.lua") import("kernel/update_mgr.lua") + import("driver/webhook.lua") event_mgr = quanta.get("event_mgr") thread_mgr = quanta.get("thread_mgr") update_mgr = quanta.get("update_mgr") @@ -88,7 +84,6 @@ function quanta.init() --主循环 init_coroutine() init_mainloop() - init_statis() --网络 init_network() --加载协议 diff --git a/script/kernel.lua b/script/kernel.lua index cc7574e1..54c358bc 100644 --- a/script/kernel.lua +++ b/script/kernel.lua @@ -24,6 +24,7 @@ local function init_core() import("kernel/thread_mgr.lua") import("kernel/event_mgr.lua") import("kernel/config_mgr.lua") + import("kernel/perfeval_mgr.lua") end --初始化网络 @@ -64,16 +65,11 @@ local function init_mainloop() import("kernel/timer_mgr.lua") import("kernel/update_mgr.lua") import("feature/scheduler.lua") + import("driver/webhook.lua") update_mgr = quanta.get("update_mgr") scheduler = quanta.get("scheduler") end ---初始化统计 -local function init_statis() - import("agent/proxy_agent.lua") - import("kernel/perfeval_mgr.lua") -end - function quanta.init() --核心加载 init_core() @@ -86,7 +82,6 @@ function quanta.init() init_coroutine() init_mainloop() init_network() - init_statis() --其他模式 if quanta.mode <= QuantaMode.ROUTER then --加载monitor diff --git a/script/kernel/perfeval_mgr.lua b/script/kernel/perfeval_mgr.lua index 475648de..81ccf98d 100644 --- a/script/kernel/perfeval_mgr.lua +++ b/script/kernel/perfeval_mgr.lua @@ -6,8 +6,6 @@ local env_status = environ.status local tclock_ms = timer.clock_ms local qdefer = quanta.defer -local proxy_agent = quanta.get("proxy_agent") - local PerfevalMgr = singleton() local prop = property(PerfevalMgr) prop:reader("eval_id", 0) @@ -75,8 +73,8 @@ function PerfevalMgr:start(eval_name) end function PerfevalMgr:stop(eval_data) - local clock_ms = tclock_ms() - proxy_agent:statistics("on_perfeval", eval_data, clock_ms) + --local clock_ms = tclock_ms() + --proxy_agent:statistics("on_perfeval", eval_data, clock_ms) self.eval_list[eval_data.co][eval_data.eval_id] = nil end diff --git a/script/network/net_client.lua b/script/network/net_client.lua index 8ae430a8..70b8ebb3 100644 --- a/script/network/net_client.lua +++ b/script/network/net_client.lua @@ -7,7 +7,6 @@ local qxpcall = quanta.xpcall local event_mgr = quanta.get("event_mgr") local socket_mgr = quanta.get("socket_mgr") local thread_mgr = quanta.get("thread_mgr") -local proxy_agent = quanta.get("proxy_agent") local proto_pb = luabus.eproto_type.pb @@ -61,7 +60,7 @@ function NetClient:connect(block) end end socket.on_call_pb = function(recv_len, session_id, cmd_id, flag, type, crc8, body) - proxy_agent:statistics("on_proto_recv", cmd_id, recv_len) + --:statistics("on_proto_recv", cmd_id, recv_len) qxpcall(self.on_socket_rpc, "on_socket_rpc: {}", self, socket, cmd_id, flag, type, session_id, body) end socket.on_error = function(token, err) @@ -116,12 +115,12 @@ function NetClient:write(cmd_id, data, type, session_id, flag) return false end -- call lbus - local send_len = self.socket.call_pb(cmd_id, flag, type, session_id, data) + local send_len = self.socket.call_pb(session_id, cmd_id, flag, type, 0, data) if send_len < 0 then log_err("[NetClient][write] call_pb failed! code:%s", send_len) return false end - proxy_agent:statistics("on_proto_send", cmd_id, send_len) + --proxy_agent:statistics("on_proto_send", cmd_id, send_len) return true end diff --git a/script/network/net_server.lua b/script/network/net_server.lua index 097f8298..3f9c50bc 100644 --- a/script/network/net_server.lua +++ b/script/network/net_server.lua @@ -14,7 +14,6 @@ local event_mgr = quanta.get("event_mgr") local thread_mgr = quanta.get("thread_mgr") local socket_mgr = quanta.get("socket_mgr") local protobuf_mgr = quanta.get("protobuf_mgr") -local proxy_agent = quanta.get("proxy_agent") local FLAG_REQ = quanta.enum("FlagMask", "REQ") local FLAG_RES = quanta.enum("FlagMask", "RES") @@ -93,7 +92,7 @@ function NetServer:on_socket_accept(session) end session.on_call_pb = function(recv_len, session_id, cmd_id, flag, type, crc8, body) local now_ms = quanta.now_ms - if session.lc_crc == crc8 and now_ms - session.lc_time < FAST_MS then + if crc8 > 0 and session.lc_crc == crc8 and now_ms - session.lc_time < FAST_MS then self:callback_errcode(session, cmd_id, TOO_FAST, session_id) return end @@ -103,7 +102,7 @@ function NetServer:on_socket_accept(session) session.fc_packet = session.fc_packet + 1 session.fc_bytes = session.fc_bytes + recv_len end - proxy_agent:statistics("on_proto_recv", cmd_id, recv_len) + --proxy_agent:statistics("on_proto_recv", cmd_id, recv_len) qxpcall(self.on_socket_recv, "on_socket_recv: {}", self, session, cmd_id, flag, type, session_id, body) end -- 绑定网络错误回调(断开) @@ -221,7 +220,7 @@ function NetServer:add_session(session) if not self.sessions[token] then self.sessions[token] = session self.session_count = self.session_count + 1 - proxy_agent:statistics("on_conn_update", self.session_type, self.session_count) + --proxy_agent:statistics("on_conn_update", self.session_type, self.session_count) end return token end @@ -232,7 +231,7 @@ function NetServer:remove_session(token) if session then self.sessions[token] = nil self.session_count = self.session_count - 1 - proxy_agent:statistics("on_conn_update", self.session_type, self.session_count) + --proxy_agent:statistics("on_conn_update", self.session_type, self.session_count) return session end end diff --git a/script/network/rpc_client.lua b/script/network/rpc_client.lua index 60a0bf73..a82d4ac8 100644 --- a/script/network/rpc_client.lua +++ b/script/network/rpc_client.lua @@ -10,7 +10,6 @@ local event_mgr = quanta.get("event_mgr") local timer_mgr = quanta.get("timer_mgr") local socket_mgr = quanta.get("socket_mgr") local thread_mgr = quanta.get("thread_mgr") -local proxy_agent = quanta.get("proxy_agent") local FLAG_REQ = quanta.enum("FlagMask", "REQ") local FLAG_RES = quanta.enum("FlagMask", "RES") @@ -59,7 +58,7 @@ end --调用rpc后续处理 function RpcClient:on_call_router(rpc, token, send_len) if send_len > 0 then - proxy_agent:statistics("on_rpc_send", rpc, send_len) + --proxy_agent:statistics("on_rpc_send", rpc, send_len) return true, send_len end log_err("[RpcClient][on_call_router] rpc {} call failed! code:{}", rpc, send_len) @@ -144,7 +143,7 @@ function RpcClient:on_socket_rpc(socket, session_id, rpc_flag, recv_len, source, if rpc == "on_heartbeat" then return end - proxy_agent:statistics("on_rpc_recv", rpc, recv_len) + --proxy_agent:statistics("on_rpc_recv", rpc, recv_len) if session_id == 0 or rpc_flag == FLAG_REQ then local function dispatch_rpc_message(...) local _ = qeval(rpc) diff --git a/script/network/rpc_server.lua b/script/network/rpc_server.lua index 82a4cdd1..8132ebc8 100644 --- a/script/network/rpc_server.lua +++ b/script/network/rpc_server.lua @@ -13,7 +13,6 @@ local hash_code = codec.hash_code local event_mgr = quanta.get("event_mgr") local thread_mgr = quanta.get("thread_mgr") local socket_mgr = quanta.get("socket_mgr") -local proxy_agent = quanta.get("proxy_agent") local FLAG_REQ = quanta.enum("FlagMask", "REQ") local FLAG_RES = quanta.enum("FlagMask", "RES") @@ -59,7 +58,7 @@ end --rpc事件 function RpcServer:on_socket_rpc(client, session_id, rpc_flag, recv_len, source, rpc, ...) - proxy_agent:statistics("on_rpc_recv", rpc, recv_len) + --proxy_agent:statistics("on_rpc_recv", rpc, recv_len) if session_id == 0 or rpc_flag == FLAG_REQ then local function dispatch_rpc_message(...) local _ = qeval(rpc) @@ -99,7 +98,7 @@ function RpcServer:on_socket_accept(client) log_err("[RpcServer][call_rpc] call failed! code:{}", send_len) return false end - proxy_agent:statistics("on_rpc_send", rpc, send_len) + --proxy_agent:statistics("on_rpc_send", rpc, send_len) return true, SUCCESS end client.on_call = function(recv_len, session_id, rpc_flag, ...) diff --git a/server/worker/proxy.lua b/server/worker/proxy.lua deleted file mode 100644 index 177463b7..00000000 --- a/server/worker/proxy.lua +++ /dev/null @@ -1,8 +0,0 @@ ---proxy.lua -import("feature/worker.lua") - ---启动worker -quanta.startup(function() - import("worker/proxy/proxy_mgr.lua") - import("worker/proxy/statis_mgr.lua") -end) diff --git a/server/worker/proxy/proxy_mgr.lua b/server/worker/proxy/proxy_mgr.lua deleted file mode 100644 index fbea6913..00000000 --- a/server/worker/proxy/proxy_mgr.lua +++ /dev/null @@ -1,48 +0,0 @@ ---proxy_mgr.lua -import("driver/webhook.lua") -import("driver/graylog.lua") -import("network/http_client.lua") - -local webhook = quanta.get("webhook") -local event_mgr = quanta.get("event_mgr") -local http_client = quanta.get("http_client") - -local ProxyMgr = singleton() - -function ProxyMgr:__init() - -- 注册事件 - event_mgr:add_listener(self, "rpc_fire_webhook") - -- 通用http请求 - event_mgr:add_listener(self, "rpc_http_post") - event_mgr:add_listener(self, "rpc_http_get") - event_mgr:add_listener(self, "rpc_http_put") - event_mgr:add_listener(self, "rpc_http_del") -end - ---日志上报 -function ProxyMgr:rpc_fire_webhook(title, content) - webhook:notify(title, content) -end - ---通用http请求 -function ProxyMgr:rpc_http_get(url, querys, headers) - local ok, status, res = http_client:call_get(url, querys, headers) - return ok and status or 404, res -end - -function ProxyMgr:rpc_http_post(url, post_data, headers, querys) - local ok, status, res = http_client:call_post(url, post_data, headers, querys) - return ok and status or 404, res -end - -function ProxyMgr:rpc_http_put(url, put_data, headers, querys) - local ok, status, res = http_client:call_put(url, put_data, headers, querys) - return ok and status or 404, res -end - -function ProxyMgr:rpc_http_del(url, querys, headers) - local ok, status, res = http_client:call_del(url, querys, headers) - return ok and status or 404, res -end - -quanta.proxy_mgr = ProxyMgr() diff --git a/server/worker/proxy/statis_mgr.lua b/server/worker/proxy/statis_mgr.lua deleted file mode 100644 index b063d67e..00000000 --- a/server/worker/proxy/statis_mgr.lua +++ /dev/null @@ -1,121 +0,0 @@ ---statis_mgr.lua -import("feature/worker.lua") -import("feature/linux.lua") - -local json_encode = json.encode - -local log_path = environ.get("QUANTA_STATIS_PATH") -local log_dump = logfeature.dump("statis", log_path, true) - -local event_mgr = quanta.get("event_mgr") -local update_mgr = quanta.get("update_mgr") -local linux_statis = quanta.get("linux_statis") - -local StatisMgr = singleton() -local prop = property(StatisMgr) -prop:reader("statis_datas", {}) -prop:reader("statis_enable", false) --统计开关 - -function StatisMgr:__init() - local statis_enable = environ.status("QUANTA_STATIS") - if statis_enable then - self.statis_enable = statis_enable - --定时处理 - update_mgr:attach_second5(self) - --系统监控 - if quanta.platform == "linux" then - linux_statis:setup() - end - end - --事件监听 - event_mgr:add_listener(self, "on_rpc_send") - event_mgr:add_listener(self, "on_rpc_recv") - event_mgr:add_listener(self, "on_perfeval") - event_mgr:add_listener(self, "on_proto_recv") - event_mgr:add_listener(self, "on_proto_send") - event_mgr:add_listener(self, "on_conn_update") -end - ---输出到日志 -function StatisMgr:flush() - for _, measure in pairs(self.statis_datas) do - log_dump(json_encode(measure)) - end - self.statis_datas = {} -end - -function StatisMgr:write_log(name, type, add_count) - if self.statis_enable then - self.statis_datas[#self.statis_datas + 1] = { - name = name, - type = type, - value = add_count, - index = quanta.index, - service = quanta.service, - ser_name = quanta.service_name, - } - end -end - --- 统计proto协议发送(KB) -function StatisMgr:on_proto_recv(cmd_id, send_len) - self:write_log(cmd_id, "proto_recv", send_len) -end - --- 统计proto协议接收(KB) -function StatisMgr:on_proto_send(cmd_id, recv_len) - self:write_log( cmd_id, "proto_send", recv_len) -end - --- 统计rpc协议发送(KB) -function StatisMgr:on_rpc_send(rpc, send_len) - self:write_log(rpc, "rpc_send", send_len) -end - --- 统计rpc协议接收(KB) -function StatisMgr:on_rpc_recv(rpc, recv_len) - self:write_log( rpc, "rpc_recv", recv_len) -end - --- 统计cmd协议连接 -function StatisMgr:on_conn_update(conn_type, conn_count) - self:write_log( conn_type, "conn", conn_count) -end - --- 统计性能 -function StatisMgr:on_perfeval(eval_data, clock_ms) - self:write_log(eval_data.eval_name, "perfeval", eval_data.yield_time) -end - --- 统计系统信息 -function StatisMgr:on_second5() - self:write_log("all_mem","system", self:_calc_mem_use()) - self:write_log("lua_mem","system", self:_calc_lua_mem()) - self:write_log("cpu_rate","system", self:_calc_cpu_rate()) - self:flush() -end - --- 计算lua内存信息(KB) -function StatisMgr:_calc_lua_mem() - return collectgarbage("count") -end - --- 计算内存信息(KB) -function StatisMgr:_calc_mem_use() - if quanta.platform == "linux" then - return linux_statis:calc_memory() - end - return 5000 -end - --- 计算cpu使用率 -function StatisMgr:_calc_cpu_rate() - if quanta.platform == "linux" then - return linux_statis:calc_cpu_rate() - end - return 0.1 -end - -quanta.statis_mgr = StatisMgr() - -return StatisMgr