From 12cadb218d9a3455a54dbf7e6615ba4d5ee9d4b9 Mon Sep 17 00:00:00 2001 From: Feng_Qi Date: Mon, 13 Apr 2020 19:10:04 +0800 Subject: [PATCH] fix port check and push debug log (#86) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix port check and push debug log 1:如果服务没有监听在 0.0.0.0 上,而是监听在特定地址上的话,在 127.0.0.1 上无法检测到端口。修改为如果 127.0.0.1 检测不到话,在 identity 的地址上再检测一次。 2. http push 部分缺乏 debug 日志,把 debug log 改到 push 里面以补全。 * Update cron.go --- src/modules/collector/sys/funcs/cron.go | 3 --- src/modules/collector/sys/funcs/push.go | 2 +- src/modules/collector/sys/ports/scheduler.go | 12 +++++++++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/modules/collector/sys/funcs/cron.go b/src/modules/collector/sys/funcs/cron.go index 8a1bf3bf..d7078a84 100644 --- a/src/modules/collector/sys/funcs/cron.go +++ b/src/modules/collector/sys/funcs/cron.go @@ -6,8 +6,6 @@ import ( "github.com/didi/nightingale/src/dataobj" "github.com/didi/nightingale/src/modules/collector/sys" "github.com/didi/nightingale/src/toolkits/identity" - - "github.com/toolkits/pkg/logger" ) func Collect() { @@ -46,7 +44,6 @@ func collect(sec int64, fn func() []*dataobj.MetricValue) { item.Step = sec item.Endpoint = identity.Identity item.Timestamp = now - logger.Debug("push item: ", item) metricValues = append(metricValues, item) } Push(metricValues) diff --git a/src/modules/collector/sys/funcs/push.go b/src/modules/collector/sys/funcs/push.go index ad61afdd..c0f66602 100644 --- a/src/modules/collector/sys/funcs/push.go +++ b/src/modules/collector/sys/funcs/push.go @@ -39,7 +39,7 @@ func Push(metricItems []*dataobj.MetricValue) error { continue } } - + logger.Debug("push item: ", item) items = append(items, item) } diff --git a/src/modules/collector/sys/ports/scheduler.go b/src/modules/collector/sys/ports/scheduler.go index 0eb20f40..dc7e4f8e 100644 --- a/src/modules/collector/sys/ports/scheduler.go +++ b/src/modules/collector/sys/ports/scheduler.go @@ -56,9 +56,19 @@ func PortCollect(p *model.PortCollect) { } func isListening(port int, timeout int) bool { + if isListen(port, timeout, "127.0.0.1") { + return true + } + if isListen(port, timeout, identity.Identity) { + return true + } + return false +} + +func isListen(port, timeout int, ip string) bool { var conn net.Conn var err error - addr := fmt.Sprintf("127.0.0.1:%d", port) + addr := fmt.Sprintf("%s:%d", ip, port) if timeout <= 0 { // default timeout 3 second timeout = 3