Skip to content

Commit

Permalink
improve GetNextColumnOidNaive 2
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreYang committed Nov 24, 2023
1 parent 86f5326 commit e0c3d05
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/collector/corechecks/snmp/internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func FetchAllFirstRowOIDsVariables(session Session) []gosnmp.SnmpPDU {
} else {
nextColumn, err := GetNextColumnOidNaive(oid)
if err != nil {
log.Debugf("Invalid column oid: %s", oid)
log.Debugf("Invalid column oid %s: %s", oid, err)
curRequestOid = oid // fallback on continuing by using the response oid as next oid to request
} else {
curRequestOid = nextColumn
Expand Down
10 changes: 7 additions & 3 deletions pkg/collector/corechecks/snmp/internal/session/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ func GetNextColumnOidNaive(oid string) (string, error) {
tableOid := oid[0:idx]
rowFullIndex := oid[idx+3:] // +3 to skip `.1.`
if !strings.Contains(rowFullIndex, ".") {
// TODO: test me
// entry `.1.` must be followed by a column + index, hence the index is expected to contain at least one `.`
return "", fmt.Errorf("the last `.1.` is not an table entry: %s", oid)
idx = strings.LastIndex(tableOid+".", ".1.") // Try to find the table Entry OID
if idx == -1 {
// not found
return "", fmt.Errorf("the oid is not a column oid: %s", oid)
}
tableOid = oid[0:idx]
rowFullIndex = oid[idx+3:] // +3 to skip `.1.`
}
rowFirstIndex := strings.Split(rowFullIndex, ".")[0]
rowFirstIndexNum, err := strconv.Atoi(rowFirstIndex)
Expand Down
4 changes: 2 additions & 2 deletions pkg/collector/corechecks/snmp/internal/session/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func TestGetNextColumnOid(t *testing.T) {
},
{
oid: "1.3.6.1.2.1.2.2.1.1.1",
expectedErr: "the last `.1.` is not an table entry: 1.3.6.1.2.1.2.2.1.1.1",
expectedOid: "1.3.6.1.2.1.2.2.1.2",
},
{
oid: "1.3.6.2.2.2.2.2.1.9",
expectedErr: "the last `.1.` is not an table entry: 1.3.6.2.2.2.2.2.1.9",
expectedErr: "the oid is not a column oid: 1.3.6.2.2.2.2.2.1.9",
},
}
for _, tt := range tests {
Expand Down

0 comments on commit e0c3d05

Please sign in to comment.