diff --git a/conf/topo_server.yaml.templete b/conf/topo_server.yaml.templete index 9da88edcf0ef2f378691d045afdf37c08352439b..1187f450a8289052db190179d3177c0831830393 100644 --- a/conf/topo_server.yaml.templete +++ b/conf/topo_server.yaml.templete @@ -17,7 +17,7 @@ topo: # 指定图数据库,若为"",则不启用图数据库来保存并查看历史图数据(支持的图数据库:neo4j) graphDB: "" # -# 静态资源目录:*.tar.gz *.rpm,用于通过PilotGo平台向业务节点远程部署topology plugin agent或其他资源 +# 静态资源目录:*.tar.gz *.rpm *.sh,用于通过PilotGo平台向业务节点远程部署topology plugin agent或其他资源 # path: "" # #------------------------------- log ------------------------------- diff --git a/resource/collect-endpoint.sh b/resource/collect-endpoint.sh new file mode 100755 index 0000000000000000000000000000000000000000..ea08029a6e9218f23c3c500a997f13b6f448c442 --- /dev/null +++ b/resource/collect-endpoint.sh @@ -0,0 +1,62 @@ +#!/bin/bash + + +# 管理node_exporter PilotGo-plugin-topology-agent elastic-agent等3个组件,PilotGo-agent需手动管理 +# elastic-agent组件stop remove时间略长 + + +WORK_DIR="$2" + + +function watchStatus() { + # $1 port; $2 app name; $3 start|stop + if [ $3 == "start" ];then + while ! netstat -tunpl | grep ":$1" | grep 'LISTEN' ; do + sleep 1 + echo -e "\033[32m$1 $2 starting... \033[0m" + done + echo -e "\033[32m$1 $2 started \033[0m" + elif [ $3 == "stop" ];then + while netstat -tunpl | grep ":$1" | grep 'LISTEN' ; do + sleep 1 + echo -e "\033[32m$1 $2 stoping... \033[0m" + done + echo -e "\033[32m$1 $2 stopped \033[0m" + fi +} + +case "$1" in + stop) + systemctl stop node_exporter & + watchStatus 9100 node_exporter stop + systemctl stop PilotGo-plugin-topology-agent & + watchStatus 9992 PilotGo-plugin-topology-agent stop + systemctl stop elastic-agent & + watchStatus 6789 elastic-agent stop + ;; + start) + systemctl start node_exporter & + watchStatus 9100 node_exporter start + systemctl start PilotGo-plugin-topology-agent & + watchStatus 9992 PilotGo-plugin-topology-agent start + systemctl start elastic-agent & + watchStatus 6789 elastic-agent start + ;; + remove) + systemctl stop node_exporter & + watchStatus 9100 node_exporter stop + yum remove node_exporter -y + systemctl stop PilotGo-plugin-topology-agent & + watchStatus 9992 PilotGo-plugin-topology-agent stop + yum remove PilotGo-plugin-topology-agent -y + systemctl stop elastic-agent & + watchStatus 6789 elastic-agent stop + elastic-agent uninstall --force + cd /root + rm -rf $WORK_DIR + ;; + *) + echo "usage: $0 {start|stop|(remove /path/to/topo-collect)}" + ;; +esac + diff --git a/resource/deploy-collect-endpoint.sh b/resource/deploy-collect-endpoint.sh index cce99a954b0f62d0cbe5324b4fc6c216e4980c06..34c4a0c2d4fd44e5d69ae4837b5102fc9e9d65b1 100755 --- a/resource/deploy-collect-endpoint.sh +++ b/resource/deploy-collect-endpoint.sh @@ -1,7 +1,7 @@ #!/bin/bash -WORK_DIR="/root/test-topo" +WORK_DIR="/root/topo-collect" TLS_ENABLED=true @@ -118,19 +118,19 @@ while true do case "$1" in --workdir) - ${WORK_DIR}="$2" + WORK_DIR="$2" shift 2 ;; --pilotgoserver) - ${PILOTGO_SERVER_ADDR}="$2" + PILOTGO_SERVER_ADDR="$2" shift 2 ;; --toposerver) - ${TOPO_SERVER_ADDR}="$2" + TOPO_SERVER_ADDR="$2" shift 2 ;; --fleet) - ${FLEET_SERVER_ADDR}="$2" + FLEET_SERVER_ADDR="$2" shift 2 ;; --) diff --git a/server/handler/collectDeployHandler.go b/server/handler/collectDeployHandler.go index 13d593cdb8a24e3b2bf73c5c7f884c9534e19ddd..1180d0d58361897bdab6582cac8e40fda33d7f5e 100644 --- a/server/handler/collectDeployHandler.go +++ b/server/handler/collectDeployHandler.go @@ -47,7 +47,7 @@ func DeployCollectEndpointHandle(ctx *gin.Context) { MachineUUIDs: uuids.MachineUUIDs, } cmdresults, err := pluginclient.Global_Client.RunScript(batch, string(script_bytes), []string{ - "--workdir=/root/test-topo", + "--workdir=/root/topo-collect", fmt.Sprintf("--pilotgoserver=%s", pluginclient.Global_Client.Server()), fmt.Sprintf("--toposerver=%s", strings.Split(pluginclient.Global_Client.PluginInfo.Url, "//")[1]), "--fleet=10.41.161.101:8220", @@ -63,5 +63,61 @@ func DeployCollectEndpointHandle(ctx *gin.Context) { errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) } - response.Success(ctx, nil, "采集端部署完成") + response.Success(ctx, nil, "collect endpoint deploy") +} + +func CollectEndpointHandle(ctx *gin.Context) { + action := ctx.Query("action") + uuids := &struct { + MachineUUIDs []string `json:"uuids"` + }{} + if err := ctx.ShouldBind(uuids); err != nil { + err = errors.Errorf("%s **errstack**0", err.Error()) + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, "parameter error") + return + } + + file, err := os.Open(strings.TrimSuffix(conf.Global_Config.Topo.Path, "/") + "/collect-endpoint.sh") + if err != nil { + err = errors.Errorf("%s **errstack**0", err.Error()) + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, "open file error: "+errors.Cause(err).Error()) + return + } + defer file.Close() + script_bytes, err := io.ReadAll(file) + if err != nil { + err = errors.Errorf("%s **errstack**0", err.Error()) + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, "read file error: "+errors.Cause(err).Error()) + return + } + + batch := &common.Batch{ + MachineUUIDs: uuids.MachineUUIDs, + } + cmdresults := []*common.CmdResult{} + if action == "stop" { + cmdresults, err = pluginclient.Global_Client.RunScript(batch, string(script_bytes), []string{ + "stop", + }) + } else if action == "remove" { + cmdresults, err = pluginclient.Global_Client.RunScript(batch, string(script_bytes), []string{ + "remove", + "/root/topo-collect", + }) + } + if err != nil { + err = errors.Errorf("%s **errstack**0", err.Error()) + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, errors.Cause(err).Error()) + return + } + for _, res := range cmdresults { + err := errors.Errorf("collect endpoint deploy: [retcode:%d][uuid:%s][ip:%s][stdout:%s][stderr:%s] **warn**0", res.RetCode, res.MachineUUID, res.MachineIP, res.Stdout, res.Stderr) + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + } + + response.Success(ctx, nil, fmt.Sprintf("collect endpoint %s", action)) } diff --git a/server/handler/router.go b/server/handler/router.go index 9047b147faf1b1d154413c2ba119386d64a3c6f9..03a9967291cfaf499207f173fd4e3e485ff742bb 100644 --- a/server/handler/router.go +++ b/server/handler/router.go @@ -57,6 +57,7 @@ func InitRouter(router *gin.Engine) { collect := router.Group("/plugin/topology/api") { collect.POST("/deploy_collect_endpoint", DeployCollectEndpointHandle) + collect.POST("/collect_endpoint", CollectEndpointHandle) } custom := router.Group("/plugin/topology/api") diff --git a/server/pluginclient/pluginClient.go b/server/pluginclient/pluginClient.go index df9a98168a7be22866681c18975298f94563107e..73b86a6032ea3885ceaa13f143d74c6ba657c61e 100644 --- a/server/pluginclient/pluginClient.go +++ b/server/pluginclient/pluginClient.go @@ -87,7 +87,19 @@ func GetExtentions() { URL: "/plugin/topology/api/deploy_collect_endpoint", Permission: "plugin.topology.agent/install", } - ex = append(ex, pe1, pe2, me1) + me2 := &common.MachineExtention{ + Type: common.ExtentionMachine, + Name: "停用topo-collect", + URL: "/plugin/topology/api/collect_endpoint?action=stop", + Permission: "plugin.topology.agent/stop", + } + me3 := &common.MachineExtention{ + Type: common.ExtentionMachine, + Name: "卸载topo-collect", + URL: "/plugin/topology/api/collect_endpoint?action=remove", + Permission: "plugin.topology.agent/remove", + } + ex = append(ex, pe1, pe2, me1, me2, me3) Global_Client.RegisterExtention(ex) }