blob: 4a7ea4226f1b6d6cb61d7149b565889c524db605 [file] [log] [blame]
Chris Austenb29d2e82016-06-07 12:25:35 -05001#!/usr/bin/python -u
2import sys
3from robot.libraries.BuiltIn import BuiltIn
4import imp
5import string
Rahul Maheshwari757d80c2016-10-17 01:09:55 -05006import random
Michael Walsh8b270ec2017-01-31 12:07:08 -06007import subprocess
8from robot.utils import DotDict
9
Chris Austenb29d2e82016-06-07 12:25:35 -050010
Rahul Maheshwari757d80c2016-10-17 01:09:55 -050011def random_mac():
12 r"""
13 Return random mac address in the following format.
14 Example: 00:01:6C:80:02:78
15 """
16 return ":".join(map(lambda x: "%02x" % x, (random.randint(0x00, 0xff)
17 for _ in range(6))))
18
19def random_ip():
20 r"""
21 Return random ip address in the following format.
22 Example: 9.3.128.100
23 """
24 return ".".join(map(str, (random.randint(0, 255)
25 for _ in range(4))))
Chris Austenb29d2e82016-06-07 12:25:35 -050026
27def get_sensor(module_name, value):
Gunnar Millsbb398ac2016-11-14 11:50:22 -060028 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050029
Gunnar Millsbb398ac2016-11-14 11:50:22 -060030 for i in m.ID_LOOKUP['SENSOR']:
Chris Austenb29d2e82016-06-07 12:25:35 -050031
Gunnar Millsbb398ac2016-11-14 11:50:22 -060032 if m.ID_LOOKUP['SENSOR'][i] == value:
33 return i
Chris Austenb29d2e82016-06-07 12:25:35 -050034
Gunnar Millsbb398ac2016-11-14 11:50:22 -060035 return 0xFF
36
Chris Austenb29d2e82016-06-07 12:25:35 -050037
38def get_inventory_sensor (module_name, value):
Gunnar Millsbb398ac2016-11-14 11:50:22 -060039 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050040
Gunnar Millsbb398ac2016-11-14 11:50:22 -060041 value = string.replace(value, m.INVENTORY_ROOT, '<inventory_root>')
Chris Austenb29d2e82016-06-07 12:25:35 -050042
Gunnar Millsbb398ac2016-11-14 11:50:22 -060043 for i in m.ID_LOOKUP['SENSOR']:
Chris Austenb29d2e82016-06-07 12:25:35 -050044
Gunnar Millsbb398ac2016-11-14 11:50:22 -060045 if m.ID_LOOKUP['SENSOR'][i] == value:
46 return i
Chris Austenb29d2e82016-06-07 12:25:35 -050047
Gunnar Millsbb398ac2016-11-14 11:50:22 -060048 return 0xFF
Chris Austenb29d2e82016-06-07 12:25:35 -050049
50
51################################################################
Gunnar Millsbb398ac2016-11-14 11:50:22 -060052# This will return the URI's of the FRU type
Chris Austenb29d2e82016-06-07 12:25:35 -050053#
54# i.e. get_inventory_list('../data/Palmetto.py')
55#
56# [/org/openbmc/inventory//system/chassis/motherboard/cpu0/core0,
57# /org/openbmc/inventory/system/chassis/motherboard/dimm0]
58################################################################
59def get_inventory_list(module_name):
60
Gunnar Millsbb398ac2016-11-14 11:50:22 -060061 l = []
62 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050063
Gunnar Millsbb398ac2016-11-14 11:50:22 -060064
65 for i in m.ID_LOOKUP['FRU']:
66 s = m.ID_LOOKUP['FRU'][i]
67 s = s.replace('<inventory_root>',m.INVENTORY_ROOT)
68 l.append(s)
69
70 return l
Chris Austenb29d2e82016-06-07 12:25:35 -050071
72
73################################################################
Gunnar Millsbb398ac2016-11-14 11:50:22 -060074# This will return the URI's of the FRU type
Chris Austenb29d2e82016-06-07 12:25:35 -050075#
76# i.e. get_inventory_fru_type_list('../data/Barreleye.py', 'CPU')
77#
78# [/org/openbmc/inventory//system/chassis/motherboard/cpu0,
79# /org/openbmc/inventory//system/chassis/motherboard/cpu1]
80################################################################
81def get_inventory_fru_type_list(module_name, fru):
Gunnar Millsbb398ac2016-11-14 11:50:22 -060082 l = []
83 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050084
Gunnar Millsbb398ac2016-11-14 11:50:22 -060085 for i in m.FRU_INSTANCES.keys():
86 if m.FRU_INSTANCES[i]['fru_type'] == fru:
87 s = i.replace('<inventory_root>',m.INVENTORY_ROOT)
88 l.append(s)
89
90 return l
Chris Austenb29d2e82016-06-07 12:25:35 -050091
92
93################################################################
94# This will return the URI's of the FRU type that contain VPD
95#
96# i.e. get_vpd_inventory_list('../data/Palmetto.py', 'DIMM')
97#
98# [/org/openbmc/inventory/system/chassis/motherboard/dimm0,
99# /org/openbmc/inventory/system/chassis/motherboard/dimm1]
100################################################################
101def get_vpd_inventory_list(module_name, fru):
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600102 l = []
103 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -0500104
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600105 for i in m.ID_LOOKUP['FRU_STR']:
106 x = m.ID_LOOKUP['FRU_STR'][i]
Chris Austenb29d2e82016-06-07 12:25:35 -0500107
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600108 if m.FRU_INSTANCES[x]['fru_type'] == fru:
109 s = x.replace('<inventory_root>',m.INVENTORY_ROOT)
110 l.append(s)
111
112 return l
Chris Austenb29d2e82016-06-07 12:25:35 -0500113
114
115def call_keyword(keyword):
116 return BuiltIn().run_keyword(keyword)
117
118
119def main():
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600120 print get_vpd_inventory_list('../data/Palmetto.py', 'DIMM')
Chris Austenb29d2e82016-06-07 12:25:35 -0500121
122
123if __name__ == "__main__":
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600124 main()
Michael Walsh8b270ec2017-01-31 12:07:08 -0600125
126
Michael Walsh8b270ec2017-01-31 12:07:08 -0600127def get_mtr_report(host=""):
128
129 r"""
130 Get an mtr report and return it as a dictionary of dictionaries.
131
132 The key for the top level dictionary will be the host DNS name. The key
133 for the next level dictionary will be the field of a given row of the
134 report.
135
136 Example result:
137
138 report:
George Keishingfb4b1252017-06-15 00:05:45 -0500139 report[host_dummy-dnsname.com]:
140 report[host_dummy-dnsname.com][row_num]: 1
141 report[host_dummy-dnsname.com][host]: host_dummy-dnsname.com
142 report[host_dummy-dnsname.com][loss]: 0.0
143 report[host_dummy-dnsname.com][snt]: 10
144 report[host_dummy-dnsname.com][last]: 0.2
145 report[host_dummy-dnsname.com][avg]: 3.5
146 report[host_dummy-dnsname.com][best]: 0.2
147 report[host_dummy-dnsname.com][wrst]: 32.5
148 report[host_dummy-dnsname.com][stdev]: 10.2
149 report[bmc-dummy-dnsname.com]:
150 report[bmc-dummy-dnsname.com][row_num]: 2
151 report[bmc-dummy-dnsname.com][host]: bmc-dummy-dnsname.com
152 report[bmc-dummy-dnsname.com][loss]: 0.0
153 report[bmc-dummy-dnsname.com][snt]: 10
154 report[bmc-dummy-dnsname.com][last]: 0.5
155 report[bmc-dummy-dnsname.com][avg]: 0.5
156 report[bmc-dummy-dnsname.com][best]: 0.5
157 report[bmc-dummy-dnsname.com][wrst]: 0.5
158 report[bmc-dummy-dnsname.com][stdev]: 0.0
Michael Walsh8b270ec2017-01-31 12:07:08 -0600159
160 Description of arguments:
161 host The DNS name or IP address to be passed to the mtr command.
162 """
163
164 # Run the mtr command. Exlude the header line. Trim leading space from
165 # each line. Change all multiple spaces delims to single space delims.
166 cmd_buf = "mtr --report " + host +\
167 " | tail -n +2 | sed -r -e 's/^[ ]+//g' -e 's/[ ]+/ /g'"
168 sub_proc = subprocess.Popen(cmd_buf, shell=True, stdout=subprocess.PIPE,
169 stderr=subprocess.STDOUT)
170 out_buf, err_buf = sub_proc.communicate()
171 shell_rc = sub_proc.returncode
172
173 # Split the output by line.
174 rows = out_buf.rstrip('\n').split("\n")
175
176 # Initialize report dictionary.
177 report = DotDict()
178 for row in rows:
179 # Process each row of mtr output.
180 # Create a list of fields by splitting on space delimiter.
181 row_list = row.split(" ")
182 # Create dictionary for the row.
183 row = DotDict()
184 row['row_num'] = row_list[0].rstrip('.')
185 row['host'] = row_list[1]
186 row['loss'] = row_list[2].rstrip('%')
187 row['snt'] = row_list[3]
188 row['last'] = row_list[4]
189 row['avg'] = row_list[5]
190 row['best'] = row_list[6]
191 row['wrst'] = row_list[7]
192 row['stdev'] = row_list[8]
193 report[row['host']] = row
194
195 # Return the full report as dictionary of dictionaries.
196 return report
197
Michael Walsh8b270ec2017-01-31 12:07:08 -0600198
Michael Walsh8b270ec2017-01-31 12:07:08 -0600199def get_mtr_row(host=""):
200
201 r"""
202 Run an mtr report and get a specified row and return it as a dictionary.
203
204 Example result:
205
206 row:
207 row[row_num]: 2
George Keishingfb4b1252017-06-15 00:05:45 -0500208 row[host]: bmc-dummy-dnsname.com
Michael Walsh8b270ec2017-01-31 12:07:08 -0600209 row[loss]: 0.0
210 row[snt]: 10
211 row[last]: 0.5
212 row[avg]: 0.5
213 row[best]: 0.4
214 row[wrst]: 0.7
215 row[stdev]: 0.1
216
217 Description of arguments:
218 host The DNS name or IP address to be passed to the mtr command as
219 well as the indicating which row of the report to return.
220 """
221
222 report = get_mtr_report(host)
223
224 # The max length of host in output is 28 chars.
225 row = [value for key, value in report.items() if host[0:28] in key][0]
226
227 return row
228
George Keishingefa97352017-03-13 07:13:03 -0500229
George Keishingefa97352017-03-13 07:13:03 -0500230def list_to_set(fru_list=""):
231 r"""
232 Pack the list into a set tuple and return.
233
234 It may seem that this function is rather trivial. However, it simplifies
235 the code and improves robot program readability and achieve the result
236 required.
237
238 Example result:
239
240 set(['Version', 'PartNumber', 'SerialNumber', 'FieldReplaceable',
241 'BuildDate', 'Present', 'Manufacturer', 'PrettyName', 'Cached', 'Model'])
242
243 # Description of arguments.
244 fru_list List of FRU's elements.
245 """
246 return set(fru_list)
247
George Keishing81ae45b2017-09-28 14:05:04 -0500248
249def min_list_value(value_list):
250 r"""
251 Returns the element from the list with minimum value.
252 """
253 return min(value_list)