blob: 38f45bd57dcac26b43a2e0df8036c2feaf5e94e3 [file] [log] [blame]
Lei YUa03e55e2018-08-03 17:43:25 +08001#!/usr/bin/env python3
2
3import yaml
4import argparse
5
Lei YUd4051652018-08-06 17:02:00 +08006from typing import NamedTuple
7
8
9class RptSensor(NamedTuple):
10 name: str
11 entityId: int
12 typeId: int
13 evtType: int
14 sensorId: int
15 fru: int
16 targetPath: str
17
18
Lei YUa03e55e2018-08-03 17:43:25 +080019entityIds = {
20 'dimm': 32,
21 'core': 208,
22 'cpu': 3,
Lei YU206f7c42018-08-07 15:12:19 +080023 'occ': 210,
Lei YUa03e55e2018-08-03 17:43:25 +080024 'gpu': 216,
25 'gpu_mem': 217,
26 'tpm': 3,
27 'state/host0': 33, # Different interfaces using different entity ID
28 # and this requires extra fix.
29 # {'state/host0', 34},
30 # {'state/host0', 35},
31 'turbo': 3,
32 'fan': 29,
33 'vdd_temp': 218,
34 'power': 10,
35 'voltage': 10,
36 'current': 10,
37 'temperature/pcie': 35,
38 'temperature/ambient': 64,
Lei YUa03e55e2018-08-03 17:43:25 +080039 'control/volatile': 33,
40}
41
42extraIds = {
43 'RebootPolicy': 33,
44 'Progress': 34,
45 'RebootAttempts': 34,
46 'OperatingSystem.Status': 35
47}
48
49
Lei YU206f7c42018-08-07 15:12:19 +080050sampleDimmTemp = {
51 'bExp': 0,
52 'entityID': 32,
53 'entityInstance': 2,
54 'interfaces': {
55 'xyz.openbmc_project.Sensor.Value': {
56 'Value': {
57 'Offsets': {
58 255: {
59 'type': 'int64_t'
60 }
61 }
62 }
63 }
64 },
65 'multiplierM': 1,
66 'mutability': 'Mutability::Write|Mutability::Read',
67 'offsetB': -127,
68 'path': '/xyz/openbmc_project/sensors/temperature/dimm0_temp',
69 'rExp': 0,
70 'readingType': 'readingData',
71 'scale': -3,
72 'sensorNamePattern': 'nameLeaf',
73 'sensorReadingType': 1,
74 'sensorType': 1,
75 'serviceInterface': 'org.freedesktop.DBus.Properties',
76 'unit': 'xyz.openbmc_project.Sensor.Value.Unit.DegreesC'
77}
78sampleCoreTemp = {
79 'bExp': 0,
80 'entityID': 208,
81 'entityInstance': 2,
82 'interfaces': {
83 'xyz.openbmc_project.Sensor.Value': {
84 'Value': {
85 'Offsets': {
86 255: {
87 'type': 'int64_t'
88 }
89 }
90 }
91 }
92 },
93 'multiplierM': 1,
94 'mutability': 'Mutability::Write|Mutability::Read',
95 'offsetB': -127,
96 'path': '/xyz/openbmc_project/sensors/temperature/p0_core0_temp',
97 'rExp': 0,
98 'readingType': 'readingData',
99 'scale': -3,
100 'sensorNamePattern': 'nameLeaf',
101 'sensorReadingType': 1,
102 'sensorType': 1,
103 'serviceInterface': 'org.freedesktop.DBus.Properties',
104 'unit': 'xyz.openbmc_project.Sensor.Value.Unit.DegreesC'
105}
106
107
Lei YUa03e55e2018-08-03 17:43:25 +0800108def openYaml(f):
109 return yaml.load(open(f))
110
111
112def saveYaml(y, f):
Lei YU206f7c42018-08-07 15:12:19 +0800113 noaliasDumper = yaml.dumper.SafeDumper
114 noaliasDumper.ignore_aliases = lambda self, data: True
115 yaml.dump(y, open(f, "w"), default_flow_style=False, Dumper=noaliasDumper)
Lei YUa03e55e2018-08-03 17:43:25 +0800116
117
118def getEntityId(p, i):
119 for k, v in entityIds.items():
120 if k in p:
121 if k == 'state/host0':
122 # get id from extraIds
123 for ek, ev in extraIds.items():
124 if ek in i:
125 return ev
126 raise Exception("Unable to find entity id:", p, i)
127 else:
128 return v
129 raise Exception('Unable to find entity id:', p)
130
131
132# Global entity instances
133entityInstances = {}
134
135
136def getEntityInstance(id):
137 instanceId = entityInstances.get(id, 0)
138 instanceId = instanceId + 1
139 entityInstances[id] = instanceId
140 print("EntityId:", id, "InstanceId:", instanceId)
141 return instanceId
142
143
Lei YUd4051652018-08-06 17:02:00 +0800144def loadRpt(rptFile):
145 sensors = []
146 with open(rptFile) as f:
147 next(f)
148 next(f)
149 for line in f:
150 fields = line.strip().split('|')
151 fields = list(map(str.strip, fields))
152 sensor = RptSensor(
153 fields[0],
154 int(fields[2], 16) if fields[2] else None,
155 int(fields[3], 16) if fields[3] else None,
156 int(fields[4], 16) if fields[4] else None,
157 int(fields[5], 16) if fields[5] else None,
158 int(fields[7], 16) if fields[7] else None,
159 fields[9])
160 # print(sensor)
161 sensors.append(sensor)
162 return sensors
163
164
Lei YU206f7c42018-08-07 15:12:19 +0800165def getDimmTempPath(p):
166 # Convert path like: /sys-0/node-0/motherboard-0/dimmconn-0/dimm-0
167 # to: /xyz/openbmc_project/sensors/temperature/dimm0_temp
168 import re
169 dimmconn = re.search(r'dimmconn-\d+', p).group()
170 dimmId = re.search(r'\d+', dimmconn).group()
171 return '/xyz/openbmc_project/sensors/temperature/dimm{}_temp'.format(dimmId)
172
173
174def getCoreTempPath(p):
175 # Convert path like: /sys-0/node-0/motherboard-0/proc_socket-0/module-0/p9_proc_s/eq0/ex0/core0
176 # to: /xyz/openbmc_project/sensors/temperature/p0_core0_temp
177 import re
178 splitted = p.split('/')
179 socket = re.search(r'\d+', splitted[4]).group()
180 core = re.search(r'\d+', splitted[9]).group()
181 return '/xyz/openbmc_project/sensors/temperature/p{}_core{}_temp'.format(socket, core)
182
183
184def getDimmTempConfig(s):
185 r = sampleDimmTemp.copy()
186 r['entityInstance'] = getEntityInstance(r['entityID'])
187 r['path'] = getDimmTempPath(s.targetPath)
188 return r
189
190
191def getCoreTempConfig(s):
192 r = sampleCoreTemp.copy()
193 r['entityInstance'] = getEntityInstance(r['entityID'])
194 r['path'] = getCoreTempPath(s.targetPath)
195 return r
196
197
Lei YUa03e55e2018-08-03 17:43:25 +0800198def main():
199 parser = argparse.ArgumentParser(
200 description='Yaml tool for updating ipmi sensor yaml config')
201 parser.add_argument('-i', '--input', required=True, dest='input',
202 help='The ipmi sensor yaml config')
203 parser.add_argument('-o', '--output', required=True, dest='output',
204 help='The output yaml file')
205 parser.add_argument('-r', '--rpt', dest='rpt',
206 help='The .rpt file generated by op-build')
207 parser.add_argument('-e', '--entity', action='store_true',
208 help='Fix entities')
209
210 args = parser.parse_args()
211 args = vars(args)
212
213 if args['input'] is None or args['output'] is None:
214 parser.print_help()
215 exit(1)
216
Lei YUa03e55e2018-08-03 17:43:25 +0800217 y = openYaml(args['input'])
218
Lei YUa03e55e2018-08-03 17:43:25 +0800219 if args['entity']:
220 # Fix entities
221 for i in y:
222 path = y[i]['path']
223 intf = list(y[i]['interfaces'].keys())[0]
224 entityId = getEntityId(path, intf)
225 y[i]['entityID'] = entityId
226 y[i]['entityInstance'] = getEntityInstance(entityId)
227 print(y[i]['path'], "id:", entityId,
228 "instance:", y[i]['entityInstance'])
229
Lei YU206f7c42018-08-07 15:12:19 +0800230 sensorIds = list(y.keys())
231 if args['rpt']:
232 rptSensors = loadRpt(args['rpt'])
233 for s in rptSensors:
234 if s.sensorId is not None and s.sensorId not in sensorIds:
235 print("Sensor ID", s.sensorId, "not in yaml:",
236 s.name, ", path:", s.targetPath)
237 if 'temp' in s.name.lower():
238 if 'dimm' in s.targetPath.lower():
239 y[s.sensorId] = getDimmTempConfig(s)
240 print('Added sensor id:', s.sensorId,
241 ', path:', y[s.sensorId]['path'])
242 if 'core' in s.targetPath.lower():
243 y[s.sensorId] = getCoreTempConfig(s)
244 print('Added sensor id:', s.sensorId,
245 ', path:', y[s.sensorId]['path'])
246
Lei YUa03e55e2018-08-03 17:43:25 +0800247 saveYaml(y, args['output'])
248
249
250if __name__ == "__main__":
251 main()