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/memcached/index.py

282 lines
6.9 KiB

7 years ago
# coding:utf-8
import sys
import io
import os
import time
6 years ago
import re
7 years ago
sys.path.append(os.getcwd() + "/class/core")
import mw
7 years ago
7 years ago
app_debug = False
if mw.isAppleSystem():
7 years ago
app_debug = True
7 years ago
import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)
7 years ago
def getPluginName():
7 years ago
return 'memcached'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
7 years ago
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
7 years ago
def getInitDFile():
if app_debug:
7 years ago
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
7 years ago
def getConf():
6 years ago
path = getServerDir() + "/init.d/memcached"
return path
3 years ago
def getConfEnv():
path = getServerDir() + "/memcached.env"
return path
6 years ago
def getConfTpl():
7 years ago
path = getPluginDir() + "/init.d/memcached.tpl"
return path
6 years ago
def getMemPort():
3 years ago
path = getServerDir() + '/memcached.env'
content = mw.readFile(path)
6 years ago
rep = 'PORT\s*=\s*(\d*)'
tmp = re.search(rep, content)
return tmp.groups()[0]
7 years ago
def getArgs():
args = sys.argv[2:]
tmp = {}
6 years ago
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
2 years ago
if t.strip() == '':
tmp = []
else:
t = t.split(':')
tmp[t[0]] = t[1]
7 years ago
tmp[t[0]] = t[1]
6 years ago
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
7 years ago
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'))
7 years ago
def status():
data = mw.execShell(
7 years ago
"ps -ef|grep " + getPluginName() + " |grep -v grep | grep -v python | awk '{print $2}'")
7 years ago
if data[0] == '':
return 'stop'
return 'start'
7 years ago
def initDreplace():
6 years ago
file_tpl = getConfTpl()
service_path = mw.getServerDir()
7 years ago
7 years ago
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
file_bin = initD_path + '/memcached'
7 years ago
6 years ago
if not os.path.exists(file_bin):
content = mw.readFile(file_tpl)
6 years ago
content = content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
7 years ago
# systemd
3 years ago
systemDir = mw.systemdCfgDir()
systemService = systemDir + '/memcached.service'
systemServiceTpl = getPluginDir() + '/init.d/memcached.service.tpl'
if os.path.exists(systemDir) and not os.path.exists(systemService):
service_path = mw.getServerDir()
se_content = mw.readFile(systemServiceTpl)
se_content = se_content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(systemService, se_content)
mw.execShell('systemctl daemon-reload')
envFile = getServerDir() + '/memcached.env'
if not os.path.exists(envFile):
wbody = "IP=127.0.0.1\n"
wbody = wbody + "PORT=11211\n"
wbody = wbody + "USER=root\n"
wbody = wbody + "MAXCONN=1024\n"
wbody = wbody + "CACHESIZE=1024\n"
wbody = wbody + "OPTIONS=''\n"
mw.writeFile(envFile, wbody)
7 years ago
return file_bin
6 years ago
def memOp(method):
7 years ago
file = initDreplace()
if not mw.isAppleSystem():
data = mw.execShell('systemctl ' + method + ' ' + getPluginName())
if data[1] == '':
return 'ok'
3 years ago
return data[1]
3 years ago
data = mw.execShell(file + ' ' + method)
7 years ago
if data[1] == '':
7 years ago
return 'ok'
6 years ago
return data[1]
def start():
return memOp('start')
7 years ago
def stop():
6 years ago
return memOp('stop')
7 years ago
def restart():
6 years ago
return memOp('restart')
7 years ago
def reload():
6 years ago
return memOp('reload')
7 years ago
def runInfo():
7 years ago
# 获取memcached状态
import telnetlib
import re
6 years ago
port = getMemPort()
7 years ago
try:
6 years ago
tn = telnetlib.Telnet('127.0.0.1', int(port))
7 years ago
tn.write(b"stats\n")
tn.write(b"quit\n")
data = tn.read_all()
if type(data) == bytes:
data = data.decode('utf-8')
data = data.replace('STAT', '').replace('END', '').split("\n")
result = {}
res = ['cmd_get', 'get_hits', 'get_misses', 'limit_maxbytes', 'curr_items', 'bytes',
'evictions', 'limit_maxbytes', 'bytes_written', 'bytes_read', 'curr_connections']
for d in data:
if len(d) < 3:
continue
t = d.split()
if not t[0] in res:
continue
result[t[0]] = int(t[1])
result['hit'] = 1
if result['get_hits'] > 0 and result['cmd_get'] > 0:
result['hit'] = float(result['get_hits']) / \
float(result['cmd_get']) * 100
conf = mw.readFile(getServerDir() + '/memcached.env')
7 years ago
result['bind'] = re.search('IP=(.+)', conf).groups()[0]
result['port'] = int(re.search('PORT=(\d+)', conf).groups()[0])
result['maxconn'] = int(re.search('MAXCONN=(\d+)', conf).groups()[0])
result['cachesize'] = int(
re.search('CACHESIZE=(\d+)', conf).groups()[0])
return mw.getJson(result)
4 years ago
except Exception as e:
return mw.getJson({})
7 years ago
7 years ago
def saveConf():
args = getArgs()
data = checkArgs(args, ['ip', 'port', 'maxconn', 'maxsize'])
if not data[0]:
return data[1]
envFile = getServerDir() + '/memcached.env'
wbody = "IP=" + args['ip'] + "\n"
wbody = wbody + "PORT=" + args['port'] + "\n"
wbody = wbody + "USER=root\n"
wbody = wbody + "MAXCONN=" + args['maxconn'] + "\n"
wbody = wbody + "CACHESIZE=" + args['maxconn'] + "\n"
wbody = wbody + "OPTIONS=''\n"
mw.writeFile(envFile, wbody)
restart()
return 'save ok'
7 years ago
def initdStatus():
if mw.isAppleSystem():
return "Apple Computer does not support"
shell_cmd = 'systemctl status ' + \
getPluginName() + ' | grep loaded | grep "enabled;"'
data = mw.execShell(shell_cmd)
if data[0] == '':
return 'fail'
return 'ok'
def initdInstall():
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl enable ' + getPluginName())
return 'ok'
def initdUinstall():
if not app_debug:
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl disable ' + getPluginName())
return 'ok'
7 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
4 years ago
print(status())
7 years ago
elif func == 'start':
4 years ago
print(start())
7 years ago
elif func == 'stop':
4 years ago
print(stop())
7 years ago
elif func == 'restart':
4 years ago
print(restart())
7 years ago
elif func == 'reload':
4 years ago
print(reload())
elif func == 'initd_status':
4 years ago
print(initdStatus())
elif func == 'initd_install':
4 years ago
print(initdInstall())
elif func == 'initd_uninstall':
4 years ago
print(initdUinstall())
elif func == 'run_info':
4 years ago
print(runInfo())
3 years ago
elif func == 'conf_env':
print(getConfEnv())
elif func == 'save_conf':
4 years ago
print(saveConf())
7 years ago
else:
3 years ago
print('error')