1. Define the URL access path for the project Simplecmdb
URL (r ' ^hostinfo/collect/$ ', ' hostinfo.views.collect '),
2. Modify the views.py file for the Hostinfo host
from hostinfo.models import hostfrom django.http import httpresponsedef Collect (req): print req #if req. post: if req.method == "POST": hostname = req. Post.get ("hostname") ip = req. Post.get ("IP") osver = req. Post.get ("Osver") vendor = req. Post.get ("Vendor") product = req. Post.get ("Product") cpu_model = req. Post.get ("Cpu_model") cpu_num = req. Post.get ("Cpu_num") memory = req. Post.get ("Memory")    &NBSp; sn = req. Post.get ("SN") host = host () host.hostname = hostname Host.ip =ip host.osver = osver host.product = product host.cpu_model = cpu_model host.cpu_num = cpu_num host.memory = memory host.vendor = vendor host.sn = sn Host.save () return httpresponse ("OK") else: return httpresponse (" No data ")
3. Test shell command Execution insert
Command: curl-d hostname= "HUANGZP2"-D ip= "192.168.2.231"-D osver= "Centos7"-D vendor= "HP"-D product= "BL 380"-D sn= "12345677 "-D cpu_model=" Intel "-D cpu_num=" 8 "-D memory=" 16G " http://192.168.2.230:8000/hostinfo/collect/
Operation Result:
4. python script to collect host information
#/usr/bin/env python#coding: utf-8from subprocess import pipe,popenimport urllib , Urllib2def getifconfig (): p = popen (["Ifconfig"],stdout=pipe) data = p.stdout.read () return datadef getdmi (): p = popen (["Dmidecode"],stdout=pipe) data = p.stdout.read () return data def parsedata (data): parse_data = [] new_line = "" data = [i for i in data.split ("\ n") if i] for line in data: if line[0]. Strip (): parse_data.append (New_line) new_line = line + "\ n" else: new_line += line + "\ n" parse_data.append (new_line) return [i for i in parse_data if i] def Parseifconfig (parse_data): dic = {} parse_data = [i for i in parse_data if i and not i.startswith ("Lo")] for lines in parse_data: line_list = [i for i in lines.split ("\ n") if i] #devname = line_list[0].split (":") [0] for line in line_list : if Line.split () [0] == "inet": ip = line.split () [1] if line.split () [0] == "Ether": mac = line.split () [1] dic["IP"] = ip #dic ["Mac"] =  MAC     RETURN DIC    DEF PARSEDMI (Parse_data): dic = {} parse_data = [i for i in parse_data if i.startswith ("System information")] parse_ Data = [ i foR i in parse_data[0].split ("\ n") [1:] if i] dmi_dic = dict ([I.strip (). Split (":") for i in parse_data]) dic["Vendor" ] = dmi_dic["Manufacturer"] dic["Product"] = dmi_dic["Product Name "] dic[" sn "] = dmi_dic[" Serial number "] Return dic def gethostname (f): with open (f) as fd: for line in fd: hostname = line.strip () break return {"hostname": Hostname} def getversion (f): with open (f) as fd: for line in fd: version = line.strip () break return {"Osver": Version} def getcpu ( f): num = 0 with open (f) as fd: for line in fd: if line.startswith ("Processor"): num += 1 if line.startswith ("Model name"): cpu_model = line.split (":") [ 1].strip () return {"cpu_num": num, "Cpu_model": Cpu_model} def getmem (f): with open (f) as fd: for line in fd: if Line.startswith ("Memtotal:"): mem = line.split (":") [1].strip (). Split () [0] break mem = "%.2f" % (int (MEM)/ 1024.0) + "M" return {"Memory": mem} if __ name__ == "__main__": dic = {} data_ip = getifconfig () parsed_data_ip = parsedata (DATA_IP) ip = parseifconfiG (PARSED_DATA_IP) data_dmi = getdmi () parsed_data_dmi = parsedata (DATA_DMI) dmi = parsedmi (PARSED_DATA_DMI) hostname = gethostname ("/etc/hostname") version = getversion ("/etc/redhat-release") Cpu = GETCPU ("/proc/cpuinfo") mem = getmem ("/proc/ Meminfo ") dic.update (IP) dic.update (DMI) Dic.update (hostname) dic.update (version) dic.update (CPU) dic.update (MEM) print dic d = urllib.urlencode (DIC) req = urllib2.urlopen ("Http://192.168.2.230:8000/ hostinfo/collect/", d) print req.read ()
Operation Result:
django-collecting host information to the CMDB (2)