码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-12-27 00:17:56      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:raise   ima   shel   set   response   技术   plugin   imp   shell   

技术分享图片

# __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

标签:raise   ima   shel   set   response   技术   plugin   imp   shell   

原文地址:https://www.cnblogs.com/pymkl/p/8120698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!