Lei YU | a03e55e | 2018-08-03 17:43:25 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import yaml |
| 4 | import argparse |
| 5 | |
Lei YU | d405165 | 2018-08-06 17:02:00 +0800 | [diff] [blame^] | 6 | from typing import NamedTuple |
| 7 | |
| 8 | |
| 9 | class 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 YU | a03e55e | 2018-08-03 17:43:25 +0800 | [diff] [blame] | 19 | entityIds = { |
| 20 | 'dimm': 32, |
| 21 | 'core': 208, |
| 22 | 'cpu': 3, |
| 23 | 'gpu': 216, |
| 24 | 'gpu_mem': 217, |
| 25 | 'tpm': 3, |
| 26 | 'state/host0': 33, # Different interfaces using different entity ID |
| 27 | # and this requires extra fix. |
| 28 | # {'state/host0', 34}, |
| 29 | # {'state/host0', 35}, |
| 30 | 'turbo': 3, |
| 31 | 'fan': 29, |
| 32 | 'vdd_temp': 218, |
| 33 | 'power': 10, |
| 34 | 'voltage': 10, |
| 35 | 'current': 10, |
| 36 | 'temperature/pcie': 35, |
| 37 | 'temperature/ambient': 64, |
| 38 | 'occ': 210, |
| 39 | 'control/volatile': 33, |
| 40 | } |
| 41 | |
| 42 | extraIds = { |
| 43 | 'RebootPolicy': 33, |
| 44 | 'Progress': 34, |
| 45 | 'RebootAttempts': 34, |
| 46 | 'OperatingSystem.Status': 35 |
| 47 | } |
| 48 | |
| 49 | |
| 50 | def openYaml(f): |
| 51 | return yaml.load(open(f)) |
| 52 | |
| 53 | |
| 54 | def saveYaml(y, f): |
| 55 | yaml.dump(y, open(f, "w")) |
| 56 | |
| 57 | |
| 58 | def getEntityId(p, i): |
| 59 | for k, v in entityIds.items(): |
| 60 | if k in p: |
| 61 | if k == 'state/host0': |
| 62 | # get id from extraIds |
| 63 | for ek, ev in extraIds.items(): |
| 64 | if ek in i: |
| 65 | return ev |
| 66 | raise Exception("Unable to find entity id:", p, i) |
| 67 | else: |
| 68 | return v |
| 69 | raise Exception('Unable to find entity id:', p) |
| 70 | |
| 71 | |
| 72 | # Global entity instances |
| 73 | entityInstances = {} |
| 74 | |
| 75 | |
| 76 | def getEntityInstance(id): |
| 77 | instanceId = entityInstances.get(id, 0) |
| 78 | instanceId = instanceId + 1 |
| 79 | entityInstances[id] = instanceId |
| 80 | print("EntityId:", id, "InstanceId:", instanceId) |
| 81 | return instanceId |
| 82 | |
| 83 | |
Lei YU | d405165 | 2018-08-06 17:02:00 +0800 | [diff] [blame^] | 84 | def loadRpt(rptFile): |
| 85 | sensors = [] |
| 86 | with open(rptFile) as f: |
| 87 | next(f) |
| 88 | next(f) |
| 89 | for line in f: |
| 90 | fields = line.strip().split('|') |
| 91 | fields = list(map(str.strip, fields)) |
| 92 | sensor = RptSensor( |
| 93 | fields[0], |
| 94 | int(fields[2], 16) if fields[2] else None, |
| 95 | int(fields[3], 16) if fields[3] else None, |
| 96 | int(fields[4], 16) if fields[4] else None, |
| 97 | int(fields[5], 16) if fields[5] else None, |
| 98 | int(fields[7], 16) if fields[7] else None, |
| 99 | fields[9]) |
| 100 | # print(sensor) |
| 101 | sensors.append(sensor) |
| 102 | return sensors |
| 103 | |
| 104 | |
Lei YU | a03e55e | 2018-08-03 17:43:25 +0800 | [diff] [blame] | 105 | def main(): |
| 106 | parser = argparse.ArgumentParser( |
| 107 | description='Yaml tool for updating ipmi sensor yaml config') |
| 108 | parser.add_argument('-i', '--input', required=True, dest='input', |
| 109 | help='The ipmi sensor yaml config') |
| 110 | parser.add_argument('-o', '--output', required=True, dest='output', |
| 111 | help='The output yaml file') |
| 112 | parser.add_argument('-r', '--rpt', dest='rpt', |
| 113 | help='The .rpt file generated by op-build') |
| 114 | parser.add_argument('-e', '--entity', action='store_true', |
| 115 | help='Fix entities') |
| 116 | |
| 117 | args = parser.parse_args() |
| 118 | args = vars(args) |
| 119 | |
| 120 | if args['input'] is None or args['output'] is None: |
| 121 | parser.print_help() |
| 122 | exit(1) |
| 123 | |
Lei YU | a03e55e | 2018-08-03 17:43:25 +0800 | [diff] [blame] | 124 | y = openYaml(args['input']) |
| 125 | |
Lei YU | d405165 | 2018-08-06 17:02:00 +0800 | [diff] [blame^] | 126 | sensorIds = list(y.keys()) |
| 127 | if args['rpt']: |
| 128 | rptSensors = loadRpt(args['rpt']) |
| 129 | for s in rptSensors: |
| 130 | if s.sensorId is not None and s.sensorId not in sensorIds: |
| 131 | print("Sensor ID:", s.sensorId, |
| 132 | " not in yaml, path", s.targetPath) |
| 133 | # TODO: add new items in yaml with missing sensors |
| 134 | return |
| 135 | |
Lei YU | a03e55e | 2018-08-03 17:43:25 +0800 | [diff] [blame] | 136 | if args['entity']: |
| 137 | # Fix entities |
| 138 | for i in y: |
| 139 | path = y[i]['path'] |
| 140 | intf = list(y[i]['interfaces'].keys())[0] |
| 141 | entityId = getEntityId(path, intf) |
| 142 | y[i]['entityID'] = entityId |
| 143 | y[i]['entityInstance'] = getEntityInstance(entityId) |
| 144 | print(y[i]['path'], "id:", entityId, |
| 145 | "instance:", y[i]['entityInstance']) |
| 146 | |
| 147 | saveYaml(y, args['output']) |
| 148 | |
| 149 | |
| 150 | if __name__ == "__main__": |
| 151 | main() |