Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/xhprof/index.py

255 lines
5.6 KiB

5 years ago
# coding:utf-8
import sys
import io
import os
import time
import re
5 months ago
import json
5 years ago
web_dir = os.getcwd() + "/web"
if os.path.exists(web_dir):
sys.path.append(web_dir)
os.chdir(web_dir)
import core.mw as mw
from utils.site import sites as MwSites
5 years ago
app_debug = False
if mw.isAppleSystem():
app_debug = True
def getPluginName():
return 'xhprof'
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 getConf():
return mw.getServerDir() + '/web_conf/nginx/vhost/xhprof.conf'
def getPort():
file = getConf()
content = mw.readFile(file)
10 months ago
rep = r'listen\s*(.*);'
5 years ago
tmp = re.search(rep, content)
return tmp.groups()[0].strip()
def getHomePage():
try:
port = getPort()
ip = '127.0.0.1'
if not mw.isAppleSystem():
ip = mw.getLocalIp()
url = 'http://' + ip + ':' + port + '/index.php'
return mw.returnJson(True, 'OK', url)
except Exception as e:
return mw.returnJson(False, '插件未启动!')
5 months ago
def getPhpVer(expect=74):
v = MwSites.instance().getPhpVersion()
5 years ago
for i in range(len(v)):
t = int(v[i]['version'])
if (t >= expect):
return str(t)
return str(expect)
def getCachePhpVer():
cacheFile = getServerDir() + '/php.pl'
v = ''
if os.path.exists(cacheFile):
v = mw.readFile(cacheFile)
else:
v = getPhpVer()
mw.writeFile(cacheFile, v)
return v
def contentReplace(content):
service_path = mw.getServerDir()
php_ver = getCachePhpVer()
# print php_ver
6 months ago
content = content.replace('{$ROOT_PATH}', mw.getFatherDir())
5 years ago
content = content.replace('{$SERVER_PATH}', service_path)
content = content.replace('{$PHP_VER}', php_ver)
return content
5 years ago
def contentReplacePHP(content, version):
service_path = mw.getServerDir()
# print php_ver
6 months ago
content = content.replace('{$ROOT_PATH}', mw.getFatherDir())
5 years ago
content = content.replace('{$SERVER_PATH}', service_path)
content = content.replace('{$PHP_VER}', version)
return content
5 years ago
def status():
conf = getConf()
if os.path.exists(conf):
return 'start'
return 'stop'
5 months ago
def getConfAppStart():
pstart = mw.getServerDir() + '/php/app_start.php'
return pstart
def phpPrependFile(version):
app_start = getConfAppStart()
5 months ago
tpl = mw.getPluginDir() + '/php/conf/app_start.php'
content = mw.readFile(tpl)
content = contentReplace(content, version)
mw.writeFile(app_start, content)
5 years ago
def start():
5 months ago
phpPrependFile()
5 years ago
file_tpl = getPluginDir() + '/conf/xhprof.conf'
file_run = getConf()
if not os.path.exists(file_run):
centent = mw.readFile(file_tpl)
centent = contentReplace(centent)
mw.writeFile(file_run, centent)
mw.restartWeb()
return 'ok'
def stop():
conf = getConf()
if os.path.exists(conf):
os.remove(conf)
mw.restartWeb()
return 'ok'
def restart():
return start()
def reload():
return start()
def setPhpVer():
args = getArgs()
if not 'phpver' in args:
return 'phpver missing'
cacheFile = getServerDir() + '/php.pl'
mw.writeFile(cacheFile, args['phpver'])
5 years ago
file_tpl = getPluginDir() + '/conf/xhprof.conf'
file_run = getConf()
5 years ago
centent = mw.readFile(file_tpl)
5 years ago
centent = contentReplacePHP(centent, args['phpver'])
5 years ago
mw.writeFile(file_run, centent)
5 years ago
mw.restartWeb()
5 years ago
return 'ok'
def getSetPhpVer():
cacheFile = getServerDir() + '/php.pl'
if os.path.exists(cacheFile):
return mw.readFile(cacheFile).strip()
return ''
def getXhPort():
try:
port = getPort()
return mw.returnJson(True, 'OK', port)
except Exception as e:
return mw.returnJson(False, '插件未启动!')
def setXhPort():
args = getArgs()
if not 'port' in args:
return mw.returnJson(False, 'port missing!')
port = args['port']
if port == '80':
return mw.returnJson(False, '80端不能使用!')
file = getConf()
if not os.path.exists(file):
return mw.returnJson(False, '插件未启动!')
content = mw.readFile(file)
10 months ago
rep = r'listen\s*(.*);'
5 years ago
content = re.sub(rep, "listen " + port + ';', content)
mw.writeFile(file, content)
mw.restartWeb()
return mw.returnJson(True, '修改成功!')
3 years ago
def installPreInspection():
path = mw.getServerDir() + '/php'
if not os.path.exists(path):
return "先安装一个可用的PHP版本!"
3 years ago
return 'ok'
5 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
4 years ago
print(status())
5 years ago
elif func == 'start':
4 years ago
print(start())
5 years ago
elif func == 'stop':
4 years ago
print(stop())
5 years ago
elif func == 'restart':
4 years ago
print(restart())
5 years ago
elif func == 'reload':
4 years ago
print(reload())
3 years ago
elif func == 'install_pre_inspection':
print(installPreInspection())
5 years ago
elif func == 'conf':
4 years ago
print(getConf())
5 years ago
elif func == 'get_home_page':
4 years ago
print(getHomePage())
5 years ago
elif func == 'set_php_ver':
4 years ago
print(setPhpVer())
5 years ago
elif func == 'get_set_php_ver':
4 years ago
print(getSetPhpVer())
5 years ago
elif func == 'get_xhprof_port':
4 years ago
print(getXhPort())
5 years ago
elif func == 'set_xhprof_port':
4 years ago
print(setXhPort())
5 years ago
elif func == 'app_start':
4 years ago
print(getConfAppStart())
5 years ago
else:
4 years ago
print('error')