test port listen on 127.0.0.1 and identity.ip and ::1

This commit is contained in:
UlricQin 2020-12-23 15:57:11 +08:00
parent 28d311e759
commit ee96ce5046
3 changed files with 22 additions and 18 deletions

View File

@ -63,6 +63,11 @@ func main() {
loggeri.Init(config.Config.Logger) loggeri.Init(config.Config.Logger)
stats.Init("agent") stats.Init("agent")
if err := report.GatherBase(); err != nil {
fmt.Println("gatherBase fail: ", err)
os.Exit(1)
}
if config.Config.Enable.Mon { if config.Config.Enable.Mon {
monStart() monStart()
} }
@ -76,7 +81,6 @@ func main() {
} }
if config.Config.Enable.Metrics { if config.Config.Enable.Metrics {
// 初始化 statsd服务 // 初始化 statsd服务
statsd.Start() statsd.Start()
@ -90,11 +94,6 @@ func main() {
} }
func reportStart() { func reportStart() {
if err := report.GatherBase(); err != nil {
fmt.Println("gatherBase fail: ", err)
os.Exit(1)
}
go report.LoopReport() go report.LoopReport()
} }

View File

@ -19,9 +19,9 @@ import (
) )
var ( var (
sn string SN string
ip string IP string
ident string Ident string
) )
func LoopReport() { func LoopReport() {
@ -36,21 +36,21 @@ func LoopReport() {
func GatherBase() error { func GatherBase() error {
var err error var err error
sn, err = exec(config.Config.Report.SN) SN, err = exec(config.Config.Report.SN)
if err != nil { if err != nil {
return fmt.Errorf("cannot get sn: %s", err) return fmt.Errorf("cannot get sn: %s", err)
} }
ip, err = identity.GetIP() IP, err = identity.GetIP()
if err != nil { if err != nil {
return fmt.Errorf("cannot get ip: %s", err) return fmt.Errorf("cannot get ip: %s", err)
} }
if !str.IsIP(ip) { if !str.IsIP(IP) {
return fmt.Errorf("'%s' not ip", ip) return fmt.Errorf("'%s' not ip", IP)
} }
ident, err = identity.GetIdent() Ident, err = identity.GetIdent()
if err != nil { if err != nil {
return fmt.Errorf("cannot get ident: %s", err) return fmt.Errorf("cannot get ident: %s", err)
} }
@ -98,9 +98,9 @@ func report() error {
} }
form := hostRegisterForm{ form := hostRegisterForm{
SN: sn, SN: SN,
IP: ip, IP: IP,
Ident: ident, Ident: Ident,
Name: name, Name: name,
Cate: config.Config.Report.Cate, Cate: config.Config.Report.Cate,
UniqKey: config.Config.Report.UniqKey, UniqKey: config.Config.Report.UniqKey,

View File

@ -11,6 +11,7 @@ import (
"github.com/didi/nightingale/src/models" "github.com/didi/nightingale/src/models"
"github.com/didi/nightingale/src/modules/agent/config" "github.com/didi/nightingale/src/modules/agent/config"
"github.com/didi/nightingale/src/modules/agent/core" "github.com/didi/nightingale/src/modules/agent/core"
"github.com/didi/nightingale/src/modules/agent/report"
) )
type PortScheduler struct { type PortScheduler struct {
@ -46,7 +47,11 @@ func (p *PortScheduler) Stop() {
func PortCollect(p *models.PortCollect) { func PortCollect(p *models.PortCollect) {
value := 0 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 value = 1
} }