Before Width: | Height: | Size: 181 B |
@ -1,16 +0,0 @@ |
|||||||
<div class="bt-form"> |
|
||||||
<div class="bt-w-main"> |
|
||||||
<div class="bt-w-menu"> |
|
||||||
<p class="bgw" onclick="pluginService('abkill');">服务</p> |
|
||||||
<p onclick="pluginInitD('abkill');">自启动</p> |
|
||||||
<p onclick="pluginConfig('abkill');">黑名单</p> |
|
||||||
<p onclick="pluginConfig('abkill');">异常日志</p> |
|
||||||
</div> |
|
||||||
<div class="bt-w-con pd15"> |
|
||||||
<div class="soft-man-con"></div> |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
<script type="text/javascript"> |
|
||||||
$.getScript( "/plugins/file?name=abkill&f=js/abkill.js"); |
|
||||||
</script> |
|
@ -1,172 +0,0 @@ |
|||||||
# coding: utf-8 |
|
||||||
|
|
||||||
import time |
|
||||||
import random |
|
||||||
import os |
|
||||||
import urllib |
|
||||||
import binascii |
|
||||||
import json |
|
||||||
import re |
|
||||||
import sys |
|
||||||
import subprocess |
|
||||||
|
|
||||||
sys.path.append(os.getcwd() + "/class/core") |
|
||||||
import public |
|
||||||
|
|
||||||
|
|
||||||
app_debug = False |
|
||||||
if public.getOs() == 'darwin': |
|
||||||
app_debug = True |
|
||||||
|
|
||||||
|
|
||||||
def getPluginName(): |
|
||||||
return 'abkill' |
|
||||||
|
|
||||||
|
|
||||||
def getPluginDir(): |
|
||||||
return public.getPluginDir() + '/' + getPluginName() |
|
||||||
|
|
||||||
|
|
||||||
def getServerDir(): |
|
||||||
return public.getServerDir() + '/' + getPluginName() |
|
||||||
|
|
||||||
|
|
||||||
def getInitDFile(): |
|
||||||
if app_debug: |
|
||||||
return '/tmp/' + getPluginName() |
|
||||||
return '/etc/init.d/' + getPluginName() |
|
||||||
|
|
||||||
|
|
||||||
def getArgs(): |
|
||||||
args = sys.argv[2:] |
|
||||||
tmp = {} |
|
||||||
args_len = len(args) |
|
||||||
|
|
||||||
if args_len == 1: |
|
||||||
t = args[0].strip('{').strip('}') |
|
||||||
t = t.split(':') |
|
||||||
tmp[t[0]] = t[1] |
|
||||||
elif args_len > 1: |
|
||||||
for i in range(len(args)): |
|
||||||
t = args[i].split(':') |
|
||||||
tmp[t[0]] = t[1] |
|
||||||
|
|
||||||
return tmp |
|
||||||
|
|
||||||
|
|
||||||
def initDreplace(): |
|
||||||
initd_file = getInitDFile() |
|
||||||
|
|
||||||
if not os.path.exists(initd_file): |
|
||||||
return getServerDir() |
|
||||||
|
|
||||||
return initd_file |
|
||||||
|
|
||||||
|
|
||||||
def status(): |
|
||||||
data = public.execShell( |
|
||||||
"ps -ef|grep " + getPluginName() + " |grep -v grep | grep -v python | awk '{print $2}'") |
|
||||||
if data[0] == '': |
|
||||||
return 'stop' |
|
||||||
return 'start' |
|
||||||
|
|
||||||
|
|
||||||
def csvnOp(method): |
|
||||||
|
|
||||||
if app_debug: |
|
||||||
os_name = public.getOs() |
|
||||||
if os_name == 'darwin': |
|
||||||
return "Apple Computer does not support" |
|
||||||
|
|
||||||
_initd_csvn = '/etc/init.d/csvn' |
|
||||||
_initd_csvn_httpd = '/etc/init.d/csvn-httpd' |
|
||||||
#_csvn = getServerDir() + '/bin/csvn' |
|
||||||
#_csvn_httpd = getServerDir() + '/bin/csvn-httpd' |
|
||||||
|
|
||||||
ret_csvn_httpd = public.execShell(_initd_csvn_httpd + ' ' + method) |
|
||||||
# ret_csvn = public.execShell(_initd_csvn + ' ' + method) |
|
||||||
subprocess.Popen(_initd_csvn + ' ' + method, |
|
||||||
stdout=subprocess.PIPE, shell=True) |
|
||||||
if ret_csvn_httpd[1] == '': |
|
||||||
return 'ok' |
|
||||||
return 'fail' |
|
||||||
|
|
||||||
|
|
||||||
def start(): |
|
||||||
return csvnOp('start') |
|
||||||
|
|
||||||
|
|
||||||
def stop(): |
|
||||||
return csvnOp('stop') |
|
||||||
|
|
||||||
|
|
||||||
def restart(): |
|
||||||
return csvnOp('restart') |
|
||||||
|
|
||||||
|
|
||||||
def reload(): |
|
||||||
return csvnOp('reload') |
|
||||||
|
|
||||||
|
|
||||||
def initdStatus(): |
|
||||||
if not app_debug: |
|
||||||
if public.getOs() == 'darwin': |
|
||||||
return "Apple Computer does not support" |
|
||||||
|
|
||||||
_initd_csvn = '/etc/init.d/csvn' |
|
||||||
_initd_csvn_httpd = '/etc/init.d/csvn-httpd' |
|
||||||
|
|
||||||
if os.path.exists(_initd_csvn) and os.path.exists(_initd_csvn_httpd): |
|
||||||
return 'ok' |
|
||||||
return 'fail' |
|
||||||
|
|
||||||
|
|
||||||
def initdInstall(): |
|
||||||
import shutil |
|
||||||
if not app_debug: |
|
||||||
if public.getOs() == 'darwin': |
|
||||||
return "Apple Computer does not support" |
|
||||||
|
|
||||||
_csvn = getServerDir() + '/bin/csvn' |
|
||||||
_csvn_httpd = getServerDir() + '/bin/csvn-httpd' |
|
||||||
|
|
||||||
ret_csvn = public.execShell(_csvn + ' install') |
|
||||||
ret_csvn_httpd = public.execShell(_csvn_httpd + ' install') |
|
||||||
if ret_csvn[1] == '' and ret_csvn_httpd[1] == '': |
|
||||||
return 'ok' |
|
||||||
return 'fail' |
|
||||||
|
|
||||||
|
|
||||||
def initdUinstall(): |
|
||||||
if not app_debug: |
|
||||||
if public.getOs() == 'darwin': |
|
||||||
return "Apple Computer does not support" |
|
||||||
|
|
||||||
_csvn = getServerDir() + '/bin/csvn' |
|
||||||
_csvn_httpd = getServerDir() + '/bin/csvn-httpd' |
|
||||||
|
|
||||||
ret_csvn = public.execShell(_csvn + ' remove') |
|
||||||
ret_csvn_httpd = public.execShell(_csvn_httpd + ' remove') |
|
||||||
return 'ok' |
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__": |
|
||||||
func = sys.argv[1] |
|
||||||
if func == 'status': |
|
||||||
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() |
|
||||||
else: |
|
||||||
print 'fail' |
|
@ -1,16 +0,0 @@ |
|||||||
{ |
|
||||||
"sort": 7, |
|
||||||
"ps": "异常程序监控和KILL", |
|
||||||
"shell": "install.sh", |
|
||||||
"name": "abkill", |
|
||||||
"title": "异常程序监控", |
|
||||||
"versions": "0.1", |
|
||||||
"updates":"0.1", |
|
||||||
"tip": "soft", |
|
||||||
"checks": "server/abkill", |
|
||||||
"author": "midoks", |
|
||||||
"date": "201-04-01", |
|
||||||
"home": "https://www.collab.net", |
|
||||||
"type": "异常程序监控和KILL", |
|
||||||
"pid": "4" |
|
||||||
} |
|
@ -1,76 +0,0 @@ |
|||||||
#!/bin/bash |
|
||||||
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin |
|
||||||
|
|
||||||
ak_path={$SERVER_PATH}/abkill |
|
||||||
|
|
||||||
ak_start(){ |
|
||||||
isStart=$(ps aux |grep 'abkill.py'|grep -v grep|awk '{print $2}') |
|
||||||
if [ "$isStart" == '' ];then |
|
||||||
echo -e "Starting abkill... \c" |
|
||||||
cd $ak_path && nohup python abkill.py >> $ak_path/task.log 2>&1 & |
|
||||||
sleep 0.3 |
|
||||||
isStart=$(ps aux |grep 'abkill.py'|grep -v grep|awk '{print $2}') |
|
||||||
if [ "$isStart" == '' ];then |
|
||||||
echo -e "\033[31mfailed\033[0m" |
|
||||||
echo '------------------------------------------------------' |
|
||||||
tail -n 20 $ak_path/task.log |
|
||||||
echo '------------------------------------------------------' |
|
||||||
echo -e "\033[31mError: abkill service startup failed.\033[0m" |
|
||||||
return; |
|
||||||
fi |
|
||||||
echo -e "\033[32mdone\033[0m" |
|
||||||
else |
|
||||||
echo "Starting abkill(pid $isStart) already running" |
|
||||||
fi |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
ak_stop() |
|
||||||
{ |
|
||||||
echo -e "Stopping abkill... \c"; |
|
||||||
pids=$(ps aux | grep 'abkill.py' | grep -v grep|awk '{print $2}') |
|
||||||
arr=($pids) |
|
||||||
|
|
||||||
for p in ${arr[@]} |
|
||||||
do |
|
||||||
kill -9 $p |
|
||||||
done |
|
||||||
echo -e "\033[32mdone\033[0m" |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
ak_status() |
|
||||||
{ |
|
||||||
isStart=$(ps aux|grep 'abkill.py'|grep -v grep|awk '{print $2}') |
|
||||||
if [ "$isStart" != '' ];then |
|
||||||
echo -e "\033[32mabkill (pid $(echo $isStart)) already running\033[0m" |
|
||||||
else |
|
||||||
echo -e "\033[31mabkill not running\033[0m" |
|
||||||
fi |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
ak_reload() |
|
||||||
{ |
|
||||||
isStart=$(ps aux|grep 'abkill.py'|grep -v grep|awk '{print $2}') |
|
||||||
|
|
||||||
if [ "$isStart" != '' ];then |
|
||||||
ak_stop |
|
||||||
ak_start |
|
||||||
else |
|
||||||
echo -e "\033[31mmw not running\033[0m" |
|
||||||
mw_start |
|
||||||
fi |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
case "$1" in |
|
||||||
'start') ak_start;; |
|
||||||
'stop') ak_stop;; |
|
||||||
'reload') ak_reload;; |
|
||||||
'restart') |
|
||||||
ak_stop |
|
||||||
sleep 0.3 |
|
||||||
ak_start;; |
|
||||||
'status') ak_status;; |
|
||||||
esac |
|
@ -1,32 +0,0 @@ |
|||||||
#!/bin/bash |
|
||||||
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin |
|
||||||
export PATH |
|
||||||
|
|
||||||
curPath=`pwd` |
|
||||||
rootPath=$(dirname "$curPath") |
|
||||||
rootPath=$(dirname "$rootPath") |
|
||||||
serverPath=$(dirname "$rootPath") |
|
||||||
|
|
||||||
|
|
||||||
install_tmp=${rootPath}/tmp/bt_install.pl |
|
||||||
|
|
||||||
Install_abkill() |
|
||||||
{ |
|
||||||
echo '正在安装脚本文件...' > $install_tmp |
|
||||||
mkdir -p $serverPath/abkill |
|
||||||
echo '0.1' > $serverPath/abkill/version.pl |
|
||||||
echo '安装完成' > $install_tmp |
|
||||||
} |
|
||||||
|
|
||||||
Uninstall_abkill() |
|
||||||
{ |
|
||||||
rm -f $serverPath/abkill |
|
||||||
echo "卸载完成" > $install_tmp |
|
||||||
} |
|
||||||
|
|
||||||
action=$1 |
|
||||||
if [ "${1}" == 'install' ];then |
|
||||||
Install_abkill |
|
||||||
else |
|
||||||
Uninstall_abkill |
|
||||||
fi |
|
@ -1 +0,0 @@ |
|||||||
pluginService('abkill'); |
|
@ -1,85 +0,0 @@ |
|||||||
# coding: utf-8 |
|
||||||
|
|
||||||
# 使用示例: |
|
||||||
# 1、将此文件重命名为btkill.py , 然后上传到服务器/root目录 |
|
||||||
# 2、执行 python /root/btkill.py |
|
||||||
|
|
||||||
import time |
|
||||||
import os |
|
||||||
import sys |
|
||||||
sys.path.append("/usr/local/lib/python2.7/site-packages") |
|
||||||
import psutil |
|
||||||
|
|
||||||
|
|
||||||
class btkill: |
|
||||||
__limit = 10 # Cpu使用率触发上限 |
|
||||||
__vmsize = 1048576 / 4 # 虚拟内存触发上限(字节) |
|
||||||
|
|
||||||
def checkMain(self): |
|
||||||
pids = psutil.pids() |
|
||||||
print pids |
|
||||||
num = 0 |
|
||||||
for pid in pids: |
|
||||||
try: |
|
||||||
p = psutil.Process(pid) |
|
||||||
if p.exe() == "": |
|
||||||
continue |
|
||||||
name = p.name() |
|
||||||
if self.whiteList(name): |
|
||||||
continue |
|
||||||
cputimes = p.cpu_times() |
|
||||||
if cputimes.user < 0.1: |
|
||||||
continue |
|
||||||
print p |
|
||||||
percent = p.cpu_percent(interval=1) |
|
||||||
vm = p.memory_info().vms |
|
||||||
if percent > self.__limit or vm > self.__vmsize: |
|
||||||
log = time.strftime('%Y-%m-%d %X', time.localtime()) + " (PID=" + str( |
|
||||||
pid) + ", NAME=" + name + ", VMS=" + str(vm) + ", PERCENT=" + str(percent) + "%)" |
|
||||||
# p.kill() |
|
||||||
num += 1 |
|
||||||
print log + " >> killed\n" |
|
||||||
except Exception as ex: |
|
||||||
print str(ex) |
|
||||||
return num |
|
||||||
|
|
||||||
# 检查白名单 |
|
||||||
def whiteList(self, name): |
|
||||||
wlist = ['yum', 'apt-get', 'apt', 'redis-cli', 'memcached', 'sshd', 'vm', 'vim', 'htop', 'top', 'sh', 'bash', 'zip', 'gzip', 'rsync', |
|
||||||
'tar', 'unzip', 'php', 'composer', 'pkill', 'mongo', 'mongod', 'php-fpm', 'nginx', 'httpd', 'lsof', 'ps', 'redis-server', |
|
||||||
'mysqld', 'mysqld_safe', 'mysql', 'pure-ftpd', 'sparse_dd', 'stunnel', 'squeezed', 'vncterm', 'awk', 'ruby', 'postgres', |
|
||||||
'mpathalert', 'vncterm', 'multipathd', 'fe', 'elasticsyslog', 'syslogd', 'v6d', 'xapi', 'screen', 'runsvdir', 'svlogd', |
|
||||||
'java', 'udevd', 'ntpd', 'irqbalance', 'qmgr', 'wpa_supplicant', 'mysqld_safe', 'sftp-server', 'lvmetad', 'gitlab-web', |
|
||||||
'pure-ftpd', 'auditd', 'master', 'dbus-daemon', 'tapdisk', 'sshd', 'init', 'ksoftirqd', 'kworker', 'kmpathd', |
|
||||||
'kmpath_handlerd', 'python', 'kdmflush', 'bioset', 'crond', 'kthreadd', 'migration', 'rcu_sched', 'kjournald', |
|
||||||
'gcc', 'gcc++', 'nginx', 'mysqld', 'php-cgi', 'login', 'firewalld', 'iptables', 'systemd', 'network', 'dhclient', |
|
||||||
'systemd-journald', 'NetworkManager', 'systemd-logind', 'systemd-udevd', 'polkitd', 'tuned', 'rsyslogd', 'AliYunDunUpdate', 'AliYunDun', 'sendmail'] |
|
||||||
wslist = ['vif', 'qemu', 'scsi_eh', 'xcp', |
|
||||||
'xen', 'docker', 'yunsuo', 'aliyun', 'PM2'] |
|
||||||
|
|
||||||
for key in wlist: |
|
||||||
if key == name: |
|
||||||
return True |
|
||||||
|
|
||||||
for key in wslist: |
|
||||||
if name.find(key) != -1: |
|
||||||
return True |
|
||||||
|
|
||||||
return False |
|
||||||
|
|
||||||
# 开始处理 |
|
||||||
def start(self): |
|
||||||
num = 0 |
|
||||||
while True: |
|
||||||
num += self.checkMain() |
|
||||||
print "查杀完成, 共查杀[" + str(num) + "]个异常进程!" |
|
||||||
time.sleep(3) |
|
||||||
print '=======================================' |
|
||||||
print "查杀完成, 共查杀[" + str(num) + "]个异常进程!" |
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__": |
|
||||||
print "正在检测异常进程..." |
|
||||||
print '=======================================' |
|
||||||
c = btkill() |
|
||||||
c.start() |
|
Before Width: | Height: | Size: 832 B |
Before Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 874 B |
Before Width: | Height: | Size: 683 B |
Before Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 3.9 KiB |
@ -1,355 +0,0 @@ |
|||||||
# coding: utf-8 |
|
||||||
|
|
||||||
import time |
|
||||||
import psutil |
|
||||||
import random |
|
||||||
import os |
|
||||||
import urllib |
|
||||||
import binascii |
|
||||||
import json |
|
||||||
import re |
|
||||||
import sys |
|
||||||
|
|
||||||
sys.path.append(os.getcwd() + "/class/core") |
|
||||||
import public |
|
||||||
|
|
||||||
|
|
||||||
class score_main: |
|
||||||
|
|
||||||
__APIURL = 'https://www.bt.cn/api/Auth' |
|
||||||
__UPATH = 'data/userInfo.json' |
|
||||||
__userInfo = None |
|
||||||
__PDATA = None |
|
||||||
|
|
||||||
def CheckToken(self): |
|
||||||
pdata = {} |
|
||||||
data = {} |
|
||||||
if os.path.exists(self.__UPATH): |
|
||||||
self.__userInfo = json.loads(public.readFile(self.__UPATH)) |
|
||||||
if self.__userInfo: |
|
||||||
pdata['access_key'] = self.__userInfo['access_key'] |
|
||||||
data['secret_key'] = self.__userInfo['secret_key'] |
|
||||||
else: |
|
||||||
pdata['access_key'] = 'test' |
|
||||||
data['secret_key'] = '123456' |
|
||||||
pdata['data'] = data |
|
||||||
self.__PDATA = pdata |
|
||||||
|
|
||||||
# 修正信息 |
|
||||||
def SetScore(self, get): |
|
||||||
if hasattr(get, 'ips'): |
|
||||||
return self.SubmitSetScore('ips', get.ips) |
|
||||||
if hasattr(get, 'virt'): |
|
||||||
return self.SubmitSetScore('virt', get.virt) |
|
||||||
|
|
||||||
# 发送信息修正 |
|
||||||
def SubmitSetScore(self, key, value): |
|
||||||
self.CheckToken() |
|
||||||
self.__PDATA['data'][key] = value |
|
||||||
self.__PDATA['data'] = self.De_Code(self.__PDATA['data']) |
|
||||||
result = json.loads(public.httpPost( |
|
||||||
self.__APIURL + '/SetSocre', self.__PDATA)) |
|
||||||
result['data'] = self.En_Code(result['data']) |
|
||||||
return result |
|
||||||
|
|
||||||
# 获取得分列表 |
|
||||||
def GetScore(self, get): |
|
||||||
self.CheckToken() |
|
||||||
self.__PDATA['data'] = self.De_Code(self.__PDATA['data']) |
|
||||||
result = json.loads(public.httpPost( |
|
||||||
self.__APIURL + '/GetSocre', self.__PDATA)) |
|
||||||
result['data'] = self.En_Code(result['data']) |
|
||||||
return result |
|
||||||
|
|
||||||
# 获取配置信息 |
|
||||||
def GetConfig(self, get=None): |
|
||||||
virt = '/usr/sbin/virt-what' |
|
||||||
if not os.path.exists(virt): |
|
||||||
if os.path.exists('/etc/yum.repos.d/epel.repo'): |
|
||||||
os.system( |
|
||||||
'mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo_backup') |
|
||||||
os.system('yum install virt-what -y') |
|
||||||
if os.path.exists('/etc/yum.repos.d/epel.repo_backup'): |
|
||||||
os.system( |
|
||||||
'mv /etc/yum.repos.d/epel.repo_backup /etc/yum.repos.d/epel.repo') |
|
||||||
|
|
||||||
data = {} |
|
||||||
data['virt'] = public.ExecShell('virt-what')[0].strip() |
|
||||||
cpuinfo = open('/proc/cpuinfo', 'r').read() |
|
||||||
rep = "model\s+name\s+:\s+(.+)" |
|
||||||
tmp = re.search(rep, cpuinfo) |
|
||||||
|
|
||||||
data['cpu'] = tmp.groups()[0] |
|
||||||
data['core'] = psutil.cpu_count() |
|
||||||
data['memory'] = psutil.virtual_memory().total / 1024 / 1024 |
|
||||||
data['system'] = self.GetSystemVersion() |
|
||||||
|
|
||||||
scoreInfo = self.readScore() |
|
||||||
data['disk'] = str(scoreInfo['read']) + ',' + str(scoreInfo['write']) |
|
||||||
data['mem_score'] = scoreInfo['mem'] |
|
||||||
data['cpu_score'] = scoreInfo['cpu1'] + \ |
|
||||||
scoreInfo['cpu2'] + scoreInfo['cpu3'] + scoreInfo['cpu4'] |
|
||||||
data['disk_score'] = scoreInfo['disk_score'] |
|
||||||
data['total_score'] = scoreInfo['mem'] + \ |
|
||||||
data['cpu_score'] + scoreInfo['disk_score'] |
|
||||||
return data |
|
||||||
|
|
||||||
# 提交到云端 |
|
||||||
def SubmitScore(self, get): |
|
||||||
try: |
|
||||||
self.CheckToken() |
|
||||||
pdata = self.GetConfig(get) |
|
||||||
if not pdata['total_score']: |
|
||||||
return public.returnMsg(False, '请先跑分!') |
|
||||||
pdata['secret_key'] = self.__userInfo['secret_key'] |
|
||||||
self.__PDATA['data'] = self.De_Code(pdata) |
|
||||||
result = json.loads(public.httpPost( |
|
||||||
self.__APIURL + '/SubmitScore', self.__PDATA)) |
|
||||||
result['data'] = self.En_Code(result['data']) |
|
||||||
return result |
|
||||||
except: |
|
||||||
return None |
|
||||||
|
|
||||||
# 取操作系统版本 |
|
||||||
def GetSystemVersion(self): |
|
||||||
version = public.readFile('/etc/redhat-release') |
|
||||||
if not version: |
|
||||||
version = public.readFile( |
|
||||||
'/etc/issue').replace('\\n \\l', '').strip() |
|
||||||
else: |
|
||||||
version = version.replace('release ', '').strip() |
|
||||||
return version |
|
||||||
|
|
||||||
# 写当前得分 |
|
||||||
def writeScore(self, type, value): |
|
||||||
scoreFile = '/www/server/panel/plugin/score/score.json' |
|
||||||
if not os.path.exists(scoreFile): |
|
||||||
data = {} |
|
||||||
data['cpu1'] = 0 |
|
||||||
data['cpu2'] = 0 |
|
||||||
data['cpu3'] = 0 |
|
||||||
data['cpu4'] = 0 |
|
||||||
data['mem'] = 0 |
|
||||||
data['disk_score'] = 0 |
|
||||||
data['read'] = 0 |
|
||||||
data['write'] = 0 |
|
||||||
public.writeFile(scoreFile, json.dumps(data)) |
|
||||||
|
|
||||||
data = json.loads(public.readFile(scoreFile)) |
|
||||||
data[type] = value |
|
||||||
public.writeFile(scoreFile, json.dumps(data)) |
|
||||||
|
|
||||||
# 读当前得分 |
|
||||||
def readScore(self): |
|
||||||
scoreFile = '/www/server/panel/plugin/score/score.json' |
|
||||||
if not os.path.exists(scoreFile): |
|
||||||
data = {} |
|
||||||
data['cpu1'] = 0 |
|
||||||
data['cpu2'] = 0 |
|
||||||
data['cpu3'] = 0 |
|
||||||
data['cpu4'] = 0 |
|
||||||
data['mem'] = 0 |
|
||||||
data['disk_score'] = 0 |
|
||||||
data['read'] = 0 |
|
||||||
data['write'] = 0 |
|
||||||
public.writeFile(scoreFile, json.dumps(data)) |
|
||||||
data = json.loads(public.readFile(scoreFile)) |
|
||||||
return data |
|
||||||
|
|
||||||
# 测试CPU |
|
||||||
def testCpu(self, get, n=1): |
|
||||||
data = {} |
|
||||||
data['cpuCount'] = psutil.cpu_count() |
|
||||||
if not hasattr(get, 'type'): |
|
||||||
get.type = '0' |
|
||||||
import re |
|
||||||
cpuinfo = open('/proc/cpuinfo', 'r').read() |
|
||||||
rep = "model\s+name\s+:\s+(.+)" |
|
||||||
tmp = re.search(rep, cpuinfo) |
|
||||||
data['cpuType'] = "" |
|
||||||
if tmp: |
|
||||||
data['cpuType'] = tmp.groups()[0] |
|
||||||
|
|
||||||
import system |
|
||||||
data['system'] = system.system().GetSystemVersion() |
|
||||||
path = '/www/server/panel/plugin/score/testcpu' |
|
||||||
if not os.path.exists(path): |
|
||||||
os.system('gcc ' + path + '.c -o ' + path + ' -lpthread') |
|
||||||
start = time.time() |
|
||||||
os.system(path + ' 32 ' + get.type) |
|
||||||
end = time.time() |
|
||||||
data['score'] = int(400 * 10 / (end - start)) |
|
||||||
if not os.path.exists(path): |
|
||||||
data['score'] = 0 |
|
||||||
self.writeScore('cpu' + get.type, data['score']) |
|
||||||
return data |
|
||||||
pass |
|
||||||
|
|
||||||
# 测试整数运算 |
|
||||||
def testInt(self): |
|
||||||
return self.testIntOrFloat(1) |
|
||||||
|
|
||||||
# 测试浮点运行 |
|
||||||
def testFloat(self): |
|
||||||
return self.testIntOrFloat(1.01) |
|
||||||
|
|
||||||
# CPU测试体 |
|
||||||
def testIntOrFloat(self, n=1): |
|
||||||
start = time.time() |
|
||||||
num = 10000 * 100 |
|
||||||
for i in range(num): |
|
||||||
if i == 0: |
|
||||||
continue |
|
||||||
a = n + i |
|
||||||
b = n - i |
|
||||||
c = n * i |
|
||||||
d = n / i |
|
||||||
n = n + 1 |
|
||||||
|
|
||||||
end = time.time() |
|
||||||
usetime = end - start |
|
||||||
return num / 100 / usetime |
|
||||||
|
|
||||||
# 冒泡算法测试 |
|
||||||
def testBubble(self): |
|
||||||
start = time.time() |
|
||||||
num = 10000 * 5 |
|
||||||
xx = 'qwertyuiopasdfghjklzxcvbnm1234567890' |
|
||||||
for c in xrange(num): |
|
||||||
lst = [] |
|
||||||
for k in range(10): |
|
||||||
lst.append(xx[random.randint(0, len(xx) - 1)]) |
|
||||||
lst = self.bubbleSort(lst) |
|
||||||
end = time.time() |
|
||||||
usetime = end - start |
|
||||||
return num / 5 / usetime |
|
||||||
|
|
||||||
# 冒泡排序 |
|
||||||
def bubbleSort(self, lst): |
|
||||||
length = len(lst) |
|
||||||
for i in xrange(0, length, 1): |
|
||||||
for j in xrange(0, length - 1 - i, 1): |
|
||||||
if lst[j] < lst[j + 1]: |
|
||||||
temp = lst[j] |
|
||||||
lst[j] = lst[j + 1] |
|
||||||
lst[j + 1] = temp |
|
||||||
return lst |
|
||||||
|
|
||||||
# 二叉树算法测试 |
|
||||||
def testTree(self): |
|
||||||
import testTree |
|
||||||
|
|
||||||
start = time.time() |
|
||||||
elems = range(3000) # 生成树节点 |
|
||||||
tree = testTree.Tree() # 新建一个树对象 |
|
||||||
for elem in elems: |
|
||||||
tree.add(elem) # 逐个加入树的节点 |
|
||||||
|
|
||||||
tree.level_queue(tree.root) |
|
||||||
tree.front_digui(tree.root) |
|
||||||
tree.middle_digui(tree.root) |
|
||||||
tree.later_digui(tree.root) |
|
||||||
tree.front_stack(tree.root) |
|
||||||
tree.middle_stack(tree.root) |
|
||||||
tree.later_stack(tree.root) |
|
||||||
|
|
||||||
end = time.time() |
|
||||||
usetime = end - start |
|
||||||
return 3000 / usetime |
|
||||||
|
|
||||||
# 测试内存 |
|
||||||
def testMem(self, get): |
|
||||||
mem = psutil.virtual_memory() |
|
||||||
self.writeScore('mem', mem.total / 1024 / 1024) |
|
||||||
# 提交数据 |
|
||||||
self.SubmitScore(get) |
|
||||||
return mem.total / 1024 / 1024 |
|
||||||
|
|
||||||
# 测试磁盘 |
|
||||||
def testDisk(self, get): |
|
||||||
import os |
|
||||||
data = {} |
|
||||||
os.system('rm -f testDisk_*') |
|
||||||
filename = "testDisk_" + \ |
|
||||||
time.strftime('%Y%m%d%H%M%S', time.localtime()) |
|
||||||
data['write'] = self.testDiskWrite(filename) |
|
||||||
import shutil |
|
||||||
filename2 = "testDisk_" + \ |
|
||||||
time.strftime('%Y%m%d%H%M%S', time.localtime()) |
|
||||||
shutil.move(filename, filename2) |
|
||||||
data['read'] = self.testDiskRead(filename2) |
|
||||||
diskIo = psutil.disk_partitions() |
|
||||||
diskInfo = [] |
|
||||||
for disk in diskIo: |
|
||||||
tmp = {} |
|
||||||
tmp['path'] = disk[1] |
|
||||||
tmp['size'] = psutil.disk_usage(disk[1])[0] |
|
||||||
diskInfo.append(tmp) |
|
||||||
data['diskInfo'] = diskInfo |
|
||||||
writeDisk = data['write'] |
|
||||||
if data['write'] > 1000: |
|
||||||
writeDisk = 1000 |
|
||||||
readDisk = data['read'] |
|
||||||
if data['read'] > 1000: |
|
||||||
readDisk = 1000 |
|
||||||
|
|
||||||
data['score'] = (writeDisk * 6) + (readDisk * 6) |
|
||||||
os.remove(filename2) |
|
||||||
|
|
||||||
self.writeScore('disk_score', data['score']) |
|
||||||
self.writeScore('write', data['write']) |
|
||||||
self.writeScore('read', data['read']) |
|
||||||
return data |
|
||||||
pass |
|
||||||
|
|
||||||
# 测试磁盘写入速度 |
|
||||||
def testDiskWrite(self, filename): |
|
||||||
import random |
|
||||||
start = time.time() |
|
||||||
fp = open(filename, 'w+') |
|
||||||
strTest = "" |
|
||||||
strTmp = "" |
|
||||||
for n in range(4): |
|
||||||
strTmp += chr(random.randint(97, 122)) |
|
||||||
for n in range(1024): |
|
||||||
strTest += strTmp |
|
||||||
|
|
||||||
for i in range(1024 * 256): |
|
||||||
fp.write(strTest) |
|
||||||
|
|
||||||
del(strTest) |
|
||||||
del(strTmp) |
|
||||||
fp.close() |
|
||||||
end = time.time() |
|
||||||
usetime = end - start |
|
||||||
return int(1024 / usetime) |
|
||||||
|
|
||||||
# 测试磁盘读取速度 |
|
||||||
def testDiskRead(self, filename): |
|
||||||
os.system('echo 3 > /proc/sys/vm/drop_caches') |
|
||||||
import random |
|
||||||
start = time.time() |
|
||||||
fp = open(filename, 'r') |
|
||||||
size = 4096 |
|
||||||
while True: |
|
||||||
tmp = fp.read(size) |
|
||||||
if not tmp: |
|
||||||
break |
|
||||||
del(tmp) |
|
||||||
fp.close() |
|
||||||
end = time.time() |
|
||||||
usetime = end - start |
|
||||||
return int(1024 / usetime) |
|
||||||
|
|
||||||
def testWorkNet(self): |
|
||||||
pass |
|
||||||
|
|
||||||
# 加密数据 |
|
||||||
def De_Code(self, data): |
|
||||||
pdata = urllib.urlencode(data) |
|
||||||
return binascii.hexlify(pdata) |
|
||||||
|
|
||||||
# 解密数据 |
|
||||||
def En_Code(self, data): |
|
||||||
result = urllib.unquote(binascii.unhexlify(data)) |
|
||||||
return json.loads(result) |
|
@ -1,16 +0,0 @@ |
|||||||
{ |
|
||||||
"title":"跑分测试", |
|
||||||
"tip":"lib", |
|
||||||
"name":"score", |
|
||||||
"type":"扩展", |
|
||||||
"ps":"测试服务器基础性能!", |
|
||||||
"versions": "1.3", |
|
||||||
"shell":"install.sh", |
|
||||||
"checks":"server/score", |
|
||||||
"author":"mdserver-web", |
|
||||||
"home":"github.com/midoks/mdserver-web", |
|
||||||
"date":"2018-11-02", |
|
||||||
"default":false, |
|
||||||
"display":0, |
|
||||||
"pid":"4" |
|
||||||
} |
|
@ -1,38 +0,0 @@ |
|||||||
#!/bin/bash |
|
||||||
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin |
|
||||||
export PATH |
|
||||||
|
|
||||||
curPath=`pwd` |
|
||||||
rootPath=$(dirname "$curPath") |
|
||||||
rootPath=$(dirname "$rootPath") |
|
||||||
serverPath=$(dirname "$rootPath") |
|
||||||
|
|
||||||
install_tmp=${rootPath}/tmp/bt_install.pl |
|
||||||
|
|
||||||
Install_score() |
|
||||||
{ |
|
||||||
echo '正在安装脚本文件...' > $install_tmp |
|
||||||
|
|
||||||
mkdir -p $serverPath/score |
|
||||||
|
|
||||||
gcc $curPath/testcpu.c -o $serverPath/score/testcpu -lpthread |
|
||||||
if [ ! -f $serverPath/score/testcpu ];then |
|
||||||
sleep 0.1 |
|
||||||
gcc $curPath/testcpu.c -o $serverPath/score/testcpu -lpthread |
|
||||||
fi |
|
||||||
|
|
||||||
echo '安装完成' > $install_tmp |
|
||||||
} |
|
||||||
|
|
||||||
Uninstall_score() |
|
||||||
{ |
|
||||||
rm -rf $serverPath/score |
|
||||||
echo '卸载完成' > $install_tmp |
|
||||||
} |
|
||||||
|
|
||||||
action=$1 |
|
||||||
if [ "${1}" == 'install' ];then |
|
||||||
Install_score |
|
||||||
else |
|
||||||
Uninstall_score |
|
||||||
fi |
|
@ -1,129 +0,0 @@ |
|||||||
#include <stdio.h> |
|
||||||
#include <pthread.h> |
|
||||||
#include <unistd.h> |
|
||||||
#include <time.h> |
|
||||||
|
|
||||||
const int N_qsort = 10000; |
|
||||||
const int N_int = 10000; |
|
||||||
const int N_float = 20000; |
|
||||||
const int N_pi = 50000000; |
|
||||||
|
|
||||||
|
|
||||||
int thread_count = 0; |
|
||||||
|
|
||||||
void int_comp(void); |
|
||||||
void float_comp(void); |
|
||||||
void pi_comp(void); |
|
||||||
void to_qsort(int arr[],int low,int high); |
|
||||||
void qsort_comp(void); |
|
||||||
void thread(void); |
|
||||||
void thread_int(void); |
|
||||||
void thread_float(void); |
|
||||||
void thread_pi(void); |
|
||||||
void thread_qsort(void); |
|
||||||
|
|
||||||
int main(int argc,char** argv){ |
|
||||||
int count,i,result,type; |
|
||||||
count = atoi(argv[1]); |
|
||||||
type = atoi(argv[2]); |
|
||||||
if(argc < 3){ |
|
||||||
printf("ERROR: Parameter error[%d]",argc); |
|
||||||
} |
|
||||||
pthread_t tid; |
|
||||||
for(i=0;i<count;i++){ |
|
||||||
switch(type){ |
|
||||||
case 1: |
|
||||||
pthread_create(&tid,NULL,(void *) thread_int,NULL); |
|
||||||
break; |
|
||||||
case 2: |
|
||||||
pthread_create(&tid,NULL,(void *) thread_float,NULL); |
|
||||||
break; |
|
||||||
case 3: |
|
||||||
pthread_create(&tid,NULL,(void *) thread_pi,NULL); |
|
||||||
break; |
|
||||||
case 4: |
|
||||||
pthread_create(&tid,NULL,(void *) thread_qsort,NULL); |
|
||||||
break; |
|
||||||
default: |
|
||||||
pthread_create(&tid,NULL,(void *) thread,NULL); |
|
||||||
break; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
while(thread_count != count){ |
|
||||||
sleep(0.1); |
|
||||||
} |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
void thread(void){ |
|
||||||
int_comp(); |
|
||||||
float_comp(); |
|
||||||
pi_comp(); |
|
||||||
qsort_comp(); |
|
||||||
thread_count++; |
|
||||||
} |
|
||||||
|
|
||||||
void thread_int(void){ |
|
||||||
int_comp(); |
|
||||||
thread_count++; |
|
||||||
} |
|
||||||
|
|
||||||
void thread_float(void){ |
|
||||||
float_comp(); |
|
||||||
thread_count++; |
|
||||||
} |
|
||||||
|
|
||||||
void thread_pi(void){ |
|
||||||
pi_comp(); |
|
||||||
thread_count++; |
|
||||||
} |
|
||||||
|
|
||||||
void thread_qsort(void){ |
|
||||||
qsort_comp(); |
|
||||||
thread_count++; |
|
||||||
} |
|
||||||
|
|
||||||
void int_comp(void){ |
|
||||||
int i,j; |
|
||||||
for(i=0;i<N_int;i++) |
|
||||||
for(j=0;j<N_float;j++); |
|
||||||
} |
|
||||||
|
|
||||||
void float_comp(void){ |
|
||||||
float i,j; |
|
||||||
for(i=0;i<N_int;i++) |
|
||||||
for(j=0;j<N_float;j++); |
|
||||||
} |
|
||||||
|
|
||||||
void pi_comp(void){ |
|
||||||
int m,i=1; |
|
||||||
double s=0; |
|
||||||
for(m=1;m<N_pi;m+=2){ |
|
||||||
s+=i*(1.0/m); |
|
||||||
i=-i; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void to_qsort(int arr[],int low,int high){ |
|
||||||
if(low>=high) return; |
|
||||||
int first=low; |
|
||||||
int last=high; |
|
||||||
int key=arr[first]; |
|
||||||
while(first<last){ |
|
||||||
while(first<last&&arr[last]>=key) --last; |
|
||||||
arr[first]=arr[last]; |
|
||||||
while(first<last&&arr[first]<=key) ++first; |
|
||||||
arr[last]=arr[first]; |
|
||||||
} |
|
||||||
arr[first]=key; |
|
||||||
to_qsort(arr,low,first-1); |
|
||||||
to_qsort(arr,first+1,high); |
|
||||||
} |
|
||||||
|
|
||||||
void qsort_comp(void){ |
|
||||||
int arr[N_qsort],i; |
|
||||||
for(i=N_qsort;i>0;i--) arr[N_qsort-1]=i; |
|
||||||
to_qsort(arr,0,N_qsort-1); |
|
||||||
} |
|