Skip to content

Commit

Permalink
4.0.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
freedomkk-qfeng committed Sep 4, 2017
1 parent f6f9574 commit 98b2cfc
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 24 deletions.
10 changes: 10 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# Changelog #
## 4.0.6.2 ##
#### 新功能 ####
1. 增加了动态重载配置的功能,详见 [README](https://github.com/gaochao1/swcollector/blob/master/README.md})
#### bug修复 ####
1. 现在当 tag 为空时,debug 的时候应该不会重复的打印日志了
#### 改进 ####
1. 现在自定义 Oid 采集时,可以支持 string 类型的返回了。系统会强制转换成 float64 上报,如果转换出错则抛出错误
2. 现在对交换机类型的判断时,也会采取重试(由配置中的 retry 决定重试次数)来规避偶发性的异常了。


## 4.0.6.1 ##
#### bug修复 ####
1. 现在当采集异常 Channel 关闭时,应该会正常的抛弃而不会给 transfer 上报一个空的 endpoint 了
Expand Down
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ swcollector需要部署到有交换机SNMP访问权限的服务器上。
"limitCon": 4 #对于单台交换机上,多个指标采集的并发限制
},
"switchhosts":{
"enabled":true,
"enabled":false,
"hosts":"./hosts.json" #自定义的host与Ip地址对应表,如果配置,则上报时会用这里的host替换ip地址
},
"customMetrics":{
"enabled":true,
"enabled":false,
"template":"./custom.json" #自定义的metric列表,如果配置,会根据这个配置文件,采集额外的自定义metric
},
"transfer": {
Expand All @@ -139,8 +139,9 @@ swcollector需要部署到有交换机SNMP访问权限的服务器上。
"timeout": 1000
},
"http": {
"enabled": true,
"listen": ":1989"
"enabled": false,
"listen": ":1989",
"trustIps":["192.168.0.1","192.168.0.2"]
}
}
Expand Down Expand Up @@ -199,6 +200,28 @@ swcollector需要部署到有交换机SNMP访问权限的服务器上。
}
```

#### 配置的热重载
4.0.6.2 版本起,支持配置的热重载。修改配置后无需重启 swcollector 了。
开启配置热重载需要开启 swcollector 的 http 模块。然后使用以下接口重载配置。
```
# curl http://127.0.0.1:1990/config/reload
```
注意对于 transfer 的 interval 修改,热重载无效,还是需要重启 swcollector

同时也可以使用下列接口来查看 swcollector 的相关信息(类似于 Open-Falcon 的官方的 agent)
```
# curl http://127.0.0.1:1990/ips
查看当前 trustIp 的列表
# curl http://127.0.0.1:1990/workdir
查看当前的工作目录
# curl http://127.0.0.1:1990/exit
远程退出进程
# curl http://127.0.0.1:1990/health
查看当前状态
# curl http://127.0.0.1:1990/version
查看当前版本
```

#### 部署说明
由于是并发采集,因此每个周期的采集耗时,主要取决于被采集的交换机中,最慢的那个。
因此我们可以在 debug 模式下观察每个交换机的采集耗时。
Expand Down
3 changes: 2 additions & 1 deletion cfg.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"http": {
"enabled": false,
"listen": ":1989"
"listen": ":1989",
"trustIps":[["192.168.0.1","192.168.0.2"]]
}
}
10 changes: 9 additions & 1 deletion funcs/custmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package funcs
import (
"errors"
"log"
"strconv"

"time"

Expand Down Expand Up @@ -154,8 +155,15 @@ func interfaceTofloat64(v interface{}) (float64, error) {
return float64(value), nil
case float64:
return value, nil
case string:
value_parsed, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, err
} else {
return value_parsed, nil
}
default:
err = errors.New("value is not digital")
err = errors.New("value cannot not Parse to digital")
return 0, err
}
}
5 changes: 3 additions & 2 deletions funcs/swsystem.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package funcs

import (
"log"

"github.com/gaochao1/sw"
"github.com/gaochao1/swcollector/g"
"log"
)

type SwSystem struct {
Expand Down Expand Up @@ -71,7 +72,7 @@ func swSystemInfo(ip string, ch chan SwSystem) {
swSystem.Mem = memUtili
}

swModel, err := sw.SysModel(ip, g.Config().Switch.Community, timeout)
swModel, err := sw.SysModel(ip, g.Config().Switch.Community, timeout, 1)
if err != nil {
log.Println(err)
} else {
Expand Down
13 changes: 3 additions & 10 deletions g/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ type SwitchConfig struct {
FastPingMode bool `json:"fastPingMode"`
}

type HeartbeatConfig struct {
Enabled bool `json:"enabled"`
Addr string `json:"addr"`
Interval int `json:"interval"`
Timeout int `json:"timeout"`
}

type TransferConfig struct {
Enabled bool `json:"enabled"`
Addr string `json:"addr"`
Expand All @@ -63,8 +56,9 @@ type TransferConfig struct {
}

type HttpConfig struct {
Enabled bool `json:"enabled"`
Listen string `json:"listen"`
Enabled bool `json:"enabled"`
Listen string `json:"listen"`
TrustIps []string `json:trustIps`
}

type SwitchHostsConfig struct {
Expand All @@ -81,7 +75,6 @@ type GlobalConfig struct {
Debug bool `json:"debug"`
Debugmetric *DebugmetricConfig `json:"debugmetric`
Switch *SwitchConfig `json:"switch"`
Heartbeat *HeartbeatConfig `json:"heartbeat"`
Transfer *TransferConfig `json:"transfer"`
SwitchHosts *SwitchHostsConfig `json:switchhosts`
CustomMetrics *CustomMetricsConfig `json:customMetrics`
Expand Down
3 changes: 2 additions & 1 deletion g/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
// 4.0.4 add lock on map;add limconn for switch snmp request
// 4.0.5 add custom metric,custom host
// 4.0.6.1 fix channal closed bug
// 4.0.6.2 fix Vendor bug;add remote config api
const (
VERSION = "4.0.6.1"
VERSION = "4.0.6.2"
COLLECT_INTERVAL = time.Second
)
33 changes: 29 additions & 4 deletions g/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"log"
"os"
"strings"
"sync"

"github.com/toolkits/slice"

"time"

Expand Down Expand Up @@ -71,10 +74,6 @@ func SendToTransfer(metrics []*model.MetricValue) {
if array_include(debug_Tags, metric_tags) {
log.Printf("=> <Total=%d> %v\n", len(metrics), metric)
}
if debug_tags == "" {
log.Printf("=> <Total=%d> %v\n", len(metrics), metric)
}

}
}
}
Expand Down Expand Up @@ -113,3 +112,29 @@ func in_array(a string, array []string) bool {
}
return false
}

var (
ips []string
ipsLock = new(sync.Mutex)
)

func TrustableIps() []string {
ipsLock.Lock()
defer ipsLock.Unlock()
ips := Config().Http.TrustIps
return ips
}

func IsTrustable(remoteAddr string) bool {
ip := remoteAddr
idx := strings.LastIndex(remoteAddr, ":")
if idx > 0 {
ip = remoteAddr[0:idx]
}

if ip == "127.0.0.1" {
return true
}

return slice.ContainsString(TrustableIps(), ip)
}
35 changes: 34 additions & 1 deletion http/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package http

import (
"net/http"
"os"
"time"

"github.com/gaochao1/swcollector/g"

"github.com/toolkits/file"
)
Expand All @@ -11,5 +15,34 @@ func configAdminRoutes() {
http.HandleFunc("/workdir", func(w http.ResponseWriter, r *http.Request) {
RenderDataJson(w, file.SelfDir())
})

http.HandleFunc("/ips", func(w http.ResponseWriter, r *http.Request) {
RenderDataJson(w, g.TrustableIps())
})
http.HandleFunc("/exit", func(w http.ResponseWriter, r *http.Request) {
if g.IsTrustable(r.RemoteAddr) {
w.Write([]byte("exiting..."))
go func() {
time.Sleep(time.Second)
os.Exit(0)
}()
} else {
w.Write([]byte("no privilege"))
}
})
http.HandleFunc("/config/reload", func(w http.ResponseWriter, r *http.Request) {
if g.IsTrustable(r.RemoteAddr) {
g.ParseConfig(g.ConfigFile)
if g.Config().SwitchHosts.Enabled {
hostcfg := g.Config().SwitchHosts.Hosts
g.ParseHostConfig(hostcfg)
}
if g.Config().CustomMetrics.Enabled {
custMetrics := g.Config().CustomMetrics.Template
g.ParseCustConfig(custMetrics)
}
RenderDataJson(w, g.Config())
} else {
w.Write([]byte("no privilege"))
}
})
}

0 comments on commit 98b2cfc

Please sign in to comment.