author | Tero Marttila <terom@paivola.fi> |
Mon, 09 Mar 2015 19:48:19 +0200 | |
changeset 735 | 008cfe47b194 |
parent 394 | 7077379fb5a0 |
permissions | -rw-r--r-- |
389 | 1 |
""" |
2 |
Requirements: |
|
3 |
memoized-property |
|
4 |
||
5 |
Setup: |
|
6 |
./opt/bin/build-pysnmp-mib -o usr/mibs/LLDP-MIB.py etc/mibs/LLDP-MIB |
|
7 |
||
8 |
Run: |
|
9 |
PYSNMP_MIB_DIRS=usr/mibs/ ./opt/bin/python ... |
|
394
7077379fb5a0
pvl.snmp.lldp: docdoc used MIBS
Tero Marttila <terom@paivola.fi>
parents:
389
diff
changeset
|
10 |
|
7077379fb5a0
pvl.snmp.lldp: docdoc used MIBS
Tero Marttila <terom@paivola.fi>
parents:
389
diff
changeset
|
11 |
SNMP: |
7077379fb5a0
pvl.snmp.lldp: docdoc used MIBS
Tero Marttila <terom@paivola.fi>
parents:
389
diff
changeset
|
12 |
LLDP-MIB::lldpLocalSystemData |
7077379fb5a0
pvl.snmp.lldp: docdoc used MIBS
Tero Marttila <terom@paivola.fi>
parents:
389
diff
changeset
|
13 |
LLDP-MIB::lldpRemTable |
389 | 14 |
""" |
15 |
||
16 |
from pvl.snmp import snmp |
|
17 |
from pvl.snmp.snmp import pysnmp |
|
18 |
||
19 |
from memoized_property import memoized_property |
|
20 |
||
21 |
import logging; log = logging.getLogger('pvl.snmp.lldp') |
|
22 |
||
23 |
def macaddr (value) : |
|
24 |
""" |
|
25 |
Excepts a MAC address from an SNMP OctetString. |
|
26 |
""" |
|
27 |
||
28 |
return ':'.join('{octet:02x}'.format(octet=c) for c in value.asNumbers()) |
|
29 |
||
30 |
class LLDPAgent (snmp.SNMPAgent) : |
|
31 |
""" |
|
32 |
Query LLDP info from a remote agent. |
|
33 |
""" |
|
34 |
||
35 |
@classmethod |
|
36 |
def _chassis_id (cls, chassis_id, subtype) : |
|
37 |
log.debug("%s: %r", subtype, chassis_id) |
|
38 |
||
39 |
# XXX: reference names from LLDP-MIB.py |
|
40 |
if subtype == 4: |
|
41 |
return macaddr(chassis_id) |
|
42 |
else : |
|
43 |
return chassis_id |
|
44 |
||
45 |
@classmethod |
|
46 |
def _port_id (cls, port_id, subtype, desc) : |
|
47 |
log.debug("%s: %r (%s)", subtype, port_id, desc) |
|
48 |
||
49 |
# XXX: reference names from LLDP-MIB.py |
|
50 |
if subtype == 5: # interfaceName -> IF-MIB::ifName ? |
|
51 |
return str(port_id) |
|
52 |
elif subtype == 3 : # macAddress |
|
53 |
if desc : |
|
54 |
# prefer desc in this case |
|
55 |
return desc |
|
56 |
else : |
|
57 |
return macaddr(port_id) |
|
58 |
elif subtype == 7 : # local |
|
59 |
return str(port_id) # XXX: integer? |
|
60 |
else : |
|
61 |
log.warn("unknown subtype=%d: %r", subtype, port_id) |
|
62 |
||
63 |
return port_id |
|
64 |
||
65 |
LLDP_LOC_CHASSIS_ID = pysnmp.MibVariable('LLDP-MIB', 'lldpLocChassisId') |
|
66 |
LLDP_LOC_CHASSIS_ID_SUBTYPE = pysnmp.MibVariable('LLDP-MIB', 'lldpLocChassisIdSubtype') |
|
67 |
LLDP_LOC_SYS_NAME = pysnmp.MibVariable('LLDP-MIB', 'lldpLocSysName') |
|
68 |
||
69 |
@memoized_property |
|
70 |
def local (self) : |
|
71 |
""" |
|
72 |
Describe the local system. |
|
73 |
""" |
|
74 |
||
75 |
for idx, data in self.table( |
|
76 |
self.LLDP_LOC_CHASSIS_ID, |
|
77 |
self.LLDP_LOC_CHASSIS_ID_SUBTYPE, |
|
78 |
self.LLDP_LOC_SYS_NAME, |
|
79 |
) : |
|
80 |
return { |
|
81 |
'chassis': self._chassis_id(data[self.LLDP_LOC_CHASSIS_ID], data[self.LLDP_LOC_CHASSIS_ID_SUBTYPE]), |
|
82 |
'sys_name': str(data[self.LLDP_LOC_SYS_NAME]), |
|
83 |
} |
|
84 |
||
85 |
LLDP_LOC_PORT_DESC = pysnmp.MibVariable('LLDP-MIB', 'lldpLocPortDesc') |
|
86 |
LLDP_LOC_PORT_ID = pysnmp.MibVariable('LLDP-MIB', 'lldpLocPortId') |
|
87 |
LLDP_LOC_PORT_ID_SUBTYPE = pysnmp.MibVariable('LLDP-MIB', 'lldpLocPortIdSubtype') |
|
88 |
||
89 |
@memoized_property |
|
90 |
def ports (self) : |
|
91 |
""" |
|
92 |
Describe the local ports. |
|
93 |
""" |
|
94 |
||
95 |
ports = { } |
|
96 |
||
97 |
for idx, data in self.table( |
|
98 |
self.LLDP_LOC_PORT_DESC, |
|
99 |
self.LLDP_LOC_PORT_ID, |
|
100 |
self.LLDP_LOC_PORT_ID_SUBTYPE, |
|
101 |
) : |
|
102 |
port, = idx |
|
103 |
||
104 |
ports[int(port)] = { |
|
105 |
'port_id': int(port), |
|
106 |
'port': self._port_id(data[self.LLDP_LOC_PORT_ID], data[self.LLDP_LOC_PORT_ID_SUBTYPE], data[self.LLDP_LOC_PORT_DESC]), |
|
107 |
} |
|
108 |
||
109 |
return ports |
|
110 |
||
111 |
def port (self, port) : |
|
112 |
return self.ports[int(port)] |
|
113 |
||
114 |
LLDP_REM_CHASSIS_ID = pysnmp.MibVariable('LLDP-MIB', 'lldpRemChassisId') |
|
115 |
LLDP_REM_CHASSIS_ID_SUBTYPE = pysnmp.MibVariable('LLDP-MIB', 'lldpRemChassisIdSubtype') |
|
116 |
LLDP_REM_SYS_NAME = pysnmp.MibVariable('LLDP-MIB', 'lldpRemSysName') |
|
117 |
LLDP_REM_PORT_ID = pysnmp.MibVariable('LLDP-MIB', 'lldpRemPortId') |
|
118 |
LLDP_REM_PORT_ID_SUBTYPE = pysnmp.MibVariable('LLDP-MIB', 'lldpRemPortIdSubtype') |
|
119 |
LLDP_REM_PORT_DESC = pysnmp.MibVariable('LLDP-MIB', 'lldpRemPortDesc') |
|
120 |
||
121 |
def remotes (self) : |
|
122 |
""" |
|
123 |
Describe remote systems, indexed by local port. |
|
124 |
""" |
|
125 |
||
126 |
for idx, data in self.table( |
|
127 |
self.LLDP_REM_CHASSIS_ID, |
|
128 |
self.LLDP_REM_CHASSIS_ID_SUBTYPE, |
|
129 |
self.LLDP_REM_SYS_NAME, |
|
130 |
self.LLDP_REM_PORT_ID, |
|
131 |
self.LLDP_REM_PORT_ID_SUBTYPE, |
|
132 |
self.LLDP_REM_PORT_DESC, |
|
133 |
) : |
|
134 |
time, port, idx = idx |
|
135 |
||
136 |
yield int(port), { |
|
137 |
'chassis': self._chassis_id(data[self.LLDP_REM_CHASSIS_ID], data[self.LLDP_REM_CHASSIS_ID_SUBTYPE]), |
|
138 |
'sys_name': str(data[self.LLDP_REM_SYS_NAME]), |
|
139 |
'port': self._port_id(data[self.LLDP_REM_PORT_ID], data[self.LLDP_REM_PORT_ID_SUBTYPE], data[self.LLDP_REM_PORT_DESC]), |
|
140 |
} |
|
141 |
||
142 |