From 18483c0cb1e38920d6b561252884abc3c62b7980 Mon Sep 17 00:00:00 2001 From: naiba Date: Mon, 27 Sep 2021 21:24:04 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=90=20=E9=A2=84=E7=BD=AE=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E4=BB=BB=E5=8A=A1=EF=BC=8CAgent=20=E5=8F=AF=E7=A6=81?= =?UTF-8?q?=E6=AD=A2=E8=87=AA=E5=8A=A8=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- cmd/agent/main.go | 18 +++++++++++++----- model/monitor.go | 1 + 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 99b643c..8f4991a 100644 --- a/README.md +++ b/README.md @@ -38,9 +38,9 @@ _\* 使用 WatchTower 可以自动更新面板,Windows 终端可以使用 nssm ### 特殊技能 -编辑 `/etc/systemd/system/nezha-agent.service`,在 `ExecStart=` 这一行的末尾加上 +通过执行 `./nezha-agent --help` 查看支持的参数,如果你使用一键脚本,可以编辑 `/etc/systemd/system/nezha-agent.service`,在 `ExecStart=` 这一行的末尾加上 -- `--skip-conn` 不监控连接数,机场/连接密集型机器推荐设置,不然比较占CPU([shirou/gopsutil/issues#220](https://github.com/shirou/gopsutil/issues/220)) +- `--skip-conn` 不监控连接数,机场/连接密集型机器推荐设置,不然比较占 CPU([shirou/gopsutil/issues#220](https://github.com/shirou/gopsutil/issues/220)) - `--disable-auto-update` 禁止 Agent 自动更新 ## 功能说明 diff --git a/cmd/agent/main.go b/cmd/agent/main.go index a1a39db..de68826 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -97,7 +97,13 @@ func run() { if _, err := semver.Parse(version); err == nil && !agentConf.DisableAutoUpdate { go func() { for range updateCh { - go doSelfUpdate() + go func() { + defer func() { + time.Sleep(time.Minute * 20) + updateCh <- struct{}{} + }() + doSelfUpdate() + }() } }() updateCh <- struct{}{} @@ -186,6 +192,8 @@ func doTask(task *pb.Task) { handleTcpPingTask(task, &result) case model.TaskTypeCommand: handleCommandTask(task, &result) + case model.TaskTypeUpgrade: + handleUpgradeTask(task, &result) default: println("不支持的任务:", task) } @@ -217,10 +225,6 @@ func reportState() { } func doSelfUpdate() { - defer func() { - time.Sleep(time.Minute * 20) - updateCh <- struct{}{} - }() v := semver.MustParse(version) println("检查更新:", v) latest, err := selfupdate.UpdateSelf(v, "naiba/nezha") @@ -233,6 +237,10 @@ func doSelfUpdate() { } } +func handleUpgradeTask(task *pb.Task, result *pb.TaskResult) { + doSelfUpdate() +} + func handleTcpPingTask(task *pb.Task, result *pb.TaskResult) { start := time.Now() conn, err := net.DialTimeout("tcp", task.GetData(), time.Second*10) diff --git a/model/monitor.go b/model/monitor.go index abc9b8d..aa64c62 100644 --- a/model/monitor.go +++ b/model/monitor.go @@ -16,6 +16,7 @@ const ( TaskTypeTCPPing TaskTypeCommand TaskTypeTerminal + TaskTypeUpgrade ) type TerminalTask struct {