-
Notifications
You must be signed in to change notification settings - Fork 390
/
contrail-status.py
executable file
·431 lines (379 loc) · 14.8 KB
/
contrail-status.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/python
#
# Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
#
from optparse import OptionParser
import subprocess
import os
import glob
import platform
import ConfigParser
import socket
import requests
from StringIO import StringIO
from lxml import etree
from sandesh_common.vns.constants import ServiceHttpPortMap, \
NodeUVEImplementedServices, ServicesDefaultConfigurationFile, \
BackupImplementedServices
try:
subprocess.check_call(["dpkg-vendor", "--derives-from", "debian"])
distribution = 'debian'
except:
distribution = 'centos'
class EtreeToDict(object):
"""Converts the xml etree to dictionary/list of dictionary."""
def __init__(self, xpath):
self.xpath = xpath
#end __init__
def _handle_list(self, elems):
"""Handles the list object in etree."""
a_list = []
for elem in elems.getchildren():
rval = self._get_one(elem, a_list)
if 'element' in rval.keys():
a_list.append(rval['element'])
elif 'list' in rval.keys():
a_list.append(rval['list'])
else:
a_list.append(rval)
if not a_list:
return None
return a_list
#end _handle_list
def _get_one(self, xp, a_list=None):
"""Recrusively looks for the entry in etree and converts to dictionary.
Returns a dictionary.
"""
val = {}
child = xp.getchildren()
if not child:
val.update({xp.tag: xp.text})
return val
for elem in child:
if elem.tag == 'list':
val.update({xp.tag: self._handle_list(elem)})
else:
rval = self._get_one(elem, a_list)
if elem.tag in rval.keys():
val.update({elem.tag: rval[elem.tag]})
else:
val.update({elem.tag: rval})
return val
#end _get_one
def get_all_entry(self, path):
"""All entries in the etree is converted to the dictionary
Returns the list of dictionary/didctionary.
"""
xps = path.xpath(self.xpath)
if type(xps) is not list:
return self._get_one(xps)
val = []
for xp in xps:
val.append(self._get_one(xp))
return val
#end get_all_entry
def find_entry(self, path, match):
"""Looks for a particular entry in the etree.
Returns the element looked for/None.
"""
xp = path.xpath(self.xpath)
f = filter(lambda x: x.text == match, xp)
if len(f):
return f[0].text
return None
#end find_entry
#end class EtreeToDict
class IntrospectUtil(object):
def __init__(self, ip, port, debug, timeout):
self._ip = ip
self._port = port
self._debug = debug
self._timeout = timeout
#end __init__
def _mk_url_str(self, path):
return "http://%s:%d/%s" % (self._ip, self._port, path)
#end _mk_url_str
def _load(self, path):
url = self._mk_url_str(path)
resp = requests.get(url, timeout=self._timeout)
if resp.status_code == requests.codes.ok:
return etree.fromstring(resp.text)
else:
if self._debug:
print 'URL: %s : HTTP error: %s' % (url, str(resp.status_code))
return None
#end _load
def get_uve(self, tname):
path = 'Snh_SandeshUVECacheReq?x=%s' % (tname)
xpath = './/' + tname
p = self._load(path)
if p is not None:
return EtreeToDict(xpath).get_all_entry(p)
else:
if self._debug:
print 'UVE: %s : not found' % (path)
return None
#end get_uve
#end class IntrospectUtil
def service_installed(svc):
if distribution == 'debian':
cmd = 'initctl show-config ' + svc
else:
cmd = 'chkconfig --list ' + svc
with open(os.devnull, "w") as fnull:
return not subprocess.call(cmd.split(), stdout=fnull, stderr=fnull)
def service_bootstatus(svc):
if distribution == 'debian':
cmd = 'initctl show-config ' + svc
cmdout = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
if cmdout.find(' start on') != -1:
return ''
else:
return ' (disabled on boot)'
else:
cmd = 'chkconfig ' + svc
with open(os.devnull, "w") as fnull:
if not subprocess.call(cmd.split(), stdout=fnull, stderr=fnull):
return ''
else:
return ' (disabled on boot)'
def service_status(svc):
cmd = 'service ' + svc + ' status'
cmdout = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
if cmdout.find('running') != -1:
return 'active'
else:
return 'inactive'
def check_svc(svc):
psvc = svc + ':'
if service_installed(svc):
bootstatus = service_bootstatus(svc)
status = service_status(svc)
else:
bootstatus = ' (disabled on boot)'
status='inactive'
print '%-30s%s%s' %(psvc, status, bootstatus)
_DEFAULT_CONF_FILE_DIR = '/etc/contrail/'
_DEFAULT_CONF_FILE_EXTENSION = '.conf'
def get_http_server_port_from_conf(svc_name, debug):
# Open and extract conf file
if svc_name in ServicesDefaultConfigurationFile:
default_conf_file = ServicesDefaultConfigurationFile[svc_name]
else:
default_conf_file = _DEFAULT_CONF_FILE_DIR + svc_name + \
_DEFAULT_CONF_FILE_EXTENSION
try:
fp = open(default_conf_file)
except IOError as e:
if debug:
print '{0}: Could not read filename {1}'.format(\
svc_name, default_conf_file)
return -1
else:
data = StringIO('\n'.join(line.strip() for line in fp))
# Parse conf file
parser = ConfigParser.SafeConfigParser()
try:
parser.readfp(data)
except ConfigParser.ParsingError as e:
fp.close()
if debug:
print '{0}: Parsing error: {1}'.format(svc_name, \
str(e))
return -1
# Read DEFAULT.http_server_port from the conf file. If that fails try
# DEFAULTS.http_server_port (for python daemons)
try:
http_server_port = parser.getint('DEFAULT', 'http_server_port')
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError, \
ValueError) as de:
try:
http_server_port = parser.getint('DEFAULTS', 'http_server_port')
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError) as dse:
fp.close()
if debug:
print '{0}: DEFAULT/S.http_server_port not present'.format(
svc_name)
return -1
else:
fp.close()
return http_server_port
else:
fp.close()
return http_server_port
def get_default_http_server_port(svc_name, debug):
if svc_name in ServiceHttpPortMap:
return ServiceHttpPortMap[svc_name]
else:
if debug:
print '{0}: Introspect port not found'.format(svc_name)
return -1
def get_http_server_port(svc_name, debug):
http_server_port = get_http_server_port_from_conf(svc_name, debug)
if http_server_port == -1:
http_server_port = get_default_http_server_port(svc_name, debug)
return http_server_port
def get_svc_uve_status(svc_name, debug, timeout):
# Get the HTTP server (introspect) port for the service
http_server_port = get_http_server_port(svc_name, debug)
if http_server_port == -1:
return None, None
# Now check the NodeStatus UVE
svc_introspect = IntrospectUtil('localhost', http_server_port, debug,
timeout)
node_status = svc_introspect.get_uve('NodeStatus')
if node_status is None:
if debug:
print '{0}: NodeStatusUVE not found'.format(svc_name)
return None, None
node_status = [item for item in node_status if 'process_status' in item]
if not len(node_status):
if debug:
print '{0}: ProcessStatus not present in NodeStatusUVE'.format(
svc_name)
return None, None
process_status_info = node_status[0]['process_status']
if len(process_status_info) == 0:
if debug:
print '{0}: Empty ProcessStatus in NodeStatusUVE'.format(svc_name)
return None, None
return process_status_info[0]['state'], process_status_info[0]['description']
def check_svc_status(service_name, debug, detail, timeout):
service_sock = service_name.replace('-', '_')
service_sock = service_sock.replace('supervisor_', 'supervisord_') + '.sock'
cmd = 'supervisorctl -s unix:///tmp/' + service_sock + ' status'
cmdout = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
if cmdout.find('refused connection') == -1:
cmdout = cmdout.replace(' STARTING', 'initializing')
cmdout = cmdout.replace(' RUNNING', 'active')
cmdout = cmdout.replace(' STOPPED', 'inactive')
cmdout = cmdout.replace(' FATAL', 'failed')
cmdoutlist = cmdout.split('\n')
if debug:
print '%s: %s' % (str(service_name), cmdoutlist)
for supervisor_svc_info_cmdout in cmdoutlist:
supervisor_svc_info = supervisor_svc_info_cmdout.split()
if len(supervisor_svc_info) >= 2:
svc_name = supervisor_svc_info[0]
svc_status = supervisor_svc_info[1]
svc_detail_info = ' '.join(supervisor_svc_info[2:])
# Extract UVE state only for running processes
svc_uve_description = None
if (svc_name in NodeUVEImplementedServices or
svc_name.rsplit('-', 1)[0] in NodeUVEImplementedServices) and svc_status == 'active':
try:
svc_uve_status, svc_uve_description = get_svc_uve_status(svc_name, debug, timeout)
except requests.ConnectionError, e:
if debug:
print 'Socket Connection error : %s' % (str(e))
svc_uve_status = "connection-error"
except (requests.Timeout, socket.timeout) as te:
if debug:
print 'Timeout error : %s' % (str(te))
svc_uve_status = "connection-timeout"
if svc_uve_status is not None:
if svc_uve_status == 'Non-Functional':
svc_status = 'initializing'
elif svc_uve_status == 'connection-error':
if svc_name in BackupImplementedServices:
svc_status = 'backup'
else:
svc_status = 'initializing'
elif svc_uve_status == 'connection-timeout':
svc_status = 'timeout'
else:
svc_status = 'initializing'
if svc_uve_description is not None and svc_uve_description is not '':
svc_status = svc_status + ' (' + svc_uve_description + ')'
if not detail:
print '{0:<30}{1:<20}'.format(svc_name, svc_status)
else:
print '{0:<30}{1:<20}{2:<40}'.format(svc_name, svc_status, svc_detail_info)
print
def check_status(svc_name, options):
check_svc(svc_name)
check_svc_status(svc_name, options.debug, options.detail, options.timeout)
def supervisor_status(nodetype, options):
if nodetype == 'compute':
print "== Contrail vRouter =="
check_status('supervisor-vrouter', options)
elif nodetype == 'config':
print "== Contrail Config =="
check_status('supervisor-config', options)
elif nodetype == 'control':
print "== Contrail Control =="
check_status('supervisor-control', options)
elif nodetype == 'analytics':
print "== Contrail Analytics =="
check_status('supervisor-analytics', options)
elif nodetype == 'database':
print "== Contrail Database =="
check_status('supervisor-database', options)
elif nodetype == 'webui':
print "== Contrail Web UI =="
check_status('supervisor-webui', options)
elif nodetype == 'support-service':
print "== Contrail Support Services =="
check_status('supervisor-support-service', options)
def package_installed(pkg):
if distribution == 'debian':
cmd = "dpkg -l " + pkg
else:
cmd = "rpm -q " + pkg
with open(os.devnull, "w") as fnull:
return (not subprocess.call(cmd.split(), stdout=fnull, stderr=fnull))
def main():
parser = OptionParser()
parser.add_option('-d', '--detail', dest='detail',
default=False, action='store_true',
help="show detailed status")
parser.add_option('-x', '--debug', dest='debug',
default=False, action='store_true',
help="show debugging information")
parser.add_option('-t', '--timeout', dest='timeout', type="float",
default=2,
help="timeout in seconds to use for HTTP requests to services")
(options, args) = parser.parse_args()
if args:
parser.error("No arguments are permitted")
control = package_installed('contrail-control')
analytics = package_installed('contrail-analytics')
agent = package_installed('contrail-vrouter')
capi = package_installed('contrail-config')
cwebui = package_installed('contrail-web-controller')
cwebstorage = package_installed('contrail-web-storage')
database = (package_installed('contrail-openstack-database') or
package_installed('contrail-database'))
storage = package_installed('contrail-storage')
vr = False
lsmodout = subprocess.Popen('lsmod', stdout=subprocess.PIPE).communicate()[0]
if lsmodout.find('vrouter') != -1:
vr = True
if agent:
if not vr:
print "vRouter is NOT PRESENT\n"
supervisor_status('compute', options)
else:
if vr:
print "vRouter is PRESENT\n"
if control:
supervisor_status('control', options)
if analytics:
supervisor_status('analytics', options)
if capi:
supervisor_status('config', options)
if cwebui or cwebstorage:
supervisor_status('webui', options)
if database:
supervisor_status('database', options)
if capi:
supervisor_status('support-service', options)
if storage:
print "== Contrail Storage =="
check_svc('contrail-storage-stats')
if len(glob.glob('/var/crashes/core.*')) != 0:
print "========Run time service failures============="
for file in glob.glob('/var/crashes/core.*'):
print file
if __name__ == '__main__':
main()