Skip to content

Commit 2faebb7

Browse files
mykolafqiluo-msft
authored andcommitted
[snmp] Add snmp_facts support for ieee802.1ab MIBs, extend snmp testcase (sonic-net#679)
* [snmp] Add snmp_facts support for ieee802.1ab MIBs, extend snmp testcase * review comments - indentation, etc * also indent multiline debug messages
1 parent ab171a8 commit 2faebb7

3 files changed

Lines changed: 348 additions & 0 deletions

File tree

ansible/library/snmp_facts.py

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,47 @@ def __init__(self,dotprefix=False):
141141
self.ipAdEntIfIndex = dp + "1.3.6.1.2.1.4.20.1.2"
142142
self.ipAdEntNetMask = dp + "1.3.6.1.2.1.4.20.1.3"
143143

144+
# From LLDP-MIB: lldpLocalSystemData
145+
self.lldpLocChassisIdSubtype = dp + "1.0.8802.1.1.2.1.3.1"
146+
self.lldpLocChassisId = dp + "1.0.8802.1.1.2.1.3.2"
147+
self.lldpLocSysName = dp + "1.0.8802.1.1.2.1.3.3"
148+
self.lldpLocSysDesc = dp + "1.0.8802.1.1.2.1.3.4"
149+
150+
# From LLDP-MIB: lldpLocPortTable
151+
self.lldpLocPortNum = dp + "1.0.8802.1.1.2.1.3.7.1.1" # + .ifindex
152+
self.lldpLocPortIdSubtype = dp + "1.0.8802.1.1.2.1.3.7.1.2" # + .ifindex
153+
self.lldpLocPortId = dp + "1.0.8802.1.1.2.1.3.7.1.3" # + .ifindex
154+
self.lldpLocPortDesc = dp + "1.0.8802.1.1.2.1.3.7.1.4" # + .ifindex
155+
156+
# From LLDP-MIB: lldpLocManAddrTables
157+
self.lldpLocManAddrSubtype = dp + "1.0.8802.1.1.2.1.3.8.1.1" # + .subtype + .man addr
158+
self.lldpLocManAddr = dp + "1.0.8802.1.1.2.1.3.8.1.2" # + .subtype + .man addr
159+
self.lldpLocManAddrLen = dp + "1.0.8802.1.1.2.1.3.8.1.3" # + .subtype + .man addr
160+
self.lldpLocManAddrIfSubtype = dp + "1.0.8802.1.1.2.1.3.8.1.4" # + .subtype + .man addr
161+
self.lldpLocManAddrIfId = dp + "1.0.8802.1.1.2.1.3.8.1.5" # + .subtype + .man addr
162+
self.lldpLocManAddrOID = dp + "1.0.8802.1.1.2.1.3.8.1.6" # + .subtype + .man addr
163+
164+
# From LLDP-MIB: lldpRemTable
165+
self.lldpRemTimeMark = dp + "1.0.8802.1.1.2.1.4.1.1.1" # + .time mark + .ifindex + .rem index
166+
self.lldpRemLocalPortNum = dp + "1.0.8802.1.1.2.1.4.1.1.2" # + .time mark + .ifindex + .rem index
167+
self.lldpRemIndex = dp + "1.0.8802.1.1.2.1.4.1.1.3" # + .time mark + .ifindex + .rem index
168+
self.lldpRemChassisIdSubtype = dp + "1.0.8802.1.1.2.1.4.1.1.4" # + .time mark + .ifindex + .rem index
169+
self.lldpRemChassisId = dp + "1.0.8802.1.1.2.1.4.1.1.5" # + .time mark + .ifindex + .rem index
170+
self.lldpRemPortIdSubtype = dp + "1.0.8802.1.1.2.1.4.1.1.6" # + .time mark + .ifindex + .rem index
171+
self.lldpRemPortId = dp + "1.0.8802.1.1.2.1.4.1.1.7" # + .time mark + .ifindex + .rem index
172+
self.lldpRemPortDesc = dp + "1.0.8802.1.1.2.1.4.1.1.8" # + .time mark + .ifindex + .rem index
173+
self.lldpRemSysName = dp + "1.0.8802.1.1.2.1.4.1.1.9" # + .time mark + .ifindex + .rem index
174+
self.lldpRemSysDesc = dp + "1.0.8802.1.1.2.1.4.1.1.10" # + .time mark + .ifindex + .rem index
175+
self.lldpRemSysCapSupported = dp + "1.0.8802.1.1.2.1.4.1.1.11" # + .time mark + .ifindex + .rem index
176+
self.lldpRemSysCapEnabled = dp + "1.0.8802.1.1.2.1.4.1.1.12" # + .time mark + .ifindex + .rem index
177+
178+
# From LLDP-MIB: lldpRemManAddrTable
179+
self.lldpRemManAddrSubtype = dp + "1.0.8802.1.1.2.1.4.2.1.1" # + .time mark + .ifindex + .rem index + .addr_subtype + .man addr
180+
self.lldpRemManAddr = dp + "1.0.8802.1.1.2.1.4.2.1.2" # + .time mark + .ifindex + .rem index + .addr_subtype + .man addr
181+
self.lldpRemManAddrIfSubtype = dp + "1.0.8802.1.1.2.1.4.2.1.3" # + .time mark + .ifindex + .rem index + .addr_subtype + .man addr
182+
self.lldpRemManAddrIfId = dp + "1.0.8802.1.1.2.1.4.2.1.4" # + .time mark + .ifindex + .rem index + .addr_subtype + .man addr
183+
self.lldpRemManAddrOID = dp + "1.0.8802.1.1.2.1.4.2.1.5" # + .time mark + .ifindex + .rem index + .addr_subtype + .man addr
184+
144185
# From Dell Private MIB
145186
self.ChStackUnitCpuUtil5sec = dp + "1.3.6.1.4.1.6027.3.10.1.2.9.1.2.1"
146187

@@ -480,6 +521,195 @@ def main():
480521
if current_oid == v.ChStackUnitCpuUtil5sec:
481522
results['ansible_ChStackUnitCpuUtil5sec'] = decode_type(module, current_oid, val)
482523

524+
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
525+
snmp_auth,
526+
cmdgen.UdpTransportTarget((m_args['host'], 161)),
527+
cmdgen.MibVariable(p.lldpLocChassisIdSubtype,),
528+
cmdgen.MibVariable(p.lldpLocChassisId,),
529+
cmdgen.MibVariable(p.lldpLocSysName,),
530+
cmdgen.MibVariable(p.lldpLocSysDesc,),
531+
)
532+
533+
if errorIndication:
534+
module.fail_json(msg=str(errorIndication) + ' querying lldp local system infomation.')
535+
536+
for oid, val in varBinds:
537+
current_oid = oid.prettyPrint()
538+
current_val = val.prettyPrint()
539+
if current_oid == v.lldpLocChassisIdSubtype:
540+
results['snmp_lldp']['lldpLocChassisIdSubtype'] = current_val
541+
elif current_oid == v.lldpLocChassisId:
542+
results['snmp_lldp']['lldpLocChassisId'] = current_val
543+
elif current_oid == v.lldpLocSysName:
544+
results['snmp_lldp']['lldpLocSysName'] = current_val
545+
elif current_oid == v.lldpLocSysDesc:
546+
results['snmp_lldp']['lldpLocSysDesc'] = current_val
547+
548+
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
549+
snmp_auth,
550+
cmdgen.UdpTransportTarget((m_args['host'], 161)),
551+
cmdgen.MibVariable(p.lldpLocPortNum,),
552+
cmdgen.MibVariable(p.lldpLocPortIdSubtype,),
553+
cmdgen.MibVariable(p.lldpLocPortId,),
554+
cmdgen.MibVariable(p.lldpLocPortDesc,),
555+
)
556+
557+
if errorIndication:
558+
module.fail_json(msg=str(errorIndication) + ' querying lldpLocPortTable counters')
559+
560+
for varBinds in varTable:
561+
for oid, val in varBinds:
562+
current_oid = oid.prettyPrint()
563+
current_val = val.prettyPrint()
564+
if v.lldpLocPortNum in current_oid:
565+
ifIndex = int(current_oid.rsplit('.', 1)[-1])
566+
results['snmp_interfaces'][ifIndex]['lldpLocPortNum'] = current_val
567+
if v.lldpLocPortIdSubtype in current_oid:
568+
ifIndex = int(current_oid.rsplit('.', 1)[-1])
569+
results['snmp_interfaces'][ifIndex]['lldpLocPortIdSubtype'] = current_val
570+
if v.lldpLocPortId in current_oid:
571+
ifIndex = int(current_oid.rsplit('.', 1)[-1])
572+
results['snmp_interfaces'][ifIndex]['lldpLocPortId'] = current_val
573+
if v.lldpLocPortDesc in current_oid:
574+
ifIndex = int(current_oid.rsplit('.', 1)[-1])
575+
results['snmp_interfaces'][ifIndex]['lldpLocPortDesc'] = current_val
576+
577+
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
578+
snmp_auth,
579+
cmdgen.UdpTransportTarget((m_args['host'], 161)),
580+
cmdgen.MibVariable(p.lldpLocManAddrSubtype,),
581+
cmdgen.MibVariable(p.lldpLocManAddr,),
582+
cmdgen.MibVariable(p.lldpLocManAddrLen,),
583+
cmdgen.MibVariable(p.lldpLocManAddrIfSubtype,),
584+
cmdgen.MibVariable(p.lldpLocManAddrIfId,),
585+
cmdgen.MibVariable(p.lldpLocManAddrOID,),
586+
)
587+
588+
if errorIndication:
589+
module.fail_json(msg=str(errorIndication) + ' querying lldpLocPortTable counters')
590+
591+
for varBinds in varTable:
592+
for oid, val in varBinds:
593+
current_oid = oid.prettyPrint()
594+
current_val = val.prettyPrint()
595+
if v.lldpLocManAddrSubtype in current_oid:
596+
address = '.'.join(current_oid.split('.')[13:])
597+
results['snmp_lldp']['lldpLocManAddrSubtype'] = current_val
598+
if v.lldpLocManAddr in current_oid:
599+
address = '.'.join(current_oid.split('.')[13:])
600+
results['snmp_lldp']['lldpLocManAddr'] = current_val
601+
if v.lldpLocManAddrLen in current_oid:
602+
address = '.'.join(current_oid.split('.')[13:])
603+
results['snmp_lldp']['lldpLocManAddrLen'] = current_val
604+
if v.lldpLocManAddrIfSubtype in current_oid:
605+
address = '.'.join(current_oid.split('.')[13:])
606+
results['snmp_lldp']['lldpLocManAddrIfSubtype'] = current_val
607+
if v.lldpLocManAddrIfId in current_oid:
608+
address = '.'.join(current_oid.split('.')[13:])
609+
results['snmp_lldp']['lldpLocManAddrIfId'] = current_val
610+
if v.lldpLocManAddrOID in current_oid:
611+
address = '.'.join(current_oid.split('.')[13:])
612+
results['snmp_lldp']['lldpLocManAddrOID'] = current_val
613+
614+
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
615+
snmp_auth,
616+
cmdgen.UdpTransportTarget((m_args['host'], 161)),
617+
cmdgen.MibVariable(p.lldpRemTimeMark,),
618+
cmdgen.MibVariable(p.lldpRemLocalPortNum,),
619+
cmdgen.MibVariable(p.lldpRemIndex,),
620+
cmdgen.MibVariable(p.lldpRemChassisIdSubtype,),
621+
cmdgen.MibVariable(p.lldpRemChassisId,),
622+
cmdgen.MibVariable(p.lldpRemPortIdSubtype,),
623+
cmdgen.MibVariable(p.lldpRemPortId,),
624+
cmdgen.MibVariable(p.lldpRemPortDesc,),
625+
cmdgen.MibVariable(p.lldpRemSysName,),
626+
cmdgen.MibVariable(p.lldpRemSysDesc,),
627+
cmdgen.MibVariable(p.lldpRemSysCapSupported,),
628+
cmdgen.MibVariable(p.lldpRemSysCapEnabled,),
629+
)
630+
631+
if errorIndication:
632+
module.fail_json(msg=str(errorIndication) + ' querying lldpLocPortTable counters')
633+
634+
for varBinds in varTable:
635+
for oid, val in varBinds:
636+
current_oid = oid.prettyPrint()
637+
current_val = val.prettyPrint()
638+
if v.lldpRemTimeMark in current_oid:
639+
ifIndex = int(current_oid.split('.')[12])
640+
results['snmp_interfaces'][ifIndex]['lldpRemTimeMark'] = current_val
641+
if v.lldpRemLocalPortNum in current_oid:
642+
ifIndex = int(current_oid.split('.')[12])
643+
results['snmp_interfaces'][ifIndex]['lldpRemLocalPortNum'] = current_val
644+
if v.lldpRemIndex in current_oid:
645+
ifIndex = int(current_oid.split('.')[12])
646+
results['snmp_interfaces'][ifIndex]['lldpRemIndex'] = current_val
647+
if v.lldpRemChassisIdSubtype in current_oid:
648+
ifIndex = int(current_oid.split('.')[12])
649+
results['snmp_interfaces'][ifIndex]['lldpRemChassisIdSubtype'] = current_val
650+
if v.lldpRemChassisId in current_oid:
651+
ifIndex = int(current_oid.split('.')[12])
652+
results['snmp_interfaces'][ifIndex]['lldpRemChassisId'] = current_val
653+
if v.lldpRemPortIdSubtype in current_oid:
654+
ifIndex = int(current_oid.split('.')[12])
655+
results['snmp_interfaces'][ifIndex]['lldpRemPortIdSubtype'] = current_val
656+
if v.lldpRemPortId in current_oid:
657+
ifIndex = int(current_oid.split('.')[12])
658+
results['snmp_interfaces'][ifIndex]['lldpRemPortId'] = current_val
659+
if v.lldpRemPortDesc in current_oid:
660+
ifIndex = int(current_oid.split('.')[12])
661+
results['snmp_interfaces'][ifIndex]['lldpRemPortDesc'] = current_val
662+
if v.lldpRemSysName in current_oid:
663+
ifIndex = int(current_oid.split('.')[12])
664+
results['snmp_interfaces'][ifIndex]['lldpRemSysName'] = current_val
665+
if v.lldpRemSysDesc in current_oid:
666+
ifIndex = int(current_oid.split('.')[12])
667+
results['snmp_interfaces'][ifIndex]['lldpRemSysDesc'] = current_val
668+
if v.lldpRemSysCapSupported in current_oid:
669+
ifIndex = int(current_oid.split('.')[12])
670+
results['snmp_interfaces'][ifIndex]['lldpRemSysCapSupported'] = current_val
671+
if v.lldpRemSysCapEnabled in current_oid:
672+
ifIndex = int(current_oid.split('.')[12])
673+
results['snmp_interfaces'][ifIndex]['lldpRemSysCapEnabled'] = current_val
674+
675+
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
676+
snmp_auth,
677+
cmdgen.UdpTransportTarget((m_args['host'], 161)),
678+
cmdgen.MibVariable(p.lldpRemManAddrSubtype,),
679+
cmdgen.MibVariable(p.lldpRemManAddr,),
680+
cmdgen.MibVariable(p.lldpRemManAddrIfSubtype,),
681+
cmdgen.MibVariable(p.lldpRemManAddrIfId,),
682+
cmdgen.MibVariable(p.lldpRemManAddrOID,),
683+
)
684+
685+
if errorIndication:
686+
module.fail_json(msg=str(errorIndication) + ' querying lldpLocPortTable counters')
687+
688+
for varBinds in varTable:
689+
for oid, val in varBinds:
690+
current_oid = oid.prettyPrint()
691+
current_val = val.prettyPrint()
692+
if v.lldpRemManAddrSubtype in current_oid:
693+
ifIndex = int(current_oid.split('.')[12])
694+
address = '.'.join(current_oid.split('.')[16:])
695+
results['snmp_interfaces'][ifIndex]['lldpRemManAddrSubtype'] = current_val
696+
if v.lldpRemManAddr in current_oid:
697+
ifIndex = int(current_oid.split('.')[12])
698+
address = '.'.join(current_oid.split('.')[16:])
699+
results['snmp_interfaces'][ifIndex]['lldpRemManAddr'] = current_val
700+
if v.lldpRemManAddrIfSubtype in current_oid:
701+
ifIndex = int(current_oid.split('.')[12])
702+
address = '.'.join(current_oid.split('.')[16:])
703+
results['snmp_interfaces'][ifIndex]['lldpRemManAddrIfSubtype'] = current_val
704+
if v.lldpRemManAddrIfId in current_oid:
705+
ifIndex = int(current_oid.split('.')[12])
706+
address = '.'.join(current_oid.split('.')[16:])
707+
results['snmp_interfaces'][ifIndex]['lldpRemManAddrIfId'] = current_val
708+
if v.lldpRemManAddrOID in current_oid:
709+
ifIndex = int(current_oid.split('.')[12])
710+
address = '.'.join(current_oid.split('.')[16:])
711+
results['snmp_interfaces'][ifIndex]['lldpRemManAddrOID'] = current_val
712+
483713
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
484714
snmp_auth,
485715
cmdgen.UdpTransportTarget((m_args['host'], 161)),

ansible/roles/test/tasks/snmp.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@
2222

2323
- name: include snmp PSU test
2424
include: roles/test/tasks/snmp/psu.yml
25+
26+
- name: include snmp lldp test
27+
include: roles/test/tasks/snmp/lldp.yml
2528
when: testcase_name is defined
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Test checks for ieee802_1ab MIBs:
2+
# - lldpLocalSystemData 1.0.8802.1.1.2.1.3
3+
# - lldpLocPortTable 1.0.8802.1.1.2.1.3.7
4+
# - lldpLocManAddrTable 1.0.8802.1.1.2.1.3.8
5+
#
6+
# - lldpRemTable 1.0.8802.1.1.2.1.4.1
7+
# - lldpRemManAddrTable 1.0.8802.1.1.2.1.4.2
8+
#
9+
# For local data check if every OID has value
10+
# For remote values check for availability for
11+
# at least 80% of minigraph neighbors
12+
# (similar to lldp test)
13+
14+
15+
# Gather facts with SNMP version 2
16+
- name: Gathering basic snmp facts about the device
17+
snmp_facts: host={{ ansible_host }} version=v2c community={{ snmp_rocommunity }}
18+
connection: local
19+
20+
- name: Print SNMP LLDP information
21+
debug: msg="{{ snmp_lldp }}"
22+
23+
# Check if lldpLocalSysData is present
24+
25+
- name: "Verify {{ item }} is defined"
26+
assert: { that: "{{ snmp_lldp[item] is defined }}
27+
and not {{ snmp_lldp[item] | search('No Such Object currently exists') }}" }
28+
with_items:
29+
- lldpLocChassisIdSubtype
30+
- lldpLocChassisId
31+
- lldpLocSysName
32+
- lldpLocSysDesc
33+
34+
- set_fact:
35+
snmp_ports: "{{ snmp_ports|default({}) | combine({ item.key : item.value}) }}"
36+
when: "{{ item.value.name is defined }}
37+
and {{ item.value['name'].find('Ethernet') != -1 }}"
38+
with_dict: "{{ snmp_interfaces }}"
39+
40+
# Check if lldpLocPortTable is present for all ports
41+
- name: "Verify lldpLocPortTable is present"
42+
assert: { that: "{{ item.value['lldpLocPortNum'] is defined }}
43+
and {{ item.value['lldpLocPortIdSubtype'] is defined }}
44+
and {{ item.value['lldpLocPortId'] is defined }}
45+
and {{ item.value['lldpLocPortDesc'] is defined }}" }
46+
with_dict: "{{ snmp_ports }}"
47+
48+
# Check if lldpLocManAddrTable is present
49+
- name: "Verify {{ item }} is defined"
50+
assert: { that: "{{ snmp_lldp[item] is defined }}
51+
and not {{ snmp_lldp[item] | search('No Such Object currently exists') }}" }
52+
with_items:
53+
- lldpLocManAddrSubtype
54+
- lldpLocManAddr
55+
- lldpLocManAddrLen
56+
- lldpLocManAddrIfSubtype
57+
- lldpLocManAddrIfId
58+
- lldpLocManAddrOID
59+
60+
# Check if lldpRemTable is present
61+
- set_fact:
62+
active_intf: []
63+
64+
- name: find minigraph lldp neighbor
65+
set_fact:
66+
minigraph_lldp_nei: "{{ minigraph_lldp_nei|default({}) | combine({ item.key : item.value}) }}"
67+
when: "'server' not in item.value['name'] | lower"
68+
with_dict: minigraph_neighbors
69+
70+
- name: Create list of ports with lldpRemTable data
71+
when: "{{ item.value['lldpRemTimeMark'] is defined }}
72+
and {{ item.value['lldpRemLocalPortNum'] is defined }}
73+
and {{ item.value['lldpRemIndex'] is defined }}
74+
and {{ item.value['lldpRemChassisIdSubtype'] is defined }}
75+
and {{ item.value['lldpRemChassisId'] is defined }}
76+
and {{ item.value['lldpRemPortIdSubtype'] is defined }}
77+
and {{ item.value['lldpRemPortId'] is defined }}
78+
and {{ item.value['lldpRemPortDesc'] is defined }}
79+
and {{ item.value['lldpRemSysName'] is defined }}
80+
and {{ item.value['lldpRemSysDesc'] is defined }}
81+
and {{ item.value['lldpRemSysCapSupported'] is defined }}
82+
and {{ item.value['lldpRemSysCapEnabled'] is defined }}"
83+
set_fact:
84+
active_intf: "{{ active_intf + [item] }}"
85+
with_dict: "{{ snmp_interfaces }}"
86+
87+
- debug:
88+
msg: "Found {{ active_intf | length }} Ifs with lldpRemTable data\n
89+
Minigraph contains {{ minigraph_lldp_nei | length }} neighbors"
90+
91+
- name: Verify lldpRemTable is available on most interfaces
92+
assert: {that: "{{ minigraph_lldp_nei | length }} > {{ active_intf | length * 0.8 }}" }
93+
94+
95+
# Check if lldpRemManAddrTable is present
96+
- set_fact:
97+
active_intf: []
98+
99+
- name: Create list of ports with lldpRemManAddr data
100+
when: "{{ item.value['lldpRemManAddrSubtype'] is defined }}
101+
and {{ item.value['lldpRemManAddr'] is defined }}
102+
and {{ item.value['lldpRemManAddr'] is defined }}
103+
and {{ item.value['lldpRemManAddrIfSubtype'] is defined }}
104+
and {{ item.value['lldpRemManAddrIfId'] is defined }}
105+
and {{ item.value['lldpRemManAddrOID'] is defined }}"
106+
set_fact:
107+
active_intf: "{{ active_intf + [item] }}"
108+
with_dict: "{{ snmp_interfaces }}"
109+
110+
- debug:
111+
msg: "Found {{ active_intf | length }} Ifs with lldpRemManAddr data\n
112+
Minigraph contains {{ minigraph_lldp_nei | length }} neighbors"
113+
114+
- name: Verify lldpRemManAddr is available on most interfaces
115+
assert: {that: "{{ minigraph_lldp_nei | length }} > {{ active_intf | length * 0.8 }}" }

0 commit comments

Comments
 (0)