戴尔R740服务器获取cpu、内存、硬盘参数信息。
admin
2023-02-23 09:00:10
0

戴尔R740服务器获取cpu、内存、硬盘参数信息。使用redfish协议,只使用了system的一个总URL即可获取所有参数。


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

##使用一个system总的URL分别获取到cpu、内存、存储三个url.所以只修改system的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/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):  #定义总获取函数,传参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'])

        ###处理内存
        memory = temp_system_result1['Memory']['@odata.id']  ##获取内存的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'])

        ##处理存储
        storage = temp_system_result1['Storage']['@odata.id']  ##获取存储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')

相关内容

热门资讯

携程被罚没51.79亿元 7月25日,市场监管总局依法对携程集团有限公司(以下简称携程)滥用市场支配地位实施垄断行为作出行政处...
多地停运停航停课,台风“红霞”... 中央气象台7月25日06时继续发布台风橙色预警:今年第12号台风“红霞”已于今天(25日)早晨由强热...
“直-20家族”全亮相 近日,在陆军某旅直升机分队演训中,直-20与直-20T表现亮眼。075型两栖攻击舰日前也搭载直-20...
蒋万安借“725上凯道”成蓝营... 中国国民党25日将于台北凯道举办“我是人,我反毒台”集会,向民进党当局抗议中联毒油案。对此,资深媒体...
波轮式全自动洗衣机脱水时波轮转 波轮式全自动洗衣机脱水时波轮转1、衣物放置不平导致;2、排水系统发生阻塞。3、顶部的安全开关损坏;4...
洗衣机不出水怎么查原因 当洗衣机不出水时,一定是出了问题。下面就是一些可能的原因及其解决方法:1. 电源问题洗衣机不能正常工...
半自动洗衣机放不出水是什么原因... 1、检查过滤网是否堵塞。若堵塞了需要将脏东西取出来,然后再用清水将过滤网清洗干净。2、检查一下是不是...
全自动洗衣机不出水是什么原因 1、进水口的滤网被水中的杂质堵住。2、控制板上的进水继电器坏了。3、电路故障,进水电磁阀有故障或者插...
抽油烟机只通电不转怎么回事 只通电不转的故障原因和解决方法:1、电容量减小或损坏。解决方法:抽油烟机电机不转,还有嗡嗡声,一般是...
谷歌被罚9亿欧元后,特朗普称欧... 当地时间7月23日,欧盟委员会指控美国谷歌公司违反欧盟《数字市场法》,对其处以8.9亿欧元罚款。这一...