#!/usr/bin/python#Coding:utf-8"""Capture Machine Self information 1 hostname 2 Memory 3 IP and MAC address 4 CPU information 5 hard disk partition information 6 manufacturer Information 7 Factory date 8 system version"""ImportSocketImportPsutilImportsubprocessImport TimeImportPlatformImportJSONImportRequestsdevice_white= ['eth1','eth2','Eth3','bond0','Bond1']defget_hostname ():returnSocket.gethostname ()defGet_meminfo (): With open ("/proc/meminfo") as F:tmp= Int (F.readline (). Split () [1]) returntmp/1024defGet_device_info (): Ret= [] forDevice, Device_infoinchPsutil.net_if_addrs (). Iteritems ():ifDeviceinchDevice_white:tmp_device= {} forSincinchDevice_info:ifsinc.family = = 2: tmp_device['IP'] =sinc.addressifsinc.family = = 17: tmp_device['mac'] =sinc.address ret.append (tmp_device)returnretdefGet_cpu_info (): Ret= {'CPU':"','Num': 0} with open ('/proc/cpuinfo') as F: forLineinchf:tmp= Line.split (":") Key=Tmp[0].strip ()ifKey = ="Processor": ret['Num'] + = 1ifKey = ="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= [] forDevinchdisk_data.stdout.readlines ():#size = Int (Dev.strip (). Split () [4])/1024/1024/1024size = Int (Dev.strip (). Split (',') [1].split () [0])/1024/1024/1024patition_size.append (str (size))return " + ". Join (patition_size)#Get manufacturer InformationdefGet_manufacturer_info (): Ret={} cmd="""/usr/sbin/dmidecode | grep-a6 ' System information '"""Manufacturer_data= subprocess. Popen (cmd, Shell = True, stdout = subprocess. PIPE, stderr=subprocess. STDOUT) forLineinchmanufacturer_data.stdout.readlines ():if 'manufacturer' inchline:ret['Manufacturers'] = Line.split (':') [1].strip ()elif 'Product Name' inchline:ret['Server_type'] = Line.split (':') [1].strip ()elif 'Serial Number' inchline:ret['St'] = Line.split (':') [1].strip ()elif 'UUID' inchline:ret['UUID'] = Line.split (':') [1].strip ()returnret#Get Factory Datedefget_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 ()returnTime.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= {} forInfoinchIpinfo:ifInfo.has_key ('IP') andInfo.get ('Device', None)inchinner_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 DataSend (data)defSend (data): URL="http://192.168.99.10:8000/resources/server/reporting/"R= Requests.post (URL, data =data)PrintRPrintDataif __name__=="__main__": Run ()
Get server hardware information through a Python script