CMDB数据收集-更新中

一、机器整体信息收集

主要用到的命令:

ipmitool lan print       #获取ipmi地址

hostname                   #获取主机名

dmidecode -t system #获取设备型号和sn等信息

import subprocess
hostname = subprocess.Popen("hostname", stdout=subprocess.PIPE, shell=True).stdout.read().decode().split('\n')[0]
IPMI = subprocess.Popen("ipmitool lan print | grep 'IP Address '| grep -v Source | awk -F ':' '{print $2}'", stdout=subprocess.PIPE, shell=True).stdout.read().decode().split('\n')[0]
filter_keys = ['Manufacturer','Serial Number', 'Product Name', 'UUID', 'Wake-up Type']
for key in filter_keys:
    try:
        res = subprocess.Popen("dmidecode -t system | grep '%s'" % key, stdout=subprocess.PIPE, shell=True)
        result = res.stdout.read().decode
        data_list = result.split(':')
        if len(data_list) > 1:
            raw_data[key]  = data_list[1].strip()
        else:
            raw_data[key] = ''
    except Exception as e:
        print(e)
        raw_data[key] = ''
data = dict()
data['Manufacturer'] = raw_data['Manufacturer']
data['sn'] = raw_data['Serial Number']
data['model'] = raw_data['Product Name']
data['uuid'] = raw_data['UUID']
data['wale_up_type'] = raw_data['Wake-up Type']
data['hostname'] = hostname
data['IPMI'] = IPMI

二、操作系统信息收集

主要用到的命令:
lsb_release -a 
def get_os_info():
    destributor = subprocess.Popen("lsb_release -a | grep 'Distributor ID'", stdout=subprocess.PIPE, shell=True)
    destributor = destributor.stdout.read().decode().split(":")
 
 
    releas = subprocess.Popen("lsb_release -a | grep 'Description'", stdout=subprocess.PIPE, shell=True)
    releas.releas.stdout.read().decode().split(":")
 
 
    data_dice = {
        "os_destribution": destributor[1].strip() if len(destributor) > 1 else "",
        "os_release": release[1].strip() if len(release) > 1 else "",
        "os_type" : "linux"
    }
    return data_dict

 

三、cpu信息收集

实现方法:

cat /proc/cpuinfo

def get_cpu_info():
    raw_cmd = 'cat /proc/cpuinfo'
 
    raw_data = {
        'cpu_model': "%s |grep 'model name' |head -1 " % raw_cmd,
        'cpu_count':  "%s |grep 'processor'|wc -l " % raw_cmd,
        'cpu_core_count': "%s |grep 'cpu cores' |awk -F: '{SUM +=$2} END {print SUM}'" % raw_cmd,
    }
 
    for key, cmd in raw_data.items():
        try:
            result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
            raw_data[key] = result.stdout.read().decode().strip()
        except ValueError as e:
            print(e)
            raw_data[key] = ""
 
    data = {
        "cpu_count": raw_data["cpu_count"],
        "cpu_core_count": raw_data["cpu_core_count"]
        }
 
    cpu_model = raw_data["cpu_model"].split(":")
 
    if len(cpu_model) > 1:
        data["cpu_model"] = cpu_model[1].strip()
    else:
        data["cpu_model"] = ''
 
    return data

 

四、内存信息收集

实现方法:

dmidecode -t memory

def get_ram_info():
    raw_data = subprocess.Popen("dmidecode -t memory", stdout=subprocess.PIPE, shell=True)
    raw_list = raw_data.stdout.read().decode().split("\n")
    raw_ram_list = []
    item_list = []
    for line in raw_list:
        if line.startswith("Memory Device"):
            raw_ram_list.append(item_list)
            item_list = []
        else:
            item_list.append(line.strip())
 
    ram_list = []
    for item in raw_ram_list:
        item_ram_size = 0
        ram_item_to_dic = {}
        for i in item:
            data = i.split(":")
            if len(data) == 2:
                key, v = data
                if key == 'Size':
                    if v.strip() != "No Module Installed":
                        ram_item_to_dic['capacity'] = v.split()[0].strip()
                        item_ram_size = round(v.split()[0])
                    else:
                        ram_item_to_dic['capacity'] = 0
 
                if key == 'Type':
                    ram_item_to_dic['model'] = v.strip()
                if key == 'Manufacturer':
                    ram_item_to_dic['manufacturer'] = v.strip()
                if key == 'Serial Number':
                    ram_item_to_dic['sn'] = v.strip()
                if key == 'Asset Tag':
                    ram_item_to_dic['asset_tag'] = v.strip()
                if key == 'Locator':
                    ram_item_to_dic['slot'] = v.strip()
 
        if item_ram_size == 0:
            pass
        else:
            ram_list.append(ram_item_to_dic)
 
    raw_total_size = subprocess.Popen("cat /proc/meminfo|grep MemTotal ", stdout=subprocess.PIPE, shell=True)
    raw_total_size = raw_total_size.stdout.read().decode().split(":")
    p = {}
    ram_sizelist = []
    for i in ram_list:
        info = i['capacity']+'@'+i['speed']
        ram_sizelist.append(info)
    for i in ram_sizelist:
        if ram_sizelist.count(i)>0:
            p[i] = ram_sizelist.count(i)
    fres = ''
    for (k , v) in p.items():
        fres += '%s*%d' % (str(k).replace('b','').replace(' ','').strip('\''),v)+'+'
        res=fres[:-1]
    ram_data = {'ram': ram_list}
    ram_data['ram_num'] = str(len(ram_list))
    ram_data['ram_info'] = res
    if len(raw_total_size) == 2:
        total_gb_size = int(raw_total_size[1].split()[0]) / 1024**2
        ram_data['ram_size'] = total_gb_size
 
    return ram_data

 

五、网卡信息收集

实现方法:

由于ifconfig在centos6和centos7上的返回结果不同,所以使用ip a的方式获取信息 

def get_nic_info():
    raw_data =  subprocess.Popen("ip a | grep -E 'mtu|inet|ether' | grep -v inet6", stdout=subprocess.PIPE, shell=True)
    raw_list = raw_data.stdout.read().decode().split("\n")
    raw_ram_list = []
    item_list = {}
    for line in raw_list:
        r = '^[0-9]'
        res = re.search(r, line)
        if not res:
            if str("ether") in line:
                mac = line.split("ether")[1].split()[0]
                item_list['mac'] = mac
            if str("inet") in line:
                ip = line.split("inet")[1].split()[0]
                ip_addr = ip.split("/")[0]
                netmask = ip.split("/")[1]
                item_list['ip_address'] = ip_addr
                item_list['net_mask'] = exchange_maskint(int(netmask))
        else:
            item_list={}
            nic_name = line.split(":")[1].strip()
            item_list['name'] = nic_name
            item_list['module'] = 'unknow'
            item_list['mac'] = 'None'
            raw_ram_list.append(item_list)
     
    return {'nic':raw_ram_list}
 
 
def exchange_maskint(mask_int):
    bin_arr = ['0' for i in range(32)]
    for i in range(mask_int):
        bin_arr[i] = '1'
    tmpmask = [''.join(bin_arr[i * 8;i * 8 + 8]) for i in range(4)]
    tmpmask = [str(int(tmpstr, 2)) for tmpstr in tmpmask]
    return '.'.join(tmpmask)

 

六、磁盘信息收集

这里用到python中的megacli模块

    pip install megacli 

from megacli import MegaCLI, MegaCLIError
 
 
def get_disk_info():
    cli=MegaCLI()
    disks = cli.physicaldrives()
    result = {'physicak_disk_driver':[]}
    for disk in disks:
        sn = disk['inquiry_data'].split()
        if 'seagate' in sn:
            manufacturer = 'seagate'
            pn = sn[1]
            sn = sn[2]
        elf 'intel' in sn:
            manufacturer = 'intel'
            pn = sn[2]
            sn = sn[0]
        elif 'mfaoaa70' in sn:
            manufacturer = 'hgst'
            pn = sn[2]
            sn = sn[0]
        elif 'sn05' in sn:
            manufacturer = 'sn05'
            if str('-') in sn[0]
                pn = sn[0].split('-')[1]   
                sn = sn[0].split('-')[0]
            else:
                pn = 'None'
                sn = sn[0]
        elif 'sn04' in sn:
            manufacturer = 'sn04'
            if str('-') in sn[0]
                pn = sn[0].split('-')[1]   
                sn = sn[0].split('-')[0]
            else:
                pn = 'None'
                sn = sn[0]
        elif 'sn02' in sn:
            manufacturer = 'sn02'
            if str('-') in sn[0]
                pn = sn[0].split('-')[1]   
                sn = sn[0].split('-')[0]
            else:
                pn = 'None'
                sn = sn[0]
        elif 'hgst' in sn:
            manufacturer = 'hgst'
            pn = sn[1]
            sn = sn[2]
        elif 'hitachi' in sn:
            manufacturer = 'hitachi'
            pn = sn[1]
            sn = sn[2]
        elif 'wd' in sn:
            manufacturer = 'wd'
            pn = sn[1]
            sn = sn[2]
        elif 'toshiba' in sn:
            manufacturer = 'toshiba'
            pn = sn[1]
            sn = sn[2]
        else:
            manufacturer = 'None'
            pn = "None"
            sn = "None"
         
        disk_dict = {}
        disk_dict['model'] = pn
        disk_dict['sn'] = sn
        disk_dict['slot'] = str(disk['slot_number'])
        disk_dict['size'] = str(round(float(disk['raw_size'])/1024/1024/1024))
        disk_dict['manufacturer'] = manufacturer
        disk_dict['pd_type'] = disk['pd_type']
        if 'dirve_postion' in disk or 'drive_position' in disk:
            try:
                disk_dict['raid_group'] = disk['dirve_postion'].split()[0].split(",")[0]
            except Exception as e:
                disk_dict['raid_group'] = disk['dirve_position'].split()[0].split(",")[0]
            result['physical_disk_driver'].append(disk_dict)
         
    if result['physical_disk_driver'] == []:
        raw_data = subprocess.Popen("lshw -c disk -json -quiet | grep -v ^$" , stdout=subprocess.PIPE, shell=True)
        raw_str_old = str("[")+raw_data.stdout.read().decode().replace('\n','').strip()+str("]")
        raw_str=re.sub('} +{','},{',raw_str_old)
        raw_list=json.loads(raw_str)
        for line in raw_list:
            item_list = {}
            if str("product") in line.keys():
                item_list['model'] = line["product"]   
            elif str("serial") in line.keys():
                item_list['sn'] = line["serial"]
            elif str("handle") in line.keys():
                item_list['sn'] = line["handle"]
            if str("size") in line.keys():
                item_list['size'] = str(int(line["size"]/1024/1024/1024))+str("GB")
            if str("description") in line.keys():
                item_list['py_type'] = line["description"]
            if str("size") in line.keys():
                result['physical_disk_driver'].append(item_list)
 
 
    p = {}
    disk_sizelist = []
    for i in result['physical_disk_driver']:
        if 'py_type' in i.keys():
            info = i['py_type']+'@'+i['size']
        else:
            info = 'unknow@'i['size']
        disk_sizelist.append(info)
    for i in disk_sizelist:
        if disk_sizelist.count(i)>0:
            p[i] = disk_sizelist.count(i)
    fres=''
    for (k,v) in p.items():
        fres += '%s*%d' % (str(k),strip('\''),v)+'+'
        res=fres[:-1]
     
    result[diskinfo]=res
    return result

 

七、显卡信息收集(nvidia)

八、集群信息收集

posted @ 2020-01-06 16:20  邱石  阅读(267)  评论(0)    收藏  举报