您好,登錄后才能下訂單哦!
華為2288HV5獲取cpu、內存、存儲等參數信息,采用redfish協議。華為的技術支撐團隊很給力,獲取資料很全面,講解也很到位。所以第一個redfish案例就是用華為。
import requests
import json
requests.packages.urllib3.disable_warnings()
###原理:cpu、內存、存儲分別使用不同的url獲取到值,如url不一致只分別修改對應的第一個url即可,后面的詳細參數的URL函數自動提取
##sel日志單獨使用URL獲取
class GetHostInfo(object):
def __init__(self,ipaddr,username,password):
self.URLprefix='https://'+ipaddr.strip()
self.username=username.strip()
self.password=password.strip()
global token ##同時存在4-5個token鏈接,每個token鏈接時間為5分鐘,可以自己設置。
token=0
tokenurl=self.URLprefix+'/redfish/v1/SessionService/Sessions'
print(tokenurl)
data={
"UserName":self.username,
"Password":self.password
}
header={
"Content-Type":"application/json"
}
re1=requests.post(tokenurl,json.dumps(data),headers=header,verify=False)
#print (re1.status_code)
if re1.status_code == 201:
#print (re1.json())
#print (re1.headers)
print (re1.headers['X-Auth-Token'])
token=re1.headers['X-Auth-Token']
else:
pass
def GetInfo(self,URL_suffix): #定義總獲取函數,傳參url的后半部分。如'/redfish/v1/Systems/1/Memory'
urlset=self.URLprefix+URL_suffix
if token !=0:
header = {
"Content-Type":"application/json",
"X-Auth-Token":token
}
re1=requests.get(urlset,headers=header,verify=False)
#print(re1.status_code)
return (re1.json())
else:
pass
def selectlog(self): ##查詢SEL日志,13版pdf815頁
##post https://device_ip/redfish/v1/Systems/system_id/LogServices/LogService_id/Actions/Oem/Huawei/LogService.QuerySelLogEntries
url_selcetSELlog = self.URLprefix+'/redfish/v1/Systems/1/LogServices/Log1/Actions/Oem/Huawei/LogService.QuerySelLogEntries' ##LogService_id==Log1
if token != 0:
header = {
"Content-Type": "application/json",
"X-Auth-Token": token
}
# "StartEntryId": StartEntryId_value, ##查詢sel日志的起始條數,必選項。從1開始,且不大于日志總數
# "EntriesCount": EntriesCount_value, ##日志條數,大于0的整數
# "SelLevel": SelLevelvalue, ##日志級別,非必選,四類:Informational、Minor、Major、Critical
# "SelObjectType": SelObjectTypevalue, ##主題類型,非必選
# "SelBeginTime": SelBeginTime_value, ##起始時間,非必選,支持的日期格式:yyyy-MM-dd HH:mm:ss
# "SelEndTime": SelEndTimevalue, ##結束時間,非必選,支持的日期格式:yyyy-MM-dd HH:mm:ss
# "SelSearchString": SelSearchStringvalue ##關鍵字,非必選
###################ssh iBMC#####################
#iBMC:/->ipmcget -d sel -v info ##顯示日志信息
# SEL Information
# Version : 1.0.0
# Current Event Number : 97 ##97個條目,redfish協議查不出多少條目
# Max Event Number : 4000
#iBMC:/->ipmcget -d sel -v list ##顯示所有條目,數據庫格式
#iBMC:/->ipmcget -d sel -v suggestion 1 ##1為event number.
# iBMC:/->ipmcset -d sel -v clear ##清空歷史記錄
# WARNING: The operation may have many adverse effects.
# Do you want to continue?[Y/N]:Y
# Clear SEL records successfully.
# iBMC:/->
data={
"StartEntryId":1,
"EntriesCount":10
}
re1 = requests.post(url_selcetSELlog ,json.dumps(data), headers=header, verify=False)
print('SelectLog_StatusCode', re1.status_code)
return re1.json()
def Collect_Info(ipaddr,username,password):
hw2288HV5=GetHostInfo(ipaddr,username,password)
####處理CPU
select_cpu_total = '/redfish/v1/Systems/1/Processors'
#print('cpu_total', hw2288HV5.GetInfo(select_cpu_total))
temp_cpu_result1= hw2288HV5.GetInfo(select_cpu_total)
if isinstance(temp_cpu_result1,dict) and ('error' not in temp_cpu_result1.keys() ):
cpu_count = temp_cpu_result1['Members@odata.count']
print('@' * 50)
print('CPU Count:', cpu_count)
##獲取多個cpu的URLsuffix值
#print('select_cpu_single_URLsuffix:',[ x['@odata.id'] for x in temp_cpu_result1['Members']])
cpu_single_URLsuffix_list=[ x['@odata.id'] for x in temp_cpu_result1['Members']]
for cpu_single in cpu_single_URLsuffix_list:
temp_cpu_single_result1 = hw2288HV5.GetInfo(cpu_single)
#print(temp_cpu_single_result1)
if isinstance(temp_cpu_single_result1, dict) and ('error' not in temp_cpu_single_result1.keys()):
print('CPU single name:',temp_cpu_single_result1['Name'])
print('CPU single ID:',temp_cpu_single_result1['Id'])
print('CPU single TotalCores(cpus):', temp_cpu_single_result1['TotalCores'])
print('CPU single Model(cpus):', temp_cpu_single_result1['Model'])
####處理內存
# 取出內存個數,字典鍵'Members@odata.count'
select_memory_total = '/redfish/v1/Systems/1/Memory'
temp_memory_result1=hw2288HV5.GetInfo(select_memory_total)
if isinstance(temp_memory_result1,dict) and ('error' not in temp_memory_result1.keys() ):
Mem_count = temp_memory_result1['Members@odata.count']
print('@' * 50)
print('Memory Count:', Mem_count)
#獲取Memory的URLsuffix.
#print('select_memory_single_URLsuffix:', [x['@odata.id'] for x in temp_memory_result1['Members']])
Mem_single_URLsuffix_list = [x['@odata.id'] for x in temp_memory_result1['Members']]
#print (Mem_single_URLsuffix_list)
# 取出類型和大小
for select_memory_single in Mem_single_URLsuffix_list:
temp_memory_result2=hw2288HV5.GetInfo(select_memory_single)
#print (temp_memory_result2)
if isinstance(temp_memory_result2,dict) and ('error' not in temp_memory_result2.keys() ):
# return temp_memory_result2['CapacityMiB']/1024 ##M/1024=G
print('Memory name:',temp_memory_result2['Name'])
print('Memory ID:',temp_memory_result2['Id'])
print('Memory Size:', temp_memory_result2['CapacityMiB'])
print('Memory Type:', temp_memory_result2['MemoryDeviceType'])
####處理存儲storages
select_storages_total='/redfish/v1/Systems/1/Storages'
temp_storages_result1 = hw2288HV5.GetInfo(select_storages_total)
#print (temp_storages_result1)
if isinstance(temp_storages_result1, dict) and ('error' not in temp_storages_result1.keys()):
storages_raid_count = temp_storages_result1['Members@odata.count']
print('@' * 50)
print('Storages Raidzone Count:', storages_raid_count) #'StorageControllers@odata.count'
# 獲取storages raidzonename的URLsuffix.
#print('select_storages_raidzone_name_URLsuffix:', [x['@odata.id'] for x in temp_storages_result1['Members']])
storages_single_URLsuffix_list = [x['@odata.id'] for x in temp_storages_result1['Members']]
#print(storages_single_URLsuffix_list) #raidzone的URLsuffix
# 取出raidzone中磁盤的類型和大小
for select_storages_raidzone_single in storages_single_URLsuffix_list:
temp_storages_raiddisk_result2 = hw2288HV5.GetInfo(select_storages_raidzone_single)
#print (temp_storages_raiddisk_result2)
print ('@'*60)
print ('raidzone name:',temp_storages_raiddisk_result2['Name'])
print ('raidzone ID:',temp_storages_raiddisk_result2['Id'])
print ('raidzone disk count:',temp_storages_raiddisk_result2['Drives@odata.count'])
if isinstance(temp_storages_raiddisk_result2, dict) and ('error' not in temp_storages_raiddisk_result2.keys()):
storages_singledisk_URLsuffix_list = [x['@odata.id'] for x in temp_storages_raiddisk_result2['Drives']]
#print ('storages_singledisk-URLsuffix',storages_singledisk_URLsuffix_list)
for storages_singledisk in storages_singledisk_URLsuffix_list:
temp_storages_singledisk_result2 = hw2288HV5.GetInfo(storages_singledisk)
#print('temp_storages_singledisk_result2 :',temp_storages_singledisk_result2)
if isinstance(temp_storages_singledisk_result2, dict) and ('error' not in temp_storages_singledisk_result2.keys()):
print ('disk name:',temp_storages_singledisk_result2['Name'])
print ('disk ID:',temp_storages_singledisk_result2['Id'])
print ('disk CapacityBytes:',temp_storages_singledisk_result2['CapacityBytes'])
print ('disk MediaType:',temp_storages_singledisk_result2['MediaType'])
##SEL日志處理
print('log_SEL', hw2288HV5.selectlog()) ##查詢出sel_log的一定范圍的
if __name__ == '__main__':
Collect_Info('10.251.100.213', 'root', 'Huawei12#$') ##華為,兩個raid組。
#Collect_Info('10.249.160.2', 'root', 'Huawei12#$') ##華為,單個raid組。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。