diff --git a/plugins/haproxy/conf/haproxy.conf b/plugins/haproxy/conf/haproxy.conf new file mode 100755 index 000000000..8885d0e78 --- /dev/null +++ b/plugins/haproxy/conf/haproxy.conf @@ -0,0 +1,9 @@ +global + log 127.0.0.1 local2 + chroot /var/lib/haproxy + pidfile /var/run/haproxy.pid + maxconn 4000 + user haproxy + group haproxy + daemon + stats socket /var/lib/haproxy/stats diff --git a/plugins/haproxy/ico.png b/plugins/haproxy/ico.png new file mode 100644 index 000000000..3fc4e8c6d Binary files /dev/null and b/plugins/haproxy/ico.png differ diff --git a/plugins/haproxy/index.html b/plugins/haproxy/index.html new file mode 100755 index 000000000..f64aa937b --- /dev/null +++ b/plugins/haproxy/index.html @@ -0,0 +1,21 @@ +
+
+
+

服务

+

自启动

+

配置修改

+

运行日志

+

查询日志

+

运行状态

+

说明

+
+
+
+
+
+
+ \ No newline at end of file diff --git a/plugins/haproxy/index.py b/plugins/haproxy/index.py new file mode 100755 index 000000000..c79e2a831 --- /dev/null +++ b/plugins/haproxy/index.py @@ -0,0 +1,355 @@ +# coding:utf-8 + +import sys +import io +import os +import time +import re +import string +import subprocess + +sys.path.append(os.getcwd() + "/class/core") +import mw + +app_debug = False +if mw.isAppleSystem(): + app_debug = True + + +def getPluginName(): + return 'haproxy' + + +def getPluginDir(): + return mw.getPluginDir() + '/' + getPluginName() + + +def getServerDir(): + return mw.getServerDir() + '/' + getPluginName() + + +def getInitDFile(): + if app_debug: + return '/tmp/' + getPluginName() + return '/etc/init.d/' + getPluginName() + + +def getConfTpl(): + path = getPluginDir() + "/conf/haproxy.conf" + return path + + +def getConf(): + path = getServerDir() + "/sphinx.conf" + return path + + +def getInitDTpl(): + path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl" + return path + + +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 checkArgs(data, ck=[]): + for i in range(len(ck)): + if not ck[i] in data: + return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!')) + return (True, mw.returnJson(True, 'ok')) + + +def configTpl(): + path = getPluginDir() + '/tpl' + pathFile = os.listdir(path) + tmp = [] + for one in pathFile: + file = path + '/' + one + tmp.append(file) + return mw.getJson(tmp) + + +def readConfigTpl(): + args = getArgs() + data = checkArgs(args, ['file']) + if not data[0]: + return data[1] + + content = mw.readFile(args['file']) + content = contentReplace(content) + return mw.returnJson(True, 'ok', content) + + +def contentReplace(content): + service_path = mw.getServerDir() + content = content.replace('{$ROOT_PATH}', mw.getRootDir()) + content = content.replace('{$SERVER_PATH}', service_path) + content = content.replace('{$SERVER_APP}', service_path + '/haproxy') + return content + + +def status(): + data = mw.execShell( + "ps -ef|grep haproxy |grep -v grep | grep -v python | awk '{print $2}'") + if data[0] == '': + return 'stop' + return 'start' + + +def mkdirAll(): + content = mw.readFile(getConf()) + rep = 'path\s*=\s*(.*)' + p = re.compile(rep) + tmp = p.findall(content) + + for x in tmp: + if x.find('binlog') != -1: + mw.execShell('mkdir -p ' + x) + else: + mw.execShell('mkdir -p ' + os.path.dirname(x)) + + +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 + if not os.path.exists(file_bin): + content = mw.readFile(file_tpl) + content = contentReplace(content) + mw.writeFile(file_bin, content) + mw.execShell('chmod +x ' + file_bin) + + # config replace + conf_bin = getConf() + if not os.path.exists(conf_bin): + conf_content = mw.readFile(getConfTpl()) + conf_content = contentReplace(conf_content) + mw.writeFile(getServerDir() + '/haproxy.conf', conf_content) + + # systemd + systemDir = mw.systemdCfgDir() + systemService = systemDir + '/haproxy.service' + systemServiceTpl = getPluginDir() + '/init.d/haproxy.service.tpl' + if os.path.exists(systemDir) and not os.path.exists(systemService): + service_path = mw.getServerDir() + se_content = mw.readFile(systemServiceTpl) + se_content = se_content.replace('{$SERVER_PATH}', service_path) + mw.writeFile(systemService, se_content) + mw.execShell('systemctl daemon-reload') + + mkdirAll() + return file_bin + + +def checkIndexSph(): + content = mw.readFile(getConf()) + rep = 'path\s*=\s*(.*)' + p = re.compile(rep) + tmp = p.findall(content) + for x in tmp: + if x.find('binlog') != -1: + continue + else: + p = x + '.sph' + if os.path.exists(p): + return False + return True + + +def sphOp(method): + file = initDreplace() + + if not mw.isAppleSystem(): + data = mw.execShell('systemctl ' + method + ' haproxy') + if data[1] == '': + return 'ok' + return 'fail' + + data = mw.execShell(file + ' ' + method) + if data[1] == '': + return 'ok' + return data[1] + + +def start(): + return sphOp('start') + + +def stop(): + return sphOp('stop') + + +def restart(): + return sphOp('restart') + + +def reload(): + return sphOp('reload') + + +def rebuild(): + file = initDreplace() + subprocess.Popen(file + ' rebuild &', + stdout=subprocess.PIPE, shell=True) + # data = mw.execShell(file + ' rebuild') + return 'ok' + + +def initdStatus(): + if mw.isAppleSystem(): + return "Apple Computer does not support" + + shell_cmd = 'systemctl status haproxy | grep loaded | grep "enabled;"' + data = mw.execShell(shell_cmd) + if data[0] == '': + return 'fail' + return 'ok' + + +def initdInstall(): + if mw.isAppleSystem(): + return "Apple Computer does not support" + + mw.execShell('systemctl enable haproxy') + return 'ok' + + +def initdUinstall(): + if mw.isAppleSystem(): + return "Apple Computer does not support" + + mw.execShell('systemctl disable haproxy') + return 'ok' + + +def runLog(): + path = getConf() + content = mw.readFile(path) + rep = 'log\s*=\s*(.*)' + tmp = re.search(rep, content) + return tmp.groups()[0] + + +def getPort(): + path = getConf() + content = mw.readFile(path) + rep = 'listen\s*=\s*(.*)' + tmp = re.search(rep, content) + return tmp.groups()[0] + + +def queryLog(): + path = getConf() + content = mw.readFile(path) + rep = 'query_log\s*=\s*(.*)' + tmp = re.search(rep, content) + return tmp.groups()[0] + + +def runStatus(): + s = status() + if s != 'start': + return mw.returnJson(False, '没有启动程序') + + sys.path.append(getPluginDir() + "/class") + import sphinxapi + + sh = sphinxapi.SphinxClient() + port = getPort() + sh.SetServer('127.0.0.1', port) + info_status = sh.Status() + + rData = {} + for x in range(len(info_status)): + rData[info_status[x][0]] = info_status[x][1] + + return mw.returnJson(True, 'ok', rData) + + +def sphinxConfParse(): + file = getConf() + bin_dir = getServerDir() + content = mw.readFile(file) + rep = 'index\s(.*)' + sindex = re.findall(rep, content) + indexlen = len(sindex) + cmd = {} + if indexlen > 0: + cmd_index = [] + cmd_delta = [] + for x in range(indexlen): + if sindex[x].find(':') != -1: + cmd_delta.append(sindex[x]) + else: + cmd_index.append(sindex[x]) + + cmd['index'] = cmd_index + cmd['delta'] = cmd_delta + cmd['cmd'] = bin_dir + '/bin/bin/indexer -c ' + bin_dir + '/sphinx.conf' + return cmd + + +def sphinxCmd(): + data = sphinxConfParse() + if 'index' in data: + return mw.returnJson(True, 'ok', data) + else: + return mw.returnJson(False, 'no index') + + +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 == 'rebuild': + print(rebuild()) + elif func == 'initd_status': + print(initdStatus()) + elif func == 'initd_install': + print(initdInstall()) + elif func == 'initd_uninstall': + print(initdUinstall()) + elif func == 'conf': + print(getConf()) + elif func == 'config_tpl': + print(configTpl()) + elif func == 'read_config_tpl': + print(readConfigTpl()) + elif func == 'run_log': + print(runLog()) + elif func == 'query_log': + print(queryLog()) + elif func == 'run_status': + print(runStatus()) + elif func == 'sphinx_cmd': + print(sphinxCmd()) + else: + print('error') diff --git a/plugins/haproxy/info.json b/plugins/haproxy/info.json new file mode 100755 index 000000000..98700f813 --- /dev/null +++ b/plugins/haproxy/info.json @@ -0,0 +1,19 @@ +{ + "sort": 7, + "ps": "可靠、高性能的TCP/HTTP负载平衡器", + "name": "haproxy", + "title": "haproxy", + "shell": "install.sh", + "versions":["2.6"], + "updates":["2.6"], + "tip": "soft", + "checks": "server/haproxy", + "path": "server/haproxy", + "display": 1, + "author": "midoks", + "date": "2022-08-26", + "home": "https://www.haproxy.org/", + "doc1": "https://www.haproxy.org/", + "type": 0, + "pid": "4" +} \ No newline at end of file diff --git a/plugins/haproxy/init.d/haproxy.service.tpl b/plugins/haproxy/init.d/haproxy.service.tpl new file mode 100644 index 000000000..ce417c2be --- /dev/null +++ b/plugins/haproxy/init.d/haproxy.service.tpl @@ -0,0 +1,12 @@ +[Unit] +Description=he Reliable, High Performance TCP/HTTP Load Balancer +After=network.target + +[Service] +Type=forking +ExecStart={$SERVER_PATH}/sphinx/bin/bin/searchd -c {$SERVER_PATH}/sphinx/sphinx.conf +ExecReload=/bin/kill -USR2 $MAINPID +Restart=on-failure + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/plugins/haproxy/init.d/haproxy.tpl b/plugins/haproxy/init.d/haproxy.tpl new file mode 100644 index 000000000..4652aa078 --- /dev/null +++ b/plugins/haproxy/init.d/haproxy.tpl @@ -0,0 +1,82 @@ +#! /bin/bash +# +# searchd: sphinx Daemon +# +# chkconfig: - 90 25 +# description: sphinx Daemon +# +### BEGIN INIT INFO +# Provides: sphinx +# Required-Start: $syslog +# Required-Stop: $syslog +# Should-Start: $local_fs +# Should-Stop: $local_fs +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: sphinx - Document Index Daemon +# Description: sphinx - Document Index Daemon +### END INIT INFO + +APP_PATH={$SERVER_APP} +APP_CONF={$SERVER_APP}/sphinx.conf +prog="sphinx" + +start () { + echo -n $"Starting $prog: " + ${APP_PATH}/bin/bin/searchd -c ${APP_CONF} + if [ "$?" != 0 ] ; then + echo " failed" + exit 1 + else + echo " done" + fi +} + +rebuild () { + ${APP_PATH}/bin/bin/indexer -c ${APP_CONF} --all --rotate & +} + + +stop () { + echo -n $"Stopping $prog: " + if [ ! -e ${APP_PATH}/index/searchd.pid ]; then + echo -n $"$prog is not running." + exit 1 + fi + kill `cat ${APP_PATH}/index/searchd.pid` + if [ "$?" != 0 ] ; then + echo " failed" + exit 1 + else + rm -f ${APP_PATH}/index/searchd.pid + echo " done" + fi +} + +restart () { + $0 stop + sleep 2 + $0 start +} + +# See how we were called. +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart|reload) + restart + ;; + rebuild) + rebuild + ;; + *) + echo $"Usage: $0 {start|stop|status|restart|reload}" + exit 1 + ;; +esac + +exit $? \ No newline at end of file diff --git a/plugins/haproxy/install.sh b/plugins/haproxy/install.sh new file mode 100755 index 000000000..5f656275b --- /dev/null +++ b/plugins/haproxy/install.sh @@ -0,0 +1,44 @@ +#!/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 + + +action=$1 +type=$2 + +echo $action $type + +if [ "${2}" == "" ];then + echo '缺少安装脚本...' > $install_tmp + exit 0 +fi + +if [ ! -d $curPath/versions/$2 ];then + echo '缺少安装脚本2...' > $install_tmp + exit 0 +fi + +if [ "${action}" == "uninstall" ];then + + if [ -f /usr/lib/systemd/system/haproxy.service ] || [ -f /lib/systemd/system/haproxy.service ];then + systemctl stop haproxy + systemctl disable haproxy + rm -rf /usr/lib/systemd/system/haproxy.service + rm -rf /lib/systemd/system/haproxy.service + systemctl daemon-reload + fi +fi + +sh -x $curPath/versions/$2/install.sh $1 + +if [ "${action}" == "install" ] && [ -d $serverPath/haproxy ];then + #初始化 + cd ${rootPath} && python3 ${rootPath}/plugins/haproxy/index.py start ${type} + cd ${rootPath} && python3 ${rootPath}/plugins/haproxy/index.py initd_install ${type} +fi diff --git a/plugins/haproxy/js/haproxy.js b/plugins/haproxy/js/haproxy.js new file mode 100755 index 000000000..37448384c --- /dev/null +++ b/plugins/haproxy/js/haproxy.js @@ -0,0 +1,135 @@ +function spPostMin(method, args, callback){ + + var req_data = {}; + req_data['name'] = 'haproxy'; + req_data['func'] = method; + + if (typeof(args) != 'undefined' && args!=''){ + req_data['args'] = JSON.stringify(args); + } + + $.post('/plugins/run', req_data, function(data) { + if (!data.status){ + layer.msg(data.msg,{icon:0,time:2000,shade: [0.3, '#000']}); + return; + } + + if(typeof(callback) == 'function'){ + callback(data); + } + },'json'); +} + +function spPost(method, args, callback){ + var loadT = layer.msg('正在获取...', { icon: 16, time: 0, shade: 0.3 }); + spPostMin(method,args,function(data){ + layer.close(loadT); + if(typeof(callback) == 'function'){ + callback(data); + } + }); +} + +function rebuild(){ + var con = ''; + $(".soft-man-con").html(con); +} + +function rebuildIndex(){ + spPost('rebuild', '', function(data){ + if (data.data == 'ok'){ + layer.msg('在重建中..',{icon:1,time:2000,shade: [0.3, '#000']}); + } else { + layer.msg(data.data,{icon:2,time:2000,shade: [0.3, '#000']}); + } + }); +} + +function secToTime(s) { + var t; + if(s > -1){ + var hour = Math.floor(s/3600); + var min = Math.floor(s/60) % 60; + var sec = s % 60; + if(hour < 10) { + t = '0'+ hour + ":"; + } else { + t = hour + ":"; + } + + if(min < 10){t += "0";} + t += min + ":"; + if(sec < 10){t += "0";} + t += sec.toFixed(2); + } + return t; +} + + +function runStatus(){ + spPost('run_status', '', function(data){ + var rdata = $.parseJSON(data.data); + if (!rdata['status']){ + layer.msg(rdata['msg'],{icon:2,time:2000,shade: [0.3, '#000']}); + return; + } + var idata = rdata.data; + // console.log(idata); + var con = '
\ + \ + \ + \ + \ + \ + \ +
运行时间' + secToTime(idata.uptime) + '每秒查询' + parseInt(parseInt(idata.queries) / parseInt(idata.uptime)) + '
总连接次数' + idata.connections + 'work_queue_length' +idata.work_queue_length + '
agent_connect' + idata.agent_connect+ 'workers_active' + idata.workers_active + '
agent_retry' + idata.agent_retry + 'workers_total' + idata.workers_total + '
\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ +
command_delete' + idata.command_delete + 'command_delete
command_excerpt' + idata.command_excerpt + 'command_excerpt
command_flushattrs' + idata.command_flushattrs + 'command_flushattrs
command_keywords' + idata.command_keywords + 'command_keywords
command_persist' + idata.command_persist + 'command_persist
command_search' + idata.command_search + 'command_search
command_status' + idata.command_status + 'command_status
command_update' + idata.command_update + 'command_update
'; + + $(".soft-man-con").html(con); + }); +} + +function readme(){ + spPost('sphinx_cmd', '', function(data){ + + var rdata = $.parseJSON(data.data); + if (!rdata['status']){ + layer.msg(rdata['msg'],{icon:2,time:2000,shade: [0.3, '#000']}); + return; + } + + var con = ''; + + $(".soft-man-con").html(con); + }); + +} + diff --git a/plugins/haproxy/versions/2.6/install.sh b/plugins/haproxy/versions/2.6/install.sh new file mode 100755 index 000000000..74ae7e193 --- /dev/null +++ b/plugins/haproxy/versions/2.6/install.sh @@ -0,0 +1,70 @@ +#!/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") +sysName=`uname` + +install_tmp=${rootPath}/tmp/mw_install.pl + + +bash ${rootPath}/scripts/getos.sh +OSNAME=`cat ${rootPath}/data/osname.pl` +OSNAME_ID=`cat /etc/*-release | grep VERSION_ID | awk -F = '{print $2}' | awk -F "\"" '{print $2}'` + + +VERSION=2.6.4 +MIN_VERSION=2.6 +Install_App() +{ + + echo '正在安装脚本文件...' > $install_tmp + mkdir -p $serverPath/haproxy + + APP_DIR=${serverPath}/source/haproxy + mkdir -p $APP_DIR + + if [ ! -f ${APP_DIR}/haproxy-${VERSION}.tar.gz ];then + if [ $sysName == 'Darwin' ]; then + wget -O ${APP_DIR}/haproxy-${VERSION}.tar.gz https://www.haproxy.org/download/${MIN_VERSION}/src/haproxy-${VERSION}.tar.gz + else + curl -sSLo ${APP_DIR}/haproxy-${VERSION}.tar.gz https://www.haproxy.org/download/${MIN_VERSION}/src/haproxy-${VERSION}.tar.gz + fi + fi + + if [ ! -f ${APP_DIR}/haproxy-${VERSION}.tar.gz ];then + curl -sSLo ${APP_DIR}/haproxy-${VERSION}.tar.gz https://www.haproxy.org/download/${MIN_VERSION}/src/haproxy-${VERSION}.tar.gz + fi + + + cd ${APP_DIR} && tar -zxvf haproxy-${VERSION}.tar.gz + + if [ "$OSNAME" == "macos" ];then + cd ${APP_DIR}/haproxy-${VERSION} && make TARGET=osx && make install PREFIX=$serverPath/haproxy + else + cd ${APP_DIR}/haproxy-${VERSION} && make TARGET=linux-glibc && make install PREFIX=$serverPath/haproxy + fi + + echo $MIN_VERSION > $serverPath/haproxy/version.pl + echo 'Install_HA' > $install_tmp +} + +Uninstall_App() +{ + if [ -f $serverPath/haproxy/initd/haproxy ];then + $serverPath/haproxy/initd/haproxy stop + fi + + rm -rf $serverPath/haproxy + echo "Uninstall_HA" > $install_tmp +} + +action=$1 +if [ "${1}" == 'install' ];then + Install_App +else + Uninstall_App +fi