1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
| #!/usr/bin/env python
"""
file name: collect_info_a.py
"""
from subprocess import Popen, PIPE
def getIfconfig():
p = Popen(['ifconfig'], stdout=PIPE, stderr=PIPE)
data = p.stdout.read()
return data
def getDmi():
p = Popen(['dmidecode'], stdout=PIPE, stderr=PIPE)
data = p.stdout.read()
return data
"""
从getIfconfig() 和getDmi() 函数返回的都是一个字符串。下面再定义一个
parseData(data) 的函数,将字符串分割成一个列表,每遇到顶格的行,就是
新的一段信息.
"""
def parseData(data):
parsed_data = []
new_line = ''
data = [i for i in data.split('\n') if i] #将字符串分割,去掉空行的元素
for line in data:
if line[0].strip(): #当遇到顶格的行,就把new_line 保存的上一段信息,append 到parsed_line
parsed_data.append(new_line)
new_line = line+'\n' #重新保存新的一段的信息
else:
new_line += line+'\n'
parsed_data.append(new_line)
return [i for i in parsed_data if i] #去掉空行的元素
"""
parseData(data) 函数返回的就是一个处理过的列表,将收集到的ip 字符串信息和 dmidecode 字符串信息,交给
下面定义的parseIfconfig() 和parseDmi() 函数分别处理,返回ip 信息的字典,和dmidecode 信息的字典。
"""
def parseIfconfig(parsed_data):
parsed_data = [i for parsed_data if i and not i.startswith('lo')] #将"lo" 网卡的信息去掉
dic = {}
for lines in parsed_data:
devname = lines.split('\n')[0].split()[0]
macaddr = lines.split('\n')[0].split()[-1]
ipaddr = lines.split('\n')[1].split()[1].split(':')[1]
break #由于只需要第一张网卡的信息,所以这里就可以退出循环了
dic['ip'] = ipaddr
return dic
def parseDmi(parsed_data):
dic = {}
parsed_data = [i for i in parsed_data if i.startswith('System Information')] #把这段信息直接整出来
parsed_data = [i for i in parsed_data[0].split('\n')[1:] if i ]
parsed_data = [i.strip().split(':') for i in parsed_data if i]
dmi_dic = dict(parsed_data)
dic = {}
dic['vender'] = dmi_dic['Manufacturer'].strip()
dic['product'] = dmi_dic['Product Name'].strip()
dic['sn'] = dmi_dic['Serial Number'].strip()
return dic
"""
getHostName: 函数
fn : 文件名参数
功能: 通过fn 传入文件名,读取HOSTNAME 信息
"""
def getHostName(fn):
with open(fn) as fd:
for line in fd:
if line.startswith('HOSTNAME'):
HostName = line.split('=')[1].strip()
break
return {'HostName': HostName}
"""
getOSver: 函数
fn : 文件名参数
功能: 打开fn 文件,读取操作系统版本信息
"""
def getOSver(fn):
with open(fn) as fd:
for line in fd:
osver = line.strip()
break
return {'osver': osver}
"""
getCpu: 函数
fn : 文件名参数
功能: 读取fn 文件信息,读取cpu 核数和cpu 型号
"""
def getCpu(fn):
num = 0
dic = {}
with open(fn) as fd:
for line in fd:
if line.startswith('processor'):
num += 1
if line.startswith('model name'):
model_name = line.split(':')[1]
model_name = model_name.split()[0] + ' ' + model_name.split()[-1]
dic['cpu_num'] = num
dic['cpu_model'] = model_name
return dic
"""
getMemory: 函数
fn : 文件名参数
功能: 打开fn 文件,读取系统MemTotal 数值
"""
def getMemory(fn):
with open(fn) as fd:
for line in fd:
if line.startswith('MemTotal'):
mem = int(line.split()[1].strip())
break
mem = '%s' % int((mem/1024.0)) +'M'
return {'Memory': mem}
if __name__ == '__main__':
data_ip = getIfconfig()
parsed_data_ip = parseData(data_ip)
ip = parseIfconfig(parsed_data_ip)
data_dmi = getDmi()
parsed_data_dmi = parseData(data_dmi)
dmi = prseDmi(parsed_data_dmi)
HostName = getHostName('/etc/sysconfig/network')
osver = getOSver('/etc/issue')
cpu = getCpu('/proc/cpuinfo')
mem = getMemory('/proc/meminfo')
dic = {} #定义空字典,上面收集到的主机信息都是字典形式的,就是为了现在能将它们update 在一个字典
dic.update(ip)
dic.update(dmi)
dic.update(HostName)
dic.update(osver)
dic.update(cpu)
dic.update(mem)
print dic
|