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

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

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

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

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

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

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

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