def get_server_info(hostname=None): ''' 获取服务器信息, 并且将所有的数据进行打包。 :param hostname: # hostname 为空时,表示要通过agent方式获取。所以不需要主机名。 :return: ''' response = BasicPlugin(hostname).execute() # os_platform_str = response.data['os_platform'] # Linux or window or mac if not response.status: # 如果中途发生了错误,使status值改为false的话。 在这里就返回,不再执行 return response ''' PLUGINS_DICT = { k v 'cpu': 'src.plugins.cpu.CpuPlugin', 'disk': 'src.plugins.disk.DiskPlugin', 'main_board': 'src.plugins.main_board.MainBoardPlugin', 'memory': 'src.plugins.memory.MemoryPlugin', 'nic': 'src.plugins.nic.NicPlugin', } ''' # 循环导入配置文件中的需要获取的硬件信息。 并执行每个类的 execute for k, v in settings.PLUGINS_DICT.items(): # src.plugins.cpu CpuPlugin model_path, class_name = v.rsplit(".", maxsplit=1) cls = getattr(import_module(model_path), class_name) # 利用反射去找到 指定路径下的包, 包中的 类。 response.data[k] = cls(hostname).execute() # 得到想要的类实例化并执行 execute()。 结果保存到字典中。 键就是 k return response
def get_server_info(): """ 获取服务器基本信息 """ response = BasicPlugin().linux() if not response.status: return response for k, v in settings.PLUGINS_DICT.items(): module_path, cls_name = v.rsplit('.', 1) cls = getattr(importlib.import_module(module_path), cls_name) obj = cls().linux() response.data[k] = obj return response
def get_server_info(hostname=None): '''通过settings文件动态获取服务器的基本信息''' response = BasicPlugin(hostname).execute() if not response.status: return response for k, v in settings.PLUGINS_DICT.items(): module_path, cls_name = v.rsplit('.', 1) #动态去根据settings文件的字典找类 cls = getattr(importlib.import_module(module_path), cls_name) obj = cls(hostname).execute() response.data[k] = obj return response
def get_server_info(hostname=None): """ 获取服务器基本信息 :param hostname: agent模式时,hostname为空;salt或ssh模式时,hostname表示要连接的远程服务器 :return: """ response = BasicPlugin(hostname).execute() if not response.status: return response for k, v in settings.PLUGINS_DICT.items(): module_path, cls_name = v.rsplit('.', 1) cls = getattr(importlib.import_module(module_path), cls_name) obj = cls(hostname).execute() response.data[k] = obj return response
def get_server_info(hostname=None): """ 获取服务器基本信息 :param hostname: agent模式时,hostname为空;salt或ssh模式时,hostname表示要连接的远程服务器 :return: """ print(hostname) response = BasicPlugin(hostname).execute() # 获取基础数据, 系统平台、系统版本、主机名 if not response.status: return response for k, v in settings.PLUGINS_DICT.items(): module_path, cls_name = v.rsplit('.', 1) # python2.6 中没有 importlib 模块 __import__ 只能够导入目录,不能够导入文件, # 在plugins 目录平级的 __init__.py 文件中需要先导入一下各个插件的函数 cls = getattr(__import__(module_path), cls_name) obj = cls(hostname).execute() response.data[k] = obj return response
def get_server_info(manager_ip=None, info_dict=None): """ 获取服务器基本信息 :param manager_ip: 管理IP,Snmp模式要用到管理IP :return: """ # 获取基本信息 response = BasicPlugin(manager_ip, info_dict).execute() # 如果基本信息获取报错返回response if not response.status: return response # 如果采集类型为Agent if settings.MODE == 'agent': # 执行收集资产的各类插件 for k, v in settings.AGENT_PLUGINS_DICT.items(): module_path, cls_name = v.rsplit( '.', 1) # ['src.plugins.agent.disk', 'DiskPlugin'] cls = getattr(importlib.import_module(module_path), cls_name) res = cls(manager_ip).execute() response.data[k] = res # 如果采集类型为Snmp elif settings.MODE == 'snmp': # 执行收集资产的各类插件 if info_dict['manufacturer'] == 'dell': plugin_dict = settings.SNMP_DELL_PLUGINS_DICT[ info_dict['device_type']] for k, v in plugin_dict.items(): module_path, cls_name = v.rsplit('.', 1) cls = getattr(importlib.import_module(module_path), cls_name) res = cls(manager_ip, info_dict).execute() response.data[k] = res elif info_dict['manufacturer'] == 'h3c': plugin_dict = settings.SNMP_H3C_PLUGINS_DICT[ info_dict['device_type']] for k, v in plugin_dict.items(): module_path, cls_name = v.rsplit('.', 1) cls = getattr(importlib.import_module(module_path), cls_name) res = cls(manager_ip, info_dict).execute() response.data[k] = res elif info_dict['manufacturer'] == 'juniper': plugin_dict = settings.SNMP_JUNIPER_PLUGINS_DICT[ info_dict['device_type']] for k, v in plugin_dict.items(): module_path, cls_name = v.rsplit('.', 1) cls = getattr(importlib.import_module(module_path), cls_name) res = cls(manager_ip, info_dict).execute() response.data[k] = res return response