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



AutoClient



#settings.py
# ————————01CMDB获取服务器基本信息————————
import os BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))##当前路径 # 采集资产的方式,选项有:agent(默认), salt, ssh
MODE = 'agent' # ————————01CMDB获取服务器基本信息————————
#settings.py

#base.py
# ————————01CMDB获取服务器基本信息————————
from config import settings #配置文件 class BasePlugin(object):
def __init__(self, hostname=''):
if hasattr(settings, 'MODE'):
self.mode = settings.MODE #采集资产的方式
else:
self.mode = 'agent'#默认,采集资产的方式 def execute(self):
return self.windows() def windows(self):
raise Exception('您必须实现windows的方法')
# ————————01CMDB获取服务器基本信息————————
#base.py

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

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

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

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

#__init__.py
# ————————01CMDB获取服务器基本信息————————
from src.plugins.basic import BasicPlugin def get_server_info(hostname=None):
"""
获取服务器基本信息
:param hostname: agent模式时,hostname为空;salt或ssh模式时,hostname表示要连接的远程服务器
:return:
"""
response = BasicPlugin(hostname).execute()#获取基本信息
"""
class BaseResponse(object):
def __init__(self):
self.status = True
self.message = None
self.data = None
self.error = None
"""
return response if __name__ == '__main__':
ret = get_server_info()
# ————————01CMDB获取服务器基本信息————————
#__init__.py

# basic.py
# ————————01CMDB获取服务器基本信息————————
from .base import BasePlugin #采集资产的方式
from lib.response import BaseResponse #提交数据的类型
import platform #platform模块给我们提供了很多方法去获取操作系统的信息
import wmi#Windows操作系统上管理数据和操作的基础设施
"""
本模块基于windows操作系统,依赖wmi和win32com库,需要提前使用pip进行安装,
我们依然可以通过pip install pypiwin32来安装win32com模块
或者下载安装包手动安装。
""" class BasicPlugin(BasePlugin):
def os_platform(self):#获取系统平台
output=platform.system()
return output.strip()#strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
def os_version(self):#获取系统版本
output = wmi.WMI().Win32_OperatingSystem()[0].Caption
return output.strip()#strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
def os_hostname(self):#获取主机名
output = wmi.WMI().Win32_OperatingSystem()[0].CSName
return output.strip()#strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。 def windows(self):
response = BaseResponse()#提交数据的类型
try:
ret = {
'os_platform': self.os_platform(),#系统平台
'os_version': self.os_version(),#系统版本
'hostname': self.os_hostname(),#主机名
}
response.data = ret #字典形式
print('windows服务器基本信息:',response.data)
except Exception as e:
response.status = False#获取信息时出现错误
return response
"""
class BaseResponse(object): #提交数据的类型
def __init__(self):
self.status = True #状态
self.message = None #消息
self.data = None #数据内容
self.error = None #错误信息 """
# ————————01CMDB获取服务器基本信息————————
# basic.py
import wmi
"""
本模块基于windows操作系统,依赖wmi和win32com库,需要提前使用pip进行安装,
我们依然可以通过pip install pypiwin32来安装win32com模块
或者下载安装包手动安装。
"""

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




Django项目:CMDB(服务器硬件资产自动采集系统)--01--01CMDB获取服务器基本信息的更多相关文章
- Django项目:CMDB(服务器硬件资产自动采集系统)--06--06CMDB测试Linux系统采集硬件数据的命令01
#base.py # ————————01CMDB获取服务器基本信息———————— from config import settings #配置文件 class BasePlugin(object ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--03--03CMDB信息安全API接口交互认证
#settings.py """ Django settings for AutoCmdb project. Generated by 'django-admin sta ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--02--02CMDB将服务器基本信息提交到API接口
AutoCmdb # urls.py """AutoCmdb URL Configuration The `urlpatterns` list routes URLs t ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--12--08CMDB采集硬件数据日志记录
#settings.py # ————————01CMDB获取服务器基本信息———————— import os BASEDIR = os.path.dirname(os.path.dirname(o ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--11--07CMDB文件模式测试采集硬件数据
#settings.py # ————————01CMDB获取服务器基本信息———————— import os BASEDIR = os.path.dirname(os.path.dirname(o ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--05--05CMDB采集硬件数据的插件
#__init__.py # ————————05CMDB采集硬件数据的插件———————— from config import settings import importlib # —————— ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--04--04CMDB本地(Agent)模式客户端唯一标识(ID)
# client.py # ————————01CMDB获取服务器基本信息———————— from src import plugins #__init__.py from lib.serializ ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--07--06CMDB测试Linux系统采集硬件数据的命令02
#settings.py """ Django settings for AutoCmdb project. Generated by 'django-admin sta ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--10--06CMDB测试Linux系统采集硬件数据的命令05
cd /py/AutoClient/bin python3 auto-client.py /usr/local/python3/bin/pip install requests python3 aut ...
随机推荐
- day10 nfs服务,nginx负载均衡,定时任务
==================nginx 负载均衡==================== 实现nginx负载均衡的效果,并运用nfs服务共享目录,使所有nginx服务拥有共同的http目录 n ...
- csps模拟8990部分题解
题面: 666: 重点在题意转化:每个数可以乘k,代价为k,可以减一,代价为1, 所以跑最短路即可 #include<iostream> #include<cstdio> #i ...
- C#获取当前运行的源代码的文件名和当前源代码的行数的方法
在C#中记录日志时,为了以后查找错误或者跟踪的方便,最好能记录下出错的源代码的文件名和出错的源代码的行数. 这2个方法如下: /// <summary> /// 取得当前源 ...
- Python 日期和时间_python 当前日期时间_python日期格式化
Python 日期和时间_python 当前日期时间_python日期格式化 Python程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 cal ...
- Day 8 : Python 文档操作
Python 文件的操作方法: 打开文件 f = open('test','r',encoding='utf-8') #f :文件句柄 #test:文件绝对路径 #r:打开方式 #encoding 打 ...
- threading线程中的方法(27-11)
t1.start() # 执行线程 t1.join() # 阻塞 t1.setDaemon(True) #守护线程 threading.current_thread() # 查看执行的是哪一个线程 t ...
- 从虚拟机视角谈 Java 应用性能优化
从虚拟机视角谈 Java 应用性能优化 周 祥, 软件工程师, IBM 简介:Java 的普及和广泛应用,以及其基于虚拟机运行的机制,使得性能问题越来越重要.本文从 Java 虚拟机的角度,特别是垃圾 ...
- CentOS6.3搭建ZooKeeper伪集群
1. 将zookeeper安装包移动至/home, 解压后改名为zookeeper 相关命令 # 解压 .tar.gz # 重命名 zookeeper 2. 进入zookeeper/conf/目录下, ...
- pure-Python PDF library
# -*- coding: utf-8 -*- # # vim: sw=4:expandtab:foldmethod=marker # # Copyright (c) 2006, Mathieu Fe ...
- JavaSE_12_序列化流和打印流
1.1 序列化流概述 Java 提供了一种对象序列化的机制.用一个字节序列可以表示一个对象,该字节序列包含该对象的数据.对象的类型和对象中存储的属性等信息.字节序列写出到文件之后,相当于文件中持久保存 ...