mirror of https://github.com/midoks/mdserver-web
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.
230 lines
9.2 KiB
230 lines
9.2 KiB
7 years ago
|
# coding:utf-8
|
||
|
|
||
|
import sys
|
||
|
import io
|
||
|
import os
|
||
|
import time
|
||
|
|
||
|
sys.path.append(os.getcwd() + "/class/core")
|
||
|
import public
|
||
|
|
||
|
|
||
|
def checkMyCnf():
|
||
|
# 处理MySQL配置文件
|
||
|
import os
|
||
|
confFile = '/etc/my.cnf'
|
||
|
if os.path.exists(confFile):
|
||
|
conf = readFile(confFile)
|
||
|
if len(conf) > 100:
|
||
|
return True
|
||
|
versionFile = '/www/server/mysql/version.pl'
|
||
|
if not os.path.exists(versionFile):
|
||
|
return False
|
||
|
|
||
|
versions = ['5.1', '5.5', '5.6', '5.7', 'AliSQL']
|
||
|
version = readFile(versionFile)
|
||
|
for key in versions:
|
||
|
if key in version:
|
||
|
version = key
|
||
|
break
|
||
|
|
||
|
shellStr = '''
|
||
|
#!/bin/bash
|
||
|
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
|
||
|
export PATH
|
||
|
|
||
|
CN='125.88.182.172'
|
||
|
HK='download.bt.cn'
|
||
|
HK2='103.224.251.67'
|
||
|
US='174.139.221.74'
|
||
|
sleep 0.5;
|
||
|
CN_PING=`ping -c 1 -w 1 $CN|grep time=|awk '{print $7}'|sed "s/time=//"`
|
||
|
HK_PING=`ping -c 1 -w 1 $HK|grep time=|awk '{print $7}'|sed "s/time=//"`
|
||
|
HK2_PING=`ping -c 1 -w 1 $HK2|grep time=|awk '{print $7}'|sed "s/time=//"`
|
||
|
US_PING=`ping -c 1 -w 1 $US|grep time=|awk '{print $7}'|sed "s/time=//"`
|
||
|
|
||
|
echo "$HK_PING $HK" > ping.pl
|
||
|
echo "$HK2_PING $HK2" >> ping.pl
|
||
|
echo "$US_PING $US" >> ping.pl
|
||
|
echo "$CN_PING $CN" >> ping.pl
|
||
|
nodeAddr=`sort -V ping.pl|sed -n '1p'|awk '{print $2}'`
|
||
|
if [ "$nodeAddr" == "" ];then
|
||
|
nodeAddr=$HK
|
||
|
fi
|
||
|
|
||
|
Download_Url=http://$nodeAddr:5880
|
||
|
|
||
|
|
||
|
MySQL_Opt()
|
||
|
{
|
||
|
MemTotal=`free -m | grep Mem | awk '{print $2}'`
|
||
|
if [[ ${MemTotal} -gt 1024 && ${MemTotal} -lt 2048 ]]; then
|
||
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" /etc/my.cnf
|
||
|
sed -i "s#^table_open_cache.*#table_open_cache = 128#" /etc/my.cnf
|
||
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" /etc/my.cnf
|
||
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" /etc/my.cnf
|
||
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" /etc/my.cnf
|
||
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" /etc/my.cnf
|
||
|
sed -i "s#^query_cache_size.*#query_cache_size = 16M#" /etc/my.cnf
|
||
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 32M#" /etc/my.cnf
|
||
|
elif [[ ${MemTotal} -ge 2048 && ${MemTotal} -lt 4096 ]]; then
|
||
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" /etc/my.cnf
|
||
|
sed -i "s#^table_open_cache.*#table_open_cache = 256#" /etc/my.cnf
|
||
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" /etc/my.cnf
|
||
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" /etc/my.cnf
|
||
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" /etc/my.cnf
|
||
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" /etc/my.cnf
|
||
|
sed -i "s#^query_cache_size.*#query_cache_size = 32M#" /etc/my.cnf
|
||
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 64M#" /etc/my.cnf
|
||
|
elif [[ ${MemTotal} -ge 4096 && ${MemTotal} -lt 8192 ]]; then
|
||
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" /etc/my.cnf
|
||
|
sed -i "s#^table_open_cache.*#table_open_cache = 512#" /etc/my.cnf
|
||
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" /etc/my.cnf
|
||
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" /etc/my.cnf
|
||
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" /etc/my.cnf
|
||
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" /etc/my.cnf
|
||
|
sed -i "s#^query_cache_size.*#query_cache_size = 64M#" /etc/my.cnf
|
||
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 128M#" /etc/my.cnf
|
||
|
elif [[ ${MemTotal} -ge 8192 && ${MemTotal} -lt 16384 ]]; then
|
||
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" /etc/my.cnf
|
||
|
sed -i "s#^table_open_cache.*#table_open_cache = 1024#" /etc/my.cnf
|
||
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" /etc/my.cnf
|
||
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" /etc/my.cnf
|
||
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" /etc/my.cnf
|
||
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" /etc/my.cnf
|
||
|
sed -i "s#^query_cache_size.*#query_cache_size = 128M#" /etc/my.cnf
|
||
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 256M#" /etc/my.cnf
|
||
|
elif [[ ${MemTotal} -ge 16384 && ${MemTotal} -lt 32768 ]]; then
|
||
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 512M#" /etc/my.cnf
|
||
|
sed -i "s#^table_open_cache.*#table_open_cache = 2048#" /etc/my.cnf
|
||
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 8M#" /etc/my.cnf
|
||
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 8M#" /etc/my.cnf
|
||
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 128M#" /etc/my.cnf
|
||
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 256#" /etc/my.cnf
|
||
|
sed -i "s#^query_cache_size.*#query_cache_size = 256M#" /etc/my.cnf
|
||
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 256M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 2048M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 512M#" /etc/my.cnf
|
||
|
elif [[ ${MemTotal} -ge 32768 ]]; then
|
||
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 1024M#" /etc/my.cnf
|
||
|
sed -i "s#^table_open_cache.*#table_open_cache = 4096#" /etc/my.cnf
|
||
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 16M#" /etc/my.cnf
|
||
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 16M#" /etc/my.cnf
|
||
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 256M#" /etc/my.cnf
|
||
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 512#" /etc/my.cnf
|
||
|
sed -i "s#^query_cache_size.*#query_cache_size = 512M#" /etc/my.cnf
|
||
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 512M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 4096M#" /etc/my.cnf
|
||
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 1024M#" /etc/my.cnf
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
wget -O /etc/my.cnf $Download_Url/install/conf/mysql-%s.conf -T 5
|
||
|
MySQL_Opt
|
||
|
''' % (version,)
|
||
|
# 判断是否迁移目录
|
||
|
if os.path.exists('data/datadir.pl'):
|
||
|
newPath = public.readFile('data/datadir.pl')
|
||
|
mycnf = public.readFile('/etc/my.cnf')
|
||
|
mycnf = mycnf.replace('/www/server/data', newPath)
|
||
|
public.writeFile('/etc/my.cnf', mycnf)
|
||
|
|
||
|
os.system(shellStr)
|
||
|
WriteLog('TYPE_SOFE', 'MYSQL_CHECK_ERR')
|
||
|
return True
|
||
|
|
||
|
|
||
|
def status():
|
||
|
data = public.execShell(
|
||
|
"ps -ef|grep redis |grep -v grep | grep -v python | awk '{print $2}'")
|
||
|
if data[0] == '':
|
||
|
return 'stop'
|
||
|
return 'start'
|
||
|
|
||
|
|
||
|
def start():
|
||
|
path = os.path.dirname(os.getcwd())
|
||
|
cmd = path + "/redis/bin/redis-server"
|
||
|
cmd = cmd + " " + path + "/redis/redis.conf"
|
||
|
data = public.execShell(cmd)
|
||
|
if data[0] == '':
|
||
|
return 'ok'
|
||
|
return 'fail'
|
||
|
|
||
|
|
||
|
def stop():
|
||
|
data = public.execShell(
|
||
|
"ps -ef|grep redis |grep -v grep |grep -v python |awk '{print $2}' | xargs kill -9")
|
||
|
if data[0] == '':
|
||
|
return 'ok'
|
||
|
return 'fail'
|
||
|
|
||
|
|
||
|
def restart():
|
||
|
return 'ok'
|
||
|
|
||
|
|
||
|
def reload():
|
||
|
return 'ok'
|
||
|
|
||
|
|
||
|
def runInfo():
|
||
|
path = os.path.dirname(os.getcwd())
|
||
|
cmd = path + "/redis/bin/redis-cli info"
|
||
|
data = public.execShell(cmd)[0]
|
||
|
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 public.getJson(result)
|
||
|
|
||
|
|
||
|
def getConf():
|
||
|
path = os.path.dirname(os.getcwd()) + "/redis/redis.conf"
|
||
|
return path
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
func = sys.argv[1]
|
||
|
if func == 'run_info':
|
||
|
print runInfo()
|
||
|
elif func == 'conf':
|
||
|
print getConf()
|
||
|
elif func == 'status':
|
||
|
print status()
|
||
|
elif func == 'start':
|
||
|
print start()
|
||
|
elif func == 'stop':
|
||
|
print stop()
|
||
|
elif func == 'restart':
|
||
|
print restart()
|
||
|
elif func == 'reload':
|
||
|
print reload()
|