blob: a5aa2225e5c7ce8d56537899f7542222e46dc91a [file] [log] [blame]
Lei YUa03e55e2018-08-03 17:43:25 +08001#!/usr/bin/env python3
2
3import yaml
4import argparse
5
6entityIds = {
7 'dimm': 32,
8 'core': 208,
9 'cpu': 3,
10 'gpu': 216,
11 'gpu_mem': 217,
12 'tpm': 3,
13 'state/host0': 33, # Different interfaces using different entity ID
14 # and this requires extra fix.
15 # {'state/host0', 34},
16 # {'state/host0', 35},
17 'turbo': 3,
18 'fan': 29,
19 'vdd_temp': 218,
20 'power': 10,
21 'voltage': 10,
22 'current': 10,
23 'temperature/pcie': 35,
24 'temperature/ambient': 64,
25 'occ': 210,
26 'control/volatile': 33,
27}
28
29extraIds = {
30 'RebootPolicy': 33,
31 'Progress': 34,
32 'RebootAttempts': 34,
33 'OperatingSystem.Status': 35
34}
35
36
37def openYaml(f):
38 return yaml.load(open(f))
39
40
41def saveYaml(y, f):
42 yaml.dump(y, open(f, "w"))
43
44
45def getEntityId(p, i):
46 for k, v in entityIds.items():
47 if k in p:
48 if k == 'state/host0':
49 # get id from extraIds
50 for ek, ev in extraIds.items():
51 if ek in i:
52 return ev
53 raise Exception("Unable to find entity id:", p, i)
54 else:
55 return v
56 raise Exception('Unable to find entity id:', p)
57
58
59# Global entity instances
60entityInstances = {}
61
62
63def getEntityInstance(id):
64 instanceId = entityInstances.get(id, 0)
65 instanceId = instanceId + 1
66 entityInstances[id] = instanceId
67 print("EntityId:", id, "InstanceId:", instanceId)
68 return instanceId
69
70
71def main():
72 parser = argparse.ArgumentParser(
73 description='Yaml tool for updating ipmi sensor yaml config')
74 parser.add_argument('-i', '--input', required=True, dest='input',
75 help='The ipmi sensor yaml config')
76 parser.add_argument('-o', '--output', required=True, dest='output',
77 help='The output yaml file')
78 parser.add_argument('-r', '--rpt', dest='rpt',
79 help='The .rpt file generated by op-build')
80 parser.add_argument('-e', '--entity', action='store_true',
81 help='Fix entities')
82
83 args = parser.parse_args()
84 args = vars(args)
85
86 if args['input'] is None or args['output'] is None:
87 parser.print_help()
88 exit(1)
89
90# TODO: read rtp and generate the whole config yaml
91# if args['rpt']:
92# pass
93
94 y = openYaml(args['input'])
95
96 if args['entity']:
97 # Fix entities
98 for i in y:
99 path = y[i]['path']
100 intf = list(y[i]['interfaces'].keys())[0]
101 entityId = getEntityId(path, intf)
102 y[i]['entityID'] = entityId
103 y[i]['entityInstance'] = getEntityInstance(entityId)
104 print(y[i]['path'], "id:", entityId,
105 "instance:", y[i]['entityInstance'])
106
107 saveYaml(y, args['output'])
108
109
110if __name__ == "__main__":
111 main()