本文整理汇总了Golang中github.com/soniah/gosnmp.GoSNMP.WalkAll方法的典型用法代码示例。如果您正苦于以下问题:Golang GoSNMP.WalkAll方法的具体用法?Golang GoSNMP.WalkAll怎么用?Golang GoSNMP.WalkAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/soniah/gosnmp.GoSNMP
的用法示例。
在下文中一共展示了GoSNMP.WalkAll方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sysInterfaces
func sysInterfaces(snmp *gosnmp.GoSNMP) (map[int]sysInterface, error) {
w, err := snmp.WalkAll(".1.3.6.1.2.1.2.2.1")
if err != nil {
return nil, err
}
ids := make(map[int]sysInterface)
for _, v := range w {
switch v.Type {
case gosnmp.OctetString:
switch {
case strings.HasPrefix(v.Name, ".1.3.6.1.2.1.2.2.1.2"):
ids[index(v.Name)] = sysInterface{Name: string(v.Value.([]byte))}
}
}
}
for _, v := range w {
i, ok := ids[index(v.Name)]
if !ok {
continue
}
switch v.Type {
case gosnmp.Integer:
switch {
case strings.HasPrefix(v.Name, ".1.3.6.1.2.1.2.2.1.3"):
i.Type = v.Value.(int)
}
case gosnmp.Counter32:
switch {
case strings.HasPrefix(v.Name, ".1.3.6.1.2.1.2.2.1.10"):
if n := (uint32)(v.Value.(uint)); true {
i.RX = &n
}
case strings.HasPrefix(v.Name, ".1.3.6.1.2.1.2.2.1.16"):
if n := (uint32)(v.Value.(uint)); true {
i.TX = &n
}
}
}
ids[index(v.Name)] = i
}
return ids, nil
}
示例2: 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
}
示例3: getTable
func getTable(g *gosnmp.GoSNMP, oid string) {
results, err := g.WalkAll(oid)
if err != nil {
log.Fatalf("Get() err: %v", err)
}
for _, variable := range results {
fmt.Printf("oid: %s ", variable.Name[len(oid)+1:])
// the Value of each variable returned by Get() implements
// interface{}. You could do a type switch...
switch variable.Type {
case gosnmp.OctetString:
fmt.Printf("string: %s\n", string(variable.Value.([]byte)))
default:
// ... or often you're just interested in numeric values.
// ToBigInt() will return the Value as a BigInt, for plugging
// into your calculations.
fmt.Printf("number: %d\n", gosnmp.ToBigInt(variable.Value))
}
}
}