cauto 2 years ago
parent
commit
d6c8c0bc7d

+ 5 - 5
api/v1/nodeconfig.go

@@ -3,7 +3,7 @@ package v1
 import "github.com/gogf/gf/v2/frame/g"
 
 type NodeConfigReq struct {
-	g.Meta   `path:"/add" tags:"add" method:"get" summary:"添加节点SSH配置"`
+	g.Meta   `path:"/add" tags:"add" method:"post" summary:"添加节点SSH配置"`
 	ServerId int    `p:"serverid"`
 	SshCom   string `p:"sshcom"`
 	SshPass  string `p:"sshpass"`
@@ -13,7 +13,7 @@ type NodeConfigRes struct {
 }
 
 type NodeConfigEditReq struct {
-	g.Meta   `path:"/edit" tags:"edit" method:"get" summary:"编辑节点SSH配置"`
+	g.Meta   `path:"/edit" tags:"edit" method:"post" summary:"编辑节点SSH配置"`
 	ServerId int    `p:"serverid"`
 	SshCom   string `p:"sshcom"`
 	SshPass  string `p:"sshpass"`
@@ -22,9 +22,9 @@ type NodeConfigEditReq struct {
 type NodeConfigEditRes struct {
 }
 
-type NodeConfigDeleteReq struct {
-	g.Meta   `path:"/delete" tags:"delete" method:"get" summary:"删除SSH配置"`
+type NodeConfigDelReq struct {
+	g.Meta   `path:"/del" tags:"del" method:"post" summary:"删除SSH配置"`
 	ServerId int `p:"serverid"`
 }
-type NodeConfigDeleteRes struct {
+type NodeConfigDelRes struct {
 }

+ 25 - 0
api/v1/pingconfig.go

@@ -0,0 +1,25 @@
+package v1
+
+import (
+	"github.com/gogf/gf/v2/frame/g"
+	"nodeMonitor/internal/model/entity"
+)
+
+type PingConfigGetReq struct {
+	g.Meta `path:"/get" tags:"get" method:"get" summary:"编辑节点配置"`
+}
+type PingConfigGetRes struct {
+	g.Meta     `mime:"application/json"`
+	PingConfig *entity.Pingconfig `json:"nodeList"`
+}
+
+type PingConfigEditReq struct {
+	g.Meta    `path:"/edit" tags:"edit" method:"post" summary:"编辑节点配置"`
+	NodeDie   int    `p:"nodedie"`
+	NodeCount int    `p:"nodecount"`
+	NodeLoos  int    `p:"nodeloos"`
+	PingTime  int    `p:"pingtime"`
+	PingName  string `p:"pingname"`
+}
+type PingConfigEditRes struct {
+}

+ 1 - 2
hack/config.yaml

@@ -7,8 +7,7 @@ gfcli:
     arch: "amd64"
     system: "linux"
     mod: "none"
-    cgo: 0
-    pack: ""
+    packSrc: ""
     version: ""
     output: "./bin"
     extra: ""

+ 18 - 0
internal/cmd/cmd.go

@@ -35,6 +35,7 @@ var (
 			}
 
 			defer func() {
+
 				gcron.Stop("ping_status")
 				gcron.Remove("ping_status")
 			}()
@@ -79,6 +80,23 @@ func StartPingStart(ctx context.Context) error {
 		_, err = gcron.AddSingleton(ctx, s, func(ctx context.Context) {
 			go task.Ping(ctx)
 		}, "ping_status")
+	} else {
+
+		taskStatusTime, err := g.Cfg().Get(ctx, "node.taskStatusTime")
+		if err != nil {
+			glog.Debug(ctx, err.Error())
+			return err
+		}
+
+		taskName, err := g.Cfg().Get(ctx, "node.taskName")
+		if err != nil {
+			glog.Debug(ctx, err.Error())
+			return err
+		}
+		s := fmt.Sprintf("*/%d * * * * *", taskStatusTime.Int())
+		_, err = gcron.AddSingleton(ctx, s, func(ctx context.Context) {
+			go task.PingStatus(ctx)
+		}, taskName.String())
 	}
 
 	return nil

+ 1 - 1
internal/controller/nodeconfig.go

@@ -48,7 +48,7 @@ func (c *sNodeConfig) Edit(ctx context.Context, req *v1.NodeConfigEditReq) (res
 	return
 }
 
-func (c *sNodeConfig) Delete(ctx context.Context, req *v1.NodeConfigDeleteReq) (res *v1.NodeConfigDeleteRes, err error) {
+func (c *sNodeConfig) Del(ctx context.Context, req *v1.NodeConfigDelReq) (res *v1.NodeConfigDelRes, err error) {
 	err = service.NodeConfig().Delete(ctx, req.ServerId)
 	return
 }

+ 30 - 0
internal/controller/pingconfig.go

@@ -0,0 +1,30 @@
+package controller
+
+import (
+	"golang.org/x/net/context"
+	v1 "nodeMonitor/api/v1"
+	"nodeMonitor/internal/model"
+	"nodeMonitor/internal/service"
+)
+
+var PingConfig = sPingConfig{}
+
+type sPingConfig struct {
+}
+
+func (c *sPingConfig) Get(ctx context.Context, req *v1.PingConfigGetReq) (res *v1.PingConfigGetRes, err error) {
+	res = new(v1.PingConfigGetRes)
+
+	res.PingConfig, err = service.PingConfig().Get(ctx)
+	return
+}
+func (c *sPingConfig) Edit(ctx context.Context, req *v1.PingConfigEditReq) (res *v1.PingConfigEditRes, err error) {
+	err = service.PingConfig().Edit(ctx, model.PingConfigEditInput{
+		NodeDie:   req.NodeDie,
+		NodeCount: req.NodeCount,
+		NodeLoos:  req.NodeLoos,
+		PingTime:  req.PingTime,
+		PingName:  req.PingName,
+	})
+	return
+}

+ 23 - 0
internal/logic/node/node.go

@@ -131,3 +131,26 @@ func (c *sNode) UpdateNodeUrlStatus(ctx context.Context, input model.NodeCreateI
 	})
 	return err
 }
+
+// 获取节点执行次数
+func (c *sNode) GetNodeUrlCount(ctx context.Context, id int) (*entity.Node, error) {
+	var ccc *entity.Node
+	err := dao.Node.Ctx(ctx).Where(g.Map{
+		"id": id,
+	}).Scan(&ccc)
+
+	return ccc, err
+}
+
+// 更新执行次数
+func (c *sNode) UPdateNodeUrlCount(ctx context.Context, id int, urlCount int, urlret string, urlstatus int) error {
+
+	_, err := dao.Node.Ctx(ctx).Where(g.Map{
+		"id": id,
+	}).Update(g.Map{
+		"urlCount":   urlCount,
+		"url_status": urlstatus,
+		"urlRet":     urlret,
+	})
+	return err
+}

+ 3 - 3
internal/logic/nodeconfig/nodeconfig.go

@@ -55,7 +55,7 @@ func (c *sNodeConfig) Edit(ctx context.Context, input model.NodeConfigAndEditCre
 
 // IsExist 节点是否存在
 func (c *sNodeConfig) IsExist(ctx context.Context, serverid int) (bool, error) {
-	count, err := dao.Node.Ctx(ctx).Where(g.Map{
+	count, err := dao.Nodeconfig.Ctx(ctx).Where(g.Map{
 		"serverid": serverid,
 	}).Count()
 	if err != nil {
@@ -64,9 +64,9 @@ func (c *sNodeConfig) IsExist(ctx context.Context, serverid int) (bool, error) {
 	return count == 0, nil
 }
 
-// IsExist 节点是否存在
+// Delete 节点是否存在
 func (c *sNodeConfig) Delete(ctx context.Context, serverid int) error {
-	_, err := dao.Node.Ctx(ctx).Where(g.Map{
+	_, err := dao.Nodeconfig.Ctx(ctx).Where(g.Map{
 		"serverid": serverid,
 	}).Delete()
 

+ 2 - 1
internal/logic/pingconfig/pingconfig.go

@@ -23,10 +23,11 @@ func New() *sPingConfig {
 
 // Edit 编辑节点配置
 func (c *sPingConfig) Edit(ctx context.Context, input model.PingConfigEditInput) error {
-	_, err := dao.Pingconfig.Ctx(ctx).Update(do.Pingconfig{
+	_, err := dao.Pingconfig.Ctx(ctx).Where("pingName", input.PingName).Update(do.Pingconfig{
 		NodeDie:   input.NodeDie,
 		NodeCount: input.NodeCount,
 		NodeLoos:  input.NodeLoos,
+		PingTime:  input.PingTime,
 	})
 	return err
 }

+ 2 - 0
internal/model/node.go

@@ -26,6 +26,8 @@ type PingConfigEditInput struct {
 	NodeDie   int
 	NodeCount int
 	NodeLoos  int
+	PingTime  int
+	PingName  string
 }
 
 type NodeEditInput struct {

+ 9 - 0
internal/router/router.go

@@ -22,6 +22,7 @@ func BindController(group *ghttp.RouterGroup) {
 		//group.Middleware(ghttp.MiddlewareCORS())
 		NodeRouter(group)
 		NodeConfigRouter(group)
+		PingConfigRouter(group)
 		DomeRouter(group)
 	})
 
@@ -43,6 +44,14 @@ func NodeConfigRouter(group *ghttp.RouterGroup) {
 	})
 }
 
+func PingConfigRouter(group *ghttp.RouterGroup) {
+	group.Group("/node/ping/config", func(group *ghttp.RouterGroup) {
+		group.Bind(
+			controller.PingConfig,
+		)
+	})
+}
+
 func DomeRouter(group *ghttp.RouterGroup) {
 	group.Group("/hello", func(group *ghttp.RouterGroup) {
 		group.Bind(

+ 33 - 10
internal/task/ping.go

@@ -139,7 +139,6 @@ func AddPingLog(ctx context.Context, pingres model.PingSt, addr string, serverid
 
 func CheckNodeStatus(ctx context.Context, target *entity.Node, wg *sync.WaitGroup) {
 
-	glog.Debug(ctx, "start url req .....")
 	//获取不通的IP进程url请求
 	status, err := service.Ping().GetStatus(ctx, target.Id)
 	if err != nil {
@@ -148,28 +147,52 @@ func CheckNodeStatus(ctx context.Context, target *entity.Node, wg *sync.WaitGrou
 	}
 	if status {
 
-		if target.UrlStatus == 200 {
+		node, err := service.Node().GetNodeUrlCount(ctx, target.Id)
+		if err != nil {
+			glog.Debug(ctx, err.Error())
 			return
 		}
+		//var ping_rest nettools.IPingResult
+		urlcount := node.UrlCount
+		urlret := ""
+		ipstatus := 0
+		glog.Debug(ctx, "start url req .....")
 		client := g.Client()
 		client.SetTimeout(30 * time.Second)
 		r, err := client.Get(ctx, target.Url)
 		if err != nil {
 			glog.Debug(ctx, err.Error())
-			SSHTaskCommand(ctx, target.Host, target.Port, target.Id)
 			return
 		}
 		defer r.Close()
 		glog.Debug(ctx, "req :", target.Url, "status :", r.Status)
+		//如果成功是放回IP,如果不成功是返回空
 		if r.StatusCode == 200 {
-			err := service.Node().UpdateNodeUrlStatus(ctx, model.NodeCreateInput{Host: target.Host, UrlStatus: r.StatusCode})
-			if err != nil {
-				glog.Debug(ctx, err.Error())
-				return
-			}
-		} else {
-			SSHTaskCommand(ctx, target.Host, target.Port, target.Id)
+			urlret = r.ReadAllString()
+			//for i := 0; i < 20; i++ {
+			//	if target.PingType == 0 {
+			//		tcpping := nettools.NewIcmpPing(target.Host, time.Second*4)
+			//		ping_rest = tcpping.Ping()
+			//	} else {
+			//		tcpping := nettools.NewTcpPing(target.Host, target.Port, time.Second*4)
+			//		ping_rest = tcpping.Ping()
+			//	}
+			//}
 		}
+
+		//if ping_rest.Error() == nil {
+		//	ipstatus = 200
+		//} else {
+		//	ipstatus = 400
+		//}
+
+		urlcount++
+		err = service.Node().UPdateNodeUrlCount(ctx, target.Id, urlcount, urlret, ipstatus)
+		if err != nil {
+			glog.Debug(ctx, err.Error())
+			return
+		}
+
 	}
 
 	wg.Done()

+ 2 - 1
manifest/config/config.yaml

@@ -43,7 +43,8 @@ node:
   startTime: 900 #用于查询15分钟内的数据
   taskName: "ping_task" #任务名称
   taskStatusName: "ping_status_task" #任务名称
-  nodePing: 1 #用于表示是不是检测PING的节点
+  nodePing: 0 #用于表示是不是检测PING的节点
+  taskStatusTime: 60
 #gfToken:
 #  cacheKey: "gfToken_"
 #  timeOut: 10800