cauto 2 years ago
parent
commit
4f4487bb59

+ 0 - 10
api/v1/node.go

@@ -90,13 +90,3 @@ type NodeStatusReq struct {
 type NodeStatusRes struct {
 	NodeItme []*model.UserItem
 }
-
-type NodeConfigReq struct {
-	g.Meta   `path:"/nodeconfig" tags:"nodeconfig" method:"get" summary:"节点配置请求"`
-	ServerId int    `p:"serverid"`
-	SshCom   string `p:"sshcom"`
-	SshPass  string `p:"sshpass"`
-	SshUser  string `p:"sshuser"`
-}
-type NodeConfigRes struct {
-}

+ 30 - 0
api/v1/nodeconfig.go

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

+ 54 - 0
internal/controller/nodeconfig.go

@@ -0,0 +1,54 @@
+package controller
+
+import (
+	"github.com/gogf/gf/v2/errors/gcode"
+	"github.com/gogf/gf/v2/errors/gerror"
+	"golang.org/x/net/context"
+	v1 "nodeMonitor/api/v1"
+	"nodeMonitor/internal/model"
+	"nodeMonitor/internal/service"
+)
+
+var NodeConfig = sNodeConfig{}
+
+type sNodeConfig struct {
+}
+
+/*
+*
+添加node配置接口
+*/
+func (c *sNodeConfig) Add(ctx context.Context, req *v1.NodeConfigReq) (res *v1.NodeConfigRes, err error) {
+	exist, err := service.NodeConfig().IsExist(ctx, req.ServerId)
+	if err != nil {
+		return
+	}
+	if !exist {
+		err = gerror.NewCode(gcode.New(401, "", nil), "数据已经存在")
+		return
+	}
+
+	err = service.NodeConfig().Create(ctx, model.NodeConfigAndEditCreateInput{
+		ServerId: req.ServerId,
+		SshCom:   req.SshCom,
+		SshPass:  req.SshPass,
+		SshUser:  req.SshUser,
+	})
+
+	return
+}
+
+func (c *sNodeConfig) Edit(ctx context.Context, req *v1.NodeConfigEditReq) (res *v1.NodeConfigEditRes, err error) {
+	err = service.NodeConfig().Edit(ctx, model.NodeConfigAndEditCreateInput{
+		ServerId: req.ServerId,
+		SshCom:   req.SshCom,
+		SshPass:  req.SshPass,
+		SshUser:  req.SshUser,
+	})
+	return
+}
+
+func (c *sNodeConfig) Delete(ctx context.Context, req *v1.NodeConfigDeleteReq) (res *v1.NodeConfigDeleteRes, err error) {
+	err = service.NodeConfig().Delete(ctx, req.ServerId)
+	return
+}

+ 20 - 0
internal/logic/nodeconfig/nodeConfig.go → internal/logic/nodeconfig/nodeconfig.go

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

+ 2 - 2
internal/logic/pingconfig/pingConfig.go → internal/logic/pingconfig/pingconfig.go

@@ -23,7 +23,7 @@ func New() *sPingConfig {
 
 // Edit 编辑节点配置
 func (c *sPingConfig) Edit(ctx context.Context, input model.PingConfigEditInput) error {
-	_, err := dao.Nodeconfig.Ctx(ctx).Update(do.Pingconfig{
+	_, err := dao.Pingconfig.Ctx(ctx).Update(do.Pingconfig{
 		NodeDie:   input.NodeDie,
 		NodeCount: input.NodeCount,
 		NodeLoos:  input.NodeLoos,
@@ -34,6 +34,6 @@ func (c *sPingConfig) Edit(ctx context.Context, input model.PingConfigEditInput)
 // Get   编辑节点配置
 func (c *sPingConfig) Get(ctx context.Context) (*entity.Pingconfig, error) {
 	var pingconfig = new(entity.Pingconfig)
-	err := dao.Nodeconfig.Ctx(ctx).Scan(&pingconfig)
+	err := dao.Pingconfig.Ctx(ctx).Scan(&pingconfig)
 	return pingconfig, err
 }

+ 8 - 2
internal/logic/pinglog/pinglog.go

@@ -49,14 +49,20 @@ func (c *sPing) GetStatus(ctx context.Context, serverid int) (bool, error) {
 		return false, err
 	}
 	//查询的数据超过3次就是不ok了
-	loos, err := g.Cfg().Get(ctx, "node.loos")
+	//loos, err := g.Cfg().Get(ctx, "node.loos")
+	//if err != nil {
+	//	glog.Debug(ctx, err.Error())
+	//	return false, err
+	//}
+
+	pingConfig, err := service.PingConfig().Get(ctx)
 	if err != nil {
 		glog.Debug(ctx, err.Error())
 		return false, err
 	}
 
 	timeStartStr := gtime.Now().Add(-startTime.Duration() * time.Second).String()
-	count, err := dao.Pinglog.Ctx(ctx).Where("create_at > ?", timeStartStr).Where("avgdelay = 0").Where("losspk > ?", loos.Int()).Where("maxdelay = 0 ").Where("serverid = ?", serverid).Count()
+	count, err := dao.Pinglog.Ctx(ctx).Where("create_at > ?", timeStartStr).Where("avgdelay = 0").Where("losspk > ?", pingConfig.NodeLoos).Where("maxdelay = 0 ").Where("serverid = ?", serverid).Count()
 
 	return count >= 3, err
 }

+ 9 - 0
internal/router/router.go

@@ -21,6 +21,7 @@ func BindController(group *ghttp.RouterGroup) {
 		//group.Middleware(middleware.Middleware().CORS)
 		//group.Middleware(ghttp.MiddlewareCORS())
 		NodeRouter(group)
+		NodeConfigRouter(group)
 		DomeRouter(group)
 	})
 
@@ -34,6 +35,14 @@ func NodeRouter(group *ghttp.RouterGroup) {
 	})
 }
 
+func NodeConfigRouter(group *ghttp.RouterGroup) {
+	group.Group("/node/config", func(group *ghttp.RouterGroup) {
+		group.Bind(
+			controller.NodeConfig,
+		)
+	})
+}
+
 func DomeRouter(group *ghttp.RouterGroup) {
 	group.Group("/hello", func(group *ghttp.RouterGroup) {
 		group.Bind(

+ 8 - 18
internal/task/ping.go

@@ -81,7 +81,7 @@ func PingTask(ctx context.Context, t string, wg *sync.WaitGroup, pingType int, h
 
 			if rest.Error() == nil {
 				delay := rest.Result()
-				//seelog.Info("[func:IcmpPing] Finish Addr:", addr, delay, "ms")
+				glog.Debug(ctx, "[func:IcmpPing] Finish Addr:", addr, delay, "ms")
 				stat.AvgDelay = stat.AvgDelay + rest.Result()
 				if stat.MaxDelay < delay {
 					stat.MaxDelay = delay
@@ -156,7 +156,7 @@ func CheckNodeStatus(ctx context.Context, target *entity.Node, wg *sync.WaitGrou
 		r, err := client.Get(ctx, target.Url)
 		if err != nil {
 			glog.Debug(ctx, err.Error())
-			SSHTaskCommand(ctx, target.Host, target.Port)
+			SSHTaskCommand(ctx, target.Host, target.Port, target.Id)
 			return
 		}
 		defer r.Close()
@@ -168,7 +168,7 @@ func CheckNodeStatus(ctx context.Context, target *entity.Node, wg *sync.WaitGrou
 				return
 			}
 		} else {
-			SSHTaskCommand(ctx, target.Host, target.Port)
+			SSHTaskCommand(ctx, target.Host, target.Port, target.Id)
 		}
 	}
 
@@ -176,29 +176,19 @@ func CheckNodeStatus(ctx context.Context, target *entity.Node, wg *sync.WaitGrou
 
 }
 
-func SSHTaskCommand(ctx context.Context, host string, port int) {
+func SSHTaskCommand(ctx context.Context, host string, port int, serverid int) {
 
-	sshPassword, err := g.Cfg().Get(ctx, "node.sshPassword")
-	if err != nil {
-		glog.Debug(ctx, err.Error())
-		return
-	}
-	sshUsername, err := g.Cfg().Get(ctx, "node.sshUsername")
+	nodeconfig, err := service.NodeConfig().Get(ctx, serverid)
 	if err != nil {
 		glog.Debug(ctx, err.Error())
 		return
 	}
 
-	sshCommand, err := g.Cfg().Get(ctx, "node.sshCommand")
-	if err != nil {
-		glog.Debug(ctx, err.Error())
-		return
-	}
 	s2 := fmt.Sprintf("%s:%d\n", host, port)
 	// 建立SSH客户端连接
 	client, err := ssh.Dial("tcp", s2, &ssh.ClientConfig{
-		User:            sshUsername.String(),
-		Auth:            []ssh.AuthMethod{ssh.Password(sshPassword.String())},
+		User:            nodeconfig.Sshusername,
+		Auth:            []ssh.AuthMethod{ssh.Password(nodeconfig.Sshpassword)},
 		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
 	})
 	if err != nil {
@@ -216,7 +206,7 @@ func SSHTaskCommand(ctx context.Context, host string, port int) {
 		return
 	}
 
-	result, err := session.Output(sshCommand.String())
+	result, err := session.Output(nodeconfig.Sshcommand)
 	if err != nil {
 		//fmt.Fprintf(os.Stdout, "Failed to run command, Err:%s", err.Error())
 		//os.Exit(0)

+ 1 - 6
manifest/config/config.yaml

@@ -40,15 +40,10 @@ database:
     maxLifetime: 30 #(单位秒)连接对象可重复使用的时间长度
 
 node:
-  die: 3    #用于判断线路 不通的次数
-  pingCount: 20 #用于检测线路的次数
   startTime: 900 #用于查询15分钟内的数据
-  loos: 30 #用于丢包率检测 如果丢包率>当前值就是坏了
   taskName: "ping_task" #任务名称
   taskStatusName: "ping_status_task" #任务名称
-  sshCommand: "" #用于执行ssh 命令
-  sshPassword: "" #ssh密码
-  sshUsername: "root"
+  nodePing: 1 #用于表示是不是检测PING的节点
 #gfToken:
 #  cacheKey: "gfToken_"
 #  timeOut: 10800