本文整理汇总了Golang中github.com/soniah/gosnmp.GoSNMP.Connect方法的典型用法代码示例。如果您正苦于以下问题:Golang GoSNMP.Connect方法的具体用法?Golang GoSNMP.Connect怎么用?Golang GoSNMP.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/soniah/gosnmp.GoSNMP
的用法示例。
在下文中一共展示了GoSNMP.Connect方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ScrapeTarget
func ScrapeTarget(target string, config *Module) ([]gosnmp.SnmpPDU, error) {
// Set the options.
snmp := gosnmp.GoSNMP{}
snmp.Retries = 3
snmp.MaxRepetitions = 25
snmp.Timeout = time.Second * 60
snmp.Target = target
snmp.Port = 161
if host, port, err := net.SplitHostPort(target); err == nil {
snmp.Target = host
p, err := strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("Error converting port number to int for target %s: %s", target, err)
}
snmp.Port = uint16(p)
}
// Configure auth.
config.configureSNMP(&snmp)
// Do the actual walk.
err := snmp.Connect()
if err != nil {
return nil, fmt.Errorf("Error connecting to target %s: %s", target, err)
}
defer snmp.Conn.Close()
result := []gosnmp.SnmpPDU{}
for _, subtree := range config.Walk {
var pdus []gosnmp.SnmpPDU
if snmp.Version == gosnmp.Version1 {
pdus, err = snmp.WalkAll(subtree)
} else {
pdus, err = snmp.BulkWalkAll(subtree)
}
if err != nil {
return nil, fmt.Errorf("Error walking target %s: %s", snmp.Target, err)
}
result = append(result, pdus...)
}
return result, nil
}
示例2: send
// SNMP Configuration
snmp := g.GoSNMP{
Target: diskStation,
Port: 161,
Community: "public",
Version: g.Version2c,
Timeout: time.Duration(2) * time.Second,
}
// Plugins
plugins := []p.Plugin{p.DiskPlugin{}, p.LoadPlugin{}, p.CPUPlugin{}} //, p.MemoryPlugin{}, p.NetworkPlugin{}}
// Execute plugins every interval
for now := range time.Tick(interval) {
snmp.Connect()
defer snmp.Conn.Close()
for _, plugin := range plugins {
data := plugin.FetchData(snmp)
for key, value := range data {
metric := fmt.Sprintf("%s.%s %v %d\n\r", prefix, key, value, now.Unix())
send(metric, carbonAddr)
}
}
}
}
func send(metric string, carbonAddr string) {
conn, err := net.Dial("tcp", carbonAddr)