Esempio 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
}
Esempio n. 2
0
func main() {
	// `snmpgo.SNMP.Open` function execute the EngineID Discovery when you use V3.
	// Specify the Agent's EngineID to `snmpgo.SNMPArguments.SecurityEngineId` parameter,
	// if you want to suppress this behavior.
	snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
		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 VarBind list
	var varBinds snmpgo.VarBinds
	varBinds = append(varBinds, snmpgo.NewVarBind(snmpgo.OidSysUpTime, snmpgo.NewTimeTicks(1000)))

	oid, _ := snmpgo.NewOid("1.3.6.1.6.3.1.1.5.3")
	varBinds = append(varBinds, snmpgo.NewVarBind(snmpgo.OidSnmpTrap, oid))

	oid, _ = snmpgo.NewOid("1.3.6.1.2.1.2.2.1.1.2")
	varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewInteger(2)))

	oid, _ = snmpgo.NewOid("1.3.6.1.2.1.31.1.1.1.1.2")
	varBinds = append(varBinds, 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(varBinds); err != nil {
		// Failed to request
		fmt.Println(err)
		return
	}
}
Esempio 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
	}
}
Esempio n. 4
0
func TestVarBinds(t *testing.T) {
	var v snmpgo.VarBinds

	oid, _ := snmpgo.NewOid("1.3.6.1.2.1.1.1.0")
	v = append(v, snmpgo.NewVarBind(oid, snmpgo.NewOctetString([]byte("MyHost"))))
	oid, _ = snmpgo.NewOid("1.3.6.1.2.1.1.2.0")
	v = append(v, snmpgo.NewVarBind(oid, snmpgo.NewNull()))
	oid, _ = snmpgo.NewOid("1.3.6.1.2.1.1.3.0")
	v = append(v, snmpgo.NewVarBind(oid, snmpgo.NewTimeTicks(uint32(11111))))

	oid, _ = snmpgo.NewOid("1.3.6.1.2.1.1.1.0")
	varBind := v.MatchOid(oid)
	if varBind == nil || !varBind.Oid.Equal(oid) {
		t.Errorf("Failed to MatchOid()")
	}
	oid, _ = snmpgo.NewOid("1.3.6.1.2.1.1.1.1")
	varBind = v.MatchOid(oid)
	if varBind != nil {
		t.Errorf("Failed to MatchOid() - no match")
	}
	varBind = v.MatchOid(nil)
	if varBind != nil {
		t.Errorf("Failed to MatchOid() - nil")
	}

	oid, _ = snmpgo.NewOid("1.3.6.1.2.1.1")
	varBinds := v.MatchBaseOids(oid)
	if len(varBinds) != 3 {
		t.Errorf("Failed to MatchBaseOids()")
	}
	oid, _ = snmpgo.NewOid("1.3.6.1.2.1.1.1.0")
	varBinds = v.MatchBaseOids(oid)
	if len(varBinds) != 1 || !varBinds[0].Oid.Equal(oid) {
		t.Errorf("Failed to MatchBaseOids() - one")
	}
	oid, _ = snmpgo.NewOid("1.3.6.1.2.1.1.1.1")
	varBinds = v.MatchBaseOids(oid)
	if len(varBinds) != 0 {
		t.Errorf("Failed to MatchBaseOids() - no match")
	}
	varBinds = v.MatchBaseOids(nil)
	if len(varBinds) != 0 {
		t.Errorf("Failed to MatchBaseOids() - nil")
	}

	var w snmpgo.VarBinds
	for _, o := range []string{
		"1.3.6.1.2.1.1.2.0",
		"1.3.6.1.2.1.1.1.0",
		"1.3.6.1.2.1.1.3.0",
		"1.3.6.1.2.1.1",
		"1.3.6.1.2.1.1.1.0",
	} {
		oid, _ = snmpgo.NewOid(o)
		w = append(w, snmpgo.NewVarBind(oid, snmpgo.NewNull()))
	}

	expOids, _ := snmpgo.NewOids([]string{
		"1.3.6.1.2.1.1",
		"1.3.6.1.2.1.1.1.0",
		"1.3.6.1.2.1.1.1.0",
		"1.3.6.1.2.1.1.2.0",
		"1.3.6.1.2.1.1.3.0",
	})
	w = w.Sort()
	if len(expOids) != len(w) {
		t.Errorf("Sort() - expected [%d], actual [%d]", len(expOids), len(w))
	}
	for i, o := range expOids {
		if !o.Equal(w[i].Oid) {
			t.Errorf("Sort() - expected [%s], actual [%s]", o, w[i].Oid)
		}
	}

	expOids, _ = snmpgo.NewOids([]string{
		"1.3.6.1.2.1.1",
		"1.3.6.1.2.1.1.1.0",
		"1.3.6.1.2.1.1.2.0",
		"1.3.6.1.2.1.1.3.0",
	})
	w = w.Sort().Uniq()
	if len(expOids) != len(w) {
		t.Errorf("Uniq() - expected [%d], actual [%d]", len(expOids), len(w))
	}
	for i, o := range expOids {
		if !o.Equal(w[i].Oid) {
			t.Errorf("Uniq() - expected [%s], actual [%s]", o, w[i].Oid)
		}
	}
}