blob: 65a0a3b133f97bcbbb64a6939c95c318600b75d6 [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 YU206f7c42018-08-07 15:12:19 +080019sampleDimmTemp = {
20 'bExp': 0,
21 'entityID': 32,
22 'entityInstance': 2,
23 'interfaces': {
24 'xyz.openbmc_project.Sensor.Value': {
25 'Value': {
26 'Offsets': {
27 255: {
28 'type': 'int64_t'
29 }
30 }
31 }
32 }
33 },
34 'multiplierM': 1,
35 'mutability': 'Mutability::Write|Mutability::Read',
36 'offsetB': -127,
37 'path': '/xyz/openbmc_project/sensors/temperature/dimm0_temp',
38 'rExp': 0,
39 'readingType': 'readingData',
40 'scale': -3,
41 'sensorNamePattern': 'nameLeaf',
42 'sensorReadingType': 1,
43 'sensorType': 1,
44 'serviceInterface': 'org.freedesktop.DBus.Properties',
45 'unit': 'xyz.openbmc_project.Sensor.Value.Unit.DegreesC'
46}
47sampleCoreTemp = {
48 'bExp': 0,
49 'entityID': 208,
50 'entityInstance': 2,
51 'interfaces': {
52 'xyz.openbmc_project.Sensor.Value': {
53 'Value': {
54 'Offsets': {
55 255: {
56 'type': 'int64_t'
57 }
58 }
59 }
60 }
61 },
62 'multiplierM': 1,
63 'mutability': 'Mutability::Write|Mutability::Read',
64 'offsetB': -127,
65 'path': '/xyz/openbmc_project/sensors/temperature/p0_core0_temp',
66 'rExp': 0,
67 'readingType': 'readingData',
68 'scale': -3,
69 'sensorNamePattern': 'nameLeaf',
70 'sensorReadingType': 1,
71 'sensorType': 1,
72 'serviceInterface': 'org.freedesktop.DBus.Properties',
73 'unit': 'xyz.openbmc_project.Sensor.Value.Unit.DegreesC'
74}
75
76
Lei YUa03e55e2018-08-03 17:43:25 +080077def openYaml(f):
78 return yaml.load(open(f))
79
80
Lei YUdf0c4172018-08-08 10:42:08 +080081def saveYaml(y, f, safe=True):
82 if safe:
83 noaliasDumper = yaml.dumper.SafeDumper
84 noaliasDumper.ignore_aliases = lambda self, data: True
85 yaml.dump(y, open(f, "w"), default_flow_style=False,
86 Dumper=noaliasDumper)
87 else:
88 yaml.dump(y, open(f, "w"))
Lei YUa03e55e2018-08-03 17:43:25 +080089
90
Lei YU9f394032018-08-08 11:25:21 +080091def getEntityIdAndNamePattern(p, intfs, m):
92 key = (p, intfs)
93 match = m.get(key, None)
94 if match is None:
95 raise Exception('Unable to find sensor', key, 'from map')
96 return (m[key]['entityID'], m[key]['sensorNamePattern'])
Lei YUa03e55e2018-08-03 17:43:25 +080097
98
99# Global entity instances
100entityInstances = {}
101
102
103def getEntityInstance(id):
104 instanceId = entityInstances.get(id, 0)
105 instanceId = instanceId + 1
106 entityInstances[id] = instanceId
107 print("EntityId:", id, "InstanceId:", instanceId)
108 return instanceId
109
110
Lei YUd4051652018-08-06 17:02:00 +0800111def loadRpt(rptFile):
112 sensors = []
113 with open(rptFile) as f:
114 next(f)
115 next(f)
116 for line in f:
117 fields = line.strip().split('|')
118 fields = list(map(str.strip, fields))
119 sensor = RptSensor(
120 fields[0],
121 int(fields[2], 16) if fields[2] else None,
122 int(fields[3], 16) if fields[3] else None,
123 int(fields[4], 16) if fields[4] else None,
124 int(fields[5], 16) if fields[5] else None,
125 int(fields[7], 16) if fields[7] else None,
126 fields[9])
127 # print(sensor)
128 sensors.append(sensor)
129 return sensors
130
131
Lei YU206f7c42018-08-07 15:12:19 +0800132def getDimmTempPath(p):
133 # Convert path like: /sys-0/node-0/motherboard-0/dimmconn-0/dimm-0
134 # to: /xyz/openbmc_project/sensors/temperature/dimm0_temp
135 import re
136 dimmconn = re.search(r'dimmconn-\d+', p).group()
137 dimmId = re.search(r'\d+', dimmconn).group()
138 return '/xyz/openbmc_project/sensors/temperature/dimm{}_temp'.format(dimmId)
139
140
141def getCoreTempPath(p):
142 # Convert path like: /sys-0/node-0/motherboard-0/proc_socket-0/module-0/p9_proc_s/eq0/ex0/core0
143 # to: /xyz/openbmc_project/sensors/temperature/p0_core0_temp
144 import re
145 splitted = p.split('/')
146 socket = re.search(r'\d+', splitted[4]).group()
147 core = re.search(r'\d+', splitted[9]).group()
148 return '/xyz/openbmc_project/sensors/temperature/p{}_core{}_temp'.format(socket, core)
149
150
151def getDimmTempConfig(s):
152 r = sampleDimmTemp.copy()
153 r['entityInstance'] = getEntityInstance(r['entityID'])
154 r['path'] = getDimmTempPath(s.targetPath)
155 return r
156
157
158def getCoreTempConfig(s):
159 r = sampleCoreTemp.copy()
160 r['entityInstance'] = getEntityInstance(r['entityID'])
161 r['path'] = getCoreTempPath(s.targetPath)
162 return r
163
164
Lei YUa03e55e2018-08-03 17:43:25 +0800165def main():
166 parser = argparse.ArgumentParser(
167 description='Yaml tool for updating ipmi sensor yaml config')
168 parser.add_argument('-i', '--input', required=True, dest='input',
169 help='The ipmi sensor yaml config')
170 parser.add_argument('-o', '--output', required=True, dest='output',
171 help='The output yaml file')
Lei YU9f394032018-08-08 11:25:21 +0800172 parser.add_argument('-m', '--map', dest='map', default='sensor_map.yaml',
173 help='The sample map yaml file')
Lei YUa03e55e2018-08-03 17:43:25 +0800174 parser.add_argument('-r', '--rpt', dest='rpt',
175 help='The .rpt file generated by op-build')
Lei YU9f394032018-08-08 11:25:21 +0800176 parser.add_argument('-f', '--fix', action='store_true',
177 help='Fix entities and sensorNamePattern')
Lei YUdf0c4172018-08-08 10:42:08 +0800178 parser.add_argument('-g', '--generate', action='store_true',
179 help='Generate maps for entityID and sensorNamePattern')
Lei YUa03e55e2018-08-03 17:43:25 +0800180
181 args = parser.parse_args()
182 args = vars(args)
183
184 if args['input'] is None or args['output'] is None:
185 parser.print_help()
186 exit(1)
187
Lei YUa03e55e2018-08-03 17:43:25 +0800188 y = openYaml(args['input'])
189
Lei YU9f394032018-08-08 11:25:21 +0800190 if args['fix']:
191 # Fix entities and sensorNamePattern
192 m = openYaml(args['map'])
193
Lei YUa03e55e2018-08-03 17:43:25 +0800194 for i in y:
195 path = y[i]['path']
Lei YU9f394032018-08-08 11:25:21 +0800196 intfs = tuple(sorted(list(y[i]['interfaces'].keys())))
197 entityId, namePattern = getEntityIdAndNamePattern(path, intfs, m)
Lei YUa03e55e2018-08-03 17:43:25 +0800198 y[i]['entityID'] = entityId
199 y[i]['entityInstance'] = getEntityInstance(entityId)
Lei YU9f394032018-08-08 11:25:21 +0800200 y[i]['sensorNamePattern'] = namePattern
Lei YUa03e55e2018-08-03 17:43:25 +0800201 print(y[i]['path'], "id:", entityId,
202 "instance:", y[i]['entityInstance'])
203
Lei YU206f7c42018-08-07 15:12:19 +0800204 sensorIds = list(y.keys())
205 if args['rpt']:
206 rptSensors = loadRpt(args['rpt'])
207 for s in rptSensors:
208 if s.sensorId is not None and s.sensorId not in sensorIds:
209 print("Sensor ID", s.sensorId, "not in yaml:",
210 s.name, ", path:", s.targetPath)
211 if 'temp' in s.name.lower():
212 if 'dimm' in s.targetPath.lower():
213 y[s.sensorId] = getDimmTempConfig(s)
214 print('Added sensor id:', s.sensorId,
215 ', path:', y[s.sensorId]['path'])
216 if 'core' in s.targetPath.lower():
217 y[s.sensorId] = getCoreTempConfig(s)
218 print('Added sensor id:', s.sensorId,
219 ', path:', y[s.sensorId]['path'])
220
Lei YUdf0c4172018-08-08 10:42:08 +0800221 if args['generate']:
222 m = {}
223 for i in y:
224 path = y[i]['path']
225 intfs = tuple(sorted(list(y[i]['interfaces'].keys())))
226 entityId = y[i]['entityID']
227 sensorNamePattern = y[i]['sensorNamePattern']
228 m[(path, intfs)] = {'entityID': entityId,
229 'sensorNamePattern': sensorNamePattern}
230 y = m
231
232 safe = False if args['generate'] else True
233
234 saveYaml(y, args['output'], safe)
Lei YUa03e55e2018-08-03 17:43:25 +0800235
236
237if __name__ == "__main__":
238 main()