Skip to content

Commit

Permalink
Add awk back
Browse files Browse the repository at this point in the history
Signed-off-by: maipbui <maibui@microsoft.com>
  • Loading branch information
maipbui committed Sep 8, 2022
1 parent cd61bca commit 017f143
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 101 deletions.
90 changes: 46 additions & 44 deletions device/dell/x86_64-dellemc_s5232f_c3538-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
IPMI_PSU2_DATA = ["docker", "exec", "-it", "pmon", "ipmitool", "raw", "0x04", "0x2d", "0x32"]
IPMI_PSU2_DATA_DOCKER = ["ipmitool", "raw", "0x04", "0x2d", "0x32"]
PSU_PRESENCE = "PSU{0}_stat"
awk_cmd = ['awk', '{print substr($0,9,1)}']
# Use this for older firmware
# PSU_PRESENCE="PSU{0}_prsnt"

Expand All @@ -40,28 +41,28 @@ def isDockerEnv(self):
# Fetch a BMC register
def get_pmc_register(self, reg_name):

status = 1
ipmi_cmd = ''
dockerenv = self.isDockerEnv()
if dockerenv == True:
if index == 1:
p = subprocess.run(IPMI_PSU1_DATA_DOCKER, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU1_DATA_DOCKER
elif index == 2:
p = subprocess.run(IPMI_PSU2_DATA_DOCKER, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU2_DATA_DOCKER
else:
if index == 1:
p = subprocess.run(IPMI_PSU1_DATA, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU1_DATA
elif index == 2:
p = subprocess.run(IPMI_PSU2_DATA, capture_output=True, universal_newlines=True)
status = p.returncode
line = p.stdout
if line[-1:] == '\n':
line = line[:-1]
ipmi_sdr_list = line[8] if len(line) > 8 else ''

if status:
logging.error('Failed to execute ipmitool')
sys.exit(0)

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]
output = ipmi_sdr_list

return output
Expand All @@ -85,28 +86,28 @@ 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:
if index == 1:
p = subprocess.run(IPMI_PSU1_DATA_DOCKER, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU1_DATA_DOCKER
elif index == 2:
p = subprocess.run(IPMI_PSU2_DATA_DOCKER, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU2_DATA_DOCKER
else:
if index == 1:
p = subprocess.run(IPMI_PSU1_DATA, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU1_DATA
elif index == 2:
p = subprocess.run(IPMI_PSU2_DATA, capture_output=True, universal_newlines=True)
ret_status = p.returncode
line = p.stdout
if line[-1:] == '\n':
line = line[:-1]
ipmi_sdr_list = line[8] if len(line) > 8 else ''

if ret_status:
logging.error('Failed to execute ipmitool : ')
sys.exit(0)

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]
psu_status = ipmi_sdr_list
return (not int(psu_status, 16) > 1)

Expand All @@ -118,27 +119,28 @@ 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:
if index == 1:
p = subprocess.run(IPMI_PSU1_DATA_DOCKER, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU1_DATA_DOCKER
elif index == 2:
p = subprocess.run(IPMI_PSU2_DATA_DOCKER, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU2_DATA_DOCKER
else:
if index == 1:
p = subprocess.run(IPMI_PSU1_DATA, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU1_DATA
elif index == 2:
p = subprocess.run(IPMI_PSU2_DATA, capture_output=True, universal_newlines=True)
ret_status = p.returncode
line = p.stdout
if line[-1:] == '\n':
line = line[:-1]
ipmi_sdr_list = line[8] if len(line) > 8 else '0'

if ret_status:
logging.error('Failed to execute ipmitool : ')
sys.exit(0)
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]

psu_status = ipmi_sdr_list
return (int(psu_status, 16) & 1)
13 changes: 7 additions & 6 deletions device/dell/x86_64-dellemc_s5296f_c3538-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ 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)]
p = subprocess.run(cmd, capture_output=True, universal_newlines=True)
line = p.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)}"]
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]
return 1 if psu_status == '1' else 0
56 changes: 26 additions & 30 deletions device/dell/x86_64-dellemc_z9332f_d1508-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#


import os.path
import logging
import sys
import subprocess
Expand All @@ -19,6 +18,7 @@
# Use this for older firmware
# PSU_PRESENCE="PSU{0}_prsnt"
ipmi_sdr_list = ""
awk_cmd = ['awk', '{print substr($0,9,1)}']


try:
Expand All @@ -43,32 +43,28 @@ def isDockerEnv(self):
# Fetch a BMC register
def get_pmc_register(self, reg_name):

status = 1
global ipmi_sdr_list
ipmi_dev_node = "/dev/pmi0"
ipmi_cmd_1 = IPMI_PSU1_DATA
ipmi_cmd_2 = IPMI_PSU1_DATA
ipmi_cmd = ''
dockerenv = self.isDockerEnv()
if dockerenv == True:
if index == 1:
p = subprocess.run(IPMI_PSU1_DATA_DOCKER, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU1_DATA_DOCKER
elif index == 2:
p = subprocess.run(IPMI_PSU2_DATA_DOCKER, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU2_DATA_DOCKER
else:
if index == 1:
p = subprocess.run(IPMI_PSU1_DATA, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU1_DATA
elif index == 2:
p = subprocess.run(IPMI_PSU2_DATA, capture_output=True, universal_newlines=True)
status = p.returncode
line = p.stdout
if line[-1:] == '\n':
line = line[:-1]
ipmi_sdr_list = line[8] if len(line) > 8 else ''

if status:
logging.error('Failed to execute ipmitool')
sys.exit(0)

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]
output = ipmi_sdr_list

return output
Expand Down Expand Up @@ -101,32 +97,32 @@ 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
"""
ipmi_cmd = ''
status = 0
ret_status = 1
# ret_status = 1
global ipmi_sdr_list
ipmi_dev_node = "/dev/pmi0"
dockerenv = self.isDockerEnv()
if dockerenv == True:
if index == 1:
p = subprocess.run(IPMI_PSU1_DATA_DOCKER, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU1_DATA_DOCKER
elif index == 2:
p = subprocess.run(IPMI_PSU2_DATA_DOCKER, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU2_DATA_DOCKER
else:
if index == 1:
p = subprocess.run(IPMI_PSU1_DATA, capture_output=True, universal_newlines=True)
ipmi_cmd = IPMI_PSU1_DATA
elif index == 2:
p = subprocess.run(IPMI_PSU2_DATA, capture_output=True, universal_newlines=True)
status = p.returncode
line = p.stdout
if line[-1:] == '\n':
line = line[:-1]
ipmi_sdr_list = line[8] if len(line) > 8 else ''
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 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':
Expand Down
44 changes: 23 additions & 21 deletions device/dell/x86_64-dellemc_z9432f_c3758-r0/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PsuUtil(PsuBase):
IPMI_PSU_FAN_SPEED = ["ipmitool", "sdr", "get", "PSU{0}_Fan"]
IPMI_FRU = ["ipmitool", "fru"]
IPMI_FRU_DATA = ["ipmitool", "fru", "print", ""]
awk_cmd = ['awk', '{print substr($0,9,1)}']

def __init__(self):
PsuBase.__init__(self)
Expand Down Expand Up @@ -146,22 +147,22 @@ def get_psu_status(self, index):
faulty
"""
psu_status = '0'

ipmi_cmd = ''
if index == 1:
p = subprocess.run(self.IPMI_PSU1_DATA, capture_output=True, universal_newlines=True)
cmd_status, line = p.returncode, p.stdout
ipmi_cmd = self.IPMI_PSU1_DATA
elif index == 2:
p = subprocess.run(self.IPMI_PSU2_DATA, capture_output=True, universal_newlines=True)
cmd_status, line = p.returncode, p.stdout
ipmi_cmd = self.IPMI_PSU2_DATA
else:
logging.error("Invalid PSU number:" + index)
if line[-1:] == '\n':
line = line[:-1]
psu_status = line[8] if len(line) > 8 else '0'

if cmd_status:
logging.error('Failed to execute ipmitool')

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]
return (not int(psu_status, 16) > 1)

def get_psu_presence(self, index):
Expand All @@ -173,21 +174,22 @@ def get_psu_presence(self, index):
"""

psu_status = '0'

ipmi_cmd = ''
if index == 1:
p = subprocess.run(self.IPMI_PSU1_DATA, capture_output=True, universal_newlines=True)
cmd_status, line = p.returncode, p.stdout
ipmi_cmd = self.IPMI_PSU1_DATA
elif index == 2:
p = subprocess.run(self.IPMI_PSU2_DATA, capture_output=True, universal_newlines=True)
cmd_status, line = p.returncode, p.stdout
ipmi_cmd = self.IPMI_PSU2_DATA
else:
logging.error("Invalid PSU number:" + index)
if line[-1:] == '\n':
line = line[:-1]
psu_status = line[8] if len(line) > 8 else '0'

if cmd_status:
logging.error('Failed to execute ipmitool')
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]

return (int(psu_status, 16) & 1)

Expand Down

0 comments on commit 017f143

Please sign in to comment.