Skip to content

Commit

Permalink
Add DateAndTime display hint
Browse files Browse the repository at this point in the history
Also improve peformance:
 - unsafe string conversion to remove allocation
 - hex formatting is done manually instead by fmt.Sprintf
  • Loading branch information
grongor committed Oct 15, 2021
1 parent 87f3dc4 commit f93de04
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 118 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ linters:
- wsl

linters-settings:
exhaustive:
default-signifies-exhaustive: true

goimports:
local-prefixes: gitlab.cdn77.eu

Expand Down
3 changes: 1 addition & 2 deletions cmd/snmp-proxy/snmp-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ func main() {
config.Logger.Fatalw("mib parser error: ", zap.Error(err))
}

mibDataProvider := mib.NewDataProvider(displayHints)
requester := snmpproxy.NewGosnmpRequester(mibDataProvider)
requester := snmpproxy.NewGosnmpRequester(snmpproxy.NewValueFormatter(mib.NewDataProvider(displayHints)))

apiListener := snmpproxy.NewApiListener(
validator,
Expand Down
1 change: 1 addition & 0 deletions snmpproxy/mib/mib.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
DisplayHintUnknown = DisplayHint(iota)
DisplayHintString
DisplayHintHexadecimal
DisplayHintDateAndTime
)

type DisplayHints map[string]DisplayHint
Expand Down
2 changes: 2 additions & 0 deletions snmpproxy/mib/netsnmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func (p *NetsnmpMibParser) findStringTypesDisplayHints(displayHints DisplayHints
displayHints[oid] = DisplayHintString
case "PhysAddress":
displayHints[oid] = DisplayHintHexadecimal
case "DateAndTime":
displayHints[oid] = DisplayHintDateAndTime
}

return
Expand Down
45 changes: 5 additions & 40 deletions snmpproxy/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
"fmt"
"strconv"
"strings"
"unicode"
"unicode/utf8"

"github.com/gosnmp/gosnmp"
"github.com/grongor/go-snmp-proxy/snmpproxy/mib"
)

type requestResult struct {
Expand All @@ -23,7 +20,7 @@ type Requester interface {
}

type GosnmpRequester struct {
mibDataProvider *mib.DataProvider
valueFormatter *ValueFormatter
}

func (r *GosnmpRequester) ExecuteRequest(apiRequest *ApiRequest) ([][]interface{}, error) {
Expand Down Expand Up @@ -141,7 +138,7 @@ func (r *GosnmpRequester) processGetPacket(packet *gosnmp.SnmpPacket, request Re
return result, fmt.Errorf("end of mib: %s", dataUnit.Name)
}

result = append(result, dataUnit.Name, r.getPduValue(dataUnit))
result = append(result, dataUnit.Name, r.valueFormatter.Format(dataUnit))
}

return result, nil
Expand Down Expand Up @@ -178,7 +175,7 @@ func (r *GosnmpRequester) executeWalk(apiRequest *ApiRequest, requestNo int, res
oid := request.Oids[0]

err = walker(oid, func(dataUnit gosnmp.SnmpPDU) error {
result.result = append(result.result, dataUnit.Name, r.getPduValue(dataUnit))
result.result = append(result.result, dataUnit.Name, r.valueFormatter.Format(dataUnit))

return nil
})
Expand Down Expand Up @@ -250,38 +247,6 @@ func (*GosnmpRequester) createSnmpHandler(apiRequest *ApiRequest) (gosnmp.Handle
return snmp, nil
}

func (r *GosnmpRequester) getPduValue(dataUnit gosnmp.SnmpPDU) interface{} {
switch dataUnit.Type { //nolint:exhaustive // We only care about gosnmp.OctetString, the rest is just passed along
case gosnmp.OctetString:
displayHint := r.mibDataProvider.GetDisplayHint(dataUnit.Name)
if displayHint == mib.DisplayHintString ||
// best effort to display octet strings correctly without the MIBs
displayHint == mib.DisplayHintUnknown && r.isStringPrintable(dataUnit.Value.([]byte)) {
return string(dataUnit.Value.([]byte))
}

return fmt.Sprintf("% X", dataUnit.Value)
default:
return dataUnit.Value
}
}

func (*GosnmpRequester) isStringPrintable(value []byte) bool {
if !utf8.Valid(value) {
return false
}

for _, b := range value {
if unicode.IsPrint(rune(b)) || unicode.IsSpace(rune(b)) {
continue
}

return false
}

return true
}

func NewGosnmpRequester(mibDataProvider *mib.DataProvider) *GosnmpRequester {
return &GosnmpRequester{mibDataProvider: mibDataProvider}
func NewGosnmpRequester(valueFormatter *ValueFormatter) *GosnmpRequester {
return &GosnmpRequester{valueFormatter: valueFormatter}
}
Loading

0 comments on commit f93de04

Please sign in to comment.