CMDB项目要点总结之中控机
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项目要点总结之中控机的更多相关文章
- CMDB项目要点之技术点(面试题)
1.单例模式 日志对象用单例模式 django admin中注册类是,用到单例模式 为什么要用单例模式 同一个对象操作 维护全局变量 + 对全局变量做一些操作 # __new__ import thr ...
- cmdb项目-1
1.什么是cmdb 配置管理数据库 ,存储基础设备的各种信息配置等 CMDB可以存储并自动发现整个IT网络上的各种信息,比如一个IT网络上有多少台服务器.多少存储.设备的品牌.资产编号.维护人员.所属 ...
- CMDB项目开发
CMDB介绍 CMDB --Configuration Management Database 配置管理数据库, CMDB存储与管理企业IT架构中设备的各种配置信息,它与所有服务支持和服务交付流程都紧 ...
- ct任务添加与中控机批量后台操作
ct 任务nohup sh ./bin/start.sh </dev/null >/dev/null 2>&1 & 中控机批量 for h in `get_hosts ...
- Python Django CMDB项目实战之-3创建form表单,并在前端页面上展示
基于之前的项目代码 Python Django CMDB项目实战之-1如何开启一个Django-并设置base页.index页.文章页面 Python Django CMDB项目实战之-2创建APP. ...
- Python Django CMDB项目实战之-2创建APP、建模(models.py)、数据库同步、高级URL、前端页面展示数据库中数据
基于之前的项目代码来编写 Python Django CMDB项目实战之-1如何开启一个Django-并设置base页index页文章页面 现在我们修改一个文章列表是从数据库中获取数据, 下面我们就需 ...
- Python Django CMDB项目实战之-1如何开启一个Django-并设置base页、index页、文章页面
1.环境 win10 python 2.7.14 django 1.8.2 需要用到的依赖包:MySQLdb(数据库的接口包).PIL/pillow(处理图片的包) 安装命令: pip install ...
- vue项目在安卓低版本机显示空白原因
vue项目在安卓低版本机显示空白原因: 可能的原因一: 查看安卓debug,报错,可能有箭头函数语法错误,或者其他语法问题,那可能是ES6语法问题. 这时候需要安装babel-pollyfill. 网 ...
- 关于如何获取项目所部署的本机IP和端口的问题
关于如何获取项目所部署的本机IP和端口的问题 今天在写一个需求的时候碰到一个不常见的问题,在没有继承或者实现服务器提供的接口或者实现类的时候,比如说部署在tomacat上,某个类不去继承servelt ...
随机推荐
- ssh配置方面小实验②
4.禁止使用密码登录当我们学会了使用密钥对进行验证后,建议生产环境下将账户密码登录功能关掉配置文件:/etc/ssh/sshd_config选项: PasswordAuthentication no ...
- 在kubernetes集群里集成Apollo配置中心(4)之dubbo服务提供者连接apollo实战
1.登录portal.od.com(Apollo-portal),新建一个dubbo-demo-service项目 2.在Apollo项目中的dubbo-demo-service添加配置 (1)添加d ...
- LCIS(最长公共上升子序列)模板
求出LCIS并输出其路径. 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #inc ...
- 80行Python代码搞定全国区划代码
微信搜索:码农StayUp 主页地址:https://gozhuyinglong.github.io 源码分享:https://github.com/gozhuyinglong/blog-demos ...
- Shell 编程快速上手
Shell 编程快速上手 test.sh #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do touch ...
- 使用 Promise 实现请求自动重试
使用 Promise 实现请求自动重试 "use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgqf ...
- Apple iPhone 12 Pro 数据迁移方式 All In One
Apple iPhone 12 Pro 数据迁移方式 All In One iPhone 12 Pro https://mp.weixin.qq.com/s/US1Z_69zVQIhV-cNW1E6A ...
- 如何在 macOS 上搭建 PHP 开发环境
如何在 macOS 上搭建 PHP 开发环境 Linux, Nginx, MySQL, PHP $ php --version $ php -v # PHP 7.3.11 (cli) (built: ...
- React & react-native & vue & cli & environment information & report bugs
React & react-native & vue & cli & environment information & report bugs cli che ...
- webpack 4 & dev server
webpack 4 & dev server proxy https://webpack.js.org/configuration/dev-server/#devserverproxy htt ...