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

252 lines
6.0 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 public
7 years ago
app_debug = False
6 years ago
if public.isAppleSystem():
7 years ago
app_debug = True
7 years ago
7 years ago
def getPluginName():
7 years ago
return 'memcached'
def getPluginDir():
7 years ago
return public.getPluginDir() + '/' + getPluginName()
7 years ago
def getServerDir():
7 years ago
return public.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
def getConfTpl():
7 years ago
path = getPluginDir() + "/init.d/memcached.tpl"
return path
6 years ago
def getMemPort():
path = getConf()
content = public.readFile(path)
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('}')
t = t.split(':')
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
7 years ago
def status():
data = public.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()
7 years ago
service_path = public.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
7 years ago
# if os.path.exists(file_bin):
# return file_bin
7 years ago
7 years ago
content = public.readFile(file_tpl)
7 years ago
content = content.replace('{$SERVER_PATH}', service_path)
7 years ago
public.writeFile(file_bin, content)
public.execShell('chmod +x ' + file_bin)
return file_bin
7 years ago
def start():
7 years ago
file = initDreplace()
data = public.execShell(file + ' start')
if data[1] == '':
7 years ago
return 'ok'
return 'fail'
def stop():
7 years ago
file = initDreplace()
data = public.execShell(file + ' stop')
if data[1] == '':
7 years ago
return 'ok'
return 'fail'
def restart():
7 years ago
file = initDreplace()
data = public.execShell(file + ' reload')
if data[1] == '':
return 'ok'
return 'fail'
7 years ago
def reload():
7 years ago
file = initDreplace()
data = public.execShell(file + ' reload')
if data[1] == '':
return 'ok'
return 'fail'
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 = public.readFile(getConf())
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 public.getJson(result)
except Exception, e:
return public.getJson({})
7 years ago
7 years ago
def saveConf():
7 years ago
# 设置memcached缓存大小
import re
confFile = getConf()
try:
args = getArgs()
content = public.readFile(confFile)
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)
public.writeFile(confFile, content)
reload()
return 'ok'
except Exception as e:
pass
return 'fail'
7 years ago
def initdStatus():
if not app_debug:
os_name = public.getOs()
if os_name == 'darwin':
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:
os_name = public.getOs()
if os_name == 'darwin':
return "Apple Computer does not support"
mem_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(mem_bin, initd_bin)
public.execShell('chmod +x ' + initd_bin)
return 'ok'
def initdUinstall():
if not app_debug:
os_name = public.getOs()
if os_name == 'darwin':
return "Apple Computer does not support"
initd_bin = getInitDFile()
os.remove(initd_bin)
return 'ok'
7 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
7 years ago
print status()
elif func == 'start':
print start()
elif func == 'stop':
print stop()
elif func == 'restart':
print restart()
elif func == 'reload':
print reload()
elif func == 'initd_status':
print initdStatus()
elif func == 'initd_install':
print initdInstall()
elif func == 'initd_uninstall':
print initdUinstall()
elif func == 'run_info':
print runInfo()
elif func == 'conf':
print getConf()
6 years ago
elif func == 'conf_tpl':
print getConfTpl()
elif func == 'save_conf':
print saveConf()
7 years ago
else:
print 'fail'