From 8593247200669a7eb7121db97b7ed364b156b302 Mon Sep 17 00:00:00 2001 From: maipbui Date: Tue, 13 Sep 2022 21:13:57 +0000 Subject: [PATCH] Add function in sonic_py_common library Signed-off-by: maipbui --- .../plugins/fanutil.py | 8 +-- .../plugins/psuutil.py | 10 +-- .../plugins/fanutil.py | 8 +-- .../plugins/psuutil.py | 10 +-- .../plugins/psuutil.py | 14 ++-- .../plugins/psuutil.py | 16 ++--- .../plugins/psuutil.py | 57 +++++++--------- .../plugins/psuutil.py | 5 +- .../plugins/psuutil.py | 11 +-- .../plugins/psuutil.py | 5 +- .../plugins/psuutil.py | 26 +++---- .../plugins/fanutil.py | 34 ++++------ .../plugins/psuutil.py | 68 ++++++++----------- 13 files changed, 105 insertions(+), 167 deletions(-) diff --git a/device/dell/x86_64-dellemc_n3248pxe_c3338-r0/plugins/fanutil.py b/device/dell/x86_64-dellemc_n3248pxe_c3338-r0/plugins/fanutil.py index 1e489aa4375d..e4d342923014 100644 --- a/device/dell/x86_64-dellemc_n3248pxe_c3338-r0/plugins/fanutil.py +++ b/device/dell/x86_64-dellemc_n3248pxe_c3338-r0/plugins/fanutil.py @@ -4,7 +4,7 @@ # import sys -import subprocess +from sonic_py_common.general import getstatusoutput_noshell SENSORS_CMD = ["docker", "exec", "-i", "pmon", "/usr/bin/sensors"] DOCKER_SENSORS_CMD = "/usr/bin/sensors" @@ -47,11 +47,9 @@ def get_direction(self, idx): def get_speed(self, idx): dockerenv = self.isDockerEnv() if not dockerenv: - p = subprocess.run(SENSORS_CMD, capture_output=True, universal_newlines=True) - status, cmd_output = p.returncode, p.stdout + status, cmd_output = getstatusoutput_noshell(SENSORS_CMD) else: - p = subprocess.run(DOCKER_SENSORS_CMD, capture_output=True, universal_newlines=True) - status, cmd_output = p.returncode, p.stdout + status, cmd_output = getstatusoutput_noshell(DOCKER_SENSORS_CMD) if status: print('Failed to execute sensors command') sys.exit(0) diff --git a/device/dell/x86_64-dellemc_n3248pxe_c3338-r0/plugins/psuutil.py b/device/dell/x86_64-dellemc_n3248pxe_c3338-r0/plugins/psuutil.py index 436dae2773fc..758068dc4734 100644 --- a/device/dell/x86_64-dellemc_n3248pxe_c3338-r0/plugins/psuutil.py +++ b/device/dell/x86_64-dellemc_n3248pxe_c3338-r0/plugins/psuutil.py @@ -5,7 +5,7 @@ import os import sys -import subprocess +from sonic_py_common.general import getstatusoutput_noshell SENSORS_CMD = ["docker", "exec", "-i", "pmon", "/usr/bin/sensors"] DOCKER_SENSORS_CMD = "/usr/bin/sensors" @@ -95,13 +95,9 @@ def get_psu_presence(self, index): def get_sensor(self): dockerenv = self.isDockerEnv() if not dockerenv: - p = subprocess.run(SENSORS_CMD, capture_output=True, universal_newlines=True) - status, cmd_output = p.returncode, p.stdout + status, cmd_output = getstatusoutput_noshell(SENSORS_CMD) else: - p = subprocess.run(DOCKER_SENSORS_CMD, capture_output=True, universal_newlines=True) - status, cmd_output = p.returncode, p.stdout - if cmd_output[-1:] == '\n': - cmd_output = cmd_output[:-1] + status, cmd_output = getstatusoutput_noshell(DOCKER_SENSORS_CMD) if status: print('Failed to execute sensors command') sys.exit(0) diff --git a/device/dell/x86_64-dellemc_n3248te_c3338-r0/plugins/fanutil.py b/device/dell/x86_64-dellemc_n3248te_c3338-r0/plugins/fanutil.py index 6302f42d54e1..974d9156e2dc 100644 --- a/device/dell/x86_64-dellemc_n3248te_c3338-r0/plugins/fanutil.py +++ b/device/dell/x86_64-dellemc_n3248te_c3338-r0/plugins/fanutil.py @@ -3,8 +3,8 @@ # Platform-specific FAN status interface for SONiC # -import subprocess import sys +from sonic_py_common.general import getstatusoutput_noshell SENSORS_CMD = ["docker", "exec", "-i", "pmon", "/usr/bin/sensors"] DOCKER_SENSORS_CMD = "/usr/bin/sensors" @@ -47,11 +47,9 @@ def get_direction(self, idx): def get_speed(self, idx): dockerenv = self.isDockerEnv() if not dockerenv: - p = subprocess.run(SENSORS_CMD, capture_output=True, universal_newlines=True) - status, cmd_output = p.returncode, p.stdout + status, cmd_output = getstatusoutput_noshell(SENSORS_CMD) else: - p = subprocess.run(DOCKER_SENSORS_CMD, capture_output=True, universal_newlines=True) - status, cmd_output = p.returncode, p.stdout + status, cmd_output = getstatusoutput_noshell(DOCKER_SENSORS_CMD) if status: print('Failed to execute sensors command') sys.exit(0) diff --git a/device/dell/x86_64-dellemc_n3248te_c3338-r0/plugins/psuutil.py b/device/dell/x86_64-dellemc_n3248te_c3338-r0/plugins/psuutil.py index 41d71cf56234..c334cc89271d 100644 --- a/device/dell/x86_64-dellemc_n3248te_c3338-r0/plugins/psuutil.py +++ b/device/dell/x86_64-dellemc_n3248te_c3338-r0/plugins/psuutil.py @@ -5,7 +5,7 @@ import os import sys -import subprocess +from sonic_py_common.general import getstatusoutput_noshell SENSORS_CMD = ["docker", "exec", "-i", "pmon", "/usr/bin/sensors"] DOCKER_SENSORS_CMD = "/usr/bin/sensors" @@ -95,13 +95,9 @@ def get_psu_presence(self, index): def get_sensor(self): dockerenv = self.isDockerEnv() if not dockerenv: - p = subprocess.run(SENSORS_CMD, capture_output=True, universal_newlines=True) - status, cmd_output = p.returncode, p.stdout + status, cmd_output = getstatusoutput_noshell(SENSORS_CMD) else: - p = subprocess.run(DOCKER_SENSORS_CMD, capture_output=True, universal_newlines=True) - status, cmd_output = p.returncode, p.stdout - if cmd_output[-1:] == '\n': - cmd_output = cmd_output[:-1] + status, cmd_output = getstatusoutput_noshell(DOCKER_SENSORS_CMD) if status: print('Failed to execute sensors command') sys.exit(0) diff --git a/device/dell/x86_64-dellemc_s5212f_c3538-r0/plugins/psuutil.py b/device/dell/x86_64-dellemc_s5212f_c3538-r0/plugins/psuutil.py index 6008d038baf7..f8c5b0193a4c 100644 --- a/device/dell/x86_64-dellemc_s5212f_c3538-r0/plugins/psuutil.py +++ b/device/dell/x86_64-dellemc_s5212f_c3538-r0/plugins/psuutil.py @@ -5,7 +5,7 @@ import logging import sys -import subprocess +from sonic_py_common.general import getstatusoutput_noshell, getstatusoutput_noshell_pipe S5212F_MAX_PSUS = 2 IPMI_PSU_DATA = ["docker", "exec", "-it", "pmon", "ipmitool", "sdr", "list"] @@ -44,8 +44,7 @@ def get_pmc_register(self, reg_name): if dockerenv == True: ipmi_cmd = IPMI_PSU_DATA_DOCKER - p = subprocess.run(ipmi_cmd, capture_output=True, universal_newlines=True) - status, ipmi_sdr_list = p.returncode, p.stdout + status, ipmi_sdr_list = getstatusoutput_noshell(ipmi_cmd) if status: logging.error('Failed to execute:' + ipmi_sdr_list) @@ -92,11 +91,8 @@ def get_psu_presence(self, index): :param index: An integer, index of the PSU of which to query status :return: Boolean, True if PSU is plugged, False if not """ - cmd = ["ipmitool", "raw", "0x04", "0x2d", hex(0x30 + index)] - proc = subprocess.run(cmd, capture_output=True, universal_newlines=True) - line = proc.stdout - if line[-1:] == '\n': - line = line[:-1] - psu_status = line[8] if len(line) > 8 else '' + ipmi_cmd = ["ipmitool", "raw", "0x04", "0x2d", hex(0x30 + index)] + awk_cmd = ['awk', '{print substr($0,9,1)}'] + cmd_status, psu_status = getstatusoutput_noshell_pipe(ipmi_cmd, awk_cmd) return 1 if psu_status == '1' else 0 diff --git a/device/dell/x86_64-dellemc_s5224f_c3538-r0/plugins/psuutil.py b/device/dell/x86_64-dellemc_s5224f_c3538-r0/plugins/psuutil.py index 4270e39e8b0b..6613af1661ea 100644 --- a/device/dell/x86_64-dellemc_s5224f_c3538-r0/plugins/psuutil.py +++ b/device/dell/x86_64-dellemc_s5224f_c3538-r0/plugins/psuutil.py @@ -6,7 +6,7 @@ import logging import sys -import subprocess +from sonic_py_common.general import getstatusoutput_noshell, getstatusoutput_noshell_pipe S5224F_MAX_PSUS = 2 IPMI_PSU_DATA = "docker exec -it pmon ipmitool sdr list" @@ -45,10 +45,7 @@ def get_pmc_register(self, reg_name): if dockerenv == True: ipmi_cmd = IPMI_PSU_DATA_DOCKER - p = subprocess.run(ipmi_cmd, capture_output=True, universal_newlines=True) - status, ipmi_sdr_list = p.returncode, p.stdout - if ipmi_sdr_list[-1:] == '\n': - ipmi_sdr_list = ipmi_sdr_list[:-1] + status, ipmi_sdr_list = getstatusoutput_noshell(ipmi_cmd) if status: logging.error('Failed to execute:' + ipmi_sdr_list) @@ -95,11 +92,8 @@ def get_psu_presence(self, index): :param index: An integer, index of the PSU of which to query status :return: Boolean, True if PSU is plugged, False if not """ - cmd = ["ipmitool", "raw", "0x04", "0x2d", hex(0x30 + index)] - proc = subprocess.run(cmd, capture_output=True, universal_newlines=True) - line = proc.stdout - if line[-1:] == '\n': - line = line[:-1] - psu_status = line[8] if len(line) > 8 else '' + ipmi_cmd = ["ipmitool", "raw", "0x04", "0x2d", hex(0x30 + index)] + awk_cmd = ['awk', '{print substr($0,9,1)}'] + cmd_status, psu_status = getstatusoutput_noshell_pipe(ipmi_cmd, awk_cmd) return 1 if psu_status == '1' else 0 diff --git a/device/dell/x86_64-dellemc_s5232f_c3538-r0/plugins/psuutil.py b/device/dell/x86_64-dellemc_s5232f_c3538-r0/plugins/psuutil.py index f31373ac12fd..5dc44f9918fd 100644 --- a/device/dell/x86_64-dellemc_s5232f_c3538-r0/plugins/psuutil.py +++ b/device/dell/x86_64-dellemc_s5232f_c3538-r0/plugins/psuutil.py @@ -6,7 +6,7 @@ import logging import sys -import subprocess +from sonic_py_common.general import getstatusoutput_noshell_pipe S5232F_MAX_PSUS = 2 IPMI_PSU1_DATA = ["docker", "exec", "-it", "pmon", "ipmitool", "raw", "0x04", "0x2d", "0x31"] @@ -39,8 +39,9 @@ def isDockerEnv(self): return False # Fetch a BMC register - def get_pmc_register(self, reg_name): + def get_pmc_register(self, index): + status = 1 ipmi_cmd = '' dockerenv = self.isDockerEnv() if dockerenv == True: @@ -53,16 +54,13 @@ def get_pmc_register(self, reg_name): ipmi_cmd = IPMI_PSU1_DATA elif index == 2: ipmi_cmd = IPMI_PSU2_DATA - with subprocess.Popen(ipmi_cmd, universal_newlines=True, stdout=subprocess.PIPE) as p1: - with subprocess.Popen(awk_cmd, universal_newlines=True, stdin=p1.stdout, stdout=subprocess.PIPE) as p2: - ipmi_sdr_list = p2.communicate()[0] - p1.wait() - if p1.returncode != 0 and p2.returncode != 0: - logging.error('Failed to execute ipmitool') - sys.exit(0) - - if ipmi_sdr_list[-1:] == '\n': - ipmi_sdr_list = ipmi_sdr_list[:-1] + if ipmi_cmd != '': + status, ipmi_sdr_list = getstatusoutput_noshell_pipe(ipmi_cmd, awk_cmd) + + if status: + logging.error('Failed to execute ipmitool') + sys.exit(0) + output = ipmi_sdr_list return output @@ -86,6 +84,7 @@ def get_psu_status(self, index): # Until psu_status is implemented this is hardcoded temporarily psu_status = '' + ret_status = 1 ipmi_cmd = '' dockerenv = self.isDockerEnv() if dockerenv == True: @@ -98,16 +97,13 @@ def get_psu_status(self, index): ipmi_cmd = IPMI_PSU1_DATA elif index == 2: ipmi_cmd = IPMI_PSU2_DATA - with subprocess.Popen(ipmi_cmd, universal_newlines=True, stdout=subprocess.PIPE) as p1: - with subprocess.Popen(awk_cmd, universal_newlines=True, stdin=p1.stdout, stdout=subprocess.PIPE) as p2: - ipmi_sdr_list = p2.communicate()[0] - p1.wait() - if p1.returncode != 0 and p2.returncode != 0: - logging.error('Failed to execute ipmitool') - sys.exit(0) - - if ipmi_sdr_list[-1:] == '\n': - ipmi_sdr_list = ipmi_sdr_list[:-1] + if ipmi_cmd != '': + ret_status, ipmi_sdr_list = getstatusoutput_noshell_pipe(ipmi_cmd, awk_cmd) + + if ret_status: + logging.error('Failed to execute ipmitool') + sys.exit(0) + psu_status = ipmi_sdr_list return (not int(psu_status, 16) > 1) @@ -119,6 +115,7 @@ def get_psu_presence(self, index): :return: Boolean, True if PSU is plugged, False if not """ psu_status = '0' + ret_status = 1 ipmi_cmd = '' dockerenv = self.isDockerEnv() if dockerenv == True: @@ -131,16 +128,12 @@ def get_psu_presence(self, index): ipmi_cmd = IPMI_PSU1_DATA elif index == 2: ipmi_cmd = IPMI_PSU2_DATA - with subprocess.Popen(ipmi_cmd, universal_newlines=True, stdout=subprocess.PIPE) as p1: - with subprocess.Popen(awk_cmd, universal_newlines=True, stdin=p1.stdout, stdout=subprocess.PIPE) as p2: - ipmi_sdr_list = p2.communicate()[0] - p1.wait() - if p1.returncode != 0 and p2.returncode != 0: - logging.error('Failed to execute ipmitool') - sys.exit(0) - - if ipmi_sdr_list[-1:] == '\n': - ipmi_sdr_list = ipmi_sdr_list[:-1] + if ipmi_cmd != '': + ret_status, ipmi_sdr_list = getstatusoutput_noshell_pipe(ipmi_cmd, awk_cmd) + + if ret_status: + logging.error('Failed to execute ipmitool') + sys.exit(0) psu_status = ipmi_sdr_list return (int(psu_status, 16) & 1) diff --git a/device/dell/x86_64-dellemc_s5248f_c3538-r0/plugins/psuutil.py b/device/dell/x86_64-dellemc_s5248f_c3538-r0/plugins/psuutil.py index 27516258f811..9a4e20705b32 100644 --- a/device/dell/x86_64-dellemc_s5248f_c3538-r0/plugins/psuutil.py +++ b/device/dell/x86_64-dellemc_s5248f_c3538-r0/plugins/psuutil.py @@ -6,7 +6,7 @@ import logging import sys -import subprocess +from sonic_py_common.general import getstatusoutput_noshell S5248F_MAX_PSUS = 2 @@ -48,8 +48,7 @@ def get_pmc_register(self, reg_name): if dockerenv == True: ipmi_cmd = IPMI_PSU_DATA_DOCKER - p = subprocess.run(ipmi_cmd, capture_output=True, universal_newlines=True) - status, ipmi_sdr_list = p.returncode, p.stdout + status, ipmi_sdr_list = getstatusoutput_noshell(ipmi_cmd) if status: logging.error('Failed to execute:' + ipmi_sdr_list) diff --git a/device/dell/x86_64-dellemc_s5296f_c3538-r0/plugins/psuutil.py b/device/dell/x86_64-dellemc_s5296f_c3538-r0/plugins/psuutil.py index 8f58a8466b58..2c39fa793434 100644 --- a/device/dell/x86_64-dellemc_s5296f_c3538-r0/plugins/psuutil.py +++ b/device/dell/x86_64-dellemc_s5296f_c3538-r0/plugins/psuutil.py @@ -6,7 +6,7 @@ import logging import sys -import subprocess +from sonic_py_common.general import getstatusoutput_noshell, getstatusoutput_noshell_pipe S5296F_MAX_PSUS = 2 @@ -46,8 +46,7 @@ def get_pmc_register(self, reg_name): if dockerenv == True: ipmi_cmd = IPMI_PSU_DATA_DOCKER - p = subprocess.run(ipmi_cmd, capture_output=True, universal_newlines=True) - status, ipmi_sdr_list = p.returncode, p.stdout + status, ipmi_sdr_list = getstatusoutput_noshell(ipmi_cmd) if status: logging.error('Failed to execute:' + ipmi_sdr_list) @@ -96,9 +95,5 @@ def get_psu_presence(self, index): """ ipmi_cmd = ["ipmitool", "raw", "0x04", "0x2d", hex(0x30 + index)] awk_cmd = ["awk", "{print substr($0,9,1)}"] - with subprocess.Popen(ipmi_cmd, universal_newlines=True, stdout=subprocess.PIPE) as p1: - with subprocess.Popen(awk_cmd, universal_newlines=True, stdin=p1.stdout, stdout=subprocess.PIPE) as p2: - psu_status = p2.communicate()[0] - if psu_status[-1:] == '\n': - psu_status = psu_status[:-1] + cmd_status, psu_status = getstatusoutput_noshell_pipe(ipmi_cmd, awk_cmd) return 1 if psu_status == '1' else 0 diff --git a/device/dell/x86_64-dellemc_z9264f_c3538-r0/plugins/psuutil.py b/device/dell/x86_64-dellemc_z9264f_c3538-r0/plugins/psuutil.py index af6f0f0b487f..3f3841095ce9 100644 --- a/device/dell/x86_64-dellemc_z9264f_c3538-r0/plugins/psuutil.py +++ b/device/dell/x86_64-dellemc_z9264f_c3538-r0/plugins/psuutil.py @@ -6,7 +6,7 @@ import logging import sys -import subprocess +from sonic_py_common.general import getstatusoutput_noshell Z9264F_MAX_PSUS = 2 @@ -48,8 +48,7 @@ def get_pmc_register(self, reg_name): if dockerenv == True: ipmi_cmd = IPMI_PSU_DATA_DOCKER - p = subprocess.run(ipmi_cmd, capture_output=True, universal_newlines=True) - status, ipmi_sdr_list = p.returncode, p.stdout + status, ipmi_sdr_list = getstatusoutput_noshell(ipmi_cmd) if status: logging.error('Failed to execute:' + ipmi_sdr_list) diff --git a/device/dell/x86_64-dellemc_z9332f_d1508-r0/plugins/psuutil.py b/device/dell/x86_64-dellemc_z9332f_d1508-r0/plugins/psuutil.py index 2997c926feaf..c5654dfda706 100644 --- a/device/dell/x86_64-dellemc_z9332f_d1508-r0/plugins/psuutil.py +++ b/device/dell/x86_64-dellemc_z9332f_d1508-r0/plugins/psuutil.py @@ -6,7 +6,7 @@ import logging import sys -import subprocess +from sonic_py_common.general import getstatusoutput_noshell_pipe Z9332F_MAX_PSUS = 2 @@ -42,7 +42,7 @@ def isDockerEnv(self): # Fetch a BMC register def get_pmc_register(self, reg_name): - + status = 1 global ipmi_sdr_list ipmi_cmd = '' dockerenv = self.isDockerEnv() @@ -56,15 +56,11 @@ def get_pmc_register(self, reg_name): ipmi_cmd = IPMI_PSU1_DATA elif index == 2: ipmi_cmd = IPMI_PSU2_DATA - with subprocess.Popen(ipmi_cmd, universal_newlines=True, stdout=subprocess.PIPE) as p1: - with subprocess.Popen(awk_cmd, universal_newlines=True, stdin=p1.stdout, stdout=subprocess.PIPE) as p2: - ipmi_sdr_list = p2.communicate()[0] - p1.wait() - if p1.returncode != 0 and p2.returncode != 0: - logging.error('Failed to execute ipmitool') - sys.exit(0) - if ipmi_sdr_list[-1:] == '\n': - ipmi_sdr_list = ipmi_sdr_list[:-1] + if ipmi_cmd != '': + status, ipmi_sdr_list = getstatusoutput_noshell_pipe(ipmi_cmd, awk_cmd) + if status: + logging.error('Failed to execute ipmitool') + sys.exit(0) output = ipmi_sdr_list return output @@ -112,17 +108,13 @@ def get_psu_presence(self, index): ipmi_cmd = IPMI_PSU1_DATA elif index == 2: ipmi_cmd = IPMI_PSU2_DATA - with subprocess.Popen(ipmi_cmd, universal_newlines=True, stdout=subprocess.PIPE) as p1: - with subprocess.Popen(awk_cmd, universal_newlines=True, stdin=p1.stdout, stdout=subprocess.PIPE) as p2: - ipmi_sdr_list = p2.communicate()[0] - + if ipmi_cmd != '': + status, ipmi_sdr_list = getstatusoutput_noshell_pipe(ipmi_cmd, awk_cmd) # if ret_status: # print ipmi_sdr_list # logging.error('Failed to execute ipmitool') # sys.exit(0) - if ipmi_sdr_list[-1:] == '\n': - ipmi_sdr_list = ipmi_sdr_list[:-1] psu_status = ipmi_sdr_list if psu_status == '1': diff --git a/device/dell/x86_64-dellemc_z9432f_c3758-r0/plugins/fanutil.py b/device/dell/x86_64-dellemc_z9432f_c3758-r0/plugins/fanutil.py index 0d976b70992f..3788c0d70585 100644 --- a/device/dell/x86_64-dellemc_z9432f_c3758-r0/plugins/fanutil.py +++ b/device/dell/x86_64-dellemc_z9432f_c3758-r0/plugins/fanutil.py @@ -7,7 +7,7 @@ # ############################################################################# import logging -import subprocess +from sonic_py_common.general import getstatusoutput_noshell try: from sonic_fan.fan_base import FanBase @@ -20,9 +20,9 @@ class FanUtil(FanBase): NUM_FANS_PERTRAY = 2 FANTRAY_NUM_START_IDX = 1 FRU_FAN_START_IDX = 1 - IPMI_FAN_PRESENCE = ["ipmitool", "sensor", "get", "FAN{0}_prsnt"] - IPMI_FAN_FRONT_SPEED = ["ipmitool", "sdr", "get", "Fan{0}_Front_rpm"] - IPMI_FAN_REAR_SPEED = ["ipmitool", "sdr", "get", "Fan{0}_Rear_rpm"] + IPMI_FAN_PRESENCE = ["ipmitool", "sensor", "get", ""] + IPMI_FAN_FRONT_SPEED = ["ipmitool", "sdr", "get", ""] + IPMI_FAN_REAR_SPEED = ["ipmitool", "sdr", "get", ""] IPMI_FRU_DATA = ["ipmitool", "fru", "print", ""] def __init__(self, log_level=logging.DEBUG): @@ -31,45 +31,41 @@ def __init__(self, log_level=logging.DEBUG): def get_fan_status(self,fan_id): try: - self.IPMI_FAN_PRESENCE[3] = self.IPMI_FAN_PRESENCE[3].format(fan_id) - p = subprocess.run(self.IPMI_FAN_PRESENCE, capture_output=True, universal_newlines=True) - ret_status, ipmi_cmd_ret = p.returncode, p.stdout + self.IPMI_FAN_PRESENCE[3] = 'FAN' + str(fan_id) + '_prsnt' + ret_status, ipmi_cmd_ret = getstatusoutput_noshell(self.IPMI_FAN_PRESENCE) if ret_status == 0: return(ipmi_cmd_ret.splitlines()[5].strip(' ').strip('[]')) except Exception: - logging.error('Failed to execute : %s'%(''.join(self.IPMI_FAN_PRESENCE))) + logging.error('Failed to execute : %s'%(' '.join(self.IPMI_FAN_PRESENCE))) def get_front_fan_speed(self,fan_id): try: - self.IPMI_FAN_FRONT_SPEED[3] = self.IPMI_FAN_FRONT_SPEED[3].format(fan_id) - p = subprocess.run(split(self.IPMI_FAN_FRONT_SPEED.format(fan_id)), capture_output=True, universal_newlines=True) - ret_status, ipmi_cmd_ret = p.returncode, p.stdout + self.IPMI_FAN_FRONT_SPEED[3] = 'Fan' + str(fan_id) + '_Front_rpm' + ret_status, ipmi_cmd_ret = getstatusoutput_noshell(self.IPMI_FAN_FRONT_SPEED) if ret_status == 0: rdata = ipmi_cmd_ret.splitlines()[3].split(':')[1].split(' ')[1] return rdata except Exception: - logging.error('Failed to execute : %s'%(''.join(self.IPMI_FAN_FRONT_SPEED))) + logging.error('Failed to execute : %s'%(' '.join(self.IPMI_FAN_FRONT_SPEED))) def get_rear_fan_speed(self,fan_id): try: - self.IPMI_FAN_REAR_SPEED[3] = self.IPMI_FAN_REAR_SPEED[3].format(fan_id) - p = subprocess.run(self.IPMI_FAN_REAR_SPEED, capture_output=True, universal_newlines=True) - ret_status, ipmi_cmd_ret = p.returncode, p.stdout + self.IPMI_FAN_REAR_SPEED[3] = 'Fan' + str(fan_id) + '_Rear_rpm' + ret_status, ipmi_cmd_ret = getstatusoutput_noshell(self.IPMI_FAN_REAR_SPEED) if ret_status == 0: rdata = ipmi_cmd_ret.splitlines()[3].split(':')[1].split(' ')[1] return rdata except Exception: - logging.error('Failed to execute : %s'%(''.join(self.IPMI_FAN_REAR_SPEED))) + logging.error('Failed to execute : %s'%(' '.join(self.IPMI_FAN_REAR_SPEED))) # Read FAN FRU info def get_fan_direction_from_fru(self,fru_id,reg_name): output = None try: - self.IPMI_FRU_DATA[3] += str(fru_id) - p = subprocess.run(self.IPMI_FRU_DATA, capture_output=True, universal_newlines=True) - status, ipmi_fru_list = p.returncode, p.stdout + self.IPMI_FRU_DATA[3] = str(fru_id) + status, ipmi_fru_list = getstatusoutput_noshell(self.IPMI_FRU_DATA) if status == 0: for item in ipmi_fru_list.split("\n"): if reg_name in item: diff --git a/device/dell/x86_64-dellemc_z9432f_c3758-r0/plugins/psuutil.py b/device/dell/x86_64-dellemc_z9432f_c3758-r0/plugins/psuutil.py index 5fa201fd3eec..bad5138343e6 100644 --- a/device/dell/x86_64-dellemc_z9432f_c3758-r0/plugins/psuutil.py +++ b/device/dell/x86_64-dellemc_z9432f_c3758-r0/plugins/psuutil.py @@ -3,7 +3,7 @@ # Platform-specific PSU status interface for SONiC # import logging -import subprocess +from sonic_py_common.general import getstatusoutput_noshell, getstatusoutput_noshell_pipe Z9332F_MAX_PSUS = 2 FRU_PSUL = 11 @@ -20,10 +20,10 @@ class PsuUtil(PsuBase): """Platform-specific PSUutil class""" IPMI_PSU1_DATA = ["ipmitool", "raw", "0x04", "0x2d", "0x2f"] IPMI_PSU2_DATA = ["ipmitool", "raw", "0x04", "0x2d", "0x39"] - IPMI_PSU_VOUT = ["ipmitool", "sdr", "get", "PSU{0}_VOut"] - IPMI_PSU_POUT = ["ipmitool", "sdr", "get", "PSU{0}_POut"] - IPMI_PSU_COUT = ["ipmitool", "sdr", "get", "PSU{0}_COut"] - IPMI_PSU_FAN_SPEED = ["ipmitool", "sdr", "get", "PSU{0}_Fan"] + IPMI_PSU_VOUT = ["ipmitool", "sdr", "get", ""] + IPMI_PSU_POUT = ["ipmitool", "sdr", "get", ""] + IPMI_PSU_COUT = ["ipmitool", "sdr", "get", ""] + IPMI_PSU_FAN_SPEED = ["ipmitool", "sdr", "get", ""] IPMI_FRU = ["ipmitool", "fru"] IPMI_FRU_DATA = ["ipmitool", "fru", "print", ""] awk_cmd = ['awk', '{print substr($0,9,1)}'] @@ -40,51 +40,47 @@ def isDockerEnv(self): def get_psu_vout(self,index): try: - self.IPMI_PSU_VOUT[3] = self.IPMI_PSU_VOUT[3].format(index) - p = subprocess.run(self.IPMI_PSU_VOUT, capture_output=True, universal_newlines=True) - ret_status, ipmi_cmd_ret = p.returncode, p.stdout + self.IPMI_PSU_VOUT[3] = 'PSU' + str(index) + '_VOut' + ret_status, ipmi_cmd_ret = getstatusoutput_noshell(self.IPMI_PSU_VOUT) if ret_status == 0: rdata = ipmi_cmd_ret.splitlines()[3].split(':')[1].split(' ')[1] return rdata except Exception: - logging.error('Failed to execute : %s'%(''.join(self.IPMI_PSU_VOUT))) + logging.error('Failed to execute : %s'%(' '.join(self.IPMI_PSU_VOUT))) def get_psu_cout(self,index): try: - self.IPMI_PSU_COUT[3] = self.IPMI_PSU_COUT[3].format(index) - p = subprocess.run(self.IPMI_PSU_COUT, capture_output=True, universal_newlines=True) - ret_status, ipmi_cmd_ret = p.returncode, p.stdout + self.IPMI_PSU_COUT[3] = 'PSU' + str(index) + 'POut' + ret_status, ipmi_cmd_ret = getstatusoutput_noshell(self.IPMI_PSU_COUT) if ret_status == 0: rdata = ipmi_cmd_ret.splitlines()[3].split(':')[1].split(' ')[1] return rdata except Exception: - logging.error('Failed to execute : %s'%(''.join(self.IPMI_PSU_COUT))) + logging.error('Failed to execute : %s'%(' '.join(self.IPMI_PSU_COUT))) def get_psu_pout(self,index): try: - self.IPMI_PSU_POUT[3] = self.IPMI_PSU_POUT[3].format(index) - p = subprocess.run(self.IPMI_PSU_POUT, capture_output=True, universal_newlines=True) - ret_status, ipmi_cmd_ret = p.returncode, p.stdout + self.IPMI_PSU_POUT[3] = 'PSU' + str(index) + '_COut' + ret_status, ipmi_cmd_ret = getstatusoutput_noshell(self.IPMI_PSU_POUT) if ret_status == 0: rdata = ipmi_cmd_ret.splitlines()[3].split(':')[1].split(' ')[1] return rdata except Exception: - logging.error('Failed to execute : %s'%(''.join(self.IPMI_PSU_POUT))) + logging.error('Failed to execute : %s'%(' '.join(self.IPMI_PSU_POUT))) def get_psu_fan_speed(self,index): try: - self.IPMI_PSU_FAN_SPEED[3] = self.IPMI_PSU_FAN_SPEED[3].format(index) - p = subprocess.run(self.IPMI_PSU_FAN_SPEED, capture_output=True, universal_newlines=True) - ret_status, ipmi_cmd_ret = p.returncode, p.stdout + self.IPMI_PSU_FAN_SPEED[3] = 'PSU' + str(index) + '_Fan' + ret_status, ipmi_cmd_ret = getstatusoutput_noshell(self.IPMI_PSU_FAN_SPEED) if ret_status == 0: rdata = ipmi_cmd_ret.splitlines()[3].split(':')[1].split(' ')[1] return rdata except Exception: - logging.error('Failed to execute : %s'%(''.join(self.IPMI_PSU_FAN_SPEED))) + logging.error('Failed to execute : %s'%(' '.join(self.IPMI_PSU_FAN_SPEED))) #Fetch FRU Data for given fruid @@ -94,8 +90,7 @@ def get_psu_airflow(self, index): else: fru_id = 'FRU_PSUR' - p = subprocess.run(self.IPMI_FRU, capture_output=True, universal_newlines=True) - ret_status, ipmi_cmd_ret = p.returncode, p.stdout + ret_status, ipmi_cmd_ret = getstatusoutput_noshell(self.IPMI_FRU) if ret_status: logging.error('Failed to execute ipmitool: '+ self.IPMI_FRU) @@ -113,8 +108,7 @@ def get_fru_info(self,fru_id,reg_name): Found = False try: self.IPMI_FRU_DATA[3] += str(fru_id) - p = subprocess.run(self.IPMI_FRU_DATA, capture_output=True, universal_newlines=True) - status, ipmi_fru_list = p.returncode, p.stdout + status, ipmi_fru_list = getstatusoutput_noshell(self.IPMI_FRU_DATA) if status == 0: for item in ipmi_fru_list.split("\n"): if reg_name == item.split(':')[0].strip(' '): @@ -155,14 +149,10 @@ def get_psu_status(self, index): else: logging.error("Invalid PSU number:" + index) - with subprocess.Popen(ipmi_cmd, universal_newlines=True, stdout=subprocess.PIPE) as p1: - with subprocess.Popen(self.awk_cmd, universal_newlines=True, stdin=p1.stdout, stdout=subprocess.PIPE) as p2: - psu_status = p2.communicate()[0] - p1.wait() - if p1.returncode != 0 and p2.returncode != 0: - logging.error('Failed to execute ipmitool') - if psu_status[-1:] == '\n': - psu_status = psu_status[:-1] + if ipmi_cmd != '': + cmd_status, psu_status = getstatusoutput_noshell_pipe(ipmi_cmd, self.awk_cmd) + if cmd_status: + logging.error('Failed to execute ipmitool') return (not int(psu_status, 16) > 1) def get_psu_presence(self, index): @@ -182,14 +172,10 @@ def get_psu_presence(self, index): else: logging.error("Invalid PSU number:" + index) - with subprocess.Popen(ipmi_cmd, universal_newlines=True, stdout=subprocess.PIPE) as p1: - with subprocess.Popen(self.awk_cmd, universal_newlines=True, stdin=p1.stdout, stdout=subprocess.PIPE) as p2: - psu_status = p2.communicate()[0] - p1.wait() - if p1.returncode != 0 and p2.returncode != 0: - logging.error('Failed to execute ipmitool') - if psu_status[-1:] == '\n': - psu_status = psu_status[:-1] + if ipmi_cmd != '': + cmd_status, psu_status = getstatusoutput_noshell_pipe(ipmi_cmd, self.awk_cmd) + if cmd_status: + logging.error('Failed to execute ipmitool') return (int(psu_status, 16) & 1)