fix: inputs.snmp to respect number of retries configured (#10268)

This commit is contained in:
Thomas Casteleyn 2021-12-14 23:28:38 +01:00 committed by GitHub
parent 32ca79f83c
commit 7c0ffefde9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 34 deletions

View File

@ -22,42 +22,11 @@ func (gs GosnmpWrapper) Host() string {
// Walk wraps GoSNMP.Walk() or GoSNMP.BulkWalk(), depending on whether the
// connection is using SNMPv1 or newer.
// Also, if any error is encountered, it will just once reconnect and try again.
func (gs GosnmpWrapper) Walk(oid string, fn gosnmp.WalkFunc) error {
var err error
// On error, retry once.
// Unfortunately we can't distinguish between an error returned by gosnmp, and one returned by the walk function.
for i := 0; i < 2; i++ {
if gs.Version == gosnmp.Version1 {
err = gs.GoSNMP.Walk(oid, fn)
} else {
err = gs.GoSNMP.BulkWalk(oid, fn)
}
if err == nil {
return nil
}
if err := gs.GoSNMP.Connect(); err != nil {
return fmt.Errorf("reconnecting: %w", err)
}
if gs.Version == gosnmp.Version1 {
return gs.GoSNMP.Walk(oid, fn)
}
return err
}
// Get wraps GoSNMP.GET().
// If any error is encountered, it will just once reconnect and try again.
func (gs GosnmpWrapper) Get(oids []string) (*gosnmp.SnmpPacket, error) {
var err error
var pkt *gosnmp.SnmpPacket
for i := 0; i < 2; i++ {
pkt, err = gs.GoSNMP.Get(oids)
if err == nil {
return pkt, nil
}
if err := gs.GoSNMP.Connect(); err != nil {
return nil, fmt.Errorf("reconnecting: %w", err)
}
}
return nil, err
return gs.GoSNMP.BulkWalk(oid, fn)
}
func NewWrapper(s ClientConfig) (GosnmpWrapper, error) {