1#!/usr/bin/env python
2
3r"""
4BMC FFDC will at times include the journal in json format
5(journalctl -o json-pretty ).  This is a quick and dirty script which
6will convert that json output into the standard journalctl output
7"""
8
9import json
10import re
11import time
12from argparse import ArgumentParser
13
14
15def jpretty_to_python(buf):
16    entries = []
17
18    for entry in re.findall(
19            '^{$(.+?)^}$', buf, re.DOTALL | re.MULTILINE):
20        entries += [json.loads('{{{}}}'.format(entry))]
21
22    return entries
23
24
25if __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:
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
44