1.setting.py文件,修改數據存儲方式python
說明:註釋sqlite3數據庫,改用mysql鏈接mysql
DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", "NAME": "cmdb", "USER": "root", "PASSWORD": "root", "HOST": "127.0.0.1", "PORT": "3306", } }
2.安裝mysql,建立庫cmdb數據庫sql
命令:yum install -y mysql-server數據庫
3.刪除原來sqlite文件、安裝mysql模塊文件django
命令:rm -rf db.sqlite3服務器
yum list|grep MySQL-pythonide
yum install -y MySQL-python.x86_643d
4.使用管理腳本,同步數據庫server
python manage.py validatesqlite
python manage.py syncdb
5.修改hostinfo目錄下,修改views.py文件,查看啓動屬性
6.啓動服務器,和收集腳本
python manage.py runserver 0.0.0.0:8000
python getHostInfo.py
7.修改收集信息腳本,將信息序列化內存中,執行腳本,序列化,查看打印信息
執行收集腳本,結果:
8.反序列化,修改views.py
執行收集腳本,運行結果:
9.修改信息收集腳本,實例化
from django.shortcuts import render from hostinfo.models import Host from django.http import HttpResponse import pickle def collect(req): #print req if req.POST: #if req.method == "POST": #print req.body #print pickle.loads(req.body) #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") obj = pickle.loads(req.body) hostname = obj["hostname"] ip = obj["ip"] osver = obj["osver"] vendor = obj["vendor"] product = obj["product"] cpu_model = obj["cpu_model"] cpu_num = obj["cpu_num"] memory = obj["memory"] sn = obj"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")
執行收集腳本,運行結果: