cauto 2 years ago
parent
commit
35d2cfbd84

+ 13 - 10
api/v1/node.go

@@ -4,7 +4,6 @@ import (
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/os/gcron"
 	"nodeMonitor/internal/model"
-	"nodeMonitor/internal/model/entity"
 )
 
 type NodeReq struct {
@@ -24,14 +23,18 @@ type NodeRes struct {
 
 }
 type NodeEditReq struct {
-	g.Meta    `path:"/edit" tags:"edit" method:"post" summary:"编辑"`
-	Id        int    `p:"id" v:"required#节点ID不能为空"`
-	Name      string `p:"name" v:"required#节点名称不能为空"`
-	Host      string `p:"host" v:"required#节点不能为空"`
-	Port      int    `p:"port"`
-	Url       string `p:"url"`
-	PingType  int    `p:"pingType"`
-	UrlStatus int    `p:"urlstatus"`
+	g.Meta      `path:"/edit" tags:"edit" method:"post" summary:"编辑"`
+	Id          int    `p:"id" v:"required#节点ID不能为空"`
+	Name        string `p:"name" v:"required#节点名称不能为空"`
+	Host        string `p:"host" v:"required#节点不能为空"`
+	Port        int    `p:"port"`
+	Url         string `p:"url"`
+	PingType    int    `p:"pingType"`
+	UrlStatus   int    `p:"urlstatus"`
+	IsNotice    int    `p:"isNotice"`
+	Sshcommand  string `p:"sshcommand"`
+	Sshpassword string `p:"sshpassword"`
+	Sshusername string `p:"sshusername"`
 }
 type NodeEditRes struct {
 }
@@ -51,7 +54,7 @@ type NodeAllReq struct {
 
 type NodeALlRes struct {
 	g.Meta   `mime:"application/json"`
-	NodeList []*entity.Node `json:"nodeList"`
+	NodeList []*model.NodeConfigItem `json:"nodeList"`
 }
 
 type NodeCronStartReq struct {

+ 1 - 0
go.mod

@@ -6,6 +6,7 @@ require (
 	github.com/BurntSushi/toml v1.2.1 // indirect
 	github.com/clbanning/mxj/v2 v2.5.7 // indirect
 	github.com/fsnotify/fsnotify v1.6.0 // indirect
+	github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
 	github.com/gogf/gf/contrib/drivers/mysql/v2 v2.2.1
 	github.com/gogf/gf/v2 v2.2.1
 	github.com/mattn/go-colorable v0.1.13 // indirect

+ 2 - 0
go.sum

@@ -34,6 +34,8 @@ github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq
 github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
 github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
 github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
+github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
+github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
 github.com/gogf/gf/contrib/drivers/mysql/v2 v2.2.1 h1:O3GF8PIuvECnB0BdsJnVNj+KKYfsRpDTDl07rN4uczs=
 github.com/gogf/gf/contrib/drivers/mysql/v2 v2.2.1/go.mod h1:z+/0qiOwMroAnj5ESuobTv0l5P83rf+XR3r6Fj8WJyk=
 github.com/gogf/gf/v2 v2.0.0/go.mod h1:apktt6TleWtCIwpz63vBqUnw8MX8gWKoZyxgDpXFtgM=

+ 11 - 1
internal/controller/Node.go

@@ -57,6 +57,8 @@ func (c *sNode) Del(ctx context.Context, req *v1.NodeDelReq) (res *v1.NodeDelRes
 		Id: req.Id,
 	})
 
+	err = service.NodeConfig().Delete(ctx, req.Id)
+
 	return
 }
 
@@ -69,13 +71,21 @@ func (c *sNode) Edit(ctx context.Context, req *v1.NodeEditReq) (res *v1.NodeEdit
 		Url:       req.Url,
 		PingType:  req.PingType,
 		UrlStatus: req.UrlStatus,
+		IsNotice:  req.IsNotice,
+	})
+
+	err = service.NodeConfig().Edit(ctx, model.NodeConfigAndEditCreateInput{
+		ServerId: req.Id,
+		SshCom:   req.Sshcommand,
+		SshPass:  req.Sshpassword,
+		SshUser:  req.Sshusername,
 	})
 	return
 }
 
 func (c *sNode) All(ctx context.Context, req *v1.NodeAllReq) (res *v1.NodeALlRes, err error) {
 	res = new(v1.NodeALlRes)
-	res.NodeList, err = service.Node().GetNode(ctx)
+	res.NodeList, err = service.NodeConfig().GetAll(ctx)
 	return
 }
 

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

@@ -89,6 +89,7 @@ func (c *sNode) UpdateNode(ctx context.Context, input model.NodeEditInput) error
 		"url":        input.Url,
 		"ping_type":  input.PingType,
 		"url_status": input.UrlStatus,
+		"is_notice":  input.IsNotice,
 	})
 	return err
 }

+ 15 - 4
internal/model/node.go

@@ -39,6 +39,7 @@ type NodeEditInput struct {
 	Url       string
 	PingType  int
 	UrlStatus int
+	IsNotice  int
 }
 
 type NodeDelInput struct {
@@ -82,10 +83,20 @@ type EntityNodeConfigInfo struct {
 }
 
 type NodeInfo struct {
-	g.Meta `orm:"table:node"`
-	Id     interface{} // ID主键自动添加
-	Name   interface{} // 服务器名称
-	Host   interface{} // 服务器IP
+	g.Meta    `orm:"table:node"`
+	Id        int         `json:"id"        description:"ID主键自动添加"`             // ID主键自动添加
+	Name      string      `json:"name"      description:"服务器名称"`                // 服务器名称
+	Host      string      `json:"host"      description:"服务器IP"`                // 服务器IP
+	Port      int         `json:"port"      description:"服务器端口"`                // 服务器端口
+	Url       string      `json:"url"       description:"服务器切换地址"`              // 服务器切换地址
+	UpdateAt  *gtime.Time `json:"updateAt"  description:"更新时间"`                 // 更新时间
+	CreateAt  *gtime.Time `json:"createAt"  description:"创建时间"`                 // 创建时间
+	PingType  int         `json:"pingType"  description:"服务器延迟类型 0 icmp 1 tcp"` // 服务器延迟类型 0 icmp 1 tcp
+	UrlStatus int         `json:"urlStatus" description:"用于表示url是否执行完成"`        // 用于表示url是否执行完成
+	NodeMs    int         `json:"nodeMs"    description:"服务器延迟"`                // 服务器延迟
+	UrlCount  int         `json:"urlCount"  description:"用于服务器切换IP执行次数"`        // 用于服务器切换IP执行次数
+	UrlRet    string      `json:"urlRet"    description:"切换服务器放回结果"`            // 切换服务器放回结果
+	IsNotice  int         `json:"isNotice"  description:"是否通知到电报"`              // 是否通知到电报
 }
 
 // 组合模型,用户信息

+ 49 - 34
internal/task/ping.go

@@ -10,6 +10,7 @@ import (
 	"net"
 	"nodeMonitor/internal/model"
 	"nodeMonitor/internal/service"
+	"nodeMonitor/internal/telegram"
 	"nodeMonitor/library/nettools"
 	"sync"
 	"time"
@@ -131,7 +132,7 @@ func CheckNodeStatus(ctx context.Context, nextTime time.Time) {
 	glog.Info(ctx, nextTime)
 	nodeList, err := service.Node().GetNode(ctx)
 	if err != nil {
-		glog.Debug(ctx, err.Error())
+		glog.Error(ctx, err.Error())
 		return
 	}
 	for _, target := range nodeList {
@@ -145,34 +146,47 @@ func CheckNodeStatus(ctx context.Context, nextTime time.Time) {
 
 			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())
+				glog.Error(ctx, err.Error())
 				return
 			}
-			defer r.Close()
-			glog.Debug(ctx, "req :", target.Url, "status :", r.Status)
-			//如果成功是放回IP,如果不成功是返回空
-			if r.StatusCode == 200 {
-				urlret = r.ReadAllString()
-			}
-			urlcount++
-			err = service.Node().UPdateNodeUrlCount(ctx, target.Id, urlcount, urlret, ipstatus)
-			if err != nil {
-				glog.Debug(ctx, err.Error())
-				return
+
+			if target.Url != "" {
+				urlcount := node.UrlCount
+				urlret := ""
+				ipstatus := 0
+				glog.Error(ctx, "start url req .....")
+				client := g.Client()
+				client.SetTimeout(30 * time.Second)
+				r, err := client.Get(ctx, target.Url)
+				if err != nil {
+					glog.Error(ctx, err.Error())
+					return
+				}
+				defer r.Close()
+				glog.Info(ctx, "req :", target.Url, "status :", r.Status)
+				//如果成功是放回IP,如果不成功是返回空
+				if r.StatusCode == 200 {
+					urlret = r.ReadAllString()
+				}
+				urlcount++
+				err = service.Node().UPdateNodeUrlCount(ctx, target.Id, urlcount, urlret, ipstatus)
+				if err != nil {
+					glog.Error(ctx, err.Error())
+					return
+				}
+			} else {
+				if target.IsNotice == 1 {
+					s := fmt.Sprintf("%s 节点需要更新换IP,立即执行\n", target.Host)
+					err := telegram.SetTelegramMess(ctx, s)
+					if err != nil {
+						glog.Error(ctx, err.Error())
+						return
+					}
+				}
 			}
 
+			SSHTaskCommand(ctx, target.Host, target.Port, target.Id)
+
 		}
 	}
 
@@ -182,7 +196,12 @@ func SSHTaskCommand(ctx context.Context, host string, port int, serverid int) {
 
 	nodeconfig, err := service.NodeConfig().Get(ctx, serverid)
 	if err != nil {
-		glog.Debug(ctx, err.Error())
+		glog.Error(ctx, err.Error())
+		return
+	}
+
+	if nodeconfig.Sshcommand == "" && nodeconfig.Sshpassword == "" && nodeconfig.Sshusername == "" {
+		glog.Info(ctx, "服务器命令账号密码为空")
 		return
 	}
 
@@ -195,27 +214,23 @@ func SSHTaskCommand(ctx context.Context, host string, port int, serverid int) {
 	})
 	if err != nil {
 		//log.Fatalf("SSH dial error: %s", err.Error())
-		glog.Debug(ctx, "SSH dial error:", err.Error())
+		glog.Error(ctx, "SSH dial error:", err.Error())
 		return
 	}
-
 	// 建立新会话
 	session, err := client.NewSession()
 	defer session.Close()
 	if err != nil {
 		//log.Fatalf("new session error: %s", err.Error())
-		glog.Debug(ctx, "new session error:", err.Error())
+		glog.Error(ctx, "new session error:", err.Error())
 		return
 	}
-
 	result, err := session.Output(nodeconfig.Sshcommand)
 	if err != nil {
-		//fmt.Fprintf(os.Stdout, "Failed to run command, Err:%s", err.Error())
-		//os.Exit(0)
-		glog.Debug(ctx, err.Error())
+		glog.Error(ctx, err.Error())
 		return
 	}
-	glog.Debug(ctx, result)
+	glog.Info(ctx, result)
 }
 
 func GetCronNextTime(cronStr string, t time.Time) (nextTime time.Time, err error) {

+ 21 - 0
internal/telegram/telegram.go

@@ -0,0 +1,21 @@
+package telegram
+
+import (
+	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+	"golang.org/x/net/context"
+)
+
+func SetTelegramMess(ctx context.Context, text string) error {
+	bot, err := tgbotapi.NewBotAPI("5676192709:AAFxU96iBHZwJpF9F67f0ARjketvb9rnPzg")
+	if err != nil {
+		return err
+	}
+	chatID := int64(-1001893783262)
+	replyMsg := tgbotapi.NewMessage(chatID, text)
+	_, err = bot.Send(replyMsg)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}

+ 2 - 2
manifest/config/config.yaml

@@ -31,8 +31,8 @@ database:
     Path: "resource/log/sql"
 
   default:
-    #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"
+    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 #空跑