blob: c0e393de6e406ed4c6d253ec48bf146af26e0c89 [file] [log] [blame]
Matt Spinler4bffa932018-03-28 10:02:50 -05001#!/usr/bin/env python
2
3'''Condenses the error policy table down to only the fields used by
4 the BMC Code.
5
6This script pulls the following 4 fields out of the full JSON policy
7table and arranges them for easier searching:
8
9 Error: The OpenBMC error.
10 e.g. xyz.openbmc_project.Thermal.Error.PowerSupplyHot
11 CommonEventID: Used to index into the online documentation.
12 e.g. FQPSPCA0065M
13 Modifier: Used in combination with the error to locate a
14 table entry.
15 e.g. <an inventory callout>
16 Message: A short message describing the error.
17 e.g. "Power supply 0 is too hot"
18
19There may be multiple CommonEventID/Modifier/Message groups per Error,
20which is why both the error and modifier are needed to find an entry.
21
22Example condensed entry, prettified:
23 {
24 "dtls":[
25 {
26 "CEID":"FQPSPCA0065M",
27 "mod":"/xyz/openbmc_project/inventory/system/ps0",
28 "msg":"Power supply 0 is too hot"
29 },
30 {
31 "CEID":"FQPSPCA0066M",
32 "mod":"/xyz/openbmc_project/inventory/system/ps1",
33 "msg":"Power supply 1 is too hot"
34 }
35 ],
36 "err":"xyz.openbmc_project.Thermal.Error.PowerSupplyHot"
37 }
38'''
39
40import argparse
41import sys
42import json
43
44
45def add_details(error, details, condensed):
46 '''Adds a details entry to an error'''
47
48 found = False
49 for errors in condensed:
50 if errors['err'] == error:
51 errors['dtls'].append(details)
52 found = True
53 break
54
55 if not found:
56 group = {}
57 group['err'] = error
58 group['dtls'] = []
59 group['dtls'].append(details)
60 condensed.append(group)
61
62
63if __name__ == '__main__':
64
65 parser = argparse.ArgumentParser(description='Error log policy condenser')
66
67 parser.add_argument('-p', '--policy',
68 dest='policy_file',
69 default='policyTable.json',
70 help='Policy Table in JSON')
71 parser.add_argument('-c', '--condensed_policy',
72 dest='condensed_file',
73 default='condensed.json',
74 help='Condensed policy output file in JSON')
75 parser.add_argument('-t', '--prettify_json',
76 dest='prettify',
77 default=False,
78 action='store_true',
79 help='Prettify the output JSON')
80
81 args = parser.parse_args()
82
83 with open(args.policy_file, 'r') as table:
84 contents = json.load(table)
85 policytable = contents['events']
86
87 condensed = []
88
89 for name in policytable:
90 details = {}
91
92 #Parse the error||modifer line. The modifier is optional.
93 if '||' in name:
94 error, modifier = name.split('||')
95 details['mod'] = modifier
96 else:
97 error = name
98 details['mod'] = ''
99
100 #The table has some nonBMC errors - they have spaces - skip them
101 if ' ' in error:
102 print("Skipping error %s because of spaces" % error)
103 continue
104
105 details['msg'] = policytable[name]['Message']
106 details['CEID'] = policytable[name]['CommonEventID']
107
108 add_details(error, details, condensed)
109
110 #if prettified there will be newlines
111 indent_value = 2 if args.prettify else None
112
113 with open(args.condensed_file, 'w') as outfile:
114 json.dump(condensed, outfile, separators=(',', ':'),
115 indent=indent_value)