Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[device/alibaba]: Update platform util follow bmc hal api #133

279 changes: 68 additions & 211 deletions device/alibaba/x86_64-alibaba_as23-128h-cl-r0/plugins/fanutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,51 @@

__author__ = 'Wirut G.<wgetbumr@celestica.com>'
__license__ = "GPL"
__version__ = "0.1.2"
__version__ = "0.2.0"
__status__ = "Development"

import requests
import re

NUM_FAN_TRAY = 5
NUM_ROTER = 2


class FanUtil():
"""Platform-specific FanUtil class"""

def __init__(self):
self.fan_info_url = "http://240.1.1.1:8080/api/fan/info"
self.all_fan_dict = None

self.fan_fru_url = "http://240.1.1.1:8080/api/sys/fruid/fan"
self.sensor_url = "http://240.1.1.1:8080/api/sys/sensors"
self.fru_data_list = None
self.sensor_data_list = None

def request_data(self):
# Reqest data from BMC if not exist.
if self.fru_data_list is None or self.sensor_data_list is None:
fru_data_req = requests.get(self.fan_fru_url)
sensor_data_req = requests.get(self.sensor_url)
fru_json = fru_data_req.json()
sensor_json = sensor_data_req.json()
self.fru_data_list = fru_json.get('Information')
self.sensor_data_list = sensor_json.get('Information')
return self.fru_data_list, self.sensor_data_list

def name_to_index(self, fan_name):
# Get fan index from fan name
match = re.match(r"(FAN)([0-9]+)-(1|2)", fan_name, re.I)
fan_index = None
if match:
i_list = list(match.groups())
fan_index = int(i_list[1])*2 - (int(i_list[2]) % 2)
return fan_index
def request_data(self, url):
try:
r = requests.get(url)
data = r.json()
except Exception as e:
return {}
return data

def get_num_fans(self):
"""
Get the number of fans
:return: int num_fans
"""
num_fans = 10
all_fan_dict = self.get_all()
num_fan_tray = all_fan_dict.get('Number', NUM_FAN_TRAY)

return num_fans
return num_fan_tray * NUM_ROTER

def get_fan_speed(self, fan_name):
"""
Get the current speed of the fan, the unit is "RPM"
:return: int fan_speed
"""

try:
# Get real fan index
index = self.name_to_index(fan_name)

# Set key and index.
fan_speed = 0
position_key = "Front" if index % 2 != 0 else "Rear"
index = int(round(float(index)/2))
fan_key = "Fan " + str(index) + " " + position_key
all_fan_dict = self.get_all()
fan_info = all_fan_dict.get(fan_name, {})

# Request and validate fan information.
self.fru_data_list, self.sensor_data_list = self.request_data()

# Get fan's speed.
for sensor_data in self.sensor_data_list:
sensor_name = sensor_data.get('name')
if "fan" in str(sensor_name):
fan_data = sensor_data.get(fan_key)
fan_sp_list = map(int, re.findall(r'\d+', fan_data))
fan_speed = fan_sp_list[0]

except:
return 0

return fan_speed
return fan_info.get('Speed', 0)

def get_fan_low_threshold(self, fan_name):
"""
Expand All @@ -88,31 +56,10 @@ def get_fan_low_threshold(self, fan_name):
:return: int fan_low_threshold
"""

try:
# Get real fan index
index = self.name_to_index(fan_name)

# Set key and index.
fan_low_threshold = 0
position_key = "Front" if index % 2 != 0 else "Rear"
index = int(round(float(index)/2))
fan_key = "Fan " + str(index) + " " + position_key

# Request and validate fan information.
self.fru_data_list, self.sensor_data_list = self.request_data()

# Get fan's threshold.
for sensor_data in self.sensor_data_list:
sensor_name = sensor_data.get('name')
if "fan" in str(sensor_name):
fan_data = sensor_data.get(fan_key)
fan_sp_list = map(int, re.findall(r'\d+', fan_data))
fan_low_threshold = fan_sp_list[1]

except:
return "N/A"
all_fan_dict = self.get_all()
fan_info = all_fan_dict.get(fan_name, {})

return fan_low_threshold
return fan_info.get('LowThd', 0)

def get_fan_high_threshold(self, fan_name):
"""
Expand All @@ -121,91 +68,31 @@ def get_fan_high_threshold(self, fan_name):
the status of the fan is not ok
:return: int fan_high_threshold
"""
all_fan_dict = self.get_all()
fan_info = all_fan_dict.get(fan_name, {})

try:
# Get real fan index
index = self.name_to_index(fan_name)

# Set key and index.
fan_high_threshold = 0
position_key = "Front" if index % 2 != 0 else "Rear"
index = int(round(float(index)/2))
fan_key = "Fan " + str(index) + " " + position_key

# Request and validate fan information.
self.fru_data_list, self.sensor_data_list = self.request_data()

# Get fan's threshold.
for sensor_data in self.sensor_data_list:
sensor_name = sensor_data.get('name')
if "fan" in str(sensor_name):
fan_data = sensor_data.get(fan_key)
fan_sp_list = map(int, re.findall(r'\d+', fan_data))
fan_high_threshold = fan_sp_list[2]

except:
return 0

return fan_high_threshold
return fan_info.get('HighThd', 0)

def get_fan_pn(self, fan_name):
"""
Get the product name of the fan
:return: str fan_pn
"""

try:
# Get real fan index
index = self.name_to_index(fan_name)

# Set key and index.
fan_pn = "N/A"
index = int(round(float(index)/2))
fan_fru_key = "Fantray" + str(index)
all_fan_dict = self.get_all()
fan_info = all_fan_dict.get(fan_name, {})

# Request and validate fan information.
self.fru_data_list, self.sensor_data_list = self.request_data()

# Get fan's fru.
for fan_fru in self.fru_data_list:
matching_fan = [s for s in fan_fru if fan_fru_key in s]
if matching_fan:
pn = [s for s in fan_fru if "Part" in s]
fan_pn = pn[0].split()[4]

except:
return "N/A"

return fan_pn
return fan_info.get('PN', 'N/A')

def get_fan_sn(self, fan_name):
"""
Get the serial number of the fan
:return: str fan_sn
"""
try:
# Get real fan index
index = self.name_to_index(fan_name)
all_fan_dict = self.get_all()
fan_info = all_fan_dict.get(fan_name, {})

# Set key and index.
fan_sn = "N/A"
index = int(round(float(index)/2))
fan_fru_key = "Fantray" + str(index)

# Request and validate fan information.
self.fru_data_list, self.sensor_data_list = self.request_data()

# Get fan's fru.
for fan_fru in self.fru_data_list:
matching_fan = [s for s in fan_fru if fan_fru_key in s]
if matching_fan:
serial = [s for s in fan_fru if "Serial" in s]
fan_sn = serial[0].split()[3]

except:
return "N/A"

return fan_sn
return fan_info.get('SN', 'N/A')

def get_fans_name_list(self):
"""
Expand Down Expand Up @@ -240,70 +127,40 @@ def get_all(self):
SN, conditional, if PRESENT is True, SN of the FAN, string)
"""

self.fru_data_list, self.sensor_data_list = self.request_data()
all_fan_dict = dict()

# Get the number of fans
n_fan = self.get_num_fans()
all_fan_dict["Number"] = n_fan

# Set fan FRU data.
fan_fru_dict = dict()
fan_raw_idx = 1
for fan_fru in self.fru_data_list:
fru_dict = dict()
fan_ps = False

if len(fan_fru) == 0:
fan_idx = fan_raw_idx
fan_pn = "N/A"
fan_sn = "N/A"
else:
fan_key = fan_fru[0].split()
if str(fan_key[-1]).lower() == "absent":
fan_idx = int(re.findall('\d+', fan_key[0])[0])

else:
fan_idx = int(re.findall('\d+', fan_key[-1])[0])
fan_ps = True
pn = [s for s in fan_fru if "Part" in s]
sn = [s for s in fan_fru if "Serial" in s]
fan_pn = pn[0].split(
":")[-1].strip() if len(pn) > 0 else 'N/A'
fan_sn = sn[0].split(
":")[-1].strip() if len(sn) > 0 else 'N/A'

fru_dict["PN"] = "N/A" if not fan_pn or fan_pn == "" else fan_pn
fru_dict["SN"] = "N/A" if not fan_sn or fan_sn == "" else fan_sn
fru_dict["Present"] = fan_ps
fan_fru_dict[fan_idx] = fru_dict
fan_raw_idx += 1

# Set fan sensor data.
for sensor_data in self.sensor_data_list:
sensor_name = sensor_data.get('name')
if "fan" in str(sensor_name):
for x in range(1, n_fan + 1):
fan_dict = dict()
f_index = int(round(float(x)/2))
pos = 1 if x % 2 else 2
position_key = "Front" if x % 2 != 0 else "Rear"
fan_key = "Fan " + str(f_index) + " " + position_key
fan_data = sensor_data.get(fan_key)
fan_sp_list = map(int, re.findall(r'\d+', fan_data))
fan_dict["Present"] = fan_fru_dict[f_index]["Present"]
if fan_dict["Present"] or fan_sp_list[0] > 0:
fan_dict["Present"] = True
fan_dict["Speed"] = fan_sp_list[0]
fan_dict["Running"] = True if fan_dict["Speed"] > 0 else False
fan_dict["LowThd"] = fan_sp_list[1]
fan_dict["HighThd"] = fan_sp_list[2]
fan_dict["PN"] = fan_fru_dict[f_index]["PN"]
fan_dict["SN"] = fan_fru_dict[f_index]["SN"]
fan_dict["AirFlow"] = "FTOB" if "R1240-G0009" in fan_dict["PN"] else "Unknown"
fan_dict["Status"] = True if fan_dict["AirFlow"] != "Unknown" else False
fan_name = 'FAN{}_{}'.format(f_index, pos)
all_fan_dict[fan_name] = fan_dict
break

return all_fan_dict
if not self.all_fan_dict:
all_fan_dict = dict()

fan_info_req = self.request_data(self.fan_info_url)
fan_info_data = fan_info_req.get('data', {})
all_fan_dict["Number"] = fan_info_data.get('Number', NUM_FAN_TRAY)

for fan_idx in range(1, all_fan_dict["Number"] + 1):
num_of_roter = fan_info_data.get('Rotors', NUM_ROTER)

for fan_pos in range(1, num_of_roter + 1):
fan_key = 'FAN{}'.format(str(fan_idx))
roter_key = 'Rotor{}'.format(str(fan_pos))

fan_info = fan_info_data.get(fan_key, {})
roter_info = fan_info.get(roter_key, {})

fan_info_dict = dict()
fan_info_dict["Present"] = True if fan_info.get(
"Present") == 'yes' else False
fan_info_dict["Speed"] = roter_info.get("Speed", "N/A")
fan_info_dict["Running"] = True if roter_info.get(
"Running") == 'yes' else False
fan_info_dict["HighThd"] = roter_info.get(
"SpeedMax", "N/A")
fan_info_dict["LowThd"] = roter_info.get("SpeedMin", "N/A")
fan_info_dict["Status"] = False if roter_info.get(
"HwAlarm") == 'yes' else True
fan_info_dict["PN"] = fan_info.get("PN", "N/A")
fan_info_dict["SN"] = fan_info.get("SN", "N/A")
fan_info_dict["AirFlow"] = fan_info.get("AirFlow", "N/A")

fan_name = '{}_{}'.format(fan_key, fan_pos)
all_fan_dict[fan_name] = fan_info_dict
self.all_fan_dict = all_fan_dict

return self.all_fan_dict
Loading