Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | BMC FFDC will at times include the journal in json format |
| 5 | (journalctl -o json-pretty ). This is a quick and dirty script which |
| 6 | will convert that json output into the standard journalctl output |
| 7 | """ |
| 8 | |
| 9 | import json |
| 10 | import re |
| 11 | import time |
| 12 | from argparse import ArgumentParser |
| 13 | |
| 14 | def jpretty_to_python(buf): |
| 15 | entries = [] |
| 16 | |
| 17 | for entry in re.findall( |
| 18 | '^{$(.+?)^}$', buf, re.DOTALL | re.MULTILINE): |
| 19 | entries += [ json.loads('{{{}}}'.format(entry)) ] |
| 20 | |
| 21 | return entries |
| 22 | |
| 23 | if __name__ == '__main__': |
| 24 | parser = ArgumentParser() |
| 25 | parser.add_argument( |
| 26 | 'journalfile', metavar='FILE', help='the file to parse') |
| 27 | |
| 28 | args = parser.parse_args() |
| 29 | |
| 30 | with open(args.journalfile) as fd: |
| 31 | entries = jpretty_to_python(fd.read()) |
| 32 | entries = sorted(entries, key=lambda k: k['__REALTIME_TIMESTAMP']) |
| 33 | |
| 34 | for e in entries: |
| 35 | e['ts'] = time.ctime(float(e['__REALTIME_TIMESTAMP'])/1000000).rstrip() |
| 36 | try: |
| 37 | print '{ts} {_HOSTNAME} {SYSLOG_IDENTIFIER}: {MESSAGE}'.format(**e) |
| 38 | except: |
| 39 | print "Unable to parse msg: " + str(e) |
| 40 | continue |
| 41 | |