/
opserver_introspect_utils.py
310 lines (268 loc) · 9.9 KB
/
opserver_introspect_utils.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
import sys
vizdtestdir = sys.path[0]
import urllib2
import xmltodict
import json
import requests
import socket
from lxml import etree
from verification_util import *
from opserver_results import *
from opserver.opserver_util import OpServerUtils
class VerificationOpsSrv (VerificationUtilBase):
def __init__(self, ip, port=8081):
super(VerificationOpsSrv, self).__init__(ip, port)
def get_ops_vm(self, vm='default-virtual-machine'):
vm_dict = self.dict_get('analytics/uves/virtual-machine/' + vm)
return OpVMResult(vm_dict)
def get_ops_vn(self, vn='default-virtual-network'):
res = None
try:
vn_dict = self.dict_get('analytics/uves/virtual-network/' + vn)
res = OpVNResult(vn_dict)
except Exception as e:
print e
finally:
return res
def get_ops_collector(self, col=None):
if (col is None):
col = socket.gethostname()
res = None
try:
#import pdb; pdb.set_trace()
col_dict = self.dict_get('analytics/uves/analytics-node/' + col)
res = OpCollectorResult(col_dict)
except Exception as e:
print e
finally:
return res
def send_tracebuffer_req(self, src, mod, instance, buf_name):
return self.dict_get('analytics/send-tracebuffer/%s/%s/%s/%s' \
% (src, mod, instance, buf_name))
def get_table_column_values(self, table, col_name):
return self.dict_get('analytics/table/%s/column-values/%s' \
% (table, col_name))
def uve_query(self, query):
return self.dict_get('analytics/uves/%s' % query)
def post_uve_request(self, table, json_body):
url = 'http://%s:%s/analytics/uves/%s' % (self._ip, str(self._port), table)
try:
res = OpServerUtils.post_url_http(url, json_body, sync=True)
res = json.loads(res)
except Exception as e:
print 'Error: POST uve request: %s' % str(e)
return None
else:
return res
# end post_uve_request
def get_alarms(self, query):
return self.dict_get('analytics/alarms/%s' % query)
# end get_alarms
def post_alarm_request(self, table, json_body):
url = 'http://%s:%s/analytics/alarms/%s' % (self._ip, str(self._port), table)
try:
res = OpServerUtils.post_url_http(url, json_body, sync=True)
res = json.loads(res)
except Exception as e:
print 'Error: POST alarm request: %s' % str(e)
return None
else:
return res
# end post_alarm_request
def get_redis_uve_info(self):
path = 'Snh_RedisUVERequest'
xpath = '/RedisUVEResponse/redis_uve_info'
p = self.dict_get(path, XmlDrv)
return EtreeToDict(xpath).get_all_entry(p)
def post_query_json(self, json_str, sync=True):
'''
this module is to support raw query given in json format
'''
res = None
try:
flows_url = OpServerUtils.opserver_query_url(self._ip, str(self._port))
print flows_url
print "query is: ", json_str
res = []
resp = OpServerUtils.post_url_http(flows_url, json_str, sync)
if sync:
if resp is not None:
res = json.loads(resp)
res = res['value']
else:
if resp is not None:
resp = json.loads(resp)
qid = resp['href'].rsplit('/', 1)[1]
result = OpServerUtils.get_query_result(self._ip, str(self._port), qid, 30)
for item in result:
res.append(item)
except Exception as e:
print str(e)
finally:
return res
def post_purge_query_json(self, json_str, sync=True):
'''
this module is to support raw purge query given in json format
'''
res = None
try:
purge_request_url = \
OpServerUtils.opserver_database_purge_query_url(
self._ip, str(self._port))
print purge_request_url
print "query is: ", json_str
resp = OpServerUtils.post_url_http(
purge_request_url, json_str, sync)
if resp is not None:
res = json.loads(resp)
except Exception as e:
print str(e)
finally:
return res
def post_query(self, table, start_time=None, end_time=None,
select_fields=None, where_clause=None,
sort_fields=None, sort=None, limit=None,
filter=None, sync=True,dir=None):
res = None
try:
flows_url = OpServerUtils.opserver_query_url(
self._ip, str(self._port))
print flows_url
query_dict = OpServerUtils.get_query_dict(
table, start_time, end_time,
select_fields,
where_clause,
sort_fields, sort, limit, filter, dir)
print json.dumps(query_dict)
res = []
resp = OpServerUtils.post_url_http(
flows_url, json.dumps(query_dict), sync)
if sync:
if resp is not None:
res = json.loads(resp)
res = res['value']
else:
if resp is not None:
resp = json.loads(resp)
qid = resp['href'].rsplit('/', 1)[1]
result = OpServerUtils.get_query_result(
self._ip, str(self._port), qid, 30)
for item in result:
res.append(item)
except Exception as e:
print str(e)
finally:
return res
if __name__ == '__main__':
vns = VerificationOpsSrv('127.0.0.1')
vn = vns.get_ops_vn(vn='abc-corp:vn02')
print "*** Verify VN Cfg ***"
print vn.get_attr('Config', 'attached_policies', 'abc-default-policy')
'''
[{u'vnp_major': u'10', u'vnp_name': u'abc-default-policy',
u'vnp_minor': u'50'}]
'''
print vn.get_attr('Config', 'connected_networks')
'''
[u'abc-corp:vn04']
'''
print vn.get_attr('Config', 'total_interfaces')
'''
10
'''
print vn.get_attr('Config', 'total_acl_rules')
'''
60
'''
print "*** Verify VN Agt ***"
print vn.get_attr('Agent', 'total_acl_rules')
'''
55
'''
print vn.get_attr('Agent', 'in_tpkts')
'''
240
'''
print vn.get_attr('Agent', 'in_stats', 'abc-corp:map-reduce-02')
'''
[{u'bytes': u'7200', u'other_vn': u'abc-corp:map-reduce-02',
u'tpkts': u'60'}]
'''
vm = vns.get_ops_vm(vm='abc-corp:vm-web-fe01')
print "*** Verify VM Cfg ***"
print vm.get_attr('Config', 'vrouter')
'''
rack01-host04
'''
print vm.get_attr('Config', 'attached_groups')
'''
[u'abc-grp01']
'''
print vm.get_attr('Config', 'interface_list', 'abc-corp:vn-fe')
'''
[{u'virtual_network': u'abc-corp:vn-fe', u'ip_address': u'10.1.1.2',
u'floating_ips': [u'67.1.1.2', u'67.1.1.3']}]
'''
print "*** Verify VM Agt ***"
print vm.get_attr('Agent', 'vrouter')
'''
rack01-host04
'''
print vm.get_attr('Agent', 'attached_groups')
'''
[u'abc-grp01']
'''
print vm.get_attr('Agent', 'interface_list')
'''
[{u'in_bytes': u'1000', u'out_bytes': u'10000',
u'floating_ips': [u'67.1.1.2', u'67.1.1.3'],
u'out_pkts': u'20', u'virtual_network': u'abc-corp:vn-fe',
u'in_pkts': u'5', u'ip_address': u'10.1.1.2'}]
'''
col = vns.get_ops_collector()
print col.get_attr('Analytics', 'generator_infos')
'''
[{u'gen_attr': {u'http_port': u'8089', u'in_clear': u'false',
u'pid': u'57160', u'connects': u'1', u'clears': u'1',
u'resets': u'0'},
u'source': u'sa-nc-mfg-30.static.jnpr.net',
u'msgtype_stats': {u'SandeshStats':
[{u'bytes': u'1363005',
u'messages': u'431',
u'message_type': u'CollectorInfo'}]},
u'module_id': u'Collector'},
{u'gen_attr': {u'http_port': u'0', u'in_clear': u'false',
u'pid': u'0', u'connects': u'1', u'clears': u'0',
u'resets': u'0'},
u'source': u'sa-nc-mfg-30.static.jnpr.net', u'msgtype_stats': {},
u'module_id': u'OpServer'},
{u'gen_attr': {u'http_port': u'8091', u'in_clear': u'false',
u'pid': u'57200', u'connects': u'2', u'clears': u'2',
u'resets': u'1'},
u'source': u'sa-nc-mfg-30.static.jnpr.net',
u'msgtype_stats': {u'SandeshStats': [{u'bytes': u'16771',
u'messages': u'66',
u'message_type': u'QELog'},
{u'bytes': u'12912',
u'messages': u'32',
u'message_type': u'QEQueryLog'}]},
u'module_id': u'QueryEngine'}]
'''
print col.get_attr('Analytics', 'generator_infos',
[('module_id', 'OpServer'),
('source', "sa-nc-mfg-30.static.jnpr.net")])
'''
[{u'gen_attr': {u'http_port': u'0', u'in_clear': u'false', u'pid': u'0',
u'connects': u'1', u'clears': u'0', u'resets': u'0'},
u'source': u'sa-nc-mfg-30.static.jnpr.net', u'msgtype_stats': {},
u'module_id': u'OpServer'}]
'''
print col.get_attr('Analytics', 'cpu_info')
'''
{u'num_cpu': u'4', u'cpu_share': u'0.00833056',
u'meminfo': {u'virt': u'2559582208', u'peakvirt': u'2559582208',
u'res': u'2805760'}}
'''