From ee96ce5046f976cd0421b49dea2c3ea9c05aa3b0 Mon Sep 17 00:00:00 2001 From: UlricQin Date: Wed, 23 Dec 2020 15:57:11 +0800 Subject: [PATCH] test port listen on 127.0.0.1 and identity.ip and ::1 --- src/modules/agent/agent.go | 11 +++++------ src/modules/agent/report/report.go | 22 +++++++++++----------- src/modules/agent/sys/ports/scheduler.go | 7 ++++++- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/modules/agent/agent.go b/src/modules/agent/agent.go index 83c7106d..c2aae2c8 100644 --- a/src/modules/agent/agent.go +++ b/src/modules/agent/agent.go @@ -63,6 +63,11 @@ func main() { loggeri.Init(config.Config.Logger) stats.Init("agent") + if err := report.GatherBase(); err != nil { + fmt.Println("gatherBase fail: ", err) + os.Exit(1) + } + if config.Config.Enable.Mon { monStart() } @@ -76,7 +81,6 @@ func main() { } if config.Config.Enable.Metrics { - // 初始化 statsd服务 statsd.Start() @@ -90,11 +94,6 @@ func main() { } func reportStart() { - if err := report.GatherBase(); err != nil { - fmt.Println("gatherBase fail: ", err) - os.Exit(1) - } - go report.LoopReport() } diff --git a/src/modules/agent/report/report.go b/src/modules/agent/report/report.go index 78ac040d..47874fe6 100644 --- a/src/modules/agent/report/report.go +++ b/src/modules/agent/report/report.go @@ -19,9 +19,9 @@ import ( ) var ( - sn string - ip string - ident string + SN string + IP string + Ident string ) func LoopReport() { @@ -36,21 +36,21 @@ func LoopReport() { func GatherBase() error { var err error - sn, err = exec(config.Config.Report.SN) + SN, err = exec(config.Config.Report.SN) if err != nil { return fmt.Errorf("cannot get sn: %s", err) } - ip, err = identity.GetIP() + IP, err = identity.GetIP() if err != nil { return fmt.Errorf("cannot get ip: %s", err) } - if !str.IsIP(ip) { - return fmt.Errorf("'%s' not ip", ip) + if !str.IsIP(IP) { + return fmt.Errorf("'%s' not ip", IP) } - ident, err = identity.GetIdent() + Ident, err = identity.GetIdent() if err != nil { return fmt.Errorf("cannot get ident: %s", err) } @@ -98,9 +98,9 @@ func report() error { } form := hostRegisterForm{ - SN: sn, - IP: ip, - Ident: ident, + SN: SN, + IP: IP, + Ident: Ident, Name: name, Cate: config.Config.Report.Cate, UniqKey: config.Config.Report.UniqKey, diff --git a/src/modules/agent/sys/ports/scheduler.go b/src/modules/agent/sys/ports/scheduler.go index a56a1a00..5a5c363c 100644 --- a/src/modules/agent/sys/ports/scheduler.go +++ b/src/modules/agent/sys/ports/scheduler.go @@ -11,6 +11,7 @@ import ( "github.com/didi/nightingale/src/models" "github.com/didi/nightingale/src/modules/agent/config" "github.com/didi/nightingale/src/modules/agent/core" + "github.com/didi/nightingale/src/modules/agent/report" ) type PortScheduler struct { @@ -46,7 +47,11 @@ func (p *PortScheduler) Stop() { func PortCollect(p *models.PortCollect) { value := 0 - if isListening(p.Port) { + if isListen(p.Port, 1, "127.0.0.1") { + value = 1 + } else if isListen(p.Port, 1, report.IP) { + value = 1 + } else if isListen(p.Port, 1, "::1") { value = 1 } -- GitLab