def platform_get_info_psu(oc_comps, old_comp_lst): """ root@switch1:/home/admin# show platform psustatus PSU Status ----- -------- PSU 1 NOT OK PSU 2 OK """ exec_cmd = 'show platform psustatus' (is_ok, output) = util_utl.utl_get_execute_cmd_output(exec_cmd) if is_ok: output = output.splitlines() psu_beg = False for idx in range(len(output)): if output[idx] == '': continue if psu_beg: psu_line = output[idx].split(" ") psu_name = psu_line[0].replace(" ", "_") oc_comp = oc_comps.component.add(psu_name) old_comp_lst.append(psu_name) oc_comp.state._set_type('POWER_SUPPLY') oc_comp.power_supply.state._set_enabled(True if "OK" == psu_line[1] else False) oc_comp.power_supply.state.enabled._mchanged = True else: if '-----' in output[idx]: psu_beg = True
def platform_get_psu(pf_yph): global OLD_COMP_DICT oc_comps = pf_yph.get("/components")[0] if "psu" in OLD_COMP_DICT: for old_psu in OLD_COMP_DICT["psu"]: oc_comps.component.delete(old_psu) OLD_COMP_DICT["psu"] = [] exec_cmd = 'show platform psustatus' (is_ok, output) = util_utl.utl_get_execute_cmd_output(exec_cmd) if is_ok: output = output.splitlines() psu_beg = False for idx in range(len(output)): if output[idx] == '': continue if psu_beg: psu_line = output[idx].split(" ") psu_name = psu_line[0].replace(" ", "_") oc_comp = oc_comps.component.add(psu_name) OLD_COMP_DICT["psu"].append(psu_name) oc_comp.state._set_type('POWER_SUPPLY') oc_comp.power_supply.state._set_enabled(True if "OK" == psu_line[1] else False) oc_comp.power_supply.state.enabled._mchanged = True else: if '-----' in output[idx]: psu_beg = True return True if len(OLD_COMP_DICT["psu"]) > 0 else False
def interface_get_my_mac(): global MY_MAC_ADDR # exec_cmd = "ip link show eth0 | grep ether | awk '{print $2}'" # bcz some vendors use different mac for eth0 exec_cmd = "sonic-cfggen -d -v DEVICE_METADATA.localhost.mac" (is_ok, output) = util_utl.utl_get_execute_cmd_output(exec_cmd) if is_ok: MY_MAC_ADDR = output.strip('\n')
def bcm_get_diag_port_map(): global BCM_PHY_PORT_MAP, BCM_USR_PORT_MAP, BCM_PORT_MAP_INIT if BCM_PORT_MAP_INIT: return #exe_cmd = 'docker exec -i syncd cat /usr/share/sonic/hwsku/port_config.ini' exe_cmd = 'cat /usr/share/sonic/hwsku/port_config.ini' (is_ok, output) = util_utl.utl_get_execute_cmd_output(exe_cmd) if is_ok: # Default column definition titles = ['name', 'lanes', 'alias', 'index'] output = output.splitlines() BCM_PHY_PORT_MAP = {} for line in output: if line.startswith('#'): if "name" in line: titles = line.strip('#').split() continue tokens = line.split() if len(tokens) < 2: continue name_index = titles.index('name') name = tokens[name_index] data = {} for i, item in enumerate(tokens): if i == name_index: continue data[titles[i]] = item data.setdefault('alias', name) BCM_PHY_PORT_MAP[name] = data for key in BCM_PHY_PORT_MAP.keys(): BCM_USR_PORT_MAP['xe' + BCM_PHY_PORT_MAP[key]['index']] = key BCM_PORT_MAP_INIT = True
def interface_remove_all_mbr_for_pc(db, pc_name): exec_cmd = 'teamdctl %s config dump actual' % pc_name (is_ok, output) = util_utl.utl_get_execute_cmd_output(exec_cmd) if is_ok: pc_cfg = json.loads(output) if "ports" in pc_cfg: for port in pc_cfg["ports"]: exec_cmd = TEAMD_CFG_PORT_CMD_TMPL.format( pc_name, 'remove', port) util_utl.utl_execute_cmd(exec_cmd) interface_restore_port_setting(db, port)
def sys_get_info(root_yph, path_ar, key_ar, disp_args): global OLD_NTP_SVR_LST new_ntp_svr_lst = [] #pdb.set_trace() (is_ok, ntpq_output) = util_utl.utl_get_execute_cmd_output(GET_NTPQ_STAUS_CMD) if is_ok: """ root@switch1:/home/admin# ntpq -pn remote refid st t when poll reach delay offset jitter ============================================================================== *103.18.128.60 140.112.2.189 2 u 247 1024 377 22.842 -0.318 2.747 """ ntpq_output = ntpq_output.splitlines()[2:] ntpq_output = [oline.split() for oline in ntpq_output if oline] oc_sys = root_yph.get("/system")[0] ntp_lst = disp_args.cfgdb.get_table(util_utl.CFGDB_TABLE_NAME_NTP) for svr in ntp_lst: if svr not in oc_sys.ntp.servers.server: oc_svr = oc_sys.ntp.servers.server.add(svr) oc_svr.config.iburst = True else: oc_svr = oc_sys.ntp.servers.server[svr] fill_ntpq_info(oc_svr, svr, ntpq_output) new_ntp_svr_lst.append(svr) # remove no existing entry for svr in OLD_NTP_SVR_LST: if svr not in new_ntp_svr_lst: oc_sys.ntp.servers.server.delete(svr) OLD_NTP_SVR_LST = new_ntp_svr_lst (is_ok, output) = util_utl.utl_get_execute_cmd_output(GET_CUR_DATE_CMD) if is_ok: oc_sys.state._set_current_datetime(output.strip('\n')) return True
def interface_get_old_pc_name_by_port(port_name, disp_args): old_pc_name = "" pc_lst = disp_args.cfgdb.get_table(util_utl.CFGDB_TABLE_NAME_PC) for pc in pc_lst: exec_cmd = 'teamdctl %s config dump actual' % pc (is_ok, output) = util_utl.utl_get_execute_cmd_output(exec_cmd) if is_ok: pc_cfg = json.loads(output) if port_name in pc_cfg["ports"]: old_pc_name = pc break return old_pc_name
def interface_get_ip_link_output(): """ use 'ip link show' command to gather information """ ret_output = {} (is_ok, output) = util_utl.utl_get_execute_cmd_output('ip -o link show') if is_ok: tmp_output = output.splitlines() for idx in range(0, len(tmp_output)): # ex: # 27: Ethernet84: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9100 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000\ link/ether 50:6b:4b:95:e1:00 brd ff:ff:ff:ff:ff:ff onel = tmp_output[idx].split() inf_name = onel[1].rstrip(':') ret_output[inf_name] = tmp_output[idx] return ret_output
def interface_get_mgmtport_info(oc_infs, fill_info_bmp, key_ar, out_tbl): ret_val = False (is_ok, output) = util_utl.utl_get_execute_cmd_output('ifconfig %s' % MGMT_PORT_NAME) if is_ok: if MGMT_PORT_NAME not in oc_infs.interface: oc_inf = oc_infs.interface.add(MGMT_PORT_NAME) else: oc_inf = oc_infs.interface[MGMT_PORT_NAME] if fill_info_bmp & FILL_INFO_STATE: interface_fill_inf_admin_oper(oc_inf, MGMT_PORT_NAME, out_tbl) if fill_info_bmp & FILL_INFO_IP: interface_fill_inf_ip_info(oc_inf, MGMT_PORT_NAME, out_tbl) ret_val = True return ret_val
def get_fan_or_temperature(oc_comps, comp_list, get_fan): exec_cmd = 'show environment' (is_ok, output) = util_utl.utl_get_execute_cmd_output(exec_cmd) if is_ok: output = output.splitlines() # step 1 get name # step 2 get Adapter # step 3 get component beg_id = 1 if 'Command:' in output[0] else 0 step = 1 pat_obj = re.compile(r'([^:]*):([^(]*)(.*)') for idx in range(beg_id, len(output)): if step == 1: comp_name = output[idx] step = step + 1 elif step == 2: step = step + 1 elif step == 3: if output[idx] == '': step = 1 # begin for next component else: m = pat_obj.match(output[idx]) if m: if (get_fan and 'RPM' in m.group(2)) or ( not get_fan and 'C' in m.group(2)): sub_comp_name = comp_name + '_' + m.group( 1).replace(' ', '_') oc_comp = oc_comps.component.add(sub_comp_name) comp_list.append(sub_comp_name) if get_fan: oc_comp.state._set_type('FAN') value = int(m.group(2).split(" RPM")[0]) oc_comp.fan.state._set_speed(value) else: oc_comp.state._set_type('SENSOR') value = float( m.group(2).split(" C")[0].replace( '+', ' ')) oc_comp.state.temperature._set_instant(value)
def interface_get_ip4_addr_output(): """ use 'ip -4 addr show' command to gather information """ ret_output = {} (is_ok, output) = util_utl.utl_get_execute_cmd_output('ip -4 addr show') if is_ok: tmp_output = output.splitlines() blk_head = 0 for idx in range(0, len(tmp_output)): if ':' in tmp_output[idx] or idx == len(tmp_output) - 1: # ex: 163: Ethernet0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9100 qdisc pfifo_fast state DOWN group default qlen 1000 head_line = tmp_output[blk_head].split(':') inf_name = head_line[1].strip() ret_output[inf_name] = [] for blk_idx in range(blk_head, idx): ret_output[inf_name].append(tmp_output[blk_idx]) blk_head = idx return ret_output
def interface_get_ip4_nbr_output(): """ use 'ip -4 neigh show' command to gather information """ ret_output = {} (is_ok, output) = util_utl.utl_get_execute_cmd_output('ip -4 neigh show') if is_ok: tmp_output = output.splitlines() for idx in range(len(tmp_output)): tmp_line = tmp_output[idx].split() # ex: # 192.168.200.10 dev eth0 lladdr a0:36:9f:8d:52:fa STALE # 192.168.200.66 dev eth0 lladdr 34:64:a9:2b:2e:ad REACHABLE # 192.168.200.1 dev eth0 FAILED if tmp_line[3] == 'FAILED': continue inf_name = tmp_line[2] if inf_name not in ret_output: ret_output[inf_name] = [] ret_output[inf_name].append(tmp_line) return ret_output
def lr_get_info(lr_yph, path_ar, key_ar, disp_args): """ use 'ip route show' command to gather information """ ret_val = False oc_lr = lr_yph.get("/local-routes")[0] global OLD_SR_LST new_sr_lst = [] (is_ok, output) = util_utl.utl_get_execute_cmd_output('ip -4 route show') if is_ok: output = output.splitlines() # ex: # default via 192.168.200.254 dev eth0 # 172.17.2.0/24 # nexthop via 10.0.0.108 dev Ethernet54 weight 1 # nexthop via 10.0.0.142 dev Ethernet71 weight 1 # # default via 192.168.200.254 dev eth0 proto zebra # 100.100.100.0/24 dev Ethernet4 proto kernel scope link src 100.100.100.104 linkdown # 172.17.2.0/24 linkdown # nexthop via 100.100.100.104 dev Ethernet4 weight 1 linkdown # nexthop via 100.100.100.108 dev Ethernet8 weight 1 linkdown idx = 0 while idx < len(output): ldata = output[idx].split() nh_id = 0 oc_sr = None pfx_str = lr_get_pfx_str(ldata[0]) if 'dev' not in ldata: # ecmp oc_sr = lr_get_oc_sr(oc_lr, pfx_str, new_sr_lst, OLD_SR_LST) idx += 1 while 'nexthop' in output[idx]: nh_data = output[idx].split() if lr_add_nexthop(lr_yph, oc_sr, nh_id, nh_data[2], nh_data[4]): nh_id += 1 idx += 1 else: oc_sr = lr_get_oc_sr(oc_lr, pfx_str, new_sr_lst, OLD_SR_LST) if lr_add_nexthop(lr_yph, oc_sr, 0, ldata[2], ldata[4]): nh_id += 1 idx += 1 if oc_sr and nh_id == 0: oc_lr.static_routes.static.delete(pfx_str) new_sr_lst.remove(pfx_str) if key_ar and key_ar[0] == pfx_str: break ret_val = True # remote old sr for old_sr in OLD_SR_LST: oc_sr = oc_lr.static_routes.static[old_sr] lr_del_all_nhop(oc_sr) oc_lr.static_routes.static.delete(old_sr) OLD_SR_LST = new_sr_lst return ret_val
def platform_get_info_fan(oc_comps, old_comp_dict): """ root@switch1:/home/admin# show environment coretemp-isa-0000 Adapter: ISA adapter Physical id 0: +40.0 C (high = +82.0 C, crit = +104.0 C) Core 0: +40.0 C (high = +82.0 C, crit = +104.0 C) Core 1: +40.0 C (high = +82.0 C, crit = +104.0 C) Core 2: +40.0 C (high = +82.0 C, crit = +104.0 C) Core 3: +40.0 C (high = +82.0 C, crit = +104.0 C) as7116_54x_fan-i2c-1-63 Adapter: i2c-0-mux (chan_id 0) front fan 1: 11700 RPM front fan 2: 11850 RPM front fan 3: 11700 RPM front fan 4: 11700 RPM front fan 5: 11700 RPM rear fan 1: 9900 RPM rear fan 2: 9750 RPM rear fan 3: 9900 RPM rear fan 4: 9750 RPM rear fan 5: 9900 RPM lm75-i2c-17-4b Adapter: i2c-1-mux (chan_id 0) temp1: +32.5 C (high = +80.0 C, hyst = +75.0 C) lm75-i2c-19-49 Adapter: i2c-1-mux (chan_id 2) temp1: +33.5 C (high = +80.0 C, hyst = +75.0 C) lm75-i2c-20-4a Adapter: i2c-1-mux (chan_id 3) temp1: +30.5 C (high = +80.0 C, hyst = +75.0 C) """ fan_comp_list = old_comp_dict["fan"] temp_comp_list = old_comp_dict["temperature"] exec_cmd = 'show environment' (is_ok, output) = util_utl.utl_get_execute_cmd_output(exec_cmd) if is_ok: output = output.splitlines() # step 1 name # step 2 Adapter # step 3 component beg_id = 1 if 'Command:' in output[0] else 0 step = 1 pat_obj = re.compile(r'([^:]*):([^(]*)(.*)') for idx in range(beg_id, len(output)): if step == 1: comp_name = output[idx] step = step + 1 elif step == 2: step = step + 1 elif step == 3: if output[idx] == '': step = 1 # begin for next component else: m = pat_obj.match(output[idx]) if m: is_fan = True if 'RPM' in m.group(2) else False is_tem = True if 'C' in m.group(2) else False if is_fan or is_tem: sub_comp_name = comp_name + '_' + m.group( 1).replace(' ', '_') oc_comp = oc_comps.component.add(sub_comp_name) if is_fan: fan_comp_list.append(sub_comp_name) else: temp_comp_list.append(sub_comp_name) if is_fan: # fan oc_comp.state._set_type('FAN') value = int(m.group(2).split(" RPM")[0]) oc_comp.fan.state._set_speed(value) else: # temperature oc_comp.state._set_type('SENSOR') value = float( m.group(2).split(" C")[0].replace( '+', ' ')) oc_comp.state.temperature._set_instant(value)
def get_show_platform_syseeprom( cmd_output ): (is_mac_ok, mac_output) = util_utl.utl_get_execute_cmd_output("cat /sys/class/net/eth0/address") if is_mac_ok: return cmd_output % (get_serial_number(), mac_output) else: return cmd_output % (get_serial_number(), "FF:FF:FF:FF:FF:FF")
def platform_get_info(pf_yph, path_ar, key_ar, disp_args): if len(path_ar) > 1: if path_ar[1] == "fan": return platform_get_fan(pf_yph) elif path_ar[1] == "temperature": return platform_get_temperature(pf_yph) elif path_ar[1] == "psu": return platform_get_psu(pf_yph) else: return False global OLD_COMP_DICT oc_comps = pf_yph.get("/components")[0] # remove old entries for old_comp_list in OLD_COMP_DICT.values(): for old_comp in old_comp_list: oc_comps.component.delete(old_comp) OLD_COMP_DICT["fan"] = [] OLD_COMP_DICT["temperature"] = [] OLD_COMP_DICT["psu"] = [] OLD_COMP_DICT["fabric"] = [] # get info for psu platform_get_info_psu(oc_comps, OLD_COMP_DICT["psu"]) # get info for fan/sensor platform_get_info_fan(oc_comps, OLD_COMP_DICT) # show platform syseeprom # ex: Command: sudo decode-syseeprom # TlvInfo Header: # Id String: TlvInfo # Version: 1 # Total Length: 169 # TLV Name Code Len Value # -------------------- ---- --- ----- # Manufacture Date 0x25 19 06/16/2016 14:01:49 # Diag Version 0x2E 7 2.0.1.4 # Label Revision 0x27 4 R01J # Manufacturer 0x2B 6 Accton # Manufacture Country 0x2C 2 TW # Base MAC Address 0x24 6 CC:37:AB:EC:D9:B2 # Serial Number 0x23 14 571254X1625041 # Part Number 0x22 13 FP1ZZ5654002A # Product Name 0x21 15 5712-54X-O-AC-B # MAC Addresses 0x2A 2 74 # Vendor Name 0x2D 8 Edgecore # Platform Name 0x28 27 x86_64-accton_as5712_54x-r0 # ONIE Version 0x29 14 20170619-debug # CRC-32 0xFE 4 0x5B1B4944 fabric_comp_list = OLD_COMP_DICT["fabric"] show_cmd_pf = 'show platform syseeprom' oc_comp = None (is_ok, output) = util_utl.utl_get_execute_cmd_output(show_cmd_pf) if is_ok: output = output.splitlines() fld_map = [{ "fld": "pd", "tag": "Product", "pos": 4 }, { "fld": "hardware_version", "tag": "Platform", "pos": 4 }, { "fld": "serial_no", "tag": "Serial", "pos": 4 }, { "fld": "part_no", "tag": "Part", "pos": 4 }, { "fld": "mfg_name", "tag": "Manufacturer", "pos": 3 }, { "fld": "mfg_date", "tag": "Manufacture Date", "pos": 4 }] for idx in range(len(fld_map)): val = platform_get_syseeprom_output_val(output, fld_map[idx]["tag"], fld_map[idx]["pos"]) if val: if idx == 0: oc_comp = oc_comps.component.add(val) fabric_comp_list.append(val) oc_comp.state._set_type('FABRIC') (is_mac_ok, mac_output) = util_utl.utl_get_execute_cmd_output( "cat /sys/class/net/eth0/address") if is_mac_ok: oc_prop = oc_comp.properties.property_.add('ETH0_MAC') oc_prop.state._set_value( mac_output.replace('\n', '').upper()) else: if idx == 5: val = val.split('/') val = val[2] + '-' + val[0] + '-' + val[1] set_fun = getattr(oc_comp.state, "_set_%s" % fld_map[idx]["fld"]) if set_fun: set_fun(val) else: if idx == 0: break # show version # ex: SONiC Software Version: SONiC.HEAD.434-dirty-20171220.093901 # Distribution: Debian 8.1 # Kernel: 3.16.0-4-amd64 # Build commit: ab2d066 # Build date: Wed Dec 20 09:44:56 UTC 2017 # Built by: johnar@jenkins-worker-3 show_cmd_ver = 'show version' (is_ok, output) = util_utl.utl_get_execute_cmd_output(show_cmd_ver) if is_ok and oc_comp: output = output.splitlines() for idx in range(len(output)): if 'Software Version' in output[idx]: oc_comp.state._set_software_version(output[idx].split(': ')[1]) break if len(OLD_COMP_DICT["fan"]) == 0 and len(OLD_COMP_DICT["temperature"]) == 0 and \ len(OLD_COMP_DICT["psu"]) == 0 and len(OLD_COMP_DICT["fabric"]) == 0: return False return True
def interface_get_pc_inf_info(oc_infs, fill_info_bmp, key_ar, out_tbl, disp_args): global OLD_AGG_MBR_LST, OLD_PC_INF_LST # 1. clear all port's aggregate-id info if fill_info_bmp & FILL_INFO_PC: for inf in OLD_AGG_MBR_LST: inf.ethernet.config._unset_aggregate_id() OLD_AGG_MBR_LST = [] ret_val = False new_pc_inf_lst = [] pc_lst = disp_args.cfgdb.get_table(util_utl.CFGDB_TABLE_NAME_PC) if pc_lst: is_key_pc = True if key_ar and key_ar[0].find('PortChannel') == 0 else False is_key_et = True if key_ar and key_ar[0].find('Ethernet') == 0 else False ret_val = True for pc in pc_lst: # case 1, key: PortChannelX if is_key_pc and key_ar[0] != pc: continue if pc not in oc_infs.interface: oc_inf = oc_infs.interface.add(pc) else: oc_inf = oc_infs.interface[pc] new_pc_inf_lst.append(pc) if not is_key_et: if fill_info_bmp & FILL_INFO_STATE: interface_fill_inf_state(oc_inf, pc, disp_args.appdb, FILL_INFO_PC) if fill_info_bmp & FILL_INFO_VLAN: interface_fill_inf_vlanmbr_info(oc_inf, pc, out_tbl["vlan_output"]) if fill_info_bmp & FILL_INFO_IP: interface_fill_inf_ip_info(oc_inf, pc, out_tbl) if fill_info_bmp & FILL_INFO_PC: oc_inf._unset_aggregation() exec_cmd = 'teamdctl %s state dump' % pc (is_ok, output) = util_utl.utl_get_execute_cmd_output(exec_cmd) if is_ok: pc_state = json.loads(output) if not is_key_et: if pc_state["setup"]["runner_name"] == "lacp": oc_inf.aggregation.state._set_lag_type('LACP') else: oc_inf.aggregation.state._set_lag_type('STATIC') if "ports" in pc_state: for port in pc_state["ports"]: if port in oc_infs.interface: if not is_key_et: OLD_AGG_MBR_LST.append(oc_infs.interface[port]) oc_inf.aggregation.state.member.append(port) oc_infs.interface[port].ethernet.config._set_aggregate_id(pc) else: util_utl.utl_err("pc [%s]'s mbr port [%s] does not exist !!!" % (pc, port)) # case 2, key: EthernetX if is_key_et and key_ar[0] == port: break # remove no existing pc for pc in OLD_PC_INF_LST: if pc not in new_pc_inf_lst and pc in oc_infs.interface: oc_infs.interface.delete(pc) OLD_PC_INF_LST = new_pc_inf_lst return ret_val