cauto 2 éve
szülő
commit
60a889fe75

+ 9 - 6
api/v1/node.go

@@ -8,12 +8,15 @@ import (
 )
 
 type NodeReq struct {
-	g.Meta   `path:"/add" tags:"add" method:"post" summary:"增加节点"`
-	Name     string `p:"name" v:"required#节点名称不能为空"`
-	Host     string `p:"host" v:"required#节点不能为空"`
-	Port     int    `p:"port"`
-	Url      string `p:"url"`
-	PingType int    `p:"pingType"`
+	g.Meta      `path:"/add" tags:"add" method:"post" summary:"增加节点"`
+	Name        string `p:"name" v:"required#节点名称不能为空"`
+	Host        string `p:"host" v:"required#节点不能为空"`
+	Port        int    `p:"port"`
+	Url         string `p:"url"`
+	PingType    int    `p:"pingType"`
+	Sshcommand  string `p:"sshcommand"`
+	Sshpassword string `p:"sshpassword"`
+	Sshusername string `p:"sshusername"`
 }
 
 type NodeRes struct {

+ 10 - 1
api/v1/nodeconfig.go

@@ -1,7 +1,16 @@
 package v1
 
-import "github.com/gogf/gf/v2/frame/g"
+import (
+	"github.com/gogf/gf/v2/frame/g"
+	"nodeMonitor/internal/model"
+)
 
+type NodeConfigAllReq struct {
+	g.Meta `path:"/all" tags:"all" method:"get" summary:"获取全部"`
+}
+type NodeConfigAllRes struct {
+	NodeConfigItem []*model.NodeConfigItem
+}
 type NodeConfigReq struct {
 	g.Meta   `path:"/add" tags:"add" method:"post" summary:"添加节点SSH配置"`
 	ServerId int    `p:"serverid"`

+ 1 - 1
api/v1/pingconfig.go

@@ -10,7 +10,7 @@ type PingConfigGetReq struct {
 }
 type PingConfigGetRes struct {
 	g.Meta     `mime:"application/json"`
-	PingConfig *entity.Pingconfig `json:"nodeList"`
+	PingConfig *entity.Pingconfig `json:"PingConfig"`
 }
 
 type PingConfigEditReq struct {

+ 33 - 6
db/nodeMonitor.sql

@@ -1,17 +1,17 @@
 /*
  Navicat Premium Data Transfer
 
- Source Server         : localhost
+ Source Server         : 156.234.193.212
  Source Server Type    : MySQL
  Source Server Version : 50734
- Source Host           : localhost:3306
- Source Schema         : nodeMonitor
+ Source Host           : 156.234.193.212:33060
+ Source Schema         : nodemonitor
 
  Target Server Type    : MySQL
  Target Server Version : 50734
  File Encoding         : 65001
 
- Date: 07/11/2022 14:51:21
+ Date: 10/11/2022 10:21:15
 */
 
 SET NAMES utf8mb4;
@@ -32,8 +32,35 @@ CREATE TABLE `node` (
   `ping_type` int(11) NOT NULL DEFAULT '0' COMMENT '服务器延迟类型 0 icmp 1 tcp',
   `url_status` int(11) DEFAULT NULL COMMENT '用于表示url是否执行完成',
   `nodeMs` int(11) DEFAULT NULL COMMENT ' 服务器延迟',
+  `url_count` int(11) DEFAULT '0' COMMENT '用于服务器切换IP执行次数',
+  `url_ret` varchar(128) DEFAULT NULL COMMENT '切换服务器放回结果',
   PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8mb4;
+) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;
+
+-- ----------------------------
+-- Table structure for nodeconfig
+-- ----------------------------
+DROP TABLE IF EXISTS `nodeconfig`;
+CREATE TABLE `nodeconfig` (
+  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID主键',
+  `serverid` int(11) NOT NULL COMMENT '服务器ID',
+  `sshcommand` text NOT NULL COMMENT '服务器执行命令',
+  `sshpassword` varchar(128) DEFAULT NULL COMMENT '服务器密码',
+  `sshusername` varchar(128) DEFAULT NULL COMMENT 'ssh账号',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
+
+-- ----------------------------
+-- Table structure for pingconfig
+-- ----------------------------
+DROP TABLE IF EXISTS `pingconfig`;
+CREATE TABLE `pingconfig` (
+  `node_die` int(11) DEFAULT '3' COMMENT '节点死亡次数用于判断节节点可用性',
+  `node_count` int(11) DEFAULT '20' COMMENT '节点检测次数',
+  `node_loos` int(11) DEFAULT '30' COMMENT '节点丢包率',
+  `ping_time` int(11) DEFAULT '60' COMMENT '节点计划任务开启时间',
+  `ping_name` varchar(128) DEFAULT 'ping_config' COMMENT '配置名称'
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
 -- ----------------------------
 -- Table structure for pinglog
@@ -49,6 +76,6 @@ CREATE TABLE `pinglog` (
   `create_at` datetime DEFAULT NULL COMMENT '创建时间',
   `serverid` int(11) DEFAULT NULL COMMENT '服务器ID',
   PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=343 DEFAULT CHARSET=utf8mb4;
+) ENGINE=InnoDB AUTO_INCREMENT=3946 DEFAULT CHARSET=utf8mb4;
 
 SET FOREIGN_KEY_CHECKS = 1;

+ 10 - 0
internal/controller/Node.go

@@ -20,6 +20,7 @@ type sNode struct {
 }
 
 func (c *sNode) Add(ctx context.Context, req *v1.NodeReq) (res *v1.NodeRes, err error) {
+	//res = new(v1.NodeRes)
 	exist, err := service.Node().IsExist(ctx, req.Host)
 	if err != nil {
 		return
@@ -37,6 +38,15 @@ func (c *sNode) Add(ctx context.Context, req *v1.NodeReq) (res *v1.NodeRes, err
 		PingType: req.PingType,
 	})
 
+	server, err := service.Node().FindServer(ctx, req.Host)
+
+	err = service.NodeConfig().Create(ctx, model.NodeConfigAndEditCreateInput{
+		ServerId: server.Id,
+		SshCom:   req.Sshcommand,
+		SshPass:  req.Sshpassword,
+		SshUser:  req.Sshusername,
+	})
+
 	return
 }
 

+ 5 - 0
internal/controller/login.go

@@ -2,6 +2,8 @@ package controller
 
 import (
 	"github.com/gogf/gf/v2/crypto/gmd5"
+	"github.com/gogf/gf/v2/errors/gcode"
+	"github.com/gogf/gf/v2/errors/gerror"
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/net/ghttp"
 	"github.com/gogf/gf/v2/os/glog"
@@ -46,6 +48,9 @@ func (c *sLogin) Login(ctx context.Context, req *v1.LoginReq) (*v1.LoginRes, err
 			return res, err
 		}
 		res.Token = token
+	} else {
+		err = gerror.NewCode(gcode.New(401, "", nil), "账号或者密码错误")
+		return res, err
 	}
 
 	return res, nil

+ 6 - 4
internal/controller/nodeconfig.go

@@ -14,10 +14,12 @@ var NodeConfig = sNodeConfig{}
 type sNodeConfig struct {
 }
 
-/*
-*
-添加node配置接口
-*/
+func (c *sNodeConfig) All(ctx context.Context, req *v1.NodeConfigAllReq) (res *v1.NodeConfigAllRes, err error) {
+	res = new(v1.NodeConfigAllRes)
+	res.NodeConfigItem, err = service.NodeConfig().GetAll(ctx)
+	return
+}
+
 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 {

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

@@ -40,6 +40,14 @@ func (c *sNode) Create(ctx context.Context, input model.NodeCreateInput) error {
 		return err
 	})
 }
+func (c *sNode) FindServer(ctx context.Context, host string) (*entity.Node, error) {
+	var data *entity.Node
+	err := dao.Node.Ctx(ctx).Where("host", host).Scan(&data)
+	if err != nil {
+		return nil, err
+	}
+	return data, nil
+}
 
 // UpdateNodeMs 更新节点延迟1
 func (c *sNode) UpdateNodeMs(ctx context.Context, input model.NodeUpdateInput) error {

+ 15 - 0
internal/logic/nodeconfig/nodeconfig.go

@@ -22,6 +22,21 @@ func New() *sNodeConfig {
 	return &sNodeConfig{}
 }
 
+func (c *sNodeConfig) GetAll(ctx context.Context) (nodeConfig []*model.NodeConfigItem, err error) {
+	var node []*model.NodeConfigItem
+	err = dao.Node.Ctx(ctx).OrderDesc("create_at").ScanList(&node, "Node")
+	if err != nil {
+		return nil, err
+	}
+	err = dao.Nodeconfig.Ctx(ctx).Where("serverid", gdb.ListItemValuesUnique(node, "Node", "Id")).ScanList(&node, "NodeConfig", "Node", "serverid=id")
+
+	if err != nil {
+		return nil, err
+	}
+	nodeConfig = node
+	return
+}
+
 // Get 获取节点配置
 func (c *sNodeConfig) Get(ctx context.Context, id int) (nodeConfig *entity.Nodeconfig, err error) {
 	var node = new(entity.Nodeconfig)

+ 14 - 6
internal/logic/pingconfig/pingconfig.go

@@ -1,10 +1,10 @@
 package pingconfig
 
 import (
+	"github.com/gogf/gf/v2/frame/g"
 	"golang.org/x/net/context"
 	"nodeMonitor/internal/dao"
 	"nodeMonitor/internal/model"
-	"nodeMonitor/internal/model/do"
 	"nodeMonitor/internal/model/entity"
 	"nodeMonitor/internal/service"
 )
@@ -23,11 +23,19 @@ func New() *sPingConfig {
 
 // Edit 编辑节点配置
 func (c *sPingConfig) Edit(ctx context.Context, input model.PingConfigEditInput) error {
-	_, err := dao.Pingconfig.Ctx(ctx).Where("pingName", input.PingName).Update(do.Pingconfig{
-		NodeDie:   input.NodeDie,
-		NodeCount: input.NodeCount,
-		NodeLoos:  input.NodeLoos,
-		PingTime:  input.PingTime,
+	/*
+		do.Pingconfig{
+				NodeDie:   input.NodeDie,
+				NodeCount: input.NodeCount,
+				NodeLoos:  input.NodeLoos,
+				PingTime:  input.PingTime,
+			}
+	*/
+	_, err := dao.Pingconfig.Ctx(ctx).Where("ping_name", input.PingName).Update(g.Map{
+		"node_die":   input.NodeDie,
+		"node_count": input.NodeCount,
+		"node_loos":  input.NodeLoos,
+		"ping_time":  input.PingTime,
 	})
 	return err
 }

+ 6 - 0
internal/middleware/auth.go

@@ -34,6 +34,12 @@ func (s *middlewareImpl) CORS(r *ghttp.Request) {
 // Ctx 自定义上下文对象
 func (s *middlewareImpl) Ctx(r *ghttp.Request) {
 	ctx := r.GetCtx()
+	//url := gstr.TrimLeft(r.Request.URL.Path, "/")
+	//if !gstr.Contains(url, "/api/v1/node/add") {
+	//	// 执行下一步请求逻辑
+	//	r.Middleware.Next()
+	//}
+	//r.Request.URL.Path
 	// 初始化登录用户信息
 	data, err := base.GfToken().ParseToken(r)
 	if err != nil {

+ 23 - 0
internal/model/node.go

@@ -1,6 +1,7 @@
 package model
 
 import (
+	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/os/gtime"
 	"github.com/gogf/gf/v2/util/gmeta"
 )
@@ -70,3 +71,25 @@ type UserItem struct {
 	Node    *EntityNodeInfo
 	PingLog []*EntityNodePingLog
 }
+
+type EntityNodeConfigInfo struct {
+	g.Meta      `orm:"table:nodeconfig"`
+	Id          interface{} // ID主键
+	Serverid    interface{} // 服务器ID
+	Sshcommand  interface{} // 服务器执行命令
+	Sshpassword interface{} // 服务器密码
+	Sshusername interface{} // ssh账号
+}
+
+type NodeInfo struct {
+	g.Meta `orm:"table:node"`
+	Id     interface{} // ID主键自动添加
+	Name   interface{} // 服务器名称
+	Host   interface{} // 服务器IP
+}
+
+// 组合模型,用户信息
+type NodeConfigItem struct {
+	Node       *NodeInfo
+	NodeConfig []*EntityNodeConfigInfo
+}

+ 1 - 1
internal/router/router.go

@@ -50,7 +50,7 @@ func NodeConfigRouter(group *ghttp.RouterGroup) {
 }
 
 func PingConfigRouter(group *ghttp.RouterGroup) {
-	group.Group("/node/ping/config", func(group *ghttp.RouterGroup) {
+	group.Group("/ping/config", func(group *ghttp.RouterGroup) {
 		group.Bind(
 			controller.PingConfig,
 		)

+ 4 - 2
manifest/config/config.yaml

@@ -31,7 +31,8 @@ database:
     Path: "resource/log/sql"
 
   default:
-    link: "mysql:nodemonitor:m4A6zLaDnRCNd4xw@tcp(127.0.0.1:33060)/nodemonitor?loc=Local&parseTime=true" #156.234.193.212
+    #link: "mysql:nodemonitor:m4A6zLaDnRCNd4xw@tcp(156.234.193.212:33060)/nodemonitor?loc=Local&parseTime=true" #156.234.193.212
+    link: "mysql:root:123456@tcp(127.0.0.1:3306)/nodeMonitor?loc=Local&parseTime=true"
     debug: true
     charset: "utf8mb4" #数据库编码
     dryRun: false #空跑
@@ -57,7 +58,8 @@ gfToken:
   cacheModel: "redis"
   excludePaths:
     - "/api/v1/auth/login"
-    - "api/v1/auth/loginout"
+    - "/api/v1/auth/loginout"
+    - "/api/v1/node/add"
 
 # Redis 配置示例
 redis: