Skip to content

Commit

Permalink
added snmp walk cache again
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFireMike committed May 26, 2021
1 parent 4b9c757 commit 0c98235
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions core/network/snmp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (

// SNMPClient is used to communicate via snmp.
type SNMPClient struct {
client *gosnmp.GoSNMP
useCache bool
getCache requestCache
client *gosnmp.GoSNMP
useCache bool
getCache requestCache
walkCache requestCache
}

type snmpClientCreation struct {
Expand Down Expand Up @@ -269,9 +270,10 @@ func newSNMPClientTestConnection(client *gosnmp.GoSNMP) (*SNMPClient, error) {
client.ExponentialTimeout = true

return &SNMPClient{
client: client,
useCache: true,
getCache: newRequestCache(),
client: client,
useCache: true,
getCache: newRequestCache(),
walkCache: newRequestCache(),
}, nil
}

Expand Down Expand Up @@ -413,6 +415,17 @@ func (s *SNMPClient) SNMPGet(ctx context.Context, oid ...string) ([]SNMPResponse

// SNMPWalk sends a snmpwalk request to the specified oid.
func (s *SNMPClient) SNMPWalk(ctx context.Context, oid string) ([]SNMPResponse, error) {
if s.useCache {
x, err := s.walkCache.get(oid)
if err == nil {
res, ok := x.res.([]SNMPResponse)
if !ok {
return nil, errors.New("cached snmp result is not a snmp response")
}
return res, nil
}
}

s.client.Context = ctx

var response []gosnmp.SnmpPDU
Expand All @@ -428,11 +441,17 @@ func (s *SNMPClient) SNMPWalk(ctx context.Context, oid string) ([]SNMPResponse,
}
if err != nil {
err = errors.Wrap(err, "snmpwalk failed")
if s.useCache {
s.walkCache.add(oid, nil, err)
}
return nil, err
}

if response == nil {
err = tholaerr.NewNotFoundError("No Such Object available on this agent at this OID")
if s.useCache {
s.walkCache.add(oid, nil, err)
}
return nil, err
}

Expand All @@ -454,6 +473,10 @@ func (s *SNMPClient) SNMPWalk(ctx context.Context, oid string) ([]SNMPResponse,
}
}

if s.useCache {
s.walkCache.add(oid, res, nil)
}

return res, nil
}

Expand Down

0 comments on commit 0c98235

Please sign in to comment.