|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +############################################################################# |
| 4 | +# Mellanox |
| 5 | +# |
| 6 | +# Module contains an implementation of SONiC Platform Base API and |
| 7 | +# provides the PSUs status which are available in the platform |
| 8 | +# |
| 9 | +############################################################################# |
| 10 | + |
| 11 | +import os.path |
| 12 | + |
| 13 | +try: |
| 14 | + from sonic_platform_base.psu_base import PsuBase |
| 15 | +except ImportError as e: |
| 16 | + raise ImportError (str(e) + "- required module not found") |
| 17 | + |
| 18 | +psu_list = [] |
| 19 | + |
| 20 | +class Psu(PsuBase): |
| 21 | + """Platform-specific Psu class""" |
| 22 | + def __init__(self, psu_index): |
| 23 | + global psu_list |
| 24 | + PsuBase.__init__(self) |
| 25 | + # PSU is 1-based on Mellanox platform |
| 26 | + self.index = psu_index + 1 |
| 27 | + psu_list.append(psu_index) |
| 28 | + self.psu_path = "/var/run/hw-management/thermal/" |
| 29 | + self.psu_oper_status = "psu{}_pwr_status".format(self.index) |
| 30 | + self.psu_presence = "psu{}_status".format(self.index) |
| 31 | + if os.path.exists(os.path.join(self.psu_path, self.psu_presence)): |
| 32 | + self.presence_file_exists = True |
| 33 | + else: |
| 34 | + self.presence_file_exists = False |
| 35 | + |
| 36 | + def get_status(self): |
| 37 | + """ |
| 38 | + Retrieves the operational status of power supply unit (PSU) defined |
| 39 | +
|
| 40 | + Returns: |
| 41 | + bool: True if PSU is operating properly, False if not |
| 42 | + """ |
| 43 | + status = 0 |
| 44 | + try: |
| 45 | + with open(os.path.join(self.psu_path, self.psu_oper_status), 'r') as power_status: |
| 46 | + status = int(power_status.read()) |
| 47 | + except (ValueError, IOError): |
| 48 | + status = 0 |
| 49 | + |
| 50 | + return status == 1 |
| 51 | + |
| 52 | + def get_presence(self): |
| 53 | + """ |
| 54 | + Retrieves the presence status of power supply unit (PSU) defined |
| 55 | +
|
| 56 | + Returns: |
| 57 | + bool: True if PSU is present, False if not |
| 58 | + """ |
| 59 | + status = 0 |
| 60 | + if self.presence_file_exists: |
| 61 | + try: |
| 62 | + with open(os.path.join(self.psu_path, self.psu_presence), 'r') as presence_status: |
| 63 | + status = int(presence_status.read()) |
| 64 | + except (ValueError, IOError): |
| 65 | + status = 0 |
| 66 | + else: |
| 67 | + status = self.index in psu_list |
| 68 | + |
| 69 | + return status == 1 |
| 70 | + |
0 commit comments