forked from gaochao1/swcollector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcc71d7
commit 7433fff
Showing
12 changed files
with
315 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ func BuildMappers() { | |
MemMetrics, | ||
PingMetrics, | ||
ConnMetrics, | ||
CustMetrics, | ||
}, | ||
Interval: interval, | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
} |
Oops, something went wrong.