戴爾R740服務(wù)器獲取cpu、內(nèi)存、硬盤參數(shù)信息。使用redfish協(xié)議,只使用了system的一個(gè)總URL即可獲取所有參數(shù)。
import requests
import json
requests.packages.urllib3.disable_warnings()
##使用一個(gè)system總的URL分別獲取到cpu、內(nèi)存、存儲(chǔ)三個(gè)url.所以只修改system的URL即可
##sel日志單獨(dú)使用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 ##同時(shí)存在4-5個(gè)token鏈接,每個(gè)token鏈接時(shí)間為5分鐘,可以自己設(shè)置。
token=0
tokenurl=self.URLprefix+'/redfish/v1/Sessions' ##dell獲取token的ID
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)
#re1=requests.post(tokenurl,auth=(self.username,self.password),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): #定義總獲取函數(shù),傳參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 Collect_Info(ipaddr,username,password):
dell740=GetHostInfo(ipaddr,username,password)
####total_system_URL收集/redfish/v1/Systems/System.Embedded.1
select_system_total = '/redfish/v1/Systems/System.Embedded.1'
#print('cpu_total', hw2288HV5.GetInfo(select_cpu_total))
temp_system_result1= dell740.GetInfo(select_system_total)
if isinstance(temp_system_result1,dict) and ('error' not in temp_system_result1.keys() ):
##處理cpu
cpu = temp_system_result1['Processors']['@odata.id'] ##獲取CPU的URL
#print ('Processors',dell740.GetInfo(cpu))
cpu_result1 = dell740.GetInfo(cpu)
cpu_count = cpu_result1['Members@odata.count']
cpu_URLsuffix_list = [x['@odata.id'] for x in cpu_result1['Members']]
print('CPU count:', cpu_count)
for single_cpuurl in cpu_URLsuffix_list:
singlecpu_result2= dell740.GetInfo(single_cpuurl)
if isinstance(singlecpu_result2, dict) and ('error' not in singlecpu_result2.keys()):
#print ('singlecpu_result2',singlecpu_result2)
print('CPU single name:', singlecpu_result2['Name'])
print('CPU single ID:', singlecpu_result2['Id'])
print('CPU single TotalCores(cpus):', singlecpu_result2['TotalCores'])
print('CPU single Model(cpus):', singlecpu_result2['Model'])
###處理內(nèi)存
memory = temp_system_result1['Memory']['@odata.id'] ##獲取內(nèi)存的URL
memory_result1 = dell740.GetInfo(memory)
memory_count = memory_result1['Members@odata.count']
memory_URLsuffix_list = [x['@odata.id'] for x in memory_result1['Members']]
print ('Memory count:',memory_count)
for single_memoryurl in memory_URLsuffix_list:
singlememory_result2 = dell740.GetInfo(single_memoryurl)
if isinstance(singlememory_result2, dict) and ('error' not in singlememory_result2.keys()):
#print('singlecpu_result2', singlememory_result2)
print('Memory name:', singlememory_result2['Name'])
print('Memory ID:', singlememory_result2['Id'])
print('Memory Size:', singlememory_result2['CapacityMiB'])
print('Memory Type:', singlememory_result2['MemoryDeviceType'])
##處理存儲(chǔ)
storage = temp_system_result1['Storage']['@odata.id'] ##獲取存儲(chǔ)URL
#print ('storage',dell740.GetInfo(storage))
storage_result1 = dell740.GetInfo(storage)
storage_URLsuffix_list = [x['@odata.id'] for x in storage_result1['Members']]
for single_storageurl in storage_URLsuffix_list:
singlestorage_result2 = dell740.GetInfo(single_storageurl)
if isinstance(singlestorage_result2, dict) and ('error' not in singlestorage_result2.keys()):
#print('singlecpu_result2', singlestorage_result2)
disk_count=singlestorage_result2['Drives@odata.count']
print('disk count:',disk_count)
print('storage name:',singlestorage_result2['Id'])
if disk_count >0: ##有的URL中disk為0,不需要去獲取值
single_disk_URLsuffix_list = [x['@odata.id'] for x in singlestorage_result2['Drives']]
for disk_single in single_disk_URLsuffix_list:
single_disk_result1 = dell740.GetInfo(disk_single)
if isinstance(single_disk_result1, dict) and ('error' not in single_disk_result1.keys()):
#print ('single_disk_result1',single_disk_result1)
print('disk name:', single_disk_result1['Name'])
print('disk ID:', single_disk_result1['Id'])
print('disk CapacityBytes:', single_disk_result1['CapacityBytes'])
print('disk MediaType:', single_disk_result1['MediaType'])
else:
pass
##獲取sel日志
logurlsuffix = '/redfish/v1/Managers/iDRAC.Embedded.1/Logs/Sel' ##日志sel
sellog=dell740.GetInfo(logurlsuffix)
if isinstance(sellog,dict) and ('error' not in sellog.keys() ):
print('SEL log:',sellog)
if __name__ == '__main__':
Collect_Info('10.252.209.7', 'username', 'password')
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享標(biāo)題:戴爾R740服務(wù)器獲取cpu、內(nèi)存、硬盤參數(shù)信息。-創(chuàng)新互聯(lián)
當(dāng)前URL:http://www.rwnh.cn/article10/csjjdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、外貿(mào)建站、用戶體驗(yàn)、網(wǎng)站導(dǎo)航、網(wǎng)站內(nèi)鏈、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)