溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

華為2288HV5獲取cpu、內(nèi)存、存儲(chǔ)等參數(shù)信息

發(fā)布時(shí)間:2020-06-15 08:14:01 來(lái)源:網(wǎng)絡(luò) 閱讀:1160 作者:chier11 欄目:系統(tǒng)運(yùn)維

華為2288HV5獲取cpu、內(nèi)存、存儲(chǔ)等參數(shù)信息,采用redfish協(xié)議。華為的技術(shù)支撐團(tuán)隊(duì)很給力,獲取資料很全面,講解也很到位。所以第一個(gè)redfish案例就是用華為。

import requests
import json
requests.packages.urllib3.disable_warnings()

###原理:cpu、內(nèi)存、存儲(chǔ)分別使用不同的url獲取到值,如url不一致只分別修改對(duì)應(yīng)的第一個(gè)url即可,后面的詳細(xì)參數(shù)的URL函數(shù)自動(dòng)提取
##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/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):  #定義總獲取函數(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 selectlog(self):   ##查詢SEL日志,13版pdf815頁(yè)
        ##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日志的起始條數(shù),必選項(xiàng)。從1開(kāi)始,且不大于日志總數(shù)
            # "EntriesCount": EntriesCount_value,  ##日志條數(shù),大于0的整數(shù)
            # "SelLevel": SelLevelvalue,  ##日志級(jí)別,非必選,四類:Informational、Minor、Major、Critical
            # "SelObjectType": SelObjectTypevalue,  ##主題類型,非必選
            # "SelBeginTime": SelBeginTime_value,  ##起始時(shí)間,非必選,支持的日期格式:yyyy-MM-dd HH:mm:ss
            # "SelEndTime": SelEndTimevalue,  ##結(jié)束時(shí)間,非必選,支持的日期格式:yyyy-MM-dd HH:mm:ss
            # "SelSearchString": SelSearchStringvalue  ##關(guān)鍵字,非必選
            ###################ssh iBMC#####################
            #iBMC:/->ipmcget -d sel -v info          ##顯示日志信息
            # SEL Information
            # Version               :  1.0.0
            # Current Event Number  :  97            ##97個(gè)條目,redfish協(xié)議查不出多少條目
            # Max Event Number      :  4000
            #iBMC:/->ipmcget -d sel -v list          ##顯示所有條目,數(shù)據(jù)庫(kù)格式
            #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)
        ##獲取多個(gè)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'])

    ####處理內(nèi)存
    # 取出內(nèi)存?zhèn)€數(shù),字典鍵'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'])
    ####處理存儲(chǔ)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#$')    ##華為,兩個(gè)raid組。
    #Collect_Info('10.249.160.2',  'root', 'Huawei12#$')     ##華為,單個(gè)raid組。
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI