Django项目:CMDB(服务器硬件资产自动采集系统)--01--01CMDB获取服务器基本信息

AutoClient

 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获取服务器基本信息————————

#settings.py

 1 #base.py
 2 # ————————01CMDB获取服务器基本信息————————
 3 from config import settings  #配置文件
 4
 5 class BasePlugin(object):
 6     def __init__(self, hostname=‘‘):
 7         if hasattr(settings, ‘MODE‘):
 8             self.mode = settings.MODE #采集资产的方式
 9         else:
10             self.mode = ‘agent‘#默认,采集资产的方式
11
12     def execute(self):
13         return self.windows()
14
15     def windows(self):
16         raise Exception(‘您必须实现windows的方法‘)
17 # ————————01CMDB获取服务器基本信息————————

#base.py

 1 #response.py
 2 # ————————01CMDB获取服务器基本信息————————
 3 class BaseResponse(object): #提交数据的类型
 4     def __init__(self):
 5         self.status = True      #状态
 6         self.message = None     #消息
 7         self.data = None        #数据内容
 8         self.error = None       #错误信息
 9
10 # ————————01CMDB获取服务器基本信息————————

#response.py

 1 #auto-client.py
 2 # ————————01CMDB获取服务器基本信息————————
 3 import os
 4 BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#当前路径
 5 print(‘当前路径:‘,type(BASEDIR),BASEDIR)
 6 os.path.join(BASEDIR)# Join(转成字符串)
 7
 8 from src.scripts import client
 9 if __name__ == ‘__main__‘:#让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行
10     client()
11 # ————————01CMDB获取服务器基本信息————————

#auto-client.py

 1 # scripts.py
 2 # ————————01CMDB获取服务器基本信息————————
 3 from src.client import AutoAgent  #本地采集模式
 4 from config import settings       #配置文件
 5
 6 def client(): #根据配置文件判断采集模式
 7     if settings.MODE == ‘agent‘:
 8         cli = AutoAgent()  #本地采集模式
 9     else:
10         raise Exception(‘请配置资产采集模式,如:agent、ssh、salt‘)
11     cli.process() #执行def process(self):
12 # ————————01CMDB获取服务器基本信息————————

# scripts.py

 1 # client.py
 2 # ————————01CMDB获取服务器基本信息————————
 3 from src import plugins  #__init__.py
 4 from lib.serialize import Json #转成字符串或者模式
 5
 6 class AutoBase(object):
 7     def process(self):#派生类需要继承此方法,用于处理请求的入口
 8         raise NotImplementedError(‘您必须实现过程的方法‘)
 9 class AutoAgent(AutoBase):
10     def process(self):
11         server_info = plugins.get_server_info()#获取本地基本信息
12         server_json = Json.dumps(server_info.data)#json.dumps将 Python 对象编码成 JSON 字符串
13         print(‘提交资产信息:‘,server_json)
14
15 # ————————01CMDB获取服务器基本信息————————

# client.py

 1 #__init__.py
 2 # ————————01CMDB获取服务器基本信息————————
 3 from src.plugins.basic import BasicPlugin
 4
 5 def get_server_info(hostname=None):
 6     """
 7     获取服务器基本信息
 8     :param hostname: agent模式时,hostname为空;salt或ssh模式时,hostname表示要连接的远程服务器
 9     :return:
10     """
11     response = BasicPlugin(hostname).execute()#获取基本信息
12     """
13     class BaseResponse(object):
14         def __init__(self):
15             self.status = True
16             self.message = None
17             self.data = None
18             self.error = None
19     """
20     return response
21
22 if __name__ == ‘__main__‘:
23     ret = get_server_info()
24 # ————————01CMDB获取服务器基本信息————————

#__init__.py

 1 # basic.py
 2 # ————————01CMDB获取服务器基本信息————————
 3 from .base import BasePlugin #采集资产的方式
 4 from lib.response import BaseResponse   #提交数据的类型
 5 import platform  #platform模块给我们提供了很多方法去获取操作系统的信息
 6 import wmi#Windows操作系统上管理数据和操作的基础设施
 7
 8 class BasicPlugin(BasePlugin):
 9     def os_platform(self):#获取系统平台
10         output=platform.system()
11         return output.strip()#strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
12     def os_version(self):#获取系统版本
13         output = wmi.WMI().Win32_OperatingSystem()[0].Caption
14         return output.strip()#strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
15     def os_hostname(self):#获取主机名
16         output = wmi.WMI().Win32_OperatingSystem()[0].CSName
17         return output.strip()#strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
18
19     def windows(self):
20         response = BaseResponse()#提交数据的类型
21         try:
22             ret = {
23                 ‘os_platform‘: self.os_platform(),#系统平台
24                 ‘os_version‘: self.os_version(),#系统版本
25                 ‘hostname‘: self.os_hostname(),#主机名
26             }
27             response.data = ret #字典形式
28             print(‘windows服务器基本信息:‘,response.data)
29         except Exception as e:
30             response.status = False#获取信息时出现错误
31         return response
32     """
33     class BaseResponse(object): #提交数据的类型
34     def __init__(self):
35         self.status = True      #状态
36         self.message = None     #消息
37         self.data = None        #数据内容
38         self.error = None       #错误信息
39
40     """
41 # ————————01CMDB获取服务器基本信息————————

# basic.py

 1 #serialize.py
 2 # ————————01CMDB获取服务器基本信息————————
 3 import json as default_json  #轻量级的数据交换格式
 4 from json.encoder import JSONEncoder  #JSONEncoder用来将模型转JSON字符串,JSONDecoder是用来将JSON字符串转为模型
 5 from .response import BaseResponse
 6
 7 class JsonEncoder(JSONEncoder):
 8     def default(self, o):
 9         if isinstance(o, BaseResponse):#isinstance()函数来判断一个对象是否是一个已知的类型
10             return o.__dict__  #返回字典
11         return JSONEncoder.default(self, o) #JSONEncoder用来将模型转JSON字符串,JSONDecoder是用来将JSON字符串转为模型
12     """
13     不是这个类型就不处理,直接返回
14     class BaseResponse(object):
15         def __init__(self):
16             self.status = True  #状态
17             self.message = None  #消息
18             self.data = None  #数据内容
19             self.error = None  #错误信息
20
21     """
22 class Json(object):
23     @staticmethod#返回函数的静态方法
24     def dumps(response, ensure_ascii=True):
25         return default_json.dumps(response, ensure_ascii=ensure_ascii, cls=JsonEncoder)#dumps 方法是将 json 的 dict 形式,转换成为字符串 str 类型
26
27 # ————————01CMDB获取服务器基本信息————————

#serialize.py

原文地址:https://www.cnblogs.com/ujq3/p/9218798.html

时间: 2024-08-28 01:22:40

Django项目:CMDB(服务器硬件资产自动采集系统)--01--01CMDB获取服务器基本信息的相关文章

Django项目:CMDB(服务器硬件资产自动采集系统)--02--02CMDB将服务器基本信息提交到API接口

AutoCmdb 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

Django项目:CMDB(服务器硬件资产自动采集系统)--11--07CMDB文件模式测试采集硬件数据

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 # ----

Django项目:CMDB(服务器硬件资产自动采集系统)--08--06CMDB测试Linux系统采集硬件数据的命令03

https://www.virtualbox.org/wiki/Downloads https://mirrors.aliyun.com/centos/7/isos/x86_64/ http://www.xitongzhijia.net/soft/24315.html http://www.downxia.com/downinfo/4574.html 原文地址:https://www.cnblogs.com/ujq3/p/9245356.html

Django项目:CMDB(服务器硬件资产自动采集系统)--09--06CMDB测试Linux系统采集硬件数据的命令04

root 123456 ip addr init 0 root 123456 ip addr root 123456 python3 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel yum -y install wget 原文地址:https://www

Django项目:CMDB(服务器硬件资产自动采集系统)--10--06CMDB测试Linux系统采集硬件数据的命令05

cd /py/AutoClient/bin python3 auto-client.py /usr/local/python3/bin/pip install requests python3 auto-client.py https://www.bejson.com/ 原文地址:https://www.cnblogs.com/ujq3/p/9245767.html

Django 1.6 最佳实践: 如何设置django项目的设置(settings.py)和部署文件(requirements.txt)

Django 1.6 最佳实践: 如何设置django项目的设置(settings.py)和部署文件(requirements.txt) 作者: Desmond Chen,发布日期: 2014-05-17, 修改日期: 2014-05-18 在Django 1.6中的settings.py中可以修改130多项设置, 但大多数都继承自默认值. 设置是在web服务器启动时首次载入的, 服务器重启时重新载入, 因此, 程序员们应尽量避免修改正式服务器上使用的settings.py文件. 以下是一些我们

Django 项目之--环境准备01

1.环境准备; (1).python 版本 3.6.0 (2).Django 版本 1.8.2 2.windows 开发环境老是出现文件编码和各种环境问题; (本博文采用远端配置python django项目环境) 3.远端linux服务器信息;    服务器主机IP          系统版本信息         内核版本信息            备注 192.168.1.12   CentOS Linux release 7.4.1708 3.10.0-693.el7.x86_64 默认

Django项目实践2 - Django模板

http://blog.csdn.net/pipisorry/article/details/45061511 上篇:Django项目实践1 - 创建Django项目 Django模板 {视图和模板对逻辑和显示进行了分隔} 上面是使用 django.http.HttpResponse() 来输出"Hello World!".该方式将数据与视图混合在一起,不符合Django的MVC思想.或者HTML被直接硬编码在 Python 代码之中. html = "<html>

使用uWSGI部署django项目

先说说什么是uWSGI吧,他是实现了WSGI协议.uwsgi.http等协议的一个web服务器,那什么是WSGI呢? WSGI是一种Web服务器网关接口.它是一个Web服务器(如nginx)与应用服务器(如uWSGI服务器)通信的一种规范(协议). 还有一种wsgi,uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信.uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information). 部署步骤: 1.