2019-11-06 20:56:17 -06:00
|
|
|
import os
|
2020-01-15 20:43:48 -06:00
|
|
|
import struct
|
2019-11-06 20:56:17 -06:00
|
|
|
import subprocess
|
2020-01-15 20:43:48 -06:00
|
|
|
from mmap import *
|
2019-11-06 20:56:17 -06:00
|
|
|
|
|
|
|
HOST_CHK_CMD = "docker > /dev/null 2>&1"
|
|
|
|
EMPTY_STRING = ""
|
|
|
|
|
|
|
|
|
|
|
|
class APIHelper():
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def is_host(self):
|
|
|
|
return os.system(HOST_CHK_CMD) == 0
|
|
|
|
|
2020-01-15 20:43:48 -06:00
|
|
|
def pci_get_value(self, resource, offset):
|
|
|
|
status = True
|
|
|
|
result = ""
|
|
|
|
try:
|
|
|
|
fd = os.open(resource, os.O_RDWR)
|
|
|
|
mm = mmap(fd, 0)
|
|
|
|
mm.seek(int(offset))
|
|
|
|
read_data_stream = mm.read(4)
|
|
|
|
result = struct.unpack('I', read_data_stream)
|
|
|
|
except:
|
|
|
|
status = False
|
|
|
|
return status, result
|
|
|
|
|
|
|
|
def run_command(self, cmd):
|
|
|
|
status = True
|
|
|
|
result = ""
|
|
|
|
try:
|
|
|
|
p = subprocess.Popen(
|
2020-11-25 12:28:36 -06:00
|
|
|
cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2020-01-15 20:43:48 -06:00
|
|
|
raw_data, err = p.communicate()
|
|
|
|
if err == '':
|
|
|
|
result = raw_data.strip()
|
|
|
|
except:
|
|
|
|
status = False
|
|
|
|
return status, result
|
|
|
|
|
|
|
|
def run_interactive_command(self, cmd):
|
|
|
|
try:
|
|
|
|
os.system(cmd)
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2019-11-06 20:56:17 -06:00
|
|
|
def read_txt_file(self, file_path):
|
|
|
|
try:
|
|
|
|
with open(file_path, 'r') as fd:
|
|
|
|
data = fd.read()
|
|
|
|
return data.strip()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
|
|
|
def ipmi_raw(self, netfn, cmd):
|
|
|
|
status = True
|
|
|
|
result = ""
|
|
|
|
try:
|
|
|
|
cmd = "ipmitool raw {} {}".format(str(netfn), str(cmd))
|
|
|
|
p = subprocess.Popen(
|
2020-11-25 12:28:36 -06:00
|
|
|
cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2019-11-06 20:56:17 -06:00
|
|
|
raw_data, err = p.communicate()
|
|
|
|
if err == '':
|
|
|
|
result = raw_data.strip()
|
2020-01-15 20:43:48 -06:00
|
|
|
else:
|
|
|
|
status = False
|
2019-11-06 20:56:17 -06:00
|
|
|
except:
|
|
|
|
status = False
|
|
|
|
return status, result
|
|
|
|
|
|
|
|
def ipmi_fru_id(self, id, key=None):
|
|
|
|
status = True
|
|
|
|
result = ""
|
|
|
|
try:
|
|
|
|
cmd = "ipmitool fru print {}".format(str(
|
|
|
|
id)) if not key else "ipmitool fru print {0} | grep '{1}' ".format(str(id), str(key))
|
|
|
|
|
|
|
|
p = subprocess.Popen(
|
2020-11-25 12:28:36 -06:00
|
|
|
cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2019-11-06 20:56:17 -06:00
|
|
|
raw_data, err = p.communicate()
|
|
|
|
if err == '':
|
|
|
|
result = raw_data.strip()
|
2020-01-15 20:43:48 -06:00
|
|
|
else:
|
|
|
|
status = False
|
|
|
|
except:
|
|
|
|
status = False
|
|
|
|
return status, result
|
|
|
|
|
|
|
|
def ipmi_set_ss_thres(self, id, threshold_key, value):
|
|
|
|
status = True
|
|
|
|
result = ""
|
|
|
|
try:
|
|
|
|
cmd = "ipmitool sensor thresh '{}' {} {}".format(str(id), str(threshold_key), str(value))
|
|
|
|
p = subprocess.Popen(
|
2020-11-25 12:28:36 -06:00
|
|
|
cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2020-01-15 20:43:48 -06:00
|
|
|
raw_data, err = p.communicate()
|
|
|
|
if err == '':
|
|
|
|
result = raw_data.strip()
|
|
|
|
else:
|
|
|
|
status = False
|
2019-11-06 20:56:17 -06:00
|
|
|
except:
|
|
|
|
status = False
|
|
|
|
return status, result
|