cmdb-資産管理
1、建立一個app(資産管理的)
python manage.py startapp asset
建立modles.py(資料表)
1、python manage.py makemigrations #看是否有資料更新
2、python manage.py migrate #生成表
3、
建立資料庫(當執行python manage.py migrate(自動建立生産資料表))
定義了
自動擷取:
name(主機名)、ip、mac位址、os系統、arch(架構,64還是32位)
記憶體、cpu、磁盤
sn号
user(負責人)
remark(備注)
purchase_time(伺服器采購時間)
over_insurance_time(伺服器過保時間)
created_time 添加時間(第一次添加到資料庫的時間)
last_time 最後更新時間 這個是自己修改後,然後更新的時間
4、建立一個自定義的指令
在asset下建立一個management的包(帶有__init__.py),在management目錄下建立一個commands的包(帶有__init__.py)
cmdb/asset/management/commands/collect_host.py
必須得帶有Command、handle固定格式
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
def handle(self, *args, **options):
print('hello, django!')
collect_host.py:擷取主機資訊,然後儲存到資料庫中
5、多個任務則寫多個elif (collect_host則是個函數)
def v2_runner_on_ok(self, result, **kwargs):
if result.task_name == 'collect_host':
self.collect_host(result._result)
寫ansible的playbook,執行setup 功能
play_source = {
'name' : "cmdb",
'hosts' : 'all',
'gather_facts' : 'no',
'tasks' : [
{
'name' : 'collect_host',
'setup' : ''
}
]
}
主機資訊:在目前目錄下的 etc/hosts 裡面
inventory = InventoryManager(loader=loader, sources=os.path.join(settings.BASE_DIR, 'etc', 'hosts'))
[root@test-01 etc]# cat hosts
localhost ansible_connection=local
#test ansible_connection=smart ansible_host=192.168.1.69 ansible_user=root ansible_ssh_pass=123456
test-03
test-04
test-05
執行:python manage.py collect_host (自定義的)
執行成功後,到資料庫檢視是否有資料
2、開始寫index.html
可以先寫一個模闆base.html,然後繼承它
{% extends "base.html" %} {#繼承base.html的模闆#}
{% block title %}資産管理{% endblock %}{#标題#}
jQuery('#form-view').find('[name=name]').val(result['result']['name']);
jQuery('#form-view').find('[name=id]').val(result['result']['id']);
jQuery('#form-view').find('[name=remark]').val(result['result']['remark']);
{# <input type="hidden" class="form-control" name="id" value=""/>,找到name=id(find('[name=id]')),value= result['result']['id']#}
3、這個是擷取id=form-view 中的字典元素 {'id':1,'name':2}
var data = jQuery('#form-view').serializeArray();
4、/*重新整理table*/ 這個隻是重新整理目前頁面的
table.ajax.reload(null, false);
jQuery.get 如果ajax請求的是get,則通路路徑會帶上token位址,post 則不會