2019-10-09 19:47:46 -05:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
|
|
|
|
try:
|
|
|
|
from sonic_psu.psu_base import PsuBase
|
|
|
|
except ImportError as e:
|
2020-11-25 12:28:36 -06:00
|
|
|
raise ImportError(str(e) + "- required module not found")
|
2019-10-09 19:47:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
class PsuUtil(PsuBase):
|
|
|
|
"""Platform-specific PSUutil class"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2022-12-09 09:30:20 -06:00
|
|
|
self.ipmi_raw = ["docker", "exec", "-ti", "pmon", "ipmitool", "raw", "0x4", "0x2d", ""]
|
2019-10-09 19:47:46 -05:00
|
|
|
self.psu1_id = "0x2f"
|
|
|
|
self.psu2_id = "0x39"
|
|
|
|
PsuBase.__init__(self)
|
|
|
|
|
|
|
|
def run_command(self, command):
|
2022-12-09 09:30:20 -06:00
|
|
|
proc = subprocess.Popen(command, universal_newlines=True, stdout=subprocess.PIPE)
|
2019-10-09 19:47:46 -05:00
|
|
|
(out, err) = proc.communicate()
|
|
|
|
|
|
|
|
if proc.returncode != 0:
|
|
|
|
sys.exit(proc.returncode)
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2019-10-09 19:47:46 -05:00
|
|
|
return out
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2019-10-09 19:47:46 -05:00
|
|
|
def find_value(self, in_string):
|
|
|
|
result = re.search("^.+ ([0-9a-f]{2}) .+$", in_string)
|
|
|
|
if result:
|
|
|
|
return result.group(1)
|
|
|
|
else:
|
|
|
|
return result
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2019-10-09 19:47:46 -05:00
|
|
|
def get_num_psus(self):
|
|
|
|
"""
|
|
|
|
Retrieves the number of PSUs available on the device
|
|
|
|
:return: An integer, the number of PSUs available on the device
|
|
|
|
"""
|
|
|
|
return 2
|
|
|
|
|
|
|
|
def get_psu_status(self, index):
|
|
|
|
"""
|
|
|
|
Retrieves the oprational status of power supply unit (PSU) defined
|
|
|
|
by 1-based index <index>
|
|
|
|
:param index: An integer, 1-based index of the PSU of which to query status
|
|
|
|
:return: Boolean, True if PSU is operating properly, False if PSU is faulty
|
|
|
|
"""
|
|
|
|
if index is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
psu_id = self.psu1_id if index == 1 else self.psu2_id
|
2022-12-09 09:30:20 -06:00
|
|
|
self.ipmi_raw[8] = psu_id
|
|
|
|
res_string = self.run_command(self.ipmi_raw)
|
2019-10-09 19:47:46 -05:00
|
|
|
status_byte = self.find_value(res_string)
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2019-10-09 19:47:46 -05:00
|
|
|
if status_byte is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
failure_detected = (int(status_byte, 16) >> 1) & 1
|
|
|
|
input_lost = (int(status_byte, 16) >> 3) & 1
|
|
|
|
if failure_detected or input_lost:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_psu_presence(self, index):
|
|
|
|
"""
|
|
|
|
Retrieves the presence status of power supply unit (PSU) defined
|
|
|
|
by 1-based index <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
|
|
|
|
"""
|
|
|
|
if index is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
psu_id = self.psu1_id if index == 1 else self.psu2_id
|
2022-12-09 09:30:20 -06:00
|
|
|
self.ipmi_raw[8] = psu_id
|
|
|
|
res_string = self.run_command(self.ipmi_raw)
|
2019-10-09 19:47:46 -05:00
|
|
|
status_byte = self.find_value(res_string)
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2019-10-09 19:47:46 -05:00
|
|
|
if status_byte is None:
|
|
|
|
return False
|
2020-11-25 12:28:36 -06:00
|
|
|
|
|
|
|
presence = (int(status_byte, 16) >> 0) & 1
|
2019-10-09 19:47:46 -05:00
|
|
|
if presence:
|
|
|
|
return True
|
|
|
|
else:
|
2020-11-25 12:28:36 -06:00
|
|
|
return False
|