Skip to content

Commit

Permalink
Add function in sonic_py_common library
Browse files Browse the repository at this point in the history
Signed-off-by: maipbui <maibui@microsoft.com>
  • Loading branch information
maipbui committed Sep 13, 2022
1 parent 017f143 commit 8593247
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions device/dell/x86_64-dellemc_n3248pxe_c3338-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions device/dell/x86_64-dellemc_n3248te_c3338-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 5 additions & 9 deletions device/dell/x86_64-dellemc_s5212f_c3538-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

16 changes: 5 additions & 11 deletions device/dell/x86_64-dellemc_s5224f_c3538-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

57 changes: 25 additions & 32 deletions device/dell/x86_64-dellemc_s5232f_c3538-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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)

Expand All @@ -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:
Expand All @@ -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)
5 changes: 2 additions & 3 deletions device/dell/x86_64-dellemc_s5248f_c3538-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import logging
import sys
import subprocess
from sonic_py_common.general import getstatusoutput_noshell


S5248F_MAX_PSUS = 2
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 3 additions & 8 deletions device/dell/x86_64-dellemc_s5296f_c3538-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
5 changes: 2 additions & 3 deletions device/dell/x86_64-dellemc_z9264f_c3538-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import logging
import sys
import subprocess
from sonic_py_common.general import getstatusoutput_noshell


Z9264F_MAX_PSUS = 2
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 8593247

Please sign in to comment.