def init_plugins(self, device_name, device): '''init_plugins initializing plugins for device ''' logger.info('Initializing plugins for %s' % device_name) for plugin_name, plugin_cache in self._plugins.items(): devices = plugin_cache.get('devices', []) if devices and device_name not in devices: logger.debug('Skipping plugin %s for device %s' % (plugin_name, device_name)) continue self._cache[plugin_name].setdefault(device_name, {}) argv = copy(sys.argv[1:]) plugin = self.load_plugin(device, **plugin_cache) self._cache[plugin_name][device_name]['instance'] = plugin # Add configuration YAML plugin arguments to argv for parsing for key, value in plugin_cache['plugin_arguments'].items(): # if key already exists in argv (provided to easypy as --key) # then argv takes priority over YAML value if key not in str(argv): argv.extend(('--{key}'.format(key=key), str(value))) # parse plugin arguments # ---------------------- # (saves arguments to plugin.args) plugin.parse_args(argv) self._cache[plugin_name][device_name]['args'] = plugin.args plugin_cache.setdefault('plugin_label', get_plugin_name(plugin))
def call_plugin(self, device, plugins): is_connected = self.is_connected(device.name, device) if not is_connected: connection = self.connections.get(device.name, {}) timeout = connection.pop('timeout', self.connection_timeout) logger.info('Lost Connection - Attempt to Recover Connection ' 'with Device ({})'.format(device.name)) # best effort, attempt to connect at least once. try: device.connect(timeout=timeout, **connection) except Exception as e: connection_failed = ('Lost Connection, failed to ' 'recover. exception: ({})'.format(str(e))) else: connection_failed = 'Lost Connection, failed to recover' is_connected = self.is_connected(device.name, device) logger.info('Connection Re-Established for ' 'Device ({})'.format(device.name)) results = dict() for plugin in plugins: # skip plugin execution if device isn't connected if is_connected: result = super().call_plugin(device, [plugin]) else: # bad connection result = dict() plugin_name = get_plugin_name(plugin) execution = result.setdefault(plugin_name, {}).setdefault(device.name, {}) execution['status'] = CRITICAL execution['result'] = { datetime.utcnow().isoformat(): connection_failed } recursive_update(results, result) if hasattr(self.instance, 'post_call_plugin'): self.instance.post_call_plugin(device, result) return results
def call_plugin(self, device, plugins): plugin_result = dict() for plugin in plugins: results = dict() plugin_name = get_plugin_name(plugin) execution = results.setdefault(plugin_name, {}).setdefault(device.name, {}) logger.info(banner("Starting Telemetry task '{}' on device '{}'".\ format(plugin_name, device.name))) try: call_result = plugin.execution(device) except Exception as e: status = ERRORED result = { datetime.utcnow().isoformat(): str(e) } else: status = call_result result = getattr(call_result, 'meta', {}) # Example # {'crashdumps':{'N95_2':{'status': 'ok', # 'result': # { '2018-03-08T17:02:27.837458Z': # '***** No patterns matched *****'} # }} execution['status'] = status execution['result'] = result recursive_update(plugin_result, results) return plugin_result
def get_device_plugins_status(self, device, label=False): '''get_device_plugins_status get all plugin status of given device Argument: - label: Flag, if true, return status_label instead of status ''' device = getattr(device, 'name', device) statuses = dict() for devices in self._cache.values(): dev = devices.get(device, {}) # the device isn't cached, skip if not dev: continue plugin = dev.get('instance', None) if label: status = dev.get('status_label', 'STATUS NOT AVAILABLE') else: status = dev.get('status', None) statuses[get_plugin_name(plugin)] = status return statuses
def set_device_plugin_status(self, device, plugin, status): plugin_name = get_plugin_name(plugin) return self.plugins.set_device_plugin_status(device.name, plugin_name, status)