Browse Source

添加ping恢复自动执行 ssh命令

alroyso 9 months ago
parent
commit
e6fc82f15f
3 changed files with 48 additions and 19 deletions
  1. 4 1
      README.MD
  2. 16 17
      hack/config.yaml
  3. 28 1
      internal/task/ping.go

+ 4 - 1
README.MD

@@ -12,4 +12,7 @@ Project Makefile Commands:
 编译
 gf build main.go -n nodeMonitor -a amd64 -s linux
 
-gf build main.go -n nodeMonitorTask -a amd64 -s linux
+gf build main.go -n nodeMonitorTask -a amd64 -s linux
+
+
+gf build tak_main.go -n nodeMonitorTask -a amd64 -s linux -p /bin

+ 16 - 17
hack/config.yaml

@@ -3,23 +3,22 @@
 # https://goframe.org/pages/viewpage.action?pageId=3673173
 gfcli:
   build:
-    name: "nodeMonitor"
-    arch: "amd64"
-    system: "linux"
-    mod: "none"
-    packSrc: ""
-    version: ""
-    output: "./bin"
-    extra: ""
-  buildTask:
-    name: "nodeMonitorTask"
-    arch: "amd64"
-    system: "linux"
-    mod: "none"
-    packSrc: ""
-    version: ""
-    output: "./bin"
-    extra: ""
+    - name: "nodeMonitor"
+      arch: "amd64"
+      system: "linux"
+      mod: "none"
+      packSrc: "main.go"
+      version: ""
+      output: "./bin/nodeMonitor"
+      extra: ""
+    - name: "nodeMonitorTask"
+      arch: "amd64"
+      system: "linux"
+      mod: "none"
+      packSrc: "task_main.go"
+      version: ""
+      output: "./bin/nodeMonitorTask"
+      extra: ""
   gen:
     dao:
       - link: "mysql:nodemonitor:m4A6zLaDnRCNd4xw@tcp(43.129.242.146:33060)/nodemonitor?loc=Local&parseTime=true"

+ 28 - 1
internal/task/ping.go

@@ -17,6 +17,10 @@ import (
 	"time"
 )
 
+// 定义一个全局变量保存节点的历史状态和恢复状态
+var nodeStatusHistory = make(map[int]bool)
+var nodeRecovered = make(map[int]bool)
+
 func Ping(ctx context.Context) {
 	glog.Debug(ctx, "start ping .....")
 	var wg sync.WaitGroup
@@ -146,14 +150,37 @@ func CheckNodeStatus(ctx context.Context, nextTime time.Time) {
 			service.TaskLog().Create(ctx, "task_url", err.Error())
 			return
 		}
+
+		// 获取节点的历史状态
+		historyStatus, exists := nodeStatusHistory[target.Id]
+		if !exists {
+			historyStatus = true // 假设初始状态为正常
+		}
+
 		if status {
 			service.TaskLog().Create(ctx, "CheckNodeStatus", fmt.Sprintf("当前节点不正常开启执行urlAndSSH...%d --- %s\n", target.Id, target.Host))
 			go URLTaskCommand(ctx, target, &wg)
+			// 如果节点当前不正常,重置恢复标志
+			nodeRecovered[target.Id] = false
 		} else {
 			service.TaskLog().Create(ctx, "CheckNodeStatus", fmt.Sprintf("当前节点正常...%d --- %s\n", target.Id, target.Host))
+
+			// 检查节点是否从不正常恢复到正常,并且尚未处理过
+			if !historyStatus && !nodeRecovered[target.Id] {
+				// 节点恢复后执行 SSH 命令
+				go func(target *entity.Node) {
+					defer wg.Done()
+					SSHTaskCommand(ctx, target, target.Host, target.Port, target.Id)
+					// 将节点标记为已处理恢复状态
+					nodeRecovered[target.Id] = true
+				}(target)
+			} else {
+				wg.Done()
+			}
 		}
 
-		//go SSHTaskCommand(ctx, target, target.Host, target.Port, target.Id, &wg)
+		// 更新节点的历史状态
+		nodeStatusHistory[target.Id] = status
 	}
 
 	wg.Wait()