Django項目:CMDB(服務器硬件資產自動採集系統)--02--02CMDB將服務器基本信息提交到API接口

 

AutoCmdbdjango

 

 

 

 1 # urls.py
 2 """AutoCmdb URL Configuration
 3 
 4 The `urlpatterns` list routes URLs to views. For more information please see:
 5     https://docs.djangoproject.com/en/2.0/topics/http/urls/
 6 Examples:
 7 Function views
 8     1. Add an import:  from my_app import views
 9     2. Add a URL to urlpatterns:  path('', views.home, name='home')
10 Class-based views
11     1. Add an import:  from other_app.views import Home
12     2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
13 Including another URLconf
14     1. Import the include() function: from django.urls import include, path
15     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
16 """
17 from django.contrib import admin
18 from django.urls import path
19 
20 # ————————02CMDB將服務器基本信息提交到API接口————————
21 from django.conf.urls import url
22 from django.conf.urls import include
23 # ————————02CMDB將服務器基本信息提交到API接口————————
24 
25 urlpatterns = [
26     path('admin/', admin.site.urls),
27 
28     # ————————02CMDB將服務器基本信息提交到API接口————————
29     url(r'^api/', include('api.urls')),
30     # ————————02CMDB將服務器基本信息提交到API接口————————
31 
32 ]
# urls.py

 

 

 

1 # urls.py
2 # ————————02CMDB將服務器基本信息提交到API接口————————
3 from django.conf.urls import url
4 from api import views
5 
6 urlpatterns = [
7     url(r'^asset$', views.AssetView.as_view()),
8 ]
9 # ————————02CMDB將服務器基本信息提交到API接口————————
# urls.py

 

 

 

 1 from django.shortcuts import render
 2 
 3 # Create your views here.
 4 
 5 # views.py
 6 
 7 # ————————02CMDB將服務器基本信息提交到API接口————————
 8 import json #輕量級的文本數據交換格式
 9 from django.views import View
10 from django.views.decorators.csrf import csrf_exempt #和HTML的{% csrf_token %}做用同樣
11 from django.utils.decorators import method_decorator #安全經過 'django.middleware.csrf.CsrfViewMiddleware',
12 from django.http import JsonResponse#這個類是HttpRespon的子類
13 class AssetView(View):# http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
14     @method_decorator(csrf_exempt)#和HTML的{% csrf_token %}做用同樣,安全經過 'django.middleware.csrf.CsrfViewMiddleware',
15     def dispatch(self, request, *args, **kwargs):
16         return super(AssetView, self).dispatch(request, *args, **kwargs)
17     def post(self, request, *args, **kwargs):#接受客戶端到信息
18         server_info = json.loads(request.body.decode('utf-8'))
19         print('獲取到的信息: ',type(server_info),server_info)
20         server_info = json.loads(server_info)#把字符串轉換成字典
21         print('轉換後的信息: ',type(server_info),server_info)
22         hostname = server_info['hostname']
23         print('主機名',hostname)
24         ret = {'code': 1000, 'message': '[%s]更新完成' % hostname}#返回到客戶端到信息
25         print(ret)
26         return JsonResponse(ret)#這個類是HttpRespon的子類
27 # ————————02CMDB將服務器基本信息提交到API接口————————
# views.py

 

 

 

 1 #settings.py
 2 # ————————01CMDB獲取服務器基本信息————————
 3 import os
 4 
 5 BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))##當前路徑
 6 
 7 # 採集資產的方式,選項有:agent(默認), salt, ssh
 8 MODE = 'agent'
 9 
10 # ————————01CMDB獲取服務器基本信息————————
11 
12 # ————————02CMDB將服務器基本信息提交到API接口————————
13 # 資產信息API
14 ASSET_API = "http://127.0.0.1:8000/api/asset"
15 # ————————02CMDB將服務器基本信息提交到API接口————————
#settings.py

 

 

 1 # client.py
 2 # ————————01CMDB獲取服務器基本信息————————
 3 from src import plugins  #__init__.py
 4 from lib.serialize import Json #轉成字符串或者模式
 5 
 6 # ————————02CMDB將服務器基本信息提交到API接口————————
 7 import requests  #僞造頁面訪問
 8 from config import settings #文件配置
 9 # ————————02CMDB將服務器基本信息提交到API接口————————
10 
11 class AutoBase(object):
12 
13     # ————————02CMDB將服務器基本信息提交到API接口————————
14     def __init__(self):
15         self.asset_api = settings.ASSET_API  #ASSET_API = "http://127.0.0.1:8000/api/asset"
16     def post_asset(self, msg):#post方式向API接口提交資產信息
17         status = True#獲取到信息
18         try:
19             response = requests.post(
20                 url=self.asset_api,
21                 json=msg
22             )
23         except Exception as e:
24             response = e
25             status = False  #獲取信息時出現錯誤
26         print(response.json())
27     # ————————02CMDB將服務器基本信息提交到API接口————————
28 
29     def process(self):#派生類須要繼承此方法,用於處理請求的入口
30         raise NotImplementedError('您必須實現過程的方法')
31 
32 class AutoAgent(AutoBase):
33     def process(self):
34         server_info = plugins.get_server_info()#獲取本地基本信息
35         server_json = Json.dumps(server_info.data)#json.dumps將 Python 對象編碼成 JSON 字符串
36         print('提交資產信息:',server_json)
37 # ————————01CMDB獲取服務器基本信息————————
38 
39         # ————————02CMDB將服務器基本信息提交到API接口————————
40         self.post_asset(server_json)# post方式向接口提交資產信息
41         # ————————02CMDB將服務器基本信息提交到API接口————————
# client.py

 

 

相關文章
相關標籤/搜索