1#!/usr/bin/env python3
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("^{$(.+?)^}$", buf, re.DOTALL | re.MULTILINE):
19        entries += [json.loads("{{{}}}".format(entry))]
20
21    return entries
22
23
24if __name__ == "__main__":
25    parser = ArgumentParser()
26    parser.add_argument(
27        "journalfile", metavar="FILE", help="the file to parse"
28    )
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(float(e["__REALTIME_TIMESTAMP"]) / 1000000)
38            try:
39                print(
40                    f'{e["ts"]} {e["_HOSTNAME"]} {e["SYSLOG_IDENTIFIER"]}:'
41                    f' {e["MESSAGE"]}'
42                )
43            except Exception:
44                print("Unable to parse msg: " + str(e))
45                continue
46