标签:django
1.改削收集脚本#/usr/bin/env python #coding: utf-8 from subprocess import PIPE,Popen import urllib,urllib2 import pickle import json 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) #d = pickle.dumps(dic) d = json.dumps(dic) req = urllib2.urlopen(":8000/hostinfo/collect/",d) print req.read()