#!/usr/bin/python#coding:utf-8
"""采集机器自身信息
1 主机名
2 内存
3 ip与mac地址
4 cpu信息
5 硬盘分区信息
6 制造商信息
7 出厂日期
8 系统版本"""
importsocketimportpsutilimportsubprocessimporttimeimportplatformimportjsonimportrequests
device_white= ['eth1', 'eth2', 'eth3', 'bond0', 'bond1']defget_hostname():returnsocket.gethostname()defget_meminfo():
with open("/proc/meminfo") as f:
tmp= int(f.readline().split()[1])return tmp / 1024
defget_device_info():
ret=[]for device, device_info inpsutil.net_if_addrs().iteritems():if device indevice_white:
tmp_device={}for sinc indevice_info:if sinc.family == 2:
tmp_device['ip'] =sinc.addressif sinc.family == 17:
tmp_device['mac'] =sinc.address
ret.append(tmp_device)returnretdefget_cpu_info():
ret= {'cpu':'','num':0}
with open('/proc/cpuinfo') as f:for line inf:
tmp= line.split(":")
key=tmp[0].strip()if key == "processor":
ret['num'] += 1
if key == "model name":
ret['cpu'] = tmp[1].strip()returnretdefget_disk_info():
cmd= """/sbin/fdisk -l|grep Disk|egrep -v 'identifier|mapper|Disk label'"""disk_data= subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE, stderr =subprocess.STDOUT)
patition_size=[]for dev indisk_data.stdout.readlines():#size = int(dev.strip().split()[4]) / 1024 / 1024/ 1024
size = int(dev.strip().split(',')[1].split()[0]) / 1024 / 1024/ 1024patition_size.append(str(size))return "+".join(patition_size)#获取制造商信息
defget_manufacturer_info():
ret={}
cmd= """/usr/sbin/dmidecode | grep -A6 'System Information'"""manufacturer_data= subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr=subprocess.STDOUT)for line inmanufacturer_data.stdout.readlines():if 'Manufacturer' inline:
ret['manufacturers'] = line.split(':')[1].strip()elif 'Product Name' inline:
ret['server_type'] = line.split(':')[1].strip()elif 'Serial Number' inline:
ret['st'] = line.split(':')[1].strip()elif 'UUID' inline:
ret['uuid'] = line.split(':')[1].strip()returnret#获取出厂日期
defget_real_date():
cmd= """/usr/sbin/dmidecode | grep -i release"""date_data= subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
real_date= date_data.stdout.readline().split(':')[1].strip()return time.strftime('%Y-%m-%d', time.strptime(real_date, "%m/%d/%Y"))defget_os_version():return ' '.join(platform.linux_distribution())defget_innerip(ipinfo):
inner_device= ['eth1', 'bond0']
ret={}for info inipinfo:if info.has_key('ip') and info.get('device', None) ininner_device:
ret['ip'] = info.get('ip')
ret['mac_address'] = info.get('mac')returnretreturn{}defrun():
data={}
data['hostname'] =get_hostname()
device_info=get_device_info()
data.update(get_innerip(device_info))
data['ipinfo'] =json.dumps(device_info)
cpu_info=get_cpu_info()
data['server_cpu'] = "{cpu} {num}".format(**cpu_info)
data['server_disk'] =get_disk_info()
data['server_mem'] =get_meminfo()
data.update(get_manufacturer_info())
data['manufacture_date'] =get_real_date()
data['os'] =get_os_version()if 'virtualbox' == data['server_type']:
data['vm_status'] =0else:
data['vm_status'] = 1
#return data
send(data)defsend(data):
url= "http://192.168.99.10:8000/resources/server/reporting/"r= requests.post(url, data =data)printrprintdataif __name__ == "__main__":
run()