예제 #1
0
func TestOids(t *testing.T) {
	oids, _ := snmpgo.NewOids([]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",
	})

	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",
	})
	oids = oids.Sort()
	if len(expOids) != len(oids) {
		t.Errorf("Sort() - expected [%d], actual [%d]", len(expOids), len(oids))
	}
	for i, o := range expOids {
		if !o.Equal(oids[i]) {
			t.Errorf("Sort() - expected [%s], actual [%s]", o, oids[i])
		}
	}

	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",
	})
	oids = oids.Sort().Uniq()
	if len(expOids) != len(oids) {
		t.Errorf("Uniq() - expected [%d], actual [%d]", len(expOids), len(oids))
	}
	for i, o := range expOids {
		if !o.Equal(oids[i]) {
			t.Errorf("Uniq() - expected [%s], actual [%s]", o, oids[i])
		}
	}

	expOids, _ = snmpgo.NewOids([]string{
		"1.3.6.1.2.1.1",
	})
	oids = oids.Sort().UniqBase()
	if len(expOids) != len(oids) {
		t.Errorf("Uniq() - expected [%d], actual [%d]", len(expOids), len(oids))
	}
	for i, o := range expOids {
		if !o.Equal(oids[i]) {
			t.Errorf("Uniq() - expected [%s], actual [%s]", o, oids[i])
		}
	}
}
예제 #2
0
func main() {
	oids, err := snmpgo.NewOids([]string{
		"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",
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	agents := []string{
		"192.168.1.1:161",
		"192.168.1.2:161",
		"192.168.1.3:161",
	}

	var wg sync.WaitGroup
	for _, agent := range agents {
		wg.Add(1)
		go func(a string) {
			defer wg.Done()
			get(a, oids)
		}(agent)
	}
	wg.Wait()
}
예제 #3
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()")
	}
}
예제 #4
0
파일: getv3.go 프로젝트: mgenov/snmpgo
func main() {
	snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
		Version:       snmpgo.V3,
		Address:       "127.0.0.1:161",
		Retries:       1,
		UserName:      "******",
		SecurityLevel: snmpgo.AuthPriv,
		AuthPassword:  "******",
		AuthProtocol:  snmpgo.Sha,
		PrivPassword:  "******",
		PrivProtocol:  snmpgo.Aes,
	})
	if err != nil {
		// Failed to create snmpgo.SNMP object
		fmt.Println(err)
		return
	}

	oids, err := snmpgo.NewOids([]string{
		"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",
	})
	if err != nil {
		// Failed to parse Oids
		fmt.Println(err)
		return
	}

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

	pdu, err := snmp.GetRequest(oids)
	if err != nil {
		// Failed to request
		fmt.Println(err)
		return
	}
	if pdu.ErrorStatus() != snmpgo.NoError {
		// Received an error from the agent
		fmt.Println(pdu.ErrorStatus(), pdu.ErrorIndex())
	}

	// get VarBind list
	fmt.Println(pdu.VarBinds())

	// select a VarBind
	fmt.Println(pdu.VarBinds().MatchOid(oids[0]))
}
예제 #5
0
func main() {
	snmp, err := snmpgo.NewSNMP(snmpgo.Arguments{
		Version:   snmpgo.V2c,
		Address:   "127.0.0.1:161",
		Retries:   1,
		Community: "public",
	})
	if err != nil {
		// Failed to create snmpgo.SNMP object
		fmt.Println(err)
		return
	}

	oids, err := snmpgo.NewOids([]string{
		"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",
	})
	if err != nil {
		// Failed to parse Oids
		fmt.Println(err)
		return
	}

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

	pdu, err := snmp.GetRequest(oids)
	if err != nil {
		// Failed to request
		fmt.Println(err)
		return
	}
	if pdu.ErrorStatus() != snmpgo.NoError {
		// Received an error from the agent
		fmt.Println(pdu.ErrorStatus(), pdu.ErrorIndex())
	}

	// get VariableBinding list
	fmt.Println(pdu.VariableBindings())

	// select a VariableBinding
	fmt.Println(pdu.VariableBindings().MatchOid(oids[0]))
}
예제 #6
0
func main() {
	snmpArgs, cmdArgs := parseArgs()
	if len(cmdArgs) < 2 {
		usage("required AGENT and OID", 2)
	}

	oids, err := snmpgo.NewOids(cmdArgs[1:])
	if err != nil {
		usage(err.Error(), 2)
	}

	snmp, err := snmpgo.NewSNMP(*snmpArgs)
	if err != nil {
		usage(err.Error(), 2)
	}

	if err = snmp.Open(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer snmp.Close()

	pdu, err := snmp.GetBulkWalk(oids, nonRepeaters, maxRepetitions)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	if pdu.ErrorStatus() != snmpgo.NoError {
		fmt.Fprintln(os.Stderr, pdu.ErrorStatus(), pdu.ErrorIndex())
		os.Exit(1)
	}

	for _, val := range pdu.VariableBindings() {
		fmt.Printf("%s = %s: %s\n", val.Oid, val.Variable.Type(), val.Variable)
	}
}
예제 #7
0
func TestSNMP(t *testing.T) {
	snmp, _ := snmpgo.NewSNMP(snmpgo.SNMPArguments{
		Version:       snmpgo.V3,
		UserName:      "******",
		SecurityLevel: snmpgo.AuthPriv,
		AuthPassword:  "******",
		AuthProtocol:  snmpgo.Md5,
		PrivPassword:  "******",
		PrivProtocol:  snmpgo.Des,
	})

	pdu := snmpgo.NewPdu(snmpgo.V3, snmpgo.Report)
	err := snmpgo.SnmpCheckPdu(snmp, pdu)
	if err != nil {
		t.Errorf("checkPdu() - has error %v", err)
	}

	oids, _ := snmpgo.NewOids([]string{"1.3.6.1.6.3.11.2.1.1.0"})
	pdu = snmpgo.NewPduWithOids(snmpgo.V3, snmpgo.Report, oids)
	err = snmpgo.SnmpCheckPdu(snmp, pdu)
	if err == nil {
		t.Error("checkPdu() - report oid")
	}
}
예제 #8
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)
		}
	}
}