Patrick Williams | b8c5eae | 2022-12-05 14:35:02 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 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 | |
Patrick Williams | b8c5eae | 2022-12-05 14:35:02 -0600 | [diff] [blame] | 18 | for entry in re.findall("^{$(.+?)^}$", buf, re.DOTALL | re.MULTILINE): |
| 19 | entries += [json.loads("{{{}}}".format(entry))] |
Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 20 | |
| 21 | return entries |
| 22 | |
Andrew Geissler | 964ae7b | 2018-11-14 12:30:01 -0600 | [diff] [blame] | 23 | |
Patrick Williams | b8c5eae | 2022-12-05 14:35:02 -0600 | [diff] [blame] | 24 | if __name__ == "__main__": |
Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 25 | parser = ArgumentParser() |
| 26 | parser.add_argument( |
Patrick Williams | b8c5eae | 2022-12-05 14:35:02 -0600 | [diff] [blame] | 27 | "journalfile", metavar="FILE", help="the file to parse" |
| 28 | ) |
Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 29 | |
| 30 | args = parser.parse_args() |
| 31 | |
| 32 | with open(args.journalfile) as fd: |
| 33 | entries = jpretty_to_python(fd.read()) |
Patrick Williams | b8c5eae | 2022-12-05 14:35:02 -0600 | [diff] [blame] | 34 | entries = sorted(entries, key=lambda k: k["__REALTIME_TIMESTAMP"]) |
Andrew Geissler | 19672b6 | 2018-02-07 12:33:02 -0600 | [diff] [blame] | 35 | |
| 36 | for e in entries: |
Patrick Williams | b8c5eae | 2022-12-05 14:35:02 -0600 | [diff] [blame] | 37 | e["ts"] = time.ctime(float(e["__REALTIME_TIMESTAMP"]) / 1000000) |
Andrew Geissler | 964ae7b | 2018-11-14 12:30:01 -0600 | [diff] [blame] | 38 | try: |
Patrick Williams | b8c5eae | 2022-12-05 14:35:02 -0600 | [diff] [blame] | 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)) |
Andrew Geissler | 964ae7b | 2018-11-14 12:30:01 -0600 | [diff] [blame] | 45 | continue |