Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | import sys |
| 3 | from robot.libraries.BuiltIn import BuiltIn |
| 4 | import imp |
| 5 | import string |
Rahul Maheshwari | 757d80c | 2016-10-17 01:09:55 -0500 | [diff] [blame] | 6 | import random |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 7 | import subprocess |
| 8 | from robot.utils import DotDict |
| 9 | |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 10 | |
Rahul Maheshwari | 757d80c | 2016-10-17 01:09:55 -0500 | [diff] [blame] | 11 | def 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 | |
| 19 | def 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 Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 26 | |
| 27 | def get_sensor(module_name, value): |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 28 | m = imp.load_source('module.name', module_name) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 29 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 30 | for i in m.ID_LOOKUP['SENSOR']: |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 31 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 32 | if m.ID_LOOKUP['SENSOR'][i] == value: |
| 33 | return i |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 34 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 35 | return 0xFF |
| 36 | |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 37 | |
| 38 | def get_inventory_sensor (module_name, value): |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 39 | m = imp.load_source('module.name', module_name) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 40 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 41 | value = string.replace(value, m.INVENTORY_ROOT, '<inventory_root>') |
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 | for i in m.ID_LOOKUP['SENSOR']: |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 44 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 45 | if m.ID_LOOKUP['SENSOR'][i] == value: |
| 46 | return i |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 47 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 48 | return 0xFF |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 49 | |
| 50 | |
| 51 | ################################################################ |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 52 | # This will return the URI's of the FRU type |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 53 | # |
| 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 | ################################################################ |
| 59 | def get_inventory_list(module_name): |
| 60 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 61 | l = [] |
| 62 | m = imp.load_source('module.name', module_name) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 63 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 64 | |
| 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 Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 71 | |
| 72 | |
| 73 | ################################################################ |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 74 | # This will return the URI's of the FRU type |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 75 | # |
| 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 | ################################################################ |
| 81 | def get_inventory_fru_type_list(module_name, fru): |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 82 | l = [] |
| 83 | m = imp.load_source('module.name', module_name) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 84 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 85 | 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 Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 91 | |
| 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 | ################################################################ |
| 101 | def get_vpd_inventory_list(module_name, fru): |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 102 | l = [] |
| 103 | m = imp.load_source('module.name', module_name) |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 104 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 105 | for i in m.ID_LOOKUP['FRU_STR']: |
| 106 | x = m.ID_LOOKUP['FRU_STR'][i] |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 107 | |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 108 | 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 Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 113 | |
| 114 | |
| 115 | def call_keyword(keyword): |
| 116 | return BuiltIn().run_keyword(keyword) |
| 117 | |
| 118 | |
| 119 | def main(): |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 120 | print get_vpd_inventory_list('../data/Palmetto.py', 'DIMM') |
Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 121 | |
| 122 | |
| 123 | if __name__ == "__main__": |
Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 124 | main() |
Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 125 | |
| 126 | |
| 127 | ############################################################################### |
| 128 | def get_mtr_report(host=""): |
| 129 | |
| 130 | r""" |
| 131 | Get an mtr report and return it as a dictionary of dictionaries. |
| 132 | |
| 133 | The key for the top level dictionary will be the host DNS name. The key |
| 134 | for the next level dictionary will be the field of a given row of the |
| 135 | report. |
| 136 | |
| 137 | Example result: |
| 138 | |
| 139 | report: |
| 140 | report[v325vrrp9-41-164-2.aus.stgla]: |
| 141 | report[v325vrrp9-41-164-2.aus.stgla][row_num]: 1 |
| 142 | report[v325vrrp9-41-164-2.aus.stgla][host]: v325vrrp9-41-164-2.aus.stgla |
| 143 | report[v325vrrp9-41-164-2.aus.stgla][loss]: 0.0 |
| 144 | report[v325vrrp9-41-164-2.aus.stgla][snt]: 10 |
| 145 | report[v325vrrp9-41-164-2.aus.stgla][last]: 0.2 |
| 146 | report[v325vrrp9-41-164-2.aus.stgla][avg]: 3.5 |
| 147 | report[v325vrrp9-41-164-2.aus.stgla][best]: 0.2 |
| 148 | report[v325vrrp9-41-164-2.aus.stgla][wrst]: 32.5 |
| 149 | report[v325vrrp9-41-164-2.aus.stgla][stdev]: 10.2 |
| 150 | report[beye6.aus.stglabs.ibm.com]: |
| 151 | report[beye6.aus.stglabs.ibm.com][row_num]: 2 |
| 152 | report[beye6.aus.stglabs.ibm.com][host]: beye6.aus.stglabs.ibm.com |
| 153 | report[beye6.aus.stglabs.ibm.com][loss]: 0.0 |
| 154 | report[beye6.aus.stglabs.ibm.com][snt]: 10 |
| 155 | report[beye6.aus.stglabs.ibm.com][last]: 0.5 |
| 156 | report[beye6.aus.stglabs.ibm.com][avg]: 0.5 |
| 157 | report[beye6.aus.stglabs.ibm.com][best]: 0.5 |
| 158 | report[beye6.aus.stglabs.ibm.com][wrst]: 0.5 |
| 159 | report[beye6.aus.stglabs.ibm.com][stdev]: 0.0 |
| 160 | |
| 161 | Description of arguments: |
| 162 | host The DNS name or IP address to be passed to the mtr command. |
| 163 | """ |
| 164 | |
| 165 | # Run the mtr command. Exlude the header line. Trim leading space from |
| 166 | # each line. Change all multiple spaces delims to single space delims. |
| 167 | cmd_buf = "mtr --report " + host +\ |
| 168 | " | tail -n +2 | sed -r -e 's/^[ ]+//g' -e 's/[ ]+/ /g'" |
| 169 | sub_proc = subprocess.Popen(cmd_buf, shell=True, stdout=subprocess.PIPE, |
| 170 | stderr=subprocess.STDOUT) |
| 171 | out_buf, err_buf = sub_proc.communicate() |
| 172 | shell_rc = sub_proc.returncode |
| 173 | |
| 174 | # Split the output by line. |
| 175 | rows = out_buf.rstrip('\n').split("\n") |
| 176 | |
| 177 | # Initialize report dictionary. |
| 178 | report = DotDict() |
| 179 | for row in rows: |
| 180 | # Process each row of mtr output. |
| 181 | # Create a list of fields by splitting on space delimiter. |
| 182 | row_list = row.split(" ") |
| 183 | # Create dictionary for the row. |
| 184 | row = DotDict() |
| 185 | row['row_num'] = row_list[0].rstrip('.') |
| 186 | row['host'] = row_list[1] |
| 187 | row['loss'] = row_list[2].rstrip('%') |
| 188 | row['snt'] = row_list[3] |
| 189 | row['last'] = row_list[4] |
| 190 | row['avg'] = row_list[5] |
| 191 | row['best'] = row_list[6] |
| 192 | row['wrst'] = row_list[7] |
| 193 | row['stdev'] = row_list[8] |
| 194 | report[row['host']] = row |
| 195 | |
| 196 | # Return the full report as dictionary of dictionaries. |
| 197 | return report |
| 198 | |
| 199 | ############################################################################### |
| 200 | |
| 201 | |
| 202 | ############################################################################### |
| 203 | def get_mtr_row(host=""): |
| 204 | |
| 205 | r""" |
| 206 | Run an mtr report and get a specified row and return it as a dictionary. |
| 207 | |
| 208 | Example result: |
| 209 | |
| 210 | row: |
| 211 | row[row_num]: 2 |
| 212 | row[host]: beye6.aus.stglabs.ibm.com |
| 213 | row[loss]: 0.0 |
| 214 | row[snt]: 10 |
| 215 | row[last]: 0.5 |
| 216 | row[avg]: 0.5 |
| 217 | row[best]: 0.4 |
| 218 | row[wrst]: 0.7 |
| 219 | row[stdev]: 0.1 |
| 220 | |
| 221 | Description of arguments: |
| 222 | host The DNS name or IP address to be passed to the mtr command as |
| 223 | well as the indicating which row of the report to return. |
| 224 | """ |
| 225 | |
| 226 | report = get_mtr_report(host) |
| 227 | |
| 228 | # The max length of host in output is 28 chars. |
| 229 | row = [value for key, value in report.items() if host[0:28] in key][0] |
| 230 | |
| 231 | return row |
| 232 | |
| 233 | ############################################################################### |
George Keishing | efa9735 | 2017-03-13 07:13:03 -0500 | [diff] [blame] | 234 | |
| 235 | |
| 236 | ############################################################################### |
| 237 | def list_to_set(fru_list=""): |
| 238 | r""" |
| 239 | Pack the list into a set tuple and return. |
| 240 | |
| 241 | It may seem that this function is rather trivial. However, it simplifies |
| 242 | the code and improves robot program readability and achieve the result |
| 243 | required. |
| 244 | |
| 245 | Example result: |
| 246 | |
| 247 | set(['Version', 'PartNumber', 'SerialNumber', 'FieldReplaceable', |
| 248 | 'BuildDate', 'Present', 'Manufacturer', 'PrettyName', 'Cached', 'Model']) |
| 249 | |
| 250 | # Description of arguments. |
| 251 | fru_list List of FRU's elements. |
| 252 | """ |
| 253 | return set(fru_list) |
| 254 | |
| 255 | ############################################################################### |