diff --git a/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/eeprom.py b/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/eeprom.py index 9f42b56ce7c2..0b854bd1cef2 100644 --- a/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/eeprom.py +++ b/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/eeprom.py @@ -1,7 +1,7 @@ #!/usr/bin/env python ############################################################################# -# Celestica DX010 +# Celestica FISHBONE # # Platform and model specific eeprom subclass, inherits from the base class, # and provides the followings: diff --git a/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/fanutil.py b/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/fanutil.py index dab2f64dd5e0..791fc35ca4db 100644 --- a/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/fanutil.py +++ b/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/fanutil.py @@ -2,51 +2,40 @@ __author__ = 'Wirut G.' __license__ = "GPL" -__version__ = "0.1.2" +__version__ = "0.2.0" __status__ = "Development" import requests import re +NUM_FAN_TRAY = 4 +NUM_ROTER = 2 + class FanUtil(): """Platform-specific FanUtil class""" def __init__(self): + self.fan_info_url = "http://240.1.1.1:8080/api/fan/info" + self.all_fan_dict = None - self.fan_fru_url = "http://240.1.1.1:8080/api/sys/fruid/fan" - self.sensor_url = "http://240.1.1.1:8080/api/sys/sensors" - self.fru_data_list = None - self.sensor_data_list = None - - def request_data(self): - # Reqest data from BMC if not exist. - if self.fru_data_list is None or self.sensor_data_list is None: - fru_data_req = requests.get(self.fan_fru_url) - sensor_data_req = requests.get(self.sensor_url) - fru_json = fru_data_req.json() - sensor_json = sensor_data_req.json() - self.fru_data_list = fru_json.get('Information') - self.sensor_data_list = sensor_json.get('Information') - return self.fru_data_list, self.sensor_data_list - - def name_to_index(self, fan_name): - # Get fan index from fan name - match = re.match(r"(FAN)([0-9]+)-(1|2)", fan_name, re.I) - fan_index = None - if match: - i_list = list(match.groups()) - fan_index = int(i_list[1])*2 - (int(i_list[2]) % 2) - return fan_index + def request_data(self, url): + try: + r = requests.get(url) + data = r.json() + except Exception as e: + return {} + return data def get_num_fans(self): """ Get the number of fans :return: int num_fans """ - num_fans = 8 + all_fan_dict = self.get_all() + num_fan_tray = all_fan_dict.get('Number', NUM_FAN_TRAY) - return num_fans + return num_fan_tray * NUM_ROTER def get_fan_speed(self, fan_name): """ @@ -54,31 +43,10 @@ def get_fan_speed(self, fan_name): :return: int fan_speed """ - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_speed = 0 - position_key = "Front" if index % 2 != 0 else "Rear" - index = int(round(float(index)/2)) - fan_key = "Fan " + str(index) + " " + position_key + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's speed. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_speed = fan_sp_list[0] - - except: - return 0 - - return fan_speed + return fan_info.get('Speed', 0) def get_fan_low_threshold(self, fan_name): """ @@ -88,31 +56,10 @@ def get_fan_low_threshold(self, fan_name): :return: int fan_low_threshold """ - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_low_threshold = 0 - position_key = "Front" if index % 2 != 0 else "Rear" - index = int(round(float(index)/2)) - fan_key = "Fan " + str(index) + " " + position_key - - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's threshold. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_low_threshold = fan_sp_list[1] - - except: - return "N/A" + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - return fan_low_threshold + return fan_info.get('LowThd', 0) def get_fan_high_threshold(self, fan_name): """ @@ -121,32 +68,10 @@ def get_fan_high_threshold(self, fan_name): the status of the fan is not ok :return: int fan_high_threshold """ + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_high_threshold = 0 - position_key = "Front" if index % 2 != 0 else "Rear" - index = int(round(float(index)/2)) - fan_key = "Fan " + str(index) + " " + position_key - - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's threshold. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_high_threshold = fan_sp_list[2] - - except: - return 0 - - return fan_high_threshold + return fan_info.get('HighThd', 0) def get_fan_pn(self, fan_name): """ @@ -154,58 +79,20 @@ def get_fan_pn(self, fan_name): :return: str fan_pn """ - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_pn = "N/A" - index = int(round(float(index)/2)) - fan_fru_key = "Fantray" + str(index) + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's fru. - for fan_fru in self.fru_data_list: - matching_fan = [s for s in fan_fru if fan_fru_key in s] - if matching_fan: - pn = [s for s in fan_fru if "Part" in s] - fan_pn = pn[0].split()[4] - - except: - return "N/A" - - return fan_pn + return fan_info.get('PN', 'N/A') def get_fan_sn(self, fan_name): """ Get the serial number of the fan :return: str fan_sn """ - try: - # Get real fan index - index = self.name_to_index(fan_name) + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - # Set key and index. - fan_sn = "N/A" - index = int(round(float(index)/2)) - fan_fru_key = "Fantray" + str(index) - - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's fru. - for fan_fru in self.fru_data_list: - matching_fan = [s for s in fan_fru if fan_fru_key in s] - if matching_fan: - serial = [s for s in fan_fru if "Serial" in s] - fan_sn = serial[0].split()[3] - - except: - return "N/A" - - return fan_sn + return fan_info.get('SN', 'N/A') def get_fans_name_list(self): """ @@ -240,71 +127,40 @@ def get_all(self): SN, conditional, if PRESENT is True, SN of the FAN, string) """ - self.fru_data_list, self.sensor_data_list = self.request_data() - all_fan_dict = dict() - - # Get the number of fans - n_fan = self.get_num_fans() - all_fan_dict["Number"] = n_fan - - # Set fan FRU data. - fan_fru_dict = dict() - fan_raw_idx = 1 - for fan_fru in self.fru_data_list: - fru_dict = dict() - fan_ps = False - - if len(fan_fru) == 0: - fan_idx = fan_raw_idx - fan_pn = "N/A" - fan_sn = "N/A" - else: - fan_key = fan_fru[0].split() - if str(fan_key[-1]).lower() == "absent": - fan_idx = int(re.findall('\d+', fan_key[0])[0]) - - else: - fan_idx = int(re.findall('\d+', fan_key[-1])[0]) - fan_ps = True - pn = [s for s in fan_fru if "Part" in s] - sn = [s for s in fan_fru if "Serial" in s] - fan_pn = pn[0].split( - ":")[-1].strip() if len(pn) > 0 else 'N/A' - fan_sn = sn[0].split( - ":")[-1].strip() if len(sn) > 0 else 'N/A' - - fru_dict["PN"] = "N/A" if not fan_pn or fan_pn == "" else fan_pn - fru_dict["SN"] = "N/A" if not fan_sn or fan_sn == "" else fan_sn - fru_dict["Present"] = fan_ps - fan_fru_dict[fan_idx] = fru_dict - fan_raw_idx += 1 - - # Set fan sensor data. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - for x in range(1, n_fan + 1): - fan_dict = dict() - f_index = int(round(float(x)/2)) - pos = 1 if x % 2 else 2 - position_key = "Front" if x % 2 != 0 else "Rear" - fan_key = "Fan " + str(f_index) + " " + position_key - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_dict["Present"] = fan_fru_dict[f_index]["Present"] - if fan_dict["Present"] or fan_sp_list[0] > 0: - fan_dict["Present"] = True - fan_dict["Speed"] = fan_sp_list[0] - fan_dict["Running"] = True if fan_dict["Speed"] > 0 else False - fan_dict["LowThd"] = fan_sp_list[1] - fan_dict["HighThd"] = fan_sp_list[2] - fan_dict["PN"] = fan_fru_dict[f_index]["PN"] - fan_dict["SN"] = fan_fru_dict[f_index]["SN"] - fan_dict["AirFlow"] = "FTOB" if "R1241-F9001" in fan_dict["PN"] else "Unknown" - fan_dict["AirFlow"] = "BTOF" if "R1241-F9002" in fan_dict["PN"] else fan_dict["AirFlow"] - fan_dict["Status"] = True if fan_dict["AirFlow"] != "Unknown" else False - fan_name = 'FAN{}_{}'.format(f_index, pos) - all_fan_dict[fan_name] = fan_dict - break - - return all_fan_dict + if not self.all_fan_dict: + all_fan_dict = dict() + + fan_info_req = self.request_data(self.fan_info_url) + fan_info_data = fan_info_req.get('data', {}) + all_fan_dict["Number"] = fan_info_data.get('Number', NUM_FAN_TRAY) + + for fan_idx in range(1, all_fan_dict["Number"] + 1): + num_of_roter = fan_info_data.get('Rotors', NUM_ROTER) + + for fan_pos in range(1, num_of_roter + 1): + fan_key = 'FAN{}'.format(str(fan_idx)) + roter_key = 'Rotor{}'.format(str(fan_pos)) + + fan_info = fan_info_data.get(fan_key, {}) + roter_info = fan_info.get(roter_key, {}) + + fan_info_dict = dict() + fan_info_dict["Present"] = True if fan_info.get( + "Present") == 'yes' else False + fan_info_dict["Speed"] = roter_info.get("Speed", "N/A") + fan_info_dict["Running"] = True if roter_info.get( + "Running") == 'yes' else False + fan_info_dict["HighThd"] = roter_info.get( + "SpeedMax", "N/A") + fan_info_dict["LowThd"] = roter_info.get("SpeedMin", "N/A") + fan_info_dict["Status"] = False if roter_info.get( + "HwAlarm") == 'yes' else True + fan_info_dict["PN"] = fan_info.get("PN", "N/A") + fan_info_dict["SN"] = fan_info.get("SN", "N/A") + fan_info_dict["AirFlow"] = fan_info.get("AirFlow", "N/A") + + fan_name = '{}_{}'.format(fan_key, fan_pos) + all_fan_dict[fan_name] = fan_info_dict + self.all_fan_dict = all_fan_dict + + return self.all_fan_dict diff --git a/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/fwmgrutil.py b/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/fwmgrutil.py index c1fb40cebfb8..bc13aa828df4 100644 --- a/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/fwmgrutil.py +++ b/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/fwmgrutil.py @@ -1,8 +1,15 @@ +#!/usr/bin/env python +# # fwmgrutil.py # # Platform-specific firmware management interface for SONiC # +__author__ = 'Wirut G.' +__license__ = "GPL" +__version__ = "0.2.0" +__status__ = "Development" + import subprocess import requests import os @@ -27,9 +34,15 @@ class FwMgrUtil(FwMgrUtilBase): def __init__(self): self.platform_name = "AS1332h" self.onie_config_file = "/host/machine.conf" - self.bmc_info_url = "http://240.1.1.1:8080/api/sys/bmc" - self.bmc_raw_command_url = "http://240.1.1.1:8080/api/sys/raw" - self.fw_upgrade_url = "http://240.1.1.1:8080/api/sys/upgrade" + self.bmc_info_url = "http://240.1.1.1:8080/api/bmc/info" + self.cpld_info_url = "http://240.1.1.1:8080/api/firmware/cpldversion" + self.bmc_raw_command_url = "http://240.1.1.1:8080/api/hw/rawcmd" + self.fw_upgrade_url = "http://240.1.1.1:8080/api/firmware/upgrade" + self.fw_refresh_url = "http://240.1.1.1:8080/api/firmware/refresh" + self.bios_next_boot = "http://240.1.1.1:8080/api/firmware/biosnextboot" + self.bmc_next_boot = "http://240.1.1.1:8080/api/bmc/nextboot" + self.bmc_reboot_url = "http://240.1.1.1:8080/api/bmc/reboot" + self.bios_boot_info = "http://240.1.1.1:8080/api/misc/biosbootstatus" self.onie_config_file = "/host/machine.conf" self.fw_upgrade_logger_path = "/var/log/fw_upgrade.log" self.cpldb_version_path = "/sys/devices/platform/%s.cpldb/getreg" % self.platform_name @@ -39,6 +52,9 @@ def __init__(self): self.switchboard_cpld3_path = "/sys/devices/platform/%s.switchboard/CPLD3/getreg" % self.platform_name self.switchboard_cpld4_path = "/sys/devices/platform/%s.switchboard/CPLD4/getreg" % self.platform_name self.bmc_pwd_path = "/usr/local/etc/bmcpwd" + self.cpld_name_list = ["CPU_CPLD", "COMBO_CPLD", + "SW_CPLD1", "SW_CPLD2", "TOP_LC_CPLD", "BOT_LC_CPLD"] + self.api_time_out = 300 def __get_register_value(self, path, register): cmd = "echo {1} > {0}; cat {0}".format(path, register) @@ -89,12 +105,12 @@ def get_bmc_version(self): """ bmc_version = None - - bmc_version_key = "OpenBMC Version" - bmc_info_req = requests.get(self.bmc_info_url, timeout=60) + bmc_version_key = "Version" + bmc_info_req = requests.get( + self.bmc_info_url, timeout=self.api_time_out) if bmc_info_req.status_code == 200: bmc_info_json = bmc_info_req.json() - bmc_info = bmc_info_json.get('Information') + bmc_info = bmc_info_json.get('data') bmc_version = bmc_info.get(bmc_version_key) return str(bmc_version) @@ -102,7 +118,8 @@ def upload_file_bmc(self, fw_path): scp_command = 'sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r %s root@240.1.1.1:/home/root/' % os.path.abspath( fw_path) child = pexpect.spawn(scp_command) - i = child.expect(["root@240.1.1.1's password:"], timeout=30) + child.timeout = self.api_time_out + i = child.expect(["root@240.1.1.1's password:"]) bmc_pwd = self.get_bmc_pass() if i == 0 and bmc_pwd: child.sendline(bmc_pwd) @@ -124,13 +141,13 @@ def get_cpld_version(self): CPLD_3 = self.__get_register_value(self.switchboard_cpld3_path, '0x00') CPLD_4 = self.__get_register_value(self.switchboard_cpld4_path, '0x00') - fan_cpld_key = "FanCPLD Version" + fan_cpld_key = "CPLD_FAN" fan_cpld = None - bmc_info_req = requests.get(self.bmc_info_url) - if bmc_info_req.status_code == 200: - bmc_info_json = bmc_info_req.json() - bmc_info = bmc_info_json.get('Information') - fan_cpld = bmc_info.get(fan_cpld_key) + fan_cpld_req = requests.get(self.cpld_info_url) + if fan_cpld_req.status_code == 200: + fancpld_info_json = fan_cpld_req.json() + fancpld_info_data = fancpld_info_json.get('data') + fan_cpld = fancpld_info_data.get(fan_cpld_key) CPLD_B = 'None' if CPLD_B is 'None' else "{}.{}".format( int(CPLD_B[2], 16), int(CPLD_B[3], 16)) @@ -144,8 +161,7 @@ def get_cpld_version(self): int(CPLD_3[2], 16), int(CPLD_3[3], 16)) CPLD_4 = 'None' if CPLD_4 is 'None' else "{}.{}".format( int(CPLD_4[2], 16), int(CPLD_4[3], 16)) - FAN_CPLD = 'None' if fan_cpld is None else "{}.{}".format( - int(fan_cpld[0], 16), int(fan_cpld[1], 16)) + FAN_CPLD = 'None' if fan_cpld is None else fan_cpld cpld_version_dict = {} cpld_version_dict.update({'CPLD_B': CPLD_B}) @@ -174,7 +190,7 @@ def get_bios_version(self): bios_version = raw_data_list[0] if len( raw_data_list) == 1 else raw_data_list[-2] - return str(bios_version) + return str(bios_version).strip() def get_onie_version(self): """Get ONiE version from SONiC @@ -224,7 +240,7 @@ def get_fpga_version(self): version = self.__get_register_value(self.fpga_version_path, '0x00') if version is not 'None': version = "{}.{}".format( - int(version[2:][:4], 16), int(version[2:][4:], 16)) + int(version[2:][1:4], 16), int(version[2:][4:], 16)) return str(version) def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): @@ -245,10 +261,13 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): return False if fw_type == 'bmc': + fw_extra_str = str(fw_extra).lower() + if fw_extra_str not in ["master", "slave", "both", "pingpong"]: + return False + self.__update_fw_upgrade_logger( "bmc_upgrade", "start BMC upgrade") # Copy BMC image file to BMC - fw_extra_str = str(fw_extra).lower() last_fw_upgrade = ["BMC", fw_path, fw_extra_str, "FAILED"] upload_file = self.upload_file_bmc(fw_path) if not upload_file: @@ -260,36 +279,36 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): filename_w_ext = os.path.basename(fw_path) json_data = dict() - json_data["path"] = "root@127.0.0.1:/home/root/%s" % filename_w_ext - json_data["password"] = bmc_pwd + json_data["Name"] = "bmc" + json_data["Path"] = "/home/root/%s" % filename_w_ext # Set flash type current_bmc = self.get_running_bmc() flash = fw_extra_str if fw_extra_str in [ "master", "slave", "both"] else "both" if fw_extra_str == "pingpong": - # flash = "master" if current_bmc == "slave" else "slave" + #flash = "master" if current_bmc == "slave" else "slave" flash = "slave" - json_data["flash"] = flash + json_data["Flash"] = flash # Install BMC if flash == "both": self.__update_fw_upgrade_logger( "bmc_upgrade", "install BMC as master mode") - json_data["flash"] = "master" - r = requests.post(self.bmc_info_url, json=json_data) - if r.status_code != 200 or 'success' not in r.json().get('result'): + json_data["Flash"] = "master" + r = requests.post(self.fw_upgrade_url, json=json_data) + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "bmc_upgrade", "fail, message=BMC API report error code %d" % r.status_code) + "bmc_upgrade", "fail, message={}".format(r.json().get('messages'))) self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False - json_data["flash"] = "slave" + json_data["Flash"] = "slave" self.__update_fw_upgrade_logger( - "bmc_upgrade", "install BMC as %s mode" % json_data["flash"]) - r = requests.post(self.bmc_info_url, json=json_data) - if r.status_code == 200 and 'success' in r.json().get('result'): + "bmc_upgrade", "install BMC as %s mode" % json_data["Flash"]) + r = requests.post(self.fw_upgrade_url, json=json_data) + if r.status_code == 200 and r.json().get('status') == 'OK': if fw_extra_str == "pingpong": flash = "master" if current_bmc == "slave" else "slave" self.__update_fw_upgrade_logger( @@ -308,7 +327,7 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): last_fw_upgrade[3] = "DONE" else: self.__update_fw_upgrade_logger( - "bmc_upgrade", "fail, message=unable to install BMC image") + "bmc_upgrade", "fail, message={}".format(r.json().get('messages'))) self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False @@ -419,18 +438,15 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): filename_w_ext = os.path.basename(fw_path) json_data = dict() - json_data["image_path"] = "root@127.0.0.1:/home/root/%s" % filename_w_ext - json_data["password"] = bmc_pwd - json_data["device"] = "cpld" - json_data["reboot"] = "no" - json_data["type"] = fw_extra_str + json_data["Path"] = "/home/root/%s" % filename_w_ext + json_data["Name"] = "cpld" # Call BMC api to install cpld image print("Installing...") r = requests.post(self.fw_upgrade_url, json=json_data) - if r.status_code != 200 or 'success' not in r.json().get('result'): + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "cpld_upgrade", "fail, message=invalid cpld image") + "cpld_upgrade", "fail, message={}".format(r.json().get('messages'))) continue cpld_result_list[i] = "DONE" @@ -455,57 +471,50 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): return True elif 'bios' in fw_type: + fw_extra_str = str(fw_extra).lower() + if fw_extra_str not in ["master", "slave", "both"]: + return False + self.__update_fw_upgrade_logger( "bios_upgrade", "start BIOS upgrade") last_fw_upgrade = ["BIOS", fw_path, None, "FAILED"] fw_extra_str = str(fw_extra).lower() - flash = fw_extra_str if fw_extra_str in [ - "master", "slave"] else "master" + flash = fw_extra_str.lower() if not os.path.exists(fw_path): self.__update_fw_upgrade_logger( "bios_upgrade", "fail, message=image not found") return False - scp_command = 'sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r %s root@240.1.1.1:/home/root/' % os.path.abspath( - fw_path) - child = pexpect.spawn(scp_command) - i = child.expect(["root@240.1.1.1's password:"], timeout=30) - if i != 0: + upload_file = self.upload_file_bmc(fw_path) + if not upload_file: self.__update_fw_upgrade_logger( "bios_upgrade", "fail, message=unable to upload image to BMC") self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False - child.sendline(bmc_pwd) - data = child.read() - print(data) - child.close - - json_data = dict() - json_data["data"] = "/usr/bin/ipmitool -b 1 -t 0x2c raw 0x2e 0xdf 0x57 0x01 0x00 0x01" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "bios_upgrade", "fail, message=unable to set state") - self.__update_fw_upgrade_logger( - "last_upgrade_result", str(last_fw_upgrade)) - return False + # json_data = dict() + # json_data["Command"] = "/usr/bin/ipmitool -b 1 -t 0x2c raw 0x2e 0xdf 0x57 0x01 0x00 0x01" + # r = requests.post(self.bmc_raw_command_url, json=json_data) + # if r.status_code != 200: + # self.__update_fw_upgrade_logger( + # "bios_upgrade", "fail, message=unable to set state") + # self.__update_fw_upgrade_logger( + # "last_upgrade_result", str(last_fw_upgrade)) + # return False filename_w_ext = os.path.basename(fw_path) json_data = dict() - json_data["image_path"] = "root@127.0.0.1:/home/root/%s" % filename_w_ext - json_data["password"] = bmc_pwd - json_data["device"] = "bios" - json_data["flash"] = flash - json_data["reboot"] = "no" + json_data["Path"] = "/home/root/%s" % filename_w_ext + json_data["Name"] = "bios" + json_data["Flash"] = flash print("Installing...") r = requests.post(self.fw_upgrade_url, json=json_data) - if r.status_code != 200 or 'success' not in r.json().get('result'): + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "bios_upgrade", "fail, message=unable install bios") + "bios_upgrade", "fail, message={}".format(r.json().get('messages'))) self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False @@ -742,77 +751,40 @@ def firmware_refresh(self, fpga_list, cpld_list, fw_extra=None): self.firmware_refresh(None, ["FAN_CPLD", "LC1_CPLD", "BASE_CPLD"], "/tmp/fw/fan_refresh.vme:none:/tmp/fw/base_refresh.vme") """ + self.__update_fw_upgrade_logger( + "fw_refresh", "start firmware refresh") - if not fpga_list and not cpld_list: - return False + cpld_list = [x.lower() for x in cpld_list] if cpld_list else [] + fpga_list = [x.lower() for x in fpga_list] if fpga_list else [] + refresh_list = cpld_list + fpga_list + fw_path_list = str(fw_extra).split(':') + refresh_img_list = ["none" for i in range(len(refresh_list))] - if type(cpld_list) is list and ("BASE_CPLD" in cpld_list or "FAN_CPLD" in cpld_list): - refresh_list = fpga_list + \ - cpld_list if type(fpga_list) is list else cpld_list + if len(refresh_list) == 0 : self.__update_fw_upgrade_logger( - "fw_refresh", "start %s refresh" % ",".join(refresh_list)) - fw_path_list = fw_extra.split(':') - command = "echo " - if len(fw_path_list) != len(cpld_list): - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=Invalid fw_extra") - return False - - for idx in range(0, len(cpld_list)): - fw_path = fw_path_list[idx] - refresh_type = { - "BASE_CPLD": "base", - "FAN_CPLD": "fan" - }.get(cpld_list[idx], None) + "fw_refresh", "fail, message=Invalid input") + return False - if not refresh_type: - continue - elif not self.upload_file_bmc(fw_path): + for idx in range(0, len(refresh_list)): + if refresh_list[idx] in ["fan_cpld", "base_cpld"]: + if not self.upload_file_bmc(fw_path_list[idx]): self.__update_fw_upgrade_logger( "cpld_refresh", "fail, message=Unable to upload refresh image to BMC") return False - else: - filename_w_ext = os.path.basename(fw_path) - - sub_command = "%s /home/root/%s > /tmp/cpld_refresh " % ( - refresh_type, filename_w_ext) - command += sub_command + refresh_img_list[idx] = "/home/root/%s" % os.path.basename( + fw_path_list[idx]) - json_data = dict() - json_data["data"] = command - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=%d Invalid refresh image" % r.status_code) - return False - elif type(cpld_list) is list: - refresh_list = fpga_list + \ - cpld_list if type(fpga_list) is list else cpld_list - self.__update_fw_upgrade_logger( - "fw_refresh", "start %s refresh" % ",".join(refresh_list)) - json_data = dict() - json_data["data"] = "echo cpu_cpld > /tmp/cpld_refresh" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=%d Unable to load new CPLD" % r.status_code) - return False - elif type(fpga_list) is list: - self.__update_fw_upgrade_logger( - "fw_refresh", "start FPGA refresh") - json_data = dict() - json_data["data"] = "echo fpga > /tmp/cpld_refresh" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=%d Unable to load new FPGA" % r.status_code) - return False - else: + json_data = dict() + json_data["Paths"] = refresh_img_list + json_data["Names"] = refresh_list + r = requests.post(self.fw_refresh_url, json=json_data) + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "fw_refresh", "fail, message=Invalid input") + "cpld_refreshfw", "fail, message={}".format(r.json().get('messages'))) return False self.__update_fw_upgrade_logger("fw_refresh", "done") + return True def get_running_bmc(self): @@ -820,21 +792,15 @@ def get_running_bmc(self): Get booting flash of running BMC. @return a string, "master" or "slave" """ - json_data = dict() - json_data["data"] = "/usr/local/bin/boot_info.sh" - r = requests.post(self.bmc_raw_command_url, json=json_data) - try: - boot_info_list = r.json().get('result') - for boot_info_raw in boot_info_list: - boot_info = boot_info_raw.split(":") - if "Current Boot Code Source" in boot_info[0]: - flash = "master" if "master "in boot_info[1].lower( - ) else "slave" - return flash - raise Exception( - "Error: Unable to detect booting flash of running BMC") - except Exception as e: - raise Exception(e) + running_bmc = "master" + running_bmc_key = "Flash" + bmc_info_req = requests.get( + self.bmc_info_url, timeout=self.api_time_out) + if bmc_info_req.status_code == 200: + bmc_info_json = bmc_info_req.json() + bmc_info = bmc_info_json.get('data') + running_bmc = bmc_info.get(running_bmc_key) + return str(running_bmc).lower() def set_bmc_boot_flash(self, flash): """ @@ -844,8 +810,8 @@ def set_bmc_boot_flash(self, flash): if flash.lower() not in ["master", "slave"]: return False json_data = dict() - json_data["data"] = "source /usr/local/bin/openbmc-utils.sh;bmc_reboot %s" % flash - r = requests.post(self.bmc_raw_command_url, json=json_data) + json_data["Flash"] = flash + r = requests.post(self.bmc_next_boot, json=json_data) if r.status_code != 200: return False return True @@ -854,10 +820,13 @@ def reboot_bmc(self): """ Reboot BMC """ - json_data = dict() - json_data["data"] = "source /usr/local/bin/openbmc-utils.sh;bmc_reboot reboot" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: + try: + r = requests.post(self.bmc_reboot_url) + if r.status_code != 200: + return False + except Exception as e: + if "Connection aborted." in e.message[0]: + return True return False return True @@ -866,17 +835,39 @@ def get_current_bios(self): # Get booting bios image of current running host OS # @return a string, "master" or "slave" """ + bios_boot_info = requests.get( + self.bios_boot_info, timeout=self.api_time_out) + if bios_boot_info.status_code == 200: + bios_boot_info_json = bios_boot_info.json() + bios_boot_info_data = bios_boot_info_json.get('data') + bios_boot = bios_boot_info_data.get("Flash") + return str(bios_boot) + + def get_bios_next_boot(self): + """ + # Get booting bios image of next booting host OS + # @return a string, "master" or "slave" + """ + bios_next_boot = "master" + bios_next_boot_info = requests.get( + self.bios_next_boot, timeout=self.api_time_out) + if bios_next_boot_info.status_code == 200: + bios_next_boot_info_json = bios_next_boot_info.json() + bios_next_boot_info_data = bios_next_boot_info_json.get('data') + bios_next_boot = bios_next_boot_info_data.get("Flash") + return str(bios_next_boot) + + def set_bios_next_boot(self, flash): + """ + # Set booting bios image of next booting host OS + # @return a string, "master" or "slave" + """ + if str(flash).lower() not in ['master', 'slave']: + return False + json_data = dict() - json_data["data"] = "source /usr/local/bin/openbmc-utils.sh;come_boot_info" - r = requests.post(self.bmc_raw_command_url, json=json_data) - try: - cpu_boot_info_list = r.json().get('result') - for cpu_boot_info_raw in cpu_boot_info_list: - if "COMe CPU boots from BIOS" in cpu_boot_info_raw: - bios_image = "master" if "master "in cpu_boot_info_raw.lower( - ) else "slave" - return bios_image - raise Exception( - "Error: Unable to detect current running bios image") - except Exception as e: - raise Exception(e) + json_data["Flash"] = str(flash).lower() + r = requests.post(self.bios_next_boot, json=json_data) + if r.status_code != 200: + return False + return True diff --git a/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/psuutil.py b/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/psuutil.py index d3a27117f5a2..b36cc0510918 100644 --- a/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/psuutil.py +++ b/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/psuutil.py @@ -2,7 +2,7 @@ __author__ = 'Wirut G.' __license__ = "GPL" -__version__ = "0.1.4" +__version__ = "0.2.0" __status__ = "Development" import requests @@ -13,43 +13,25 @@ except ImportError as e: raise ImportError(str(e) + "- required module not found") +NUM_PSU = 2 + class PsuUtil(PsuBase): """Platform-specific PSUutil class""" def __init__(self): PsuBase.__init__(self) - self.fru_status_url = "http://240.1.1.1:8080/api/sys/fruid/status" - self.psu_info_url = "http://240.1.1.1:8080/api/sys/fruid/psu" - - self.fru_status_list = None - self.psu_info_list = None - - def request_data(self): - # Reqest data from BMC if not exist. - if self.fru_status_list is None or self.psu_info_list is None: - fru_status_req = requests.get(self.fru_status_url) - psu_info_req = requests.get(self.psu_info_url) - fru_status_json = fru_status_req.json() - psu_info_json = psu_info_req.json() - self.fru_status_list = fru_status_json.get('Information') - self.psu_info_list = psu_info_json.get('Information') - return self.fru_status_list, self.psu_info_list - - def airflow_selector(self, pn): - # Set airflow type - pn = pn.upper() - if "DPS-1100FB" in pn: - airflow = "FTOB" - elif "DPS-1100AB" in pn: - airflow = "BTOF" - elif "FSJ026-A20G" in pn: - airflow = "FTOB" - elif "FSJ038-A20G" in pn: - airflow = "BTOF" - else: - airflow = "Unknown" - return airflow + + self.psu_info_url = "http://240.1.1.1:8080/api/psu/info" + self.all_psu_dict = None + + def request_data(self, url): + try: + r = requests.get(url) + data = r.json() + except Exception as e: + return {} + return data def get_num_psus(self): """ @@ -57,9 +39,9 @@ def get_num_psus(self): :return: An integer, the number of PSUs available on the device """ - num_psus = 2 + all_psu_dict = self.get_all() - return num_psus + return all_psu_dict.get('Number', NUM_PSU) def get_psu_status(self, index): """ @@ -69,28 +51,11 @@ def get_psu_status(self, index): :return: Boolean, True if PSU is operating properly, False if PSU is faulty """ - # init data - psu_key = "PSU" + str(index) - psu_status_key = "Power Status" - psu_power_status = False - - try: - # Request and validate sensor's information - self.fru_status_list, self.psu_info_list = self.request_data() + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) - # Get PSU power status. - for fru_status in self.fru_status_list: - is_psu = fru_status.get(psu_key) - psu_status = str(fru_status.get(psu_status_key)).strip() - - if is_psu is not None and psu_status == "OK": - psu_power_status = True - - except: - print("Error: Unable to access PSU power status") - return False - - return psu_power_status + return psu_info.get('PowerStatus', False) def get_psu_presence(self, index): """ @@ -99,29 +64,11 @@ def get_psu_presence(self, index): :param index: An integer, 1-based index of the PSU of which to query status :return: Boolean, True if PSU is plugged, False if not """ + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) - # Init data - psu_key = "PSU" + str(index) - psu_presence_key = "Present" - psu_presence_status = False - - try: - # Request and validate sensor's information. - self.fru_status_list, self.psu_info_list = self.request_data() - - # Get PSU present status. - for fru_status in self.fru_status_list: - is_psu = fru_status.get(psu_key) - psu_status = str(fru_status.get(psu_presence_key)).strip() - - if is_psu is not None and psu_status == "Present": - psu_presence_status = True - - except: - print("Error: Unable to access PSU presence status") - return False - - return psu_presence_status + return psu_info.get('Present', False) def get_psu_sn(self, index): """ @@ -130,25 +77,12 @@ def get_psu_sn(self, index): :param index: An integer, 1-based index of the PSU. :return: Serial number """ - serial_number = "N/A" - psu_key = "PSU" + str(index) + " FRU" - psu_sn_key = "Serial Number" - - try: - # Request and validate sensor's information. - self.fru_status_list, self.psu_info_list = self.request_data() - # Get PSU fru info. - for psu_fru in self.psu_info_list: - psu_sn = str(psu_fru.get(psu_sn_key)).strip() - if psu_fru.get(psu_key) is not None: - serial_number = psu_sn if psu_sn.strip() != "" else "N/A" - break + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) - except: - return "N/A" - - return serial_number + return psu_info.get('SN', False) def get_psu_pn(self, index): """ @@ -157,25 +91,12 @@ def get_psu_pn(self, index): :param index: An integer, 1-based index of the PSU. :return: Product name """ - product_name = "N/A" - psu_key = "PSU" + str(index) + " FRU" - psu_pn_key = "Product Name" - - try: - # Request and validate sensor's information - self.fru_status_list, self.psu_info_list = self.request_data() - - # Get PSU fru info. - for psu_fru in self.psu_info_list: - psu_pn = str(psu_fru.get(psu_pn_key)).strip() - if psu_fru.get(psu_key) is not None: - product_name = psu_pn if psu_pn.strip() != "" else "N/A" - break - - except: - return "N/A" - return product_name + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) + + return psu_info.get('PN', False) def get_all(self): """ @@ -187,62 +108,34 @@ def get_all(self): SN, conditional, if PRESENT is True, SN of the PSU, string """ - # Init data - all_psu_dict = dict() - all_psu_dict["Number"] = self.get_num_psus() - psu_sn_key_1 = "Serial Number" - psu_sn_key_2 = "Product Serial" - psu_pn_key = "Product Name" - - # Request and validate sensor's information. - self.fru_status_list, self.psu_info_list = self.request_data() - - # Set PSU FRU data. - psu_info_dict = dict() - for psu_fru in self.psu_info_list: - psu_data = dict() - pn = psu_fru.get(psu_pn_key) - sn = psu_fru.get(psu_sn_key_1) or psu_fru.get(psu_sn_key_2) - psu_data["PN"] = "N/A" if not pn or str( - pn).strip() == "" else str(pn).strip() - psu_data["SN"] = "N/A" if not pn or str( - pn).strip() == "" else str(sn).strip() - - fru_check = [psu_fru[v] for v in psu_fru.keys() if 'FRU Info' in v] - non_fru_check = [v for v in psu_fru.keys() if 'PSU' in v] - - if len(non_fru_check) > 0: - psu_idx = int(re.findall('\d+', non_fru_check[0])[0]) - psu_info_dict[psu_idx] = psu_data - elif len(fru_check) > 0: - psu_idx = int(re.findall('\d+', fru_check[0])[0]) - psu_info_dict[psu_idx] = psu_data - - # Set PSU status. - for fru_status in self.fru_status_list: - psu_status_dict = dict() - find_psu = [v for v in fru_status.keys() if "PSU" in v] - if len(find_psu) > 0: - psu_idx = int(re.findall('\d+', find_psu[0])[0]) - psu_ps_status = True if str(fru_status.get( - "Present")).strip() == "Present" else False - psu_pw_status = True if str(fru_status.get( - "Power Status")).strip() == "OK" else False - psu_pw_type = str(fru_status.get( - "Power Type")).strip() - ac_status = True if str(fru_status.get( - "AC Status")).strip().upper() == "OK" else False - - psu_status_dict["Present"] = psu_ps_status - if psu_ps_status: - psu_status_dict["PowerStatus"] = psu_pw_status - psu_status_dict["PN"] = psu_info_dict[psu_idx]["PN"] - psu_status_dict["SN"] = psu_info_dict[psu_idx]["SN"] - psu_status_dict["InputType"] = psu_pw_type - psu_status_dict["InputStatus"] = True if psu_pw_status and psu_ps_status else False - psu_status_dict["OutputStatus"] = ac_status - psu_status_dict["AirFlow"] = self.airflow_selector( - psu_status_dict["PN"]) - all_psu_dict[find_psu[0]] = psu_status_dict - - return all_psu_dict + if not self.all_psu_dict: + all_psu_dict = dict() + + psu_info_req = self.request_data(self.psu_info_url) + psu_info_data = psu_info_req.get('data', {}) + all_psu_dict["Number"] = psu_info_data.get('Number', NUM_PSU) + + for psu_idx in range(1, all_psu_dict["Number"] + 1): + psu_key = 'PSU{}'.format(str(psu_idx)) + psu_info = psu_info_data.get(psu_key, {}) + psu_input_info = psu_info.get('Inputs', {}) + psu_output_info = psu_info.get('Outputs', {}) + + psu_info_dict = dict() + psu_info_dict["InputType"] = psu_input_info.get("Type", "N/A") + psu_info_dict["InputStatus"] = True if psu_input_info.get( + "Status") else False + psu_info_dict["OutputStatus"] = True if psu_output_info.get( + "Status") else False + psu_info_dict["PowerStatus"] = ( + psu_info_dict["InputStatus"] and psu_info_dict["OutputStatus"]) + psu_info_dict["PN"] = psu_info.get("PN", "N/A") + psu_info_dict["SN"] = psu_info.get("SN", "N/A") + psu_info_dict["Present"] = True if psu_info.get("Present") == 'yes' else False + psu_info_dict["AirFlow"] = psu_info.get("AirFlow", "N/A") + + all_psu_dict[psu_key] = psu_info_dict + + self.all_psu_dict = all_psu_dict + + return self.all_psu_dict diff --git a/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/sensorutil.py b/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/sensorutil.py index 796949126ccc..efed1e850306 100644 --- a/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/sensorutil.py +++ b/device/alibaba/x86_64-alibaba_as13-32h-cl-r0/plugins/sensorutil.py @@ -12,20 +12,16 @@ class SensorUtil(): """Platform-specific SensorUtil class""" def __init__(self): - self.sensor_url = "http://240.1.1.1:8080/api/sys/sensors" - self.sys_fruid_url = "http://240.1.1.1:8080/api/sys/fruid/sys" - self.sensor_info_list = None - - def request_data(self): - # Reqest data from BMC if not exist. - if self.sensor_info_list is None: - sensor_data_req = requests.get(self.sensor_url) - sensor_json = sensor_data_req.json() - self.sensor_info_list = sensor_json.get('Information') - sys_fruid_req = requests.get(self.sys_fruid_url) - sys_fruid_json = sys_fruid_req.json() - self.sys_fruid_list = sys_fruid_json.get('Information') - return self.sensor_info_list + self.sensor_info_url = "http://240.1.1.1:8080/api/sensor/info" + self.all_sensor_dict = None + + def request_data(self, url): + try: + r = requests.get(url) + data = r.json() + except Exception as e: + return {} + return data def input_type_selector(self, unit): # Set input type. @@ -37,56 +33,17 @@ def input_type_selector(self, unit): "W": "power" }.get(unit, unit) - def input_name_selector(self, sensor_name, input_name): - - self.sensor_name = { - "syscpld-i2c-0-0d": "TEMPERATURE", - "dps1100-i2c-24-58": "PSU1", - "dps1100-i2c-25-59": "PSU2", - "fancpld-i2c-8-0d": "FAN" - }.get(sensor_name, sensor_name) - - if 'dps1100' in sensor_name: - input_name = { - "fan1": self.sensor_name + "_FAN", - "iin": self.sensor_name + "_CURR_I", - "iout1": self.sensor_name + "_CURR_O", - "pin": self.sensor_name + "_POWER_I", - "pout1": self.sensor_name + "_POWER_O", - "temp1": self.sensor_name + "_TEMP1", - "temp2": self.sensor_name + "_TEMP2", - "vin": self.sensor_name + "_VOL_I", - "vout1": self.sensor_name + "_VOL_O" - }.get(input_name, input_name) - - elif 'tmp75' in sensor_name: - input_name = { - "tmp75-i2c-7-4d": "FTB_INLET_RIGHT", - "tmp75-i2c-7-4c": "FTB_INLET_LEFT", - "tmp75-i2c-7-4b": "FTB_SWITCH_OUTLET", - "tmp75-i2c-7-4a": "BTF_SWITCH_OUTLET", - "tmp75-i2c-39-48": "BTF_INLET_RIGHT", - "tmp75-i2c-39-49": "BTF_INLET_LEFT" - }.get(sensor_name, input_name) - if self.get_sys_airflow() == "FTOB" and sensor_name == "tmp75-i2c-7-4d": - input_name = "INLET_TEMP" - - if self.get_sys_airflow() == "BTOF" and sensor_name == "tmp75-i2c-39-48": - input_name = "INLET_TEMP" - - self.sensor_name = "TEMPERATURE" - - elif 'fancpld' in sensor_name: - raw_fan_input = input_name.split() - input_name = raw_fan_input[0] + \ - raw_fan_input[1] + "_" + raw_fan_input[2] - - elif 'ir35' in sensor_name or 'ir38' in sensor_name: - sensor_name_raw = sensor_name.split("-") - sensor_name = sensor_name_raw[0] - self.sensor_name = sensor_name.upper() - - return input_name.replace(" ", "_").upper() + def input_name_selector(self, raw_sensor_name): + + sensor_name_list = raw_sensor_name.split('_') + sensor_name = sensor_name_list[0] + input_name = '_'.join(sensor_name_list[1:]) + + if sensor_name_list[0] in ["TOP", "BOTTOM"]: + sensor_name = '_'.join(sensor_name_list[0:2]) + input_name = '_'.join(sensor_name_list[2:]) + + return str(sensor_name).upper(), str(input_name).upper() def get_num_sensors(self): """ @@ -94,18 +51,9 @@ def get_num_sensors(self): :return: int num_sensors """ - num_sensors = 0 - try: - # Request and validate sensor's information - self.sensor_info_list = self.request_data() - - # Get number of sensors. - num_sensors = len(self.sensor_info_list) - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() - return num_sensors + return len(all_sensor_dict) def get_sensor_input_num(self, index): """ @@ -113,19 +61,12 @@ def get_sensor_input_num(self, index): :return: int input_num """ - input_num = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - - # Get sensor's input number. - sensor_data = self.sensor_info_list[index-1] - input_num = len(sensor_data.keys())-2 - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() + ss_keys = all_sensor_dict.keys()[index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys() - return input_num + return len(ss_if_keys) def get_sensor_name(self, index): """ @@ -133,18 +74,8 @@ def get_sensor_name(self, index): for example "coretemp-isa-0000" :return: str sensor_name """ - - sensor_name = "N/A" - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - - # Get sensor's name. - sensor_data = self.sensor_info_list[index-1] - sensor_name = sensor_data.get('name') - - except: - return "N/A" + all_sensor_dict = self.get_all() + sensor_name = all_sensor_dict.keys()[index] return sensor_name @@ -155,23 +86,13 @@ def get_sensor_input_name(self, sensor_index, input_index): :return: str sensor_input_name """ - sensor_input_name = "N/A" - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] + all_sensor_dict = self.get_all() - # Get sensor's input name. - sensor_data_key = sensor_data.keys() - sensor_input_name = sensor_data_key[input_index-1] - except: - return "N/A" + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] - return sensor_input_name + return ss_if_keys def get_sensor_input_type(self, sensor_index, input_index): """ @@ -180,24 +101,15 @@ def get_sensor_input_type(self, sensor_index, input_index): :return: str sensor_input_type """ - sensor_input_type = "N/A" - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input type name. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - sensor_input_type = self.input_type_selector(sensor_data_str[1]) - except: - return "N/A" + all_sensor_dict = self.get_all() + + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + + sensor_input_type = sensor_input_info.get('Type', "N/A") return sensor_input_type def get_sensor_input_value(self, sensor_index, input_index): @@ -206,26 +118,15 @@ def get_sensor_input_value(self, sensor_index, input_index): :return: float sensor_input_value """ - sensor_input_value = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input value. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - sensor_input_value = float( - sensor_data_str[0]) if sensor_data_str[0] != "N/A" else 0 - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() + + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + sensor_input_value = sensor_input_info.get('Value', 0.0) return sensor_input_value def get_sensor_input_low_threshold(self, sensor_index, input_index): @@ -235,33 +136,15 @@ def get_sensor_input_low_threshold(self, sensor_index, input_index): :return: float sensor_input_low_threshold """ - sensor_input_low_threshold = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input low threshold. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - indices = [i for i, s in enumerate( - sensor_data_str) if 'min' in s or 'low' in s] - l_thres = float( - sensor_data_str[indices[0] + 2]) if len(indices) != 0 else 0 - unit = sensor_data_str[indices[0] + - 3] if len(indices) != 0 else None - if unit is not None and len(unit) > 1: - sensor_input_low_threshold = l_thres * \ - 1000 if str(unit[0]).lower() == 'k' else l_thres - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() + + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + + sensor_input_low_threshold = sensor_input_info.get('LowThd', 0.0) return sensor_input_low_threshold def get_sensor_input_high_threshold(self, sensor_index, input_index): @@ -270,100 +153,54 @@ def get_sensor_input_high_threshold(self, sensor_index, input_index): the status of this item is not ok if the current value > high_threshold :return: float sensor_input_high_threshold """ + all_sensor_dict = self.get_all() - sensor_input_high_threshold = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input high threshold. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - indices = [i for i, s in enumerate( - sensor_data_str) if 'max' in s or 'high' in s] - h_thres = float( - sensor_data_str[indices[0] + 2]) if len(indices) != 0 else 0 - unit = sensor_data_str[indices[0] + - 3] if len(indices) != 0 else None - if unit is not None and len(unit) > 1: - sensor_input_high_threshold = h_thres * \ - 1000 if str(unit[0]).lower() == 'k' else h_thres - - except: - print "Error: Unable to access sensor information" - return 0 + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + + sensor_input_high_threshold = sensor_input_info.get('HighThd', 0.0) return sensor_input_high_threshold - def get_sys_airflow(self): - sys_air_flow = "Unknown" - sys_pn_data = [ - v.split(":") for v in self.sys_fruid_list if "Product Part Number" in v] + def get_all(self): + """ + Get all information of system sensors, returns JSON objects in python 'DICT'. + SensorName1, SensorName2, ... optional, string + SensorInput1, SensorInput2, ... optional, string + Type, mandatory in SensorInput$INDEX, should be on of { "temperature", "voltage", "power", "amp", "RPM" } + Value, mandatory in SensorInput$INDEX, float , real value + LowThd, mandatory in SensorInput$INDEX, float , lower bound of value + HighThd, mandatory in SensorInput$INDEX, float , upper bound of value + Return python 'dict' objects, example: + """ + + if not self.all_sensor_dict: + all_sensor_dict = dict() - if len(sys_pn_data) == 0: - return sys_air_flow + sensor_info_req = self.request_data(self.sensor_info_url) + sensor_info_data = sensor_info_req.get('data', {}) - sys_pn = sys_pn_data[0][1] - if "R1241-F0001" in sys_pn: - sys_air_flow = "FTOB" - elif"R1241-F0002" in sys_pn: - sys_air_flow = "BTOF" + for raw_ss_name, sensor_info in sensor_info_data.items(): - return sys_air_flow + sensor_name, input_name = self.input_name_selector(raw_ss_name) + sensor_dict = all_sensor_dict.get(sensor_name, {}) + new_sensor_dict = dict() + new_sensor_dict["Type"] = self.input_type_selector( + sensor_info.get('Unit', 'N/A')) + new_sensor_dict["Value"] = float(sensor_info.get('Value', 0.0)) + new_sensor_dict["HighThd"] = float(sensor_info.get('Max', 0.0)) + new_sensor_dict["LowThd"] = float(sensor_info.get('Min', 0.0)) - def get_all(self): + if sensor_dict == {}: + all_sensor_dict[sensor_name] = dict() + + input_name = input_name if input_name != '' else new_sensor_dict["Type"].upper( + ) + sensor_dict[input_name] = new_sensor_dict + all_sensor_dict[sensor_name].update(sensor_dict) + + self.all_sensor_dict = all_sensor_dict - all_sensor_dict = dict() - - # Request sensor's information. - self.sensor_info_list = self.request_data() - for sensor_data in self.sensor_info_list: - sensor_info = sensor_data.copy() - - # Remove none unuse key. - del sensor_info["name"] - del sensor_info["Adapter"] - - # Set sensor data. - sensor_dict = dict() - for k, v in sensor_info.items(): - sensor_i_dict = dict() - sensor_data_str = v.split() - indices_h = [i for i, s in enumerate( - sensor_data_str) if 'max' in s or 'high' in s] - indices_l = [i for i, s in enumerate( - sensor_data_str) if 'min' in s or 'low' in s] - h_thres = float( - sensor_data_str[indices_h[0] + 2]) if len(indices_h) != 0 else 0 - l_thres = float( - sensor_data_str[indices_l[0] + 2]) if len(indices_l) != 0 else 0 - thres_unit = sensor_data_str[-1] - - sensor_i_dict["Type"] = self.input_type_selector( - sensor_data_str[1]) - sensor_i_dict["Value"] = float( - sensor_data_str[0]) if sensor_data_str[0] != "N/A" else 0 - sensor_i_dict["HighThd"] = h_thres * \ - 1000 if str(thres_unit[0]).lower() == 'k' else h_thres - sensor_i_dict["LowThd"] = l_thres * \ - 1000 if str(thres_unit[0]).lower() == 'k' else l_thres - - k = self.input_name_selector(sensor_data.get('name'), k) - sensor_dict[k] = sensor_i_dict - - if all_sensor_dict.get(self.sensor_name) is None: - all_sensor_dict[self.sensor_name] = dict() - - all_sensor_dict[self.sensor_name].update(sensor_dict) - - sensor_dict = dict() - sensor_dict["Sys_AirFlow"] = self.get_sys_airflow() - all_sensor_dict["TEMPERATURE"].update(sensor_dict) - - return all_sensor_dict + return self.all_sensor_dict diff --git a/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/eeprom.py b/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/eeprom.py index 9f42b56ce7c2..0b854bd1cef2 100644 --- a/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/eeprom.py +++ b/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/eeprom.py @@ -1,7 +1,7 @@ #!/usr/bin/env python ############################################################################# -# Celestica DX010 +# Celestica FISHBONE # # Platform and model specific eeprom subclass, inherits from the base class, # and provides the followings: diff --git a/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/fanutil.py b/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/fanutil.py index dab2f64dd5e0..791fc35ca4db 100644 --- a/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/fanutil.py +++ b/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/fanutil.py @@ -2,51 +2,40 @@ __author__ = 'Wirut G.' __license__ = "GPL" -__version__ = "0.1.2" +__version__ = "0.2.0" __status__ = "Development" import requests import re +NUM_FAN_TRAY = 4 +NUM_ROTER = 2 + class FanUtil(): """Platform-specific FanUtil class""" def __init__(self): + self.fan_info_url = "http://240.1.1.1:8080/api/fan/info" + self.all_fan_dict = None - self.fan_fru_url = "http://240.1.1.1:8080/api/sys/fruid/fan" - self.sensor_url = "http://240.1.1.1:8080/api/sys/sensors" - self.fru_data_list = None - self.sensor_data_list = None - - def request_data(self): - # Reqest data from BMC if not exist. - if self.fru_data_list is None or self.sensor_data_list is None: - fru_data_req = requests.get(self.fan_fru_url) - sensor_data_req = requests.get(self.sensor_url) - fru_json = fru_data_req.json() - sensor_json = sensor_data_req.json() - self.fru_data_list = fru_json.get('Information') - self.sensor_data_list = sensor_json.get('Information') - return self.fru_data_list, self.sensor_data_list - - def name_to_index(self, fan_name): - # Get fan index from fan name - match = re.match(r"(FAN)([0-9]+)-(1|2)", fan_name, re.I) - fan_index = None - if match: - i_list = list(match.groups()) - fan_index = int(i_list[1])*2 - (int(i_list[2]) % 2) - return fan_index + def request_data(self, url): + try: + r = requests.get(url) + data = r.json() + except Exception as e: + return {} + return data def get_num_fans(self): """ Get the number of fans :return: int num_fans """ - num_fans = 8 + all_fan_dict = self.get_all() + num_fan_tray = all_fan_dict.get('Number', NUM_FAN_TRAY) - return num_fans + return num_fan_tray * NUM_ROTER def get_fan_speed(self, fan_name): """ @@ -54,31 +43,10 @@ def get_fan_speed(self, fan_name): :return: int fan_speed """ - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_speed = 0 - position_key = "Front" if index % 2 != 0 else "Rear" - index = int(round(float(index)/2)) - fan_key = "Fan " + str(index) + " " + position_key + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's speed. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_speed = fan_sp_list[0] - - except: - return 0 - - return fan_speed + return fan_info.get('Speed', 0) def get_fan_low_threshold(self, fan_name): """ @@ -88,31 +56,10 @@ def get_fan_low_threshold(self, fan_name): :return: int fan_low_threshold """ - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_low_threshold = 0 - position_key = "Front" if index % 2 != 0 else "Rear" - index = int(round(float(index)/2)) - fan_key = "Fan " + str(index) + " " + position_key - - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's threshold. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_low_threshold = fan_sp_list[1] - - except: - return "N/A" + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - return fan_low_threshold + return fan_info.get('LowThd', 0) def get_fan_high_threshold(self, fan_name): """ @@ -121,32 +68,10 @@ def get_fan_high_threshold(self, fan_name): the status of the fan is not ok :return: int fan_high_threshold """ + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_high_threshold = 0 - position_key = "Front" if index % 2 != 0 else "Rear" - index = int(round(float(index)/2)) - fan_key = "Fan " + str(index) + " " + position_key - - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's threshold. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_high_threshold = fan_sp_list[2] - - except: - return 0 - - return fan_high_threshold + return fan_info.get('HighThd', 0) def get_fan_pn(self, fan_name): """ @@ -154,58 +79,20 @@ def get_fan_pn(self, fan_name): :return: str fan_pn """ - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_pn = "N/A" - index = int(round(float(index)/2)) - fan_fru_key = "Fantray" + str(index) + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's fru. - for fan_fru in self.fru_data_list: - matching_fan = [s for s in fan_fru if fan_fru_key in s] - if matching_fan: - pn = [s for s in fan_fru if "Part" in s] - fan_pn = pn[0].split()[4] - - except: - return "N/A" - - return fan_pn + return fan_info.get('PN', 'N/A') def get_fan_sn(self, fan_name): """ Get the serial number of the fan :return: str fan_sn """ - try: - # Get real fan index - index = self.name_to_index(fan_name) + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - # Set key and index. - fan_sn = "N/A" - index = int(round(float(index)/2)) - fan_fru_key = "Fantray" + str(index) - - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's fru. - for fan_fru in self.fru_data_list: - matching_fan = [s for s in fan_fru if fan_fru_key in s] - if matching_fan: - serial = [s for s in fan_fru if "Serial" in s] - fan_sn = serial[0].split()[3] - - except: - return "N/A" - - return fan_sn + return fan_info.get('SN', 'N/A') def get_fans_name_list(self): """ @@ -240,71 +127,40 @@ def get_all(self): SN, conditional, if PRESENT is True, SN of the FAN, string) """ - self.fru_data_list, self.sensor_data_list = self.request_data() - all_fan_dict = dict() - - # Get the number of fans - n_fan = self.get_num_fans() - all_fan_dict["Number"] = n_fan - - # Set fan FRU data. - fan_fru_dict = dict() - fan_raw_idx = 1 - for fan_fru in self.fru_data_list: - fru_dict = dict() - fan_ps = False - - if len(fan_fru) == 0: - fan_idx = fan_raw_idx - fan_pn = "N/A" - fan_sn = "N/A" - else: - fan_key = fan_fru[0].split() - if str(fan_key[-1]).lower() == "absent": - fan_idx = int(re.findall('\d+', fan_key[0])[0]) - - else: - fan_idx = int(re.findall('\d+', fan_key[-1])[0]) - fan_ps = True - pn = [s for s in fan_fru if "Part" in s] - sn = [s for s in fan_fru if "Serial" in s] - fan_pn = pn[0].split( - ":")[-1].strip() if len(pn) > 0 else 'N/A' - fan_sn = sn[0].split( - ":")[-1].strip() if len(sn) > 0 else 'N/A' - - fru_dict["PN"] = "N/A" if not fan_pn or fan_pn == "" else fan_pn - fru_dict["SN"] = "N/A" if not fan_sn or fan_sn == "" else fan_sn - fru_dict["Present"] = fan_ps - fan_fru_dict[fan_idx] = fru_dict - fan_raw_idx += 1 - - # Set fan sensor data. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - for x in range(1, n_fan + 1): - fan_dict = dict() - f_index = int(round(float(x)/2)) - pos = 1 if x % 2 else 2 - position_key = "Front" if x % 2 != 0 else "Rear" - fan_key = "Fan " + str(f_index) + " " + position_key - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_dict["Present"] = fan_fru_dict[f_index]["Present"] - if fan_dict["Present"] or fan_sp_list[0] > 0: - fan_dict["Present"] = True - fan_dict["Speed"] = fan_sp_list[0] - fan_dict["Running"] = True if fan_dict["Speed"] > 0 else False - fan_dict["LowThd"] = fan_sp_list[1] - fan_dict["HighThd"] = fan_sp_list[2] - fan_dict["PN"] = fan_fru_dict[f_index]["PN"] - fan_dict["SN"] = fan_fru_dict[f_index]["SN"] - fan_dict["AirFlow"] = "FTOB" if "R1241-F9001" in fan_dict["PN"] else "Unknown" - fan_dict["AirFlow"] = "BTOF" if "R1241-F9002" in fan_dict["PN"] else fan_dict["AirFlow"] - fan_dict["Status"] = True if fan_dict["AirFlow"] != "Unknown" else False - fan_name = 'FAN{}_{}'.format(f_index, pos) - all_fan_dict[fan_name] = fan_dict - break - - return all_fan_dict + if not self.all_fan_dict: + all_fan_dict = dict() + + fan_info_req = self.request_data(self.fan_info_url) + fan_info_data = fan_info_req.get('data', {}) + all_fan_dict["Number"] = fan_info_data.get('Number', NUM_FAN_TRAY) + + for fan_idx in range(1, all_fan_dict["Number"] + 1): + num_of_roter = fan_info_data.get('Rotors', NUM_ROTER) + + for fan_pos in range(1, num_of_roter + 1): + fan_key = 'FAN{}'.format(str(fan_idx)) + roter_key = 'Rotor{}'.format(str(fan_pos)) + + fan_info = fan_info_data.get(fan_key, {}) + roter_info = fan_info.get(roter_key, {}) + + fan_info_dict = dict() + fan_info_dict["Present"] = True if fan_info.get( + "Present") == 'yes' else False + fan_info_dict["Speed"] = roter_info.get("Speed", "N/A") + fan_info_dict["Running"] = True if roter_info.get( + "Running") == 'yes' else False + fan_info_dict["HighThd"] = roter_info.get( + "SpeedMax", "N/A") + fan_info_dict["LowThd"] = roter_info.get("SpeedMin", "N/A") + fan_info_dict["Status"] = False if roter_info.get( + "HwAlarm") == 'yes' else True + fan_info_dict["PN"] = fan_info.get("PN", "N/A") + fan_info_dict["SN"] = fan_info.get("SN", "N/A") + fan_info_dict["AirFlow"] = fan_info.get("AirFlow", "N/A") + + fan_name = '{}_{}'.format(fan_key, fan_pos) + all_fan_dict[fan_name] = fan_info_dict + self.all_fan_dict = all_fan_dict + + return self.all_fan_dict diff --git a/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/fwmgrutil.py b/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/fwmgrutil.py index 80d2b960b37f..8ae29d09bcad 100644 --- a/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/fwmgrutil.py +++ b/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/fwmgrutil.py @@ -1,8 +1,15 @@ +#!/usr/bin/env python +# # fwmgrutil.py # # Platform-specific firmware management interface for SONiC # +__author__ = 'Wirut G.' +__license__ = "GPL" +__version__ = "0.2.0" +__status__ = "Development" + import subprocess import requests import os @@ -27,9 +34,15 @@ class FwMgrUtil(FwMgrUtilBase): def __init__(self): self.platform_name = "AS1348f8h" self.onie_config_file = "/host/machine.conf" - self.bmc_info_url = "http://240.1.1.1:8080/api/sys/bmc" - self.bmc_raw_command_url = "http://240.1.1.1:8080/api/sys/raw" - self.fw_upgrade_url = "http://240.1.1.1:8080/api/sys/upgrade" + self.bmc_info_url = "http://240.1.1.1:8080/api/bmc/info" + self.cpld_info_url = "http://240.1.1.1:8080/api/firmware/cpldversion" + self.bmc_raw_command_url = "http://240.1.1.1:8080/api/hw/rawcmd" + self.fw_upgrade_url = "http://240.1.1.1:8080/api/firmware/upgrade" + self.fw_refresh_url = "http://240.1.1.1:8080/api/firmware/refresh" + self.bios_next_boot = "http://240.1.1.1:8080/api/firmware/biosnextboot" + self.bmc_next_boot = "http://240.1.1.1:8080/api/bmc/nextboot" + self.bmc_reboot_url = "http://240.1.1.1:8080/api/bmc/reboot" + self.bios_boot_info = "http://240.1.1.1:8080/api/misc/biosbootstatus" self.onie_config_file = "/host/machine.conf" self.fw_upgrade_logger_path = "/var/log/fw_upgrade.log" self.cpldb_version_path = "/sys/devices/platform/%s.cpldb/getreg" % self.platform_name @@ -39,6 +52,9 @@ def __init__(self): self.switchboard_cpld3_path = "/sys/devices/platform/%s.switchboard/CPLD3/getreg" % self.platform_name self.switchboard_cpld4_path = "/sys/devices/platform/%s.switchboard/CPLD4/getreg" % self.platform_name self.bmc_pwd_path = "/usr/local/etc/bmcpwd" + self.cpld_name_list = ["CPU_CPLD", "COMBO_CPLD", + "SW_CPLD1", "SW_CPLD2", "TOP_LC_CPLD", "BOT_LC_CPLD"] + self.api_time_out = 300 def __get_register_value(self, path, register): cmd = "echo {1} > {0}; cat {0}".format(path, register) @@ -89,12 +105,12 @@ def get_bmc_version(self): """ bmc_version = None - - bmc_version_key = "OpenBMC Version" - bmc_info_req = requests.get(self.bmc_info_url, timeout=60) + bmc_version_key = "Version" + bmc_info_req = requests.get( + self.bmc_info_url, timeout=self.api_time_out) if bmc_info_req.status_code == 200: bmc_info_json = bmc_info_req.json() - bmc_info = bmc_info_json.get('Information') + bmc_info = bmc_info_json.get('data') bmc_version = bmc_info.get(bmc_version_key) return str(bmc_version) @@ -102,7 +118,8 @@ def upload_file_bmc(self, fw_path): scp_command = 'sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r %s root@240.1.1.1:/home/root/' % os.path.abspath( fw_path) child = pexpect.spawn(scp_command) - i = child.expect(["root@240.1.1.1's password:"], timeout=30) + child.timeout = self.api_time_out + i = child.expect(["root@240.1.1.1's password:"]) bmc_pwd = self.get_bmc_pass() if i == 0 and bmc_pwd: child.sendline(bmc_pwd) @@ -124,13 +141,13 @@ def get_cpld_version(self): CPLD_3 = self.__get_register_value(self.switchboard_cpld3_path, '0x00') CPLD_4 = self.__get_register_value(self.switchboard_cpld4_path, '0x00') - fan_cpld_key = "FanCPLD Version" + fan_cpld_key = "CPLD_FAN" fan_cpld = None - bmc_info_req = requests.get(self.bmc_info_url) - if bmc_info_req.status_code == 200: - bmc_info_json = bmc_info_req.json() - bmc_info = bmc_info_json.get('Information') - fan_cpld = bmc_info.get(fan_cpld_key) + fan_cpld_req = requests.get(self.cpld_info_url) + if fan_cpld_req.status_code == 200: + fancpld_info_json = fan_cpld_req.json() + fancpld_info_data = fancpld_info_json.get('data') + fan_cpld = fancpld_info_data.get(fan_cpld_key) CPLD_B = 'None' if CPLD_B is 'None' else "{}.{}".format( int(CPLD_B[2], 16), int(CPLD_B[3], 16)) @@ -144,8 +161,7 @@ def get_cpld_version(self): int(CPLD_3[2], 16), int(CPLD_3[3], 16)) CPLD_4 = 'None' if CPLD_4 is 'None' else "{}.{}".format( int(CPLD_4[2], 16), int(CPLD_4[3], 16)) - FAN_CPLD = 'None' if fan_cpld is None else "{}.{}".format( - int(fan_cpld[0], 16), int(fan_cpld[1], 16)) + FAN_CPLD = 'None' if fan_cpld is None else fan_cpld cpld_version_dict = {} cpld_version_dict.update({'CPLD_B': CPLD_B}) @@ -174,7 +190,7 @@ def get_bios_version(self): bios_version = raw_data_list[0] if len( raw_data_list) == 1 else raw_data_list[-2] - return str(bios_version) + return str(bios_version).strip() def get_onie_version(self): """Get ONiE version from SONiC @@ -224,7 +240,7 @@ def get_fpga_version(self): version = self.__get_register_value(self.fpga_version_path, '0x00') if version is not 'None': version = "{}.{}".format( - int(version[2:][:4], 16), int(version[2:][4:], 16)) + int(version[2:][1:4], 16), int(version[2:][4:], 16)) return str(version) def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): @@ -245,10 +261,13 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): return False if fw_type == 'bmc': + fw_extra_str = str(fw_extra).lower() + if fw_extra_str not in ["master", "slave", "both", "pingpong"]: + return False + self.__update_fw_upgrade_logger( "bmc_upgrade", "start BMC upgrade") # Copy BMC image file to BMC - fw_extra_str = str(fw_extra).lower() last_fw_upgrade = ["BMC", fw_path, fw_extra_str, "FAILED"] upload_file = self.upload_file_bmc(fw_path) if not upload_file: @@ -260,8 +279,8 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): filename_w_ext = os.path.basename(fw_path) json_data = dict() - json_data["path"] = "root@127.0.0.1:/home/root/%s" % filename_w_ext - json_data["password"] = bmc_pwd + json_data["Name"] = "bmc" + json_data["Path"] = "/home/root/%s" % filename_w_ext # Set flash type current_bmc = self.get_running_bmc() @@ -270,26 +289,26 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): if fw_extra_str == "pingpong": #flash = "master" if current_bmc == "slave" else "slave" flash = "slave" - json_data["flash"] = flash + json_data["Flash"] = flash # Install BMC if flash == "both": self.__update_fw_upgrade_logger( "bmc_upgrade", "install BMC as master mode") - json_data["flash"] = "master" - r = requests.post(self.bmc_info_url, json=json_data) - if r.status_code != 200 or 'success' not in r.json().get('result'): + json_data["Flash"] = "master" + r = requests.post(self.fw_upgrade_url, json=json_data) + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "bmc_upgrade", "fail, message=BMC API report error code %d" % r.status_code) + "bmc_upgrade", "fail, message={}".format(r.json().get('messages'))) self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False - json_data["flash"] = "slave" + json_data["Flash"] = "slave" self.__update_fw_upgrade_logger( - "bmc_upgrade", "install BMC as %s mode" % json_data["flash"]) - r = requests.post(self.bmc_info_url, json=json_data) - if r.status_code == 200 and 'success' in r.json().get('result'): + "bmc_upgrade", "install BMC as %s mode" % json_data["Flash"]) + r = requests.post(self.fw_upgrade_url, json=json_data) + if r.status_code == 200 and r.json().get('status') == 'OK': if fw_extra_str == "pingpong": flash = "master" if current_bmc == "slave" else "slave" self.__update_fw_upgrade_logger( @@ -308,7 +327,7 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): last_fw_upgrade[3] = "DONE" else: self.__update_fw_upgrade_logger( - "bmc_upgrade", "fail, message=unable to install BMC image") + "bmc_upgrade", "fail, message={}".format(r.json().get('messages'))) self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False @@ -419,18 +438,15 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): filename_w_ext = os.path.basename(fw_path) json_data = dict() - json_data["image_path"] = "root@127.0.0.1:/home/root/%s" % filename_w_ext - json_data["password"] = bmc_pwd - json_data["device"] = "cpld" - json_data["reboot"] = "no" - json_data["type"] = fw_extra_str + json_data["Path"] = "/home/root/%s" % filename_w_ext + json_data["Name"] = "cpld" # Call BMC api to install cpld image print("Installing...") r = requests.post(self.fw_upgrade_url, json=json_data) - if r.status_code != 200 or 'success' not in r.json().get('result'): + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "cpld_upgrade", "fail, message=invalid cpld image") + "cpld_upgrade", "fail, message={}".format(r.json().get('messages'))) continue cpld_result_list[i] = "DONE" @@ -455,57 +471,50 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): return True elif 'bios' in fw_type: + fw_extra_str = str(fw_extra).lower() + if fw_extra_str not in ["master", "slave", "both"]: + return False + self.__update_fw_upgrade_logger( "bios_upgrade", "start BIOS upgrade") last_fw_upgrade = ["BIOS", fw_path, None, "FAILED"] fw_extra_str = str(fw_extra).lower() - flash = fw_extra_str if fw_extra_str in [ - "master", "slave"] else "master" + flash = fw_extra_str.lower() if not os.path.exists(fw_path): self.__update_fw_upgrade_logger( "bios_upgrade", "fail, message=image not found") return False - scp_command = 'sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r %s root@240.1.1.1:/home/root/' % os.path.abspath( - fw_path) - child = pexpect.spawn(scp_command) - i = child.expect(["root@240.1.1.1's password:"], timeout=30) - if i != 0: + upload_file = self.upload_file_bmc(fw_path) + if not upload_file: self.__update_fw_upgrade_logger( "bios_upgrade", "fail, message=unable to upload image to BMC") self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False - child.sendline(bmc_pwd) - data = child.read() - print(data) - child.close - - json_data = dict() - json_data["data"] = "/usr/bin/ipmitool -b 1 -t 0x2c raw 0x2e 0xdf 0x57 0x01 0x00 0x01" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "bios_upgrade", "fail, message=unable to set state") - self.__update_fw_upgrade_logger( - "last_upgrade_result", str(last_fw_upgrade)) - return False + # json_data = dict() + # json_data["Command"] = "/usr/bin/ipmitool -b 1 -t 0x2c raw 0x2e 0xdf 0x57 0x01 0x00 0x01" + # r = requests.post(self.bmc_raw_command_url, json=json_data) + # if r.status_code != 200: + # self.__update_fw_upgrade_logger( + # "bios_upgrade", "fail, message=unable to set state") + # self.__update_fw_upgrade_logger( + # "last_upgrade_result", str(last_fw_upgrade)) + # return False filename_w_ext = os.path.basename(fw_path) json_data = dict() - json_data["image_path"] = "root@127.0.0.1:/home/root/%s" % filename_w_ext - json_data["password"] = bmc_pwd - json_data["device"] = "bios" - json_data["flash"] = flash - json_data["reboot"] = "no" + json_data["Path"] = "/home/root/%s" % filename_w_ext + json_data["Name"] = "bios" + json_data["Flash"] = flash print("Installing...") r = requests.post(self.fw_upgrade_url, json=json_data) - if r.status_code != 200 or 'success' not in r.json().get('result'): + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "bios_upgrade", "fail, message=unable install bios") + "bios_upgrade", "fail, message={}".format(r.json().get('messages'))) self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False @@ -742,77 +751,40 @@ def firmware_refresh(self, fpga_list, cpld_list, fw_extra=None): self.firmware_refresh(None, ["FAN_CPLD", "LC1_CPLD", "BASE_CPLD"], "/tmp/fw/fan_refresh.vme:none:/tmp/fw/base_refresh.vme") """ + self.__update_fw_upgrade_logger( + "fw_refresh", "start firmware refresh") - if not fpga_list and not cpld_list: - return False + cpld_list = [x.lower() for x in cpld_list] if cpld_list else [] + fpga_list = [x.lower() for x in fpga_list] if fpga_list else [] + refresh_list = cpld_list + fpga_list + fw_path_list = str(fw_extra).split(':') + refresh_img_list = ["none" for i in range(len(refresh_list))] - if type(cpld_list) is list and ("BASE_CPLD" in cpld_list or "FAN_CPLD" in cpld_list): - refresh_list = fpga_list + \ - cpld_list if type(fpga_list) is list else cpld_list + if len(refresh_list) == 0 : self.__update_fw_upgrade_logger( - "fw_refresh", "start %s refresh" % ",".join(refresh_list)) - fw_path_list = fw_extra.split(':') - command = "echo " - if len(fw_path_list) != len(cpld_list): - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=Invalid fw_extra") - return False - - for idx in range(0, len(cpld_list)): - fw_path = fw_path_list[idx] - refresh_type = { - "BASE_CPLD": "base", - "FAN_CPLD": "fan" - }.get(cpld_list[idx], None) + "fw_refresh", "fail, message=Invalid input") + return False - if not refresh_type: - continue - elif not self.upload_file_bmc(fw_path): + for idx in range(0, len(refresh_list)): + if refresh_list[idx] in ["fan_cpld", "base_cpld"]: + if not self.upload_file_bmc(fw_path_list[idx]): self.__update_fw_upgrade_logger( "cpld_refresh", "fail, message=Unable to upload refresh image to BMC") return False - else: - filename_w_ext = os.path.basename(fw_path) - - sub_command = "%s /home/root/%s > /tmp/cpld_refresh " % ( - refresh_type, filename_w_ext) - command += sub_command + refresh_img_list[idx] = "/home/root/%s" % os.path.basename( + fw_path_list[idx]) - json_data = dict() - json_data["data"] = command - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=%d Invalid refresh image" % r.status_code) - return False - elif type(cpld_list) is list: - refresh_list = fpga_list + \ - cpld_list if type(fpga_list) is list else cpld_list - self.__update_fw_upgrade_logger( - "fw_refresh", "start %s refresh" % ",".join(refresh_list)) - json_data = dict() - json_data["data"] = "echo cpu_cpld > /tmp/cpld_refresh" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=%d Unable to load new CPLD" % r.status_code) - return False - elif type(fpga_list) is list: - self.__update_fw_upgrade_logger( - "fw_refresh", "start FPGA refresh") - json_data = dict() - json_data["data"] = "echo fpga > /tmp/cpld_refresh" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=%d Unable to load new FPGA" % r.status_code) - return False - else: + json_data = dict() + json_data["Paths"] = refresh_img_list + json_data["Names"] = refresh_list + r = requests.post(self.fw_refresh_url, json=json_data) + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "fw_refresh", "fail, message=Invalid input") + "cpld_refreshfw", "fail, message={}".format(r.json().get('messages'))) return False self.__update_fw_upgrade_logger("fw_refresh", "done") + return True def get_running_bmc(self): @@ -820,21 +792,15 @@ def get_running_bmc(self): Get booting flash of running BMC. @return a string, "master" or "slave" """ - json_data = dict() - json_data["data"] = "/usr/local/bin/boot_info.sh" - r = requests.post(self.bmc_raw_command_url, json=json_data) - try: - boot_info_list = r.json().get('result') - for boot_info_raw in boot_info_list: - boot_info = boot_info_raw.split(":") - if "Current Boot Code Source" in boot_info[0]: - flash = "master" if "master "in boot_info[1].lower( - ) else "slave" - return flash - raise Exception( - "Error: Unable to detect booting flash of running BMC") - except Exception as e: - raise Exception(e) + running_bmc = "master" + running_bmc_key = "Flash" + bmc_info_req = requests.get( + self.bmc_info_url, timeout=self.api_time_out) + if bmc_info_req.status_code == 200: + bmc_info_json = bmc_info_req.json() + bmc_info = bmc_info_json.get('data') + running_bmc = bmc_info.get(running_bmc_key) + return str(running_bmc).lower() def set_bmc_boot_flash(self, flash): """ @@ -844,8 +810,8 @@ def set_bmc_boot_flash(self, flash): if flash.lower() not in ["master", "slave"]: return False json_data = dict() - json_data["data"] = "source /usr/local/bin/openbmc-utils.sh;bmc_reboot %s" % flash - r = requests.post(self.bmc_raw_command_url, json=json_data) + json_data["Flash"] = flash + r = requests.post(self.bmc_next_boot, json=json_data) if r.status_code != 200: return False return True @@ -854,10 +820,13 @@ def reboot_bmc(self): """ Reboot BMC """ - json_data = dict() - json_data["data"] = "source /usr/local/bin/openbmc-utils.sh;bmc_reboot reboot" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: + try: + r = requests.post(self.bmc_reboot_url) + if r.status_code != 200: + return False + except Exception as e: + if "Connection aborted." in e.message[0]: + return True return False return True @@ -866,17 +835,39 @@ def get_current_bios(self): # Get booting bios image of current running host OS # @return a string, "master" or "slave" """ + bios_boot_info = requests.get( + self.bios_boot_info, timeout=self.api_time_out) + if bios_boot_info.status_code == 200: + bios_boot_info_json = bios_boot_info.json() + bios_boot_info_data = bios_boot_info_json.get('data') + bios_boot = bios_boot_info_data.get("Flash") + return str(bios_boot) + + def get_bios_next_boot(self): + """ + # Get booting bios image of next booting host OS + # @return a string, "master" or "slave" + """ + bios_next_boot = "master" + bios_next_boot_info = requests.get( + self.bios_next_boot, timeout=self.api_time_out) + if bios_next_boot_info.status_code == 200: + bios_next_boot_info_json = bios_next_boot_info.json() + bios_next_boot_info_data = bios_next_boot_info_json.get('data') + bios_next_boot = bios_next_boot_info_data.get("Flash") + return str(bios_next_boot) + + def set_bios_next_boot(self, flash): + """ + # Set booting bios image of next booting host OS + # @return a string, "master" or "slave" + """ + if str(flash).lower() not in ['master', 'slave']: + return False + json_data = dict() - json_data["data"] = "source /usr/local/bin/openbmc-utils.sh;come_boot_info" - r = requests.post(self.bmc_raw_command_url, json=json_data) - try: - cpu_boot_info_list = r.json().get('result') - for cpu_boot_info_raw in cpu_boot_info_list: - if "COMe CPU boots from BIOS" in cpu_boot_info_raw: - bios_image = "master" if "master "in cpu_boot_info_raw.lower( - ) else "slave" - return bios_image - raise Exception( - "Error: Unable to detect current running bios image") - except Exception as e: - raise Exception(e) + json_data["Flash"] = str(flash).lower() + r = requests.post(self.bios_next_boot, json=json_data) + if r.status_code != 200: + return False + return True diff --git a/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/psuutil.py b/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/psuutil.py index d3a27117f5a2..b36cc0510918 100644 --- a/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/psuutil.py +++ b/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/psuutil.py @@ -2,7 +2,7 @@ __author__ = 'Wirut G.' __license__ = "GPL" -__version__ = "0.1.4" +__version__ = "0.2.0" __status__ = "Development" import requests @@ -13,43 +13,25 @@ except ImportError as e: raise ImportError(str(e) + "- required module not found") +NUM_PSU = 2 + class PsuUtil(PsuBase): """Platform-specific PSUutil class""" def __init__(self): PsuBase.__init__(self) - self.fru_status_url = "http://240.1.1.1:8080/api/sys/fruid/status" - self.psu_info_url = "http://240.1.1.1:8080/api/sys/fruid/psu" - - self.fru_status_list = None - self.psu_info_list = None - - def request_data(self): - # Reqest data from BMC if not exist. - if self.fru_status_list is None or self.psu_info_list is None: - fru_status_req = requests.get(self.fru_status_url) - psu_info_req = requests.get(self.psu_info_url) - fru_status_json = fru_status_req.json() - psu_info_json = psu_info_req.json() - self.fru_status_list = fru_status_json.get('Information') - self.psu_info_list = psu_info_json.get('Information') - return self.fru_status_list, self.psu_info_list - - def airflow_selector(self, pn): - # Set airflow type - pn = pn.upper() - if "DPS-1100FB" in pn: - airflow = "FTOB" - elif "DPS-1100AB" in pn: - airflow = "BTOF" - elif "FSJ026-A20G" in pn: - airflow = "FTOB" - elif "FSJ038-A20G" in pn: - airflow = "BTOF" - else: - airflow = "Unknown" - return airflow + + self.psu_info_url = "http://240.1.1.1:8080/api/psu/info" + self.all_psu_dict = None + + def request_data(self, url): + try: + r = requests.get(url) + data = r.json() + except Exception as e: + return {} + return data def get_num_psus(self): """ @@ -57,9 +39,9 @@ def get_num_psus(self): :return: An integer, the number of PSUs available on the device """ - num_psus = 2 + all_psu_dict = self.get_all() - return num_psus + return all_psu_dict.get('Number', NUM_PSU) def get_psu_status(self, index): """ @@ -69,28 +51,11 @@ def get_psu_status(self, index): :return: Boolean, True if PSU is operating properly, False if PSU is faulty """ - # init data - psu_key = "PSU" + str(index) - psu_status_key = "Power Status" - psu_power_status = False - - try: - # Request and validate sensor's information - self.fru_status_list, self.psu_info_list = self.request_data() + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) - # Get PSU power status. - for fru_status in self.fru_status_list: - is_psu = fru_status.get(psu_key) - psu_status = str(fru_status.get(psu_status_key)).strip() - - if is_psu is not None and psu_status == "OK": - psu_power_status = True - - except: - print("Error: Unable to access PSU power status") - return False - - return psu_power_status + return psu_info.get('PowerStatus', False) def get_psu_presence(self, index): """ @@ -99,29 +64,11 @@ def get_psu_presence(self, index): :param index: An integer, 1-based index of the PSU of which to query status :return: Boolean, True if PSU is plugged, False if not """ + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) - # Init data - psu_key = "PSU" + str(index) - psu_presence_key = "Present" - psu_presence_status = False - - try: - # Request and validate sensor's information. - self.fru_status_list, self.psu_info_list = self.request_data() - - # Get PSU present status. - for fru_status in self.fru_status_list: - is_psu = fru_status.get(psu_key) - psu_status = str(fru_status.get(psu_presence_key)).strip() - - if is_psu is not None and psu_status == "Present": - psu_presence_status = True - - except: - print("Error: Unable to access PSU presence status") - return False - - return psu_presence_status + return psu_info.get('Present', False) def get_psu_sn(self, index): """ @@ -130,25 +77,12 @@ def get_psu_sn(self, index): :param index: An integer, 1-based index of the PSU. :return: Serial number """ - serial_number = "N/A" - psu_key = "PSU" + str(index) + " FRU" - psu_sn_key = "Serial Number" - - try: - # Request and validate sensor's information. - self.fru_status_list, self.psu_info_list = self.request_data() - # Get PSU fru info. - for psu_fru in self.psu_info_list: - psu_sn = str(psu_fru.get(psu_sn_key)).strip() - if psu_fru.get(psu_key) is not None: - serial_number = psu_sn if psu_sn.strip() != "" else "N/A" - break + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) - except: - return "N/A" - - return serial_number + return psu_info.get('SN', False) def get_psu_pn(self, index): """ @@ -157,25 +91,12 @@ def get_psu_pn(self, index): :param index: An integer, 1-based index of the PSU. :return: Product name """ - product_name = "N/A" - psu_key = "PSU" + str(index) + " FRU" - psu_pn_key = "Product Name" - - try: - # Request and validate sensor's information - self.fru_status_list, self.psu_info_list = self.request_data() - - # Get PSU fru info. - for psu_fru in self.psu_info_list: - psu_pn = str(psu_fru.get(psu_pn_key)).strip() - if psu_fru.get(psu_key) is not None: - product_name = psu_pn if psu_pn.strip() != "" else "N/A" - break - - except: - return "N/A" - return product_name + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) + + return psu_info.get('PN', False) def get_all(self): """ @@ -187,62 +108,34 @@ def get_all(self): SN, conditional, if PRESENT is True, SN of the PSU, string """ - # Init data - all_psu_dict = dict() - all_psu_dict["Number"] = self.get_num_psus() - psu_sn_key_1 = "Serial Number" - psu_sn_key_2 = "Product Serial" - psu_pn_key = "Product Name" - - # Request and validate sensor's information. - self.fru_status_list, self.psu_info_list = self.request_data() - - # Set PSU FRU data. - psu_info_dict = dict() - for psu_fru in self.psu_info_list: - psu_data = dict() - pn = psu_fru.get(psu_pn_key) - sn = psu_fru.get(psu_sn_key_1) or psu_fru.get(psu_sn_key_2) - psu_data["PN"] = "N/A" if not pn or str( - pn).strip() == "" else str(pn).strip() - psu_data["SN"] = "N/A" if not pn or str( - pn).strip() == "" else str(sn).strip() - - fru_check = [psu_fru[v] for v in psu_fru.keys() if 'FRU Info' in v] - non_fru_check = [v for v in psu_fru.keys() if 'PSU' in v] - - if len(non_fru_check) > 0: - psu_idx = int(re.findall('\d+', non_fru_check[0])[0]) - psu_info_dict[psu_idx] = psu_data - elif len(fru_check) > 0: - psu_idx = int(re.findall('\d+', fru_check[0])[0]) - psu_info_dict[psu_idx] = psu_data - - # Set PSU status. - for fru_status in self.fru_status_list: - psu_status_dict = dict() - find_psu = [v for v in fru_status.keys() if "PSU" in v] - if len(find_psu) > 0: - psu_idx = int(re.findall('\d+', find_psu[0])[0]) - psu_ps_status = True if str(fru_status.get( - "Present")).strip() == "Present" else False - psu_pw_status = True if str(fru_status.get( - "Power Status")).strip() == "OK" else False - psu_pw_type = str(fru_status.get( - "Power Type")).strip() - ac_status = True if str(fru_status.get( - "AC Status")).strip().upper() == "OK" else False - - psu_status_dict["Present"] = psu_ps_status - if psu_ps_status: - psu_status_dict["PowerStatus"] = psu_pw_status - psu_status_dict["PN"] = psu_info_dict[psu_idx]["PN"] - psu_status_dict["SN"] = psu_info_dict[psu_idx]["SN"] - psu_status_dict["InputType"] = psu_pw_type - psu_status_dict["InputStatus"] = True if psu_pw_status and psu_ps_status else False - psu_status_dict["OutputStatus"] = ac_status - psu_status_dict["AirFlow"] = self.airflow_selector( - psu_status_dict["PN"]) - all_psu_dict[find_psu[0]] = psu_status_dict - - return all_psu_dict + if not self.all_psu_dict: + all_psu_dict = dict() + + psu_info_req = self.request_data(self.psu_info_url) + psu_info_data = psu_info_req.get('data', {}) + all_psu_dict["Number"] = psu_info_data.get('Number', NUM_PSU) + + for psu_idx in range(1, all_psu_dict["Number"] + 1): + psu_key = 'PSU{}'.format(str(psu_idx)) + psu_info = psu_info_data.get(psu_key, {}) + psu_input_info = psu_info.get('Inputs', {}) + psu_output_info = psu_info.get('Outputs', {}) + + psu_info_dict = dict() + psu_info_dict["InputType"] = psu_input_info.get("Type", "N/A") + psu_info_dict["InputStatus"] = True if psu_input_info.get( + "Status") else False + psu_info_dict["OutputStatus"] = True if psu_output_info.get( + "Status") else False + psu_info_dict["PowerStatus"] = ( + psu_info_dict["InputStatus"] and psu_info_dict["OutputStatus"]) + psu_info_dict["PN"] = psu_info.get("PN", "N/A") + psu_info_dict["SN"] = psu_info.get("SN", "N/A") + psu_info_dict["Present"] = True if psu_info.get("Present") == 'yes' else False + psu_info_dict["AirFlow"] = psu_info.get("AirFlow", "N/A") + + all_psu_dict[psu_key] = psu_info_dict + + self.all_psu_dict = all_psu_dict + + return self.all_psu_dict diff --git a/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/sensorutil.py b/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/sensorutil.py index 44e48e743b57..efed1e850306 100644 --- a/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/sensorutil.py +++ b/device/alibaba/x86_64-alibaba_as13-48f8h-cl-r0/plugins/sensorutil.py @@ -12,20 +12,16 @@ class SensorUtil(): """Platform-specific SensorUtil class""" def __init__(self): - self.sensor_url = "http://240.1.1.1:8080/api/sys/sensors" - self.sys_fruid_url = "http://240.1.1.1:8080/api/sys/fruid/sys" - self.sensor_info_list = None - - def request_data(self): - # Reqest data from BMC if not exist. - if self.sensor_info_list is None: - sensor_data_req = requests.get(self.sensor_url) - sensor_json = sensor_data_req.json() - self.sensor_info_list = sensor_json.get('Information') - sys_fruid_req = requests.get(self.sys_fruid_url) - sys_fruid_json = sys_fruid_req.json() - self.sys_fruid_list = sys_fruid_json.get('Information') - return self.sensor_info_list + self.sensor_info_url = "http://240.1.1.1:8080/api/sensor/info" + self.all_sensor_dict = None + + def request_data(self, url): + try: + r = requests.get(url) + data = r.json() + except Exception as e: + return {} + return data def input_type_selector(self, unit): # Set input type. @@ -37,56 +33,17 @@ def input_type_selector(self, unit): "W": "power" }.get(unit, unit) - def input_name_selector(self, sensor_name, input_name): - - self.sensor_name = { - "syscpld-i2c-0-0d": "TEMPERATURE", - "dps1100-i2c-24-58": "PSU1", - "dps1100-i2c-25-59": "PSU2", - "fancpld-i2c-8-0d": "FAN" - }.get(sensor_name, sensor_name) - - if 'dps1100' in sensor_name: - input_name = { - "fan1": self.sensor_name + "_FAN", - "iin": self.sensor_name + "_CURR_I", - "iout1": self.sensor_name + "_CURR_O", - "pin": self.sensor_name + "_POWER_I", - "pout1": self.sensor_name + "_POWER_O", - "temp1": self.sensor_name + "_TEMP1", - "temp2": self.sensor_name + "_TEMP2", - "vin": self.sensor_name + "_VOL_I", - "vout1": self.sensor_name + "_VOL_O" - }.get(input_name, input_name) - - elif 'tmp75' in sensor_name: - input_name = { - "tmp75-i2c-7-4d": "FTB_INLET_RIGHT", - "tmp75-i2c-7-4c": "FTB_INLET_LEFT", - "tmp75-i2c-7-4b": "FTB_SWITCH_OUTLET", - "tmp75-i2c-7-4a": "BTF_SWITCH_OUTLET", - "tmp75-i2c-39-48": "BTF_INLET_RIGHT", - "tmp75-i2c-39-49": "BTF_INLET_LEFT" - }.get(sensor_name, input_name) - if self.get_sys_airflow() == "FTOB" and sensor_name == "tmp75-i2c-7-4d": - input_name = "INLET_TEMP" - - if self.get_sys_airflow() == "BTOF" and sensor_name == "tmp75-i2c-39-48": - input_name = "INLET_TEMP" - - self.sensor_name = "TEMPERATURE" - - elif 'fancpld' in sensor_name: - raw_fan_input = input_name.split() - input_name = raw_fan_input[0] + \ - raw_fan_input[1] + "_" + raw_fan_input[2] - - elif 'ir35' in sensor_name or 'ir38' in sensor_name: - sensor_name_raw = sensor_name.split("-") - sensor_name = sensor_name_raw[0] - self.sensor_name = sensor_name.upper() - - return input_name.replace(" ", "_").upper() + def input_name_selector(self, raw_sensor_name): + + sensor_name_list = raw_sensor_name.split('_') + sensor_name = sensor_name_list[0] + input_name = '_'.join(sensor_name_list[1:]) + + if sensor_name_list[0] in ["TOP", "BOTTOM"]: + sensor_name = '_'.join(sensor_name_list[0:2]) + input_name = '_'.join(sensor_name_list[2:]) + + return str(sensor_name).upper(), str(input_name).upper() def get_num_sensors(self): """ @@ -94,18 +51,9 @@ def get_num_sensors(self): :return: int num_sensors """ - num_sensors = 0 - try: - # Request and validate sensor's information - self.sensor_info_list = self.request_data() - - # Get number of sensors. - num_sensors = len(self.sensor_info_list) - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() - return num_sensors + return len(all_sensor_dict) def get_sensor_input_num(self, index): """ @@ -113,19 +61,12 @@ def get_sensor_input_num(self, index): :return: int input_num """ - input_num = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - - # Get sensor's input number. - sensor_data = self.sensor_info_list[index-1] - input_num = len(sensor_data.keys())-2 - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() + ss_keys = all_sensor_dict.keys()[index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys() - return input_num + return len(ss_if_keys) def get_sensor_name(self, index): """ @@ -133,18 +74,8 @@ def get_sensor_name(self, index): for example "coretemp-isa-0000" :return: str sensor_name """ - - sensor_name = "N/A" - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - - # Get sensor's name. - sensor_data = self.sensor_info_list[index-1] - sensor_name = sensor_data.get('name') - - except: - return "N/A" + all_sensor_dict = self.get_all() + sensor_name = all_sensor_dict.keys()[index] return sensor_name @@ -155,23 +86,13 @@ def get_sensor_input_name(self, sensor_index, input_index): :return: str sensor_input_name """ - sensor_input_name = "N/A" - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] + all_sensor_dict = self.get_all() - # Get sensor's input name. - sensor_data_key = sensor_data.keys() - sensor_input_name = sensor_data_key[input_index-1] - except: - return "N/A" + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] - return sensor_input_name + return ss_if_keys def get_sensor_input_type(self, sensor_index, input_index): """ @@ -180,24 +101,15 @@ def get_sensor_input_type(self, sensor_index, input_index): :return: str sensor_input_type """ - sensor_input_type = "N/A" - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input type name. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - sensor_input_type = self.input_type_selector(sensor_data_str[1]) - except: - return "N/A" + all_sensor_dict = self.get_all() + + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + + sensor_input_type = sensor_input_info.get('Type', "N/A") return sensor_input_type def get_sensor_input_value(self, sensor_index, input_index): @@ -206,26 +118,15 @@ def get_sensor_input_value(self, sensor_index, input_index): :return: float sensor_input_value """ - sensor_input_value = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input value. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - sensor_input_value = float( - sensor_data_str[0]) if sensor_data_str[0] != "N/A" else 0 - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() + + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + sensor_input_value = sensor_input_info.get('Value', 0.0) return sensor_input_value def get_sensor_input_low_threshold(self, sensor_index, input_index): @@ -235,33 +136,15 @@ def get_sensor_input_low_threshold(self, sensor_index, input_index): :return: float sensor_input_low_threshold """ - sensor_input_low_threshold = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input low threshold. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - indices = [i for i, s in enumerate( - sensor_data_str) if 'min' in s or 'low' in s] - l_thres = float( - sensor_data_str[indices[0] + 2]) if len(indices) != 0 else 0 - unit = sensor_data_str[indices[0] + - 3] if len(indices) != 0 else None - if unit is not None and len(unit) > 1: - sensor_input_low_threshold = l_thres * \ - 1000 if str(unit[0]).lower() == 'k' else l_thres - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() + + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + + sensor_input_low_threshold = sensor_input_info.get('LowThd', 0.0) return sensor_input_low_threshold def get_sensor_input_high_threshold(self, sensor_index, input_index): @@ -270,101 +153,54 @@ def get_sensor_input_high_threshold(self, sensor_index, input_index): the status of this item is not ok if the current value > high_threshold :return: float sensor_input_high_threshold """ + all_sensor_dict = self.get_all() - sensor_input_high_threshold = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input high threshold. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - indices = [i for i, s in enumerate( - sensor_data_str) if 'max' in s or 'high' in s] - h_thres = float( - sensor_data_str[indices[0] + 2]) if len(indices) != 0 else 0 - unit = sensor_data_str[indices[0] + - 3] if len(indices) != 0 else None - if unit is not None and len(unit) > 1: - sensor_input_high_threshold = h_thres * \ - 1000 if str(unit[0]).lower() == 'k' else h_thres - - except: - print "Error: Unable to access sensor information" - return 0 + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + + sensor_input_high_threshold = sensor_input_info.get('HighThd', 0.0) return sensor_input_high_threshold - def get_sys_airflow(self): - sys_air_flow = "Unknown" - sys_pn_data = [ - v.split(":") for v in self.sys_fruid_list if "Product Part Number" in v] + def get_all(self): + """ + Get all information of system sensors, returns JSON objects in python 'DICT'. + SensorName1, SensorName2, ... optional, string + SensorInput1, SensorInput2, ... optional, string + Type, mandatory in SensorInput$INDEX, should be on of { "temperature", "voltage", "power", "amp", "RPM" } + Value, mandatory in SensorInput$INDEX, float , real value + LowThd, mandatory in SensorInput$INDEX, float , lower bound of value + HighThd, mandatory in SensorInput$INDEX, float , upper bound of value + Return python 'dict' objects, example: + """ + + if not self.all_sensor_dict: + all_sensor_dict = dict() - if len(sys_pn_data) == 0: - return sys_air_flow + sensor_info_req = self.request_data(self.sensor_info_url) + sensor_info_data = sensor_info_req.get('data', {}) - sys_pn = sys_pn_data[0][1] - if "R1241-F0001" in sys_pn: - sys_air_flow = "FTOB" - elif"R1241-F0002" in sys_pn: - sys_air_flow = "BTOF" + for raw_ss_name, sensor_info in sensor_info_data.items(): - return sys_air_flow + sensor_name, input_name = self.input_name_selector(raw_ss_name) + sensor_dict = all_sensor_dict.get(sensor_name, {}) + new_sensor_dict = dict() + new_sensor_dict["Type"] = self.input_type_selector( + sensor_info.get('Unit', 'N/A')) + new_sensor_dict["Value"] = float(sensor_info.get('Value', 0.0)) + new_sensor_dict["HighThd"] = float(sensor_info.get('Max', 0.0)) + new_sensor_dict["LowThd"] = float(sensor_info.get('Min', 0.0)) - def get_all(self): + if sensor_dict == {}: + all_sensor_dict[sensor_name] = dict() + + input_name = input_name if input_name != '' else new_sensor_dict["Type"].upper( + ) + sensor_dict[input_name] = new_sensor_dict + all_sensor_dict[sensor_name].update(sensor_dict) + + self.all_sensor_dict = all_sensor_dict - all_sensor_dict = dict() - - # Request sensor's information. - self.sensor_info_list = self.request_data() - for sensor_data in self.sensor_info_list: - sensor_info = sensor_data.copy() - - # Remove none unuse key. - del sensor_info["name"] - del sensor_info["Adapter"] - - # Set sensor data. - sensor_dict = dict() - - for k, v in sensor_info.items(): - sensor_i_dict = dict() - sensor_data_str = v.split() - indices_h = [i for i, s in enumerate( - sensor_data_str) if 'max' in s or 'high' in s] - indices_l = [i for i, s in enumerate( - sensor_data_str) if 'min' in s or 'low' in s] - h_thres = float( - sensor_data_str[indices_h[0] + 2]) if len(indices_h) != 0 else 0 - l_thres = float( - sensor_data_str[indices_l[0] + 2]) if len(indices_l) != 0 else 0 - thres_unit = sensor_data_str[-1] - - sensor_i_dict["Type"] = self.input_type_selector( - sensor_data_str[1]) - sensor_i_dict["Value"] = float( - sensor_data_str[0]) if sensor_data_str[0] != "N/A" else 0 - sensor_i_dict["HighThd"] = h_thres * \ - 1000 if str(thres_unit[0]).lower() == 'k' else h_thres - sensor_i_dict["LowThd"] = l_thres * \ - 1000 if str(thres_unit[0]).lower() == 'k' else l_thres - - k = self.input_name_selector(sensor_data.get('name'), k) - sensor_dict[k] = sensor_i_dict - - if all_sensor_dict.get(self.sensor_name) is None: - all_sensor_dict[self.sensor_name] = dict() - - all_sensor_dict[self.sensor_name].update(sensor_dict) - - sensor_dict = dict() - sensor_dict["Sys_AirFlow"] = self.get_sys_airflow() - all_sensor_dict["TEMPERATURE"].update(sensor_dict) - - return all_sensor_dict + return self.all_sensor_dict diff --git a/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/fanutil.py b/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/fanutil.py index 7c37088b927e..f25dbdd587eb 100644 --- a/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/fanutil.py +++ b/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/fanutil.py @@ -2,51 +2,40 @@ __author__ = 'Wirut G.' __license__ = "GPL" -__version__ = "0.1.2" +__version__ = "0.2.0" __status__ = "Development" import requests import re +NUM_FAN_TRAY = 5 +NUM_ROTER = 2 + class FanUtil(): """Platform-specific FanUtil class""" def __init__(self): + self.fan_info_url = "http://240.1.1.1:8080/api/fan/info" + self.all_fan_dict = None - self.fan_fru_url = "http://240.1.1.1:8080/api/sys/fruid/fan" - self.sensor_url = "http://240.1.1.1:8080/api/sys/sensors" - self.fru_data_list = None - self.sensor_data_list = None - - def request_data(self): - # Reqest data from BMC if not exist. - if self.fru_data_list is None or self.sensor_data_list is None: - fru_data_req = requests.get(self.fan_fru_url) - sensor_data_req = requests.get(self.sensor_url) - fru_json = fru_data_req.json() - sensor_json = sensor_data_req.json() - self.fru_data_list = fru_json.get('Information') - self.sensor_data_list = sensor_json.get('Information') - return self.fru_data_list, self.sensor_data_list - - def name_to_index(self, fan_name): - # Get fan index from fan name - match = re.match(r"(FAN)([0-9]+)-(1|2)", fan_name, re.I) - fan_index = None - if match: - i_list = list(match.groups()) - fan_index = int(i_list[1])*2 - (int(i_list[2]) % 2) - return fan_index + def request_data(self, url): + try: + r = requests.get(url) + data = r.json() + except Exception as e: + return {} + return data def get_num_fans(self): """ Get the number of fans :return: int num_fans """ - num_fans = 10 + all_fan_dict = self.get_all() + num_fan_tray = all_fan_dict.get('Number', NUM_FAN_TRAY) - return num_fans + return num_fan_tray * NUM_ROTER def get_fan_speed(self, fan_name): """ @@ -54,31 +43,10 @@ def get_fan_speed(self, fan_name): :return: int fan_speed """ - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_speed = 0 - position_key = "Front" if index % 2 != 0 else "Rear" - index = int(round(float(index)/2)) - fan_key = "Fan " + str(index) + " " + position_key + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's speed. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_speed = fan_sp_list[0] - - except: - return 0 - - return fan_speed + return fan_info.get('Speed', 0) def get_fan_low_threshold(self, fan_name): """ @@ -88,31 +56,10 @@ def get_fan_low_threshold(self, fan_name): :return: int fan_low_threshold """ - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_low_threshold = 0 - position_key = "Front" if index % 2 != 0 else "Rear" - index = int(round(float(index)/2)) - fan_key = "Fan " + str(index) + " " + position_key - - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's threshold. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_low_threshold = fan_sp_list[1] - - except: - return "N/A" + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - return fan_low_threshold + return fan_info.get('LowThd', 0) def get_fan_high_threshold(self, fan_name): """ @@ -121,32 +68,10 @@ def get_fan_high_threshold(self, fan_name): the status of the fan is not ok :return: int fan_high_threshold """ + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_high_threshold = 0 - position_key = "Front" if index % 2 != 0 else "Rear" - index = int(round(float(index)/2)) - fan_key = "Fan " + str(index) + " " + position_key - - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's threshold. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_high_threshold = fan_sp_list[2] - - except: - return 0 - - return fan_high_threshold + return fan_info.get('HighThd', 0) def get_fan_pn(self, fan_name): """ @@ -154,58 +79,20 @@ def get_fan_pn(self, fan_name): :return: str fan_pn """ - try: - # Get real fan index - index = self.name_to_index(fan_name) - - # Set key and index. - fan_pn = "N/A" - index = int(round(float(index)/2)) - fan_fru_key = "Fantray" + str(index) + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's fru. - for fan_fru in self.fru_data_list: - matching_fan = [s for s in fan_fru if fan_fru_key in s] - if matching_fan: - pn = [s for s in fan_fru if "Part" in s] - fan_pn = pn[0].split()[4] - - except: - return "N/A" - - return fan_pn + return fan_info.get('PN', 'N/A') def get_fan_sn(self, fan_name): """ Get the serial number of the fan :return: str fan_sn """ - try: - # Get real fan index - index = self.name_to_index(fan_name) + all_fan_dict = self.get_all() + fan_info = all_fan_dict.get(fan_name, {}) - # Set key and index. - fan_sn = "N/A" - index = int(round(float(index)/2)) - fan_fru_key = "Fantray" + str(index) - - # Request and validate fan information. - self.fru_data_list, self.sensor_data_list = self.request_data() - - # Get fan's fru. - for fan_fru in self.fru_data_list: - matching_fan = [s for s in fan_fru if fan_fru_key in s] - if matching_fan: - serial = [s for s in fan_fru if "Serial" in s] - fan_sn = serial[0].split()[3] - - except: - return "N/A" - - return fan_sn + return fan_info.get('SN', 'N/A') def get_fans_name_list(self): """ @@ -240,70 +127,40 @@ def get_all(self): SN, conditional, if PRESENT is True, SN of the FAN, string) """ - self.fru_data_list, self.sensor_data_list = self.request_data() - all_fan_dict = dict() - - # Get the number of fans - n_fan = self.get_num_fans() - all_fan_dict["Number"] = n_fan - - # Set fan FRU data. - fan_fru_dict = dict() - fan_raw_idx = 1 - for fan_fru in self.fru_data_list: - fru_dict = dict() - fan_ps = False - - if len(fan_fru) == 0: - fan_idx = fan_raw_idx - fan_pn = "N/A" - fan_sn = "N/A" - else: - fan_key = fan_fru[0].split() - if str(fan_key[-1]).lower() == "absent": - fan_idx = int(re.findall('\d+', fan_key[0])[0]) - - else: - fan_idx = int(re.findall('\d+', fan_key[-1])[0]) - fan_ps = True - pn = [s for s in fan_fru if "Part" in s] - sn = [s for s in fan_fru if "Serial" in s] - fan_pn = pn[0].split( - ":")[-1].strip() if len(pn) > 0 else 'N/A' - fan_sn = sn[0].split( - ":")[-1].strip() if len(sn) > 0 else 'N/A' - - fru_dict["PN"] = "N/A" if not fan_pn or fan_pn == "" else fan_pn - fru_dict["SN"] = "N/A" if not fan_sn or fan_sn == "" else fan_sn - fru_dict["Present"] = fan_ps - fan_fru_dict[fan_idx] = fru_dict - fan_raw_idx += 1 - - # Set fan sensor data. - for sensor_data in self.sensor_data_list: - sensor_name = sensor_data.get('name') - if "fan" in str(sensor_name): - for x in range(1, n_fan + 1): - fan_dict = dict() - f_index = int(round(float(x)/2)) - pos = 1 if x % 2 else 2 - position_key = "Front" if x % 2 != 0 else "Rear" - fan_key = "Fan " + str(f_index) + " " + position_key - fan_data = sensor_data.get(fan_key) - fan_sp_list = map(int, re.findall(r'\d+', fan_data)) - fan_dict["Present"] = fan_fru_dict[f_index]["Present"] - if fan_dict["Present"] or fan_sp_list[0] > 0: - fan_dict["Present"] = True - fan_dict["Speed"] = fan_sp_list[0] - fan_dict["Running"] = True if fan_dict["Speed"] > 0 else False - fan_dict["LowThd"] = fan_sp_list[1] - fan_dict["HighThd"] = fan_sp_list[2] - fan_dict["PN"] = fan_fru_dict[f_index]["PN"] - fan_dict["SN"] = fan_fru_dict[f_index]["SN"] - fan_dict["AirFlow"] = "FTOB" if "R1240-G0009" in fan_dict["PN"] else "Unknown" - fan_dict["Status"] = True if fan_dict["AirFlow"] != "Unknown" else False - fan_name = 'FAN{}_{}'.format(f_index, pos) - all_fan_dict[fan_name] = fan_dict - break - - return all_fan_dict + if not self.all_fan_dict: + all_fan_dict = dict() + + fan_info_req = self.request_data(self.fan_info_url) + fan_info_data = fan_info_req.get('data', {}) + all_fan_dict["Number"] = fan_info_data.get('Number', NUM_FAN_TRAY) + + for fan_idx in range(1, all_fan_dict["Number"] + 1): + num_of_roter = fan_info_data.get('Rotors', NUM_ROTER) + + for fan_pos in range(1, num_of_roter + 1): + fan_key = 'FAN{}'.format(str(fan_idx)) + roter_key = 'Rotor{}'.format(str(fan_pos)) + + fan_info = fan_info_data.get(fan_key, {}) + roter_info = fan_info.get(roter_key, {}) + + fan_info_dict = dict() + fan_info_dict["Present"] = True if fan_info.get( + "Present") == 'yes' else False + fan_info_dict["Speed"] = roter_info.get("Speed", "N/A") + fan_info_dict["Running"] = True if roter_info.get( + "Running") == 'yes' else False + fan_info_dict["HighThd"] = roter_info.get( + "SpeedMax", "N/A") + fan_info_dict["LowThd"] = roter_info.get("SpeedMin", "N/A") + fan_info_dict["Status"] = False if roter_info.get( + "HwAlarm") == 'yes' else True + fan_info_dict["PN"] = fan_info.get("PN", "N/A") + fan_info_dict["SN"] = fan_info.get("SN", "N/A") + fan_info_dict["AirFlow"] = fan_info.get("AirFlow", "N/A") + + fan_name = '{}_{}'.format(fan_key, fan_pos) + all_fan_dict[fan_name] = fan_info_dict + self.all_fan_dict = all_fan_dict + + return self.all_fan_dict diff --git a/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/fwmgrutil.py b/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/fwmgrutil.py index 619aa7b173f3..e2fffa83fc83 100644 --- a/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/fwmgrutil.py +++ b/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/fwmgrutil.py @@ -1,8 +1,15 @@ +#!/usr/bin/env python +# # fwmgrutil.py # # Platform-specific firmware management interface for SONiC # +__author__ = 'Wirut G.' +__license__ = "GPL" +__version__ = "0.2.0" +__status__ = "Development" + import subprocess import requests import os @@ -27,9 +34,15 @@ class FwMgrUtil(FwMgrUtilBase): def __init__(self): self.platform_name = "AS23128h" self.onie_config_file = "/host/machine.conf" - self.bmc_info_url = "http://240.1.1.1:8080/api/sys/bmc" - self.bmc_raw_command_url = "http://240.1.1.1:8080/api/sys/raw" - self.fw_upgrade_url = "http://240.1.1.1:8080/api/sys/upgrade" + self.bmc_info_url = "http://240.1.1.1:8080/api/bmc/info" + self.cpld_info_url = "http://240.1.1.1:8080/api/firmware/cpldversion" + self.bmc_raw_command_url = "http://240.1.1.1:8080/api/hw/rawcmd" + self.fw_upgrade_url = "http://240.1.1.1:8080/api/firmware/upgrade" + self.fw_refresh_url = "http://240.1.1.1:8080/api/firmware/refresh" + self.bios_next_boot = "http://240.1.1.1:8080/api/firmware/biosnextboot" + self.bmc_next_boot = "http://240.1.1.1:8080/api/bmc/nextboot" + self.bmc_reboot_url = "http://240.1.1.1:8080/api/bmc/reboot" + self.bios_boot_info = "http://240.1.1.1:8080/api/misc/biosbootstatus" self.onie_config_file = "/host/machine.conf" self.fw_upgrade_logger_path = "/var/log/fw_upgrade.log" self.cpldb_version_path = "/sys/devices/platform/%s.cpldb/getreg" % self.platform_name @@ -39,6 +52,9 @@ def __init__(self): self.switchboard_cpld3_path = "/sys/devices/platform/%s.switchboard/CPLD3/getreg" % self.platform_name self.switchboard_cpld4_path = "/sys/devices/platform/%s.switchboard/CPLD4/getreg" % self.platform_name self.bmc_pwd_path = "/usr/local/etc/bmcpwd" + self.cpld_name_list = ["CPU_CPLD", "COMBO_CPLD", + "SW_CPLD1", "SW_CPLD2", "TOP_LC_CPLD", "BOT_LC_CPLD"] + self.api_time_out = 300 def __get_register_value(self, path, register): cmd = "echo {1} > {0}; cat {0}".format(path, register) @@ -89,12 +105,12 @@ def get_bmc_version(self): """ bmc_version = None - - bmc_version_key = "OpenBMC Version" - bmc_info_req = requests.get(self.bmc_info_url, timeout=60) + bmc_version_key = "Version" + bmc_info_req = requests.get( + self.bmc_info_url, timeout=self.api_time_out) if bmc_info_req.status_code == 200: bmc_info_json = bmc_info_req.json() - bmc_info = bmc_info_json.get('Information') + bmc_info = bmc_info_json.get('data') bmc_version = bmc_info.get(bmc_version_key) return str(bmc_version) @@ -102,7 +118,8 @@ def upload_file_bmc(self, fw_path): scp_command = 'sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r %s root@240.1.1.1:/home/root/' % os.path.abspath( fw_path) child = pexpect.spawn(scp_command) - i = child.expect(["root@240.1.1.1's password:"], timeout=30) + child.timeout = self.api_time_out + i = child.expect(["root@240.1.1.1's password:"]) bmc_pwd = self.get_bmc_pass() if i == 0 and bmc_pwd: child.sendline(bmc_pwd) @@ -124,13 +141,13 @@ def get_cpld_version(self): CPLD_3 = self.__get_register_value(self.switchboard_cpld3_path, '0x00') CPLD_4 = self.__get_register_value(self.switchboard_cpld4_path, '0x00') - fan_cpld_key = "FanCPLD Version" + fan_cpld_key = "CPLD_FAN" fan_cpld = None - bmc_info_req = requests.get(self.bmc_info_url) - if bmc_info_req.status_code == 200: - bmc_info_json = bmc_info_req.json() - bmc_info = bmc_info_json.get('Information') - fan_cpld = bmc_info.get(fan_cpld_key) + fan_cpld_req = requests.get(self.cpld_info_url) + if fan_cpld_req.status_code == 200: + fancpld_info_json = fan_cpld_req.json() + fancpld_info_data = fancpld_info_json.get('data') + fan_cpld = fancpld_info_data.get(fan_cpld_key) CPLD_B = 'None' if CPLD_B is 'None' else "{}.{}".format( int(CPLD_B[2], 16), int(CPLD_B[3], 16)) @@ -144,8 +161,7 @@ def get_cpld_version(self): int(CPLD_3[2], 16), int(CPLD_3[3], 16)) CPLD_4 = 'None' if CPLD_4 is 'None' else "{}.{}".format( int(CPLD_4[2], 16), int(CPLD_4[3], 16)) - FAN_CPLD = 'None' if fan_cpld is None else "{}.{}".format( - int(fan_cpld[0], 16), int(fan_cpld[1], 16)) + FAN_CPLD = 'None' if fan_cpld is None else fan_cpld cpld_version_dict = {} cpld_version_dict.update({'CPLD_B': CPLD_B}) @@ -174,7 +190,7 @@ def get_bios_version(self): bios_version = raw_data_list[0] if len( raw_data_list) == 1 else raw_data_list[-2] - return str(bios_version) + return str(bios_version).strip() def get_onie_version(self): """Get ONiE version from SONiC @@ -224,7 +240,7 @@ def get_fpga_version(self): version = self.__get_register_value(self.fpga_version_path, '0x00') if version is not 'None': version = "{}.{}".format( - int(version[2:][:4], 16), int(version[2:][4:], 16)) + int(version[2:][1:4], 16), int(version[2:][4:], 16)) return str(version) def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): @@ -245,10 +261,13 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): return False if fw_type == 'bmc': + fw_extra_str = str(fw_extra).lower() + if fw_extra_str not in ["master", "slave", "both", "pingpong"]: + return False + self.__update_fw_upgrade_logger( "bmc_upgrade", "start BMC upgrade") # Copy BMC image file to BMC - fw_extra_str = str(fw_extra).lower() last_fw_upgrade = ["BMC", fw_path, fw_extra_str, "FAILED"] upload_file = self.upload_file_bmc(fw_path) if not upload_file: @@ -260,8 +279,8 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): filename_w_ext = os.path.basename(fw_path) json_data = dict() - json_data["path"] = "root@127.0.0.1:/home/root/%s" % filename_w_ext - json_data["password"] = bmc_pwd + json_data["Name"] = "bmc" + json_data["Path"] = "/home/root/%s" % filename_w_ext # Set flash type current_bmc = self.get_running_bmc() @@ -270,26 +289,26 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): if fw_extra_str == "pingpong": #flash = "master" if current_bmc == "slave" else "slave" flash = "slave" - json_data["flash"] = flash + json_data["Flash"] = flash # Install BMC if flash == "both": self.__update_fw_upgrade_logger( "bmc_upgrade", "install BMC as master mode") - json_data["flash"] = "master" - r = requests.post(self.bmc_info_url, json=json_data) - if r.status_code != 200 or 'success' not in r.json().get('result'): + json_data["Flash"] = "master" + r = requests.post(self.fw_upgrade_url, json=json_data) + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "bmc_upgrade", "fail, message=BMC API report error code %d" % r.status_code) + "bmc_upgrade", "fail, message={}".format(r.json().get('messages'))) self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False - json_data["flash"] = "slave" + json_data["Flash"] = "slave" self.__update_fw_upgrade_logger( - "bmc_upgrade", "install BMC as %s mode" % json_data["flash"]) - r = requests.post(self.bmc_info_url, json=json_data) - if r.status_code == 200 and 'success' in r.json().get('result'): + "bmc_upgrade", "install BMC as %s mode" % json_data["Flash"]) + r = requests.post(self.fw_upgrade_url, json=json_data) + if r.status_code == 200 and r.json().get('status') == 'OK': if fw_extra_str == "pingpong": flash = "master" if current_bmc == "slave" else "slave" self.__update_fw_upgrade_logger( @@ -308,7 +327,7 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): last_fw_upgrade[3] = "DONE" else: self.__update_fw_upgrade_logger( - "bmc_upgrade", "fail, message=unable to install BMC image") + "bmc_upgrade", "fail, message={}".format(r.json().get('messages'))) self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False @@ -419,18 +438,15 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): filename_w_ext = os.path.basename(fw_path) json_data = dict() - json_data["image_path"] = "root@127.0.0.1:/home/root/%s" % filename_w_ext - json_data["password"] = bmc_pwd - json_data["device"] = "cpld" - json_data["reboot"] = "no" - json_data["type"] = fw_extra_str + json_data["Path"] = "/home/root/%s" % filename_w_ext + json_data["Name"] = "cpld" # Call BMC api to install cpld image print("Installing...") r = requests.post(self.fw_upgrade_url, json=json_data) - if r.status_code != 200 or 'success' not in r.json().get('result'): + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "cpld_upgrade", "fail, message=invalid cpld image") + "cpld_upgrade", "fail, message={}".format(r.json().get('messages'))) continue cpld_result_list[i] = "DONE" @@ -455,57 +471,50 @@ def firmware_upgrade(self, fw_type, fw_path, fw_extra=None): return True elif 'bios' in fw_type: + fw_extra_str = str(fw_extra).lower() + if fw_extra_str not in ["master", "slave", "both"]: + return False + self.__update_fw_upgrade_logger( "bios_upgrade", "start BIOS upgrade") last_fw_upgrade = ["BIOS", fw_path, None, "FAILED"] fw_extra_str = str(fw_extra).lower() - flash = fw_extra_str if fw_extra_str in [ - "master", "slave"] else "master" + flash = fw_extra_str.lower() if not os.path.exists(fw_path): self.__update_fw_upgrade_logger( "bios_upgrade", "fail, message=image not found") return False - scp_command = 'sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r %s root@240.1.1.1:/home/root/' % os.path.abspath( - fw_path) - child = pexpect.spawn(scp_command) - i = child.expect(["root@240.1.1.1's password:"], timeout=30) - if i != 0: + upload_file = self.upload_file_bmc(fw_path) + if not upload_file: self.__update_fw_upgrade_logger( "bios_upgrade", "fail, message=unable to upload image to BMC") self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False - child.sendline(bmc_pwd) - data = child.read() - print(data) - child.close - - json_data = dict() - json_data["data"] = "/usr/bin/ipmitool -b 1 -t 0x2c raw 0x2e 0xdf 0x57 0x01 0x00 0x01" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "bios_upgrade", "fail, message=unable to set state") - self.__update_fw_upgrade_logger( - "last_upgrade_result", str(last_fw_upgrade)) - return False + # json_data = dict() + # json_data["Command"] = "/usr/bin/ipmitool -b 1 -t 0x2c raw 0x2e 0xdf 0x57 0x01 0x00 0x01" + # r = requests.post(self.bmc_raw_command_url, json=json_data) + # if r.status_code != 200: + # self.__update_fw_upgrade_logger( + # "bios_upgrade", "fail, message=unable to set state") + # self.__update_fw_upgrade_logger( + # "last_upgrade_result", str(last_fw_upgrade)) + # return False filename_w_ext = os.path.basename(fw_path) json_data = dict() - json_data["image_path"] = "root@127.0.0.1:/home/root/%s" % filename_w_ext - json_data["password"] = bmc_pwd - json_data["device"] = "bios" - json_data["flash"] = flash - json_data["reboot"] = "no" + json_data["Path"] = "/home/root/%s" % filename_w_ext + json_data["Name"] = "bios" + json_data["Flash"] = flash print("Installing...") r = requests.post(self.fw_upgrade_url, json=json_data) - if r.status_code != 200 or 'success' not in r.json().get('result'): + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "bios_upgrade", "fail, message=unable install bios") + "bios_upgrade", "fail, message={}".format(r.json().get('messages'))) self.__update_fw_upgrade_logger( "last_upgrade_result", str(last_fw_upgrade)) return False @@ -742,77 +751,40 @@ def firmware_refresh(self, fpga_list, cpld_list, fw_extra=None): self.firmware_refresh(None, ["FAN_CPLD", "LC1_CPLD", "BASE_CPLD"], "/tmp/fw/fan_refresh.vme:none:/tmp/fw/base_refresh.vme") """ + self.__update_fw_upgrade_logger( + "fw_refresh", "start firmware refresh") - if not fpga_list and not cpld_list: - return False + cpld_list = [x.lower() for x in cpld_list] if cpld_list else [] + fpga_list = [x.lower() for x in fpga_list] if fpga_list else [] + refresh_list = cpld_list + fpga_list + fw_path_list = str(fw_extra).split(':') + refresh_img_list = ["none" for i in range(len(refresh_list))] - if type(cpld_list) is list and ("BASE_CPLD" in cpld_list or "FAN_CPLD" in cpld_list): - refresh_list = fpga_list + \ - cpld_list if type(fpga_list) is list else cpld_list + if len(refresh_list) == 0 : self.__update_fw_upgrade_logger( - "fw_refresh", "start %s refresh" % ",".join(refresh_list)) - fw_path_list = fw_extra.split(':') - command = "echo " - if len(fw_path_list) != len(cpld_list): - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=Invalid fw_extra") - return False - - for idx in range(0, len(cpld_list)): - fw_path = fw_path_list[idx] - refresh_type = { - "BASE_CPLD": "base", - "FAN_CPLD": "fan" - }.get(cpld_list[idx], None) + "fw_refresh", "fail, message=Invalid input") + return False - if not refresh_type: - continue - elif not self.upload_file_bmc(fw_path): + for idx in range(0, len(refresh_list)): + if refresh_list[idx] in ["fan_cpld", "base_cpld"]: + if not self.upload_file_bmc(fw_path_list[idx]): self.__update_fw_upgrade_logger( "cpld_refresh", "fail, message=Unable to upload refresh image to BMC") return False - else: - filename_w_ext = os.path.basename(fw_path) - - sub_command = "%s /home/root/%s > /tmp/cpld_refresh " % ( - refresh_type, filename_w_ext) - command += sub_command + refresh_img_list[idx] = "/home/root/%s" % os.path.basename( + fw_path_list[idx]) - json_data = dict() - json_data["data"] = command - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=%d Invalid refresh image" % r.status_code) - return False - elif type(cpld_list) is list: - refresh_list = fpga_list + \ - cpld_list if type(fpga_list) is list else cpld_list - self.__update_fw_upgrade_logger( - "fw_refresh", "start %s refresh" % ",".join(refresh_list)) - json_data = dict() - json_data["data"] = "echo cpu_cpld > /tmp/cpld_refresh" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=%d Unable to load new CPLD" % r.status_code) - return False - elif type(fpga_list) is list: - self.__update_fw_upgrade_logger( - "fw_refresh", "start FPGA refresh") - json_data = dict() - json_data["data"] = "echo fpga > /tmp/cpld_refresh" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: - self.__update_fw_upgrade_logger( - "cpld_refresh", "fail, message=%d Unable to load new FPGA" % r.status_code) - return False - else: + json_data = dict() + json_data["Paths"] = refresh_img_list + json_data["Names"] = refresh_list + r = requests.post(self.fw_refresh_url, json=json_data) + if r.status_code != 200 or r.json().get('status') != 'OK': self.__update_fw_upgrade_logger( - "fw_refresh", "fail, message=Invalid input") + "cpld_refreshfw", "fail, message={}".format(r.json().get('messages'))) return False self.__update_fw_upgrade_logger("fw_refresh", "done") + return True def get_running_bmc(self): @@ -820,21 +792,15 @@ def get_running_bmc(self): Get booting flash of running BMC. @return a string, "master" or "slave" """ - json_data = dict() - json_data["data"] = "/usr/local/bin/boot_info.sh" - r = requests.post(self.bmc_raw_command_url, json=json_data) - try: - boot_info_list = r.json().get('result') - for boot_info_raw in boot_info_list: - boot_info = boot_info_raw.split(":") - if "Current Boot Code Source" in boot_info[0]: - flash = "master" if "master "in boot_info[1].lower( - ) else "slave" - return flash - raise Exception( - "Error: Unable to detect booting flash of running BMC") - except Exception as e: - raise Exception(e) + running_bmc = "master" + running_bmc_key = "Flash" + bmc_info_req = requests.get( + self.bmc_info_url, timeout=self.api_time_out) + if bmc_info_req.status_code == 200: + bmc_info_json = bmc_info_req.json() + bmc_info = bmc_info_json.get('data') + running_bmc = bmc_info.get(running_bmc_key) + return str(running_bmc).lower() def set_bmc_boot_flash(self, flash): """ @@ -844,8 +810,8 @@ def set_bmc_boot_flash(self, flash): if flash.lower() not in ["master", "slave"]: return False json_data = dict() - json_data["data"] = "source /usr/local/bin/openbmc-utils.sh;bmc_reboot %s" % flash - r = requests.post(self.bmc_raw_command_url, json=json_data) + json_data["Flash"] = flash + r = requests.post(self.bmc_next_boot, json=json_data) if r.status_code != 200: return False return True @@ -854,10 +820,13 @@ def reboot_bmc(self): """ Reboot BMC """ - json_data = dict() - json_data["data"] = "source /usr/local/bin/openbmc-utils.sh;bmc_reboot reboot" - r = requests.post(self.bmc_raw_command_url, json=json_data) - if r.status_code != 200: + try: + r = requests.post(self.bmc_reboot_url) + if r.status_code != 200: + return False + except Exception as e: + if "Connection aborted." in e.message[0]: + return True return False return True @@ -866,17 +835,39 @@ def get_current_bios(self): # Get booting bios image of current running host OS # @return a string, "master" or "slave" """ + bios_boot_info = requests.get( + self.bios_boot_info, timeout=self.api_time_out) + if bios_boot_info.status_code == 200: + bios_boot_info_json = bios_boot_info.json() + bios_boot_info_data = bios_boot_info_json.get('data') + bios_boot = bios_boot_info_data.get("Flash") + return str(bios_boot) + + def get_bios_next_boot(self): + """ + # Get booting bios image of next booting host OS + # @return a string, "master" or "slave" + """ + bios_next_boot = "master" + bios_next_boot_info = requests.get( + self.bios_next_boot, timeout=self.api_time_out) + if bios_next_boot_info.status_code == 200: + bios_next_boot_info_json = bios_next_boot_info.json() + bios_next_boot_info_data = bios_next_boot_info_json.get('data') + bios_next_boot = bios_next_boot_info_data.get("Flash") + return str(bios_next_boot) + + def set_bios_next_boot(self, flash): + """ + # Set booting bios image of next booting host OS + # @return a string, "master" or "slave" + """ + if str(flash).lower() not in ['master', 'slave']: + return False + json_data = dict() - json_data["data"] = "source /usr/local/bin/openbmc-utils.sh;come_boot_info" - r = requests.post(self.bmc_raw_command_url, json=json_data) - try: - cpu_boot_info_list = r.json().get('result') - for cpu_boot_info_raw in cpu_boot_info_list: - if "COMe CPU boots from BIOS" in cpu_boot_info_raw: - bios_image = "master" if "master "in cpu_boot_info_raw.lower( - ) else "slave" - return bios_image - raise Exception( - "Error: Unable to detect current running bios image") - except Exception as e: - raise Exception(e) + json_data["Flash"] = str(flash).lower() + r = requests.post(self.bios_next_boot, json=json_data) + if r.status_code != 200: + return False + return True diff --git a/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/psuutil.py b/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/psuutil.py index 55c3a41e1f68..8a0d9334586f 100644 --- a/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/psuutil.py +++ b/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/psuutil.py @@ -2,7 +2,7 @@ __author__ = 'Wirut G.' __license__ = "GPL" -__version__ = "0.1.4" +__version__ = "0.2.0" __status__ = "Development" import requests @@ -13,43 +13,25 @@ except ImportError as e: raise ImportError(str(e) + "- required module not found") +NUM_PSU = 4 + class PsuUtil(PsuBase): """Platform-specific PSUutil class""" def __init__(self): PsuBase.__init__(self) - self.fru_status_url = "http://240.1.1.1:8080/api/sys/fruid/status" - self.psu_info_url = "http://240.1.1.1:8080/api/sys/fruid/psu" - - self.fru_status_list = None - self.psu_info_list = None - - def request_data(self): - # Reqest data from BMC if not exist. - if self.fru_status_list is None or self.psu_info_list is None: - fru_status_req = requests.get(self.fru_status_url) - psu_info_req = requests.get(self.psu_info_url) - fru_status_json = fru_status_req.json() - psu_info_json = psu_info_req.json() - self.fru_status_list = fru_status_json.get('Information') - self.psu_info_list = psu_info_json.get('Information') - return self.fru_status_list, self.psu_info_list - - def airflow_selector(self, pn): - # Set airflow type - pn = pn.upper() - if "DPS-1100FB" in pn: - airflow = "FTOB" - elif "DPS-1100AB" in pn: - airflow = "BTOF" - elif "FSJ026-A20G" in pn: - airflow = "FTOB" - elif "FSJ038-A20G" in pn: - airflow = "BTOF" - else: - airflow = "Unknown" - return airflow + + self.psu_info_url = "http://240.1.1.1:8080/api/psu/info" + self.all_psu_dict = None + + def request_data(self, url): + try: + r = requests.get(url) + data = r.json() + except Exception as e: + return {} + return data def get_num_psus(self): """ @@ -57,9 +39,9 @@ def get_num_psus(self): :return: An integer, the number of PSUs available on the device """ - num_psus = 4 + all_psu_dict = self.get_all() - return num_psus + return all_psu_dict.get('Number', NUM_PSU) def get_psu_status(self, index): """ @@ -69,28 +51,11 @@ def get_psu_status(self, index): :return: Boolean, True if PSU is operating properly, False if PSU is faulty """ - # init data - psu_key = "PSU" + str(index) - psu_status_key = "Power Status" - psu_power_status = False - - try: - # Request and validate sensor's information - self.fru_status_list, self.psu_info_list = self.request_data() + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) - # Get PSU power status. - for fru_status in self.fru_status_list: - is_psu = fru_status.get(psu_key) - psu_status = str(fru_status.get(psu_status_key)).strip() - - if is_psu is not None and psu_status == "OK": - psu_power_status = True - - except: - print("Error: Unable to access PSU power status") - return False - - return psu_power_status + return psu_info.get('PowerStatus', False) def get_psu_presence(self, index): """ @@ -99,29 +64,11 @@ def get_psu_presence(self, index): :param index: An integer, 1-based index of the PSU of which to query status :return: Boolean, True if PSU is plugged, False if not """ + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) - # Init data - psu_key = "PSU" + str(index) - psu_presence_key = "Present" - psu_presence_status = False - - try: - # Request and validate sensor's information. - self.fru_status_list, self.psu_info_list = self.request_data() - - # Get PSU present status. - for fru_status in self.fru_status_list: - is_psu = fru_status.get(psu_key) - psu_status = str(fru_status.get(psu_presence_key)).strip() - - if is_psu is not None and psu_status == "Present": - psu_presence_status = True - - except: - print("Error: Unable to access PSU presence status") - return False - - return psu_presence_status + return psu_info.get('Present', False) def get_psu_sn(self, index): """ @@ -130,25 +77,12 @@ def get_psu_sn(self, index): :param index: An integer, 1-based index of the PSU. :return: Serial number """ - serial_number = "N/A" - psu_key = "PSU" + str(index) + " FRU" - psu_sn_key = "Serial Number" - - try: - # Request and validate sensor's information. - self.fru_status_list, self.psu_info_list = self.request_data() - # Get PSU fru info. - for psu_fru in self.psu_info_list: - psu_sn = str(psu_fru.get(psu_sn_key)).strip() - if psu_fru.get(psu_key) is not None: - serial_number = psu_sn if psu_sn.strip() != "" else "N/A" - break + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) - except: - return "N/A" - - return serial_number + return psu_info.get('SN', False) def get_psu_pn(self, index): """ @@ -157,25 +91,12 @@ def get_psu_pn(self, index): :param index: An integer, 1-based index of the PSU. :return: Product name """ - product_name = "N/A" - psu_key = "PSU" + str(index) + " FRU" - psu_pn_key = "Product Name" - - try: - # Request and validate sensor's information - self.fru_status_list, self.psu_info_list = self.request_data() - - # Get PSU fru info. - for psu_fru in self.psu_info_list: - psu_pn = str(psu_fru.get(psu_pn_key)).strip() - if psu_fru.get(psu_key) is not None: - product_name = psu_pn if psu_pn.strip() != "" else "N/A" - break - - except: - return "N/A" - return product_name + all_psu_dict = self.get_all() + psu_key = 'PSU{}'.format(index) + psu_info = all_psu_dict.get(psu_key, {}) + + return psu_info.get('PN', False) def get_all(self): """ @@ -187,62 +108,34 @@ def get_all(self): SN, conditional, if PRESENT is True, SN of the PSU, string """ - # Init data - all_psu_dict = dict() - all_psu_dict["Number"] = self.get_num_psus() - psu_sn_key_1 = "Serial Number" - psu_sn_key_2 = "Product Serial" - psu_pn_key = "Product Name" - - # Request and validate sensor's information. - self.fru_status_list, self.psu_info_list = self.request_data() - - # Set PSU FRU data. - psu_info_dict = dict() - for psu_fru in self.psu_info_list: - psu_data = dict() - pn = psu_fru.get(psu_pn_key) - sn = psu_fru.get(psu_sn_key_1) or psu_fru.get(psu_sn_key_2) - psu_data["PN"] = "N/A" if not pn or str( - pn).strip() == "" else str(pn).strip() - psu_data["SN"] = "N/A" if not pn or str( - pn).strip() == "" else str(sn).strip() - - fru_check = [psu_fru[v] for v in psu_fru.keys() if 'FRU Info' in v] - non_fru_check = [v for v in psu_fru.keys() if 'PSU' in v] - - if len(non_fru_check) > 0: - psu_idx = int(re.findall('\d+', non_fru_check[0])[0]) - psu_info_dict[psu_idx] = psu_data - elif len(fru_check) > 0: - psu_idx = int(re.findall('\d+', fru_check[0])[0]) - psu_info_dict[psu_idx] = psu_data - - # Set PSU status. - for fru_status in self.fru_status_list: - psu_status_dict = dict() - find_psu = [v for v in fru_status.keys() if "PSU" in v] - if len(find_psu) > 0: - psu_idx = int(re.findall('\d+', find_psu[0])[0]) - psu_ps_status = True if str(fru_status.get( - "Present")).strip() == "Present" else False - psu_pw_status = True if str(fru_status.get( - "Power Status")).strip() == "OK" else False - psu_pw_type = str(fru_status.get( - "Power Type")).strip() - ac_status = True if str(fru_status.get( - "AC Status")).strip().upper() == "OK" else False - - psu_status_dict["Present"] = psu_ps_status - if psu_ps_status: - psu_status_dict["PowerStatus"] = psu_pw_status - psu_status_dict["PN"] = psu_info_dict[psu_idx]["PN"] - psu_status_dict["SN"] = psu_info_dict[psu_idx]["SN"] - psu_status_dict["InputType"] = psu_pw_type - psu_status_dict["InputStatus"] = True if psu_pw_status and psu_ps_status else False - psu_status_dict["OutputStatus"] = ac_status - psu_status_dict["AirFlow"] = self.airflow_selector( - psu_status_dict["PN"]) - all_psu_dict[find_psu[0]] = psu_status_dict - - return all_psu_dict + if not self.all_psu_dict: + all_psu_dict = dict() + + psu_info_req = self.request_data(self.psu_info_url) + psu_info_data = psu_info_req.get('data', {}) + all_psu_dict["Number"] = psu_info_data.get('Number', NUM_PSU) + + for psu_idx in range(1, all_psu_dict["Number"] + 1): + psu_key = 'PSU{}'.format(str(psu_idx)) + psu_info = psu_info_data.get(psu_key, {}) + psu_input_info = psu_info.get('Inputs', {}) + psu_output_info = psu_info.get('Outputs', {}) + + psu_info_dict = dict() + psu_info_dict["InputType"] = psu_input_info.get("Type", "N/A") + psu_info_dict["InputStatus"] = True if psu_input_info.get( + "Status") else False + psu_info_dict["OutputStatus"] = True if psu_output_info.get( + "Status") else False + psu_info_dict["PowerStatus"] = ( + psu_info_dict["InputStatus"] and psu_info_dict["OutputStatus"]) + psu_info_dict["PN"] = psu_info.get("PN", "N/A") + psu_info_dict["SN"] = psu_info.get("SN", "N/A") + psu_info_dict["Present"] = True if psu_info.get("Present") == 'yes' else False + psu_info_dict["AirFlow"] = psu_info.get("AirFlow", "N/A") + + all_psu_dict[psu_key] = psu_info_dict + + self.all_psu_dict = all_psu_dict + + return self.all_psu_dict diff --git a/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/sensorutil.py b/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/sensorutil.py index 29f56e8578e6..efed1e850306 100644 --- a/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/sensorutil.py +++ b/device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/sensorutil.py @@ -12,20 +12,16 @@ class SensorUtil(): """Platform-specific SensorUtil class""" def __init__(self): - self.sensor_url = "http://240.1.1.1:8080/api/sys/sensors" - self.sys_fruid_url = "http://240.1.1.1:8080/api/sys/fruid/sys" - self.sensor_info_list = None - - def request_data(self): - # Reqest data from BMC if not exist. - if self.sensor_info_list is None: - sensor_data_req = requests.get(self.sensor_url) - sensor_json = sensor_data_req.json() - self.sensor_info_list = sensor_json.get('Information') - sys_fruid_req = requests.get(self.sys_fruid_url) - sys_fruid_json = sys_fruid_req.json() - self.sys_fruid_list = sys_fruid_json.get('Information') - return self.sensor_info_list + self.sensor_info_url = "http://240.1.1.1:8080/api/sensor/info" + self.all_sensor_dict = None + + def request_data(self, url): + try: + r = requests.get(url) + data = r.json() + except Exception as e: + return {} + return data def input_type_selector(self, unit): # Set input type. @@ -37,70 +33,17 @@ def input_type_selector(self, unit): "W": "power" }.get(unit, unit) - def input_name_selector(self, sensor_name, input_name): - - self.sensor_name = { - "syscpld-i2c-0-0d": "TEMPERATURE", - "dps1100-i2c-27-58": "PSU1", - "dps1100-i2c-26-58": "PSU2", - "dps1100-i2c-25-58": "PSU3", - "dps1100-i2c-24-58": "PSU4", - "fancpld-i2c-8-0d": "FAN", - "isl68137-i2c-17-60": "ISL68137" - }.get(sensor_name, sensor_name) - - if 'dps1100' in sensor_name: - input_name = { - "fan1": self.sensor_name + "_FAN", - "iin": self.sensor_name + "_CURR_I", - "iout1": self.sensor_name + "_CURR_O", - "pin": self.sensor_name + "_POWER_I", - "pout1": self.sensor_name + "_POWER_O", - "temp1": self.sensor_name + "_TEMP1", - "temp2": self.sensor_name + "_TEMP2", - "vin": self.sensor_name + "_VOL_I", - "vout1": self.sensor_name + "_VOL_O" - }.get(input_name, input_name) - - elif 'isl68137' in sensor_name: - input_name = { - "iin": self.sensor_name + "_CURR_I", - "iout2": self.sensor_name + "_CURR_O", - "pin": self.sensor_name + "_POWER_I", - "pout2": self.sensor_name + "_POWER_O", - "vin": self.sensor_name + "_VOL_I", - "vout2": self.sensor_name + "_VOL_O", - "temp1": self.sensor_name + "_TEMP1" - }.get(input_name, input_name) - - elif 'tmp75' in sensor_name or 'max31730' in sensor_name: - input_name = { - "tmp75-i2c-7-4f": "BASEBOARD_INLET_RIGHT", - "tmp75-i2c-7-4e": "BASEBOARD_INLET_CENTER", - "tmp75-i2c-7-4d": "SWITCH_OUTLET", - "tmp75-i2c-31-48": "PSU_INLET_LEFT", - "tmp75-i2c-31-49": "INLET_TEMP", - "tmp75-i2c-39-48": "FANBOARD_LEFT", - "tmp75-i2c-39-49": "FANBOARD_RIGHT", - "tmp75-i2c-42-48": "LINECARD_TOP_RIGHT", - "tmp75-i2c-42-49": "LINECARD_TOP_LEFT", - "tmp75-i2c-43-48": "LINECARD_BOTTOM_RIGHT", - "tmp75-i2c-43-49": "LINECARD_BOTTOM_LEFT", - "max31730-i2c-7-4c": "SWITCH_REMOTE_" + input_name - }.get(sensor_name, input_name) - self.sensor_name = "TEMPERATURE" - - elif 'fancpld' in sensor_name: - raw_fan_input = input_name.split() - input_name = raw_fan_input[0] + \ - raw_fan_input[1] + "_" + raw_fan_input[2] - - elif 'ir35' in sensor_name or 'ir38' in sensor_name: - sensor_name_raw = sensor_name.split("-") - sensor_name = sensor_name_raw[0] - self.sensor_name = sensor_name.upper() - - return input_name.replace(" ", "_").upper() + def input_name_selector(self, raw_sensor_name): + + sensor_name_list = raw_sensor_name.split('_') + sensor_name = sensor_name_list[0] + input_name = '_'.join(sensor_name_list[1:]) + + if sensor_name_list[0] in ["TOP", "BOTTOM"]: + sensor_name = '_'.join(sensor_name_list[0:2]) + input_name = '_'.join(sensor_name_list[2:]) + + return str(sensor_name).upper(), str(input_name).upper() def get_num_sensors(self): """ @@ -108,18 +51,9 @@ def get_num_sensors(self): :return: int num_sensors """ - num_sensors = 0 - try: - # Request and validate sensor's information - self.sensor_info_list = self.request_data() - - # Get number of sensors. - num_sensors = len(self.sensor_info_list) - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() - return num_sensors + return len(all_sensor_dict) def get_sensor_input_num(self, index): """ @@ -127,19 +61,12 @@ def get_sensor_input_num(self, index): :return: int input_num """ - input_num = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - - # Get sensor's input number. - sensor_data = self.sensor_info_list[index-1] - input_num = len(sensor_data.keys())-2 - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() + ss_keys = all_sensor_dict.keys()[index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys() - return input_num + return len(ss_if_keys) def get_sensor_name(self, index): """ @@ -147,18 +74,8 @@ def get_sensor_name(self, index): for example "coretemp-isa-0000" :return: str sensor_name """ - - sensor_name = "N/A" - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - - # Get sensor's name. - sensor_data = self.sensor_info_list[index-1] - sensor_name = sensor_data.get('name') - - except: - return "N/A" + all_sensor_dict = self.get_all() + sensor_name = all_sensor_dict.keys()[index] return sensor_name @@ -169,23 +86,13 @@ def get_sensor_input_name(self, sensor_index, input_index): :return: str sensor_input_name """ - sensor_input_name = "N/A" - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] + all_sensor_dict = self.get_all() - # Get sensor's input name. - sensor_data_key = sensor_data.keys() - sensor_input_name = sensor_data_key[input_index-1] - except: - return "N/A" + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] - return sensor_input_name + return ss_if_keys def get_sensor_input_type(self, sensor_index, input_index): """ @@ -194,24 +101,15 @@ def get_sensor_input_type(self, sensor_index, input_index): :return: str sensor_input_type """ - sensor_input_type = "N/A" - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input type name. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - sensor_input_type = self.input_type_selector(sensor_data_str[1]) - except: - return "N/A" + all_sensor_dict = self.get_all() + + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + + sensor_input_type = sensor_input_info.get('Type', "N/A") return sensor_input_type def get_sensor_input_value(self, sensor_index, input_index): @@ -220,26 +118,15 @@ def get_sensor_input_value(self, sensor_index, input_index): :return: float sensor_input_value """ - sensor_input_value = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input value. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - sensor_input_value = float( - sensor_data_str[0]) if sensor_data_str[0] != "N/A" else 0 - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() + + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + sensor_input_value = sensor_input_info.get('Value', 0.0) return sensor_input_value def get_sensor_input_low_threshold(self, sensor_index, input_index): @@ -249,33 +136,15 @@ def get_sensor_input_low_threshold(self, sensor_index, input_index): :return: float sensor_input_low_threshold """ - sensor_input_low_threshold = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input low threshold. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - indices = [i for i, s in enumerate( - sensor_data_str) if 'min' in s or 'low' in s] - l_thres = float( - sensor_data_str[indices[0] + 2]) if len(indices) != 0 else 0 - unit = sensor_data_str[indices[0] + - 3] if len(indices) != 0 else None - if unit is not None and len(unit) > 1: - sensor_input_low_threshold = l_thres * \ - 1000 if str(unit[0]).lower() == 'k' else l_thres - except: - print "Error: Unable to access sensor information" - return 0 + all_sensor_dict = self.get_all() + + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + + sensor_input_low_threshold = sensor_input_info.get('LowThd', 0.0) return sensor_input_low_threshold def get_sensor_input_high_threshold(self, sensor_index, input_index): @@ -284,100 +153,54 @@ def get_sensor_input_high_threshold(self, sensor_index, input_index): the status of this item is not ok if the current value > high_threshold :return: float sensor_input_high_threshold """ + all_sensor_dict = self.get_all() - sensor_input_high_threshold = 0 - try: - # Request and validate sensor's information. - self.sensor_info_list = self.request_data() - sensor_data = self.sensor_info_list[sensor_index-1].copy() - - # Remove none input key. - del sensor_data["name"] - del sensor_data["Adapter"] - - # Get sensor's input high threshold. - sensor_data_key = sensor_data.keys() - sensor_input_raw = sensor_data.get(sensor_data_key[input_index-1]) - sensor_data_str = sensor_input_raw.split() - indices = [i for i, s in enumerate( - sensor_data_str) if 'max' in s or 'high' in s] - h_thres = float( - sensor_data_str[indices[0] + 2]) if len(indices) != 0 else 0 - unit = sensor_data_str[indices[0] + - 3] if len(indices) != 0 else None - if unit is not None and len(unit) > 1: - sensor_input_high_threshold = h_thres * \ - 1000 if str(unit[0]).lower() == 'k' else h_thres - - except: - print "Error: Unable to access sensor information" - return 0 + ss_keys = all_sensor_dict.keys()[sensor_index] + sensor_info = all_sensor_dict.get(ss_keys, {}) + ss_if_keys = sensor_info.keys()[input_index] + sensor_input_info = sensor_info.get(ss_if_keys, {}) + + sensor_input_high_threshold = sensor_input_info.get('HighThd', 0.0) return sensor_input_high_threshold - def get_sys_airflow(self): - sys_air_flow = "Unknown" - sys_pn_data = [ - v.split(":") for v in self.sys_fruid_list if "Product Part Number" in v] + def get_all(self): + """ + Get all information of system sensors, returns JSON objects in python 'DICT'. + SensorName1, SensorName2, ... optional, string + SensorInput1, SensorInput2, ... optional, string + Type, mandatory in SensorInput$INDEX, should be on of { "temperature", "voltage", "power", "amp", "RPM" } + Value, mandatory in SensorInput$INDEX, float , real value + LowThd, mandatory in SensorInput$INDEX, float , lower bound of value + HighThd, mandatory in SensorInput$INDEX, float , upper bound of value + Return python 'dict' objects, example: + """ + + if not self.all_sensor_dict: + all_sensor_dict = dict() - if len(sys_pn_data) == 0: - return sys_air_flow + sensor_info_req = self.request_data(self.sensor_info_url) + sensor_info_data = sensor_info_req.get('data', {}) - sys_pn = sys_pn_data[0][1] - if "R1240-F0001" in sys_pn: - sys_air_flow = "FTOB" - elif"R1240-F0002" in sys_pn: - sys_air_flow = "BTOF" + for raw_ss_name, sensor_info in sensor_info_data.items(): - return sys_air_flow + sensor_name, input_name = self.input_name_selector(raw_ss_name) + sensor_dict = all_sensor_dict.get(sensor_name, {}) + new_sensor_dict = dict() + new_sensor_dict["Type"] = self.input_type_selector( + sensor_info.get('Unit', 'N/A')) + new_sensor_dict["Value"] = float(sensor_info.get('Value', 0.0)) + new_sensor_dict["HighThd"] = float(sensor_info.get('Max', 0.0)) + new_sensor_dict["LowThd"] = float(sensor_info.get('Min', 0.0)) - def get_all(self): + if sensor_dict == {}: + all_sensor_dict[sensor_name] = dict() + + input_name = input_name if input_name != '' else new_sensor_dict["Type"].upper( + ) + sensor_dict[input_name] = new_sensor_dict + all_sensor_dict[sensor_name].update(sensor_dict) + + self.all_sensor_dict = all_sensor_dict - all_sensor_dict = dict() - - # Request sensor's information. - self.sensor_info_list = self.request_data() - for sensor_data in self.sensor_info_list: - sensor_info = sensor_data.copy() - - # Remove none unuse key. - del sensor_info["name"] - del sensor_info["Adapter"] - - # Set sensor data. - sensor_dict = dict() - for k, v in sensor_info.items(): - sensor_i_dict = dict() - sensor_data_str = v.split() - indices_h = [i for i, s in enumerate( - sensor_data_str) if 'max' in s or 'high' in s] - indices_l = [i for i, s in enumerate( - sensor_data_str) if 'min' in s or 'low' in s] - h_thres = float( - sensor_data_str[indices_h[0] + 2]) if len(indices_h) != 0 else 0 - l_thres = float( - sensor_data_str[indices_l[0] + 2]) if len(indices_l) != 0 else 0 - thres_unit = sensor_data_str[-1] - - sensor_i_dict["Type"] = self.input_type_selector( - sensor_data_str[1]) - sensor_i_dict["Value"] = float( - sensor_data_str[0]) if sensor_data_str[0] != "N/A" else 0 - sensor_i_dict["HighThd"] = h_thres * \ - 1000 if str(thres_unit[0]).lower() == 'k' else h_thres - sensor_i_dict["LowThd"] = l_thres * \ - 1000 if str(thres_unit[0]).lower() == 'k' else l_thres - - k = self.input_name_selector(sensor_data.get('name'), k) - sensor_dict[k] = sensor_i_dict - - if all_sensor_dict.get(self.sensor_name) is None: - all_sensor_dict[self.sensor_name] = dict() - - all_sensor_dict[self.sensor_name].update(sensor_dict) - - sensor_dict = dict() - sensor_dict["Sys_AirFlow"] = self.get_sys_airflow() - all_sensor_dict["TEMPERATURE"].update(sensor_dict) - - return all_sensor_dict + return self.all_sensor_dict diff --git a/platform/broadcom/sonic-platform-modules-cel b/platform/broadcom/sonic-platform-modules-cel index 0b11a72824d0..a36a0318ecd6 160000 --- a/platform/broadcom/sonic-platform-modules-cel +++ b/platform/broadcom/sonic-platform-modules-cel @@ -1 +1 @@ -Subproject commit 0b11a72824d0cfe42f7656292c3133483b0c42a9 +Subproject commit a36a0318ecd6e7308f84555a92e7e9cc4e5a4087