diff --git a/core/network/snmp_client.go b/core/network/snmp_client.go index d5ddea2..98a7f67 100644 --- a/core/network/snmp_client.go +++ b/core/network/snmp_client.go @@ -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 { @@ -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 } @@ -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 @@ -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 } @@ -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 }