硬件信息采集系统
硬件信息采集系统:
1、目录结构如下:

2、思维导图如下

3、完整代码如下
1 # -*- coding: utf-8 -*- 2 # __author__ = "maple" 3 import sys 4 import os 5 6 BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 7 sys.path.append(BASEDIR) 8 os.environ['CLIENT_SETTINGS'] = 'conf.settings' 9 10 if __name__ == '__main__': 11 from src.plugins import PluginManager 12 13 obj = PluginManager() 14 server_dict = obj.exec_plugin() 15 print(server_dict)
1 # -*- coding: utf-8 -*- 2 # __author__ = "maple" 3 PLUGIN_ITEMS = { 4 "basic":"src.plugins.basic.Basic", 5 "nic": "src.plugins.nic.Nic", 6 "disk": "src.plugins.disk.Disk", 7 } 8 9 API = 'http://127.0.0.1:8000/api/server.html' 10 11 DEBUG = True 12 13 MODE = 'AGENT' # AGENT/SSH/SALT 14 15 16 HOSTNAME = None 17 PORT = 22 18 SSH_USERNAME = None 19 SSH_PWD = None
1 # -*- coding: utf-8 -*- 2 # __author__ = "maple" 3 from . import global_settings 4 import os 5 import importlib 6 7 8 class Settings(object): 9 """ 10 global_setting,配置获取 11 settings.py,配置获取 12 13 """ 14 15 def __init__(self): 16 for item in dir(global_settings): 17 if item.isupper(): 18 k = item 19 v = getattr(global_settings, item) 20 setattr(self, k, v) 21 22 setting_path = os.environ.get('CLIENT_SETTINGS') 23 md_settings = importlib.import_module(setting_path) 24 for item in dir(md_settings): 25 if item.isupper(): 26 k = item 27 v = getattr(md_settings, item) 28 setattr(self, k, v) 29 30 31 settings = Settings()
1 # -*- coding: utf-8 -*- 2 # __author__ = "maple" 3 4 TEST = True 5 NAME = 'aasdaaa'
1 # -*- coding: utf-8 -*- 2 # __author__ = "maple" 3 import importlib 4 from lib.config import settings 5 6 7 class PluginManager(object): 8 def __init__(self): 9 pass 10 11 def exec_plugin(self): 12 server_info = {} 13 for k, v in settings.PLUGIN_ITEMS.items(): 14 # 找到v字符串:src.plugins.nic.Nic,src.plugins.disk.Disk 15 module_path, cls_name = v.rsplit('.', maxsplit=1) 16 module = importlib.import_module(module_path) 17 cls = getattr(module, cls_name) 18 19 if hasattr(cls, 'initial'): 20 obj = cls.initial() 21 else: 22 obj = cls() 23 ret = obj.process() 24 25 server_info[k] = ret 26 return server_info
1 # -*- coding: utf-8 -*- 2 # __author__ = "maple" 3 from lib.config import settings 4 5 6 class BasePlugin: 7 def __init__(self): 8 self.hostname = settings.HOSTNAME 9 self.port = settings.PORT 10 self.user = settings.SSH_USERNAME 11 self.pwd = settings.SSH_PWD 12 13 def exec_cmd(self, cmd): 14 if settings.MODE == 'AGENT': 15 import subprocess 16 result = subprocess.getoutput(cmd) 17 elif settings.MODE == 'SSH': 18 import paramiko 19 ssh = paramiko.SSHClient() 20 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 21 ssh.connect(hostname=self.hostname, port=self.port, username=self.user, password=self.pwd) 22 stdin, stdout, stderr = ssh.exec_command(cmd) 23 result = stdout.read() 24 ssh.close() 25 26 elif settings.MODE == "SALT": 27 import subprocess 28 result = subprocess.getoutput('salt "c1.com" cmd.run "%s"' % cmd) 29 else: 30 raise Exception("模式选择错误:AGENT,SSH,SALT") 31 32 return result
1 # -*- coding: utf-8 -*- 2 # __author__ = "maple" 3 from .base import BasePlugin 4 5 6 class Basic(BasePlugin): 7 @classmethod 8 def initial(cls): 9 return cls() 10 11 def process(self): 12 result = self.exec_cmd("dir") 13 return result

浙公网安备 33010602011771号