From b10ee951f67857e11fc0b484862a9e2de4c519ed Mon Sep 17 00:00:00 2001 From: midoks Date: Tue, 19 Jul 2022 01:54:10 +0800 Subject: [PATCH] =?UTF-8?q?webstats=20=20=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- class/core/config_api.py | 2 +- plugins/webstats/conf/init.sql | 20 ++++ plugins/webstats/conf/webstats.conf | 2 + plugins/webstats/ico.png | Bin 0 -> 820 bytes plugins/webstats/index.html | 18 +++ plugins/webstats/index.py | 158 +++++++++++++++++++++++++ plugins/webstats/info.json | 17 +++ plugins/webstats/install.sh | 93 +++++++++++++++ plugins/webstats/js/stats.js | 0 plugins/webstats/lua/web_stats_log.lua | 49 ++++++++ 10 files changed, 358 insertions(+), 1 deletion(-) create mode 100644 plugins/webstats/conf/init.sql create mode 100644 plugins/webstats/conf/webstats.conf create mode 100644 plugins/webstats/ico.png create mode 100755 plugins/webstats/index.html create mode 100755 plugins/webstats/index.py create mode 100755 plugins/webstats/info.json create mode 100755 plugins/webstats/install.sh create mode 100644 plugins/webstats/js/stats.js create mode 100644 plugins/webstats/lua/web_stats_log.lua diff --git a/class/core/config_api.py b/class/core/config_api.py index e47f865a2..3fc674260 100755 --- a/class/core/config_api.py +++ b/class/core/config_api.py @@ -17,7 +17,7 @@ class config_api: # 进行中. # 兼容主流Linux系统 - __version = '0.8.6.9' + __version = '0.8.6.10' def __init__(self): pass diff --git a/plugins/webstats/conf/init.sql b/plugins/webstats/conf/init.sql new file mode 100644 index 000000000..465f6f409 --- /dev/null +++ b/plugins/webstats/conf/init.sql @@ -0,0 +1,20 @@ +CREATE TABLE IF NOT EXISTS `web_logs` ( + `time` INTEGER, + `ip` TEXT, + `domain` TEXT, + `server_name` TEXT, + `method` TEXT, + `status_code` INTEGER, + `uri` TEXT, + `body_length` INTEGER, + `referer` TEXT DEFAULT "", + `user_agent` TEXT, + `is_spider` INTEGER DEFAULT 0, + `protocol` TEXT, + `request_time` INTEGER, + `request_headers` TEXT DEFAULT "", + `ip_list` TEXT DEFAULT "", + `client_port` INTEGER DEFAULT -1 +); + +CREATE INDEX time_inx ON web_logs(`time`); diff --git a/plugins/webstats/conf/webstats.conf b/plugins/webstats/conf/webstats.conf new file mode 100644 index 000000000..0101113ad --- /dev/null +++ b/plugins/webstats/conf/webstats.conf @@ -0,0 +1,2 @@ +lua_shared_dict mw_total 50m; +include {$SERVER_APP}/lua/web_stats_log.lua; \ No newline at end of file diff --git a/plugins/webstats/ico.png b/plugins/webstats/ico.png new file mode 100644 index 0000000000000000000000000000000000000000..33f6c09f0208aed6772172f69212e584739cbc27 GIT binary patch literal 820 zcmV-41Izr0P)Px%?ny*JRA@u(SUqSIQ4s#-nHa5-Mr5YW8~L<|n7fu}~{x z6%Ba1A}R=?HY$naHXE#+hmty18>WF^WK|p zzL{_49bt=qw8j000$dim#j+puO0?N~;Q`Zu6WfTo zJfM$&oWH*Cw>A6qPS2k(POfGX8-TPiagrdORW4&b<5-2q^%tk~Qc4g_0q9_O{jS;j zJV^kq5X1<8dB@D}Y35j3FFhrqeuj6|HG6L)20%tH4TI` z07Q*IuwOB-&Pc9Fg;IK{13;&*BK7wXFhE47ndrS+)D9-D2C_q?bIf#47AOXPam~WM zL;=Vc_5^@J)v2Ehd}_de&rBx+^X@7#y5TC68MKc=;7zqb8NauSho48aU-baehCStX z+2jETsqbqA;P(q+ez$^2A!b`zT0hK;_I#72tG_NS;=nOk;|UF5c{;A*BRz0T?W_#M zh6AVt1eo{JZDlR_BRK7<{7z&!Ma_sb%*D;X5~doC9J%2~I~Eq&2MFT+e-PrBQ#}5v z8Hi0|KsGaF4clcPMZkr!nQtoJi3Jc=ZWVwaH^ZustOn|bLz((11{4EL^idUYeQL;x zDX~SIG3>hl&dVB4+yItL0ES}~taW$d?9gN%GmphJPKS}vlwQhJ|33l~X>L(_xo!ad z#*A)%1R)#tK|{<>8eb@wiF4I=L&I^JL0v@X^Y=4j+_my2A`Nr3TF~vW;9%K^8_PPQ z>{$8zva_U(Z0P|Reex6pA7-vR?3aDS*a<>A1K%Z=wgK<}EH<(uV2+5U%SCNm=6q#X y4P%sK8wFsq*-;EA1|mj4rBK8GM&7>V8TbdfA6`!YHWYUN0000 +
+
+

服务

+

概览

+

全局设置

+
+
+
+
+
+ + \ No newline at end of file diff --git a/plugins/webstats/index.py b/plugins/webstats/index.py new file mode 100755 index 000000000..13c279c40 --- /dev/null +++ b/plugins/webstats/index.py @@ -0,0 +1,158 @@ +# coding:utf-8 + +import sys +import io +import os +import time + +sys.path.append(os.getcwd() + "/class/core") +import mw + +app_debug = False +if mw.isAppleSystem(): + app_debug = True + + +def getPluginName(): + return 'webstats' + + +def getPluginDir(): + return mw.getPluginDir() + '/' + getPluginName() + + +def getServerDir(): + return mw.getServerDir() + '/' + getPluginName() + + +def getArgs(): + args = sys.argv[2:] + tmp = {} + args_len = len(args) + + if args_len == 1: + t = args[0].strip('{').strip('}') + t = t.split(':') + tmp[t[0]] = t[1] + elif args_len > 1: + for i in range(len(args)): + t = args[i].split(':') + tmp[t[0]] = t[1] + + return tmp + + +def checkArgs(data, ck=[]): + for i in range(len(ck)): + if not ck[i] in data: + return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!')) + return (True, mw.returnJson(True, 'ok')) + + +def luaConf(): + return mw.getServerDir() + '/web_conf/nginx/vhost/webstats.conf' + + +def status(): + path = luaConf() + if not os.path.exists(path): + return 'stop' + return 'start' + + +def pSqliteDb(dbname='web_logs'): + file = getServerDir() + '/webstats.db' + name = 'webstats' + if not os.path.exists(file): + conn = mw.M(dbname).dbPos(getServerDir(), name) + sql = mw.readFile(getPluginDir() + '/conf/init.sql') + sql_list = sql.split(';') + for index in range(len(sql_list)): + conn.execute(sql_list[index], ()) + else: + conn = mw.M(dbname).dbPos(getServerDir(), name) + return conn + + +def initDreplace(): + + service_path = getServerDir() + + pSqliteDb() + + path = luaConf() + path_tpl = getPluginDir() + '/conf/webstats.conf' + if not os.path.exists(path): + content = mw.readFile(path_tpl) + content = content.replace('{$SERVER_APP}', service_path) + content = content.replace('{$ROOT_PATH}', mw.getServerDir()) + mw.writeFile(path, content) + + lua_dir = getServerDir() + "/lua" + lua_dst = lua_dir + "/web_stats_log.lua" + if not os.path.exists(lua_dst): + mw.execShell('mkdir -p ' + lua_dir) + lua_tpl = getPluginDir() + '/lua/web_stats_log.lua' + content = mw.readFile(lua_tpl) + content = content.replace('{$SERVER_APP}', service_path) + content = content.replace('{$ROOT_PATH}', mw.getServerDir()) + mw.writeFile(lua_dst, content) + + debug_log = getServerDir() + "/debug.log" + if not os.path.exists(debug_log): + mw.execShell('mkdir -p ' + lua_dir) + mw.writeFile(debug_log, '') + + return 'ok' + + +def start(): + initDreplace() + mw.restartWeb() + return 'ok' + + +def stop(): + path = luaConf() + os.remove(path) + mw.restartWeb() + return 'ok' + + +def restart(): + initDreplace() + return 'ok' + + +def reload(): + initDreplace() + + lua_dir = getServerDir() + "/lua" + lua_dst = lua_dir + "/web_stats_log.lua" + lua_tpl = getPluginDir() + '/lua/web_stats_log.lua' + content = mw.readFile(lua_tpl) + content = content.replace('{$SERVER_APP}', getServerDir()) + content = content.replace('{$ROOT_PATH}', mw.getServerDir()) + mw.writeFile(lua_dst, content) + mw.restartWeb() + return 'ok' + + +if __name__ == "__main__": + func = sys.argv[1] + if func == 'status': + print(status()) + elif func == 'start': + print(start()) + elif func == 'stop': + print(stop()) + elif func == 'restart': + print(restart()) + elif func == 'reload': + print(reload()) + elif func == 'run_info': + print(runInfo()) + elif func == 'conf': + print(getConf()) + else: + print('error') diff --git a/plugins/webstats/info.json b/plugins/webstats/info.json new file mode 100755 index 000000000..69f2fa3e7 --- /dev/null +++ b/plugins/webstats/info.json @@ -0,0 +1,17 @@ +{ + "sort": 7, + "ps": "【DEV】网站统计报表", + "name": "webstats", + "title": "网站统计报表", + "shell": "install.sh", + "versions":["0.0.1"], + "tip": "soft", + "checks": "server/webstats", + "path": "server/webstats", + "display": 1, + "author": "midoks", + "date": "2022-07-18", + "home": "https://github.com/midoks/mdserver-web", + "type": 0, + "pid": "1" +} \ No newline at end of file diff --git a/plugins/webstats/install.sh b/plugins/webstats/install.sh new file mode 100755 index 000000000..3081a72ef --- /dev/null +++ b/plugins/webstats/install.sh @@ -0,0 +1,93 @@ +#!/bin/bash +PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin +export PATH + + +## https://www.yangshuaibin.com/detail/392251 + +curPath=`pwd` +rootPath=$(dirname "$curPath") +rootPath=$(dirname "$rootPath") +serverPath=$(dirname "$rootPath") + + +install_tmp=${rootPath}/tmp/mw_install.pl + +VERSION=$2 + +Install_App() +{ + echo '正在安装脚本文件...' > $install_tmp + mkdir -p $serverPath/source/webstats + + + mkdir -p $serverPath/webstats + + # 下载源码安装包 + # curl -O $serverPath/source/webstats/lua-5.1.5.tar.gz https://www.lua.org/ftp/lua-5.1.5.tar.gz + # cd $serverPath/source/webstats && tar xvf lua-5.1.5.tar.gz + # cd lua-5.1.5 + # make linux test && make install + + + # luarocks + + if [ ! -f $serverPath/source/webstats/luarocks-3.5.0.tar.gz ];then + wget -O $serverPath/source/webstats/luarocks-3.5.0.tar.gz https://luarocks.org/releases/luarocks-3.5.0.tar.gz + fi + + # which luarocks + # if [ "$?" != "0" ];then + if [ ! -d $serverPath/webstats/luarocks ];then + cd $serverPath/source/webstats && tar xvf luarocks-3.5.0.tar.gz + # cd luarocks-3.9.1 && ./configure && make bootstrap + + cd luarocks-3.5.0 && ./configure --prefix=$serverPath/webstats/luarocks --with-lua-include=$serverPath/openresty/luajit/include/luajit-2.1 --with-lua-bin=$serverPath/openresty/luajit/bin + make -I${serverPath}/openresty/luajit/bin + make install + fi + + + if [ ! -f $serverPath/source/webstats/lsqlite3_fsl09y.zip ];then + wget -O $serverPath/source/webstats/lsqlite3_fsl09y.zip http://lua.sqlite.org/index.cgi/zip/lsqlite3_fsl09y.zip?uuid=fsl_9y + cd $serverPath/source/webstats && unzip lsqlite3_fsl09y.zip + fi + + # PATH=${serverPath}/openresty/luajit:${serverPath}/openresty/luajit/include/luajit-2.1:$PATH + # export PATH=$PATH:$serverPath/webstats/luarocks/bin + # cd $serverPath/source/webstats/lsqlite3_fsl09y && make + + + # if [ ! -d $serverPath/source/webstats/luasql-2.6.0 ];then + # wget -O $serverPath/source/webstats/luasql_2.6.0.tar.gz https://github.com/keplerproject/luasql/archive/refs/tags/2.6.0.tar.gz + # cd $serverPath/source/webstats && tar xvf luasql_2.6.0.tar.gz + # fi + + # PATH=${serverPath}/openresty/luajit:${serverPath}/openresty/luajit/include/luajit-2.1:$PATH + # export PATH + # export LUA_INCDIR=${serverPath}/openresty/luajit/include/luajit-2.1 + # cd $serverPath/source/webstats/luasql-2.6.0 && make sqlite3 + + # $serverPath/webstats/luarocks/bin/luarocks config --scope user lib_modules_path ${serverPath}/openresty/luajit/lib + # $serverPath/webstats/luarocks/bin/luarocks config --scope user lib_modules_path + # $serverPath/webstats/luarocks/bin/luarocks install lua-sqlite3 + $serverPath/webstats/luarocks/bin/luarocks install luasql-sqlite3 SQLITE_DIR=$serverPath/webstats/lua + + + echo "${VERSION}" > $serverPath/webstats/version.pl + echo '安装完成' > $install_tmp + # cd $rootPath && python3 ${rootPath}/plugins/webstats/index.py start +} + +Uninstall_App() +{ + rm -rf $serverPath/webstats + echo "Uninstall_redis" > $install_tmp +} + +action=$1 +if [ "${1}" == 'install' ];then + Install_App +else + Uninstall_App +fi diff --git a/plugins/webstats/js/stats.js b/plugins/webstats/js/stats.js new file mode 100644 index 000000000..e69de29bb diff --git a/plugins/webstats/lua/web_stats_log.lua b/plugins/webstats/lua/web_stats_log.lua new file mode 100644 index 000000000..ed472e906 --- /dev/null +++ b/plugins/webstats/lua/web_stats_log.lua @@ -0,0 +1,49 @@ +log_by_lua_block { + + local ver = '0.0.1' + local debug_mode = true + + local server_name,ip,today,day,body_length,method,config,cache_count + + local db = nil + local cache = ngx.shared.mw_total + + local function D(msg) + if not debug_mode then return true end + local fp = io.open('{$SERVER_APP}/debug.log', 'ab') + if fp == nil then + return nil + end + local localtime = os.date("%Y-%m-%d %H:%M:%S") + if server_name then + fp:write(tostring(msg) .. "\n") + else + fp:write(localtime..":"..tostring(msg) .. "\n") + end + fp:flush() + fp:close() + return true + end + + + local function run_app() + D("debug start") + + local presult, err = pcall( + function() + json = require "cjson" + sqlite3 = require "lsqlite3" + end + ) + + if not presult then + D("depend on :"..tostring(err)) + return true + end + + D("debug end") + end + + return run_app() +} +