|
@@ -6,7 +6,6 @@ import (
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
|
|
"github.com/robfig/cron"
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
- "golang.org/x/crypto/ssh/knownhosts"
|
|
|
"golang.org/x/net/context"
|
|
|
"net"
|
|
|
"nodeMonitor/internal/model"
|
|
@@ -136,21 +135,25 @@ func CheckNodeStatus(ctx context.Context, nextTime time.Time) {
|
|
|
glog.Error(ctx, err.Error())
|
|
|
return
|
|
|
}
|
|
|
+ taskLogMsg := ""
|
|
|
for _, target := range nodeList {
|
|
|
//获取不通的IP进程url请求
|
|
|
glog.Info(ctx, "获取不通的IP进程url请求")
|
|
|
-
|
|
|
+ //taskLogMsg = taskLogMsg + ":" + "获取不通的IP进程url请求"
|
|
|
status, err := service.Ping().GetStatus(ctx, target.Id)
|
|
|
if err != nil {
|
|
|
glog.Error(ctx, err.Error())
|
|
|
+ service.TaskLog().Create(ctx, "task_url", err.Error())
|
|
|
return
|
|
|
}
|
|
|
if status {
|
|
|
glog.Info(ctx, "查询到15分钟的数据")
|
|
|
- service.TaskLog().Create(ctx, "task_url", "查询到15分钟的数据")
|
|
|
+ taskLogMsg = taskLogMsg + target.Name + ":" + "查询到15分钟的数据"
|
|
|
+ //service.TaskLog().Create(ctx, "task_url", "查询到15分钟的数据")
|
|
|
node, err := service.Node().GetNodeUrlCount(ctx, target.Id)
|
|
|
if err != nil {
|
|
|
glog.Error(ctx, err.Error())
|
|
|
+ service.TaskLog().Create(ctx, "task_url", err.Error())
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -160,55 +163,66 @@ func CheckNodeStatus(ctx context.Context, nextTime time.Time) {
|
|
|
ipstatus := 0
|
|
|
glog.Info(ctx, "start url req .....")
|
|
|
s := fmt.Sprintf("url_log ---- 开始请求%s\n", target.Url)
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s
|
|
|
glog.Info(ctx, s)
|
|
|
- service.TaskLog().Create(ctx, "task_url", s)
|
|
|
+ service.TaskLog().Create(ctx, "task_url", taskLogMsg)
|
|
|
client := g.Client()
|
|
|
client.SetTimeout(30 * time.Second)
|
|
|
r, err := client.Get(ctx, target.Url)
|
|
|
if err != nil {
|
|
|
s := fmt.Sprintf("url_log ---- 请求错误%s\n", err.Error())
|
|
|
- service.TaskLog().Create(ctx, "task_url", s)
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s
|
|
|
+ service.TaskLog().Create(ctx, "task_url", taskLogMsg)
|
|
|
glog.Info(ctx, s)
|
|
|
glog.Error(ctx, err.Error())
|
|
|
return
|
|
|
}
|
|
|
defer r.Close()
|
|
|
- service.TaskLog().Create(ctx, "task_url", fmt.Sprintf("%s --- %s\n", target.Url, r.Status))
|
|
|
+ taskLogMsg = taskLogMsg + ":" + fmt.Sprintf("%s --- %s\n", target.Url, r.Status)
|
|
|
+ //service.TaskLog().Create(ctx, "task_url", fmt.Sprintf("%s --- %s\n", target.Url, r.Status))
|
|
|
glog.Info(ctx, "req :", target.Url, "status :", r.Status)
|
|
|
//如果成功是放回IP,如果不成功是返回空
|
|
|
if r.StatusCode == 200 {
|
|
|
urlret = r.ReadAllString()
|
|
|
s := fmt.Sprintf("url_log ---- url结果和状态%s --- %d\n", urlret, r.StatusCode)
|
|
|
- service.TaskLog().Create(ctx, "task_url", s)
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s
|
|
|
+ service.TaskLog().Create(ctx, "task_url", taskLogMsg)
|
|
|
}
|
|
|
urlcount++
|
|
|
s = fmt.Sprintf("url_log --- url执行次数%d\n", urlcount)
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s
|
|
|
glog.Info(ctx, s)
|
|
|
- service.TaskLog().Create(ctx, "task_url", s)
|
|
|
+ service.TaskLog().Create(ctx, "url_log", taskLogMsg)
|
|
|
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("telegram_log --- %s 节点需要更新换IP,立即执行\n", target.Name)
|
|
|
glog.Info(ctx, s)
|
|
|
- service.TaskLog().Create(ctx, "telegram_log", s)
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s
|
|
|
+ //service.TaskLog().Create(ctx, "telegram_log", s)
|
|
|
not := fmt.Sprintf(" @new_pumpcloud %s 节点需要更新换IP,请帮我立即更换,谢谢!\n", target.Name)
|
|
|
- service.TaskLog().Create(ctx, "telegram_log", s)
|
|
|
+ service.TaskLog().Create(ctx, "telegram_log", taskLogMsg)
|
|
|
+ //taskLogMsg = taskLogMsg + ":" + s
|
|
|
err := telegram.SetTelegramMess(ctx, not)
|
|
|
if err != nil {
|
|
|
glog.Error(ctx, err.Error())
|
|
|
s := fmt.Sprintf("telegram_log --- 电报发送: %s", err.Error())
|
|
|
- service.TaskLog().Create(ctx, "telegram_log", s)
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s
|
|
|
+ //service.TaskLog().Create(ctx, "telegram_log", s)
|
|
|
+ service.TaskLog().Create(ctx, "telegram_log", taskLogMsg)
|
|
|
return
|
|
|
}
|
|
|
+ //service.TaskLog().Create(ctx, "telegram_log", taskLogMsg)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- SSHTaskCommand(ctx, target.Host, target.Port, target.Id)
|
|
|
+ go SSHTaskCommand(ctx, target.Host, target.Port, target.Id)
|
|
|
|
|
|
}
|
|
|
}
|
|
@@ -216,35 +230,51 @@ func CheckNodeStatus(ctx context.Context, nextTime time.Time) {
|
|
|
}
|
|
|
|
|
|
func SSHTaskCommand(ctx context.Context, host string, port int, serverid int) {
|
|
|
-
|
|
|
+ taskLogMsg := host
|
|
|
nodeconfig, err := service.NodeConfig().Get(ctx, serverid)
|
|
|
if err != nil {
|
|
|
glog.Error(ctx, err.Error())
|
|
|
+ err := service.TaskLog().Create(ctx, "task_ssh", err.Error())
|
|
|
+ if err != nil {
|
|
|
+ glog.Info(ctx, "task_log:", err.Error())
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if nodeconfig.Sshcommand == "" && nodeconfig.Sshpassword == "" && nodeconfig.Sshusername == "" {
|
|
|
glog.Info(ctx, "服务器命令账号密码为空")
|
|
|
- service.TaskLog().Create(ctx, "task_ssh", "服务器命令账号密码为空")
|
|
|
+ taskLogMsg = taskLogMsg + ":" + "服务器命令账号密码为空"
|
|
|
+ err := service.TaskLog().Create(ctx, "task_ssh", "服务器命令账号密码为空")
|
|
|
+ if err != nil {
|
|
|
+ glog.Info(ctx, "task_log:", err.Error())
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
- hostKeyCallback, err := knownhosts.New("~/.ssh/known_hosts")
|
|
|
- if err != nil {
|
|
|
- glog.Error(ctx, err.Error())
|
|
|
- }
|
|
|
- s2 := fmt.Sprintf("%s:%d\n", host, port)
|
|
|
- service.TaskLog().Create(ctx, "task_ssh", s2)
|
|
|
+ //hostKeyCallback, err := knownhosts.New("~/.ssh/known_hosts")
|
|
|
+ //if err != nil {
|
|
|
+ // glog.Error(ctx, err.Error())
|
|
|
+ //}
|
|
|
+ s2 := fmt.Sprintf("%s:%d", host, port)
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s2
|
|
|
+ //service.TaskLog().Create(ctx, "task_ssh", s2)
|
|
|
// 建立SSH客户端连接
|
|
|
client, err := ssh.Dial("tcp", s2, &ssh.ClientConfig{
|
|
|
User: nodeconfig.Sshusername,
|
|
|
Auth: []ssh.AuthMethod{ssh.Password(nodeconfig.Sshpassword)},
|
|
|
- HostKeyCallback: hostKeyCallback,
|
|
|
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
|
+ Timeout: 60 * time.Second,
|
|
|
})
|
|
|
if err != nil {
|
|
|
//log.Fatalf("SSH dial error: %s", err.Error())
|
|
|
s := fmt.Sprintf("ssh_log --- SSH dial error: %s", err.Error())
|
|
|
- service.TaskLog().Create(ctx, "task_ssh", s)
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s
|
|
|
+ //service.TaskLog().Create(ctx, "task_ssh", s)
|
|
|
glog.Info(ctx, "SSH dial error:", err.Error())
|
|
|
+ err := service.TaskLog().Create(ctx, "task_ssh", taskLogMsg)
|
|
|
+ if err != nil {
|
|
|
+ glog.Info(ctx, "task_log:", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
// 建立新会话
|
|
@@ -252,28 +282,36 @@ func SSHTaskCommand(ctx context.Context, host string, port int, serverid int) {
|
|
|
defer session.Close()
|
|
|
if err != nil {
|
|
|
s := fmt.Sprintf("ssh_log --- new session error: %s", err.Error())
|
|
|
- service.TaskLog().Create(ctx, "task_ssh", s)
|
|
|
- //websocket.SendToAll(&websocket.WResponse{
|
|
|
- // Event: "sys_log",
|
|
|
- // Data: g.Map{
|
|
|
- // "msg": s,
|
|
|
- // },
|
|
|
- //})
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s
|
|
|
glog.Info(ctx, "new session error:", err.Error())
|
|
|
+ err := service.TaskLog().Create(ctx, "task_ssh", taskLogMsg)
|
|
|
+ if err != nil {
|
|
|
+ glog.Info(ctx, "task_log:", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
|
|
|
s := fmt.Sprintf("ssh_log --- 执行命令 %s", nodeconfig.Sshcommand)
|
|
|
glog.Info(ctx, "ssh_log --- 执行命令:", s)
|
|
|
- service.TaskLog().Create(ctx, "task_ssh", s)
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s
|
|
|
+ service.TaskLog().Create(ctx, "task_ssh", taskLogMsg)
|
|
|
result, err := session.Output(nodeconfig.Sshcommand)
|
|
|
if err != nil {
|
|
|
+ taskLogMsg = taskLogMsg + ":" + err.Error()
|
|
|
+ service.TaskLog().Create(ctx, "task_ssh", taskLogMsg)
|
|
|
glog.Info(ctx, err.Error())
|
|
|
return
|
|
|
}
|
|
|
s = fmt.Sprintf("ssh_log ---- 执行命令结果 %s", string(result))
|
|
|
+ taskLogMsg = taskLogMsg + ":" + s
|
|
|
glog.Info(ctx, s)
|
|
|
- service.TaskLog().Create(ctx, "task_ssh", s)
|
|
|
+ //err := service.TaskLog().Create(ctx, "task_ssh", taskLogMsg)
|
|
|
+ err = service.TaskLog().Create(ctx, "task_ssh", taskLogMsg)
|
|
|
+ if err != nil {
|
|
|
+ glog.Info(ctx, "task_log:", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
glog.Info(ctx, result)
|
|
|
}
|
|
|
|