Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4 | BMC FFDC will at times include the journal in json format |
Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 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 | |
Andrew Geissler | 964ae7b | 2018-11-14 12:30:01 -0600 | [diff] [blame] | 14 | |
Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 15 | def jpretty_to_python(buf): |
| 16 | entries = [] |
| 17 | |
| 18 | for entry in re.findall( |
| 19 | '^{$(.+?)^}$', buf, re.DOTALL | re.MULTILINE): |
Andrew Geissler | 964ae7b | 2018-11-14 12:30:01 -0600 | [diff] [blame] | 20 | entries += [json.loads('{{{}}}'.format(entry))] |
Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 21 | |
| 22 | return entries |
| 23 | |
Andrew Geissler | 964ae7b | 2018-11-14 12:30:01 -0600 | [diff] [blame] | 24 | |
Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 25 | if __name__ == '__main__': |
| 26 | parser = ArgumentParser() |
| 27 | parser.add_argument( |
| 28 | 'journalfile', metavar='FILE', help='the file to parse') |
| 29 | |
| 30 | args = parser.parse_args() |
| 31 | |
| 32 | with open(args.journalfile) as fd: |
| 33 | entries = jpretty_to_python(fd.read()) |
| 34 | entries = sorted(entries, key=lambda k: k['__REALTIME_TIMESTAMP']) |
| 35 | |
| 36 | for e in entries: |
Andrew Geissler | 964ae7b | 2018-11-14 12:30:01 -0600 | [diff] [blame] | 37 | e['ts'] = time.ctime( |
| 38 | float(e['__REALTIME_TIMESTAMP']) / 1000000).rstrip() |
| 39 | try: |
| 40 | print ('{ts} {_HOSTNAME} {SYSLOG_IDENTIFIER}: {MESSAGE}'.format(**e)) |
| 41 | except: |
| 42 | print ("Unable to parse msg: " + str(e)) |
| 43 | continue |