George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 -u |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | Generic utility functions. |
| 5 | """ |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 6 | import imp |
| 7 | import string |
Rahul Maheshwari | 757d80c | 2016-10-17 01:09:55 -0500 | [diff] [blame] | 8 | import random |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 9 | import subprocess |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 10 | from robot.libraries.BuiltIn import BuiltIn |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 11 | from robot.utils import DotDict |
| 12 | |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 13 | |
Rahul Maheshwari | 757d80c | 2016-10-17 01:09:55 -0500 | [diff] [blame] | 14 | def random_mac(): |
| 15 | r""" |
| 16 | Return random mac address in the following format. |
| 17 | Example: 00:01:6C:80:02:78 |
| 18 | """ |
| 19 | return ":".join(map(lambda x: "%02x" % x, (random.randint(0x00, 0xff) |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 20 | for _ in range(6)))) |
| 21 | |
Rahul Maheshwari | 757d80c | 2016-10-17 01:09:55 -0500 | [diff] [blame] | 22 | |
| 23 | def random_ip(): |
| 24 | r""" |
| 25 | Return random ip address in the following format. |
| 26 | Example: 9.3.128.100 |
| 27 | """ |
| 28 | return ".".join(map(str, (random.randint(0, 255) |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 29 | for _ in range(4)))) |
| 30 | |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 31 | |
| 32 | def get_sensor(module_name, value): |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 33 | r""" |
| 34 | Return sensor matched ID name. |
| 35 | """ |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 36 | m = imp.load_source('module.name', module_name) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 37 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 38 | for i in m.ID_LOOKUP['SENSOR']: |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 39 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 40 | if m.ID_LOOKUP['SENSOR'][i] == value: |
| 41 | return i |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 42 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 43 | return 0xFF |
| 44 | |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 45 | |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 46 | def get_inventory_sensor(module_name, value): |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 47 | r""" |
| 48 | Return sensor matched ID name from inventory. |
| 49 | """ |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 50 | m = imp.load_source('module.name', module_name) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 51 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 52 | value = string.replace(value, m.INVENTORY_ROOT, '<inventory_root>') |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 53 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 54 | for i in m.ID_LOOKUP['SENSOR']: |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 55 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 56 | if m.ID_LOOKUP['SENSOR'][i] == value: |
| 57 | return i |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 58 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 59 | return 0xFF |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 60 | |
| 61 | |
| 62 | ################################################################ |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 63 | # This will return the URI's of the FRU type |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 64 | # |
| 65 | # i.e. get_inventory_list('../data/Palmetto.py') |
| 66 | # |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 67 | # [/org/openbmc/inventory/system/chassis/motherboard/cpu0/core0, |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 68 | # /org/openbmc/inventory/system/chassis/motherboard/dimm0] |
| 69 | ################################################################ |
| 70 | def get_inventory_list(module_name): |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 71 | r""" |
| 72 | Return all FRU URI(s) list available from inventory. |
| 73 | """ |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 74 | |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 75 | inventory_list = [] |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 76 | m = imp.load_source('module.name', module_name) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 77 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 78 | for i in m.ID_LOOKUP['FRU']: |
| 79 | s = m.ID_LOOKUP['FRU'][i] |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 80 | s = s.replace('<inventory_root>', m.INVENTORY_ROOT) |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 81 | inventory_list.append(s) |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 82 | |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 83 | return inventory_list |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 84 | |
| 85 | |
| 86 | ################################################################ |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 87 | # This will return the URI's of the FRU type |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 88 | # |
George Keishing | be3cdfd | 2018-08-28 00:00:06 -0500 | [diff] [blame] | 89 | # i.e. get_inventory_fru_type_list('../data/Witherspoon.py', 'CPU') |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 90 | # |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 91 | # [/org/openbmc/inventory/system/chassis/motherboard/cpu0, |
| 92 | # /org/openbmc/inventory/system/chassis/motherboard/cpu1] |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 93 | ################################################################ |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 94 | def get_inventory_fru_type_list(module_name, fru): |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 95 | r""" |
| 96 | Return FRU URI(s) list of a given type from inventory. |
| 97 | """ |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 98 | inventory_list = [] |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 99 | m = imp.load_source('module.name', module_name) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 100 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 101 | for i in m.FRU_INSTANCES.keys(): |
| 102 | if m.FRU_INSTANCES[i]['fru_type'] == fru: |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 103 | s = i.replace('<inventory_root>', m.INVENTORY_ROOT) |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 104 | inventory_list.append(s) |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 105 | |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 106 | return inventory_list |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 107 | |
| 108 | |
| 109 | ################################################################ |
| 110 | # This will return the URI's of the FRU type that contain VPD |
| 111 | # |
| 112 | # i.e. get_vpd_inventory_list('../data/Palmetto.py', 'DIMM') |
| 113 | # |
| 114 | # [/org/openbmc/inventory/system/chassis/motherboard/dimm0, |
| 115 | # /org/openbmc/inventory/system/chassis/motherboard/dimm1] |
| 116 | ################################################################ |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 117 | def get_vpd_inventory_list(module_name, fru): |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 118 | r""" |
| 119 | Return VPD URI(s) list of a FRU type from inventory. |
| 120 | """ |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 121 | inventory_list = [] |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 122 | m = imp.load_source('module.name', module_name) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 123 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 124 | for i in m.ID_LOOKUP['FRU_STR']: |
| 125 | x = m.ID_LOOKUP['FRU_STR'][i] |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 126 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 127 | if m.FRU_INSTANCES[x]['fru_type'] == fru: |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 128 | s = x.replace('<inventory_root>', m.INVENTORY_ROOT) |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 129 | inventory_list.append(s) |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 130 | |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 131 | return inventory_list |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 132 | |
| 133 | |
| 134 | def call_keyword(keyword): |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 135 | r""" |
| 136 | Return result of the execute robot keyword. |
| 137 | """ |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 138 | return BuiltIn().run_keyword(keyword) |
| 139 | |
| 140 | |
| 141 | def main(): |
George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 142 | r""" |
| 143 | Python main func call. |
| 144 | """ |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 145 | print(get_vpd_inventory_list('../data/Palmetto.py', 'DIMM')) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 146 | |
| 147 | |
| 148 | if __name__ == "__main__": |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 149 | main() |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 150 | |
| 151 | |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 152 | def get_mtr_report(host=""): |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 153 | r""" |
| 154 | Get an mtr report and return it as a dictionary of dictionaries. |
| 155 | |
| 156 | The key for the top level dictionary will be the host DNS name. The key |
| 157 | for the next level dictionary will be the field of a given row of the |
| 158 | report. |
| 159 | |
| 160 | Example result: |
| 161 | |
| 162 | report: |
George Keishing | fb4b125 | 2017-06-15 00:05:45 -0500 | [diff] [blame] | 163 | report[host_dummy-dnsname.com]: |
| 164 | report[host_dummy-dnsname.com][row_num]: 1 |
| 165 | report[host_dummy-dnsname.com][host]: host_dummy-dnsname.com |
| 166 | report[host_dummy-dnsname.com][loss]: 0.0 |
| 167 | report[host_dummy-dnsname.com][snt]: 10 |
| 168 | report[host_dummy-dnsname.com][last]: 0.2 |
| 169 | report[host_dummy-dnsname.com][avg]: 3.5 |
| 170 | report[host_dummy-dnsname.com][best]: 0.2 |
| 171 | report[host_dummy-dnsname.com][wrst]: 32.5 |
| 172 | report[host_dummy-dnsname.com][stdev]: 10.2 |
| 173 | report[bmc-dummy-dnsname.com]: |
| 174 | report[bmc-dummy-dnsname.com][row_num]: 2 |
| 175 | report[bmc-dummy-dnsname.com][host]: bmc-dummy-dnsname.com |
| 176 | report[bmc-dummy-dnsname.com][loss]: 0.0 |
| 177 | report[bmc-dummy-dnsname.com][snt]: 10 |
| 178 | report[bmc-dummy-dnsname.com][last]: 0.5 |
| 179 | report[bmc-dummy-dnsname.com][avg]: 0.5 |
| 180 | report[bmc-dummy-dnsname.com][best]: 0.5 |
| 181 | report[bmc-dummy-dnsname.com][wrst]: 0.5 |
| 182 | report[bmc-dummy-dnsname.com][stdev]: 0.0 |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 183 | |
| 184 | Description of arguments: |
| 185 | host The DNS name or IP address to be passed to the mtr command. |
| 186 | """ |
| 187 | |
Gunnar Mills | acc7c56 | 2019-08-20 13:12:46 -0500 | [diff] [blame] | 188 | # Run the mtr command. Exclude the header line. Trim leading space from |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 189 | # each line. Change all multiple spaces delims to single space delims. |
| 190 | cmd_buf = "mtr --report " + host +\ |
| 191 | " | tail -n +2 | sed -r -e 's/^[ ]+//g' -e 's/[ ]+/ /g'" |
| 192 | sub_proc = subprocess.Popen(cmd_buf, shell=True, stdout=subprocess.PIPE, |
| 193 | stderr=subprocess.STDOUT) |
| 194 | out_buf, err_buf = sub_proc.communicate() |
| 195 | shell_rc = sub_proc.returncode |
| 196 | |
| 197 | # Split the output by line. |
| 198 | rows = out_buf.rstrip('\n').split("\n") |
| 199 | |
| 200 | # Initialize report dictionary. |
| 201 | report = DotDict() |
| 202 | for row in rows: |
| 203 | # Process each row of mtr output. |
| 204 | # Create a list of fields by splitting on space delimiter. |
| 205 | row_list = row.split(" ") |
| 206 | # Create dictionary for the row. |
| 207 | row = DotDict() |
| 208 | row['row_num'] = row_list[0].rstrip('.') |
| 209 | row['host'] = row_list[1] |
| 210 | row['loss'] = row_list[2].rstrip('%') |
| 211 | row['snt'] = row_list[3] |
| 212 | row['last'] = row_list[4] |
| 213 | row['avg'] = row_list[5] |
| 214 | row['best'] = row_list[6] |
| 215 | row['wrst'] = row_list[7] |
| 216 | row['stdev'] = row_list[8] |
| 217 | report[row['host']] = row |
| 218 | |
| 219 | # Return the full report as dictionary of dictionaries. |
| 220 | return report |
| 221 | |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 222 | |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 223 | def get_mtr_row(host=""): |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 224 | r""" |
| 225 | Run an mtr report and get a specified row and return it as a dictionary. |
| 226 | |
| 227 | Example result: |
| 228 | |
| 229 | row: |
| 230 | row[row_num]: 2 |
George Keishing | fb4b125 | 2017-06-15 00:05:45 -0500 | [diff] [blame] | 231 | row[host]: bmc-dummy-dnsname.com |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 232 | row[loss]: 0.0 |
| 233 | row[snt]: 10 |
| 234 | row[last]: 0.5 |
| 235 | row[avg]: 0.5 |
| 236 | row[best]: 0.4 |
| 237 | row[wrst]: 0.7 |
| 238 | row[stdev]: 0.1 |
| 239 | |
| 240 | Description of arguments: |
| 241 | host The DNS name or IP address to be passed to the mtr command as |
| 242 | well as the indicating which row of the report to return. |
| 243 | """ |
| 244 | |
| 245 | report = get_mtr_report(host) |
| 246 | |
| 247 | # The max length of host in output is 28 chars. |
| 248 | row = [value for key, value in report.items() if host[0:28] in key][0] |
| 249 | |
| 250 | return row |
| 251 | |
George Keishing | efa9735 | 2017-03-13 07:13:03 -0500 | [diff] [blame] | 252 | |
George Keishing | efa9735 | 2017-03-13 07:13:03 -0500 | [diff] [blame] | 253 | def list_to_set(fru_list=""): |
| 254 | r""" |
| 255 | Pack the list into a set tuple and return. |
| 256 | |
| 257 | It may seem that this function is rather trivial. However, it simplifies |
| 258 | the code and improves robot program readability and achieve the result |
| 259 | required. |
| 260 | |
| 261 | Example result: |
| 262 | |
| 263 | set(['Version', 'PartNumber', 'SerialNumber', 'FieldReplaceable', |
| 264 | 'BuildDate', 'Present', 'Manufacturer', 'PrettyName', 'Cached', 'Model']) |
| 265 | |
| 266 | # Description of arguments. |
| 267 | fru_list List of FRU's elements. |
| 268 | """ |
| 269 | return set(fru_list) |
| 270 | |
George Keishing | 81ae45b | 2017-09-28 14:05:04 -0500 | [diff] [blame] | 271 | |
| 272 | def min_list_value(value_list): |
| 273 | r""" |
| 274 | Returns the element from the list with minimum value. |
| 275 | """ |
| 276 | return min(value_list) |
nagarjunb22 | 87138e6 | 2022-04-19 16:49:16 +0530 | [diff] [blame] | 277 | |
| 278 | |
| 279 | def convert_lsb_to_msb(string): |
| 280 | r""" |
| 281 | Reverse given string (From LSB first to MSB first) and converts to HEX. |
| 282 | |
George Keishing | 86d85f4 | 2022-08-18 23:02:22 -0500 | [diff] [blame] | 283 | Input string 0a 00 |
nagarjunb22 | 87138e6 | 2022-04-19 16:49:16 +0530 | [diff] [blame] | 284 | Return string 0a |
| 285 | """ |
| 286 | datalist = string.split(" ") |
| 287 | new_list = datalist[::-1] |
| 288 | new_string = "".join([str(element) for element in new_list]) |
| 289 | return int(new_string, 16) |
| 290 | |
| 291 | |
| 292 | def add_prefix_to_string(string, prefix): |
| 293 | r""" |
| 294 | Add given prefix to the string and return string. |
| 295 | |
| 296 | Input string 0a 01 |
| 297 | Return string 0x0a 0x01 |
| 298 | """ |
| 299 | prefix_string = '' |
| 300 | data_list = string.strip().split(" ") |
| 301 | for item in data_list: |
| 302 | prefix_string += prefix + item + ' ' |
| 303 | return prefix_string.strip() |