添加PHP守护插件
This commit is contained in:
parent
48b2c4a35e
commit
2ddcc3227b
|
|
@ -120,3 +120,4 @@ data/admin_path.pl
|
|||
data/close.pl
|
||||
ssl/input.pl
|
||||
data/datadir.pl
|
||||
data/502Task.pl
|
||||
|
|
|
|||
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
<script type="text/javascript">
|
||||
setTimeout(function(){
|
||||
layer.closeAll();
|
||||
layer.msg('PHP守护已启动,无需设置',{icon:1,time:2000,shade: [0.3, '#000']});
|
||||
},1);
|
||||
</script>
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
# coding:utf-8
|
||||
|
||||
import sys
|
||||
import io
|
||||
import os
|
||||
import time
|
||||
import re
|
||||
import json
|
||||
import shutil
|
||||
|
||||
reload(sys)
|
||||
sys.setdefaultencoding('utf8')
|
||||
|
||||
sys.path.append(os.getcwd() + "/class/core")
|
||||
sys.path.append("/usr/local/lib/python2.7/site-packages")
|
||||
|
||||
import public
|
||||
|
||||
app_debug = False
|
||||
if public.isAppleSystem():
|
||||
app_debug = True
|
||||
|
||||
|
||||
def getPluginName():
|
||||
return 'php_guard'
|
||||
|
||||
|
||||
def getPluginDir():
|
||||
return public.getPluginDir() + '/' + getPluginName()
|
||||
|
||||
|
||||
def getServerDir():
|
||||
return public.getServerDir() + '/' + getPluginName()
|
||||
|
||||
|
||||
def getInitDFile(version):
|
||||
if app_debug:
|
||||
return '/tmp/' + getPluginName()
|
||||
return '/etc/init.d/' + getPluginName()+version
|
||||
|
||||
|
||||
def getArgs():
|
||||
args = sys.argv[3:]
|
||||
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 checkArgs(data, ck=[]):
|
||||
for i in range(len(ck)):
|
||||
if not ck[i] in data:
|
||||
return (False, public.returnJson(False, '参数:(' + ck[i] + ')没有!'))
|
||||
return (True, public.returnJson(True, 'ok'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
func = sys.argv[1]
|
||||
if func == 'status':
|
||||
print 'start'
|
||||
else:
|
||||
print "fail"
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"sort": 7,
|
||||
"ps": "监控PHP-FPM运行状态,防止大批量出现502错误!",
|
||||
"shell": "install.sh",
|
||||
"name": "php_guard",
|
||||
"title": "PHP守护",
|
||||
"versions": "1.0",
|
||||
"updates": "1.0",
|
||||
"tip": "soft",
|
||||
"checks": "server/php_guard",
|
||||
"path": "server/php/VERSION",
|
||||
"display": 1,
|
||||
"author": "midoks",
|
||||
"date": "2019-03-01",
|
||||
"home": "https://github.com/midoks",
|
||||
"type": "语言解释器",
|
||||
"pid": "1"
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
#!/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/mw_install.pl
|
||||
|
||||
|
||||
Install_pg()
|
||||
{
|
||||
echo 'install scripts ...' > $install_tmp
|
||||
|
||||
|
||||
echo 'True' > ${rootPath}/data/502Task.pl
|
||||
|
||||
mkdir -p $serverPath/php_guard
|
||||
echo '1.0' > $serverPath/php_guard/version.pl
|
||||
|
||||
echo 'install ok' > $install_tmp
|
||||
}
|
||||
|
||||
Uninstall_pg()
|
||||
{
|
||||
rm -rf ${rootPath}/data/502Task.pl
|
||||
rm -rf $serverPath/php_guard
|
||||
}
|
||||
|
||||
|
||||
action=$1
|
||||
host=$2
|
||||
if [ "${1}" == 'install' ];then
|
||||
Install_pg
|
||||
else
|
||||
Uninstall_pg
|
||||
fi
|
||||
87
task.py
87
task.py
|
|
@ -341,6 +341,89 @@ def systemTask():
|
|||
time.sleep(30)
|
||||
systemTask()
|
||||
|
||||
|
||||
# -------------------------------------- PHP监控 start --------------------------------------------- #
|
||||
#502错误检查线程
|
||||
def check502Task():
|
||||
try:
|
||||
while True:
|
||||
if os.path.exists(public.getRunDir()+'/data/502Task.pl'):
|
||||
check502();
|
||||
time.sleep(10);
|
||||
except:
|
||||
time.sleep(10);
|
||||
check502Task();
|
||||
|
||||
def check502():
|
||||
try:
|
||||
phpversions = ['53','54','55','56','70','71','72','73','74']
|
||||
for version in phpversions:
|
||||
sdir = public.getServerDir()
|
||||
php_path = sdir + '/php/' + version + '/sbin/php-fpm'
|
||||
if not os.path.exists(php_path): continue;
|
||||
if checkPHPVersion(version): continue;
|
||||
if startPHPVersion(version):
|
||||
print '检测到PHP-' + version + '处理异常,已自动修复!'
|
||||
public.writeLog('PHP守护程序','检测到PHP-' + version + '处理异常,已自动修复!')
|
||||
except Exception as e:
|
||||
print str(e)
|
||||
|
||||
|
||||
#处理指定PHP版本
|
||||
def startPHPVersion(version):
|
||||
sdir = public.getServerDir()
|
||||
try:
|
||||
fpm = sdir+'/php/init.d/php'+version
|
||||
php_path = sdir+'/php/' + version + '/sbin/php-fpm'
|
||||
if not os.path.exists(php_path):
|
||||
if os.path.exists(fpm): os.remove(fpm)
|
||||
return False;
|
||||
|
||||
#尝试重载服务
|
||||
os.system(fpm + ' reload');
|
||||
if checkPHPVersion(version): return True;
|
||||
|
||||
#尝试重启服务
|
||||
cgi = '/tmp/php-cgi-'+version + '.sock'
|
||||
pid = sdir+'/php/'+version+'/var/run/php-fpm.pid';
|
||||
os.system('ps -ef | grep php/'+version+' | grep -v grep|grep -v python |awk "{print $2}"|xargs kill')
|
||||
time.sleep(0.5);
|
||||
if not os.path.exists(cgi): os.system('rm -f ' + cgi);
|
||||
if not os.path.exists(pid): os.system('rm -f ' + pid);
|
||||
os.system(fpm + ' start');
|
||||
if checkPHPVersion(version): return True;
|
||||
|
||||
#检查是否正确启动
|
||||
if os.path.exists(cgi): return True;
|
||||
except Exception as e:
|
||||
print str(e)
|
||||
return True;
|
||||
|
||||
|
||||
#检查指定PHP版本
|
||||
def checkPHPVersion(version):
|
||||
try:
|
||||
url = 'http://127.0.0.1/phpfpm_status_'+version;
|
||||
result = public.httpGet(url);
|
||||
# print version,result
|
||||
#检查nginx
|
||||
if result.find('Bad Gateway') != -1: return False;
|
||||
if result.find('HTTP Error 404: Not Found') != -1: return False;
|
||||
|
||||
#检查Web服务是否启动
|
||||
if result.find('Connection refused') != -1:
|
||||
global isTask
|
||||
if os.path.exists(isTask):
|
||||
isStatus = public.readFile(isTask);
|
||||
if isStatus == 'True': return True;
|
||||
filename = '/etc/init.d/openresty';
|
||||
if os.path.exists(filename): os.system(filename + ' start');
|
||||
return True;
|
||||
except:
|
||||
return True;
|
||||
|
||||
# --------------------------------------PHP监控 end--------------------------------------------- #
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
import threading
|
||||
|
|
@ -348,4 +431,8 @@ if __name__ == "__main__":
|
|||
t.setDaemon(True)
|
||||
t.start()
|
||||
|
||||
p = threading.Thread(target=check502Task)
|
||||
p.setDaemon(True)
|
||||
p.start()
|
||||
|
||||
startTask()
|
||||
|
|
|
|||
Loading…
Reference in New Issue