blob: 7cf4415837b0a6b4d2e18de78b71eb7c774fc191 [file] [log] [blame]
Patrick Williamsb8c5eae2022-12-05 14:35:02 -06001#!/usr/bin/env python3
Andrew Geissler19672b62018-02-07 12:33:02 -06002
3r"""
Nagaraju Gorugantic1a00af2018-11-07 00:52:11 -06004BMC FFDC will at times include the journal in json format
Andrew Geissler19672b62018-02-07 12:33:02 -06005(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
Andrew Geissler964ae7b2018-11-14 12:30:01 -060014
Andrew Geissler19672b62018-02-07 12:33:02 -060015def jpretty_to_python(buf):
16 entries = []
17
Patrick Williamsb8c5eae2022-12-05 14:35:02 -060018 for entry in re.findall("^{$(.+?)^}$", buf, re.DOTALL | re.MULTILINE):
19 entries += [json.loads("{{{}}}".format(entry))]
Andrew Geissler19672b62018-02-07 12:33:02 -060020
21 return entries
22
Andrew Geissler964ae7b2018-11-14 12:30:01 -060023
Patrick Williamsb8c5eae2022-12-05 14:35:02 -060024if __name__ == "__main__":
Andrew Geissler19672b62018-02-07 12:33:02 -060025 parser = ArgumentParser()
26 parser.add_argument(
Patrick Williamsb8c5eae2022-12-05 14:35:02 -060027 "journalfile", metavar="FILE", help="the file to parse"
28 )
Andrew Geissler19672b62018-02-07 12:33:02 -060029
30 args = parser.parse_args()
31
32 with open(args.journalfile) as fd:
33 entries = jpretty_to_python(fd.read())
Patrick Williamsb8c5eae2022-12-05 14:35:02 -060034 entries = sorted(entries, key=lambda k: k["__REALTIME_TIMESTAMP"])
Andrew Geissler19672b62018-02-07 12:33:02 -060035
36 for e in entries:
Patrick Williamsb8c5eae2022-12-05 14:35:02 -060037 e["ts"] = time.ctime(float(e["__REALTIME_TIMESTAMP"]) / 1000000)
Andrew Geissler964ae7b2018-11-14 12:30:01 -060038 try:
Patrick Williamsb8c5eae2022-12-05 14:35:02 -060039 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))
Andrew Geissler964ae7b2018-11-14 12:30:01 -060045 continue