From 2b30d8e0bd1f48c9bae61c0906f55e79a85e6c1b Mon Sep 17 00:00:00 2001 From: 710leo <710leo@gmail.com> Date: Mon, 23 Mar 2020 15:32:23 +0800 Subject: [PATCH 1/9] refactor: stats default use address.yml --- src/toolkits/stats/init.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/toolkits/stats/init.go b/src/toolkits/stats/init.go index 3ef9bca6..bf7dd36d 100644 --- a/src/toolkits/stats/init.go +++ b/src/toolkits/stats/init.go @@ -3,12 +3,17 @@ package stats import ( "bytes" "encoding/json" + "fmt" "net/http" + "path" "time" "github.com/didi/nightingale/src/dataobj" + "github.com/didi/nightingale/src/toolkits/address" + "github.com/toolkits/pkg/file" "github.com/toolkits/pkg/logger" + "github.com/toolkits/pkg/runner" ) var ( @@ -16,7 +21,13 @@ var ( ) func Init(prefix string, addr ...string) { - if len(addr) > 0 { + + if file.IsExist(path.Join(runner.Cwd, "etc", "address.yml")) { + //address.yml 存在,使用配置文件的地址 + port := address.GetHTTPPort("collector") + PushUrl = fmt.Sprintf("http://127.0.0.1:%d/api/collector/push", port) + } else if len(addr) > 0 && addr[0] != "" { + // address.yml 不存在,使用 addr 参数 PushUrl = addr[0] } From 408b6494d86e3fbcd4b3f1594d458e59de236064 Mon Sep 17 00:00:00 2001 From: 710leo <710leo@gmail.com> Date: Mon, 23 Mar 2020 18:06:44 +0800 Subject: [PATCH 2/9] refactor: stats use address.yml --- src/toolkits/stats/init.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/toolkits/stats/init.go b/src/toolkits/stats/init.go index bf7dd36d..9b74917d 100644 --- a/src/toolkits/stats/init.go +++ b/src/toolkits/stats/init.go @@ -21,14 +21,14 @@ var ( ) func Init(prefix string, addr ...string) { + if len(addr) > 0 && addr[0] != "" { + //如果配置了 addr,使用 addr 参数 + PushUrl = addr[0] - if file.IsExist(path.Join(runner.Cwd, "etc", "address.yml")) { - //address.yml 存在,使用配置文件的地址 + } else if file.IsExist(path.Join(runner.Cwd, "etc", "address.yml")) { + //address.yml 存在,则使用配置文件的地址 port := address.GetHTTPPort("collector") PushUrl = fmt.Sprintf("http://127.0.0.1:%d/api/collector/push", port) - } else if len(addr) > 0 && addr[0] != "" { - // address.yml 不存在,使用 addr 参数 - PushUrl = addr[0] } Counter = NewCounter(prefix) From b71847feba09f2a6ebd1ba2d1b05353d5b5b82ec Mon Sep 17 00:00:00 2001 From: higkoo Date: Mon, 23 Mar 2020 19:46:59 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=9B=B4=E6=96=B0IP=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E7=9A=84=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ifconfig 和 route 命令,写绝对路径会导致兼容性不好。 1. Debian系统路径对应: /sbin/ifconfig 和 /sbin/route 2. 原命令获取的IP地址兼容性不好,会带“addr:”在前面。 addr:192.163.10.31 3. 本地配置正确的情况下 `hostname -i`是能拿到正确IP的 --- etc/collector.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/collector.yml b/etc/collector.yml index f1218cdd..eb0e960b 100644 --- a/etc/collector.yml +++ b/etc/collector.yml @@ -4,7 +4,7 @@ logger: keepHours: 2 identity: specify: "" - shell: /usr/sbin/ifconfig `/usr/sbin/route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|head -n 1 + shell: hostname -i sys: # timeout in ms # interval in second From 16871e724fd54ee1551784af0841af94113c510c Mon Sep 17 00:00:00 2001 From: higkoo Date: Mon, 23 Mar 2020 19:49:27 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E6=9B=B4=E6=96=B0IP=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E7=9A=84=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在不同操作系统下全路径不一样,比如上述的 ifconfig / route 在 Debian 系统的 /sbin 目录下。 另外 Debian 打印出来 多 'addr:' 需要多处理 /sbin/ifconfig `/sbin/route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|awk -F':' '{print $2}'|head -n 1 还不如直接 hostname -i 获取,正常配置下都Okay的。 --- etc/index.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/index.yml b/etc/index.yml index c65da2e8..2edebbdd 100644 --- a/etc/index.yml +++ b/etc/index.yml @@ -4,4 +4,4 @@ logger: keepHours: 2 identity: specify: "" - shell: /usr/sbin/ifconfig `/usr/sbin/route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|head -n 1 + shell: hostname -i From 060c00fa259f4e7f23ff7736b669aef337194f4e Mon Sep 17 00:00:00 2001 From: higkoo Date: Mon, 23 Mar 2020 19:52:19 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E6=9B=B4=E6=96=B0IP=E7=9A=84=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原命令也是基于 路径正确、操作系统输出格式正确,还不如 hostname -i 来得简单。 hostname -i 在正确配置的操作系统下,都能正常获取IP地址。 简单高效,简单就是好。 --- etc/judge.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/etc/judge.yml b/etc/judge.yml index cebec418..a2035f0d 100644 --- a/etc/judge.yml +++ b/etc/judge.yml @@ -14,9 +14,9 @@ redis: identity: specify: "" - shell: /usr/sbin/ifconfig `/usr/sbin/route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|head -n 1 + shell: hostname -i logger: dir: logs/judge level: WARNING - keepHours: 2 \ No newline at end of file + keepHours: 2 From 50bb36f2ed449c116a74633a03a3763d0753b52b Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Tue, 24 Mar 2020 10:32:53 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E8=8E=B7=E5=8F=96ip?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- etc/collector.yml | 2 +- etc/index.yml | 2 +- etc/judge.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/etc/collector.yml b/etc/collector.yml index eb0e960b..8ba85351 100644 --- a/etc/collector.yml +++ b/etc/collector.yml @@ -4,7 +4,7 @@ logger: keepHours: 2 identity: specify: "" - shell: hostname -i + shell: ifconfig `route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|awk -F ':' '{print $NF}'|head -n 1 sys: # timeout in ms # interval in second diff --git a/etc/index.yml b/etc/index.yml index 2edebbdd..736a4e03 100644 --- a/etc/index.yml +++ b/etc/index.yml @@ -4,4 +4,4 @@ logger: keepHours: 2 identity: specify: "" - shell: hostname -i + shell: ifconfig `route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|awk -F ':' '{print $NF}'|head -n 1 diff --git a/etc/judge.yml b/etc/judge.yml index a2035f0d..e86f7d39 100644 --- a/etc/judge.yml +++ b/etc/judge.yml @@ -14,7 +14,7 @@ redis: identity: specify: "" - shell: hostname -i + shell: ifconfig `route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|awk -F ':' '{print $NF}'|head -n 1 logger: dir: logs/judge From 4e5f7a56daebeedd829ab62e260bc5d342b7c494 Mon Sep 17 00:00:00 2001 From: 710leo <710leo@gmail.com> Date: Tue, 24 Mar 2020 11:15:00 +0800 Subject: [PATCH 7/9] fix: tagkv api get redundant values --- src/modules/index/cache/tag_map.go | 5 +++-- src/modules/index/http/routes/index_router.go | 4 ++-- src/modules/transfer/http/routes/routes.go | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/modules/index/cache/tag_map.go b/src/modules/index/cache/tag_map.go index 205d5296..dba7a5c8 100644 --- a/src/modules/index/cache/tag_map.go +++ b/src/modules/index/cache/tag_map.go @@ -30,8 +30,9 @@ func (t *TagkvIndex) GetTagkv() []*TagPair { t.RLock() defer t.RUnlock() tagkvs := []*TagPair{} - var vs []string + for k, vm := range t.Tagkv { + var vs []string for v, _ := range vm { vs = append(vs, v) } @@ -50,8 +51,8 @@ func (t *TagkvIndex) GetTagkvMap() map[string][]string { defer t.RUnlock() tagkvs := make(map[string][]string) - var vs []string for k, vm := range t.Tagkv { + var vs []string for v, _ := range vm { vs = append(vs, v) } diff --git a/src/modules/index/http/routes/index_router.go b/src/modules/index/http/routes/index_router.go index c047d972..e5233fe9 100644 --- a/src/modules/index/http/routes/index_router.go +++ b/src/modules/index/http/routes/index_router.go @@ -93,9 +93,8 @@ func GetTagPairs(c *gin.Context) { resp := []*IndexTagkvResp{} - tagkvFilter := make(map[string]map[string]struct{}) - for _, metric := range recv.Metrics { + tagkvFilter := make(map[string]map[string]struct{}) tagkvs := []*cache.TagPair{} for _, endpoint := range recv.Endpoints { @@ -107,6 +106,7 @@ func GetTagPairs(c *gin.Context) { } tagkvMap := metricIndex.TagkvMap.GetTagkvMap() + for tagk, tagvs := range tagkvMap { tagvFilter, exists := tagkvFilter[tagk] if !exists { diff --git a/src/modules/transfer/http/routes/routes.go b/src/modules/transfer/http/routes/routes.go index d6e31563..cec93b7c 100644 --- a/src/modules/transfer/http/routes/routes.go +++ b/src/modules/transfer/http/routes/routes.go @@ -18,7 +18,7 @@ func Config(r *gin.Engine) { sys.GET("/alive-judges", judges) sys.POST("/push", PushData) - sys.POST("/data", QueryDataForJudge) + sys.POST("/data", QueryData) sys.POST("/data/ui", QueryDataForUI) } From 19c9e4eb363fc0204dc4d437a850fbd229438aec Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Tue, 24 Mar 2020 17:09:09 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=97=A0=E8=AE=BA=E5=93=AA=E7=A7=8D?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E9=83=BD=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/monapi/cron/event_consumer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/monapi/cron/event_consumer.go b/src/modules/monapi/cron/event_consumer.go index 94610165..9207ff04 100644 --- a/src/modules/monapi/cron/event_consumer.go +++ b/src/modules/monapi/cron/event_consumer.go @@ -58,7 +58,7 @@ func consume(event *model.Event) { SetEventStatus(event, model.STATUS_UPGRADE) if needNotify { - if event.EventType == config.ALERT && NeedCallback(event.Sid) { + if NeedCallback(event.Sid) { if err := PushCallbackEvent(event); err != nil { logger.Errorf("push event to callback queue failed, callbackEvent: %+v", event) } @@ -82,7 +82,7 @@ func consume(event *model.Event) { return } - if event.EventType == config.ALERT && NeedCallback(event.Sid) { + if NeedCallback(event.Sid) { if err := PushCallbackEvent(event); err != nil { logger.Errorf("push event to callback queue failed, callbackEvent: %+v", event) } From fb43b03b2c2e455d4941f6d74be031ee2c76169d Mon Sep 17 00:00:00 2001 From: 710leo <710leo@gmail.com> Date: Tue, 24 Mar 2020 20:41:21 +0800 Subject: [PATCH 9/9] refactor: change collect config path --- etc/port/20_2058 | 1 + etc/port/20_22 | 1 - etc/port/20_5800 | 1 + etc/port/20_5810 | 1 + etc/port/20_5820 | 1 + etc/port/20_5830 | 1 + etc/port/20_5840 | 1 + src/modules/collector/config/config.go | 8 ++++---- 8 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 etc/port/20_2058 delete mode 100644 etc/port/20_22 create mode 100644 etc/port/20_5800 create mode 100644 etc/port/20_5810 create mode 100644 etc/port/20_5820 create mode 100644 etc/port/20_5830 create mode 100644 etc/port/20_5840 diff --git a/etc/port/20_2058 b/etc/port/20_2058 new file mode 100644 index 00000000..a8f83c1c --- /dev/null +++ b/etc/port/20_2058 @@ -0,0 +1 @@ +n9e-collector diff --git a/etc/port/20_22 b/etc/port/20_22 deleted file mode 100644 index 09920bc7..00000000 --- a/etc/port/20_22 +++ /dev/null @@ -1 +0,0 @@ -sshd \ No newline at end of file diff --git a/etc/port/20_5800 b/etc/port/20_5800 new file mode 100644 index 00000000..34878d81 --- /dev/null +++ b/etc/port/20_5800 @@ -0,0 +1 @@ +n9e-monapi diff --git a/etc/port/20_5810 b/etc/port/20_5810 new file mode 100644 index 00000000..87fd6ec8 --- /dev/null +++ b/etc/port/20_5810 @@ -0,0 +1 @@ +n9e-transfer \ No newline at end of file diff --git a/etc/port/20_5820 b/etc/port/20_5820 new file mode 100644 index 00000000..76765ebb --- /dev/null +++ b/etc/port/20_5820 @@ -0,0 +1 @@ +n9e-tsdb diff --git a/etc/port/20_5830 b/etc/port/20_5830 new file mode 100644 index 00000000..2fa1392b --- /dev/null +++ b/etc/port/20_5830 @@ -0,0 +1 @@ +n9e-index diff --git a/etc/port/20_5840 b/etc/port/20_5840 new file mode 100644 index 00000000..bd723305 --- /dev/null +++ b/etc/port/20_5840 @@ -0,0 +1 @@ +n9e-judge \ No newline at end of file diff --git a/src/modules/collector/config/config.go b/src/modules/collector/config/config.go index 427d0464..6b64419c 100644 --- a/src/modules/collector/config/config.go +++ b/src/modules/collector/config/config.go @@ -62,16 +62,16 @@ func Parse(conf string) error { "enable": true, "timeout": 1000, "interval": 10, //采集策略更新时间 - "portPath": "/home/n9e/etc/port", - "procPath": "/home/n9e/etc/proc", - "logPath": "/home/n9e/etc/log", + "portPath": "./etc/port", + "procPath": "./etc/proc", + "logPath": "./etc/log", "api": "/api/portal/collects/", }) viper.SetDefault("sys", map[string]interface{}{ "timeout": 1000, //请求超时时间 "interval": 10, //基础指标上报周期 - "plugin": "/home/n9e/plugin", + "plugin": "./plugin", }) err = viper.Unmarshal(&Config)