blob: 4014f8d57ef977f8b60f7c82e12265116c482354 [file] [log] [blame]
Andrew Geissler19672b62018-02-07 12:33:02 -06001#!/usr/bin/env python
2
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
18 for entry in re.findall(
19 '^{$(.+?)^}$', buf, re.DOTALL | re.MULTILINE):
Andrew Geissler964ae7b2018-11-14 12:30:01 -060020 entries += [json.loads('{{{}}}'.format(entry))]
Andrew Geissler19672b62018-02-07 12:33:02 -060021
22 return entries
23
Andrew Geissler964ae7b2018-11-14 12:30:01 -060024
Andrew Geissler19672b62018-02-07 12:33:02 -060025if __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 Geissler964ae7b2018-11-14 12:30:01 -060037 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