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

222 lines
5.2 KiB

7 years ago
# coding:utf-8
import sys
import io
import os
import time
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
def getPluginName():
return 'redis'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
7 years ago
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
7 years ago
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getConf():
path = getPluginDir() + "/config/redis.conf"
return path
def getInitDTpl():
6 years ago
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl"
7 years ago
return path
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 = mw.execShell(
5 years ago
"ps -ef|grep redis |grep -v grep | grep -v python | grep -v mdserver-web | awk '{print $2}'")
7 years ago
if data[0] == '':
return 'stop'
return 'start'
7 years ago
7 years ago
7 years ago
def initDreplace():
file_tpl = getInitDTpl()
service_path = os.path.dirname(os.getcwd())
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
file_bin = initD_path + '/' + getPluginName()
# initd replace
content = mw.readFile(file_tpl)
7 years ago
content = content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
7 years ago
# config replace
conf_content = mw.readFile(getConf())
7 years ago
conf_content = conf_content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(getServerDir() + '/redis.conf', conf_content)
7 years ago
return file_bin
7 years ago
def start():
7 years ago
file = initDreplace()
data = mw.execShell(file + ' start')
7 years ago
if data[1] == '':
7 years ago
return 'ok'
return 'fail'
def stop():
7 years ago
file = initDreplace()
data = mw.execShell(file + ' stop')
7 years ago
if data[1] == '':
7 years ago
return 'ok'
return 'fail'
def restart():
7 years ago
file = initDreplace()
data = mw.execShell(file + ' restart')
5 years ago
log_file = getServerDir() + "/data/redis.log"
mw.execShell("echo '' > " + log_file)
7 years ago
if data[1] == '':
return 'ok'
return 'fail'
7 years ago
def reload():
7 years ago
file = initDreplace()
data = mw.execShell(file + ' reload')
7 years ago
if data[1] == '':
return 'ok'
return 'fail'
7 years ago
def runInfo():
7 years ago
cmd = getServerDir() + "/bin/redis-cli info"
data = mw.execShell(cmd)[0]
7 years ago
res = [
'tcp_port',
'uptime_in_days', # 已运行天数
'connected_clients', # 连接的客户端数量
'used_memory', # Redis已分配的内存总量
'used_memory_rss', # Redis占用的系统内存总量
'used_memory_peak', # Redis所用内存的高峰值
'mem_fragmentation_ratio', # 内存碎片比率
'total_connections_received', # 运行以来连接过的客户端的总数量
'total_commands_processed', # 运行以来执行过的命令的总数量
'instantaneous_ops_per_sec', # 服务器每秒钟执行的命令数量
'keyspace_hits', # 查找数据库键成功的次数
'keyspace_misses', # 查找数据库键失败的次数
'latest_fork_usec' # 最近一次 fork() 操作耗费的毫秒数
]
data = data.split("\n")
result = {}
for d in data:
if len(d) < 3:
continue
t = d.strip().split(':')
if not t[0] in res:
continue
result[t[0]] = t[1]
return mw.getJson(result)
7 years ago
7 years ago
def initdStatus():
if not app_debug:
if mw.isAppleSystem():
7 years ago
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:
if mw.isAppleSystem():
7 years ago
return "Apple Computer does not support"
source_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(source_bin, initd_bin)
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
7 years ago
return 'ok'
def initdUinstall():
if not app_debug:
if mw.isAppleSystem():
7 years ago
return "Apple Computer does not support"
mw.execShell('chkconfig --del ' + getPluginName())
7 years ago
initd_bin = getInitDFile()
os.remove(initd_bin)
return 'ok'
6 years ago
def runLog():
return getServerDir() + '/data/redis.log'
7 years ago
if __name__ == "__main__":
func = sys.argv[1]
7 years ago
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())
7 years ago
elif func == 'initd_status':
4 years ago
print(initdStatus())
7 years ago
elif func == 'initd_install':
4 years ago
print(initdInstall())
7 years ago
elif func == 'initd_uninstall':
4 years ago
print(initdUinstall())
7 years ago
elif func == 'run_info':
4 years ago
print(runInfo())
7 years ago
elif func == 'conf':
4 years ago
print(getConf())
6 years ago
elif func == 'run_log':
4 years ago
print(runLog())
7 years ago
else:
4 years ago
print('error')