diff --git a/plugins/nezha/index.html b/plugins/nezha/index.html index eaa296235..7e27470a7 100755 --- a/plugins/nezha/index.html +++ b/plugins/nezha/index.html @@ -1,10 +1,11 @@
+

面板服务

面板自启动

-

AGENT服务

-

AGENT自启动

+

AGENT服务

+

AGENT自启动

面板配置文件

@@ -16,7 +17,7 @@ \ No newline at end of file diff --git a/plugins/nezha/index.py b/plugins/nezha/index.py index c85f5851b..93b8dc94e 100755 --- a/plugins/nezha/index.py +++ b/plugins/nezha/index.py @@ -98,6 +98,13 @@ class App: return 'stop' return 'start' + def status_agent(self): + cmd = "ps -ef | grep nezha-agent | grep -v grep | grep -v python | awk '{print $2}'" + data = mw.execShell(cmd) + if data[0] == '': + return 'stop' + return 'start' + def contentReplace(self, content): service_path = mw.getServerDir() @@ -139,6 +146,31 @@ class App: return file_bin + def initDAgent(self): + initD_path = self.getServerDir() + '/init.d' + if not os.path.exists(initD_path): + os.mkdir(initD_path) + + file_agent_bin = initD_path + '/nezha-agent' + if not os.path.exists(file_agent_bin): + content = mw.readFile(file_tpl) + content = self.contentReplace(content) + mw.writeFile(file_agent_bin, content) + mw.execShell('chmod +x ' + file_agent_bin) + + # systemd + sysDir = mw.systemdCfgDir() + sysService = sysDir + '/nezha-agent.service' + sysServiceTpl = self.getPluginDir() + '/init.d/nezha-agent.service.tpl' + if os.path.exists(sysDir) and not os.path.exists(sysService): + service_path = mw.getServerDir() + content = mw.readFile(sysServiceTpl) + content = self.contentReplace(content) + mw.writeFile(sysService, content) + mw.execShell('systemctl daemon-reload') + + return file_agent_bin + def imOp(self, method): file = self.initDreplace() @@ -166,6 +198,33 @@ class App: def reload(self): return self.imOp('reload') + def agOp(self, method): + file = self.initDAgent() + + if not mw.isAppleSystem(): + cmd = 'systemctl {} {}'.format(method, 'nezha-agent') + data = mw.execShell(cmd) + if data[1] == '': + return 'ok' + return 'fail' + + data = mw.execShell(file + ' ' + method) + if data[1] == '': + return 'ok' + return data[0] + + def start_agent(self): + return self.agOp('start') + + def stop_agent(self): + return self.agOp('stop') + + def restart_agent(self): + return self.agOp('restart') + + def reload_agent(self): + return self.agentOp('reload') + def initd_status(self): cmd = 'systemctl status nezha | grep loaded | grep "enabled;"' data = mw.execShell(cmd) diff --git a/plugins/nezha/info.json b/plugins/nezha/info.json index 2a9159425..afb4e6990 100755 --- a/plugins/nezha/info.json +++ b/plugins/nezha/info.json @@ -12,6 +12,7 @@ "author": "midoks", "date": "2023-06-26", "home": "https://github.com/midoks/nezha", + "doc1": "https://nezha.wiki/", "type": 0, "pid": "5" } \ No newline at end of file diff --git a/plugins/nezha/init.d/nezha-agent.tpl b/plugins/nezha/init.d/nezha-agent.tpl new file mode 100644 index 000000000..2b053d938 --- /dev/null +++ b/plugins/nezha/init.d/nezha-agent.tpl @@ -0,0 +1,87 @@ +#!/bin/bash +# chkconfig: 2345 55 25 +# description: Nezha Service + +### BEGIN INIT INFO +# Provides: Nezha +# Required-Start: $all +# Required-Stop: $all +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: starts Nezha +# Description: starts the Nezha +### END INIT INFO + +PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin + +if [ -f /etc/init.d/functions ];then + . /etc/init.d/functions +fi + +if [ -f /etc/rc.d/init.d/functions ];then + . /etc/rc.d/init.d/functions +fi + +SERVICENAME=nezha-agnet +LOG_PATH={$SERVER_PATH}/${SERVICENAME}/logs +APP_PATH={$SERVER_PATH}/${SERVICENAME}/agent + + +app_start(){ + isStart=`ps -ef|grep "${SERVICENAME} web" | grep -v grep | grep -v python | grep -v "/bin/bash" | awk '{print $2}'` + if [ "$isStart" == '' ];then + echo -e "Starting ${SERVICENAME}... \c" + cd $APP_PATH && exec nohup ${APP_PATH}/${SERVICENAME} web >> ${LOG_PATH}/${SERVICENAME}.log 2>&1 & + isStart="" + while [[ "$isStart" == "" ]]; + do + echo -e ".\c" + sleep 0.5 + isStart=$(ps -ef|grep "${SERVICENAME} web" |grep -v grep | grep -v python | grep -v "/bin/bash" | awk '{print $2}') + let n+=1 + if [ $n -gt 15 ];then + break; + fi + done + if [ "$isStart" == '' ];then + echo -e "\033[31mError: ${SERVICENAME} service startup failed.\033[0m" + return; + fi + echo -e "\033[32mdone\033[0m" + else + echo "Starting ${SERVICENAME}(pid $(echo $isStart)) already running" + fi +} + +app_stop(){ + pids=`ps -ef | grep "${SERVICENAME} web" | grep -v grep | grep -v python | grep -v "/bin/bash" |awk '{print $2}'` + arr=($pids) + echo -e "Stopping ${SERVICENAME}... \c" + for p in ${arr[@]} + do + # echo "$p" + kill -9 $p + done + echo -e "\033[32mdone\033[0m" +} + +app_status(){ + isStart=`ps -ef | grep "${SERVICENAME} web" | grep -v grep | grep -v python | grep -v "/bin/bash" | awk '{print $2}'` + if [ "$isStart" == '' ];then + echo -e "${SERVICENAME} not running" + else + echo -e "${SERVICENAME}(pid $(echo $isStart)) already running" + fi +} + +case "$1" in + 'start') app_start;; + 'stop') app_stop;; + 'status') app_status;; + 'reload') + app_stop + app_start;; + 'restart') + app_stop + app_start;; +esac \ No newline at end of file diff --git a/route/static/app/public.js b/route/static/app/public.js index 305161b97..6f51ad838 100755 --- a/route/static/app/public.js +++ b/route/static/app/public.js @@ -1924,14 +1924,19 @@ function utf8to16(str) { } -function pluginService(_name, version){ - var data = {name:_name, func:'status'} +function pluginService(_name, version, _suffix_name=''){ + + var default_name = 'status'; + if ( _suffix_name != '' ){ + default_name = 'status_'+_suffix_name; + } + + var data = {name:_name, func:default_name}; if ( typeof(version) != 'undefined' ){ data['version'] = version; } else { version = ''; } - // console.log(version); var loadT = layer.msg('正在获取...', { icon: 16, time: 0, shade: 0.3 }); $.post('/plugins/run', data, function(data) { @@ -1941,27 +1946,39 @@ function pluginService(_name, version){ return; } if (data.data == 'start'){ - pluginSetService(_name, true, version); + pluginSetService(_name, true, version, _suffix_name); } else { - pluginSetService(_name, false, version); + pluginSetService(_name, false, version, _suffix_name); } },'json'); } -function pluginSetService(_name ,status, version){ +function pluginSetService(_name ,status, version, _suffix_name=''){ + + var default_name = 'status'; + var restart_name = 'restart'; + var reload_name = 'reload'; + var status_ss = (status?'stop':'start'); + if ( _suffix_name != '' ){ + default_name = 'status_'+_suffix_name; + restart_name = 'restart_'+_suffix_name; + reload_name = 'reload_'+_suffix_name; + status_ss = status_ss+'_'+_suffix_name; + } + var serviceCon ='

当前状态:'+(status ? '开启' : '关闭' )+ '

\ - \ - \ - \ + \ + \ + \
'; $(".soft-man-con").html(serviceCon); } -function pluginOpService(a, b, v) { +function pluginOpService(a, b, v, _suffix_name='') { var c = "name=" + a + "&func=" + b; if(v != ''){ @@ -1969,6 +1986,8 @@ function pluginOpService(a, b, v) { } var d = ""; + + b = b.split('_')[0]; switch(b) { case "stop":d = '停止';break; case "start":d = '启动';break; @@ -1980,15 +1999,14 @@ function pluginOpService(a, b, v) { $.post("/plugins/run", c, function(g) { layer.close(e); - var f = g.data == 'ok' ? msgTpl('{1}{2}服务已{3}',[a,v,d]) : msgTpl('{1}{2}服务{3}失败!',[a,v,d]); layer.msg(f, {icon: g.data == 'ok' ? 1 : 2}); if( b != "reload" && g.data == 'ok' ) { if ( b == 'start' ) { - pluginSetService(a, true, v); + pluginSetService(a, true, v, _suffix_name); } else if ( b == 'stop' ){ - pluginSetService(a, false, v); + pluginSetService(a, false, v, _suffix_name); } }