Exemplo n.º 1
0
func buildVarBinds(cmdArgs []string) (snmpgo.VariableBindings, error) {
	var VariableBindings snmpgo.VariableBindings

	uptime := snmpgo.NewTimeTicks(getUptime(cmdArgs[1]))
	VariableBindings = append(VariableBindings, snmpgo.NewVarBind(snmpgo.OidSysUpTime, uptime))

	oid, err := snmpgo.ParseOidFromString(cmdArgs[2])
	if err != nil {
		return nil, err
	}
	VariableBindings = append(VariableBindings, snmpgo.NewVarBind(snmpgo.OidSnmpTrap, oid))

	for i := 3; i < len(cmdArgs); i += 3 {
		oid, err := snmpgo.ParseOidFromString(cmdArgs[i])
		if err != nil {
			return nil, err
		}

		val, err := buildVariable(cmdArgs[i+1], cmdArgs[i+2])
		if err != nil {
			return nil, err
		}

		VariableBindings = append(VariableBindings, snmpgo.NewVarBind(oid, val))
	}

	return VariableBindings, nil
}
Exemplo n.º 2
0
func main() {
	// `snmpgo.SNMP.Open` function execute the EngineID Discovery when you use V3.
	// Specify the Agent's EngineID to `snmpgo.Arguments.SecurityEngineId` parameter,
	// if you want to suppress this behavior.
	snmp, err := snmpgo.NewSNMP(snmpgo.Arguments{
		Version:          snmpgo.V3,
		Address:          "127.0.0.1:162",
		Retries:          1,
		UserName:         "******",
		SecurityLevel:    snmpgo.AuthPriv,
		AuthPassword:     "******",
		AuthProtocol:     snmpgo.Sha,
		PrivPassword:     "******",
		PrivProtocol:     snmpgo.Aes,
		SecurityEngineId: "8000000004736e6d70676f",
	})
	if err != nil {
		// Failed to create snmpgo.SNMP object
		fmt.Println(err)
		return
	}

	// Build VariableBinding list
	var VariableBindings snmpgo.VariableBindings
	VariableBindings = append(VariableBindings, snmpgo.NewVarBind(snmpgo.OidSysUpTime, snmpgo.NewTimeTicks(1000)))

	oid, _ := snmpgo.ParseOidFromString("1.3.6.1.6.3.1.1.5.3")
	VariableBindings = append(VariableBindings, snmpgo.NewVarBind(snmpgo.OidSnmpTrap, oid))

	oid, _ = snmpgo.ParseOidFromString("1.3.6.1.2.1.2.2.1.1.2")
	VariableBindings = append(VariableBindings, snmpgo.NewVarBind(oid, snmpgo.NewInteger(2)))

	oid, _ = snmpgo.ParseOidFromString("1.3.6.1.2.1.31.1.1.1.1.2")
	VariableBindings = append(VariableBindings, snmpgo.NewVarBind(oid, snmpgo.NewOctetString([]byte("eth0"))))

	if err = snmp.Open(); err != nil {
		// Failed to open connection
		fmt.Println(err)
		return
	}
	defer snmp.Close()

	if err = snmp.V2Trap(VariableBindings); err != nil {
		// Failed to request
		fmt.Println(err)
		return
	}
}
Exemplo n.º 3
0
func main() {
	snmp, err := snmpgo.NewSNMP(snmpgo.Arguments{
		Version:   snmpgo.V2c,
		Address:   "127.0.0.1:162",
		Retries:   1,
		Community: "public",
	})
	if err != nil {
		// Failed to create snmpgo.SNMP object
		fmt.Println(err)
		return
	}

	// Build VariableBinding list
	var VariableBindings snmpgo.VariableBindings
	VariableBindings = append(VariableBindings, snmpgo.NewVarBind(snmpgo.OidSysUpTime, snmpgo.NewTimeTicks(1000)))

	oid, _ := snmpgo.ParseOidFromString("1.3.6.1.6.3.1.1.5.3")
	VariableBindings = append(VariableBindings, snmpgo.NewVarBind(snmpgo.OidSnmpTrap, oid))

	oid, _ = snmpgo.ParseOidFromString("1.3.6.1.2.1.2.2.1.1.2")
	VariableBindings = append(VariableBindings, snmpgo.NewVarBind(oid, snmpgo.NewInteger(2)))

	oid, _ = snmpgo.ParseOidFromString("1.3.6.1.2.1.31.1.1.1.1.2")
	VariableBindings = append(VariableBindings, snmpgo.NewVarBind(oid, snmpgo.NewOctetString([]byte("eth0"))))

	if err = snmp.Open(); err != nil {
		// Failed to open connection
		fmt.Println(err)
		return
	}
	defer snmp.Close()

	if err = snmp.V2Trap(VariableBindings); err != nil {
		// Failed to request
		fmt.Println(err)
		return
	}
}
Exemplo n.º 4
0
func buildVariable(kind string, value string) (val snmpgo.Variable, err error) {
	switch kind {
	case "i":
		var num int64
		if num, err = strconv.ParseInt(value, 10, 32); err == nil {
			val = snmpgo.NewInteger(int32(num))
		}
	case "u":
		var num uint64
		if num, err = strconv.ParseUint(value, 10, 32); err == nil {
			val = snmpgo.NewGauge32(uint32(num))
		}
	case "c":
		var num uint64
		if num, err = strconv.ParseUint(value, 10, 32); err == nil {
			val = snmpgo.NewCounter32(uint32(num))
		}
	case "C":
		var num uint64
		if num, err = strconv.ParseUint(value, 10, 64); err == nil {
			val = snmpgo.NewCounter64(num)
		}
	case "t":
		var num uint64
		if num, err = strconv.ParseUint(value, 10, 32); err == nil {
			val = snmpgo.NewTimeTicks(uint32(num))
		}
	case "a":
		if ip := net.ParseIP(value); ip != nil && len(ip) == 4 {
			val = snmpgo.NewIpaddress(ip[0], ip[1], ip[2], ip[3])
		} else {
			return nil, fmt.Errorf("%s: no valid IP Address", value)
		}
	case "o":
		val, err = snmpgo.ParseOidFromString(value)
	case "n":
		val = snmpgo.NewNull()
	case "s":
		val = snmpgo.NewOctetString([]byte(value))
	case "x":
		var b []byte
		hx := hexPrefix.ReplaceAllString(value, "")
		if b, err = hex.DecodeString(hx); err == nil {
			val = snmpgo.NewOctetString(b)
		} else {
			return nil, fmt.Errorf("%s: no valid Hex String", value)
		}
	case "d":
		s := strings.Split(value, ".")
		b := make([]byte, len(s))
		for i, piece := range s {
			var num int
			if num, err = strconv.Atoi(piece); err != nil || num > 0xff {
				return nil, fmt.Errorf("%s: no valid Decimal String", value)
			}
			b[i] = byte(num)
		}
		val = snmpgo.NewOctetString(b)
	default:
		return nil, fmt.Errorf("%s: unknown TYPE", kind)
	}

	return
}