feat: 在面板重启后及时更新host信息
This commit is contained in:
parent
f837a6fdc8
commit
1f7706201b
@ -57,11 +57,11 @@ type AgentCliParam struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version string
|
version string
|
||||||
arch string
|
arch string
|
||||||
client pb.NezhaServiceClient
|
client pb.NezhaServiceClient
|
||||||
inited bool
|
initialized bool
|
||||||
resolver = &net.Resolver{PreferGo: true}
|
resolver = &net.Resolver{PreferGo: true}
|
||||||
)
|
)
|
||||||
|
|
||||||
var agentCmd = &cobra.Command{
|
var agentCmd = &cobra.Command{
|
||||||
@ -222,7 +222,7 @@ func run() {
|
|||||||
go pty.DownloadDependency()
|
go pty.DownloadDependency()
|
||||||
}
|
}
|
||||||
// 上报服务器信息
|
// 上报服务器信息
|
||||||
go reportState()
|
go reportStateDaemon()
|
||||||
// 更新IP信息
|
// 更新IP信息
|
||||||
go monitor.UpdateIP(agentCliParam.UseIPv6CountryCode, agentCliParam.IPReportPeriod)
|
go monitor.UpdateIP(agentCliParam.UseIPv6CountryCode, agentCliParam.IPReportPeriod)
|
||||||
|
|
||||||
@ -240,7 +240,7 @@ func run() {
|
|||||||
var conn *grpc.ClientConn
|
var conn *grpc.ClientConn
|
||||||
|
|
||||||
retry := func() {
|
retry := func() {
|
||||||
inited = false
|
initialized = false
|
||||||
println("Error to close connection ...")
|
println("Error to close connection ...")
|
||||||
if conn != nil {
|
if conn != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@ -280,7 +280,7 @@ func run() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
inited = true
|
initialized = true
|
||||||
// 执行 Task
|
// 执行 Task
|
||||||
tasks, err := client.RequestTask(context.Background(), monitor.GetHost().PB())
|
tasks, err := client.RequestTask(context.Background(), monitor.GetHost().PB())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -382,7 +382,7 @@ func doTask(task *pb.Task) {
|
|||||||
result.Id = task.GetId()
|
result.Id = task.GetId()
|
||||||
result.Type = task.GetType()
|
result.Type = task.GetType()
|
||||||
switch task.GetType() {
|
switch task.GetType() {
|
||||||
case model.TaskTypeHTTPGET:
|
case model.TaskTypeHTTPGet:
|
||||||
handleHttpGetTask(task, &result)
|
handleHttpGetTask(task, &result)
|
||||||
case model.TaskTypeICMPPing:
|
case model.TaskTypeICMPPing:
|
||||||
handleIcmpPingTask(task, &result)
|
handleIcmpPingTask(task, &result)
|
||||||
@ -398,6 +398,9 @@ func doTask(task *pb.Task) {
|
|||||||
case model.TaskTypeNAT:
|
case model.TaskTypeNAT:
|
||||||
handleNATTask(task)
|
handleNATTask(task)
|
||||||
return
|
return
|
||||||
|
case model.TaskTypeReportHostInfo:
|
||||||
|
reportState(time.Time{})
|
||||||
|
return
|
||||||
case model.TaskTypeKeepalive:
|
case model.TaskTypeKeepalive:
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
@ -407,36 +410,41 @@ func doTask(task *pb.Task) {
|
|||||||
client.ReportTask(context.Background(), &result)
|
client.ReportTask(context.Background(), &result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// reportState 向server上报状态信息
|
// reportStateDaemon 向server上报状态信息
|
||||||
func reportState() {
|
func reportStateDaemon() {
|
||||||
var lastReportHostInfo time.Time
|
var lastReportHostInfo time.Time
|
||||||
var err error
|
var err error
|
||||||
defer println("reportState exit", time.Now(), "=>", err)
|
defer println("reportState exit", time.Now(), "=>", err)
|
||||||
for {
|
for {
|
||||||
// 为了更准确的记录时段流量,inited 后再上传状态信息
|
// 为了更准确的记录时段流量,inited 后再上传状态信息
|
||||||
if client != nil && inited {
|
lastReportHostInfo = reportState(lastReportHostInfo)
|
||||||
monitor.TrackNetworkSpeed()
|
time.Sleep(time.Second * time.Duration(agentCliParam.ReportDelay))
|
||||||
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut)
|
}
|
||||||
_, err = client.ReportSystemState(timeOutCtx, monitor.GetState(agentCliParam.SkipConnectionCount, agentCliParam.SkipProcsCount).PB())
|
}
|
||||||
cancel()
|
|
||||||
if err != nil {
|
func reportState(lastReportHostInfo time.Time) time.Time {
|
||||||
println("reportState error", err)
|
if client != nil && initialized {
|
||||||
time.Sleep(delayWhenError)
|
monitor.TrackNetworkSpeed()
|
||||||
}
|
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut)
|
||||||
// 每10分钟重新获取一次硬件信息
|
_, err := client.ReportSystemState(timeOutCtx, monitor.GetState(agentCliParam.SkipConnectionCount, agentCliParam.SkipProcsCount).PB())
|
||||||
if lastReportHostInfo.Before(time.Now().Add(-10 * time.Minute)) {
|
cancel()
|
||||||
lastReportHostInfo = time.Now()
|
if err != nil {
|
||||||
client.ReportSystemInfo(context.Background(), monitor.GetHost().PB())
|
println("reportState error", err)
|
||||||
if monitor.GeoQueryIP != "" {
|
time.Sleep(delayWhenError)
|
||||||
geoip, err := client.LookupGeoIP(context.Background(), &pb.GeoIP{Ip: monitor.GeoQueryIP})
|
}
|
||||||
if err == nil {
|
// 每10分钟重新获取一次硬件信息
|
||||||
monitor.CachedCountryCode = geoip.GetCountryCode()
|
if lastReportHostInfo.Before(time.Now().Add(-10 * time.Minute)) {
|
||||||
}
|
lastReportHostInfo = time.Now()
|
||||||
|
client.ReportSystemInfo(context.Background(), monitor.GetHost().PB())
|
||||||
|
if monitor.GeoQueryIP != "" {
|
||||||
|
geoip, err := client.LookupGeoIP(context.Background(), &pb.GeoIP{Ip: monitor.GeoQueryIP})
|
||||||
|
if err == nil {
|
||||||
|
monitor.CachedCountryCode = geoip.GetCountryCode()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second * time.Duration(agentCliParam.ReportDelay))
|
|
||||||
}
|
}
|
||||||
|
return lastReportHostInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
// doSelfUpdate 执行更新检查 如果更新成功则会结束进程
|
// doSelfUpdate 执行更新检查 如果更新成功则会结束进程
|
||||||
|
@ -2,7 +2,7 @@ package model
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
_ = iota
|
_ = iota
|
||||||
TaskTypeHTTPGET
|
TaskTypeHTTPGet
|
||||||
TaskTypeICMPPing
|
TaskTypeICMPPing
|
||||||
TaskTypeTCPPing
|
TaskTypeTCPPing
|
||||||
TaskTypeCommand
|
TaskTypeCommand
|
||||||
@ -11,6 +11,7 @@ const (
|
|||||||
TaskTypeKeepalive
|
TaskTypeKeepalive
|
||||||
TaskTypeTerminalGRPC
|
TaskTypeTerminalGRPC
|
||||||
TaskTypeNAT
|
TaskTypeNAT
|
||||||
|
TaskTypeReportHostInfo
|
||||||
)
|
)
|
||||||
|
|
||||||
type TerminalTask struct {
|
type TerminalTask struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user