diff --git a/ChangeLog.md b/ChangeLog.md index 8d787b1..9bba04d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,4 +1,17 @@ # Changelog # +## 4.0.2 ## +#### 新功能 #### +1. 增加接口速率的采集 + * switch.if.Speed +2. 增加接口流量百分比的采集 + * switch.if.InSpeedPercent + * switch.if.OutSpeedPercent + +#### 改进 #### +1. Counter 类型的数据上报逻辑大幅更改。现在 swcollector 将在本地计算出相应的数值,再以 Gauge 类型上报。如果出现异常的数据,则在本地直接抛弃。因此最终呈现的绘图至多只会出现断点,而不再会出现极端的异常图形。 +2. 优化了 gosnmp 的端口采集,现在 gosnmp 端口采集的超时情况应该大幅度降低了 + + ## 3.2.1.1 ## 1. debugmetric 现在支持配置多个 endpoint 和 metric 了 ## 3.2.1 ## @@ -21,7 +34,7 @@ 6. 现在能够通过 gosnmp 的配置选项,选择采用 gosnmp 还是 snmpwalk 进行数据采集了。两者效率相仿,snmpwalk稍微多占用点系统资源 -### 改进 ### +#### 改进 #### 1. 优化了 gosnmp 的端口采集,略微控制了一下并发速率,现在 gosnmp 采集端口超时的概率,应该有所降低了 2. 代码优化,删除了部分无关代码(比如 hbs 相关的部分……) 3. 部分日志的输出可读性更强了 diff --git a/README.md b/README.md index 71098dc..e87088a 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ * IfOutErros * IfInUnknownProtos * IfOutQLen +* IfSpeed +* IfSpeedPercent * IfOperStatus(接口状态,1 up, 2 down, 3 testing, 4 unknown, 5 dormant, 6 notPresent, 7 lowerLayerDown) @@ -82,36 +84,43 @@ swcollector需要部署到有交换机SNMP访问权限的服务器上。 ``` { "debug": true, - "debugmetric":{ # 在日志中 debug 具体的指标 - "endpoints":["192.168.56.101","192.168.56.102"], # 必填 - "metrics":["switch.if.In","switch.if.Out"], # 必填 - "tags":"ifName=Fa0/1" # 有则匹配 tag,如为 "" 则打印该 metric 的全部信息 + "debugmetric":{ # 在日志中 debug 具体的指标 + "endpoints":["192.168.56.101","192.168.56.102"], # 必填 + "metrics":["switch.if.In","switch.if.Out"], # 必填 + "tags":"ifName=Fa0/1" # 有则匹配 tag,如为 "" 则打印该 metric 的全部信息 }, "switch":{ - "enabled": true, #交换机IP地址段,对该网段有效IP,先发Ping包探测,对存活IP发SNMP请求 - "ipRange":[ + "enabled": true, + "ipRange":[ #交换机IP地址段,对该网段有效IP,先发Ping包探测,对存活IP发SNMP请求 "192.168.56.101/32", "192.168.56.102/32", "172.16.114.233" ], - "gosnmp":true, #是否使用 gosnmp 采集, false 则使用 snmpwalk - "pingTimeout":300, #Ping超时时间,单位毫秒 - "pingRetry":4, #Ping探测重试次数 - "community":"public", #SNMP认证字符串 - "snmpTimeout":1000, #SNMP超时时间,单位毫秒 - "snmpRetry":5, #SNMP重试次数 - "ignoreIface": ["Nu","NU","Vlan","Vl"], #忽略的接口,如Nu匹配ifName为*Nu*的接口 - "ignorePkt": true, #不采集IfHCInUcastPkts和IfHCOutUcastPkts - "ignoreBroadcastPkt": true, #不采集IfHCInBroadcastPkts和IfHCOutBroadcastPkts - "ignoreMulticastPkt": true, #不采集IfHCInMulticastPkts和IfHCOutMulticastPkts - "ignoreDiscards": true, #不采集IfInDiscards和IfOutDiscardss - "ignoreErrors": true, #不采集IfInErrors和IfOutErros - "ignoreOperStatus": true, #不采集IfOperStatus - "ignoreUnknownProtos":true, #不采集IfInUnknownProtos - "ignoreOutQLen":true, #不采集IfOutQLen - "displayByBit": true, #true时,上报的流量单位为bit,为false则单位为byte。 - "fastPingMode": false, #是否开启 fastPing 模式,开启 Ping 的效率更高,并能解决高并发时,会有小概率 ping 通宕机的交换机地址的情况。但 fastPing 可能被防火墙过滤。 - "limitConcur": 1000 #限制并发数 + "gosnmp":true, #是否使用 gosnmp 采集, false 则使用 snmpwalk + "pingTimeout":300, #Ping超时时间,单位毫秒 + "pingRetry":4, #Ping探测重试次数 + "community":"public", #SNMP认证字符串 + "snmpTimeout":1000, #SNMP超时时间,单位毫秒 + "snmpRetry":5, #SNMP重试次数 + "ignoreIface": ["Nu","NU","Vlan","Vl"], #忽略的接口,如Nu匹配ifName为*Nu*的接口 + "ignoreOperStatus": true, #不采集IfOperStatus + "speedlimit":0, #流量的上限,如果采集计算出的流量超过这个数值,则抛弃不上报。如果填0,则以接口的速率(ifSpeed)作为上限计算。注意 interface vlan 这样的虚接口是没有 ifSpeed 的,因此不进行最大值的比较。 + "ignorePkt": true, #不采集IfHCInUcastPkts和IfHCOutUcastPkts + "pktlimit": 0, #pkt的上限,如果采集计算出的包速率超过这个数值,则抛弃不上报。如果填0,则不进行最大值比较。 + "ignoreBroadcastPkt": true, #不采集IfHCInBroadcastPkts和IfHCOutBroadcastPkts + "broadcastPktlimit": 0, #broadcastPkt的上限,如果采集计算出的包速率超过这个数值,则抛弃不上报。如果填0,则不进行最大值比较。 + "ignoreMulticastPkt": true, #不采集IfHCInMulticastPkts和IfHCOutMulticastPkts + "multicastPktlimit": 0, #multicastPkt的上限,如果采集计算出的包速率超过这个数值,则抛弃不上报。如果填0,则不进行最大值比较。 + "ignoreDiscards": true, #不采集IfInDiscards和IfOutDiscards + "discardsPktlimit": 0, #discardsPkt的上限,如果采集计算出的包速率超过这个数值,则抛弃不上报。如果填0,则不进行最大值比较。 + "ignoreErrors": true, #不采集IfInErrors和IfOutErros + "errorsPktlimit": 0, #errorsPkt的上限,如果采集计算出的包速率超过这个数值,则抛弃不上报。如果填0,则不进行最大值比较。 + "ignoreUnknownProtos":true, #不采集IfInUnknownProtos + "unknownProtosPktlimit": 0, #unknownProtosPkt的上限,如果采集计算出的包速率超过这个数值,则抛弃不上报。如果填0,则不进行最大值比较。 + "ignoreOutQLen":true, #不采集IfOutQLen + "outQLenPktlimit": 0, #outQLenPkt的上限,如果采集计算出的包速率超过这个数值,则抛弃不上报。如果填0,则不进行最大值比较。 + "fastPingMode": true, + "limitConcur": 1000 }, "transfer": { "enabled": true, @@ -124,6 +133,7 @@ swcollector需要部署到有交换机SNMP访问权限的服务器上。 "listen": ":1989" } } + ``` ##部署说明 @@ -133,7 +143,7 @@ swcollector需要部署到有交换机SNMP访问权限的服务器上。 2016/08/16 21:31:24 swifstat.go:121: IP: 192.168.10.1 PingResult: true len_list: 440 UsedTime: 5 2016/08/16 21:31:24 swifstat.go:121: IP: 192.168.10.252 PingResult: true len_list: 97 UsedTime: 2 2016/08/16 21:31:24 swifstat.go:121: IP: 192.168.13.1 PingResult: true len_list: 24 UsedTime: 1 -2016/08/16 21:31:24 swifstat.go:121: IP: 192.1680.14.1 PingResult: true len_list: 23 UsedTime: 1 +2016/08/16 21:31:24 swifstat.go:121: IP: 192.168.14.1 PingResult: true len_list: 23 UsedTime: 1 2016/08/16 21:31:24 swifstat.go:121: IP: 192.168.23.1 PingResult: true len_list: 61 UsedTime: 2 2016/08/16 21:31:24 swifstat.go:121: IP: 192.168.12.1 PingResult: true len_list: 55 UsedTime: 1 2016/08/16 21:31:24 swifstat.go:121: IP: 192.168.12.5 PingResult: true len_list: 26 UsedTime: 2 diff --git a/cfg.example.json b/cfg.example.json index a62418a..b4e3cdd 100644 --- a/cfg.example.json +++ b/cfg.example.json @@ -19,22 +19,22 @@ "snmpTimeout":1000, "snmpRetry":5, "ignoreIface": ["Nu","NU","Vlan","Vl"], - "ignoreOperStatus": false, + "ignoreOperStatus": true, "speedlimit":0, - "ignorePkt": false, - "pktlimit": 100000000, - "ignoreBroadcastPkt": false, - "broadcastPktlimit": 100000000, - "ignoreMulticastPkt": false, - "multicastPktlimit": 100000000, - "ignoreDiscards": false, - "discardsPktlimit": 100000000, - "ignoreErrors": false, - "errorsPktlimit": 100000000, - "ignoreUnknownProtos":false, - "unknownProtosPktlimit": 100000000, - "ignoreOutQLen":false, - "outQLenPktlimit": 100000000, + "ignorePkt": true, + "pktlimit": 0, + "ignoreBroadcastPkt": true, + "broadcastPktlimit": 0, + "ignoreMulticastPkt": true, + "multicastPktlimit": 0, + "ignoreDiscards": true, + "discardsPktlimit": 0, + "ignoreErrors": true, + "errorsPktlimit": 0, + "ignoreUnknownProtos":true, + "unknownProtosPktlimit": 0, + "ignoreOutQLen":true, + "outQLenPktlimit": 0, "fastPingMode": true, "limitConcur": 1000 }, diff --git a/funcs/swconn.go b/funcs/swconn.go index 73e4729..6b4fbe0 100644 --- a/funcs/swconn.go +++ b/funcs/swconn.go @@ -43,6 +43,7 @@ func connMetrics(ip string, ch chan SwConn) { 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 } diff --git a/funcs/swcpu.go b/funcs/swcpu.go index 31f708a..2c21dbe 100644 --- a/funcs/swcpu.go +++ b/funcs/swcpu.go @@ -38,6 +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 return } diff --git a/funcs/swifstat.go b/funcs/swifstat.go index fa71169..e4c202d 100644 --- a/funcs/swifstat.go +++ b/funcs/swifstat.go @@ -133,16 +133,16 @@ func swIfMetrics() (L []*model.MetricValue) { if ifStat.IfIndex == lastifStat.IfIndex { interval := ifStat.TS - lastifStat.TS broadcastlimit := g.Config().Switch.BroadcastPktlimit - IfHCInBroadcastPkts := float64(ifStat.IfHCInBroadcastPkts-lastifStat.IfHCInBroadcastPkts) / float64(interval) - IfHCOutBroadcastPkts := float64(ifStat.IfHCOutBroadcastPkts-lastifStat.IfHCOutBroadcastPkts) / float64(interval) - if IfHCInBroadcastPkts >= 0 && IfHCInBroadcastPkts <= broadcastlimit { + 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 IfHCOutBroadcastPkts >= 0 && IfHCOutBroadcastPkts <= broadcastlimit { + 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) @@ -159,16 +159,16 @@ func swIfMetrics() (L []*model.MetricValue) { if ifStat.IfIndex == lastifStat.IfIndex { interval := ifStat.TS - lastifStat.TS multicastlimit := g.Config().Switch.MulticastPktlimit - IfHCInMulticastPkts := float64(ifStat.IfHCInMulticastPkts-lastifStat.IfHCInMulticastPkts) / float64(interval) - IfHCOutMulticastPkts := float64(ifStat.IfHCOutMulticastPkts-lastifStat.IfHCOutMulticastPkts) / float64(interval) - if IfHCInMulticastPkts >= 0 && IfHCInMulticastPkts <= multicastlimit { + 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 IfHCOutMulticastPkts >= 0 && IfHCOutMulticastPkts <= multicastlimit { + 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) @@ -186,16 +186,16 @@ func swIfMetrics() (L []*model.MetricValue) { if ifStat.IfIndex == lastifStat.IfIndex { interval := ifStat.TS - lastifStat.TS discardlimit := g.Config().Switch.DiscardsPktlimit - IfInDiscards := float64(ifStat.IfInDiscards-lastifStat.IfInDiscards) / float64(interval) - IfOutDiscards := float64(ifStat.IfOutDiscards-lastifStat.IfOutDiscards) / float64(interval) - if IfInDiscards >= 0 && IfInDiscards <= discardlimit { + 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 IfOutDiscards >= 0 && IfOutDiscards <= discardlimit { + 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) @@ -213,16 +213,16 @@ func swIfMetrics() (L []*model.MetricValue) { if ifStat.IfIndex == lastifStat.IfIndex { interval := ifStat.TS - lastifStat.TS errorlimit := g.Config().Switch.ErrorsPktlimit - IfInErrors := float64(ifStat.IfInErrors-lastifStat.IfInErrors) / float64(interval) - IfOutErrors := float64(ifStat.IfOutErrors-lastifStat.IfOutErrors) / float64(interval) - if IfInErrors >= 0 && IfInErrors <= errorlimit { + 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 IfOutErrors >= 0 && IfOutErrors <= errorlimit { + 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) @@ -240,8 +240,8 @@ func swIfMetrics() (L []*model.MetricValue) { if ifStat.IfIndex == lastifStat.IfIndex { interval := ifStat.TS - lastifStat.TS unknownProtoslimit := g.Config().Switch.UnknownProtosPktlimit - IfInUnknownProtos := float64(ifStat.IfInUnknownProtos-lastifStat.IfInUnknownProtos) / float64(interval) - if IfInUnknownProtos >= 0 && IfInUnknownProtos <= unknownProtoslimit { + 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) @@ -259,8 +259,8 @@ func swIfMetrics() (L []*model.MetricValue) { if ifStat.IfIndex == lastifStat.IfIndex { interval := ifStat.TS - lastifStat.TS outQlenlimit := g.Config().Switch.OutQLenPktlimit - IfOutQLen := float64(ifStat.IfOutQLen-lastifStat.IfOutQLen) / float64(interval) - if IfOutQLen >= 0 && IfOutQLen <= outQlenlimit { + 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) @@ -279,16 +279,16 @@ func swIfMetrics() (L []*model.MetricValue) { if ifStat.IfIndex == lastifStat.IfIndex { interval := ifStat.TS - lastifStat.TS pktlimit := g.Config().Switch.Pktlimit - IfHCInUcastPkts := float64(ifStat.IfHCInUcastPkts-lastifStat.IfHCInUcastPkts) / float64(interval) - IfHCOutUcastPkts := float64(ifStat.IfHCOutUcastPkts-lastifStat.IfHCOutUcastPkts) / float64(interval) - if IfHCInUcastPkts >= 0 && IfHCInUcastPkts <= 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 IfHCOutUcastPkts >= 0 && IfHCOutUcastPkts <= pktlimit { + 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) @@ -307,23 +307,27 @@ func swIfMetrics() (L []*model.MetricValue) { if speedlimit == 0 { speedlimit = float64(ifStat.IfSpeed) } - IfHCInOctets := 8 * float64(ifStat.IfHCInOctets-lastifStat.IfHCInOctets) / float64(interval) - IfHCOutOctets := 8 * float64(ifStat.IfHCOutOctets-lastifStat.IfHCOutOctets) / float64(interval) - if IfHCInOctets >= 0 && IfHCInOctets <= speedlimit { - InSpeedPercent := IfHCInOctets / 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)) - L = append(L, GaugeValueIp(ts, ip, "switch.if.InSpeedPercent", InSpeedPercent, 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, "Limit is ", speedlimit) + 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 IfHCOutOctets >= 0 && IfHCOutOctets <= speedlimit { - OutSpeedPercent := IfHCOutOctets / float64(ifStat.IfSpeed) + if limitCheck(IfHCOutOctets, speedlimit) { L = append(L, GaugeValueIp(ts, ip, "switch.if.Out", IfHCOutOctets, ifNameTag, ifIndexTag)) - L = append(L, GaugeValueIp(ts, ip, "switch.if.OutSpeedPercent", OutSpeedPercent, 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, "Limit is ", speedlimit) + 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) } @@ -358,6 +362,18 @@ func pingCheck(ip string) bool { return pingResult } +func limitCheck(value float64, limit float64) bool { + if value < 0 { + return false + } + if limit > 0 { + if value > limit { + return false + } + } + return true +} + func coreSwIfMetrics(ip string, ch chan ChIfStat, limitCh chan bool) { var startTime, endTime int64 startTime = time.Now().Unix() diff --git a/funcs/swmem.go b/funcs/swmem.go index c822676..3627d2d 100644 --- a/funcs/swmem.go +++ b/funcs/swmem.go @@ -38,6 +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 return } diff --git a/g/const.go b/g/const.go index e976694..7c0f510 100644 --- a/g/const.go +++ b/g/const.go @@ -15,8 +15,8 @@ import ( // 3.2.1.2 gosnmp use getnext to walk snmp // 4.0.0 caculate counter type on swcollect local,add speedpercent // 4.0.1 fix sometimes ifstat pannic - +// 4.0.2 fix speedpercent bug const ( - VERSION = "4.0.1" + VERSION = "4.0.2" COLLECT_INTERVAL = time.Second ) diff --git a/gitversion b/gitversion new file mode 100644 index 0000000..e69de29