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