url(r'^hostinfo/collect/$', 'hostinfo.views.collect'),
2.修改hostinfo主机的views.py文件
from hostinfo.models import Host from django.http import HttpResponse def 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") 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.测试shell 命令执行插入
命令: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/
运行结果:
4.收集主机信息的python脚本
#/usr/bin/env python #coding: utf-8 from subprocess import PIPE,Popen import urllib,urllib2 def getIfconfig(): p = Popen(["ifconfig"],stdout=PIPE) data = p.stdout.read() return data def 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()
运行结果:
原文地址:http://blog.51cto.com/huangzp/2089766