-
Notifications
You must be signed in to change notification settings - Fork 390
/
snmpuve.py
259 lines (235 loc) · 11.1 KB
/
snmpuve.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#
# Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
#
import pprint, socket, copy
import datetime
from pysandesh.sandesh_base import *
from pysandesh.connection_info import ConnectionState
from gen_py.prouter.ttypes import ArpTable, IfTable, IfXTable, IfStats, \
IpMib, LldpSystemCapabilitiesMap, LldpLocManAddrEntry, \
LldpLocalSystemData, LldpRemOrgDefInfoTable, \
LldpRemOrgDefInfoTable, LldpRemOrgDefInfoEntry, \
LldpRemManAddrEntry, LldpRemoteSystemsData, \
dot1qTpFdbPortTable, dot1dBasePortIfIndexTable, \
LldpTable, PRouterEntry, PRouterUVE, PRouterFlowEntry, \
PRouterFlowUVE, IfIndexOperStatusTable
from opserver.sandesh.analytics.ttypes import NodeStatusUVE, NodeStatus
from sandesh_common.vns.ttypes import Module, NodeType
from sandesh_common.vns.constants import ModuleNames, CategoryNames,\
ModuleCategoryMap, Module2NodeType, NodeTypeNames, ModuleIds,\
INSTANCE_ID_DEFAULT
from pysandesh.gen_py.process_info.ttypes import ConnectionType,\
ConnectionStatus
class SnmpUve(object):
_composite_rules = dict(
ifInPkts=['ifInUcastPkts', 'ifInBroadcastPkts', 'ifInMulticastPkts'],
ifOutPkts=['ifOutUcastPkts', 'ifOutBroadcastPkts',
'ifOutMulticastPkts'],
)
def __init__(self, conf, instance='0'):
self._conf = conf
module = Module.CONTRAIL_SNMP_COLLECTOR
self._moduleid = ModuleNames[module]
node_type = Module2NodeType[module]
self._node_type_name = NodeTypeNames[node_type]
self._hostname = socket.gethostname()
self._instance_id = instance
if self._conf.sandesh_send_rate_limit() is not None:
SandeshSystem.set_sandesh_send_rate_limit( \
self._conf.sandesh_send_rate_limit());
sandesh_global.init_generator(self._moduleid, self._hostname,
self._node_type_name, self._instance_id,
self._conf.collectors(),
self._node_type_name,
self._conf.http_port(),
['contrail_snmp_collector.gen_py',
'opserver.sandesh'],
self._conf._disc)
sandesh_global.set_logging_params(
enable_local_log=self._conf.log_local(),
category=self._conf.log_category(),
level=self._conf.log_level(),
file=self._conf.log_file(),
enable_syslog=self._conf.use_syslog(),
syslog_facility=self._conf.syslog_facility())
ConnectionState.init(sandesh_global, self._hostname, self._moduleid,
self._instance_id,
staticmethod(ConnectionState.get_process_state_cb),
NodeStatusUVE, NodeStatus)
self.if_stat = {}
self._logger = sandesh_global.logger()
def map_svc(self, svc):
return {
'api' : ConnectionType.APISERVER,
'discovery' : ConnectionType.DISCOVERY,
'zookeeper' : ConnectionType.ZOOKEEPER,
}[svc]
def map_sts(self, up):
return {
True : ConnectionStatus.UP,
False : ConnectionStatus.DOWN,
}[up]
def conn_state_notify(self, svc, msg='', up=True, servers=''):
ctype = self.map_svc(svc)
status = self.map_sts(up)
ConnectionState.update(conn_type=ctype, name='SNMP', status=status,
message=msg, server_addrs=[servers])
def killall(self):
sandesh_global.uninit()
if self._conf._disc:
self._conf._disc.uninit()
def delete(self, dev):
PRouterUVE(data=PRouterEntry(**dict(
name=dev, deleted=True))).send()
PRouterFlowUVE(data=PRouterFlowEntry(**dict(
name=dev, deleted=True))).send()
def logger(self):
return self._logger
def make_composites(self, ife):
for k in self._composite_rules.keys():
for cmpsr in self._composite_rules[k]:
if cmpsr in ife:
if k not in ife:
ife[k] = 0
ife[k] += ife[cmpsr]
def get_diff(self, data):
pname = data['name']
if pname not in self.if_stat:
self.if_stat[pname] = {}
diffs = {}
iftables = {}
for ife in data['ifMib']['ifTable'] + data['ifMib']['ifXTable']:
if 'ifDescr' in ife:
ifname = ife['ifDescr']
elif 'ifName' in ife:
ifname = ife['ifName']
else:
print str(datetime.datetime.now()),'Err: ', ife.keys(), pname
continue
if ifname not in iftables:
iftables[ifname] = {}
iftables[ifname].update(ife)
for ifname, ife in iftables.items():
self.make_composites(ife)
if ifname not in diffs:
diffs[ifname] = {}
if ifname in self.if_stat[pname]:
diffs[ifname].update(self.diff(self.if_stat[pname][ifname], ife,
ifname))
if ifname not in self.if_stat[pname]:
self.if_stat[pname][ifname] = {}
self.if_stat[pname][ifname].update(self.stat_req(ife))
data['ifStats'] = map(lambda x: IfStats(**x), diffs.values())
def diff(self, old, new, ifname):
d = dict(ifIndex=new['ifIndex'], ifName=ifname)
for k in self.stat_list():
if k in new and k in old:
d[k] = new[k] - old[k]
if d[k] < 0:
d[k] = 0 # safety check for data miss
return d
def stat_list(self):
return ('ifInPkts', 'ifOutPkts', 'ifInOctets', 'ifOutOctets',
'ifInDiscards', 'ifInErrors', 'ifOutDiscards', 'ifOutErrors',)
def stat_req(self, d):
r = {}
for k in self.stat_list():
if k in d:
r[k] = d[k]
return r
def send_ifstatus_update(self, data):
for dev in data:
ifIndexOperStatusTable = []
for ifidx in data[dev]:
ifIndexOperStatusTable.append(IfIndexOperStatusTable(
ifIndex=ifidx, ifOperStatus=data[dev][ifidx][0]))
PRouterUVE(data=PRouterEntry(
name=dev,
ifIndexOperStatusTable=ifIndexOperStatusTable)).send()
def send_flow_uve(self, data):
if data['name']:
PRouterFlowUVE(data=PRouterFlowEntry(**data)).send()
def send(self, data):
uve = self.make_uve(data)
self.send_uve(uve)
def _to_cap_map(self, cm):
return eval('LldpSystemCapabilitiesMap.' + \
LldpSystemCapabilitiesMap._VALUES_TO_NAMES[cm],
globals(), locals())
def make_uve(self, data):
# print 'Building UVE:', data['name']
if 'arpTable' in data:
data['arpTable'] = map(lambda x: ArpTable(**x),
data['arpTable'])
if 'ifMib' in data:
self.get_diff(data)
if 'ifTable' in data['ifMib']:
data['ifTable'] = map(lambda x: IfTable(**x),
data['ifMib']['ifTable'])
if 'ifXTable' in data['ifMib']:
data['ifXTable'] = map(lambda x: IfXTable(**x),
data['ifMib']['ifXTable'])
del data['ifMib']
if 'ipMib' in data:
data['ipMib'] = map(lambda x: IpMib(**x), data['ipMib'])
if 'lldpTable' in data:
if 'lldpLocalSystemData' in data['lldpTable']:
if 'lldpLocManAddrEntry' in data['lldpTable'][
'lldpLocalSystemData']:
data['lldpTable']['lldpLocalSystemData'][
'lldpLocManAddrEntry'] = LldpLocManAddrEntry(
**data['lldpTable']['lldpLocalSystemData'][
'lldpLocManAddrEntry'])
if 'lldpLocSysCapEnabled' in data['lldpTable'][
'lldpLocalSystemData']:
data['lldpTable']['lldpLocalSystemData'][
'lldpLocSysCapEnabled'] = map(self._to_cap_map, data[
'lldpTable']['lldpLocalSystemData'][
'lldpLocSysCapEnabled'])
if 'lldpLocSysCapSupported' in data['lldpTable'][
'lldpLocalSystemData']:
data['lldpTable']['lldpLocalSystemData'][
'lldpLocSysCapSupported'] = map(self._to_cap_map,
data['lldpTable']['lldpLocalSystemData'][
'lldpLocSysCapSupported'])
data['lldpTable']['lldpLocalSystemData'] = \
LldpLocalSystemData(**data['lldpTable'][
'lldpLocalSystemData'])
rl = []
if 'lldpRemoteSystemsData' in data['lldpTable']:
for d in data['lldpTable']['lldpRemoteSystemsData']:
if 'lldpRemSysCapEnabled' in d:
d['lldpRemSysCapEnabled'] = map(self._to_cap_map,
d['lldpRemSysCapEnabled'])
if 'lldpRemSysCapSupported' in d:
d['lldpRemSysCapSupported'] = map(self._to_cap_map,
d['lldpRemSysCapSupported'])
if 'lldpRemOrgDefInfoEntry' in d:
if 'lldpRemOrgDefInfoTable' in d[
'lldpRemOrgDefInfoEntry']:
d['lldpRemOrgDefInfoEntry'][
'lldpRemOrgDefInfoTable'] = map(lambda x: \
LldpRemOrgDefInfoTable(**x),
d['lldpRemOrgDefInfoEntry'][
'lldpRemOrgDefInfoTable'])
d['lldpRemOrgDefInfoEntry'] = \
LldpRemOrgDefInfoEntry(**d[
'lldpRemOrgDefInfoEntry'])
if 'lldpRemManAddrEntry' in d:
d['lldpRemManAddrEntry'] = LldpRemManAddrEntry(
**d['lldpRemManAddrEntry'])
rl.append(LldpRemoteSystemsData(**d))
data['lldpTable']['lldpRemoteSystemsData'] = rl
data['lldpTable'] = LldpTable(**data['lldpTable'])
if 'qBridgeTable' in data:
if 'dot1qTpFdbPortTable' in data['qBridgeTable']:
data['fdbPortTable'] = map(lambda x: dot1qTpFdbPortTable(**x),
data['qBridgeTable']['dot1qTpFdbPortTable'])
if 'dot1dBasePortIfIndexTable' in data['qBridgeTable']:
data['fdbPortIfIndexTable'] = map(lambda x: dot1dBasePortIfIndexTable(**x),
data['qBridgeTable']['dot1dBasePortIfIndexTable'])
del data['qBridgeTable']
return PRouterUVE(data=PRouterEntry(**data))
def send_uve(self, uve):
# print 'Sending UVE:', uve.data.name
uve.send()