|
| 1 | +#! /usr/bin/python |
| 2 | + |
| 3 | +import swsssdk |
| 4 | +import sys |
| 5 | +from tabulate import tabulate |
| 6 | +from natsort import natsorted |
| 7 | + |
| 8 | +import os |
| 9 | + |
| 10 | +# mock the redis for unit test purposes # |
| 11 | +try: |
| 12 | + if os.environ["UTILITIES_UNIT_TESTING"] == "1": |
| 13 | + modules_path = os.path.join(os.path.dirname(__file__), "..") |
| 14 | + tests_path = os.path.join(modules_path, "sonic-utilities-tests") |
| 15 | + sys.path.insert(0, modules_path) |
| 16 | + sys.path.insert(0, tests_path) |
| 17 | + import mock_tables.dbconnector |
| 18 | + client = mock_tables.dbconnector.redis.StrictRedis() |
| 19 | + if client.keys() is None: |
| 20 | + raise Exception("Invalid mock_table keys") |
| 21 | +except KeyError: |
| 22 | + pass |
| 23 | + |
| 24 | +# ========================== Common gearbox-utils logic ========================== |
| 25 | + |
| 26 | +GEARBOX_TABLE_PHY_PREFIX = "_GEARBOX_TABLE:phy:{}" |
| 27 | +GEARBOX_TABLE_INTERFACE_PREFIX = "_GEARBOX_TABLE:interface:{}" |
| 28 | +GEARBOX_TABLE_PORT_PREFIX = "_GEARBOX_TABLE:phy:{}:ports:{}" |
| 29 | + |
| 30 | +PORT_TABLE_ETHERNET_PREFIX = "PORT_TABLE:{}" |
| 31 | + |
| 32 | +PHY_NAME = "name" |
| 33 | +PHY_ID = "phy_id" |
| 34 | +PHY_FIRMWARE_MAJOR_VERSION = "firmware_major_version" |
| 35 | +PHY_LINE_LANES = "line_lanes" |
| 36 | +PHY_SYSTEM_LANES = "system_lanes" |
| 37 | + |
| 38 | +PORT_OPER_STATUS = "oper_status" |
| 39 | +PORT_ADMIN_STATUS = "admin_status" |
| 40 | +PORT_SYSTEM_SPEED = "system_speed" |
| 41 | +PORT_LINE_SPEED = "line_speed" |
| 42 | + |
| 43 | +INTF_NAME = "name" |
| 44 | +INTF_LANES = "lanes" |
| 45 | +INTF_SPEED = "speed" |
| 46 | + |
| 47 | +def get_appl_key_attr(db, key, attr, lane_count=1): |
| 48 | + """ |
| 49 | + Get APPL_DB key attribute |
| 50 | + """ |
| 51 | + |
| 52 | + val = db.get(db.APPL_DB, key, attr) |
| 53 | + if val is None: |
| 54 | + return "N/A" |
| 55 | + |
| 56 | + if "speed" in attr: |
| 57 | + if val == "0": |
| 58 | + return "N/A" |
| 59 | + |
| 60 | + speed = int(val[:-3]) |
| 61 | + |
| 62 | + if (speed % lane_count == 0): |
| 63 | + speed = speed // lane_count |
| 64 | + else: |
| 65 | + return "N/A" |
| 66 | + |
| 67 | + val = '{}G'.format(str(speed)) |
| 68 | + |
| 69 | + return val |
| 70 | + |
| 71 | +def db_connect_appl(): |
| 72 | + appl_db = swsssdk.SonicV2Connector(host='127.0.0.1') |
| 73 | + if appl_db is None: |
| 74 | + return None |
| 75 | + appl_db.connect(appl_db.APPL_DB) |
| 76 | + return appl_db |
| 77 | + |
| 78 | +def db_connect_state(): |
| 79 | + """ |
| 80 | + Connect to REDIS STATE DB and get optics info |
| 81 | + """ |
| 82 | + state_db = swsssdk.SonicV2Connector(host='127.0.0.1') |
| 83 | + if state_db is None: |
| 84 | + return None |
| 85 | + state_db.connect(state_db.STATE_DB, False) # Make one attempt only |
| 86 | + return state_db |
| 87 | + |
| 88 | +def appl_db_keys_get(appl_db): |
| 89 | + """ |
| 90 | + Get APPL_DB Keys |
| 91 | + """ |
| 92 | + return appl_db.keys(appl_db.APPL_DB, GEARBOX_TABLE_PHY_PREFIX.format("*")) |
| 93 | + |
| 94 | +def appl_db_interface_keys_get(appl_db): |
| 95 | + """ |
| 96 | + Get APPL_DB Keys |
| 97 | + """ |
| 98 | + return appl_db.keys(appl_db.APPL_DB, GEARBOX_TABLE_INTERFACE_PREFIX.format("*")) |
| 99 | + |
| 100 | +# ========================== phy-status logic ========================== |
| 101 | + |
| 102 | +phy_header_status = ['PHY Id', 'Name', 'Firmware'] |
| 103 | + |
| 104 | +class PhyStatus(object): |
| 105 | + |
| 106 | + def display_phy_status(self, appl_db_keys): |
| 107 | + """ |
| 108 | + Generate phy status output |
| 109 | + """ |
| 110 | + table = [] |
| 111 | + key = [] |
| 112 | + |
| 113 | + for key in appl_db_keys: |
| 114 | + if 'lanes' in key or 'ports' in key: |
| 115 | + continue |
| 116 | + list_items = key.split(':') |
| 117 | + phy_id = list_items[2] |
| 118 | + data_row = ( |
| 119 | + phy_id, |
| 120 | + get_appl_key_attr(self.appl_db, GEARBOX_TABLE_PHY_PREFIX.format(phy_id), PHY_NAME), |
| 121 | + get_appl_key_attr(self.appl_db, GEARBOX_TABLE_PHY_PREFIX.format(phy_id), PHY_FIRMWARE_MAJOR_VERSION)) |
| 122 | + table.append(data_row) |
| 123 | + |
| 124 | + # Sorting and tabulating the result table. |
| 125 | + sorted_table = natsorted(table) |
| 126 | + print tabulate(sorted_table, phy_header_status, tablefmt="simple", stralign='right') |
| 127 | + |
| 128 | + def __init__(self): |
| 129 | + self.appl_db = db_connect_appl() |
| 130 | + if self.appl_db is None: |
| 131 | + return |
| 132 | + |
| 133 | + appl_db_keys = appl_db_keys_get(self.appl_db) |
| 134 | + if appl_db_keys is None: |
| 135 | + return |
| 136 | + |
| 137 | + self.display_phy_status(appl_db_keys) |
| 138 | + |
| 139 | +# ========================== interface-status logic ========================== |
| 140 | + |
| 141 | +intf_header_status = ['PHY Id', 'Interface', 'MAC Lanes', 'MAC Lane Speed', 'PHY Lanes', 'PHY Lane Speed', 'Line Lanes', 'Line Lane Speed', 'Oper', 'Admin'] |
| 142 | + |
| 143 | +class InterfaceStatus(object): |
| 144 | + |
| 145 | + def display_intf_status(self, appl_db_keys): |
| 146 | + """ |
| 147 | + Generate phy status output |
| 148 | + """ |
| 149 | + table = [] |
| 150 | + key = [] |
| 151 | + |
| 152 | + for key in appl_db_keys: |
| 153 | + list_items = key.split(':') |
| 154 | + index = list_items[2] |
| 155 | + |
| 156 | + name = get_appl_key_attr(self.appl_db, GEARBOX_TABLE_INTERFACE_PREFIX.format(index), INTF_NAME), |
| 157 | + name = name[0] |
| 158 | + |
| 159 | + mac_lanes = get_appl_key_attr(self.appl_db, PORT_TABLE_ETHERNET_PREFIX.format(name), INTF_LANES) |
| 160 | + lanes = mac_lanes.split(',') |
| 161 | + lane_count = 0 |
| 162 | + for lane in lanes: |
| 163 | + lane_count += 1 |
| 164 | + |
| 165 | + phy_id = get_appl_key_attr(self.appl_db, GEARBOX_TABLE_INTERFACE_PREFIX.format(index), PHY_ID) |
| 166 | + |
| 167 | + data_row = ( |
| 168 | + phy_id, |
| 169 | + name, |
| 170 | + mac_lanes, |
| 171 | + get_appl_key_attr(self.appl_db, PORT_TABLE_ETHERNET_PREFIX.format(name), INTF_SPEED, lane_count), |
| 172 | + get_appl_key_attr(self.appl_db, GEARBOX_TABLE_INTERFACE_PREFIX.format(index), PHY_SYSTEM_LANES), |
| 173 | + get_appl_key_attr(self.appl_db, GEARBOX_TABLE_PORT_PREFIX.format(phy_id, index), PORT_SYSTEM_SPEED), |
| 174 | + get_appl_key_attr(self.appl_db, GEARBOX_TABLE_INTERFACE_PREFIX.format(index), PHY_LINE_LANES), |
| 175 | + get_appl_key_attr(self.appl_db, GEARBOX_TABLE_PORT_PREFIX.format(phy_id, index), PORT_LINE_SPEED), |
| 176 | + get_appl_key_attr(self.appl_db, PORT_TABLE_ETHERNET_PREFIX.format(name), PORT_OPER_STATUS), |
| 177 | + get_appl_key_attr(self.appl_db, PORT_TABLE_ETHERNET_PREFIX.format(name), PORT_ADMIN_STATUS)) |
| 178 | + |
| 179 | + table.append(data_row) |
| 180 | + |
| 181 | + # Sorting and tabulating the result table. |
| 182 | + sorted_table = natsorted(table) |
| 183 | + print tabulate(sorted_table, intf_header_status, tablefmt="simple", stralign='right') |
| 184 | + |
| 185 | + def __init__(self): |
| 186 | + self.appl_db = db_connect_appl() |
| 187 | + if self.appl_db is None: |
| 188 | + return |
| 189 | + |
| 190 | + appl_db_keys = appl_db_interface_keys_get(self.appl_db) |
| 191 | + if appl_db_keys is None: |
| 192 | + return |
| 193 | + |
| 194 | + self.display_intf_status(appl_db_keys) |
| 195 | + |
| 196 | +def main(args): |
| 197 | + """ |
| 198 | + phy status |
| 199 | + interfaces status |
| 200 | + interfaces counters |
| 201 | + """ |
| 202 | + |
| 203 | + if len(args) == 0: |
| 204 | + print "No valid arguments provided" |
| 205 | + return |
| 206 | + |
| 207 | + cmd1 = args[0] |
| 208 | + if cmd1 != "phys" and cmd1 != "interfaces": |
| 209 | + print "No valid command provided" |
| 210 | + return |
| 211 | + |
| 212 | + cmd2 = args[1] |
| 213 | + if cmd2 != "status" and cmd2 != "counters": |
| 214 | + print "No valid command provided" |
| 215 | + return |
| 216 | + |
| 217 | + if cmd1 == "phys" and cmd2 == "status": |
| 218 | + PhyStatus() |
| 219 | + elif cmd1 == "interfaces" and cmd2 == "status": |
| 220 | + InterfaceStatus() |
| 221 | + |
| 222 | + sys.exit(0) |
| 223 | + |
| 224 | +if __name__ == "__main__": |
| 225 | + main(sys.argv[1:]) |
0 commit comments