diff --git a/new_cli.sh b/new_cli.sh index 63f4795c7..8dcd7cd3f 100755 --- a/new_cli.sh +++ b/new_cli.sh @@ -3,6 +3,8 @@ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/opt/hom DIR=$(cd "$(dirname "$0")"; pwd) MDIR=$(dirname "$DIR") +export LC_ALL="en_US.UTF-8" + # echo $DIR PATH=$PATH:$DIR/bin @@ -14,9 +16,6 @@ if [ -f ${DIR}/bin/activate ];then fi fi -export LC_ALL="en_US.UTF-8" - - mw_start_task() { isStart=$(ps aux |grep 'panel_task.py'|grep -v grep|awk '{print $2}') diff --git a/panel_task.py b/panel_task.py index b23038993..c5d18a68a 100755 --- a/panel_task.py +++ b/panel_task.py @@ -31,16 +31,6 @@ import core.mw as mw import core.db as db -# print(mw.getPanelDir()) - -# print sys.path - -# cmd = 'ls /usr/local/lib/ | grep python | cut -d \\ -f 1 | awk \'END {print}\'' -# info = mw.execShell(cmd) -# p = "/usr/local/lib/" + info[0].strip() + "/site-packages" -# sys.path.append(p) - - global pre, timeoutCount, logPath, isTask, oldEdate, isCheck pre = 0 timeoutCount = 0 @@ -85,9 +75,12 @@ def service_cmd(method): execShell(cmd + ' ' + method) return - cmd = mw.getRunDir() + '/scripts/init.d/mw' + cmd = mw.getPanelDir() + '/scripts/init.d/mw' + print(cmd) if os.path.exists(cmd): - execShell(cmd + ' ' + method) + print(cmd + ' ' + method) + data = execShell(cmd + ' ' + method) + print(data) return @@ -573,7 +566,7 @@ def openrestyRestartAtOnce(): # --------------------------------------Panel Restart Start --------------------------------------------- # def restartPanelService(): - restartTip = 'data/restart.pl' + restartTip = mw.getPanelDir()+'/data/restart.pl' while True: if os.path.exists(restartTip): os.remove(restartTip) @@ -612,10 +605,10 @@ def run(): # oar.start() - # # Panel Restart Start - # rps = threading.Thread(target=restartPanelService) - # rps = setDaemon(rps) - # rps.start() + # Panel Restart Start + rps = threading.Thread(target=restartPanelService) + rps = setDaemon(rps) + rps.start() # 面板后台任务 startPanelTask() diff --git a/panel_tools.py b/panel_tools.py index a6a28cfb3..a56444a8f 100755 --- a/panel_tools.py +++ b/panel_tools.py @@ -228,16 +228,15 @@ def set_panel_pwd(password, ncli=False): def show_panel_pwd(): - # 设置面板密码 - sql = db.Sql() - password = sql.table('users').where('id=?', (1,)).getField('password') - - file_pwd = '' - if os.path.exists('data/default.pl'): - file_pwd = mw.readFile('data/default.pl').strip() + # 面板密码展示 + info = model.getUserByRoot() + defailt_pwd_file = mw.getPanelDir()+'/data/default.pl' + pwd = '' + if os.path.exists(defailt_pwd_file): + pwd = mw.readFile(defailt_pwd_file).strip() - if mw.md5(file_pwd) == password: - print('password: ' + file_pwd) + if mw.md5(pwd) == info['password']: + print('password: ' + pwd) return print("password has been changed!") diff --git a/scripts/init.d/mw.tpl b/scripts/init.d/mw.tpl index 4a44f97ed..236c57a63 100755 --- a/scripts/init.d/mw.tpl +++ b/scripts/init.d/mw.tpl @@ -104,7 +104,7 @@ mw_start() # /www/server/mdserver-web/tmp/panelTask.pl && service mw restart_task mw_stop_task() { - if [ -f $mw_path/tmp/panelTask.pl ];then + if [ -f ${PANEL_DIR}/tmp/panelTask.pl ];then echo -e "\033[32mthe task is running and cannot be stopped\033[0m" exit 0 fi @@ -123,7 +123,7 @@ mw_stop_panel() { echo -e "stopping mw-panel... \c"; - pidfile=${mw_path}/logs/mw.pid + pidfile=${PANEL_DIR}/logs/mw.pid if [ -f $pidfile ];then pid=`cat $pidfile` kill -9 $pid > /dev/null 2>&1 diff --git a/web/admin/crontab/__init__.py b/web/admin/crontab/__init__.py index 6162e7272..f10762d03 100644 --- a/web/admin/crontab/__init__.py +++ b/web/admin/crontab/__init__.py @@ -27,8 +27,8 @@ def list(): page = request.args.get('p', 1) size = 10 count = Crontab.query.count() - print(count) + # print(count) clist = Crontab.query.paginate(page=int(page), per_page=size) - print(clist) + # print(clist) return [] \ No newline at end of file diff --git a/web/admin/dashboard/__init__.py b/web/admin/dashboard/__init__.py index 0bf9fdee3..e3fd25933 100644 --- a/web/admin/dashboard/__init__.py +++ b/web/admin/dashboard/__init__.py @@ -32,6 +32,18 @@ blueprint = Blueprint('dashboard', __name__, url_prefix='/', template_folder='.. def index(): return render_template('default/index.html') +# 安全路径 +@blueprint.route('/',endpoint='admin_safe_path',methods=['GET']) +def admin_safe_path(path): + db_path = model.getOption('admin_path') + if db_path == path: + return render_template('default/login.html') + + unauthorized_status = model.getOption('unauthorized_status') + if unauthorized_status == '0': + return render_template('default/path.html') + return Response(status=int(unauthorized_status)) + # 仅针对webhook插件 @blueprint.route("/hook", methods=['POST', 'GET']) def webhook(): @@ -63,19 +75,6 @@ def webhook(): except Exception as e: return str(e) -# 安全路径 -@blueprint.route('/',endpoint='admin_safe_path',methods=['GET']) -def admin_safe_path(path): - db_path = model.getOption('admin_path') - if db_path == path: - return render_template('default/login.html') - - unauthorized_status = model.getOption('unauthorized_status') - if unauthorized_status == '0': - return render_template('default/path.html') - return Response(status=int(unauthorized_status)) - - # --------------------------------------------------------------------------------- # 定义登录入口相关方法 # --------------------------------------------------------------------------------- diff --git a/web/admin/firewall/__init__.py b/web/admin/firewall/__init__.py index 182390ce4..a14c0dff7 100644 --- a/web/admin/firewall/__init__.py +++ b/web/admin/firewall/__init__.py @@ -75,8 +75,21 @@ def set_ping(): mf = MwFirewall.instance() return mf.setPing() +# 添加放行端口 +@blueprint.route('/add_accept_port', endpoint='add_accept_port', methods=['POST']) +@panel_login_required +def add_accept_port(): + mf = MwFirewall.instance() + if not mf.getFwStatus(): + return mw.returnData(False, '防火墙启动时,才能添加规则!') + port = request.form.get('port', '').strip() + ps = request.form.get('ps', '').strip() + protocol = request.form.get('protocol', '').strip() + stype = request.form.get('type', '').strip() + data = mf.addAcceptPort(port, ps, stype, protocol=protocol) + return mw.getJson(data) diff --git a/web/admin/model/__init__.py b/web/admin/model/__init__.py index 1d1af4dc7..b0821ac6d 100644 --- a/web/admin/model/__init__.py +++ b/web/admin/model/__init__.py @@ -8,6 +8,7 @@ # Author: midoks # --------------------------------------------------------------------------------- + from .initdb import * from .logs import addLog,clearLog @@ -25,5 +26,11 @@ from .user import isLoginCheck from .user import getUserByName,getUserById,getUserByRoot from .user import setUserByRoot +from .firewall import addFirewall + from .temp_login import getTempLoginByToken,clearTempLogin + + + + diff --git a/web/admin/model/firewall.py b/web/admin/model/firewall.py new file mode 100644 index 000000000..db7c36939 --- /dev/null +++ b/web/admin/model/firewall.py @@ -0,0 +1,41 @@ +# coding:utf-8 + +# --------------------------------------------------------------------------------- +# MW-Linux面板 +# --------------------------------------------------------------------------------- +# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved. +# --------------------------------------------------------------------------------- +# Author: midoks +# --------------------------------------------------------------------------------- + +from admin.model import db, Firewall + +import core.mw as mw + + + +def addFirewall(port, + protocol: str | None = 'tcp', + ps: str | None = '备注' +) -> bool: + ''' + 设置配置的值 + :port -> str 端口 (必填) + :protocol -> str 协议 (可选|tcp,udp,tcp/udp) + :ps -> str 备注 (可选) + ''' + now_time = mw.formatDate() + add_data = Firewall( + port=port, + protocol=protocol, + add_time=now_time, + update_time=now_time) + db.session.add(add_data) + db.session.commit() + return True + + +def getFirewallCountByPort(port): + return Firewall.query.filter(Tasks.port==port).count() + + diff --git a/web/admin/model/logs.py b/web/admin/model/logs.py index 94f911966..54cda769f 100644 --- a/web/admin/model/logs.py +++ b/web/admin/model/logs.py @@ -13,13 +13,7 @@ import json import time from admin.model import db, Logs - -def formatDate(format="%Y-%m-%d %H:%M:%S", times=None): - # 格式化指定时间戳 - if not times: - times = int(time.time()) - time_local = time.localtime(times) - return time.strftime(format, time_local) +import core.mw as mw def clearLog(): try: @@ -42,12 +36,12 @@ def addLog(type, log, :log -> str 日志内容 (必填) :uid -> int 用户ID ''' - add_time = formatDate() + now_time = mw.formatDate() add_logs = Logs( uid=uid, log=log, type=type, - add_time=add_time) + add_time=now_time) db.session.add(add_logs) db.session.commit() db.session.close() diff --git a/web/admin/setting/setting.py b/web/admin/setting/setting.py index 0e3b1a26b..05717339f 100644 --- a/web/admin/setting/setting.py +++ b/web/admin/setting/setting.py @@ -142,7 +142,7 @@ def set_basic_auth(): return mw.returnData(True, '设置成功!') -# 设置站点状态 +# 设置面板未登录状态 @blueprint.route('/set_status_code', endpoint='set_status_code', methods=['POST']) @panel_login_required def set_status_code(): @@ -159,7 +159,32 @@ def set_status_code(): model.setOption('unauthorized_status', str(status_code)) mw.writeLog('面板设置', '将未授权响应状态码设置为:{0}:{1}'.format(status_code,info['text'])) return mw.returnData(True, '设置成功!') - +# 设置站点状态 +@blueprint.route('/set_port', endpoint='set_port', methods=['POST']) +@panel_login_required +def set_port(): + port = request.form.get('port', '') + if port != mw.getHostPort(): + import system_api + import firewall_api + + sysCfgDir = mw.systemdCfgDir() + if os.path.exists(sysCfgDir + "/firewalld.service"): + if not firewall_api.firewall_api().getFwStatus(): + return mw.returnData(False, 'firewalld必须先启动!') + + # mw.setHostPort(port) + + msg = mw.getInfo('放行端口[{1}]成功', (port,)) + mw.writeLog("防火墙管理", msg) + addtime = time.strftime('%Y-%m-%d %X', time.localtime()) + mw.M('firewall').add('port,ps,addtime', (port, "配置修改", addtime)) + + # firewall_api.firewall_api().addAcceptPort(port) + # firewall_api.firewall_api().firewallReload() + + # system_api.system_api().restartMw() + return mw.returnJson(True, '端口保存成功!') \ No newline at end of file diff --git a/web/admin/system/__init__.py b/web/admin/system/__init__.py index 840978ec2..47d29fb71 100644 --- a/web/admin/system/__init__.py +++ b/web/admin/system/__init__.py @@ -8,169 +8,6 @@ # Author: midoks # --------------------------------------------------------------------------------- - -from flask import Blueprint, render_template -from flask import request - -from admin.user_login_check import panel_login_required -import admin.model.option as option - -import core.mw as mw -import utils.system as sys - - -blueprint = Blueprint('system', __name__, url_prefix='/system', template_folder='../../templates') - -# 获取系统的统计信息 -@blueprint.route('/system_total', endpoint='system_total') -@panel_login_required -def system_total(): - data = sys.getMemInfo() - cpu = sys.getCpuInfo(interval=1) - data['cpuNum'] = cpu[1] - data['cpuRealUsed'] = cpu[0] - data['time'] = sys.getBootTime() - data['system'] = sys.getSystemVersion() - data['version'] = '0.0.1' - return data - -# 获取系统的网络流量信息 -@blueprint.route('/network', endpoint='network') -@panel_login_required -def network(): - stat = {} - stat['cpu'] = sys.getCpuInfo() - stat['load'] = sys.getLoadAverage() - stat['mem'] = sys.getMemInfo() - stat['iostat'] = sys.stats().disk() - stat['network'] = sys.stats().network() - return stat - -# 获取系统的磁盘信息 -@blueprint.route('/disk_info', endpoint='disk_info') -@panel_login_required -def disk_info(): - data = sys.getDiskInfo() - return data - -# 获取系统的负载统计信息 -@blueprint.route('/get_load_average', endpoint='get_load_average', methods=['GET']) -@panel_login_required -def get_load_average(): - start = request.args.get('start', '') - end = request.args.get('end', '') - data = mw.M('load_average').dbPos(mw.getPanelDataDir(),'system')\ - .where("addtime>=? AND addtime<=?", (start, end,))\ - .field('id,pro,one,five,fifteen,addtime')\ - .order('id asc').select() - # return self.toAddtime(data) - # print(data) - return data - -# 获取系统的磁盘IO统计信息 -@blueprint.route('/get_disk_io', endpoint='get_disk_io', methods=['GET']) -@panel_login_required -def get_disk_io(): - start = request.args.get('start', '') - end = request.args.get('end', '') - data = mw.M('diskio').dbPos(mw.getPanelDataDir(),'system')\ - .where("addtime>=? AND addtime<=?", (start, end))\ - .field('id,read_count,write_count,read_bytes,write_bytes,read_time,write_time,addtime')\ - .order('id asc').select() - return data - -# 获取系统的CPU/IO统计信息 -@blueprint.route('/get_cpu_io', endpoint='get_cpu_io', methods=['GET']) -@panel_login_required -def get_cpu_io(): - start = request.args.get('start', '') - end = request.args.get('end', '') - data = mw.M('cpuio').dbPos(mw.getPanelDataDir(),'system')\ - .where("addtime>=? AND addtime<=?",(start, end))\ - .field('id,pro,mem,addtime')\ - .order('id asc').select() - # return self.toAddtime(data) - # print(data) - return data - - -# 获取系统网络IO统计信息 -@blueprint.route('/get_network_io', endpoint='get_network_io', methods=['GET']) -@panel_login_required -def get_network_io(): - start = request.args.get('start', '') - end = request.args.get('end', '') - data = mw.M('network').dbPos(mw.getPanelDataDir(),'system')\ - .where("addtime>=? AND addtime<=?", (start, end))\ - .field('id,up,down,total_up,total_down,down_packets,up_packets,addtime')\ - .order('id asc').select() - # return self.toAddtime(data) - # print(data) - return data - -# 获取系统网络IO统计信息 -@blueprint.route('/set_control', endpoint='set_control', methods=['POST']) -@panel_login_required -def set_control(): - stype = request.form.get('type', '') - day = request.form.get('day', '') - - if stype == '0': - option.setOption('monitor_status', 'close', type='monitor') - return mw.returnData(True, "设置成功!") - elif stype == '1': - _day = int(day) - if _day < 1: - return mw.returnData(False, "设置失败!") - option.setOption('monitor_day', day, type='monitor') - return mw.returnData(True, "设置成功!") - elif stype == '2': - option.setOption('monitor_only_netio', 'close', type='monitor') - return mw.returnData(True, "设置成功!") - elif stype == '3': - option.setOption('monitor_only_netio', 'open', type='monitor') - return mw.returnData(True, "设置成功!") - elif stype == 'del': - if not mw.isRestart(): - return mw.returnData(False, '请等待所有安装任务完成再执行') - os.remove("data/system.db") - - sql = db.Sql().dbfile('system') - csql = mw.readFile('data/sql/system.sql') - csql_list = csql.split(';') - for index in range(len(csql_list)): - sql.execute(csql_list[index], ()) - return mw.returnData(True, "监控服务已关闭") - else: - monitor_status = option.getOption('monitor_status', default='open', type='monitor') - monitor_day = option.getOption('monitor_day', default='30', type='monitor') - monitor_only_netio = option.getOption('monitor_only_netio', default='open', type='monitor') - data = {} - data['day'] = monitor_day - if monitor_status == 'open': - data['status'] = True - else: - data['status'] = False - if monitor_only_netio == 'open': - data['stat_all_status'] = True - else: - data['stat_all_status'] = False - - return data - - return mw.returnData(False, "异常!") - - - - -# 升级检测 -@blueprint.route('/update_server', endpoint='update_server') -@panel_login_required -def update_server(): - panel_type = request.args.get('type', 'check') - version = request.args.get('version', '') - - return mw.returnData(False, '已经是最新,无需更新!') - - +from .system import * +from .upgrade import * diff --git a/web/admin/system/system.py b/web/admin/system/system.py new file mode 100644 index 000000000..cd6e8a578 --- /dev/null +++ b/web/admin/system/system.py @@ -0,0 +1,158 @@ +# coding:utf-8 + +# --------------------------------------------------------------------------------- +# MW-Linux面板 +# --------------------------------------------------------------------------------- +# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved. +# --------------------------------------------------------------------------------- +# Author: midoks +# --------------------------------------------------------------------------------- + + +from flask import Blueprint, render_template +from flask import request + +from admin.user_login_check import panel_login_required +import admin.model.option as option + +import core.mw as mw +import utils.system as sys + + +blueprint = Blueprint('system', __name__, url_prefix='/system', template_folder='../../templates') + +# 获取系统的统计信息 +@blueprint.route('/system_total', endpoint='system_total') +@panel_login_required +def system_total(): + data = sys.getMemInfo() + cpu = sys.getCpuInfo(interval=1) + data['cpuNum'] = cpu[1] + data['cpuRealUsed'] = cpu[0] + data['time'] = sys.getBootTime() + data['system'] = sys.getSystemVersion() + data['version'] = '0.0.1' + return data + +# 获取系统的网络流量信息 +@blueprint.route('/network', endpoint='network') +@panel_login_required +def network(): + stat = {} + stat['cpu'] = sys.getCpuInfo() + stat['load'] = sys.getLoadAverage() + stat['mem'] = sys.getMemInfo() + stat['iostat'] = sys.stats().disk() + stat['network'] = sys.stats().network() + return stat + +# 获取系统的磁盘信息 +@blueprint.route('/disk_info', endpoint='disk_info') +@panel_login_required +def disk_info(): + data = sys.getDiskInfo() + return data + +# 获取系统的负载统计信息 +@blueprint.route('/get_load_average', endpoint='get_load_average', methods=['GET']) +@panel_login_required +def get_load_average(): + start = request.args.get('start', '') + end = request.args.get('end', '') + return sys.getLoadAverageByDB(start, end) + +# 获取系统的磁盘IO统计信息 +@blueprint.route('/get_disk_io', endpoint='get_disk_io', methods=['GET']) +@panel_login_required +def get_disk_io(): + start = request.args.get('start', '') + end = request.args.get('end', '') + return sys.getDiskIoByDB(start, end) + +# 获取系统的CPU/IO统计信息 +@blueprint.route('/get_cpu_io', endpoint='get_cpu_io', methods=['GET']) +@panel_login_required +def get_cpu_io(): + start = request.args.get('start', '') + end = request.args.get('end', '') + return sys.getCpuIoByDB(start, end) + +# 获取系统网络IO统计信息 +@blueprint.route('/get_network_io', endpoint='get_network_io', methods=['GET']) +@panel_login_required +def get_network_io(): + start = request.args.get('start', '') + end = request.args.get('end', '') + return sys.getNetworkIoByDB(start, end) + +# 重启面板 +@blueprint.route('/restart', endpoint='restart', methods=['POST']) +@panel_login_required +def restart(): + mw.restartMw() + return mw.returnData(True, '面板已重启!') + +# 重启面板 +@blueprint.route('/restart_server', endpoint='restart_server', methods=['POST']) +@panel_login_required +def restart_server(): + mw.restartMw() + return mw.returnData(True, '面板已重启!') + + + +# 设置 +@blueprint.route('/set_control', endpoint='set_control', methods=['POST']) +@panel_login_required +def set_control(): + stype = request.form.get('type', '') + day = request.form.get('day', '') + + if stype == '0': + option.setOption('monitor_status', 'close', type='monitor') + return mw.returnData(True, "设置成功!") + elif stype == '1': + _day = int(day) + if _day < 1: + return mw.returnData(False, "设置失败!") + option.setOption('monitor_day', day, type='monitor') + return mw.returnData(True, "设置成功!") + elif stype == '2': + option.setOption('monitor_only_netio', 'close', type='monitor') + return mw.returnData(True, "设置成功!") + elif stype == '3': + option.setOption('monitor_only_netio', 'open', type='monitor') + return mw.returnData(True, "设置成功!") + elif stype == 'del': + if not mw.isRestart(): + return mw.returnData(False, '请等待所有安装任务完成再执行') + os.remove("data/system.db") + + sql = db.Sql().dbfile('system') + csql = mw.readFile('data/sql/system.sql') + csql_list = csql.split(';') + for index in range(len(csql_list)): + sql.execute(csql_list[index], ()) + return mw.returnData(True, "监控服务已关闭") + else: + monitor_status = option.getOption('monitor_status', default='open', type='monitor') + monitor_day = option.getOption('monitor_day', default='30', type='monitor') + monitor_only_netio = option.getOption('monitor_only_netio', default='open', type='monitor') + data = {} + data['day'] = monitor_day + if monitor_status == 'open': + data['status'] = True + else: + data['status'] = False + if monitor_only_netio == 'open': + data['stat_all_status'] = True + else: + data['stat_all_status'] = False + + return data + + return mw.returnData(False, "异常!") + + + + diff --git a/web/admin/system/upgrade.py b/web/admin/system/upgrade.py new file mode 100644 index 000000000..82a00684d --- /dev/null +++ b/web/admin/system/upgrade.py @@ -0,0 +1,34 @@ +# coding:utf-8 + +# --------------------------------------------------------------------------------- +# MW-Linux面板 +# --------------------------------------------------------------------------------- +# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved. +# --------------------------------------------------------------------------------- +# Author: midoks +# --------------------------------------------------------------------------------- + + +from flask import Blueprint, render_template +from flask import request + +from admin.user_login_check import panel_login_required + +from admin import model + +import core.mw as mw +import utils.system as sys + +from .system import blueprint + +# 升级检测 +@blueprint.route('/update_server', endpoint='update_server') +@panel_login_required +def update_server(): + panel_type = request.args.get('type', 'check') + version = request.args.get('version', '') + + return mw.returnData(False, '已经是最新,无需更新!') + + + diff --git a/web/core/mw.py b/web/core/mw.py index 76693f045..94456abfb 100644 --- a/web/core/mw.py +++ b/web/core/mw.py @@ -487,8 +487,9 @@ def getPageObject(args, result='1,2,3,4,5,8'): def getHostPort(): - if os.path.exists('data/port.pl'): - return readFile('data/port.pl').strip() + port_file = getPanelDir() + '/data/port.pl' + if os.path.exists(port_file): + return readFile(port_file).strip() return '7200' @@ -627,6 +628,11 @@ def deDoubleCrypt(key, strings): # ------------------------------ openresty start ----------------------------- +def restartMw(): + restart_file = getPanelDir()+'/data/restart.pl' + writeFile(restart_file, 'True') + return True + def restartWeb(): return opWeb("reload") diff --git a/web/setting.py b/web/setting.py index 1d6961488..d914c71a9 100755 --- a/web/setting.py +++ b/web/setting.py @@ -20,9 +20,8 @@ import os import core.mw as mw - - import utils.system as system + cpu_info = system.getCpuInfo() workers = cpu_info[1] @@ -35,8 +34,9 @@ if not os.path.exists(log_dir): # default port mw_port = '7200' -if os.path.exists(panel_dir+'/data/port.pl'): - mw_port = mw.readFile(panel_dir+'/data/port.pl') +default_port_file = panel_dir+'/data/port.pl' +if os.path.exists(default_port_file): + mw_port = mw.readFile(default_port_file) mw_port.strip() # else: # import firewall_api @@ -47,21 +47,21 @@ if os.path.exists(panel_dir+'/data/port.pl'): # mw.writeFile('data/port.pl', mw_port) bind = [] -if os.path.exists('data/ipv6.pl'): +default_ipv6_file = panel_dir+'/data/ipv6.pl' +if os.path.exists(default_ipv6_file): bind.append('[0:0:0:0:0:0:0:0]:%s' % mw_port) else: bind.append('0.0.0.0:%s' % mw_port) -print(mw_port) if workers > 2: - workers = 2 + workers = 1 threads = workers * 1 backlog = 512 reload = False daemon = True # worker_class = 'geventwebsocket.gunicorn.workers.GeventWebSocketWorker' -timeout = 7200 +timeout = 600 keepalive = 60 preload_app = True capture_output = True diff --git a/web/templates/default/layout.html b/web/templates/default/layout.html index bf99ad9ec..b02f7540a 100755 --- a/web/templates/default/layout.html +++ b/web/templates/default/layout.html @@ -41,7 +41,7 @@
@@ -137,26 +137,18 @@ $('.panel_ad_list_mode').click(function(){ }); -function getUrlPath(){ - var thisUrl = window.location.pathname; - console.log(thisUrl); - var pathArr = thisUrl.split('/'); - return pathArr[pathArr.length - 1]; -} -var thisPath = getUrlPath(); -// console.log(thisPath); +var thisPath = window.location.pathname.trim()+window.location.search; if (thisPath.indexOf('?')>-1){ var tag = $.getUrlParam('tag'); if(tag != undefined){ $('#memuAplugins_'+tag).css('background-color','#2c3138'); - } else { - $('#memuA').css('background-color','#f2f2f2'); } } else{ - if(thisPath != undefined){ - $('#memuA'+thisPath).css('background-color','#2c3138'); - } else { - $('#memuA').css('background-color','#f2f2f2'); + if(thisPath != '/'){ + var pathArr = thisPath.substring(1).split('/'); + $('#memuA'+pathArr[0]).css('background-color','#2c3138'); + } else{ + $('#memuA').css('background-color','#2c3138'); } } diff --git a/web/utils/firewall.py b/web/utils/firewall.py index 086f14d78..509b17ae6 100644 --- a/web/utils/firewall.py +++ b/web/utils/firewall.py @@ -11,6 +11,10 @@ import os import re import threading +import re +import time + +from admin import model import core.mw as mw @@ -43,6 +47,18 @@ class Firewall(object): elif mw.isAppleSystem(): self.__isMac = True + def reload(self): + if self.__isUfw: + mw.execShell('/usr/sbin/ufw reload') + return + elif self.__isIptables: + mw.execShell('service iptables save') + mw.execShell('service iptables restart') + elif self.__isFirewalld: + mw.execShell('firewall-cmd --reload') + else: + pass + def getFwStatus(self): if self.__isUfw: cmd = "/usr/sbin/ufw status| grep Status | awk -F ':' '{print $2}'" @@ -134,4 +150,94 @@ class Firewall(object): mw.writeFile(filename, conf) mw.execShell('sysctl -p') - return mw.returnData(True, '设置成功!') \ No newline at end of file + return mw.returnData(True, '设置成功!') + + def setFw(self, status): + if self.__isIptables: + self.setFwIptables(status) + return mw.returnData(True, '设置成功!') + + if status == '1': + if self.__isUfw: + mw.execShell('/usr/sbin/ufw disable') + elif self.__isFirewalld: + mw.execShell('systemctl stop firewalld.service') + mw.execShell('systemctl disable firewalld.service') + else: + pass + else: + if self.__isUfw: + mw.execShell("echo 'y'| ufw enable") + elif self.__isFirewalld: + mw.execShell('systemctl start firewalld.service') + mw.execShell('systemctl enable firewalld.service') + else: + pass + return mw.returnData(True, '设置成功!') + + def addAcceptPortCmd(self, port, + protocol:str | None ='tcp' + ): + if self.__isUfw: + if protocol == 'tcp': + mw.execShell('ufw allow ' + port + '/tcp') + if protocol == 'udp': + mw.execShell('ufw allow ' + port + '/udp') + if protocol == 'tcp/udp': + mw.execShell('ufw allow ' + port + '/tcp') + mw.execShell('ufw allow ' + port + '/udp') + elif self.__isFirewalld: + port = port.replace(':', '-') + if protocol == 'tcp': + cmd = 'firewall-cmd --permanent --zone=public --add-port=' + port + '/tcp' + mw.execShell(cmd) + if protocol == 'udp': + cmd = 'firewall-cmd --permanent --zone=public --add-port=' + port + '/udp' + mw.execShell(cmd) + if protocol == 'tcp/udp': + cmd = 'firewall-cmd --permanent --zone=public --add-port=' + port + '/tcp' + mw.execShell(cmd) + cmd = 'firewall-cmd --permanent --zone=public --add-port=' + port + '/udp' + mw.execShell(cmd) + elif self.__isIptables: + if protocol == 'tcp': + cmd = 'iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport ' + port + ' -j ACCEPT' + mw.execShell(cmd) + if protocol == 'udp': + cmd = 'iptables -I INPUT -p udp -m state --state NEW -m udp --dport ' + port + ' -j ACCEPT' + mw.execShell(cmd) + if protocol == 'tcp/udp': + cmd = 'iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport ' + port + ' -j ACCEPT' + mw.execShell(cmd) + cmd = 'iptables -I INPUT -p udp -m state --state NEW -m udp --dport ' + port + ' -j ACCEPT' + mw.execShell(cmd) + else: + pass + return True + + # 添加放行端口 + def addAcceptPort(self, port, ps, stype, + protocol: str | None ='tcp' + ): + if not self.getFwStatus(): + self.setFw(0) + + + rep = r"^\d{1,5}(:\d{1,5})?$" + if not re.search(rep, port): + return mw.returnData(False, '端口范围不正确!') + + if model.getFirewallCountByPort(port) > 0: + return mw.returnData(False, '您要放行的端口已存在,无需重复放行!') + + model.addFirewall(port,ps=ps,protocol=protocol) + self.addAcceptPortCmd(port, protocol=protocol) + self.reload() + + msg = mw.getInfo('放行端口[{1}][{2}]成功', (port, protocol,)) + mw.writeLog("防火墙管理", msg) + + return mw.returnData(True, '添加放行(' + port + ')端口成功!') + + + diff --git a/web/utils/system/__init__.py b/web/utils/system/__init__.py index de3430715..7f612c99c 100644 --- a/web/utils/system/__init__.py +++ b/web/utils/system/__init__.py @@ -8,286 +8,7 @@ # Author: midoks # --------------------------------------------------------------------------------- -import os -import sys -import re -import time -import math -import psutil - - -import core.mw as mw - -def getDiskInfo(): - # 取磁盘分区信息 - temp = mw.execShell("df -h -P|grep '/'|grep -v tmpfs | grep -v devfs")[0] - tempInodes = mw.execShell("df -i -P|grep '/'|grep -v tmpfs | grep -v devfs")[0] - temp1 = temp.split('\n') - tempInodes1 = tempInodes.split('\n') - diskInfo = [] - n = 0 - cuts = ['/mnt/cdrom', '/boot', '/boot/efi', '/dev', - '/dev/shm', '/zroot', '/run/lock', '/run', '/run/shm', '/run/user'] - for tmp in temp1: - n += 1 - inodes = tempInodes1[n - 1].split() - disk = tmp.split() - if len(disk) < 5: - continue - if disk[1].find('M') != -1: - continue - if disk[1].find('K') != -1: - continue - if len(disk[5].split('/')) > 4: - continue - if disk[5] in cuts: - continue - arr = {} - arr['path'] = disk[5] - tmp1 = [disk[1], disk[2], disk[3], disk[4]] - arr['size'] = tmp1 - arr['inodes'] = [inodes[1], inodes[2], inodes[3], inodes[4]] - diskInfo.append(arr) - return diskInfo - -class stats: - cache = {} - - # 磁盘统计 - def disk(self): - iokey = 'disk_stat' - diskInfo = {} - diskInfo['ALL'] = {} - diskInfo['ALL']['read_count'] = 0 - diskInfo['ALL']['write_count'] = 0 - diskInfo['ALL']['read_bytes'] = 0 - diskInfo['ALL']['write_bytes'] = 0 - diskInfo['ALL']['read_time'] = 0 - diskInfo['ALL']['write_time'] = 0 - diskInfo['ALL']['read_merged_count'] = 0 - diskInfo['ALL']['write_merged_count'] = 0 - - try: - diskio = None - if iokey in self.cache: - diskio = self.cache[iokey] - - mtime = int(time.time()) - if not diskio: - diskio = {} - diskio['info'] = None - diskio['time'] = mtime - - diskio_cache = diskio['info'] - stime = mtime - diskio['time'] - if not stime: stime = 1 - - diskio_group = psutil.disk_io_counters(perdisk=True) - if not diskio_cache: - diskio_cache = diskio_group - - for disk_name in diskio_group.keys(): - diskInfo[disk_name] = {} - # print('disk_name',disk_name) - # print(diskio_group[disk_name].write_time , diskio_cache[disk_name].write_time) - # print(diskio_group[disk_name].write_count , diskio_cache[disk_name].write_count) - - diskInfo[disk_name]['read_count'] = int((diskio_group[disk_name].read_count - diskio_cache[disk_name].read_count) / stime) - diskInfo[disk_name]['write_count'] = int((diskio_group[disk_name].write_count - diskio_cache[disk_name].write_count) / stime) - diskInfo[disk_name]['read_bytes'] = int((diskio_group[disk_name].read_bytes - diskio_cache[disk_name].read_bytes) / stime) - diskInfo[disk_name]['write_bytes'] = int((diskio_group[disk_name].write_bytes - diskio_cache[disk_name].write_bytes) / stime) - diskInfo[disk_name]['read_time'] = int((diskio_group[disk_name].read_time - diskio_cache[disk_name].read_time) / stime) - diskInfo[disk_name]['write_time'] = int((diskio_group[disk_name].write_time - diskio_cache[disk_name].write_time) / stime) - - if 'read_merged_count' in diskio_group[disk_name] and 'read_merged_count' in diskio_cache[disk_name]: - diskInfo[disk_name]['read_merged_count'] = int((diskio_group[disk_name].read_merged_count - diskio_cache[disk_name].read_merged_count) / stime) - if 'write_merged_count' in diskio_group[disk_name] and 'write_merged_count' in diskio_cache[disk_name]: - diskInfo[disk_name]['write_merged_count'] = int((diskio_group[disk_name].write_merged_count - diskio_cache[disk_name].write_merged_count) / stime) - - diskInfo['ALL']['read_count'] += diskInfo[disk_name]['read_count'] - diskInfo['ALL']['write_count'] += diskInfo[disk_name]['write_count'] - diskInfo['ALL']['read_bytes'] += diskInfo[disk_name]['read_bytes'] - diskInfo['ALL']['write_bytes'] += diskInfo[disk_name]['write_bytes'] - if diskInfo['ALL']['read_time'] < diskInfo[disk_name]['read_time']: - diskInfo['ALL']['read_time'] = diskInfo[disk_name]['read_time'] - if diskInfo['ALL']['write_time'] < diskInfo[disk_name]['write_time']: - diskInfo['ALL']['write_time'] = diskInfo[disk_name]['write_time'] - - if 'read_merged_count' in diskInfo[disk_name] and 'read_merged_count' in diskInfo[disk_name]: - diskInfo['ALL']['read_merged_count'] += diskInfo[disk_name]['read_merged_count'] - if 'write_merged_count' in diskInfo[disk_name] and 'write_merged_count' in diskInfo[disk_name]: - diskInfo['ALL']['write_merged_count'] += diskInfo[disk_name]['write_merged_count'] - - self.cache[iokey] = {'info':diskio_group,'time':mtime} - except Exception as e: - pass - - return diskInfo - - # 网络统计 - def network(self): - netInfo = {} - - netInfo['ALL'] = {} - netInfo['ALL']['up'] = 0 - netInfo['ALL']['down'] = 0 - netInfo['ALL']['upTotal'] = 0 - netInfo['ALL']['downTotal'] = 0 - netInfo['ALL']['upPackets'] = 0 - netInfo['ALL']['downPackets'] = 0 - - mtime = time.time() - iokey = 'net_stat' - netio = None - if iokey in self.cache: - netio = self.cache[iokey] - - if not netio: - netio = {} - netio['info'] = None - netio['all_io'] = None - netio['time'] = mtime - - stime = mtime - netio['time'] - if not stime: stime = 1 - - # print("new:",stime) - netio_group = psutil.net_io_counters(pernic=True).keys() - - netio_cache = netio['info'] - allio_cache = netio['all_io'] - if not netio_cache: - netio_cache = {} - - netio_group_t = {} - for name in netio_group: - netInfo[name] = {} - - io_data = psutil.net_io_counters(pernic=True).get(name) - if not name in netio_cache: - netio_cache[name] = io_data - - netio_group_t[name] = io_data - - netInfo[name]['up'] = round(float((io_data[0] - netio_cache[name][0]) / stime), 2) - netInfo[name]['down'] = round(float((io_data[1] - netio_cache[name][1])/ stime), 2) - - netInfo[name]['upTotal'] = io_data[0] - netInfo[name]['downTotal'] = io_data[1] - netInfo[name]['upPackets'] = io_data[2] - netInfo[name]['downPackets'] = io_data[3] - - all_io = psutil.net_io_counters()[:4] - if not allio_cache: - allio_cache = all_io - - netInfo['ALL']['up'] = round(float((all_io[0] - allio_cache[0]) /stime), 2) - netInfo['ALL']['down'] = round(float((all_io[1] - allio_cache[1]) /stime), 2) - netInfo['ALL']['upTotal'] = all_io[0] - netInfo['ALL']['downTotal'] = all_io[1] - netInfo['ALL']['upPackets'] = all_io[2] - netInfo['ALL']['downPackets'] = all_io[3] - - self.cache[iokey] = {'info':netio_group_t,'all_io':all_io,'time':mtime} - return netInfo - - - -def getLoadAverage(): - c = os.getloadavg() - data = {} - data['one'] = round(float(c[0]), 2) - data['five'] = round(float(c[1]), 2) - data['fifteen'] = round(float(c[2]), 2) - data['max'] = psutil.cpu_count() * 2 - data['limit'] = data['max'] - data['safe'] = data['max'] * 0.75 - return data - -def getSystemVersion(): - # 取操作系统版本 - current_os = mw.getOs() - # sys_temper = self.getSystemDeviceTemperature() - # print(sys_temper) - # mac - if current_os == 'darwin': - data = mw.execShell('sw_vers')[0] - data_list = data.strip().split("\n") - mac_version = '' - for x in data_list: - xlist = x.split("\t") - mac_version += xlist[len(xlist)-1] + ' ' - - arch_ver = mw.execShell("arch") - return mac_version + " (" + arch_ver[0].strip() + ")" - - # freebsd - if current_os.startswith('freebsd'): - version = mw.execShell( - "cat /etc/*-release | grep PRETTY_NAME | awk -F = '{print $2}' | awk -F '\"' '{print $2}'") - arch_ver = mw.execShell( - "sysctl -a | egrep -i 'hw.machine_arch' | awk -F ':' '{print $2}'") - return version[0].strip() + " (" + arch_ver[0].strip() + ")" - - redhat_series = '/etc/redhat-release' - if os.path.exists(redhat_series): - version = mw.readFile('/etc/redhat-release') - version = version.replace('release ', '').strip() - - arch_ver = mw.execShell("arch") - return version + " (" + arch_ver[0].strip() + ")" - - os_series = '/etc/os-release' - if os.path.exists(os_series): - version = mw.execShell( - "cat /etc/*-release | grep PRETTY_NAME | awk -F = '{print $2}' | awk -F '\"' '{print $2}'") - - arch_ver = mw.execShell("arch") - return version[0].strip() + " (" + arch_ver[0].strip() + ")" - -def getBootTime(): - # 取系统启动时间 - uptime = mw.readFile('/proc/uptime') - if uptime == False: - start_time = psutil.boot_time() - run_time = time.time() - start_time - else: - run_time = uptime.split()[0] - tStr = float(run_time) - min = tStr / 60 - hours = min / 60 - days = math.floor(hours / 24) - hours = math.floor(hours - (days * 24)) - min = math.floor(min - (days * 60 * 24) - (hours * 60)) - return mw.getInfo('已不间断运行: {1}天{2}小时{3}分钟', (str(int(days)), str(int(hours)), str(int(min)))) - -def getCpuInfo(interval=1): - # 取CPU信息 - cpuCount = psutil.cpu_count() - cpuLogicalNum = psutil.cpu_count(logical=False) - used = psutil.cpu_percent(interval=interval) - cpuLogicalNum = 0 - if os.path.exists('/proc/cpuinfo'): - c_tmp = mw.readFile('/proc/cpuinfo') - d_tmp = re.findall("physical id.+", c_tmp) - cpuLogicalNum = len(set(d_tmp)) - - used_all = psutil.cpu_percent(percpu=True) - cpu_name = mw.getCpuType() + " * {}".format(cpuLogicalNum) - return used, cpuCount, used_all, cpu_name, cpuCount, cpuLogicalNum - -def getMemInfo(): - # 取内存信息 - mem = psutil.virtual_memory() - if sys.platform == 'darwin': - memInfo = {'memTotal': mem.total} - memInfo['memRealUsed'] = memInfo['memTotal'] * (mem.percent / 100) - else: - memInfo = { - 'memTotal': mem.total, - 'memFree': mem.free, - 'memBuffers': mem.buffers, - 'memCached': mem.cached - } - memInfo['memRealUsed'] = memInfo['memTotal'] - memInfo['memFree'] - memInfo['memBuffers'] - memInfo['memCached'] - return memInfo \ No newline at end of file +from .main import * +from .query import * +from .stats import * +from .monitor import * \ No newline at end of file diff --git a/web/utils/system/main.py b/web/utils/system/main.py new file mode 100644 index 000000000..4e7b8a7b4 --- /dev/null +++ b/web/utils/system/main.py @@ -0,0 +1,153 @@ +# coding:utf-8 + +# --------------------------------------------------------------------------------- +# MW-Linux面板 +# --------------------------------------------------------------------------------- +# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved. +# --------------------------------------------------------------------------------- +# Author: midoks +# --------------------------------------------------------------------------------- + +import os +import sys +import re +import time +import math +import psutil + + +import core.mw as mw + +def getDiskInfo(): + # 取磁盘分区信息 + temp = mw.execShell("df -h -P|grep '/'|grep -v tmpfs | grep -v devfs")[0] + tempInodes = mw.execShell("df -i -P|grep '/'|grep -v tmpfs | grep -v devfs")[0] + temp1 = temp.split('\n') + tempInodes1 = tempInodes.split('\n') + diskInfo = [] + n = 0 + cuts = ['/mnt/cdrom', '/boot', '/boot/efi', '/dev', + '/dev/shm', '/zroot', '/run/lock', '/run', '/run/shm', '/run/user'] + for tmp in temp1: + n += 1 + inodes = tempInodes1[n - 1].split() + disk = tmp.split() + if len(disk) < 5: + continue + if disk[1].find('M') != -1: + continue + if disk[1].find('K') != -1: + continue + if len(disk[5].split('/')) > 4: + continue + if disk[5] in cuts: + continue + arr = {} + arr['path'] = disk[5] + tmp1 = [disk[1], disk[2], disk[3], disk[4]] + arr['size'] = tmp1 + arr['inodes'] = [inodes[1], inodes[2], inodes[3], inodes[4]] + diskInfo.append(arr) + return diskInfo + + +def getLoadAverage(): + c = os.getloadavg() + data = {} + data['one'] = round(float(c[0]), 2) + data['five'] = round(float(c[1]), 2) + data['fifteen'] = round(float(c[2]), 2) + data['max'] = psutil.cpu_count() * 2 + data['limit'] = data['max'] + data['safe'] = data['max'] * 0.75 + return data + +def getSystemVersion(): + # 取操作系统版本 + current_os = mw.getOs() + # sys_temper = self.getSystemDeviceTemperature() + # print(sys_temper) + # mac + if current_os == 'darwin': + data = mw.execShell('sw_vers')[0] + data_list = data.strip().split("\n") + mac_version = '' + for x in data_list: + xlist = x.split("\t") + mac_version += xlist[len(xlist)-1] + ' ' + + arch_ver = mw.execShell("arch") + return mac_version + " (" + arch_ver[0].strip() + ")" + + # freebsd + if current_os.startswith('freebsd'): + version = mw.execShell( + "cat /etc/*-release | grep PRETTY_NAME | awk -F = '{print $2}' | awk -F '\"' '{print $2}'") + arch_ver = mw.execShell( + "sysctl -a | egrep -i 'hw.machine_arch' | awk -F ':' '{print $2}'") + return version[0].strip() + " (" + arch_ver[0].strip() + ")" + + redhat_series = '/etc/redhat-release' + if os.path.exists(redhat_series): + version = mw.readFile('/etc/redhat-release') + version = version.replace('release ', '').strip() + + arch_ver = mw.execShell("arch") + return version + " (" + arch_ver[0].strip() + ")" + + os_series = '/etc/os-release' + if os.path.exists(os_series): + version = mw.execShell( + "cat /etc/*-release | grep PRETTY_NAME | awk -F = '{print $2}' | awk -F '\"' '{print $2}'") + + arch_ver = mw.execShell("arch") + return version[0].strip() + " (" + arch_ver[0].strip() + ")" + +def getBootTime(): + # 取系统启动时间 + uptime = mw.readFile('/proc/uptime') + if uptime == False: + start_time = psutil.boot_time() + run_time = time.time() - start_time + else: + run_time = uptime.split()[0] + tStr = float(run_time) + min = tStr / 60 + hours = min / 60 + days = math.floor(hours / 24) + hours = math.floor(hours - (days * 24)) + min = math.floor(min - (days * 60 * 24) - (hours * 60)) + return mw.getInfo('已不间断运行: {1}天{2}小时{3}分钟', (str(int(days)), str(int(hours)), str(int(min)))) + +def getCpuInfo(interval=1): + # 取CPU信息 + cpuCount = psutil.cpu_count() + cpuLogicalNum = psutil.cpu_count(logical=False) + used = psutil.cpu_percent(interval=interval) + cpuLogicalNum = 0 + if os.path.exists('/proc/cpuinfo'): + c_tmp = mw.readFile('/proc/cpuinfo') + d_tmp = re.findall("physical id.+", c_tmp) + cpuLogicalNum = len(set(d_tmp)) + + used_all = psutil.cpu_percent(percpu=True) + cpu_name = mw.getCpuType() + " * {}".format(cpuLogicalNum) + return used, cpuCount, used_all, cpu_name, cpuCount, cpuLogicalNum + +def getMemInfo(): + # 取内存信息 + mem = psutil.virtual_memory() + if sys.platform == 'darwin': + memInfo = {'memTotal': mem.total} + memInfo['memRealUsed'] = memInfo['memTotal'] * (mem.percent / 100) + else: + memInfo = { + 'memTotal': mem.total, + 'memFree': mem.free, + 'memBuffers': mem.buffers, + 'memCached': mem.cached + } + memInfo['memRealUsed'] = memInfo['memTotal'] - memInfo['memFree'] - memInfo['memBuffers'] - memInfo['memCached'] + return memInfo + + diff --git a/web/utils/system/monitor.py b/web/utils/system/monitor.py new file mode 100644 index 000000000..2f33b438a --- /dev/null +++ b/web/utils/system/monitor.py @@ -0,0 +1,21 @@ +# coding:utf-8 + +# --------------------------------------------------------------------------------- +# MW-Linux面板 +# --------------------------------------------------------------------------------- +# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved. +# --------------------------------------------------------------------------------- +# Author: midoks +# --------------------------------------------------------------------------------- + +import os +import sys +import re +import time +import math +import psutil + +import core.mw as mw + + + diff --git a/web/utils/system/query.py b/web/utils/system/query.py new file mode 100644 index 000000000..9d2710070 --- /dev/null +++ b/web/utils/system/query.py @@ -0,0 +1,103 @@ +# coding:utf-8 + +# --------------------------------------------------------------------------------- +# MW-Linux面板 +# --------------------------------------------------------------------------------- +# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved. +# --------------------------------------------------------------------------------- +# Author: midoks +# --------------------------------------------------------------------------------- + +import os +import sys +import re +import time +import math +import psutil + + +import core.mw as mw + +# -------------------------------------------- 数据查询相关 -------------------------------------------- +# 格式化addtime列 +def toAddtime(data, tomem=False): + import time + if tomem: + import psutil + mPre = (psutil.virtual_memory().total / 1024 / 1024) / 100 + length = len(data) + he = 1 + if length > 100: + he = 1 + if length > 1000: + he = 3 + if length > 10000: + he = 15 + if he == 1: + for i in range(length): + data[i]['addtime'] = time.strftime( + '%m/%d %H:%M', time.localtime(float(data[i]['addtime']))) + if tomem and data[i]['mem'] > 100: + data[i]['mem'] = data[i]['mem'] / mPre + + return data + else: + count = 0 + tmp = [] + for value in data: + if count < he: + count += 1 + continue + value['addtime'] = time.strftime( + '%m/%d %H:%M', time.localtime(float(value['addtime']))) + if tomem and value['mem'] > 100: + value['mem'] = value['mem'] / mPre + tmp.append(value) + count = 0 + return tmp + +# 格式化addtime列 +def toUseAddtime(data): + dlen = len(data) + for i in range(dlen): + data[i]['addtime'] = time.strftime('%m/%d %H:%M', time.localtime(float(data[i]['addtime']))) + return data + +def getLoadAverageByDB(start, end): + # 获取系统的负载统计信息 + data = mw.M('load_average').dbPos(mw.getPanelDataDir(),'system')\ + .where("addtime>=? AND addtime<=?", (start, end,))\ + .field('pro,one,five,fifteen,addtime')\ + .order('id asc').select() + data = toUseAddtime(data) + return data + +def getDiskIoByDB(start, end): + # 获取系统的磁盘IO统计信息 + data = mw.M('diskio').dbPos(mw.getPanelDataDir(),'system')\ + .where("addtime>=? AND addtime<=?", (start, end))\ + .field('read_count,write_count,read_bytes,write_bytes,read_time,write_time,addtime')\ + .order('id asc').select() + data = toUseAddtime(data) + return data + +def getCpuIoByDB(start, end): + # 获取系统的CPU/IO统计信息 + data = mw.M('cpuio').dbPos(mw.getPanelDataDir(),'system')\ + .where("addtime>=? AND addtime<=?",(start, end))\ + .field('pro,mem,addtime')\ + .order('id asc').select() + data = toUseAddtime(data) + return data + +def getNetworkIoByDB(start, end): + # 获取系统网络IO统计信息 + # id, + data = mw.M('network').dbPos(mw.getPanelDataDir(),'system')\ + .where("addtime>=? AND addtime<=?", (start, end))\ + .field('up,down,total_up,total_down,down_packets,up_packets,addtime')\ + .order('id asc').select() + data = toUseAddtime(data) + return data + +# -------------------------------------------- 数据查询相关 -------------------------------------------- diff --git a/web/utils/system/stats.py b/web/utils/system/stats.py new file mode 100644 index 000000000..123e5e80b --- /dev/null +++ b/web/utils/system/stats.py @@ -0,0 +1,161 @@ +# coding:utf-8 + +# --------------------------------------------------------------------------------- +# MW-Linux面板 +# --------------------------------------------------------------------------------- +# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved. +# --------------------------------------------------------------------------------- +# Author: midoks +# --------------------------------------------------------------------------------- + +import os +import sys +import re +import time +import math +import psutil + +import core.mw as mw + +class stats: + cache = {} + + # 磁盘统计 + def disk(self): + iokey = 'disk_stat' + diskInfo = {} + diskInfo['ALL'] = {} + diskInfo['ALL']['read_count'] = 0 + diskInfo['ALL']['write_count'] = 0 + diskInfo['ALL']['read_bytes'] = 0 + diskInfo['ALL']['write_bytes'] = 0 + diskInfo['ALL']['read_time'] = 0 + diskInfo['ALL']['write_time'] = 0 + diskInfo['ALL']['read_merged_count'] = 0 + diskInfo['ALL']['write_merged_count'] = 0 + + try: + diskio = None + if iokey in self.cache: + diskio = self.cache[iokey] + + mtime = int(time.time()) + if not diskio: + diskio = {} + diskio['info'] = None + diskio['time'] = mtime + + diskio_cache = diskio['info'] + stime = mtime - diskio['time'] + if not stime: stime = 1 + + diskio_group = psutil.disk_io_counters(perdisk=True) + if not diskio_cache: + diskio_cache = diskio_group + + for disk_name in diskio_group.keys(): + diskInfo[disk_name] = {} + # print('disk_name',disk_name) + # print(diskio_group[disk_name].write_time , diskio_cache[disk_name].write_time) + # print(diskio_group[disk_name].write_count , diskio_cache[disk_name].write_count) + + diskInfo[disk_name]['read_count'] = int((diskio_group[disk_name].read_count - diskio_cache[disk_name].read_count) / stime) + diskInfo[disk_name]['write_count'] = int((diskio_group[disk_name].write_count - diskio_cache[disk_name].write_count) / stime) + diskInfo[disk_name]['read_bytes'] = int((diskio_group[disk_name].read_bytes - diskio_cache[disk_name].read_bytes) / stime) + diskInfo[disk_name]['write_bytes'] = int((diskio_group[disk_name].write_bytes - diskio_cache[disk_name].write_bytes) / stime) + diskInfo[disk_name]['read_time'] = int((diskio_group[disk_name].read_time - diskio_cache[disk_name].read_time) / stime) + diskInfo[disk_name]['write_time'] = int((diskio_group[disk_name].write_time - diskio_cache[disk_name].write_time) / stime) + + if 'read_merged_count' in diskio_group[disk_name] and 'read_merged_count' in diskio_cache[disk_name]: + diskInfo[disk_name]['read_merged_count'] = int((diskio_group[disk_name].read_merged_count - diskio_cache[disk_name].read_merged_count) / stime) + if 'write_merged_count' in diskio_group[disk_name] and 'write_merged_count' in diskio_cache[disk_name]: + diskInfo[disk_name]['write_merged_count'] = int((diskio_group[disk_name].write_merged_count - diskio_cache[disk_name].write_merged_count) / stime) + + diskInfo['ALL']['read_count'] += diskInfo[disk_name]['read_count'] + diskInfo['ALL']['write_count'] += diskInfo[disk_name]['write_count'] + diskInfo['ALL']['read_bytes'] += diskInfo[disk_name]['read_bytes'] + diskInfo['ALL']['write_bytes'] += diskInfo[disk_name]['write_bytes'] + if diskInfo['ALL']['read_time'] < diskInfo[disk_name]['read_time']: + diskInfo['ALL']['read_time'] = diskInfo[disk_name]['read_time'] + if diskInfo['ALL']['write_time'] < diskInfo[disk_name]['write_time']: + diskInfo['ALL']['write_time'] = diskInfo[disk_name]['write_time'] + + if 'read_merged_count' in diskInfo[disk_name] and 'read_merged_count' in diskInfo[disk_name]: + diskInfo['ALL']['read_merged_count'] += diskInfo[disk_name]['read_merged_count'] + if 'write_merged_count' in diskInfo[disk_name] and 'write_merged_count' in diskInfo[disk_name]: + diskInfo['ALL']['write_merged_count'] += diskInfo[disk_name]['write_merged_count'] + + self.cache[iokey] = {'info':diskio_group,'time':mtime} + except Exception as e: + pass + + return diskInfo + + # 网络统计 + def network(self): + netInfo = {} + + netInfo['ALL'] = {} + netInfo['ALL']['up'] = 0 + netInfo['ALL']['down'] = 0 + netInfo['ALL']['upTotal'] = 0 + netInfo['ALL']['downTotal'] = 0 + netInfo['ALL']['upPackets'] = 0 + netInfo['ALL']['downPackets'] = 0 + + mtime = time.time() + iokey = 'net_stat' + netio = None + if iokey in self.cache: + netio = self.cache[iokey] + + if not netio: + netio = {} + netio['info'] = None + netio['all_io'] = None + netio['time'] = mtime + + stime = mtime - netio['time'] + if not stime: stime = 1 + + # print("new:",stime) + netio_group = psutil.net_io_counters(pernic=True).keys() + + netio_cache = netio['info'] + allio_cache = netio['all_io'] + if not netio_cache: + netio_cache = {} + + netio_group_t = {} + for name in netio_group: + netInfo[name] = {} + + io_data = psutil.net_io_counters(pernic=True).get(name) + if not name in netio_cache: + netio_cache[name] = io_data + + netio_group_t[name] = io_data + + netInfo[name]['up'] = round(float((io_data[0] - netio_cache[name][0]) / stime), 2) + netInfo[name]['down'] = round(float((io_data[1] - netio_cache[name][1])/ stime), 2) + + netInfo[name]['upTotal'] = io_data[0] + netInfo[name]['downTotal'] = io_data[1] + netInfo[name]['upPackets'] = io_data[2] + netInfo[name]['downPackets'] = io_data[3] + + all_io = psutil.net_io_counters()[:4] + if not allio_cache: + allio_cache = all_io + + netInfo['ALL']['up'] = round(float((all_io[0] - allio_cache[0]) /stime), 2) + netInfo['ALL']['down'] = round(float((all_io[1] - allio_cache[1]) /stime), 2) + netInfo['ALL']['upTotal'] = all_io[0] + netInfo['ALL']['downTotal'] = all_io[1] + netInfo['ALL']['upPackets'] = all_io[2] + netInfo['ALL']['downPackets'] = all_io[3] + + self.cache[iokey] = {'info':netio_group_t,'all_io':all_io,'time':mtime} + return netInfo + +