Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreYang committed Nov 22, 2023
1 parent 7d7239d commit d6e22c2
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions pkg/collector/corechecks/snmp/internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,46 @@ func FetchAllFirstRowOIDsUsingGetNext(session Session) []string {
// FetchAllOIDsUsingGetNext fetches all available OIDs
func FetchAllOIDsUsingGetNext(session Session) ([]gosnmp.SnmpPDU, error) {
var results []gosnmp.SnmpPDU
rootOid := "1.0"
curRequestOid := "1.0"
alreadySeenOIDs := make(map[string]bool)

err := session.Walk(rootOid, func(dataUnit gosnmp.SnmpPDU) error {
results = append(results, dataUnit)
return nil
})
if err != nil {
log.Debugf("GetNext error: %s", err)
return nil, err
// TODO: WHY gosnmp Walk does not work?

//err := session.Walk(rootOid, func(dataUnit gosnmp.SnmpPDU) error {
// results = append(results, dataUnit)
// return nil
//})
//if err != nil {
// log.Debugf("GetNext error: %s", err)
// return nil, err
//}

for {
curResults, err := session.GetNext([]string{curRequestOid})
if err != nil {
log.Debugf("GetNext error: %s", err)
break
}
if len(curResults.Variables) != 1 {
log.Debugf("Expect 1 variable, but got %d: %+v", len(curResults.Variables), curResults.Variables)
break
}
variable := curResults.Variables[0]
if variable.Type == gosnmp.EndOfContents || variable.Type == gosnmp.EndOfMibView {
log.Debug("No more OIDs to fetch")
break
}
if alreadySeenOIDs[curRequestOid] {
// breaking on already seen OIDs prevent infinite loop if the device mis behave by responding with non-sequential OIDs when called with GETNEXT
log.Debug("error: received non sequential OIDs")
break
}
alreadySeenOIDs[curRequestOid] = true

oid := strings.TrimLeft(variable.Name, ".")
curRequestOid = oid

results = append(results, variable)
}
return results, nil
}

0 comments on commit d6e22c2

Please sign in to comment.