/
device_config.py
165 lines (147 loc) · 6.13 KB
/
device_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#
# Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
#
from snmp import SnmpSession
import copy, traceback, time, ConfigParser
from vnc_api.vnc_api import VncApi
class DeviceDict(dict):
def __init__(self, name, vnc, *a, **k):
super(DeviceDict, self).__init__(*a, **k)
self.name = name
self._vnc = vnc
def vnc(self):
return self._vnc
class DeviceConfig(object):
__pat = None
def __init__(self, name, cfg={}, mibs=[], flow_export_source_ip=None,
mgmt_ip=None):
self._raw = cfg
self.name = name
self.snmp_ip = mgmt_ip or name
self.mibs = mibs or SnmpSession.TABLES() #all
self.flow_export_source_ip = flow_export_source_ip
self.snmp_name = None
def set_snmp_name(self, name):
self.snmp_name = name
def get_snmp_name(self):
return self.snmp_name
def set_mibs(self, mib):
if mib in SnmpSession.TABLES() and mib not in self.mibs:
self.mibs.append(mib)
def get_mibs(self):
return self.mibs
def get_flow_export_source_ip(self):
return self.flow_export_source_ip
def snmp_cfg(self):
cfg = copy.copy(self._raw)
cfg['DestHost'] = self.snmp_ip
cfg['Version'] = int(cfg['Version'])
return cfg
@classmethod
def _pat(self):
if self.__pat is None:
self.__pat = re.compile(', *| +')
return self.__pat
@staticmethod
def _get_and_remove_key(data_dict, key, default=None):
val = default
if key in data_dict:
val = data_dict[key]
del data_dict[key]
return val
@classmethod
def _mklist(self, s):
if isinstance(s, str):
return self._pat().split(s)
return s
@staticmethod
def fom_file(filename):
devices = []
devcfg = ConfigParser.SafeConfigParser()
devcfg.optionxform = str
devcfg.read([filename])
for dev in devcfg.sections():
nd = dict(devcfg.items(dev))
mibs = DeviceConfig._mklist(DeviceConfig._get_and_remove_key(nd,
'Mibs', []))
flow_export_source_ip = DeviceConfig._get_and_remove_key(nd,
'FlowExportSourceIp')
devices.append(DeviceConfig(dev, nd, mibs,
flow_export_source_ip))
return devices
@staticmethod
def get_vnc(usr, passwd, tenant, api_servers, auth_host=None,
auth_port=None, auth_protocol=None, notifycb=None):
for rt in (5, 2, 7, 9, 16, 25):
for api_server in api_servers:
srv = api_server.split(':')
if len(srv) == 2:
ip, port = srv[0], int(srv[1])
else:
ip, port = '127.0.0.1', int(srv[0])
try:
vnc = VncApi(usr, passwd, tenant, ip, port,
auth_host=auth_host, auth_port=auth_port,
auth_protocol=auth_protocol)
if callable(notifycb):
notifycb('api', 'Connected', servers=api_server)
return vnc
except Exception as e:
traceback.print_exc()
if callable(notifycb):
notifycb('api', 'Not connected', servers=api_server,
up=False)
time.sleep(rt)
raise e
@staticmethod
def fom_api_server(api_servers, usr, passwd, tenant, auth_host=None,
auth_port=None, auth_protocol=None, notifycb=None):
vnc = DeviceConfig.get_vnc(usr, passwd, tenant, api_servers,
auth_host=auth_host, auth_port=auth_port,
auth_protocol=auth_protocol, notifycb=notifycb)
devices = map(lambda e: DeviceDict(e['fq_name'][-1], vnc, **e),
vnc.physical_routers_list()[
'physical-routers'])
return devices
@staticmethod
def populate_cfg(devicelist):
devices = []
vnc = devicelist[0].vnc()
for pr in vnc.physical_routers_list(detail=True, obj_uuids=[
x['uuid'] for x in devicelist]):
snmp = pr.get_physical_router_snmp_credentials()
if snmp:
nd = { 'Version': snmp.get_version() }
if snmp.get_local_port():
nd['RemotePort'] = snmp.get_local_port()
if snmp.get_retries():
nd['Retries'] = snmp.get_retries()
if snmp.get_timeout():
#timeout converted from s to us
nd['Timeout'] = snmp.get_timeout()*1000000
if nd['Version'] in (1, 2):
nd['Community'] = snmp.get_v2_community()
elif nd['Version'] == 3:
if snmp.get_v3_security_name():
nd['SecName'] = snmp.get_v3_security_name()
if snmp.get_v3_security_level():
nd['SecLevel'] = snmp.get_v3_security_level()
if snmp.get_v3_security_engine_id():
nd['SecEngineId'] = snmp.get_v3_security_engine_id()
if snmp.get_v3_context():
nd['Context'] = snmp.get_v3_context()
if snmp.get_v3_context_engine_id():
nd['ContextEngineId'] = snmp.get_v3_context_engine_id()
if snmp.get_v3_authentication_protocol():
nd['AuthProto'] = snmp.get_v3_authentication_protocol()
if snmp.get_v3_authentication_password():
nd['AuthPass'] = snmp.get_v3_authentication_password()
if snmp.get_v3_privacy_protocol():
nd['PrivProto'] = snmp.get_v3_privacy_protocol()
if snmp.get_v3_privacy_password():
nd['PrivProto'] = snmp.get_v3_privacy_password()
devices.append(DeviceConfig(
pr.name,
nd, [], pr.get_physical_router_management_ip(),
pr.get_physical_router_management_ip()))
return devices