首先也要调用插件的方式,来写采集数据插件,在src目录下创建一个插件 plugins ,然后在plugins下创建disk.py ,memory.py, network.py等等
src
plugins
在disk.py
#获取采集,执行采集命令是cmd 是统一的我直接写agent中进行调用 def get_disk(handler,hostname): return handler.cmd("dir",hostname)
agent代码
from .base import BaseHandler from src.plugins.disk import get_disk class AgnetHandler(BaseHandler): def cmd(self,command,hostname=None): import subprocess return subprocess.getoutput(command) def handler(self): """ Agent模式下处理资产采集:硬盘、内存、网卡 :return: """ print('agent模式') #调用pulugins.disk /plugins.momory /plugins.nerwork disk = get_disk(self) print(disk)
salt.py
from .base import BaseHandler class SaltHandler(BaseHandler): def cmd(self, command, hostname=None): """ 调用saltstack远程连接主机并执行命令(saltstack的master) :param hostname:主机名 :param command: 要执行的命令 :return: """ import salt.client #安装salt local = salt.client.LocalClient() result = local.cmd(hostname, 'cmd.run', [command]) return result[hostname] def handler(self): """ Salt模式下处理资产采集 :return: """ print('salt模式')
ssh.py
from .base import BaseHandler from config import settings class SSHHandler(BaseHandler): def cmd(self, command, hostname=None): """ 调用paramiko远程连接主机并执行命令,依赖rsa :param hostname:主机名 :param command: 要执行的命令 :return: """ import paramiko private_key = paramiko.RSAKey.from_private_key_file(settings.SSH_PRIVATE_KEY) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=hostname, port=settings.SSH_PORT, username=settings.SSH_USER, pkey=private_key) stdin, stdout, stderr = ssh.exec_command(command) result = stdout.read() ssh.close() return result def handler(self): """ SSH模式下处理资产采集 :return: """ print('SSH模式')
配置settings
### #######################SSH模式的配置########################## SSH_PRIVATE_KEY="私钥路径" SSH_USER='cmdb' #用户名 SSH_PORT='22' #端口
进行cmd 命令的约束
class BaseHandler(object): def cmd(self,command,hostname=None): ''' cmd约束派生类 :return: ''' raise NotImplementedError('cmd() must Implemented.') def handler(self): ''' handler约束派生类 :return: ''' raise NotImplementedError('handler() must Implemented.')
##########################用法升级,插件写法,反射使用 #################
写配置文件,
PLUGINS_DICT = {
'disk':'src.plugins.disk.Disk',
'memory':'src.plugins.memory.Memory',
'network':'src.plugins.network.Network',
'cpu':'src.plugins.cpu.CPU',
}
disk.py
class Disk(object): def process(self): ''' 执行命令拿到结果 :return: ''' return "硬盘"
memory.py
class Memory(object): def process(self): ''' 执行命令拿到结果 :return: ''' return "内存"
network.py
class Network(object): def process(self): ''' 执行命令拿到结果 :return: ''' return "网卡"
cpu.py
class CPU(object): def process(self): ''' 执行命令拿到结果 :return: ''' return "CPU"
这里调用之前写好的反射lib 中的方法
from config import settings from lib.module_srting import import_sting def get_server_info(): ''' 获取所有的资产信息并返回 :return: ''' info={} for name,path in settings.PLUGINS_DICT.items(): cls=import_sting(path) obj = cls() info[name] = obj.process() return info
执行 agent写下测试
############################下面采集真实数据测试
使用agent的方式采集进行本地采集
agent.py
from .base import BaseHandler from ..plugins import get_server_info class AgnetHandler(BaseHandler): def cmd(self,command,hostname=None): import subprocess return subprocess.getoutput(command) def handler(self): """ Agent模式下处理资产采集:硬盘、内存、网卡 :return: """ print('agent模式') #调用pulugins.disk /plugins.momory /plugins.nerwork ret = get_server_info(self) #把自己传进去,agent默认没有主机所有不用hostname print(ret)
plugins-->__init__.py 执行命令的handler
from config import settings from lib.module_srting import import_sting def get_server_info(handler,hostname=None): ''' 获取所有的资产信息并返回 :return: ''' info={} for name,path in settings.PLUGINS_DICT.items(): cls=import_sting(path) obj = cls() info[name] = obj.process(handler,hostname) return info
disk.py ,memory.py ,cpu.py ,network.py 这里都是测试截取10个字符
class Disk(object): def process(self,handler,hostname): ''' 执行命令拿到结果磁盘 :return: ''' ret = handler.cmd('wmic diskdrive',hostname)[0:10] return ret
class CPU(object): def process(self,handler,hostname): ''' 执行命令拿到结果cpu :return: ''' ret = handler.cmd('wmic cpu',hostname)[0:10] return ret
class Network(object): def process(self,handler,hostname): ''' 执行命令拿到结果网卡信息 :return: ''' ret = handler.cmd('ipconfig',hostname)[0:10] return ret
class Memory(object): def process(self,handler,hostname): ''' 执行命令拿到结果内存 测试截取10个字符 :return: ''' ret = handler.cmd('wmic memphysical list brief',hostname)[0:10] return ret