From 2ddcc3227be926009f041c299e117c0d26089ca0 Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Tue, 19 Mar 2019 15:14:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0PHP=E5=AE=88=E6=8A=A4?= =?UTF-8?q?=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../php_guard/ico.png | Bin plugins/php_guard/index.html | 7 ++ plugins/php_guard/index.py | 71 ++++++++++++++ plugins/php_guard/info.json | 18 ++++ plugins/php_guard/install.sh | 39 ++++++++ task.py | 87 ++++++++++++++++++ 7 files changed, 223 insertions(+) rename route/static/img/soft_ico/ico-phpguard.png => plugins/php_guard/ico.png (100%) create mode 100755 plugins/php_guard/index.html create mode 100755 plugins/php_guard/index.py create mode 100755 plugins/php_guard/info.json create mode 100755 plugins/php_guard/install.sh diff --git a/.gitignore b/.gitignore index 324660097..83ae9634b 100644 --- a/.gitignore +++ b/.gitignore @@ -120,3 +120,4 @@ data/admin_path.pl data/close.pl ssl/input.pl data/datadir.pl +data/502Task.pl diff --git a/route/static/img/soft_ico/ico-phpguard.png b/plugins/php_guard/ico.png similarity index 100% rename from route/static/img/soft_ico/ico-phpguard.png rename to plugins/php_guard/ico.png diff --git a/plugins/php_guard/index.html b/plugins/php_guard/index.html new file mode 100755 index 000000000..70715852c --- /dev/null +++ b/plugins/php_guard/index.html @@ -0,0 +1,7 @@ + + \ No newline at end of file diff --git a/plugins/php_guard/index.py b/plugins/php_guard/index.py new file mode 100755 index 000000000..e33c41529 --- /dev/null +++ b/plugins/php_guard/index.py @@ -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" diff --git a/plugins/php_guard/info.json b/plugins/php_guard/info.json new file mode 100755 index 000000000..f6a73854d --- /dev/null +++ b/plugins/php_guard/info.json @@ -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" +} \ No newline at end of file diff --git a/plugins/php_guard/install.sh b/plugins/php_guard/install.sh new file mode 100755 index 000000000..cf9e97138 --- /dev/null +++ b/plugins/php_guard/install.sh @@ -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 diff --git a/task.py b/task.py index c1bffae4b..cbf62b437 100755 --- a/task.py +++ b/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()