xref: /openbmc/ibm-logging/condense_policy.py (revision 94924b4b)
1#!/usr/bin/env python3
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        separatorPos = name.find('||')
94        if separatorPos != -1:
95            error = name[0:separatorPos]
96            modifier = name[separatorPos + 2:]
97            details['mod'] = modifier
98        else:
99            error = name
100            details['mod'] = ''
101
102        #The table has some nonBMC errors - they have spaces - skip them
103        if ' ' in error:
104            print("Skipping error %s because of spaces" % error)
105            continue
106
107        details['msg'] = policytable[name]['Message']
108        details['CEID'] = policytable[name]['CommonEventID']
109
110        add_details(error, details, condensed)
111
112    #if prettified there will be newlines
113    indent_value = 2 if args.prettify else None
114
115    with open(args.condensed_file, 'w') as outfile:
116        json.dump(condensed, outfile, separators=(',', ':'),
117                  indent=indent_value)
118