2019-09-23 14:59:00 +00:00
|
|
|
#!/usr/bin/python2
|
2015-08-11 18:55:14 +00:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
2019-07-25 17:09:52 +00:00
|
|
|
# Copyright 2015-2019 Lenovo
|
2015-08-11 18:55:14 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2017-08-16 13:57:57 +00:00
|
|
|
import codecs
|
2015-08-11 18:55:14 +00:00
|
|
|
from datetime import datetime as dt
|
2017-01-06 18:28:28 +00:00
|
|
|
import optparse
|
2015-08-11 18:55:14 +00:00
|
|
|
import os
|
2017-06-20 18:56:24 +00:00
|
|
|
import signal
|
2015-08-11 18:55:14 +00:00
|
|
|
import sys
|
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2015-08-11 18:55:14 +00:00
|
|
|
path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
path = os.path.realpath(os.path.join(path, '..', 'lib', 'python'))
|
|
|
|
if path.startswith('/opt'):
|
|
|
|
sys.path.append(path)
|
|
|
|
|
|
|
|
import confluent.client as client
|
|
|
|
|
2019-06-27 19:28:59 +00:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
|
2017-08-16 13:57:57 +00:00
|
|
|
|
2017-01-06 18:28:28 +00:00
|
|
|
argparser = optparse.OptionParser(
|
2017-06-20 18:56:24 +00:00
|
|
|
usage="Usage: %prog [options] noderange [clear]")
|
2019-10-23 18:20:40 +00:00
|
|
|
argparser.add_option('-m', '--maxnodes', type='int',
|
|
|
|
help='Specify a maximum number of '
|
|
|
|
'nodes to clear if clearing log, '
|
|
|
|
'prompting if over the threshold')
|
2017-01-06 18:28:28 +00:00
|
|
|
(options, args) = argparser.parse_args()
|
2015-08-11 18:55:14 +00:00
|
|
|
try:
|
2017-01-06 18:28:28 +00:00
|
|
|
noderange = args[0]
|
2015-08-11 18:55:14 +00:00
|
|
|
except IndexError:
|
2017-01-06 18:28:28 +00:00
|
|
|
argparser.print_help()
|
2015-08-11 18:55:14 +00:00
|
|
|
sys.exit(1)
|
2017-11-13 16:49:40 +00:00
|
|
|
client.check_globbing(noderange)
|
2015-08-11 18:55:14 +00:00
|
|
|
deletemode = False
|
2019-10-23 20:47:42 +00:00
|
|
|
if len(args) > 2:
|
2017-11-15 20:38:59 +00:00
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
2019-10-23 20:47:42 +00:00
|
|
|
if len(args) == 2:
|
|
|
|
if args[1] == 'clear':
|
2015-08-11 18:55:14 +00:00
|
|
|
deletemode = True
|
2017-11-15 20:38:59 +00:00
|
|
|
else:
|
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
2015-08-11 18:55:14 +00:00
|
|
|
|
|
|
|
session = client.Command()
|
|
|
|
exitcode = 0
|
|
|
|
|
|
|
|
def format_event(evt):
|
|
|
|
retparts = []
|
2016-10-11 20:46:01 +00:00
|
|
|
if 'timestamp' in evt and evt['timestamp'] is not None:
|
2015-08-11 18:55:14 +00:00
|
|
|
display = dt.strptime(evt['timestamp'], '%Y-%m-%dT%H:%M:%S')
|
|
|
|
retparts.append(display.strftime('%m/%d/%Y %H:%M:%S'))
|
|
|
|
dscparts = []
|
2019-07-25 17:09:52 +00:00
|
|
|
if evt.get('log_id', None):
|
|
|
|
retparts.append(evt['log_id'] + ':')
|
2016-09-09 13:14:25 +00:00
|
|
|
if 'component_type' in evt and evt['component_type'] is not None:
|
2015-08-11 18:55:14 +00:00
|
|
|
dscparts.append(evt['component_type'])
|
|
|
|
if 'component' in evt and evt['component'] is not None:
|
|
|
|
dscparts.append(evt['component'])
|
2016-09-09 13:14:25 +00:00
|
|
|
if 'event' in evt and evt['event'] and evt['event'] is not None:
|
2015-08-11 18:55:14 +00:00
|
|
|
evttext = evt['event']
|
|
|
|
try:
|
|
|
|
if evttext.startswith(evt['component'] + ' - '):
|
|
|
|
evttext = evt['event'].replace(evt['component'] + ' - ', '')
|
|
|
|
except (KeyError, TypeError):
|
|
|
|
pass
|
|
|
|
dscparts.append(evttext)
|
|
|
|
retparts.append(' - '.join(dscparts))
|
2019-05-02 14:21:24 +00:00
|
|
|
msg = evt.get('message')
|
|
|
|
if not msg:
|
|
|
|
msg = ''
|
|
|
|
return ' '.join(retparts) + msg
|
2015-08-11 18:55:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
if deletemode:
|
|
|
|
func = session.delete
|
2019-10-23 20:27:18 +00:00
|
|
|
session.stop_if_noderange_over(noderange, options.maxnodes)
|
2015-08-11 18:55:14 +00:00
|
|
|
else:
|
|
|
|
func = session.read
|
|
|
|
for rsp in func('/noderange/{0}/events/hardware/log'.format(noderange)):
|
|
|
|
if 'error' in rsp:
|
|
|
|
sys.stderr.write(rsp['error'] + '\n')
|
|
|
|
exitcode |= rsp['errorcode']
|
|
|
|
if 'databynode' in rsp:
|
2015-10-19 18:27:11 +00:00
|
|
|
nodedata = rsp['databynode']
|
|
|
|
for node in nodedata:
|
|
|
|
thisdata = nodedata[node]
|
2015-08-11 18:55:14 +00:00
|
|
|
if 'error' in thisdata:
|
|
|
|
sys.stderr.write('{0}: {1}\n'.format(node, thisdata['error']))
|
|
|
|
exitcode |= 1
|
2015-10-19 18:27:11 +00:00
|
|
|
if 'events' in thisdata:
|
2015-08-11 18:55:14 +00:00
|
|
|
evtdata = thisdata['events']
|
|
|
|
for evt in evtdata:
|
2019-07-25 17:09:52 +00:00
|
|
|
print('{0}: {1}'.format(node, format_event(evt)))
|