示例#1
0
文件: snmp.go 项目: ozym/dmc
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
0
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
0
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))
		}
	}
}