Skip to content

Commit

Permalink
Add capability to show SNMP data types as strings for logging/debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnarayan committed Aug 22, 2013
1 parent 342c4bc commit 8751f9f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
37 changes: 37 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type Asn1BER byte

// SNMP Data Types
const (
Integer Asn1BER = 0x02
BitString = 0x03
Expand All @@ -34,6 +35,42 @@ const (
GetBulkRequest = 0xa5
)

// String representations of each SNMP Data Type
var dataTypeStrings = map[Asn1BER]string{
Integer: "Integer",
BitString: "BitString",
OctetString: "OctetString",
Null: "Null",
ObjectIdentifier: "ObjectIdentifier",
Sequence: "Sequence",
Counter32: "Counter32",
Gauge32: "Gauge32",
TimeTicks: "TimeTicks",
Opaque: "Opaque",
NsapAddress: "NsapAddress",
Counter64: "Counter64",
Uinteger32: "Uinteger32",
NoSuchObject: "NoSuchObject",
NoSuchInstance: "NoSuchInstance",
GetRequest: "GetRequest",
GetNextRequest: "GetNextRequest",
GetResponse: "GetResponse",
SetRequest: "SetRequest",
Trap: "Trap",
GetBulkRequest: "GetBulkRequest",
}

func (dataType Asn1BER) String() string {
str, ok := dataTypeStrings[dataType]

if !ok {
str = "Unknown"
}

return str
}


type Variable struct {
Name []int
Type Asn1BER
Expand Down
18 changes: 18 additions & 0 deletions gosnmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,21 @@ func TestConnect(t *testing.T) {
}
}

// Test Data Type stringer
func TestDataTypeStrings(t *testing.T) {
var dataType Asn1BER

// Defined data type
dataType = Integer

if dataType.String() != "Integer" {
t.Errorf("Data Type strings:\n\twant: %q\n\tgot : %q", "Integer", dataType)
}

// Undefined data type
dataType = 0x00

if dataType.String() != "Unknown" {
t.Errorf("Data Type strings:\n\twant: %q\n\tgot : %q", "Integer", dataType)
}
}

0 comments on commit 8751f9f

Please sign in to comment.