Skip to content

Commit

Permalink
version 4.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
freedomkk-qfeng committed Jan 2, 2017
1 parent dcc71d7 commit 7433fff
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 46 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ swcollector需要部署到有交换机SNMP访问权限的服务器上。
"limitConcur": 1000, #交换机采集的并发限制
"limitCon": 4 #对于单台交换机上,多个指标采集的并发限制
},
"switchhosts":{
"enabled":true,
"hosts":"./hosts.json" #自定义的host与Ip地址对应表,如果配置,则上报时会用这里的host替换ip地址
},
"customMetrics":{
"enabled":true,
"template":"./custom.json" #自定义的metric列表,如果配置,会根据这个配置文件,采集额外的自定义metric
},
"transfer": {
"enabled": true,
"addr": "127.0.0.1:8433",
Expand Down
20 changes: 13 additions & 7 deletions cfg.example.json → cfg.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
"debug": true,
"lowermetrics": true,
"debugmetric":{
"endpoints":["192.168.56.101","192.168.56.102"],
"metrics":["switch.if.In","switch.if.Out"],
"tags":"ifName=Fa0/1"
"endpoints":["vpn-lw"],
"metrics":["AnyconnectSession","ConnectionStat"],
"tags":""
},
"switch":{
"enabled": true,
"ipRange":[
"192.168.56.101/32",
"192.168.56.102-192.168.56.120",
"172.16.114.233"
"10.10..88.168"
],
"gosnmp":true,
"pingTimeout":300,
"pingRetry":4,
"community":"public",
"community":"123456",
"snmpTimeout":1000,
"snmpRetry":5,
"ignoreIface": ["Nu","NU","Vlan","Vl"],
Expand All @@ -40,6 +38,14 @@
"limitConcur": 1000,
"limitCon": 4
},
"switchhosts":{
"enabled":true,
"hosts":"./hosts.json"
},
"customMetrics":{
"enabled":true,
"template":"./custom.json"
},
"transfer": {
"enabled": true,
"addr": "127.0.0.1:8433",
Expand Down
17 changes: 17 additions & 0 deletions custom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"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"
}
]
}
125 changes: 125 additions & 0 deletions funcs/custmetric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package funcs

import (
"errors"
"log"

"time"

"github.com/gaochao1/sw"
"github.com/gaochao1/swcollector/g"
"github.com/open-falcon/common/model"
)

type CustM struct {
Ip string
custmMetrics []CustmMetric
}
type CustmMetric struct {
metric string
tag string
value float64
metrictype string
}

func CustMetrics() (L []*model.MetricValue) {
chs := make([]chan CustM, len(AliveIp))
for i, ip := range AliveIp {
if ip != "" {
chs[i] = make(chan CustM)
go custMetrics(ip, chs[i])
}
}
for _, ch := range chs {
custm := <-ch
for _, custmmetric := range custm.custmMetrics {
if custmmetric.metrictype == "GUAGE" {
L = append(L, GaugeValueIp(time.Now().Unix(), custm.Ip, custmmetric.metric, custmmetric.value, custmmetric.tag))
}
if custmmetric.metrictype == "COUNTER" {
L = append(L, CounterValueIp(time.Now().Unix(), custm.Ip, custmmetric.metric, custmmetric.value, custmmetric.tag))
}

}

}

return L
}

func custMetrics(ip string, 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)
}
}
custm.Ip = ip
custm.custmMetrics = custmmetrics
ch <- custm
return
}

func GetCustMetric(ip, community, oid string, timeout, retry int) (float64, error) {
defer func() {
if r := recover(); r != nil {
log.Println(ip+" Recovered in CustomMetric, Oid is ", oid, r)
}
}()
method := "get"
var value float64
var err error
for i := 0; i < retry; i++ {
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
}

func interfaceTofloat64(v interface{}) (float64, error) {
var err error
switch value := v.(type) {
case int:
return float64(value), nil
case int8:
return float64(value), nil
case int16:
return float64(value), nil
case int32:
return float64(value), nil
case int64:
return float64(value), nil
case uint:
return float64(value), nil
case uint8:
return float64(value), nil
case uint16:
return float64(value), nil
case uint32:
return float64(value), nil
case uint64:
return float64(value), nil
case float32:
return float64(value), nil
case float64:
return value, nil
default:
err = errors.New("value is not digital")
return 0, err
}
}
1 change: 1 addition & 0 deletions funcs/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func BuildMappers() {
MemMetrics,
PingMetrics,
ConnMetrics,
CustMetrics,
},
Interval: interval,
},
Expand Down
54 changes: 18 additions & 36 deletions g/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package g
import (
"encoding/json"
"log"
"os"
"sync"

"github.com/toolkits/file"
Expand Down Expand Up @@ -68,15 +67,25 @@ type HttpConfig struct {
Listen string `json:"listen"`
}

type SwitchHostsConfig struct {
Enabled bool `json:enabled`
Hosts string `json:hosts`
}

type CustomMetricsConfig struct {
Enabled bool `json:enbaled`
Template string `json:template`
}

type GlobalConfig struct {
Debug bool `json:"debug"`
Debugmetric *DebugmetricConfig `json:"debugmetric`
IP string `json:"ip"`
Hostname string `json:"hostname"`
Switch *SwitchConfig `json:"switch"`
Heartbeat *HeartbeatConfig `json:"heartbeat"`
Transfer *TransferConfig `json:"transfer"`
Http *HttpConfig `json:"http"`
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`
Http *HttpConfig `json:"http"`
}

var (
Expand All @@ -91,33 +100,6 @@ func Config() *GlobalConfig {
return config
}

func Hostname() (string, error) {
hostname := Config().Hostname
if hostname != "" {
return hostname, nil
}

hostname, err := os.Hostname()
if err != nil {
log.Println("ERROR: os.Hostname() fail", err)
}
return hostname, err
}

func IP() string {
ip := Config().IP
if ip != "" {
// use ip in configuration
return ip
}

if len(LocalIps) > 0 {
ip = LocalIps[0]
}

return ip
}

func ParseConfig(cfg string) {
if cfg == "" {
log.Fatalln("use -c to specify configuration file")
Expand Down
3 changes: 2 additions & 1 deletion g/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
// 4.0.1 fix sometimes ifstat pannic
// 4.0.2 fix speedpercent bug
// 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.4"
VERSION = "4.0.5"
COLLECT_INTERVAL = time.Second
)
55 changes: 55 additions & 0 deletions g/customcfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package g

import (
"encoding/json"
"log"

"sync"

"github.com/toolkits/file"
)

type MetricConfig struct {
Metric string `json:metric`
Tag string `json:tag`
Type string `json:type`
Oid string `json:oid`
}
type CustomConfig struct {
Metrics []*MetricConfig `json:"metrics`
}

var (
CustConfigFile string
custconfig *CustomConfig
custlock = new(sync.RWMutex)
)

func CustConfig() *CustomConfig {
custlock.RLock()
defer custlock.RUnlock()
return custconfig
}

func ParseCustConfig(cfg string) {

if !file.IsExist(cfg) {
log.Fatalln("config file:", cfg, "is not existent")
}
CustConfigFile = cfg
configContent, err := file.ToTrimString(cfg)
if err != nil {
log.Fatalln("read config file:", cfg, "fail:", err)
}
var c CustomConfig
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Fatalln("parse config file:", cfg, "fail:", err)
}
custlock.Lock()
defer custlock.Unlock()
custconfig = &c

log.Println("read customconfig file:", cfg, "successfully")

}
Loading

0 comments on commit 7433fff

Please sign in to comment.