|
3 | 3 | import json |
4 | 4 | from collections import defaultdict |
5 | 5 | from ansible.module_utils.basic import AnsibleModule |
6 | | -try: |
| 6 | + |
| 7 | +import asyncio |
| 8 | +import pysnmp |
| 9 | + |
| 10 | +if pysnmp.version[0] < 5: |
7 | 11 | from pysnmp.entity.rfc3413.oneliner import cmdgen |
8 | | - has_pysnmp = True |
9 | | -except Exception: |
10 | | - has_pysnmp = False |
| 12 | +else: |
| 13 | + from pysnmp.hlapi.v3arch.asyncio import ( |
| 14 | + cmdgen, |
| 15 | + UdpTransportTarget, |
| 16 | + walk_cmd, |
| 17 | + SnmpEngine, |
| 18 | + ContextData, |
| 19 | + ObjectType, |
| 20 | + ObjectIdentity |
| 21 | + ) |
11 | 22 |
|
12 | 23 | DOCUMENTATION = ''' |
13 | 24 | --- |
@@ -90,11 +101,12 @@ def __init__(self, dotprefix=False): |
90 | 101 | else: |
91 | 102 | dp = "" |
92 | 103 |
|
93 | | - # From IF-MIB |
| 104 | + # From IF-MIB, refer to https://mibs.observium.org/mib/IF-MIB/ |
94 | 105 | # ifdescr is common support, replace the lldpportid |
95 | 106 | self.if_descr = dp + ".3.6.1.2.1.2.2.1.2" |
96 | 107 |
|
97 | 108 | # From LLDP-MIB |
| 109 | + self.lldp_rem_entry = dp + ".0.8802.1.1.2.1.4.1.1" # for snmp_walk |
98 | 110 | self.lldp_rem_port_id = dp + ".0.8802.1.1.2.1.4.1.1.7" |
99 | 111 | self.lldp_rem_port_desc = dp + ".0.8802.1.1.2.1.4.1.1.8" |
100 | 112 | self.lldp_rem_sys_desc = dp + ".0.8802.1.1.2.1.4.1.1.10" |
@@ -129,27 +141,12 @@ def get_iftable(snmp_data): |
129 | 141 | return (if_table, inverse_if_table) |
130 | 142 |
|
131 | 143 |
|
132 | | -def main(): |
133 | | - module = AnsibleModule( |
134 | | - argument_spec=dict( |
135 | | - host=dict(required=True), |
136 | | - version=dict(required=True, choices=['v2', 'v2c', 'v3']), |
137 | | - community=dict(required=False, default=False), |
138 | | - username=dict(required=False), |
139 | | - level=dict(required=False, choices=['authNoPriv', 'authPriv']), |
140 | | - integrity=dict(required=False, choices=['md5', 'sha']), |
141 | | - privacy=dict(required=False, choices=['des', 'aes']), |
142 | | - authkey=dict(required=False), |
143 | | - privkey=dict(required=False), |
144 | | - removeplaceholder=dict(required=False)), |
145 | | - required_together=(['username', 'level', 'integrity', 'authkey'], [ |
146 | | - 'privacy', 'privkey'],), |
147 | | - supports_check_mode=False) |
| 144 | +def Tree(): |
| 145 | + return defaultdict(Tree) |
148 | 146 |
|
149 | | - m_args = module.params |
150 | 147 |
|
151 | | - if not has_pysnmp: |
152 | | - module.fail_json(msg='Missing required pysnmp module (check docs)') |
| 148 | +def main_legacy(module): |
| 149 | + m_args = module.params |
153 | 150 |
|
154 | 151 | cmd_gen = cmdgen.CommandGenerator() |
155 | 152 |
|
@@ -196,8 +193,6 @@ def main(): |
196 | 193 | # Use v without a prefix to use with return values |
197 | 194 | v = DefineOid(dotprefix=False) |
198 | 195 |
|
199 | | - def Tree(): return defaultdict(Tree) |
200 | | - |
201 | 196 | results = Tree() |
202 | 197 |
|
203 | 198 | host = m_args['host'] |
@@ -278,4 +273,189 @@ def Tree(): return defaultdict(Tree) |
278 | 273 | module.exit_json(ansible_facts=results) |
279 | 274 |
|
280 | 275 |
|
281 | | -main() |
| 276 | +class LLDPFactsCollector: |
| 277 | + |
| 278 | + def __init__(self, module): |
| 279 | + self.module = module |
| 280 | + self.m_args = module.params |
| 281 | + self.results = Tree() |
| 282 | + self.if_table = dict() |
| 283 | + self.inverse_if_table = dict() |
| 284 | + self.snmp_engine = SnmpEngine() |
| 285 | + self.context = ContextData() |
| 286 | + self.transport = None |
| 287 | + self._init_auth() |
| 288 | + self.p = DefineOid(dotprefix=True) |
| 289 | + self.v = DefineOid(dotprefix=False) |
| 290 | + |
| 291 | + def _init_auth(self): |
| 292 | + # Verify that we receive a community when using snmp v2 |
| 293 | + if self.m_args['version'] == "v2" or self.m_args['version'] == "v2c": |
| 294 | + if self.m_args['community'] is False: |
| 295 | + self.module.fail_json( |
| 296 | + msg='Community not set when using snmp version 2' |
| 297 | + ) |
| 298 | + |
| 299 | + if self.m_args['version'] == "v3": |
| 300 | + if self.m_args['username'] is None: |
| 301 | + self.module.fail_json( |
| 302 | + msg='Username not set when using snmp version 3' |
| 303 | + ) |
| 304 | + |
| 305 | + if self.m_args['level'] == "authPriv" and self.m_args['privacy'] is None: |
| 306 | + self.module.fail_json( |
| 307 | + msg='Privacy algorithm not set when using authPriv' |
| 308 | + ) |
| 309 | + |
| 310 | + if self.m_args['integrity'] == "sha": |
| 311 | + integrity_proto = cmdgen.usmHMACSHAAuthProtocol |
| 312 | + elif self.m_args['integrity'] == "md5": |
| 313 | + integrity_proto = cmdgen.usmHMACMD5AuthProtocol |
| 314 | + |
| 315 | + if self.m_args['privacy'] == "aes": |
| 316 | + privacy_proto = cmdgen.usmAesCfb128Protocol |
| 317 | + elif self.m_args['privacy'] == "des": |
| 318 | + privacy_proto = cmdgen.usmDESPrivProtocol |
| 319 | + |
| 320 | + # Use SNMP Version 2 |
| 321 | + if self.m_args['version'] == "v2" or self.m_args['version'] == "v2c": |
| 322 | + self.snmp_auth = cmdgen.CommunityData(self.m_args['community']) |
| 323 | + |
| 324 | + # Use SNMP Version 3 with authNoPriv |
| 325 | + elif self.m_args['level'] == "authNoPriv": |
| 326 | + self.snmp_auth = cmdgen.UsmUserData( |
| 327 | + self.m_args['username'], |
| 328 | + authKey=self.m_args['authkey'], |
| 329 | + authProtocol=integrity_proto |
| 330 | + ) |
| 331 | + # Use SNMP Version 3 with authPriv |
| 332 | + else: |
| 333 | + self.snmp_auth = cmdgen.UsmUserData( |
| 334 | + self.m_args['username'], |
| 335 | + authKey=self.m_args['authkey'], |
| 336 | + privKey=self.m_args['privkey'], |
| 337 | + authProtocol=integrity_proto, |
| 338 | + privProtocol=privacy_proto |
| 339 | + ) |
| 340 | + |
| 341 | + async def setup(self): |
| 342 | + self.transport = await UdpTransportTarget.create( |
| 343 | + (self.m_args['host'], 161), |
| 344 | + timeout=self.m_args['timeout'] |
| 345 | + ) |
| 346 | + |
| 347 | + async def collect(self): |
| 348 | + if self.transport is None: |
| 349 | + raise Exception('Transport not initialized. Call setup() first.') |
| 350 | + |
| 351 | + async for errorIndication, errorStatus, errorIndex, varBinds in walk_cmd( |
| 352 | + self.snmp_engine, |
| 353 | + self.snmp_auth, |
| 354 | + self.transport, |
| 355 | + ContextData(), |
| 356 | + ObjectType(ObjectIdentity(self.p.if_descr)), |
| 357 | + lookupMib=False, |
| 358 | + lexicographicMode=False |
| 359 | + ): |
| 360 | + if errorIndication: |
| 361 | + self.module.fail_json( |
| 362 | + msg=f"{str(errorIndication)} querying if_descr." |
| 363 | + ) |
| 364 | + |
| 365 | + for oid, val in varBinds: |
| 366 | + ifIndex = str(oid).split(".")[-1] |
| 367 | + ifDescr = str(val) |
| 368 | + self.if_table[ifDescr] = ifIndex |
| 369 | + self.inverse_if_table[ifIndex] = ifDescr |
| 370 | + |
| 371 | + lldp_rem_sys = dict() |
| 372 | + lldp_rem_port_id = dict() |
| 373 | + lldp_rem_port_desc = dict() |
| 374 | + lldp_rem_chassis_id = dict() |
| 375 | + lldp_rem_sys_desc = dict() |
| 376 | + |
| 377 | + async for errorIndication, errorStatus, errorIndex, varBinds in walk_cmd( |
| 378 | + self.snmp_engine, |
| 379 | + self.snmp_auth, |
| 380 | + self.transport, |
| 381 | + ContextData(), |
| 382 | + ObjectType(ObjectIdentity(self.p.lldp_rem_entry)), |
| 383 | + lookupMib=False, |
| 384 | + lexicographicMode=False |
| 385 | + ): |
| 386 | + if errorIndication: |
| 387 | + self.module.fail_json( |
| 388 | + msg=f"{str(errorIndication)} querying lldp_rem_entry." |
| 389 | + ) |
| 390 | + |
| 391 | + for oid, val in varBinds: |
| 392 | + current_oid = oid.prettyPrint() |
| 393 | + current_val = val.prettyPrint() |
| 394 | + |
| 395 | + ifIndex = str(current_oid).split(".")[-2] |
| 396 | + |
| 397 | + try: |
| 398 | + if_name = self.inverse_if_table[ifIndex] |
| 399 | + except Exception: |
| 400 | + print( |
| 401 | + json.dumps({"unbound_interface_index": ifIndex}) |
| 402 | + ) |
| 403 | + module.fail_json(msg="unboundinterface in inverse if table") |
| 404 | + |
| 405 | + if self.v.lldp_rem_sys_name in current_oid: |
| 406 | + lldp_rem_sys[if_name] = current_val |
| 407 | + elif self.v.lldp_rem_port_id in current_oid: |
| 408 | + lldp_rem_port_id[if_name] = current_val |
| 409 | + elif self.v.lldp_rem_port_desc in current_oid: |
| 410 | + lldp_rem_port_desc[if_name] = current_val |
| 411 | + elif self.v.lldp_rem_chassis_id in current_oid: |
| 412 | + lldp_rem_chassis_id[if_name] = current_val |
| 413 | + elif self.v.lldp_rem_sys_desc in current_oid: |
| 414 | + lldp_rem_sys_desc[if_name] = current_val |
| 415 | + |
| 416 | + lldp_data = dict() |
| 417 | + |
| 418 | + for if_name in lldp_rem_sys: |
| 419 | + lldp_data[if_name] = { |
| 420 | + 'neighbor_sys_name': lldp_rem_sys[if_name], |
| 421 | + 'neighbor_port_desc': lldp_rem_port_desc[if_name], |
| 422 | + 'neighbor_port_id': lldp_rem_port_id[if_name], |
| 423 | + 'neighbor_sys_desc': lldp_rem_sys_desc[if_name], |
| 424 | + 'neighbor_chassis_id': lldp_rem_chassis_id[if_name] |
| 425 | + } |
| 426 | + |
| 427 | + self.results['ansible_lldp_facts'] = lldp_data |
| 428 | + |
| 429 | + |
| 430 | +async def main(module): |
| 431 | + collector = LLDPFactsCollector(module) |
| 432 | + await collector.setup() |
| 433 | + await collector.collect() |
| 434 | + module.exit_json(ansible_facts=collector.results) |
| 435 | + |
| 436 | + |
| 437 | +if __name__ == '__main__': |
| 438 | + module = AnsibleModule( |
| 439 | + argument_spec=dict( |
| 440 | + host=dict(required=True), |
| 441 | + timeout=dict(reqired=False, type='int', default=20), |
| 442 | + version=dict(required=True, choices=['v2', 'v2c', 'v3']), |
| 443 | + community=dict(required=False, default=False), |
| 444 | + username=dict(required=False), |
| 445 | + level=dict(required=False, choices=['authNoPriv', 'authPriv']), |
| 446 | + integrity=dict(required=False, choices=['md5', 'sha']), |
| 447 | + privacy=dict(required=False, choices=['des', 'aes']), |
| 448 | + authkey=dict(required=False), |
| 449 | + privkey=dict(required=False), |
| 450 | + removeplaceholder=dict(required=False)), |
| 451 | + required_together=( |
| 452 | + ['username', 'level', 'integrity', 'authkey'], |
| 453 | + ['privacy', 'privkey'], |
| 454 | + ), |
| 455 | + supports_check_mode=False |
| 456 | + ) |
| 457 | + |
| 458 | + if pysnmp.version[0] < 5: |
| 459 | + main_legacy(module) |
| 460 | + else: |
| 461 | + asyncio.run(main(module)) |
0 commit comments