示例#1
0
func buildVarBinds(cmdArgs []string) (snmpgo.VarBinds, error) {
	var varBinds snmpgo.VarBinds

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

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

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

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

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

	return varBinds, nil
}
示例#2
0
文件: pdu_test.go 项目: mgenov/snmpgo
func TestScopedPdu(t *testing.T) {
	pdu := snmpgo.NewPdu(snmpgo.V3, snmpgo.GetRequest)
	pdu.SetRequestId(123)
	pdu.SetErrorStatus(snmpgo.TooBig)
	pdu.SetErrorIndex(2)

	sp := pdu.(*snmpgo.ScopedPdu)
	sp.ContextEngineId = []byte{0x80, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
	sp.ContextName = []byte("MyContext")

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

	expBuf := []byte{
		0x30, 0x54, 0x04, 0x08, 0x80, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x04, 0x09, 0x4d, 0x79, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74,
		0xa0, 0x3d, 0x02, 0x01, 0x7b, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02,
		0x30, 0x32, 0x30, 0x12, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01,
		0x01, 0x01, 0x00, 0x04, 0x06, 0x4d, 0x79, 0x48, 0x6f, 0x73, 0x74,
		0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x02,
		0x00, 0x05, 0x00, 0x30, 0x0e, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02,
		0x01, 0x01, 0x03, 0x00, 0x43, 0x02, 0x2b, 0x67,
	}
	buf, err := pdu.Marshal()
	if err != nil {
		t.Fatal("Marshal() : %v", err)
	}
	if !bytes.Equal(expBuf, buf) {
		t.Errorf("Marshal() - expected [%s], actual [%s]",
			snmpgo.ToHexStr(expBuf, " "), snmpgo.ToHexStr(buf, " "))
	}

	expStr := `{"Type": "GetRequest", "RequestId": "123", ` +
		`"ErrorStatus": "TooBig", "ErrorIndex": "2", ` +
		`"ContextEngineId": "8001020304050607", "ContextName": "MyContext", ` +
		`"VarBinds": [` +
		`{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "OctetString", "Value": "MyHost"}}, ` +
		`{"Oid": "1.3.6.1.2.1.1.2.0", "Variable": {"Type": "Null", "Value": ""}}, ` +
		`{"Oid": "1.3.6.1.2.1.1.3.0", "Variable": {"Type": "TimeTicks", "Value": "11111"}}]}`
	var w snmpgo.ScopedPdu
	rest, err := (&w).Unmarshal(buf)
	if len(rest) != 0 || err != nil {
		t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err)
	}
	if expStr != w.String() {
		t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.String())
	}
}
示例#3
0
文件: trapv3.go 项目: mgenov/snmpgo
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
	}
}
示例#4
0
func TestOidOperation(t *testing.T) {
	oid, _ := snmpgo.NewOid("1.2.3.4.5.6.7")

	oids, _ := snmpgo.NewOids([]string{"1.2.3.4", "1.2.3.4.5.6.7",
		"1.2.3.4.5.6.7.8", "1.1.3.4", "1.3.3.4"})

	if !oid.Contains(oids[0]) || !oid.Contains(oids[1]) || oid.Contains(oids[2]) ||
		oid.Contains(oids[3]) || oid.Contains(oids[4]) {
		t.Errorf("Failed to Contains()")
	}

	if oid.Compare(oids[0]) != 1 || oid.Compare(oids[1]) != 0 || oid.Compare(oids[2]) != -1 ||
		oid.Compare(oids[3]) != 1 || oid.Compare(oids[4]) != -1 {
		t.Errorf("Failed to Compare()")
	}

	if oid.Equal(oids[0]) || !oid.Equal(oids[1]) || oid.Equal(oids[2]) ||
		oid.Equal(oids[3]) || oid.Equal(oids[4]) {
		t.Errorf("Failed to Contains()")
	}

	oid, _ = oid.AppendSubIds([]int{8, 9, 10})
	if oid.String() != "1.2.3.4.5.6.7.8.9.10" {
		t.Errorf("Failed to AppendSubIds()")
	}
}
示例#5
0
文件: trapv2.go 项目: mgenov/snmpgo
func main() {
	snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
		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 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
	}
}
示例#6
0
func TestNewOid(t *testing.T) {
	expStr := ".1.3.6.1.2.1.1.1.0"
	var v snmpgo.Variable

	v, err := snmpgo.NewOid(expStr)
	if err != nil {
		t.Errorf("NewOid : %v", err)
	}

	if expStr[1:] != v.String() {
		t.Errorf("String() - expected [%s], actual[%s]", expStr[1:], v.String())
	}

	var s []string
	for i := 0; i <= 128; i++ {
		s = append(s, strconv.Itoa(i))
	}
	expStr = strings.Join(s, ".")
	v, err = snmpgo.NewOid(expStr)
	if err == nil {
		t.Errorf("NewOid sub-identifiers size")
	}

	expStr = "1.3.6.1.2.1.-1.0"
	v, err = snmpgo.NewOid(expStr)
	if err == nil {
		t.Errorf("NewOid sub-identifier range")
	}

	expStr = "1.3.6.1.2.1.4294967296.0"
	v, err = snmpgo.NewOid(expStr)
	if err == nil {
		t.Errorf("NewOid sub-identifier range")
	}

	expStr = "3.3.6.1.2.1.1.1.0"
	v, err = snmpgo.NewOid(expStr)
	if err == nil {
		t.Errorf("NewOid first sub-identifier range")
	}

	expStr = "1"
	v, err = snmpgo.NewOid(expStr)
	if err == nil {
		t.Errorf("NewOid sub-identifiers size")
	}

	expStr = "1.40.6.1.2.1.1.1.0"
	v, err = snmpgo.NewOid(expStr)
	if err == nil {
		t.Errorf("NewOid first sub-identifier range")
	}
}
示例#7
0
func TestOid(t *testing.T) {
	expStr := "1.3.6.1.2.1.1.1.0"
	expBuf := []byte{0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00}
	var v snmpgo.Variable

	v, err := snmpgo.NewOid(expStr)
	if err != nil {
		t.Errorf("NewOid : %v", err)
	}

	_, err = v.BigInt()
	if err == nil {
		t.Errorf("Failed to call BigInt()")
	}

	if expStr != v.String() {
		t.Errorf("String() - expected [%s], actual[%s]", expStr, v.String())
	}

	buf, err := v.Marshal()
	if err != nil {
		t.Errorf("Marshal(): %v", err)
	}
	if !bytes.Equal(expBuf, buf) {
		t.Errorf("Marshal() - expected [%s], actual [%s]",
			snmpgo.ToHexStr(expBuf, " "), snmpgo.ToHexStr(buf, " "))
	}

	var w snmpgo.Oid
	rest, err := (&w).Unmarshal(buf)
	if len(rest) != 0 || err != nil {
		t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err)
	}
	if expStr != w.String() {
		t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.String())
	}

	buf = append(buf, 0x00)
	rest, err = (&w).Unmarshal(buf)
	if len(rest) != 1 || err != nil {
		t.Errorf("Unmarshal() with rest - len[%d] err[%v]", len(rest), err)
	}
	if expStr != w.String() {
		t.Errorf("Unmarshal() with rest - expected [%s], actual [%s]", expStr, w.String())
	}
}
示例#8
0
文件: pdu_test.go 项目: mgenov/snmpgo
func TestVarBind(t *testing.T) {
	var v snmpgo.VarBind
	oid, _ := snmpgo.NewOid("1.3.6.1.2.1.1.1.0")
	v = snmpgo.VarBind{Oid: oid}

	v.Variable = snmpgo.NewInteger(-2147483648)
	testVarBind(t, &v,
		`{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "Integer", "Value": "-2147483648"}}`)

	v.Variable = snmpgo.NewOctetString([]byte("MyHost"))
	testVarBind(t, &v,
		`{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "OctetString", "Value": "MyHost"}}`)

	v.Variable = snmpgo.NewNull()
	testVarBind(t, &v, `{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "Null", "Value": ""}}`)

	v.Variable = snmpgo.NewCounter32(uint32(4294967295))
	testVarBind(t, &v,
		`{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "Counter32", "Value": "4294967295"}}`)

	v.Variable = snmpgo.NewCounter64(uint64(18446744073709551615))
	testVarBind(t, &v, `{"Oid": "1.3.6.1.2.1.1.1.0", `+
		`"Variable": {"Type": "Counter64", "Value": "18446744073709551615"}}`)

	expBuf := []byte{0x30, 0x00}
	v = snmpgo.VarBind{}
	buf, err := v.Marshal()
	if err != nil {
		t.Errorf("Marshal() : %v", err)
	}
	if !bytes.Equal(expBuf, buf) {
		t.Errorf("Marshal() - expected [%s], actual [%s]",
			snmpgo.ToHexStr(expBuf, " "), snmpgo.ToHexStr(buf, " "))
	}

	buf = []byte{0x00, 0x00}
	_, err = (&v).Unmarshal(buf)
	if err == nil {
		t.Errorf("Unmarshal() : can not validation")
	}
}
示例#9
0
文件: pdu_test.go 项目: mgenov/snmpgo
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)
		}
	}
}
示例#10
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.NewOid(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
}