1.基于paramiko对远程主机执行命令操作

秘钥形式


private_key = paramiko.RSAKey.from_private_key_file('c:/Users/用户名/.ssh/id_rsa') # 创建ssh对象
ssh = paramiko.SSHClient()
# 运行连接不在know_host文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='主机名', port='端口号', username='用户名', pkey=private_key)
# 执行命令
stdin, stdout, stderr = ssh.exec_command("命令")
# 获取结果
result = stdout.read() # 关闭连接
ssh.close()

2.线程池提高效率

from concurrent.futures import ThreadPoolExecutor
# 创建一个十个线程的线程池
def task(i):
print("我是任务%s" % i)
pool = ThreadPoolExecutor(10)
for i in range(10):
pool.submit(task,i)

3.基于logging模块进行日志的提取

import logging
import traceback
# import settings class Logger(object):
def __init__(self, file_path, level):
file_handler = logging.FileHandler(file_path, 'a', encoding='utf-8')
fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s: %(message)s")
file_handler.setFormatter(fmt) self.logger = logging.Logger('cmdb', level=level)
self.logger.addHandler(file_handler) def error(self, msg):
self.logger.error(msg) logger = Logger("你的日志存放路径", logging.DEBUG) logger.error(traceback.format_exc()) # 获取异常堆栈信息

4.基于类的封装对数据进行封装

class BaseResponse(object):
def __init__(self, status=True, data=None, error=None):
self.status = status
self.data = data
self.error = error @property # 作用:让调用着不用加()
def dict(self):
return self.__dict__

5.基于工厂模式和反射对资产数据进行获取

def get_server_info(ssh, hostname):
server_info = {}
for key, path in settings.PLUGIN_CLASS_DICT.items():
module_path, class_name = path.rsplit('.', maxsplit=1)
try:
module = importlib.import_module(module_path)
except Exception as e:
print(e)
cls = getattr(module, class_name)
plugin_obj = cls()
info = plugin_obj.process(ssh, hostname)
server_info[key] = info
return server_info
# settings.py
# 获取计算机信息配置
PLUGIN_CLASS_DICT = {
'basic': 'lib.plugin.basic.BasicPlugin',
'disk': 'lib.plugin.disk.DiskPlugin',
'memory': 'lib.plugin.memory.MemoryPlugin',
'network': 'lib.plugin.network.NetworkPlugin',
}
# disk.py
import traceback
from lib.utils.log import logger
from lib.utils.response import BaseResponse class DiskPlugin(BasePlugin):
"""
获取磁盘信息
"""
def process(self, ssh, hostname):
result = BaseResponse()
try:
# output = ssh(hostname, 'MegaCli -PDList -aALL')
output = open('files/disk.out').read()
result.data = output
output.close()
except Exception as e:
logger.error(traceback.format_exc())
result.status = False
result.error = traceback.format_exc()
return result.dict

6.采集资产用到的命令

cpu:
cat /proc/cpuinfo
主板:
dmidecode -t1
内存:
dmidecode -q -t 17 2>/dev/null
网卡(salt源码):
ip link show
ip addr show
硬盘:
MegaCli -PDList -aALL

CMDB项目要点总结之中控机的更多相关文章

  1. CMDB项目要点之技术点(面试题)

    1.单例模式 日志对象用单例模式 django admin中注册类是,用到单例模式 为什么要用单例模式 同一个对象操作 维护全局变量 + 对全局变量做一些操作 # __new__ import thr ...

  2. cmdb项目-1

    1.什么是cmdb 配置管理数据库 ,存储基础设备的各种信息配置等 CMDB可以存储并自动发现整个IT网络上的各种信息,比如一个IT网络上有多少台服务器.多少存储.设备的品牌.资产编号.维护人员.所属 ...

  3. CMDB项目开发

    CMDB介绍 CMDB --Configuration Management Database 配置管理数据库, CMDB存储与管理企业IT架构中设备的各种配置信息,它与所有服务支持和服务交付流程都紧 ...

  4. ct任务添加与中控机批量后台操作

    ct 任务nohup sh ./bin/start.sh </dev/null >/dev/null 2>&1 & 中控机批量 for h in `get_hosts ...

  5. Python Django CMDB项目实战之-3创建form表单,并在前端页面上展示

    基于之前的项目代码 Python Django CMDB项目实战之-1如何开启一个Django-并设置base页.index页.文章页面 Python Django CMDB项目实战之-2创建APP. ...

  6. Python Django CMDB项目实战之-2创建APP、建模(models.py)、数据库同步、高级URL、前端页面展示数据库中数据

    基于之前的项目代码来编写 Python Django CMDB项目实战之-1如何开启一个Django-并设置base页index页文章页面 现在我们修改一个文章列表是从数据库中获取数据, 下面我们就需 ...

  7. Python Django CMDB项目实战之-1如何开启一个Django-并设置base页、index页、文章页面

    1.环境 win10 python 2.7.14 django 1.8.2 需要用到的依赖包:MySQLdb(数据库的接口包).PIL/pillow(处理图片的包) 安装命令: pip install ...

  8. vue项目在安卓低版本机显示空白原因

    vue项目在安卓低版本机显示空白原因: 可能的原因一: 查看安卓debug,报错,可能有箭头函数语法错误,或者其他语法问题,那可能是ES6语法问题. 这时候需要安装babel-pollyfill. 网 ...

  9. 关于如何获取项目所部署的本机IP和端口的问题

    关于如何获取项目所部署的本机IP和端口的问题 今天在写一个需求的时候碰到一个不常见的问题,在没有继承或者实现服务器提供的接口或者实现类的时候,比如说部署在tomacat上,某个类不去继承servelt ...

随机推荐

  1. 关于malloc/free用法

    和很多人一样,我一直觉得new/delete和malloc/free的用法很随意,直到我真正遇到了麻烦,才想着去好好区分一下. (1)首先mallo函数原型void* malloc(size_t).头 ...

  2. Doris开发手记1:解决蛋疼的MySQL 8.0连接问题

    笔者作为Apache Doris的开发者,平时感觉相关Doris的文章写的很少.主要是很多时候不知道应该去记录一些怎么样的问题,感觉写的不好就会很慌张.新的一年,希望记录自己在Doris开发过程之中所 ...

  3. Linux Bash Script loop

    Linux Bash Script loop shell 编程之流程控制 for 循环.while 循环和 until 循环 for var in item1 item2 ... itemN do c ...

  4. 1 line of CSS Layouts

    1 line of CSS Layouts 10 modern layouts in 1 line of CSS 1. 绝对居中布局 <div class="container&quo ...

  5. short URL 短网址实现原理剖析

    short URL 短网址实现原理剖析 意义,简短便于分享,避免出现超长 URL 的字符长度限制问题 原理分析, 使用 HashMap 存储对应的映射关系 (长度不超过7的字符串,由大小写字母加数字共 ...

  6. HTML5 动效

    HTML5 动效 motion graphics toolbelt for the web https://github.com/xgqfrms/mojs A collection of loadin ...

  7. LGTM & code review

    LGTM & code review LGTM is an acronym meaning looks good to me, frequently used when reviewing d ...

  8. clientHeight & offsetHeight & scrollHeight

    clientHeight & offsetHeight & scrollHeight scrollWidth/scrollHeight,offsetWidth/offsetHeight ...

  9. js form.onformData事件

    在表单提交前修改数据,此事件在submit之后 f1.addEventListener("formdata", (e) => { e.formData.append(&quo ...

  10. nasm astrchr函数 x86

    xxx.asm: %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export as ...