CMDB-자산 수집

8800 단어

자산 수집


서버 정보:

  • 서버 제조업체
  • dmidecode -t system|grep 'Manufacturer'
    
  • 시스템 시퀀스 번호
  • dmidecode -t system|grep 'Serial Number'
    
  • 서버 모델
  • dmidecode -t system|grep 'Product Name'
    
  • 서버 UUID
  • dmidecode -t system|grep 'UUID'
    

    운영 체제 정보:

  • 시스템 제조업체
  • lsb_release -a|grep 'Distributor ID'
    
  • 시스템 버전 번호
  • lsb_release -a|grep 'release'
    

    CPU 정보:

  • CPU 모델
  • cat /proc/cpuinfo|grep 'model name ' |head -1
    
  • 논리 CPU 개수
  • cat /proc/cpuinfo|grep  'processor'|wc -l 
    
  • 물리적 CPU 개수
  • cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
    
  • 물리적 CPU당 코어의 개수(즉 코어)
  • cat /proc/cpuinfo| grep "cpu cores"| uniq
    

    메모리 정보:

  • 총 메모리
  • cat /proc/meminfo|grep MemTotal|awk '{sum=$2/1024/1024}END{print sum"GB"}'
    
  • 캐시
  • cat /proc/meminfo |grep 'SwapTotal'|awk '{sum=$2/1024/1024}END{print sum"GB"}'
    

    네트워크 카드 정보

     ifconfig
    

    하드 드라이브 정보:

    fdisk -l|grep Disk|grep bytes|awk '{print $2,$3"GB"}'
    

    자산 수집 스크립트(첫 번째 버전)
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import re
    import subprocess
    
    class Collect(object):
    
        @staticmethod
        def get_device_info():
            # 1. 
            manufacturer = subprocess.Popen("dmidecode -t system | grep 'Manufacturer'", stdout=subprocess.PIPE, shell=True)
            # 2. 
            sn = subprocess.Popen("dmidecode -t system | grep 'Serial Number'", stdout=subprocess.PIPE, shell=True)
            # 3. 
            product_name = subprocess.Popen("dmidecode -t system | grep 'Product Name'", stdout=subprocess.PIPE, shell=True)
            # 4. UUID
            uuid = subprocess.Popen("dmidecode -t system | grep 'UUID'", stdout=subprocess.PIPE, shell=True)
            manufacturer = manufacturer.stdout.read().decode().split(':')[1].strip()
            sn = sn.stdout.read().decode().split(':')[1].strip()
            product_name = product_name.stdout.read().decode().split(':')[1].strip()
            uuid = uuid.stdout.read().decode().split(':')[1].strip()
            return {
                "manufacturer": manufacturer,
                "product_name": product_name,
                "sn": sn,
                "uuid": uuid,
            }
    
        @staticmethod
        def get_system_info():
            #  
            distributor = subprocess.Popen("lsb_release -a | grep 'Distributor ID'", stdout=subprocess.PIPE, shell=True)
            #  
            release = subprocess.Popen("lsb_release -a | grep 'Release'", stdout=subprocess.PIPE, shell=True)
            distributor = distributor.stdout.read().decode().split(':')[1].strip()
            release = release.stdout.read().decode().split(':')[1].strip()
            return {
                "distributor": distributor,
                "release": release
            }
    
        @staticmethod
        def get_cpu_info():
            '''
            1. CPU 
            cat /proc/cpuinfo|grep 'model name ' |head -1
            2.  CPU 
            cat /proc/cpuinfo|grep  'processor'|wc -l
            3.  CPU 
            cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
            4.  CPU core ( )
            cat /proc/cpuinfo| grep "cpu cores"| uniq
            :return:
            '''
            #  1. CPU 
            model_name = subprocess.Popen("cat /proc/cpuinfo|grep 'model name'|head -1", stdout=subprocess.PIPE,
                                          shell=True)
            # 2.  CPU 
            processor_num = subprocess.Popen("cat /proc/cpuinfo|grep 'processor'|wc -l", stdout=subprocess.PIPE,
                                             shell=True)
            # 3.  CPU 
            physical_num = subprocess.Popen("cat /proc/cpuinfo| grep 'physical id'| sort| uniq| wc -l",
                                            stdout=subprocess.PIPE,
                                            shell=True)
            # 4.  CPU core ( )
            core_num = subprocess.Popen("cat /proc/cpuinfo| grep 'cpu cores'|uniq", stdout=subprocess.PIPE,
                                        shell=True)
            model_name = model_name.stdout.read().decode().split(':')[1].strip()
            processor_num = processor_num.stdout.read().decode().strip()
            physical_num = physical_num.stdout.read().decode().strip()
            core_num = core_num.stdout.read().decode().split(':')[1].strip()
            return {
                "model_name": model_name,
                "processor_num": processor_num,
                "physical_num": physical_num,
                "core_num": core_num
            }
    
        @staticmethod
        def get_nic_info():
            ips = subprocess.Popen("ifconfig|grep -E 'HWaddr|Bcast:'|grep -v '127.0.0.1'", stdout=subprocess.PIPE,
                                   shell=True)
            data = ips.stdout.read().decode().strip().split('
    ') ip_pattern = re.compile( 'inet addr:(\d+\.\d+\.\d+\.\d+).*?Bcast:(\d+\.\d+\.\d+\.\d+).*?Mask:(\d+\.\d+\.\d+\.\d+)') nic_list = {} for index, line in enumerate(data): ip_list = re.match(ip_pattern, line.strip()) if ip_list: nic_name = data[index - 1].strip().split()[0] hwaddr = data[index - 1].strip().split()[4] ipaddr = ip_list.group(1) broadcast = ip_list.group(2) netmask = ip_list.group(3) nic_list[nic_name] = { "ipaddr": ipaddr, "broadcast": broadcast, "netmask": netmask, "hwaddr": hwaddr, } else: nic_name = line.strip().split()[0] hwaddr = line.strip().split()[4] nic_list[nic_name] = { "ipaddr": "", "broadcast": "", "netmask": "", "hwaddr": hwaddr, } return nic_list @staticmethod def get_disk_info(): disk = subprocess.Popen("fdisk -l|grep Disk|grep bytes|awk '{print $2,$3\"GB\"}'", stdout=subprocess.PIPE, shell=True) disks = disk.stdout.read().decode().strip().split('
    ') data = {} for info in disks: data[info.split(':')[0]] = info.split(':')[1].strip() return data @staticmethod def get_memory_info(): ''' 1. cat /proc/meminfo|grep MemTotal|awk '{sum=$2/1024/1024}END{print sum"GB"}' 2. cat /proc/meminfo |grep 'SwapTotal'|awk '{sum=$2/1024/1024}END{print sum"GB"}' :return: ''' # 1. mem_total = subprocess.Popen("cat /proc/meminfo|grep MemTotal|awk '{sum=$2/1024/1024}END{print sum\"GB\"}'", stdout=subprocess.PIPE, shell=True) # 2. swap_total = subprocess.Popen( "cat /proc/meminfo |grep 'SwapTotal'|awk '{sum=$2/1024/1024}END{print sum\"GB\"}'", stdout=subprocess.PIPE, shell=True) mem_total = mem_total.stdout.read().decode().strip() swap_total = swap_total.stdout.read().decode().strip() return { "mem_total": mem_total, "swap_total": swap_total } def main(): system_info = Collect.get_system_info() device_info = Collect.get_device_info() disk_info = Collect.get_disk_info() cpu_info = Collect.get_cpu_info() memory_info = Collect.get_memory_info() nic_info = Collect.get_nic_info() return { "device": device_info, "system": system_info, "disk": disk_info, "cpu": cpu_info, "memory": memory_info, "nic": nic_info } if __name__ == '__main__': data = main() print(data)

    좋은 웹페이지 즐겨찾기