From 5711141722caa652a4daa35bb2980aafeba5c05b Mon Sep 17 00:00:00 2001 From: Feng_Qi Date: Fri, 4 Aug 2017 14:45:11 +0800 Subject: [PATCH] 4.0.6 update --- ChangeLog.md | 13 +- README.md | 82 +++++- cfg.json.example => cfg.example.json | 2 +- control | 2 +- custom.example.json | 32 +++ custom.json | 17 -- funcs/custmetric.go | 72 +++-- funcs/funcs.go | 1 - funcs/swconn.go | 55 ---- funcs/swcpu.go | 2 +- funcs/swifstat.go | 375 ++++++++++++++------------- funcs/swmem.go | 2 +- g/const.go | 2 +- g/customcfg.go | 9 +- g/var.go | 5 + gitversion | 1 + hosts.example.json | 9 + hosts.json | 6 - 18 files changed, 387 insertions(+), 300 deletions(-) rename cfg.json.example => cfg.example.json (97%) create mode 100644 custom.example.json delete mode 100644 custom.json delete mode 100644 funcs/swconn.go create mode 100644 hosts.example.json delete mode 100644 hosts.json diff --git a/ChangeLog.md b/ChangeLog.md index 7293638..65b45d7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,19 +1,26 @@ # Changelog # -## 4.0.2 ## +## 4.0.6 ## #### 新功能 #### 1. 增加接口速率的采集 * switch.if.Speed 2. 增加接口流量百分比的采集 * switch.if.InSpeedPercent * switch.if.OutSpeedPercent - +3. 增加 dell 交换机的内置 cpu/mem 采集 +4. 现在支持自定义 oid 的采集了 +5. 现在支持自定义交换机 host,以 host 作为 endpoint 上报 +6. 现在支持地址段的方式配置采集列表,例如 ```"192.168.56.102-192.168.56.120"``` + #### 改进 #### 1. Counter 类型的数据上报逻辑大幅更改。现在 swcollector 将在本地计算出相应的数值,再以 Gauge 类型上报。如果出现异常的数据,则在本地直接抛弃。因此最终呈现的绘图至多只会出现断点,而不再会出现极端的异常图形。 -2. 优化了 gosnmp 的端口采集,现在 gosnmp 端口采集的超时情况应该大幅度降低了 +2. 优化了 gosnmp 的端口采集,现在 gosnmp 端口采集异常的超时情况应该大幅度降低了 +3. 现在如果并发采集的某个 goroutine 的耗时超过了采集周期,则该 goroutine 会超时退出,避免异常时大量 goroutine 耗尽资源 +4. 移除了对 Cisco_ASA 的防火墙连接数内置采集,此类需求今后可通过自定义 oid 方式采集。 #### bug修复 #### 1. 现在当 cpu 和 mem 采集异常的时候,应该能正确的抛弃。而不是上报一个 0 了 + ## 3.2.1.1 ## 1. debugmetric 现在支持配置多个 endpoint 和 metric 了 ## 3.2.1 ## diff --git a/README.md b/README.md index 2ba145f..c9a95b3 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ CPU和内存的OID私有,根据设备厂家和OS版本可能不同。目前测 * H3C(Version 5) * H3C(Version 5.20) * H3C(Version 7) +* DELL ##二进制安装 从[这里](https://github.com/gaochao1/swcollector/releases) 下载编译好的最新二进制版本即可。注意:这些二进制只能跑在64位Linux上 @@ -143,10 +144,63 @@ swcollector需要部署到有交换机SNMP访问权限的服务器上。 } } +``` +自定义 host 配置说明 +``` +{ + "hosts": + { + "192.168.160":"test1", + "192.168.88.161":"test2", + "192.168.33.2":"test3", + "192.168.31.51":"test4" + } +} +``` +自定义 oid 配置说明 + +``` +{ + "metrics": + [ + { + "ipRange":[ + "192.168.0.1-192.168.0.2", #使用该自定义 oid 采集的地址 + "192.168.1.1" + ], + "metric":"switch.AnyconnectSession", #自定义的 metric + "tag":"", #自定义的 tag + "type":"GUAGE", + "oid":"1.3.6.1.4.1.9.9.392.1.3.35.0" #自定义的 oid + }, + #自定义的 oid 只支持 snmp get 方式采集,因此务必填写完整,建议先通过 snmpwalk 验证一下。 + #这是 cisco asa 上 anyconnect 的在线数量 + { + "ipRange":[ + "192.168.1.160-192.168.1.161" + ], + "metric":"switch.ConnectionStat", + "tag":"", + "type":"GUAGE", + "oid":"1.3.6.1.4.1.9.9.147.1.2.2.2.1.5.40.6" + }, + #这是 cisco asa 上的防火墙连接数 + { + "ipRange":[ + "192.168.33.2" + ], + "metric":"switch.TempStatus", + "tag":"", + "type":"GUAGE", + "oid":"1.3.6.1.4.1.9.9.13.1.3.1.3.1004" + } + #这是 cisco 交换机的温度,注意通用的 oid 是 "1.3.6.1.4.1.9.9.13.1.3.1.3",这里 1004 是硬件 index。框式交换机可能会有多个温度(多块线卡),请根据实际需要填具体的 oid 值和相应的 tag + ] +} ``` ##部署说明 -由于是并发采集,因此每个周期的采集耗时,事实上取决于被采集的交换机中,最慢的那个。 +由于是并发采集,因此每个周期的采集耗时,主要取决于被采集的交换机中,最慢的那个。 因此我们可以在 debug 模式下观察每个交换机的采集耗时。 ``` 2016/08/16 21:31:24 swifstat.go:121: IP: 192.168.10.1 PingResult: true len_list: 440 UsedTime: 5 @@ -175,7 +229,7 @@ swcollector需要部署到有交换机SNMP访问权限的服务器上。 2016/08/16 21:31:29 swifstat.go:121: IP: 192.168.11.2 PingResult: true len_list: 348 UsedTime: 10 2016/08/16 21:31:29 swifstat.go:177: UpdateIfStats complete. Process time 10.700895998s. ``` -如下所示,我们可以发现 192.168.11.2 这个交换机的采集耗时最长,用掉了 10 秒钟,这个基本就是最后的采集周期耗时。 +如下所示,我们可以发现 192.168.11.2 这个交换机的采集耗时最长,用掉了 10 秒钟,这个占用了采集周期大部分的耗时。 我们可以根据这些信息将交换机做一下分类,snmp 响应快的放在一起用一个 swcollector 采集,snmp 的超时时间可以设置的短一些,采集周期也可以设的小一点,比如30秒 响应慢的放在一起,用另外一个 swcollector 采集,snmp 的超时时间可以设置的长一点,采集周期也可以适当的放久一点,比如5分钟。 @@ -183,6 +237,26 @@ swcollector需要部署到有交换机SNMP访问权限的服务器上。 snmp 报文的响应需要消耗 cpu,因此交换机多少都对 snmp 报文的响应有速率限制,在 CPU 过高时,还可能会丢弃 snmp,icmp等优先级不高的请求。 不同品牌的对此的限定都有不同,有些型号可以通过配置修改。如果允许的话,也可以通过放开其对 snmp 的限速控制,来加快交换机对 snmp 报文的响应速度。 -如此,在我们调节了 *“木桶” *中 *“木板”* 的长度后,swcollector 的单个实例可以很轻松的带起上百台乃至更多的交换机。 - +如此,在我们调节了 *“木桶” *中 *“木板”* 的长度后,选择了合适的采集周期后,swcollector 的单个实例可以很轻松的带起上百台乃至更多的交换机。 +## v3-v4 升级说明(重要!!!!) +由于 v4 版本的 swcollector 修改了接口数据的上报格式,从Counter修改为GAUGE。因此如果同一个 endpoint,使用升级后的 swcollector 采集时,graph 内原有数据会**全部丢失!** +因此建议在升级时,开启自定义 host 功能,将交换机的 ip 地址通过 host 自定义为新的 endpoint,例如原先采集的 ip 为 +``` + "ipRange":[ + "192.168.0.1-192.168.0.2", + "192.168.1.1" + ], +``` +开启 switchhosts.enabled = true,然后配置 hosts.json +``` +{ + "hosts": + { + "192.168.0.1":"sw-192.168.0.1", + "192.168.0.2":"sw-192.168.0.1", + "192.168.1.1":"sw-192.168.1.1" + } +} +``` +这样他会以新的 "sw-192.168.x.x" 作为 endpoint 上报,原有的 192.168.x.x 依然继续保留,历史纪录不会丢失。 \ No newline at end of file diff --git a/cfg.json.example b/cfg.example.json similarity index 97% rename from cfg.json.example rename to cfg.example.json index 0dca322..6f0d482 100644 --- a/cfg.json.example +++ b/cfg.example.json @@ -9,7 +9,7 @@ "switch":{ "enabled": true, "ipRange":[ - "10.10..88.168" + "10.10.88.168" ], "gosnmp":true, "pingTimeout":300, diff --git a/control b/control index 1126af4..8b8cbb7 100755 --- a/control +++ b/control @@ -79,7 +79,7 @@ function pack() { build git log -1 --pretty=%h > gitversion version=`./$app -v` - file_list="public control cfg.example.json $app" + file_list="public control cfg.example.json custom.example.json hosts.example.json $app" echo "...tar $app-$version.tar.gz <= $file_list" tar zcf $app-$version.tar.gz gitversion $file_list } diff --git a/custom.example.json b/custom.example.json new file mode 100644 index 0000000..9f4e27a --- /dev/null +++ b/custom.example.json @@ -0,0 +1,32 @@ +{ + "metrics": + [ + { + "ipRange":[ + "192.168.1.160","192.168.1.161" + ], + "metric":"switch.AnyconnectSession", + "tag":"", + "type":"GUAGE", + "oid":"1.3.6.1.4.1.9.9.392.1.3.35.0" + }, + { + "ipRange":[ + "192.168.1.160-192.168.1.161" + ], + "metric":"switch.ConnectionStat", + "tag":"", + "type":"GUAGE", + "oid":"1.3.6.1.4.1.9.9.147.1.2.2.2.1.5.40.6" + }, + { + "ipRange":[ + "192.168.33.2" + ], + "metric":"switch.TempStatus", + "tag":"", + "type":"GUAGE", + "oid":"1.3.6.1.4.1.9.9.13.1.3.1.3.1004" + } + ] +} diff --git a/custom.json b/custom.json deleted file mode 100644 index ca3488e..0000000 --- a/custom.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "metrics": - [ - { - "metric":"AnyconnectSession", - "tag":"", - "type":"GUAGE", - "oid":"1.3.6.1.4.1.9.9.392.1.3.35" - }, - { - "metric":"ConnectionStat", - "tag":"", - "type":"GUAGE", - "oid":"1.3.6.1.4.1.9.9.147.1.2.2.2.1.5.40.6" - } - ] -} \ No newline at end of file diff --git a/funcs/custmetric.go b/funcs/custmetric.go index 3755683..94a205c 100644 --- a/funcs/custmetric.go +++ b/funcs/custmetric.go @@ -4,11 +4,11 @@ import ( "errors" "log" - "time" - + go_snmp "github.com/gaochao1/gosnmp" "github.com/gaochao1/sw" "github.com/gaochao1/swcollector/g" "github.com/open-falcon/common/model" + "time" ) type CustM struct { @@ -22,12 +22,43 @@ type CustmMetric struct { metrictype string } +func InArray(str string, array []string) bool { + for _, s := range array { + if str == s { + return true + } + } + return false +} + +func AllCustmIp(ipRange []string) (allIp []string) { + if len(ipRange) > 0 { + for _, sip := range ipRange { + aip := sw.ParseIp(sip) + for _, ip := range aip { + allIp = append(allIp, ip) + } + } + } + return allIp +} + func CustMetrics() (L []*model.MetricValue) { - chs := make([]chan CustM, len(AliveIp)) - for i, ip := range AliveIp { + if !g.Config().CustomMetrics.Enabled { + return + } + chs := make([]chan CustM, 0) + for _, ip := range AliveIp { if ip != "" { - chs[i] = make(chan CustM) - go custMetrics(ip, chs[i]) + chss := make(chan CustM) + for _, metric := range g.CustConfig().Metrics { + CustmIps := AllCustmIp(metric.IpRange) + if InArray(ip, CustmIps) { + go custMetrics(ip, metric, chss) + chs = append(chs, chss) + } + } + } } for _, ch := range chs { @@ -47,23 +78,24 @@ func CustMetrics() (L []*model.MetricValue) { return L } -func custMetrics(ip string, ch chan CustM) { +func custMetrics(ip string, metric *g.MetricConfig, ch chan CustM) { var custm CustM var custmmetric CustmMetric var custmmetrics []CustmMetric - for _, metric := range g.CustConfig().Metrics { - value, err := GetCustMetric(ip, g.Config().Switch.Community, metric.Oid, g.Config().Switch.SnmpTimeout, g.Config().Switch.SnmpRetry) - if err != nil { - log.Println(err) - } else { - custmmetric.metric = metric.Metric - custmmetric.metrictype = metric.Type - custmmetric.tag = metric.Tag - custmmetric.value = value - custmmetrics = append(custmmetrics, custmmetric) - } + value, err := GetCustMetric(ip, g.Config().Switch.Community, metric.Oid, g.Config().Switch.SnmpTimeout, g.Config().Switch.SnmpRetry) + if err != nil { + log.Println(ip, metric.Oid, err) + close(ch) + return + } else { + custmmetric.metric = metric.Metric + custmmetric.metrictype = metric.Type + custmmetric.tag = metric.Tag + custmmetric.value = value + custmmetrics = append(custmmetrics, custmmetric) } + custm.Ip = ip custm.custmMetrics = custmmetrics ch <- custm @@ -79,15 +111,15 @@ func GetCustMetric(ip, community, oid string, timeout, retry int) (float64, erro method := "get" var value float64 var err error + var snmpPDUs []go_snmp.SnmpPDU for i := 0; i < retry; i++ { - snmpPDUs, err := sw.RunSnmp(ip, community, oid, method, timeout) + snmpPDUs, err = sw.RunSnmp(ip, community, oid, method, timeout) if len(snmpPDUs) > 0 && err == nil { value, err = interfaceTofloat64(snmpPDUs[0].Value) break } time.Sleep(100 * time.Millisecond) } - return value, err } diff --git a/funcs/funcs.go b/funcs/funcs.go index 801d0e4..cafe862 100644 --- a/funcs/funcs.go +++ b/funcs/funcs.go @@ -21,7 +21,6 @@ func BuildMappers() { CpuMetrics, MemMetrics, PingMetrics, - ConnMetrics, CustMetrics, }, Interval: interval, diff --git a/funcs/swconn.go b/funcs/swconn.go deleted file mode 100644 index 6b4fbe0..0000000 --- a/funcs/swconn.go +++ /dev/null @@ -1,55 +0,0 @@ -package funcs - -import ( - "log" - "strings" - "time" - - "github.com/gaochao1/sw" - "github.com/gaochao1/swcollector/g" - "github.com/open-falcon/common/model" -) - -type SwConn struct { - Ip string - ConnectionStat int -} - -func ConnMetrics() (L []*model.MetricValue) { - - chs := make([]chan SwConn, len(AliveIp)) - for i, ip := range AliveIp { - if ip != "" { - chs[i] = make(chan SwConn) - go connMetrics(ip, chs[i]) - } - } - - for _, ch := range chs { - swConn := <-ch - L = append(L, GaugeValueIp(time.Now().Unix(), swConn.Ip, "switch.ConnectionStat", swConn.ConnectionStat)) - } - - return L -} - -func connMetrics(ip string, ch chan SwConn) { - var swConn SwConn - vendor, _ := sw.SysVendor(ip, community, snmpTimeout) - if !strings.Contains(vendor, "Cisco_ASA") { - ch <- swConn - return - } - ConnectionStat, err := sw.ConnectionStat(ip, g.Config().Switch.Community, g.Config().Switch.SnmpTimeout, g.Config().Switch.SnmpRetry) - if err != nil { - log.Println(err) - ch <- swConn - return - } - - swConn.Ip = ip - swConn.ConnectionStat = ConnectionStat - ch <- swConn - - return -} diff --git a/funcs/swcpu.go b/funcs/swcpu.go index 2c21dbe..2fe977b 100644 --- a/funcs/swcpu.go +++ b/funcs/swcpu.go @@ -38,7 +38,7 @@ func cpuMetrics(ip string, ch chan SwCpu) { cpuUtili, err := sw.CpuUtilization(ip, g.Config().Switch.Community, g.Config().Switch.SnmpTimeout, g.Config().Switch.SnmpRetry) if err != nil { log.Println(err) - ch <- swCpu + close(ch) return } diff --git a/funcs/swifstat.go b/funcs/swifstat.go index 69d7b99..dfb04a4 100644 --- a/funcs/swifstat.go +++ b/funcs/swifstat.go @@ -129,6 +129,7 @@ func swIfMetrics() (L []*model.MetricValue) { initVariable() ts := time.Now().Unix() allIp := AllSwitchIp() + timeout := time.Duration(g.Config().Transfer.Interval) * time.Second chs := make([]chan ChIfStat, len(allIp)) limitCh := make(chan bool, g.Config().Switch.LimitConcur) @@ -145,236 +146,240 @@ func swIfMetrics() (L []*model.MetricValue) { go coreSwIfMetrics(ip, chs[i], limitCh) time.Sleep(5 * time.Millisecond) } - for _, ch := range chs { - chIfStat := <-ch + for i, ch := range chs { + select { + case chIfStat := <-ch: - if chIfStat.PingResult == true && !slice.ContainsString(AliveIp, chIfStat.Ip) { - AliveIp = append(AliveIp, chIfStat.Ip) - } - if chIfStat.IfStatsList != nil { - if g.Config().Debug { - log.Println("IP:", chIfStat.Ip, "PingResult:", chIfStat.PingResult, "len_list:", len(*chIfStat.IfStatsList), "UsedTime:", chIfStat.UseTime) + if chIfStat.PingResult == true && !slice.ContainsString(AliveIp, chIfStat.Ip) { + AliveIp = append(AliveIp, chIfStat.Ip) } - - for _, ifStat := range *chIfStat.IfStatsList { - ifNameTag := "ifName=" + ifStat.IfName - ifIndexTag := "ifIndex=" + strconv.Itoa(ifStat.IfIndex) - ip := chIfStat.Ip - if ignoreOperStatus == false { - L = append(L, GaugeValueIp(ifStat.TS, ip, "switch.if.OperStatus", ifStat.IfOperStatus, ifNameTag, ifIndexTag)) + if chIfStat.IfStatsList != nil { + if g.Config().Debug { + log.Println("IP:", chIfStat.Ip, "PingResult:", chIfStat.PingResult, "len_list:", len(*chIfStat.IfStatsList), "UsedTime:", chIfStat.UseTime) } - if ignoreSpeedPercent == false { - L = append(L, GaugeValueIp(ifStat.TS, ip, "switch.if.Speed", ifStat.IfSpeed, ifNameTag, ifIndexTag)) - } - if ignoreBroadcastPkt == false { - if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { - for _, lastifStat := range *lastIfStatList { - if ifStat.IfIndex == lastifStat.IfIndex { - interval := ifStat.TS - lastifStat.TS - broadcastlimit := g.Config().Switch.BroadcastPktlimit - IfHCInBroadcastPkts := (float64(ifStat.IfHCInBroadcastPkts) - float64(lastifStat.IfHCInBroadcastPkts)) / float64(interval) - IfHCOutBroadcastPkts := (float64(ifStat.IfHCOutBroadcastPkts) - float64(lastifStat.IfHCOutBroadcastPkts)) / float64(interval) - if limitCheck(IfHCInBroadcastPkts, broadcastlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.InBroadcastPkt", IfHCInBroadcastPkts, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.InBroadcastPkt ", "out of range, value is ", IfHCInBroadcastPkts, "Limit is ", broadcastlimit) - log.Println("IfHCInBroadcastPkts This Time: ", ifStat.IfHCInBroadcastPkts) - log.Println("IfHCInBroadcastPkts Last Time: ", lastifStat.IfHCInBroadcastPkts) - } - if limitCheck(IfHCOutBroadcastPkts, broadcastlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.OutBroadcastPkt", IfHCOutBroadcastPkts, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.OutBroadcastPkt ", "out of range, value is ", IfHCOutBroadcastPkts, "Limit is ", broadcastlimit) - log.Println("IfHCOutBroadcastPkts This Time: ", ifStat.IfHCOutBroadcastPkts) - log.Println("IfHCOutBroadcastPkts Last Time: ", lastifStat.IfHCOutBroadcastPkts) + + for _, ifStat := range *chIfStat.IfStatsList { + ifNameTag := "ifName=" + ifStat.IfName + ifIndexTag := "ifIndex=" + strconv.Itoa(ifStat.IfIndex) + ip := chIfStat.Ip + if ignoreOperStatus == false { + L = append(L, GaugeValueIp(ifStat.TS, ip, "switch.if.OperStatus", ifStat.IfOperStatus, ifNameTag, ifIndexTag)) + } + if ignoreSpeedPercent == false { + L = append(L, GaugeValueIp(ifStat.TS, ip, "switch.if.Speed", ifStat.IfSpeed, ifNameTag, ifIndexTag)) + } + if ignoreBroadcastPkt == false { + if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { + for _, lastifStat := range *lastIfStatList { + if ifStat.IfIndex == lastifStat.IfIndex { + interval := ifStat.TS - lastifStat.TS + broadcastlimit := g.Config().Switch.BroadcastPktlimit + IfHCInBroadcastPkts := (float64(ifStat.IfHCInBroadcastPkts) - float64(lastifStat.IfHCInBroadcastPkts)) / float64(interval) + IfHCOutBroadcastPkts := (float64(ifStat.IfHCOutBroadcastPkts) - float64(lastifStat.IfHCOutBroadcastPkts)) / float64(interval) + if limitCheck(IfHCInBroadcastPkts, broadcastlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.InBroadcastPkt", IfHCInBroadcastPkts, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.InBroadcastPkt ", "out of range, value is ", IfHCInBroadcastPkts, "Limit is ", broadcastlimit) + log.Println("IfHCInBroadcastPkts This Time: ", ifStat.IfHCInBroadcastPkts) + log.Println("IfHCInBroadcastPkts Last Time: ", lastifStat.IfHCInBroadcastPkts) + } + if limitCheck(IfHCOutBroadcastPkts, broadcastlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.OutBroadcastPkt", IfHCOutBroadcastPkts, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.OutBroadcastPkt ", "out of range, value is ", IfHCOutBroadcastPkts, "Limit is ", broadcastlimit) + log.Println("IfHCOutBroadcastPkts This Time: ", ifStat.IfHCOutBroadcastPkts) + log.Println("IfHCOutBroadcastPkts Last Time: ", lastifStat.IfHCOutBroadcastPkts) + } } } } } - } - if ignoreMulticastPkt == false { - if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { - for _, lastifStat := range *lastIfStatList { - if ifStat.IfIndex == lastifStat.IfIndex { - interval := ifStat.TS - lastifStat.TS - multicastlimit := g.Config().Switch.MulticastPktlimit - IfHCInMulticastPkts := (float64(ifStat.IfHCInMulticastPkts) - float64(lastifStat.IfHCInMulticastPkts)) / float64(interval) - IfHCOutMulticastPkts := (float64(ifStat.IfHCOutMulticastPkts) - float64(lastifStat.IfHCOutMulticastPkts)) / float64(interval) - if limitCheck(IfHCInMulticastPkts, multicastlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.InMulticastPkt", IfHCInMulticastPkts, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.InMulticastPkt ", "out of range, value is ", IfHCInMulticastPkts, "Limit is ", multicastlimit) - log.Println("IfHCInMulticastPkts This Time: ", ifStat.IfHCInMulticastPkts) - log.Println("IfHCInMulticastPkts Last Time: ", lastifStat.IfHCInMulticastPkts) - } - if limitCheck(IfHCOutMulticastPkts, multicastlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.OutMulticastPkt", IfHCOutMulticastPkts, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.OutMulticastPkt ", "out of range, value is ", IfHCOutMulticastPkts, "Limit is ", multicastlimit) - log.Println("IfHCOutMulticastPkts This Time: ", ifStat.IfHCOutMulticastPkts) - log.Println("IfHCOutMulticastPkts Last Time: ", lastifStat.IfHCOutMulticastPkts) + if ignoreMulticastPkt == false { + if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { + for _, lastifStat := range *lastIfStatList { + if ifStat.IfIndex == lastifStat.IfIndex { + interval := ifStat.TS - lastifStat.TS + multicastlimit := g.Config().Switch.MulticastPktlimit + IfHCInMulticastPkts := (float64(ifStat.IfHCInMulticastPkts) - float64(lastifStat.IfHCInMulticastPkts)) / float64(interval) + IfHCOutMulticastPkts := (float64(ifStat.IfHCOutMulticastPkts) - float64(lastifStat.IfHCOutMulticastPkts)) / float64(interval) + if limitCheck(IfHCInMulticastPkts, multicastlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.InMulticastPkt", IfHCInMulticastPkts, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.InMulticastPkt ", "out of range, value is ", IfHCInMulticastPkts, "Limit is ", multicastlimit) + log.Println("IfHCInMulticastPkts This Time: ", ifStat.IfHCInMulticastPkts) + log.Println("IfHCInMulticastPkts Last Time: ", lastifStat.IfHCInMulticastPkts) + } + if limitCheck(IfHCOutMulticastPkts, multicastlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.OutMulticastPkt", IfHCOutMulticastPkts, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.OutMulticastPkt ", "out of range, value is ", IfHCOutMulticastPkts, "Limit is ", multicastlimit) + log.Println("IfHCOutMulticastPkts This Time: ", ifStat.IfHCOutMulticastPkts) + log.Println("IfHCOutMulticastPkts Last Time: ", lastifStat.IfHCOutMulticastPkts) + } } } } } - } - if ignoreDiscards == false { - if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { - for _, lastifStat := range *lastIfStatList { - if ifStat.IfIndex == lastifStat.IfIndex { - interval := ifStat.TS - lastifStat.TS - discardlimit := g.Config().Switch.DiscardsPktlimit - IfInDiscards := (float64(ifStat.IfInDiscards) - float64(lastifStat.IfInDiscards)) / float64(interval) - IfOutDiscards := (float64(ifStat.IfOutDiscards) - float64(lastifStat.IfOutDiscards)) / float64(interval) - if limitCheck(IfInDiscards, discardlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.InDiscards", IfInDiscards, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.InDiscards ", "out of range, value is ", IfInDiscards, "Limit is ", discardlimit) - log.Println("IfInDiscards This Time: ", ifStat.IfInDiscards) - log.Println("IfInDiscards Last Time: ", lastifStat.IfInDiscards) - } - if limitCheck(IfOutDiscards, discardlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.OutDiscards", IfOutDiscards, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.OutDiscards ", "out of range, value is ", IfOutDiscards, "Limit is ", discardlimit) - log.Println("IfOutDiscards This Time: ", ifStat.IfOutDiscards) - log.Println("IfOutDiscards Last Time: ", lastifStat.IfOutDiscards) + if ignoreDiscards == false { + if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { + for _, lastifStat := range *lastIfStatList { + if ifStat.IfIndex == lastifStat.IfIndex { + interval := ifStat.TS - lastifStat.TS + discardlimit := g.Config().Switch.DiscardsPktlimit + IfInDiscards := (float64(ifStat.IfInDiscards) - float64(lastifStat.IfInDiscards)) / float64(interval) + IfOutDiscards := (float64(ifStat.IfOutDiscards) - float64(lastifStat.IfOutDiscards)) / float64(interval) + if limitCheck(IfInDiscards, discardlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.InDiscards", IfInDiscards, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.InDiscards ", "out of range, value is ", IfInDiscards, "Limit is ", discardlimit) + log.Println("IfInDiscards This Time: ", ifStat.IfInDiscards) + log.Println("IfInDiscards Last Time: ", lastifStat.IfInDiscards) + } + if limitCheck(IfOutDiscards, discardlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.OutDiscards", IfOutDiscards, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.OutDiscards ", "out of range, value is ", IfOutDiscards, "Limit is ", discardlimit) + log.Println("IfOutDiscards This Time: ", ifStat.IfOutDiscards) + log.Println("IfOutDiscards Last Time: ", lastifStat.IfOutDiscards) + } } } } } - } - if ignoreErrors == false { - if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { - for _, lastifStat := range *lastIfStatList { - if ifStat.IfIndex == lastifStat.IfIndex { - interval := ifStat.TS - lastifStat.TS - errorlimit := g.Config().Switch.ErrorsPktlimit - IfInErrors := (float64(ifStat.IfInErrors) - float64(lastifStat.IfInErrors)) / float64(interval) - IfOutErrors := (float64(ifStat.IfOutErrors) - float64(lastifStat.IfOutErrors)) / float64(interval) - if limitCheck(IfInErrors, errorlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.InErrors", IfInErrors, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.InErrors ", "out of range, value is ", IfInErrors, "Limit is ", errorlimit) - log.Println("IfInErrors This Time: ", ifStat.IfInErrors) - log.Println("IfInErrors Last Time: ", lastifStat.IfInErrors) - } - if limitCheck(IfOutErrors, errorlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.OutErrors", IfOutErrors, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.OutErrors ", "out of range, value is ", IfOutErrors, "Limit is ", errorlimit) - log.Println("IfOutErrors This Time: ", ifStat.IfOutErrors) - log.Println("IfOutErrors Last Time: ", lastifStat.IfOutErrors) + if ignoreErrors == false { + if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { + for _, lastifStat := range *lastIfStatList { + if ifStat.IfIndex == lastifStat.IfIndex { + interval := ifStat.TS - lastifStat.TS + errorlimit := g.Config().Switch.ErrorsPktlimit + IfInErrors := (float64(ifStat.IfInErrors) - float64(lastifStat.IfInErrors)) / float64(interval) + IfOutErrors := (float64(ifStat.IfOutErrors) - float64(lastifStat.IfOutErrors)) / float64(interval) + if limitCheck(IfInErrors, errorlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.InErrors", IfInErrors, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.InErrors ", "out of range, value is ", IfInErrors, "Limit is ", errorlimit) + log.Println("IfInErrors This Time: ", ifStat.IfInErrors) + log.Println("IfInErrors Last Time: ", lastifStat.IfInErrors) + } + if limitCheck(IfOutErrors, errorlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.OutErrors", IfOutErrors, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.OutErrors ", "out of range, value is ", IfOutErrors, "Limit is ", errorlimit) + log.Println("IfOutErrors This Time: ", ifStat.IfOutErrors) + log.Println("IfOutErrors Last Time: ", lastifStat.IfOutErrors) + } } } } } - } - if ignoreUnknownProtos == false { - if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { - for _, lastifStat := range *lastIfStatList { - if ifStat.IfIndex == lastifStat.IfIndex { - interval := ifStat.TS - lastifStat.TS - unknownProtoslimit := g.Config().Switch.UnknownProtosPktlimit - IfInUnknownProtos := (float64(ifStat.IfInUnknownProtos) - float64(lastifStat.IfInUnknownProtos)) / float64(interval) - if limitCheck(IfInUnknownProtos, unknownProtoslimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.InUnknownProtos", IfInUnknownProtos, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.InUnknownProtos ", "out of range, value is ", IfInUnknownProtos, "Limit is ", unknownProtoslimit) - log.Println("IfOutQLen This Time: ", ifStat.IfInUnknownProtos) - log.Println("IfOutQLen Last Time: ", lastifStat.IfInUnknownProtos) + if ignoreUnknownProtos == false { + if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { + for _, lastifStat := range *lastIfStatList { + if ifStat.IfIndex == lastifStat.IfIndex { + interval := ifStat.TS - lastifStat.TS + unknownProtoslimit := g.Config().Switch.UnknownProtosPktlimit + IfInUnknownProtos := (float64(ifStat.IfInUnknownProtos) - float64(lastifStat.IfInUnknownProtos)) / float64(interval) + if limitCheck(IfInUnknownProtos, unknownProtoslimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.InUnknownProtos", IfInUnknownProtos, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.InUnknownProtos ", "out of range, value is ", IfInUnknownProtos, "Limit is ", unknownProtoslimit) + log.Println("IfOutQLen This Time: ", ifStat.IfInUnknownProtos) + log.Println("IfOutQLen Last Time: ", lastifStat.IfInUnknownProtos) + } } } } } - } - if ignoreOutQLen == false { - if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { - for _, lastifStat := range *lastIfStatList { - if ifStat.IfIndex == lastifStat.IfIndex { - interval := ifStat.TS - lastifStat.TS - outQlenlimit := g.Config().Switch.OutQLenPktlimit - IfOutQLen := (float64(ifStat.IfOutQLen) - float64(lastifStat.IfOutQLen)) / float64(interval) - if limitCheck(IfOutQLen, outQlenlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.OutQLen", IfOutQLen, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.OutQLen ", "out of range, value is ", IfOutQLen, "Limit is ", outQlenlimit) - log.Println("IfOutQLen This Time: ", ifStat.IfOutQLen) - log.Println("IfOutQLen Last Time: ", lastifStat.IfOutQLen) + if ignoreOutQLen == false { + if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { + for _, lastifStat := range *lastIfStatList { + if ifStat.IfIndex == lastifStat.IfIndex { + interval := ifStat.TS - lastifStat.TS + outQlenlimit := g.Config().Switch.OutQLenPktlimit + IfOutQLen := (float64(ifStat.IfOutQLen) - float64(lastifStat.IfOutQLen)) / float64(interval) + if limitCheck(IfOutQLen, outQlenlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.OutQLen", IfOutQLen, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.OutQLen ", "out of range, value is ", IfOutQLen, "Limit is ", outQlenlimit) + log.Println("IfOutQLen This Time: ", ifStat.IfOutQLen) + log.Println("IfOutQLen Last Time: ", lastifStat.IfOutQLen) + } } } } } - } - //如果IgnorePkt为false,采集Pkt - if ignorePkt == false { + //如果IgnorePkt为false,采集Pkt + if ignorePkt == false { + if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { + for _, lastifStat := range *lastIfStatList { + if ifStat.IfIndex == lastifStat.IfIndex { + interval := ifStat.TS - lastifStat.TS + pktlimit := g.Config().Switch.Pktlimit + IfHCInUcastPkts := (float64(ifStat.IfHCInUcastPkts) - float64(lastifStat.IfHCInUcastPkts)) / float64(interval) + IfHCOutUcastPkts := (float64(ifStat.IfHCOutUcastPkts) - float64(lastifStat.IfHCOutUcastPkts)) / float64(interval) + if limitCheck(IfHCInUcastPkts, pktlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.InPkts", IfHCInUcastPkts, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.InPkts ", "out of range, value is ", IfHCInUcastPkts, "Limit is ", pktlimit) + log.Println("IfHCInUcastPkts This Time: ", ifStat.IfHCInUcastPkts) + log.Println("IfHCInUcastPkts Last Time: ", lastifStat.IfHCInUcastPkts) + } + if limitCheck(IfHCOutUcastPkts, pktlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.OutPkts", IfHCOutUcastPkts, ifNameTag, ifIndexTag)) + } else { + log.Println(ip, ifNameTag, "switch.if.OutPkts ", "out of range, value is ", IfHCOutUcastPkts, "Limit is ", pktlimit) + log.Println("IfHCOutUcastPkts This Time: ", ifStat.IfHCOutUcastPkts) + log.Println("IfHCOutUcastPkts Last Time: ", lastifStat.IfHCOutUcastPkts) + } + } + } + } + } if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { for _, lastifStat := range *lastIfStatList { if ifStat.IfIndex == lastifStat.IfIndex { interval := ifStat.TS - lastifStat.TS - pktlimit := g.Config().Switch.Pktlimit - IfHCInUcastPkts := (float64(ifStat.IfHCInUcastPkts) - float64(lastifStat.IfHCInUcastPkts)) / float64(interval) - IfHCOutUcastPkts := (float64(ifStat.IfHCOutUcastPkts) - float64(lastifStat.IfHCOutUcastPkts)) / float64(interval) - if limitCheck(IfHCInUcastPkts, pktlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.InPkts", IfHCInUcastPkts, ifNameTag, ifIndexTag)) - } else { - log.Println(ip, ifNameTag, "switch.if.InPkts ", "out of range, value is ", IfHCInUcastPkts, "Limit is ", pktlimit) - log.Println("IfHCInUcastPkts This Time: ", ifStat.IfHCInUcastPkts) - log.Println("IfHCInUcastPkts Last Time: ", lastifStat.IfHCInUcastPkts) + speedlimit := g.Config().Switch.Speedlimit + if speedlimit == 0 { + speedlimit = float64(ifStat.IfSpeed) } - if limitCheck(IfHCOutUcastPkts, pktlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.OutPkts", IfHCOutUcastPkts, ifNameTag, ifIndexTag)) + IfHCInOctets := 8 * (float64(ifStat.IfHCInOctets) - float64(lastifStat.IfHCInOctets)) / float64(interval) + IfHCOutOctets := 8 * (float64(ifStat.IfHCOutOctets) - float64(lastifStat.IfHCOutOctets)) / float64(interval) + if limitCheck(IfHCInOctets, speedlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.In", IfHCInOctets, ifNameTag, ifIndexTag)) + if speedlimit > 0 { + InSpeedPercent := 100 * IfHCInOctets / float64(ifStat.IfSpeed) + L = append(L, GaugeValueIp(ts, ip, "switch.if.InSpeedPercent", InSpeedPercent, ifNameTag, ifIndexTag)) + } } else { - log.Println(ip, ifNameTag, "switch.if.OutPkts ", "out of range, value is ", IfHCOutUcastPkts, "Limit is ", pktlimit) - log.Println("IfHCOutUcastPkts This Time: ", ifStat.IfHCOutUcastPkts) - log.Println("IfHCOutUcastPkts Last Time: ", lastifStat.IfHCOutUcastPkts) + log.Println(ip, ifNameTag, "switch.if.In ", "out of range, value is ", IfHCInOctets) + log.Println("IfHCInOctets This Time: ", ifStat.IfHCInOctets) + log.Println("IfHCInOctets Last Time: ", lastifStat.IfHCInOctets) } - } - } - } - } - if lastIfStatList := lastifmap.Get(chIfStat.Ip); lastIfStatList != nil { - for _, lastifStat := range *lastIfStatList { - if ifStat.IfIndex == lastifStat.IfIndex { - interval := ifStat.TS - lastifStat.TS - speedlimit := g.Config().Switch.Speedlimit - if speedlimit == 0 { - speedlimit = float64(ifStat.IfSpeed) - } - IfHCInOctets := 8 * (float64(ifStat.IfHCInOctets) - float64(lastifStat.IfHCInOctets)) / float64(interval) - IfHCOutOctets := 8 * (float64(ifStat.IfHCOutOctets) - float64(lastifStat.IfHCOutOctets)) / float64(interval) - if limitCheck(IfHCInOctets, speedlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.In", IfHCInOctets, ifNameTag, ifIndexTag)) - if speedlimit > 0 { - InSpeedPercent := 100 * IfHCInOctets / float64(ifStat.IfSpeed) - L = append(L, GaugeValueIp(ts, ip, "switch.if.InSpeedPercent", InSpeedPercent, ifNameTag, ifIndexTag)) - } - } else { - log.Println(ip, ifNameTag, "switch.if.In ", "out of range, value is ", IfHCInOctets) - log.Println("IfHCInOctets This Time: ", ifStat.IfHCInOctets) - log.Println("IfHCInOctets Last Time: ", lastifStat.IfHCInOctets) - } - if limitCheck(IfHCOutOctets, speedlimit) { - L = append(L, GaugeValueIp(ts, ip, "switch.if.Out", IfHCOutOctets, ifNameTag, ifIndexTag)) - if speedlimit > 0 { - OutSpeedPercent := 100 * IfHCOutOctets / float64(ifStat.IfSpeed) - L = append(L, GaugeValueIp(ts, ip, "switch.if.OutSpeedPercent", OutSpeedPercent, ifNameTag, ifIndexTag)) + if limitCheck(IfHCOutOctets, speedlimit) { + L = append(L, GaugeValueIp(ts, ip, "switch.if.Out", IfHCOutOctets, ifNameTag, ifIndexTag)) + if speedlimit > 0 { + OutSpeedPercent := 100 * IfHCOutOctets / float64(ifStat.IfSpeed) + L = append(L, GaugeValueIp(ts, ip, "switch.if.OutSpeedPercent", OutSpeedPercent, ifNameTag, ifIndexTag)) + } + } else { + log.Println(ip, ifNameTag, "switch.if.Out ", "out of range, value is ", IfHCOutOctets) + log.Println("IfHCOutOctets This Time: ", ifStat.IfHCOutOctets) + log.Println("IfHCOutOctets Last Time: ", lastifStat.IfHCOutOctets) } - } else { - log.Println(ip, ifNameTag, "switch.if.Out ", "out of range, value is ", IfHCOutOctets) - log.Println("IfHCOutOctets This Time: ", ifStat.IfHCOutOctets) - log.Println("IfHCOutOctets Last Time: ", lastifStat.IfHCOutOctets) } } } } + lastifmap.Set(chIfStat.Ip, chIfStat.IfStatsList) } - lastifmap.Set(chIfStat.Ip, chIfStat.IfStatsList) + case <-time.After(timeout): + log.Println(allIp[i] + " go runtime timeout") } } diff --git a/funcs/swmem.go b/funcs/swmem.go index 3627d2d..16ba1a3 100644 --- a/funcs/swmem.go +++ b/funcs/swmem.go @@ -38,7 +38,7 @@ func memMetrics(ip string, ch chan SwMem) { memUtili, err := sw.MemUtilization(ip, g.Config().Switch.Community, g.Config().Switch.SnmpTimeout, g.Config().Switch.SnmpRetry) if err != nil { log.Println(err) - ch <- swMem + close(ch) return } diff --git a/g/const.go b/g/const.go index 2d8d16b..c711a90 100644 --- a/g/const.go +++ b/g/const.go @@ -19,6 +19,6 @@ import ( // 4.0.4 add lock on map;add limconn for switch snmp request // 4.0.5 add custom metric,custom host const ( - VERSION = "4.0.5" + VERSION = "4.0.6" COLLECT_INTERVAL = time.Second ) diff --git a/g/customcfg.go b/g/customcfg.go index 7ea4e63..6f1586c 100644 --- a/g/customcfg.go +++ b/g/customcfg.go @@ -10,10 +10,11 @@ import ( ) type MetricConfig struct { - Metric string `json:metric` - Tag string `json:tag` - Type string `json:type` - Oid string `json:oid` + IpRange []string `json:"ipRange"` + Metric string `json:metric` + Tag string `json:tag` + Type string `json:type` + Oid string `json:oid` } type CustomConfig struct { Metrics []*MetricConfig `json:"metrics` diff --git a/g/var.go b/g/var.go index c2aa704..f6e691f 100644 --- a/g/var.go +++ b/g/var.go @@ -82,6 +82,11 @@ func SendToTransfer(metrics []*model.MetricValue) { err := TransferClient.Call("Transfer.Update", metrics, &resp) if err != nil { log.Println("call Transfer.Update fail", err) + if debug { + for _, metric := range metrics { + log.Printf("=> %v\n", len(metrics), metric) + } + } } if debug { diff --git a/gitversion b/gitversion index e69de29..c8c8247 100644 --- a/gitversion +++ b/gitversion @@ -0,0 +1 @@ +549546b diff --git a/hosts.example.json b/hosts.example.json new file mode 100644 index 0000000..6071510 --- /dev/null +++ b/hosts.example.json @@ -0,0 +1,9 @@ +{ + "hosts": + { + "192.168.160":"test1", + "192.168.88.161":"test2", + "192.168.33.2":"test3", + "192.168.31.51":"test4" + } +} \ No newline at end of file diff --git a/hosts.json b/hosts.json deleted file mode 100644 index bb1d7ca..0000000 --- a/hosts.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "hosts": - { - "10.10.88.168":"vpn-lw" - } -}