- 實現的功能
搜集系統消息,有生產商,CPU型號,核數,內存,主機名,發行版名稱 - 可運行的系統
目前已在RHEL, Ubuntu, Archlinux上測試通過 - 獲取不同發行版主機名邏輯判斷思路分析
大家肯定都知道,RHEL的主機名配置文件和Ubuntu的不一樣,可是Archlinux的和Ubuntu的又不一樣,所以這里就將不同操作系統的主機名配置文件和操作系統的issue做了一個映射
主要是將不同系統的系統名和對應的配置文件及存放在一個字典[ os_type ]結構中,后續有需要可以再擴展,而下面的的邏輯代碼則不需要改變[ parseCfg() ],從字典中循環取數據,如果取到則退出的配置文件
運行結果類似如下:
key: value
product: VMware Virtual Platform
modelName: Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz
totalMem: 1.5 GB
cpuCore: 2
vender: VMware, Inc.
hostName: archlinux.vsphere
distroName: Arch Linux
SN: VMware-56 4d b9 19 26 63 ad ae-41 f3 5c 3c 34 66 ec bd
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
''' 可以在多linux平台下的多種機器下工作,需要添加相應的鍵值對兒在os_type字典里面 '''
from subprocess import Popen, PIPE
# 定義操作系統主機名字段字典, 在獲取不同類型主機名配置文件的時候使用
os_type = {'archlinux': ['Arch Linux', '/etc/hostname'],
'ubuntu': ['Ubuntu', '/etc/hostname'],
'CentOS release': ['CentOS release', '/etc/sysconfig/network']
}
def parseCfg(osName):
' 通過不同的主機名,返回對應的主機名的配置文件位置 '
for type in os_type:
if osName.startswith( os_type[type][0] ):
cfg = os_type[ type ][ 1 ]
return cfg
def getHostInfo(command):
''' @args: command 輸入要運行的系統命令
@return: 返回一個長的字符串
'''
p = Popen([command], stdout=PIPE)
data = p.stdout.read()
return data
def parseHostInfo(data):
NEWLINE = '\n'
parsedData = []
eachSection = ''
''' regenerate a list, remove the NULL element
@args: data is a long string
@return: all of sections list
'''
data = [i for i in data.split('\n') if i]
for eachLine in data:
if eachLine[0].strip():
parsedData.append(eachSection)
eachSection = eachLine + NEWLINE
else:
eachSection = eachSection + eachLine + NEWLINE
parsedData.append(eachSection)
return [i for i in parsedData if i]
def parseIPInfo(parsedData):
dic = {}
data = [i for i in parsedData if i and not i.startswith('lo')]
for lines in data:
lineLst = lines.split('\n')
devname = lineLst[0].split(':')[0]
ipaddr = lineLst[1].split()[1]
macaddr = lineLst[3].split()[1]
dic[devname] = [ipaddr, macaddr]
return dic
def parseDMIInfo(parsedData):
'''
:param parsedData:
:return:
'''
dmi_result = {}
data = [i for i in parsedData if i.startswith('System Information')]
data = [i for i in data[0].split('\n')[1:] if i]
'生成一個大列表,每個元素也是一個列表,且僅有兩個元素,所以下面可以直接生成一個字典數據結構'
l_list = [i.strip().split(':') for i in data]
sys_info_dic = dict(l_list)
dmi_result['vender'] = sys_info_dic['Manufacturer'].strip()
dmi_result['product'] = sys_info_dic['Product Name'].strip()
dmi_result['SN'] = sys_info_dic['Serial Number'].strip()
return dmi_result
def getDistroName():
with open('/etc/issue') as fd:
distroName = ' '.join(fd.read().strip().split()[:2])
return {'distroName': distroName}
def getHostname(fn, osVer):
with open(fn) as fd:
host = fd.read()
if osVer.startswith('Arch Linux') or osVer.startswith('Ubuntu'):
hostName = host.strip()
return {'hostName': hostName}
if osVer == 'CentOS release':
host = host.strip().split('\n')
for i in host:
if i.startswith('HOSTNAME'):
hostName = i.split('=')[1]
return {'hostName': hostName}
def getMeminfo():
with open('/proc/meminfo') as fd:
for eachLine in fd:
if eachLine.startswith('MemTotal'):
totalMem = eachLine.split(':')[1].strip()
totalMem = '%.1f' % (float(totalMem.split()[0]) / 1024.0 / 1024)
return {'totalMem': str(totalMem) + ' GB'}
def getCPUInfo():
with open('/proc/cpuinfo') as fd:
for eachLine in fd:
if eachLine.startswith('cpu cores'):
cpuCore = eachLine.split(':')[1].strip()
continue
if eachLine.startswith('model name'):
modelName = eachLine.split(':')[1].strip()
return {'cpuCore': cpuCore, 'modelName': modelName}
if __name__ == '__main__':
dic = {}
cfg = '/etc/sysconfig/network'
data = getHostInfo('dmidecode')
dmiLst = parseHostInfo(data)
' dmi info '
dmiInfo = parseDMIInfo(dmiLst)
memInfo = getMeminfo()
osVer = getDistroName()
osName = osVer['distroName']
cfg = parseCfg(osName)
hostInfo = getHostname(cfg, osName)
cpuInfo = getCPUInfo()
dic.update(dmiInfo)
dic.update(hostInfo)
dic.update(memInfo)
dic.update(osVer)
dic.update(cpuInfo)
print('%10s: %s' % ('key', 'value'))
for key in dic.items():
print('%10s: %s' % (key[0], key[1]))