mdserver-web/plugins/memcached/index.py

243 lines
5.7 KiB
Python
Raw Normal View History

2018-11-25 03:46:23 -05:00
# coding:utf-8
import sys
import io
import os
import time
2019-01-04 06:16:47 -05:00
import re
2018-11-25 03:46:23 -05:00
sys.path.append(os.getcwd() + "/class/core")
2020-07-10 03:57:25 -04:00
import mw
2018-11-25 03:46:23 -05:00
2018-12-07 02:52:40 -05:00
app_debug = False
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2018-12-07 05:47:33 -05:00
app_debug = True
2018-11-25 03:46:23 -05:00
2018-12-07 03:44:08 -05:00
def getPluginName():
2018-12-07 01:18:30 -05:00
return 'memcached'
def getPluginDir():
2020-07-10 03:57:25 -04:00
return mw.getPluginDir() + '/' + getPluginName()
2018-12-07 01:18:30 -05:00
def getServerDir():
2020-07-10 03:57:25 -04:00
return mw.getServerDir() + '/' + getPluginName()
2018-12-07 01:18:30 -05:00
def getInitDFile():
if app_debug:
2018-12-07 03:44:08 -05:00
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
2018-12-07 01:18:30 -05:00
def getConf():
2019-01-04 06:16:47 -05:00
path = getServerDir() + "/init.d/memcached"
return path
def getConfTpl():
2018-12-07 01:18:30 -05:00
path = getPluginDir() + "/init.d/memcached.tpl"
return path
2019-01-04 06:16:47 -05:00
def getMemPort():
path = getConf()
2020-07-10 03:57:25 -04:00
content = mw.readFile(path)
2019-01-04 06:16:47 -05:00
rep = 'PORT\s*=\s*(\d*)'
tmp = re.search(rep, content)
return tmp.groups()[0]
2018-12-07 01:18:30 -05:00
def getArgs():
args = sys.argv[2:]
tmp = {}
2019-01-04 06:16:47 -05:00
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
2018-12-07 01:18:30 -05:00
tmp[t[0]] = t[1]
2019-01-04 06:16:47 -05:00
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
2018-12-07 01:18:30 -05:00
return tmp
2018-11-25 03:46:23 -05:00
def status():
2020-07-10 03:57:25 -04:00
data = mw.execShell(
2018-12-07 03:44:08 -05:00
"ps -ef|grep " + getPluginName() + " |grep -v grep | grep -v python | awk '{print $2}'")
2018-11-25 03:46:23 -05:00
if data[0] == '':
return 'stop'
return 'start'
2018-12-06 06:49:54 -05:00
def initDreplace():
2019-01-04 06:16:47 -05:00
file_tpl = getConfTpl()
2020-07-10 03:57:25 -04:00
service_path = mw.getServerDir()
2018-12-06 06:49:54 -05:00
2018-12-07 01:18:30 -05:00
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
file_bin = initD_path + '/memcached'
2018-12-06 06:49:54 -05:00
2019-01-04 22:41:08 -05:00
if not os.path.exists(file_bin):
2020-07-10 03:57:25 -04:00
content = mw.readFile(file_tpl)
2019-01-04 22:41:08 -05:00
content = content.replace('{$SERVER_PATH}', service_path)
2020-07-10 03:57:25 -04:00
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
2018-12-06 06:49:54 -05:00
return file_bin
2019-01-05 22:33:41 -05:00
def memOp(method):
2018-12-06 06:49:54 -05:00
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' ' + method)
2018-12-06 06:49:54 -05:00
if data[1] == '':
2018-11-25 03:46:23 -05:00
return 'ok'
2019-01-05 22:33:41 -05:00
return data[1]
def start():
return memOp('start')
2018-11-25 03:46:23 -05:00
def stop():
2019-01-05 22:33:41 -05:00
return memOp('stop')
2018-11-25 03:46:23 -05:00
def restart():
2019-01-05 22:33:41 -05:00
return memOp('restart')
2018-11-25 03:46:23 -05:00
def reload():
2019-01-05 22:33:41 -05:00
return memOp('reload')
2018-11-25 03:46:23 -05:00
def runInfo():
2018-12-06 06:49:54 -05:00
# 获取memcached状态
import telnetlib
import re
2019-01-04 06:16:47 -05:00
port = getMemPort()
2018-12-06 10:29:41 -05:00
try:
2019-01-04 06:16:47 -05:00
tn = telnetlib.Telnet('127.0.0.1', int(port))
2018-12-06 10:29:41 -05:00
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
2020-07-10 03:57:25 -04:00
conf = mw.readFile(getConf())
2018-12-06 10:29:41 -05:00
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])
2020-07-10 03:57:25 -04:00
return mw.getJson(result)
2021-05-09 12:35:53 -04:00
except Exception as e:
2020-07-10 03:57:25 -04:00
return mw.getJson({})
2018-11-25 03:46:23 -05:00
2018-12-06 10:29:41 -05:00
def saveConf():
2018-12-07 01:18:30 -05:00
# 设置memcached缓存大小
import re
confFile = getConf()
2021-05-09 12:37:36 -04:00
# print confFile
2018-12-07 01:18:30 -05:00
try:
args = getArgs()
2020-07-10 03:57:25 -04:00
content = mw.readFile(confFile)
2018-12-07 01:18:30 -05:00
content = re.sub('IP=.+', 'IP=' + args['ip'], content)
content = re.sub('PORT=\d+', 'PORT=' + args['port'], content)
content = re.sub('MAXCONN=\d+', 'MAXCONN=' + args['maxconn'], content)
content = re.sub('CACHESIZE=\d+', 'CACHESIZE=' +
args['cachesize'], content)
2020-07-10 03:57:25 -04:00
mw.writeFile(confFile, content)
2019-01-04 22:41:08 -05:00
restart()
2019-01-04 22:42:29 -05:00
return 'save ok'
2018-12-07 01:18:30 -05:00
except Exception as e:
pass
return 'fail'
2018-12-06 10:29:41 -05:00
def initdStatus():
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
return "Apple Computer does not support"
initd_bin = getInitDFile()
if os.path.exists(initd_bin):
return 'ok'
return 'fail'
def initdInstall():
import shutil
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
return "Apple Computer does not support"
mem_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(mem_bin, initd_bin)
2020-07-10 03:57:25 -04:00
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
return 'ok'
def initdUinstall():
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
return "Apple Computer does not support"
2019-03-15 04:45:15 -04:00
2020-07-10 03:57:25 -04:00
mw.execShell('chkconfig --del ' + getPluginName())
initd_bin = getInitDFile()
os.remove(initd_bin)
return 'ok'
2018-11-25 03:46:23 -05:00
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
2021-05-08 13:11:18 -04:00
print(status())
2018-11-25 03:46:23 -05:00
elif func == 'start':
2021-05-08 13:11:18 -04:00
print(start())
2018-11-25 03:46:23 -05:00
elif func == 'stop':
2021-05-08 13:11:18 -04:00
print(stop())
2018-11-25 03:46:23 -05:00
elif func == 'restart':
2021-05-08 13:11:18 -04:00
print(restart())
2018-11-25 03:46:23 -05:00
elif func == 'reload':
2021-05-08 13:11:18 -04:00
print(reload())
elif func == 'initd_status':
2021-05-08 13:11:18 -04:00
print(initdStatus())
elif func == 'initd_install':
2021-05-08 13:11:18 -04:00
print(initdInstall())
elif func == 'initd_uninstall':
2021-05-08 13:11:18 -04:00
print(initdUinstall())
elif func == 'run_info':
2021-05-08 13:11:18 -04:00
print(runInfo())
elif func == 'conf':
2021-05-08 13:11:18 -04:00
print(getConf())
2019-01-04 06:16:47 -05:00
elif func == 'conf_tpl':
2021-05-08 13:11:18 -04:00
print(getConfTpl())
elif func == 'save_conf':
2021-05-08 13:11:18 -04:00
print(saveConf())
2018-12-06 10:29:41 -05:00
else:
2021-05-08 13:11:18 -04:00
print('fail')