zoukankan      html  css  js  c++  java
  • 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 本模块基于windows操作系统,依赖wmi和win32com库,需要提前使用pip进行安装,
     9 我们依然可以通过pip install pypiwin32来安装win32com模块
    10 或者下载安装包手动安装。
    11 """
    12 
    13 class BasicPlugin(BasePlugin):
    14     def os_platform(self):#获取系统平台
    15         output=platform.system()
    16         return output.strip()#strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
    17     def os_version(self):#获取系统版本
    18         output = wmi.WMI().Win32_OperatingSystem()[0].Caption
    19         return output.strip()#strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
    20     def os_hostname(self):#获取主机名
    21         output = wmi.WMI().Win32_OperatingSystem()[0].CSName
    22         return output.strip()#strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
    23 
    24     def windows(self):
    25         response = BaseResponse()#提交数据的类型
    26         try:
    27             ret = {
    28                 'os_platform': self.os_platform(),#系统平台
    29                 'os_version': self.os_version(),#系统版本
    30                 'hostname': self.os_hostname(),#主机名
    31             }
    32             response.data = ret #字典形式
    33             print('windows服务器基本信息:',response.data)
    34         except Exception as e:
    35             response.status = False#获取信息时出现错误
    36         return response
    37     """
    38     class BaseResponse(object): #提交数据的类型
    39     def __init__(self):
    40         self.status = True      #状态
    41         self.message = None     #消息
    42         self.data = None        #数据内容
    43         self.error = None       #错误信息
    44 
    45     """
    46 # ————————01CMDB获取服务器基本信息————————
    # basic.py
    import wmi
    """
    本模块基于windows操作系统,依赖wmi和win32com库,需要提前使用pip进行安装,
    我们依然可以通过pip install pypiwin32来安装win32com模块
    或者下载安装包手动安装。
    """

     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

  • 相关阅读:
    Mac_Homebrew
    Python的路径引用
    OpenTSDB-Writing Data
    OpenTSDB介绍
    Git文件状态描述
    TCollector
    TEXT和BLOB区别
    MySQL索引与Index Condition Pushdown
    webService入门学习(一)
    redis学习笔记(一 基本操作)。
  • 原文地址:https://www.cnblogs.com/ujq3/p/9218798.html
Copyright © 2011-2022 走看看