资产收集
服务器信息:
- 服务器制造商
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中core的个数(即核数)
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('\n')
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('\n')
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)