02-CMDB采集资产功能插件开发 | CMDB

Posted pymkl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了02-CMDB采集资产功能插件开发 | CMDB相关的知识,希望对你有一定的参考价值。

技术分享图片

# __init__.py
from conf import settings

def pack():
    response = {}
    for k,v in settings.PLUGINS.items():
        import importlib
        m_path,classname = v.rsplit(.,maxsplit=1)
        m = importlib.import_module(m_path)
        cls = getattr(m,classname)
        response[k] = cls().execute()
    return response
# base.py
from conf import settings

class BasePlugin(object):

    def __init__(self):
        mode_list = [SSH,Salt,"Agent"]
        if settings.MODE in mode_list:
            self.mode = settings.MODE
        else:
            raise Exception(配置文件错误)

    def ssh(self,cmd):
        pass

    def agent(self,cmd):
        pass

    def salt(self,cmd):
        pass

    def shell_cmd(self,cmd):
        if self.mode == SSH:
            ret = self.ssh(cmd)
        elif self.mode == Salt:
            ret = self.salt(cmd)
        else:
            ret = self.agent(cmd)
        return ret

    def execute(self):

        ret = self.shell_cmd(查看平台命令)

        if ret == win:
            return self.windows()
        elif ret == linux:
            return self.linux()
        else:
            raise Exception(只支持windows和linux)

    def linux(self):
        raise Exception(....)

    def windows(self):
        raise Exception(....)
# disk.py
from .base import BasePlugin

class DiskPlugin(BasePlugin):

    def windows(self):
        output = self.shell_cmd(ipconfig)
        return output

    def linux(self):
        output = self.shell_cmd(ifconfig)
        return output
# mem.py
from .base import BasePlugin

class MemPlugin(BasePlugin):

    def windows(self):
        output = self.shell_cmd(asdf)
        # 正则表达式
        return output

    def linux(self):
        output = self.shell_cmd(asdf)
        # 正则表达式
        return output
# nic.py
from .base import BasePlugin

class NicPlugin(BasePlugin):

    def windows(self):
        output = self.shell_cmd(asdf)
        # 正则表达式
        return output

    def linux(self):
        output = self.shell_cmd(asdf)
        # 正则表达式
        return output
# settings.py
MODE = "Agent" # Salt,SSH

 

以上是关于02-CMDB采集资产功能插件开发 | CMDB的主要内容,如果未能解决你的问题,请参考以下文章

CMDB资产采集插件开发

LuffyCity-CMDB实战

CMDB 资产采集——插件可插拔式可拓展思想

CMDB资产采集功能实现之saltstack

CMDB资产采集功能实现之agent

CMDB资产采集功能实现之ssh