CMDB-자산 수집
자산 수집
서버 정보:
dmidecode -t system|grep 'Manufacturer'
dmidecode -t system|grep 'Serial Number'
dmidecode -t system|grep 'Product Name'
dmidecode -t system|grep 'UUID'
운영 체제 정보:
lsb_release -a|grep 'Distributor ID'
lsb_release -a|grep 'release'
CPU 정보:
cat /proc/cpuinfo|grep 'model name ' |head -1
cat /proc/cpuinfo|grep 'processor'|wc -l
cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
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)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.