当前位置: 首页>>代码示例>>Golang>>正文


Golang GoSNMP.WalkAll方法代码示例

本文整理汇总了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
}
开发者ID:ozym,项目名称:dmc,代码行数:45,代码来源:snmp.go

示例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
}
开发者ID:RichiH,项目名称:snmp_exporter,代码行数:43,代码来源:collector.go

示例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))
		}
	}
}
开发者ID:wathiede,项目名称:snmp_exporter,代码行数:22,代码来源:main.go


注:本文中的github.com/soniah/gosnmp.GoSNMP.WalkAll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。