Skip to content

Commit d7a843f

Browse files
snmplldp: check if the SNMP object exists
1 parent 7d13c9d commit d7a843f

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

topology/probes/snmplldp/snmp.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const (
3232
remoteChassisID
3333
)
3434

35+
const noObjMsg = "A required object (or instance) doesn't exist."
36+
3537
func getLLDPInfo(host string, port uint16, community string) (*lldpInfo, error) {
3638
snmpClient := &snmp.GoSNMP{
3739
Version: snmp.Version2c,
@@ -64,13 +66,19 @@ func getLLDPInfo(host string, port uint16, community string) (*lldpInfo, error)
6466
return info, err
6567
}
6668

69+
e := result.Variables[0]
70+
if e.Type == snmp.NoSuchObject || e.Type == snmp.NoSuchInstance {
71+
err = fmt.Errorf("%v OID: %v", noObjMsg, oid)
72+
return info, err
73+
}
74+
6775
switch object {
6876
case localName:
69-
info.name = string(result.Variables[0].Value.([]byte))
77+
info.name = string(e.Value.([]byte))
7078
case localDesc:
71-
info.desc = string(result.Variables[0].Value.([]byte))
79+
info.desc = string(e.Value.([]byte))
7280
case localChassisID:
73-
hexrep := fmt.Sprintf("% x", result.Variables[0].Value)
81+
hexrep := fmt.Sprintf("% x", e.Value)
7482
info.chassisID = strings.ReplaceAll(hexrep, " ", ":")
7583
}
7684

@@ -100,6 +108,11 @@ func getRemoteTable(client *snmp.GoSNMP) (map[string]*lldpRemote, error) {
100108

101109
// Retrieve the rows (instances) of the column (object)
102110
for _, e := range results {
111+
if e.Type == snmp.NoSuchObject || e.Type == snmp.NoSuchInstance {
112+
err = fmt.Errorf("%v OID: %v", noObjMsg, oid)
113+
return table, err
114+
}
115+
103116
row := strings.ReplaceAll(e.Name, oid, "")
104117
if table[row] == nil {
105118
table[row] = &lldpRemote{}

topology/probes/snmplldp/snmplldp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (p *Probe) buildGraph() {
2828
for _, dev := range p.devices {
2929
lldp, err := getLLDPInfo(dev.host, dev.port, dev.community)
3030
if err != nil {
31-
p.log.Errorf("Obtaining LLDP info: %v", err)
31+
p.log.Errorf("Obtaining LLDP info from %+v: %v", dev, err)
3232
continue
3333
}
3434

0 commit comments

Comments
 (0)