From 373b95a1442418ebece6b99a153e47cb9e8cf0ee Mon Sep 17 00:00:00 2001 From: naiba Date: Sat, 12 Jun 2021 11:37:47 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20=E5=AF=B9=20Agent=20=E7=9A=84?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E4=B8=8A=E6=8A=A5=E5=A2=9E=E5=8A=A0=E8=B6=85?= =?UTF-8?q?=E6=97=B6(=E5=8F=AF=E8=83=BD)=E5=A2=9E=E5=8A=A0=E5=9C=A8=20Wind?= =?UTF-8?q?ows=20=E4=B8=8A=E7=9A=84=E7=A8=B3=E5=AE=9A=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- cmd/agent/main.go | 24 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9ca92db690..61bf81f4b1 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,7 @@ \>> 交流论坛:[打杂社区](https://daza.net/c/nezha) (Lemmy) -\>> QQ 交流群:872069346 **加群要求:已搭建好哪吒监控 & 有 2+ 服务器**
-群友互助/服务器交流,作者不答疑,找 naiba 请至论坛发帖 +\>> QQ 交流群:872069346 **加群要求:已搭建好哪吒监控 & 有 2+ 服务器** \>> [我们的用户](https://www.google.com/search?q="powered+by+哪吒监控%7C哪吒面板"&filter=0) (Google) @@ -103,7 +102,7 @@ URL 里面也可放置占位符,请求时会进行简单的字符串替换。 - net_in_speed(入站网速)、net_out_speed(出站网速)、net_all_speed(双向网速)、transfer_in(入站流量)、transfer_out(出站流量)、transfer_all(双向流量):Min/Max 数值为字节(1kb=1024,1mb = 1024\*1024) - offline:不支持 Min/Max 参数 - Duration:持续秒数,监控比较简陋,取持续时间内的 70% 采样结果 -- Ignore: `{"1": true, "2":false}` 忽略此规则的服务器 ID 列表 +- Ignore: `{"1": true, "2":false}` 忽略此规则的服务器 ID 列表,比如忽略服务器 ID 5 的离线通知 `[{"Type":"offline","Duration":10, "Ignore":{"5": true}}]`
@@ -198,7 +197,7 @@ URL 里面也可放置占位符,请求时会进行简单的字符串替换。
如何使 OpenWrt/LEDE 自启动?来自 @艾斯德斯 -首先在 release 下载对应的二进制解压tar.gz包后放置到 `/root`,然后 `chmod +x /root/nezha-agent` 赋予执行权限,然后创建 `/etc/init.d/nezha-service`: +首先在 release 下载对应的二进制解压 tar.gz 包后放置到 `/root`,然后 `chmod +x /root/nezha-agent` 赋予执行权限,然后创建 `/etc/init.d/nezha-service`: ``` #!/bin/sh /etc/rc.common @@ -251,6 +250,7 @@ restart() { ```nginx server{ + #原有的一些配置 #server_name blablabla... location /ws { diff --git a/cmd/agent/main.go b/cmd/agent/main.go index e3847914df..932c79d43a 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -41,7 +41,6 @@ var ( var ( client pb.NezhaServiceClient - ctx = context.Background() updateCh = make(chan struct{}) // Agent 自动更新间隔 httpClient = &http.Client{ Transport: &http.Transport{ @@ -56,6 +55,7 @@ var ( const ( delayWhenError = time.Second * 10 // Agent 重连间隔 + networkTimeOut = time.Second * 5 // 普通网络超时 ) func main() { @@ -113,7 +113,7 @@ func run() { } for { - timeOutCtx, cancel := context.WithTimeout(ctx, time.Second*5) + timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut) conn, err = grpc.DialContext(timeOutCtx, server, grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth)) if err != nil { println("grpc.Dial err: ", err) @@ -124,19 +124,25 @@ func run() { cancel() client = pb.NewNezhaServiceClient(conn) // 第一步注册 - _, err = client.ReportSystemInfo(ctx, monitor.GetHost().PB()) + timeOutCtx, cancel = context.WithTimeout(context.Background(), networkTimeOut) + _, err = client.ReportSystemInfo(timeOutCtx, monitor.GetHost().PB()) if err != nil { println("client.ReportSystemInfo err: ", err) + cancel() retry() continue } + cancel() // 执行 Task - tasks, err := client.RequestTask(ctx, monitor.GetHost().PB()) + timeOutCtx, cancel = context.WithTimeout(context.Background(), networkTimeOut) + tasks, err := client.RequestTask(timeOutCtx, monitor.GetHost().PB()) if err != nil { println("client.RequestTask err: ", err) + cancel() retry() continue } + cancel() err = receiveTasks(tasks) println("receiveTasks exit to main: ", err) retry() @@ -226,7 +232,7 @@ func doTask(task *pb.Task) { if err != nil { // 进程组创建失败,直接退出 result.Data = err.Error() - client.ReportTask(ctx, &result) + client.ReportTask(context.Background(), &result) return } timeout := time.NewTimer(time.Hour * 2) @@ -258,7 +264,7 @@ func doTask(task *pb.Task) { default: println("Unknown action: ", task) } - client.ReportTask(ctx, &result) + client.ReportTask(context.Background(), &result) } func reportState() { @@ -268,14 +274,16 @@ func reportState() { for { if client != nil { monitor.TrackNetworkSpeed() - _, err = client.ReportSystemState(ctx, monitor.GetState(dao.ReportDelay).PB()) + timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut) + _, err = client.ReportSystemState(timeOutCtx, monitor.GetState(dao.ReportDelay).PB()) + cancel() if err != nil { println("reportState error", err) time.Sleep(delayWhenError) } if lastReportHostInfo.Before(time.Now().Add(-10 * time.Minute)) { lastReportHostInfo = time.Now() - client.ReportSystemInfo(ctx, monitor.GetHost().PB()) + client.ReportSystemInfo(context.Background(), monitor.GetHost().PB()) } } }